transaction_test_util.go 6.2 KB

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