transaction_test_util.go 5.5 KB

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