transaction_test_util.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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) error {
  31. skipTest := make(map[string]bool, len(TransSkipTests))
  32. for _, name := range TransSkipTests {
  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) error {
  54. skipTest := make(map[string]bool, len(TransSkipTests))
  55. for _, name := range TransSkipTests {
  56. skipTest[name] = true
  57. }
  58. bt := make(map[string]TransactionTest)
  59. if err := readJsonFile(file, &bt); err != nil {
  60. return err
  61. }
  62. for name, test := range bt {
  63. // if the test should be skipped, return
  64. if skipTest[name] {
  65. glog.Infoln("Skipping transaction test", name)
  66. return nil
  67. }
  68. // test the block
  69. if err := runTransactionTest(test); err != nil {
  70. return err
  71. }
  72. glog.Infoln("Transaction test passed: ", name)
  73. }
  74. return nil
  75. }
  76. func runTransactionTest(txTest TransactionTest) (err error) {
  77. tx := new(types.Transaction)
  78. err = rlp.DecodeBytes(mustConvertBytes(txTest.Rlp), tx)
  79. if err != nil {
  80. if txTest.Sender == "" {
  81. // RLP decoding failed and this is expected (test OK)
  82. return nil
  83. } else {
  84. // RLP decoding failed but is expected to succeed (test FAIL)
  85. return fmt.Errorf("RLP decoding failed when expected to succeed: ", err)
  86. }
  87. }
  88. validationError := verifyTxFields(txTest, tx)
  89. if txTest.Sender == "" {
  90. if validationError != nil {
  91. // RLP decoding works but validation should fail (test OK)
  92. return nil
  93. } else {
  94. // RLP decoding works but validation should fail (test FAIL)
  95. // (this should not be possible but added here for completeness)
  96. return errors.New("Field validations succeeded but should fail")
  97. }
  98. }
  99. if txTest.Sender != "" {
  100. if validationError == nil {
  101. // RLP decoding works and validations pass (test OK)
  102. return nil
  103. } else {
  104. // RLP decoding works and validations pass (test FAIL)
  105. return fmt.Errorf("Field validations failed after RLP decoding: ", validationError)
  106. }
  107. }
  108. return errors.New("Should not happen: verify RLP decoding and field validation")
  109. }
  110. func verifyTxFields(txTest TransactionTest, decodedTx *types.Transaction) (err error) {
  111. defer func() {
  112. if recovered := recover(); recovered != nil {
  113. buf := make([]byte, 64<<10)
  114. buf = buf[:runtime.Stack(buf, false)]
  115. err = fmt.Errorf("%v\n%s", recovered, buf)
  116. }
  117. }()
  118. decodedSender, err := decodedTx.From()
  119. if err != nil {
  120. return err
  121. }
  122. expectedSender := mustConvertAddress(txTest.Sender)
  123. if expectedSender != decodedSender {
  124. return fmt.Errorf("Sender mismatch: %v %v", expectedSender, decodedSender)
  125. }
  126. expectedData := mustConvertBytes(txTest.Transaction.Data)
  127. if !bytes.Equal(expectedData, decodedTx.Payload) {
  128. return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, decodedTx.Payload)
  129. }
  130. expectedGasLimit := mustConvertBigInt(txTest.Transaction.GasLimit, 16)
  131. if expectedGasLimit.Cmp(decodedTx.GasLimit) != 0 {
  132. return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, decodedTx.GasLimit)
  133. }
  134. expectedGasPrice := mustConvertBigInt(txTest.Transaction.GasPrice, 16)
  135. if expectedGasPrice.Cmp(decodedTx.Price) != 0 {
  136. return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, decodedTx.Price)
  137. }
  138. expectedNonce := mustConvertUint(txTest.Transaction.Nonce, 16)
  139. if expectedNonce != decodedTx.AccountNonce {
  140. return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, decodedTx.AccountNonce)
  141. }
  142. expectedR := common.Bytes2Big(mustConvertBytes(txTest.Transaction.R))
  143. if expectedR.Cmp(decodedTx.R) != 0 {
  144. return fmt.Errorf("R mismatch: %v %v", expectedR, decodedTx.R)
  145. }
  146. expectedS := common.Bytes2Big(mustConvertBytes(txTest.Transaction.S))
  147. if expectedS.Cmp(decodedTx.S) != 0 {
  148. return fmt.Errorf("S mismatch: %v %v", expectedS, decodedTx.S)
  149. }
  150. expectedV := mustConvertUint(txTest.Transaction.V, 16)
  151. if expectedV != uint64(decodedTx.V) {
  152. return fmt.Errorf("V mismatch: %v %v", expectedV, uint64(decodedTx.V))
  153. }
  154. expectedTo := mustConvertAddress(txTest.Transaction.To)
  155. if decodedTx.Recipient == nil {
  156. if expectedTo != common.BytesToAddress([]byte{}) { // "empty" or "zero" address
  157. return fmt.Errorf("To mismatch when recipient is nil (contract creation): %v", expectedTo)
  158. }
  159. } else {
  160. if expectedTo != *decodedTx.Recipient {
  161. return fmt.Errorf("To mismatch: %v %v", expectedTo, *decodedTx.Recipient)
  162. }
  163. }
  164. expectedValue := mustConvertBigInt(txTest.Transaction.Value, 16)
  165. if expectedValue.Cmp(decodedTx.Amount) != 0 {
  166. return fmt.Errorf("Value mismatch: %v %v", expectedValue, decodedTx.Amount)
  167. }
  168. return nil
  169. }