@@ -2,8 +2,8 @@ package tests
22
33import (
44 "bytes"
5+ "errors"
56 "fmt"
6- "math/big"
77 "runtime"
88
99 "github.com/ethereum/go-ethereum/common"
@@ -49,93 +49,112 @@ func RunTransactionTests(file string, notWorking map[string]bool) error {
4949}
5050
5151func runTest (txTest TransactionTest ) (err error ) {
52- expectedSender , expectedTo , expectedData , rlpBytes , expectedGasLimit , expectedGasPrice , expectedValue , expectedR , expectedS , expectedNonce , expectedV , err := convertTestTypes (txTest )
52+ tx := new (types.Transaction )
53+ err = rlp .DecodeBytes (mustConvertBytes (txTest .Rlp ), tx )
5354
5455 if err != nil {
55- if txTest .Sender == "" { // tx is invalid and this is expected (test OK)
56+ if txTest .Sender == "" {
57+ // RLP decoding failed and this is expected (test OK)
5658 return nil
5759 } else {
58- return err // tx is invalid and this is NOT expected (test FAIL)
60+ // RLP decoding failed but is expected to succeed (test FAIL)
61+ return errors .New ("RLP decoding failed when expected to succeed" )
5962 }
6063 }
61- tx := new (types.Transaction )
62- rlp .DecodeBytes (rlpBytes , tx )
63- //fmt.Println("HURR tx: %v", tx)
64- sender , err := tx .From ()
64+
65+ validationError := verifyTxFields (txTest , tx )
66+ if txTest .Sender == "" {
67+ if validationError != nil {
68+ // RLP decoding works but validation should fail (test OK)
69+ return nil
70+ } else {
71+ // RLP decoding works but validation should fail (test FAIL)
72+ // (this should not be possible but added here for completeness)
73+ return errors .New ("Field validations succeeded but should fail" )
74+ }
75+ }
76+
77+ if txTest .Sender != "" {
78+ if validationError == nil {
79+ // RLP decoding works and validations pass (test OK)
80+ return nil
81+ } else {
82+ // RLP decoding works and validations pass (test FAIL)
83+ return errors .New ("Field validations failed after RLP decoding" )
84+ }
85+ }
86+ return errors .New ("Should not happen: verify RLP decoding and field validation" )
87+ }
88+
89+ func verifyTxFields (txTest TransactionTest , decodedTx * types.Transaction ) (err error ) {
90+ defer func () {
91+ if recovered := recover (); recovered != nil {
92+ buf := make ([]byte , 64 << 10 )
93+ buf = buf [:runtime .Stack (buf , false )]
94+ err = fmt .Errorf ("%v\n %s" , recovered , buf )
95+ }
96+ }()
97+
98+ decodedSender , err := decodedTx .From ()
6599 if err != nil {
66100 return err
67101 }
68102
69- if expectedSender != sender {
70- return fmt .Errorf ("Sender mismatch: %v %v" , expectedSender , sender )
103+ expectedSender := mustConvertAddress (txTest .Sender )
104+ if expectedSender != decodedSender {
105+ return fmt .Errorf ("Sender mismatch: %v %v" , expectedSender , decodedSender )
71106 }
72- if ! bytes .Equal (expectedData , tx .Payload ) {
73- return fmt .Errorf ("Tx input data mismatch: %#v %#v" , expectedData , tx .Payload )
107+
108+ expectedData := mustConvertBytes (txTest .Transaction .Data )
109+ if ! bytes .Equal (expectedData , decodedTx .Payload ) {
110+ return fmt .Errorf ("Tx input data mismatch: %#v %#v" , expectedData , decodedTx .Payload )
74111 }
75- if expectedGasLimit .Cmp (tx .GasLimit ) != 0 {
76- return fmt .Errorf ("GasLimit mismatch: %v %v" , expectedGasLimit , tx .GasLimit )
112+
113+ expectedGasLimit := mustConvertBigInt (txTest .Transaction .GasLimit , 16 )
114+ if expectedGasLimit .Cmp (decodedTx .GasLimit ) != 0 {
115+ return fmt .Errorf ("GasLimit mismatch: %v %v" , expectedGasLimit , decodedTx .GasLimit )
77116 }
78- if expectedGasPrice .Cmp (tx .Price ) != 0 {
79- return fmt .Errorf ("GasPrice mismatch: %v %v" , expectedGasPrice , tx .Price )
117+
118+ expectedGasPrice := mustConvertBigInt (txTest .Transaction .GasPrice , 16 )
119+ if expectedGasPrice .Cmp (decodedTx .Price ) != 0 {
120+ return fmt .Errorf ("GasPrice mismatch: %v %v" , expectedGasPrice , decodedTx .Price )
80121 }
81- if expectedNonce != tx .AccountNonce {
82- return fmt .Errorf ("Nonce mismatch: %v %v" , expectedNonce , tx .AccountNonce )
122+
123+ expectedNonce := mustConvertUint (txTest .Transaction .Nonce , 16 )
124+ if expectedNonce != decodedTx .AccountNonce {
125+ return fmt .Errorf ("Nonce mismatch: %v %v" , expectedNonce , decodedTx .AccountNonce )
83126 }
84- if expectedR .Cmp (tx .R ) != 0 {
85- return fmt .Errorf ("R mismatch: %v %v" , expectedR , tx .R )
127+
128+ expectedR := common .Bytes2Big (mustConvertBytes (txTest .Transaction .R ))
129+ if expectedR .Cmp (decodedTx .R ) != 0 {
130+ return fmt .Errorf ("R mismatch: %v %v" , expectedR , decodedTx .R )
86131 }
87- if expectedS .Cmp (tx .S ) != 0 {
88- return fmt .Errorf ("S mismatch: %v %v" , expectedS , tx .S )
132+
133+ expectedS := common .Bytes2Big (mustConvertBytes (txTest .Transaction .S ))
134+ if expectedS .Cmp (decodedTx .S ) != 0 {
135+ return fmt .Errorf ("S mismatch: %v %v" , expectedS , decodedTx .S )
89136 }
90- if expectedV != uint64 (tx .V ) {
91- return fmt .Errorf ("V mismatch: %v %v" , expectedV , uint64 (tx .V ))
137+
138+ expectedV := mustConvertUint (txTest .Transaction .V , 16 )
139+ if expectedV != uint64 (decodedTx .V ) {
140+ return fmt .Errorf ("V mismatch: %v %v" , expectedV , uint64 (decodedTx .V ))
92141 }
93- if tx .Recipient == nil {
142+
143+ expectedTo := mustConvertAddress (txTest .Transaction .To )
144+ if decodedTx .Recipient == nil {
94145 if expectedTo != common .BytesToAddress ([]byte {}) { // "empty" or "zero" address
95146 return fmt .Errorf ("To mismatch when recipient is nil (contract creation): %v" , expectedTo )
96147 }
97148 } else {
98- if expectedTo != * tx .Recipient {
99- return fmt .Errorf ("To mismatch: %v %v" , expectedTo , * tx .Recipient )
149+ if expectedTo != * decodedTx .Recipient {
150+ return fmt .Errorf ("To mismatch: %v %v" , expectedTo , * decodedTx .Recipient )
100151 }
101152 }
102153
103- if expectedValue .Cmp (tx .Amount ) != 0 {
104- return fmt .Errorf ("Value mismatch: %v %v" , expectedValue , tx .Amount )
154+ expectedValue := mustConvertBigInt (txTest .Transaction .Value , 16 )
155+ if expectedValue .Cmp (decodedTx .Amount ) != 0 {
156+ return fmt .Errorf ("Value mismatch: %v %v" , expectedValue , decodedTx .Amount )
105157 }
106158
107159 return nil
108160}
109-
110- func convertTestTypes (txTest TransactionTest ) (sender , to common.Address ,
111- txInputData , rlpBytes []byte ,
112- gasLimit , gasPrice , value , r , s * big.Int ,
113- nonce , v uint64 ,
114- err error ) {
115-
116- defer func () {
117- if recovered := recover (); recovered != nil {
118- buf := make ([]byte , 64 << 10 )
119- buf = buf [:runtime .Stack (buf , false )]
120- err = fmt .Errorf ("%v\n %s" , recovered , buf )
121- }
122- }()
123-
124- sender = mustConvertAddress (txTest .Sender )
125- to = mustConvertAddress (txTest .Transaction .To )
126-
127- txInputData = mustConvertBytes (txTest .Transaction .Data )
128- rlpBytes = mustConvertBytes (txTest .Rlp )
129-
130- gasLimit = mustConvertBigInt (txTest .Transaction .GasLimit , 16 )
131- gasPrice = mustConvertBigInt (txTest .Transaction .GasPrice , 16 )
132- value = mustConvertBigInt (txTest .Transaction .Value , 16 )
133-
134- r = common .Bytes2Big (mustConvertBytes (txTest .Transaction .R ))
135- s = common .Bytes2Big (mustConvertBytes (txTest .Transaction .S ))
136-
137- nonce = mustConvertUint (txTest .Transaction .Nonce , 16 )
138- v = mustConvertUint (txTest .Transaction .V , 16 )
139-
140- return sender , to , txInputData , rlpBytes , gasLimit , gasPrice , value , r , s , nonce , v , nil
141- }
0 commit comments