github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/tests/transaction_test_util.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package tests 18 19 import ( 20 "bytes" 21 "errors" 22 "fmt" 23 "io" 24 "runtime" 25 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/core/types" 28 "github.com/ethereum/go-ethereum/logger/glog" 29 "github.com/ethereum/go-ethereum/rlp" 30 ) 31 32 // Transaction Test JSON Format 33 type TtTransaction struct { 34 Data string 35 GasLimit string 36 GasPrice string 37 Nonce string 38 R string 39 S string 40 To string 41 V string 42 Value string 43 } 44 45 type TransactionTest struct { 46 Rlp string 47 Sender string 48 Transaction TtTransaction 49 } 50 51 func RunTransactionTestsWithReader(r io.Reader, skipTests []string) error { 52 skipTest := make(map[string]bool, len(skipTests)) 53 for _, name := range skipTests { 54 skipTest[name] = true 55 } 56 57 bt := make(map[string]TransactionTest) 58 if err := readJson(r, &bt); err != nil { 59 return err 60 } 61 62 for name, test := range bt { 63 // if the test should be skipped, return 64 if skipTest[name] { 65 glog.Infoln("Skipping transaction test", name) 66 return nil 67 } 68 // test the block 69 if err := runTransactionTest(test); err != nil { 70 return err 71 } 72 glog.Infoln("Transaction test passed: ", name) 73 74 } 75 return nil 76 } 77 78 func RunTransactionTests(file string, skipTests []string) error { 79 tests := make(map[string]TransactionTest) 80 if err := readJsonFile(file, &tests); err != nil { 81 return err 82 } 83 84 if err := runTransactionTests(tests, skipTests); err != nil { 85 return err 86 } 87 return nil 88 } 89 90 func runTransactionTests(tests map[string]TransactionTest, skipTests []string) error { 91 skipTest := make(map[string]bool, len(skipTests)) 92 for _, name := range skipTests { 93 skipTest[name] = true 94 } 95 96 for name, test := range tests { 97 // if the test should be skipped, return 98 if skipTest[name] { 99 glog.Infoln("Skipping transaction test", name) 100 return nil 101 } 102 103 // test the block 104 if err := runTransactionTest(test); err != nil { 105 return err 106 } 107 glog.Infoln("Transaction test passed: ", name) 108 109 } 110 return nil 111 } 112 113 func runTransactionTest(txTest TransactionTest) (err error) { 114 tx := new(types.Transaction) 115 err = rlp.DecodeBytes(mustConvertBytes(txTest.Rlp), tx) 116 117 if err != nil { 118 if txTest.Sender == "" { 119 // RLP decoding failed and this is expected (test OK) 120 return nil 121 } else { 122 // RLP decoding failed but is expected to succeed (test FAIL) 123 return fmt.Errorf("RLP decoding failed when expected to succeed: ", err) 124 } 125 } 126 127 validationError := verifyTxFields(txTest, tx) 128 if txTest.Sender == "" { 129 if validationError != nil { 130 // RLP decoding works but validation should fail (test OK) 131 return nil 132 } else { 133 // RLP decoding works but validation should fail (test FAIL) 134 // (this should not be possible but added here for completeness) 135 return errors.New("Field validations succeeded but should fail") 136 } 137 } 138 139 if txTest.Sender != "" { 140 if validationError == nil { 141 // RLP decoding works and validations pass (test OK) 142 return nil 143 } else { 144 // RLP decoding works and validations pass (test FAIL) 145 return fmt.Errorf("Field validations failed after RLP decoding: ", validationError) 146 } 147 } 148 return errors.New("Should not happen: verify RLP decoding and field validation") 149 } 150 151 func verifyTxFields(txTest TransactionTest, decodedTx *types.Transaction) (err error) { 152 defer func() { 153 if recovered := recover(); recovered != nil { 154 buf := make([]byte, 64<<10) 155 buf = buf[:runtime.Stack(buf, false)] 156 err = fmt.Errorf("%v\n%s", recovered, buf) 157 } 158 }() 159 160 decodedSender, err := decodedTx.From() 161 if err != nil { 162 return err 163 } 164 165 expectedSender := mustConvertAddress(txTest.Sender) 166 if expectedSender != decodedSender { 167 return fmt.Errorf("Sender mismatch: %v %v", expectedSender, decodedSender) 168 } 169 170 expectedData := mustConvertBytes(txTest.Transaction.Data) 171 if !bytes.Equal(expectedData, decodedTx.Data()) { 172 return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, decodedTx.Data()) 173 } 174 175 expectedGasLimit := mustConvertBigInt(txTest.Transaction.GasLimit, 16) 176 if expectedGasLimit.Cmp(decodedTx.Gas()) != 0 { 177 return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, decodedTx.Gas()) 178 } 179 180 expectedGasPrice := mustConvertBigInt(txTest.Transaction.GasPrice, 16) 181 if expectedGasPrice.Cmp(decodedTx.GasPrice()) != 0 { 182 return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, decodedTx.GasPrice()) 183 } 184 185 expectedNonce := mustConvertUint(txTest.Transaction.Nonce, 16) 186 if expectedNonce != decodedTx.Nonce() { 187 return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, decodedTx.Nonce()) 188 } 189 190 v, r, s := decodedTx.SignatureValues() 191 expectedR := mustConvertBigInt(txTest.Transaction.R, 16) 192 if r.Cmp(expectedR) != 0 { 193 return fmt.Errorf("R mismatch: %v %v", expectedR, r) 194 } 195 expectedS := mustConvertBigInt(txTest.Transaction.S, 16) 196 if s.Cmp(expectedS) != 0 { 197 return fmt.Errorf("S mismatch: %v %v", expectedS, s) 198 } 199 expectedV := mustConvertUint(txTest.Transaction.V, 16) 200 if uint64(v) != expectedV { 201 return fmt.Errorf("V mismatch: %v %v", expectedV, v) 202 } 203 204 expectedTo := mustConvertAddress(txTest.Transaction.To) 205 if decodedTx.To() == nil { 206 if expectedTo != common.BytesToAddress([]byte{}) { // "empty" or "zero" address 207 return fmt.Errorf("To mismatch when recipient is nil (contract creation): %v", expectedTo) 208 } 209 } else { 210 if expectedTo != *decodedTx.To() { 211 return fmt.Errorf("To mismatch: %v %v", expectedTo, *decodedTx.To()) 212 } 213 } 214 215 expectedValue := mustConvertBigInt(txTest.Transaction.Value, 16) 216 if expectedValue.Cmp(decodedTx.Value()) != 0 { 217 return fmt.Errorf("Value mismatch: %v %v", expectedValue, decodedTx.Value()) 218 } 219 220 return nil 221 }