transaction_test_util.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. package tests
  17. import (
  18. "bytes"
  19. "errors"
  20. "fmt"
  21. "io"
  22. "runtime"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/common/math"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/log"
  27. "github.com/ethereum/go-ethereum/params"
  28. "github.com/ethereum/go-ethereum/rlp"
  29. )
  30. // Transaction Test JSON Format
  31. type TtTransaction struct {
  32. Data string
  33. GasLimit string
  34. GasPrice string
  35. Nonce string
  36. R string
  37. S string
  38. To string
  39. V string
  40. Value string
  41. }
  42. type TransactionTest struct {
  43. Blocknumber string
  44. Rlp string
  45. Sender string
  46. Transaction TtTransaction
  47. }
  48. func RunTransactionTestsWithReader(config *params.ChainConfig, r io.Reader, skipTests []string) error {
  49. skipTest := make(map[string]bool, len(skipTests))
  50. for _, name := range skipTests {
  51. skipTest[name] = true
  52. }
  53. bt := make(map[string]TransactionTest)
  54. if err := readJson(r, &bt); err != nil {
  55. return err
  56. }
  57. for name, test := range bt {
  58. // if the test should be skipped, return
  59. if skipTest[name] {
  60. log.Info(fmt.Sprint("Skipping transaction test", name))
  61. return nil
  62. }
  63. // test the block
  64. if err := runTransactionTest(config, test); err != nil {
  65. return err
  66. }
  67. log.Info(fmt.Sprint("Transaction test passed: ", name))
  68. }
  69. return nil
  70. }
  71. func RunTransactionTests(config *params.ChainConfig, file string, skipTests []string) error {
  72. tests := make(map[string]TransactionTest)
  73. if err := readJsonFile(file, &tests); err != nil {
  74. return err
  75. }
  76. if err := runTransactionTests(config, tests, skipTests); err != nil {
  77. return err
  78. }
  79. return nil
  80. }
  81. func runTransactionTests(config *params.ChainConfig, tests map[string]TransactionTest, skipTests []string) error {
  82. skipTest := make(map[string]bool, len(skipTests))
  83. for _, name := range skipTests {
  84. skipTest[name] = true
  85. }
  86. for name, test := range tests {
  87. // if the test should be skipped, return
  88. if skipTest[name] {
  89. log.Info(fmt.Sprint("Skipping transaction test", name))
  90. return nil
  91. }
  92. // test the block
  93. if err := runTransactionTest(config, test); err != nil {
  94. return fmt.Errorf("%s: %v", name, err)
  95. }
  96. log.Info(fmt.Sprint("Transaction test passed: ", name))
  97. }
  98. return nil
  99. }
  100. func runTransactionTest(config *params.ChainConfig, txTest TransactionTest) (err error) {
  101. tx := new(types.Transaction)
  102. err = rlp.DecodeBytes(mustConvertBytes(txTest.Rlp), tx)
  103. if err != nil {
  104. if txTest.Sender == "" {
  105. // RLP decoding failed and this is expected (test OK)
  106. return nil
  107. } else {
  108. // RLP decoding failed but is expected to succeed (test FAIL)
  109. return fmt.Errorf("RLP decoding failed when expected to succeed: %s", err)
  110. }
  111. }
  112. validationError := verifyTxFields(config, txTest, tx)
  113. if txTest.Sender == "" {
  114. if validationError != nil {
  115. // RLP decoding works but validation should fail (test OK)
  116. return nil
  117. } else {
  118. // RLP decoding works but validation should fail (test FAIL)
  119. // (this should not be possible but added here for completeness)
  120. return errors.New("Field validations succeeded but should fail")
  121. }
  122. }
  123. if txTest.Sender != "" {
  124. if validationError == nil {
  125. // RLP decoding works and validations pass (test OK)
  126. return nil
  127. } else {
  128. // RLP decoding works and validations pass (test FAIL)
  129. return fmt.Errorf("Field validations failed after RLP decoding: %s", validationError)
  130. }
  131. }
  132. return errors.New("Should not happen: verify RLP decoding and field validation")
  133. }
  134. func verifyTxFields(chainConfig *params.ChainConfig, txTest TransactionTest, decodedTx *types.Transaction) (err error) {
  135. defer func() {
  136. if recovered := recover(); recovered != nil {
  137. buf := make([]byte, 64<<10)
  138. buf = buf[:runtime.Stack(buf, false)]
  139. err = fmt.Errorf("%v\n%s", recovered, buf)
  140. }
  141. }()
  142. var decodedSender common.Address
  143. signer := types.MakeSigner(chainConfig, math.MustParseBig256(txTest.Blocknumber))
  144. decodedSender, err = types.Sender(signer, decodedTx)
  145. if err != nil {
  146. return err
  147. }
  148. expectedSender := mustConvertAddress(txTest.Sender)
  149. if expectedSender != decodedSender {
  150. return fmt.Errorf("Sender mismatch: %x %x", expectedSender, decodedSender)
  151. }
  152. expectedData := mustConvertBytes(txTest.Transaction.Data)
  153. if !bytes.Equal(expectedData, decodedTx.Data()) {
  154. return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, decodedTx.Data())
  155. }
  156. expectedGasLimit := mustConvertBigInt(txTest.Transaction.GasLimit, 16)
  157. if expectedGasLimit.Cmp(decodedTx.Gas()) != 0 {
  158. return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, decodedTx.Gas())
  159. }
  160. expectedGasPrice := mustConvertBigInt(txTest.Transaction.GasPrice, 16)
  161. if expectedGasPrice.Cmp(decodedTx.GasPrice()) != 0 {
  162. return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, decodedTx.GasPrice())
  163. }
  164. expectedNonce := mustConvertUint(txTest.Transaction.Nonce, 16)
  165. if expectedNonce != decodedTx.Nonce() {
  166. return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, decodedTx.Nonce())
  167. }
  168. v, r, s := decodedTx.RawSignatureValues()
  169. expectedR := mustConvertBigInt(txTest.Transaction.R, 16)
  170. if r.Cmp(expectedR) != 0 {
  171. return fmt.Errorf("R mismatch: %v %v", expectedR, r)
  172. }
  173. expectedS := mustConvertBigInt(txTest.Transaction.S, 16)
  174. if s.Cmp(expectedS) != 0 {
  175. return fmt.Errorf("S mismatch: %v %v", expectedS, s)
  176. }
  177. expectedV := mustConvertBigInt(txTest.Transaction.V, 16)
  178. if v.Cmp(expectedV) != 0 {
  179. return fmt.Errorf("V mismatch: %v %v", expectedV, v)
  180. }
  181. expectedTo := mustConvertAddress(txTest.Transaction.To)
  182. if decodedTx.To() == nil {
  183. if expectedTo != common.BytesToAddress([]byte{}) { // "empty" or "zero" address
  184. return fmt.Errorf("To mismatch when recipient is nil (contract creation): %v", expectedTo)
  185. }
  186. } else {
  187. if expectedTo != *decodedTx.To() {
  188. return fmt.Errorf("To mismatch: %v %v", expectedTo, *decodedTx.To())
  189. }
  190. }
  191. expectedValue := mustConvertBigInt(txTest.Transaction.Value, 16)
  192. if expectedValue.Cmp(decodedTx.Value()) != 0 {
  193. return fmt.Errorf("Value mismatch: %v %v", expectedValue, decodedTx.Value())
  194. }
  195. return nil
  196. }