transaction_test_util.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, notWorking map[string]bool) error {
  29. bt := make(map[string]TransactionTest)
  30. if err := LoadJSON(file, &bt); err != nil {
  31. return err
  32. }
  33. for name, in := range bt {
  34. var err error
  35. // TODO: remove this, we currently ignore some tests which are broken
  36. if !notWorking[name] {
  37. if err = runTest(in); err != nil {
  38. return fmt.Errorf("bad test %s: %v", name, err)
  39. }
  40. fmt.Println("Test passed:", name)
  41. }
  42. }
  43. return nil
  44. }
  45. func runTest(txTest TransactionTest) (err error) {
  46. tx := new(types.Transaction)
  47. err = rlp.DecodeBytes(mustConvertBytes(txTest.Rlp), tx)
  48. if err != nil {
  49. if txTest.Sender == "" {
  50. // RLP decoding failed and this is expected (test OK)
  51. return nil
  52. } else {
  53. // RLP decoding failed but is expected to succeed (test FAIL)
  54. return errors.New("RLP decoding failed when expected to succeed")
  55. }
  56. }
  57. validationError := verifyTxFields(txTest, tx)
  58. if txTest.Sender == "" {
  59. if validationError != nil {
  60. // RLP decoding works but validation should fail (test OK)
  61. return nil
  62. } else {
  63. // RLP decoding works but validation should fail (test FAIL)
  64. // (this should not be possible but added here for completeness)
  65. return errors.New("Field validations succeeded but should fail")
  66. }
  67. }
  68. if txTest.Sender != "" {
  69. if validationError == nil {
  70. // RLP decoding works and validations pass (test OK)
  71. return nil
  72. } else {
  73. // RLP decoding works and validations pass (test FAIL)
  74. return fmt.Errorf("Field validations failed after RLP decoding: ", validationError)
  75. }
  76. }
  77. return errors.New("Should not happen: verify RLP decoding and field validation")
  78. }
  79. func verifyTxFields(txTest TransactionTest, decodedTx *types.Transaction) (err error) {
  80. defer func() {
  81. if recovered := recover(); recovered != nil {
  82. buf := make([]byte, 64<<10)
  83. buf = buf[:runtime.Stack(buf, false)]
  84. err = fmt.Errorf("%v\n%s", recovered, buf)
  85. }
  86. }()
  87. decodedSender, err := decodedTx.From()
  88. if err != nil {
  89. return err
  90. }
  91. expectedSender := mustConvertAddress(txTest.Sender)
  92. if expectedSender != decodedSender {
  93. return fmt.Errorf("Sender mismatch: %v %v", expectedSender, decodedSender)
  94. }
  95. expectedData := mustConvertBytes(txTest.Transaction.Data)
  96. if !bytes.Equal(expectedData, decodedTx.Payload) {
  97. return fmt.Errorf("Tx input data mismatch: %#v %#v", expectedData, decodedTx.Payload)
  98. }
  99. expectedGasLimit := mustConvertBigInt(txTest.Transaction.GasLimit, 16)
  100. if expectedGasLimit.Cmp(decodedTx.GasLimit) != 0 {
  101. return fmt.Errorf("GasLimit mismatch: %v %v", expectedGasLimit, decodedTx.GasLimit)
  102. }
  103. expectedGasPrice := mustConvertBigInt(txTest.Transaction.GasPrice, 16)
  104. if expectedGasPrice.Cmp(decodedTx.Price) != 0 {
  105. return fmt.Errorf("GasPrice mismatch: %v %v", expectedGasPrice, decodedTx.Price)
  106. }
  107. expectedNonce := mustConvertUint(txTest.Transaction.Nonce, 16)
  108. if expectedNonce != decodedTx.AccountNonce {
  109. return fmt.Errorf("Nonce mismatch: %v %v", expectedNonce, decodedTx.AccountNonce)
  110. }
  111. expectedR := common.Bytes2Big(mustConvertBytes(txTest.Transaction.R))
  112. if expectedR.Cmp(decodedTx.R) != 0 {
  113. return fmt.Errorf("R mismatch: %v %v", expectedR, decodedTx.R)
  114. }
  115. expectedS := common.Bytes2Big(mustConvertBytes(txTest.Transaction.S))
  116. if expectedS.Cmp(decodedTx.S) != 0 {
  117. return fmt.Errorf("S mismatch: %v %v", expectedS, decodedTx.S)
  118. }
  119. expectedV := mustConvertUint(txTest.Transaction.V, 16)
  120. if expectedV != uint64(decodedTx.V) {
  121. return fmt.Errorf("V mismatch: %v %v", expectedV, uint64(decodedTx.V))
  122. }
  123. expectedTo := mustConvertAddress(txTest.Transaction.To)
  124. if decodedTx.Recipient == nil {
  125. if expectedTo != common.BytesToAddress([]byte{}) { // "empty" or "zero" address
  126. return fmt.Errorf("To mismatch when recipient is nil (contract creation): %v", expectedTo)
  127. }
  128. } else {
  129. if expectedTo != *decodedTx.Recipient {
  130. return fmt.Errorf("To mismatch: %v %v", expectedTo, *decodedTx.Recipient)
  131. }
  132. }
  133. expectedValue := mustConvertBigInt(txTest.Transaction.Value, 16)
  134. if expectedValue.Cmp(decodedTx.Amount) != 0 {
  135. return fmt.Errorf("Value mismatch: %v %v", expectedValue, decodedTx.Amount)
  136. }
  137. return nil
  138. }