transaction_test_util.go 6.6 KB

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