transaction_test_util.go 3.8 KB

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