transaction_test_util.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "fmt"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/common/hexutil"
  21. "github.com/ethereum/go-ethereum/core"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/params"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. )
  26. // TransactionTest checks RLP decoding and sender derivation of transactions.
  27. type TransactionTest struct {
  28. RLP hexutil.Bytes `json:"rlp"`
  29. Byzantium ttFork
  30. Constantinople ttFork
  31. EIP150 ttFork
  32. EIP158 ttFork
  33. Frontier ttFork
  34. Homestead ttFork
  35. }
  36. type ttFork struct {
  37. Sender common.UnprefixedAddress `json:"sender"`
  38. Hash common.UnprefixedHash `json:"hash"`
  39. }
  40. func (tt *TransactionTest) Run(config *params.ChainConfig) error {
  41. validateTx := func(rlpData hexutil.Bytes, signer types.Signer, isHomestead bool) (*common.Address, *common.Hash, error) {
  42. tx := new(types.Transaction)
  43. if err := rlp.DecodeBytes(rlpData, tx); err != nil {
  44. return nil, nil, err
  45. }
  46. sender, err := types.Sender(signer, tx)
  47. if err != nil {
  48. return nil, nil, err
  49. }
  50. // Intrinsic gas
  51. requiredGas, err := core.IntrinsicGas(tx.Data(), tx.To() == nil, isHomestead)
  52. if err != nil {
  53. return nil, nil, err
  54. }
  55. if requiredGas > tx.Gas() {
  56. return nil, nil, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas)
  57. }
  58. h := tx.Hash()
  59. return &sender, &h, nil
  60. }
  61. for _, testcase := range []struct {
  62. name string
  63. signer types.Signer
  64. fork ttFork
  65. isHomestead bool
  66. }{
  67. {"Frontier", types.FrontierSigner{}, tt.Frontier, false},
  68. {"Homestead", types.HomesteadSigner{}, tt.Homestead, true},
  69. {"EIP150", types.HomesteadSigner{}, tt.EIP150, true},
  70. {"EIP158", types.NewEIP155Signer(config.ChainID), tt.EIP158, true},
  71. {"Byzantium", types.NewEIP155Signer(config.ChainID), tt.Byzantium, true},
  72. {"Constantinople", types.NewEIP155Signer(config.ChainID), tt.Constantinople, true},
  73. } {
  74. sender, txhash, err := validateTx(tt.RLP, testcase.signer, testcase.isHomestead)
  75. if testcase.fork.Sender == (common.UnprefixedAddress{}) {
  76. if err == nil {
  77. return fmt.Errorf("Expected error, got none (address %v)", sender.String())
  78. }
  79. continue
  80. }
  81. // Should resolve the right address
  82. if err != nil {
  83. return fmt.Errorf("Got error, expected none: %v", err)
  84. }
  85. if sender == nil {
  86. return fmt.Errorf("sender was nil, should be %x", common.Address(testcase.fork.Sender))
  87. }
  88. if *sender != common.Address(testcase.fork.Sender) {
  89. return fmt.Errorf("Sender mismatch: got %x, want %x", sender, testcase.fork.Sender)
  90. }
  91. if txhash == nil {
  92. return fmt.Errorf("txhash was nil, should be %x", common.Hash(testcase.fork.Hash))
  93. }
  94. if *txhash != common.Hash(testcase.fork.Hash) {
  95. return fmt.Errorf("Hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash)
  96. }
  97. }
  98. return nil
  99. }