transaction_test_util.go 4.6 KB

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