transaction_test_util.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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(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(test); err != nil {
  64. return err
  65. }
  66. glog.Infoln("Transaction test passed: ", name)
  67. }
  68. return nil
  69. }
  70. func RunTransactionTests(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(tests, skipTests); err != nil {
  76. return err
  77. }
  78. return nil
  79. }
  80. func runTransactionTests(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(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(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(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(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 (
  142. decodedSender common.Address
  143. )
  144. chainConfig := &params.ChainConfig{HomesteadBlock: params.MainNetHomesteadBlock}
  145. if chainConfig.IsHomestead(common.String2Big(txTest.Blocknumber)) {
  146. decodedSender, err = decodedTx.From()
  147. } else {
  148. decodedSender, err = decodedTx.FromFrontier()
  149. }
  150. if err != nil {
  151. return err
  152. }
  153. expectedSender := mustConvertAddress(txTest.Sender)
  154. if expectedSender != decodedSender {
  155. return fmt.Errorf("Sender mismatch: %v %v", expectedSender, decodedSender)
  156. }
  157. expectedData := mustConvertBytes(txTest.Transaction.Data)
  158. if !bytes.Equal(expectedData, decodedTx.Data()) {
  159. return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, decodedTx.Data())
  160. }
  161. expectedGasLimit := mustConvertBigInt(txTest.Transaction.GasLimit, 16)
  162. if expectedGasLimit.Cmp(decodedTx.Gas()) != 0 {
  163. return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, decodedTx.Gas())
  164. }
  165. expectedGasPrice := mustConvertBigInt(txTest.Transaction.GasPrice, 16)
  166. if expectedGasPrice.Cmp(decodedTx.GasPrice()) != 0 {
  167. return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, decodedTx.GasPrice())
  168. }
  169. expectedNonce := mustConvertUint(txTest.Transaction.Nonce, 16)
  170. if expectedNonce != decodedTx.Nonce() {
  171. return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, decodedTx.Nonce())
  172. }
  173. v, r, s := decodedTx.SignatureValues()
  174. expectedR := mustConvertBigInt(txTest.Transaction.R, 16)
  175. if r.Cmp(expectedR) != 0 {
  176. return fmt.Errorf("R mismatch: %v %v", expectedR, r)
  177. }
  178. expectedS := mustConvertBigInt(txTest.Transaction.S, 16)
  179. if s.Cmp(expectedS) != 0 {
  180. return fmt.Errorf("S mismatch: %v %v", expectedS, s)
  181. }
  182. expectedV := mustConvertUint(txTest.Transaction.V, 16)
  183. if uint64(v) != expectedV {
  184. return fmt.Errorf("V mismatch: %v %v", expectedV, v)
  185. }
  186. expectedTo := mustConvertAddress(txTest.Transaction.To)
  187. if decodedTx.To() == nil {
  188. if expectedTo != common.BytesToAddress([]byte{}) { // "empty" or "zero" address
  189. return fmt.Errorf("To mismatch when recipient is nil (contract creation): %v", expectedTo)
  190. }
  191. } else {
  192. if expectedTo != *decodedTx.To() {
  193. return fmt.Errorf("To mismatch: %v %v", expectedTo, *decodedTx.To())
  194. }
  195. }
  196. expectedValue := mustConvertBigInt(txTest.Transaction.Value, 16)
  197. if expectedValue.Cmp(decodedTx.Value()) != 0 {
  198. return fmt.Errorf("Value mismatch: %v %v", expectedValue, decodedTx.Value())
  199. }
  200. return nil
  201. }