2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2015-04-10 04:55:31 -05:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-07-11 06:49:14 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2025-02-06 05:33:40 -06:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2019-05-21 06:58:06 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2015-04-10 04:55:31 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2016-01-19 16:48:50 -06:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2015-04-10 04:55:31 -05:00
|
|
|
)
|
|
|
|
|
2017-07-11 06:49:14 -05:00
|
|
|
// TransactionTest checks RLP decoding and sender derivation of transactions.
|
2015-04-10 04:55:31 -05:00
|
|
|
type TransactionTest struct {
|
2024-08-08 06:50:00 -05:00
|
|
|
Txbytes hexutil.Bytes `json:"txbytes"`
|
2025-02-06 05:33:40 -06:00
|
|
|
Result map[string]*ttFork
|
2024-08-08 06:50:00 -05:00
|
|
|
}
|
|
|
|
|
2025-02-06 05:33:40 -06:00
|
|
|
type ttFork struct {
|
|
|
|
Sender *common.UnprefixedAddress `json:"sender"`
|
|
|
|
Hash *common.UnprefixedHash `json:"hash"`
|
|
|
|
Exception *string `json:"exception"`
|
|
|
|
IntrinsicGas math.HexOrDecimal64 `json:"intrinsicGas"`
|
2015-04-10 04:55:31 -05:00
|
|
|
}
|
|
|
|
|
2025-02-06 05:33:40 -06:00
|
|
|
func (tt *TransactionTest) validate() error {
|
|
|
|
if tt.Txbytes == nil {
|
|
|
|
return fmt.Errorf("missing txbytes")
|
|
|
|
}
|
|
|
|
for name, fork := range tt.Result {
|
|
|
|
if err := tt.validateFork(fork); err != nil {
|
|
|
|
return fmt.Errorf("invalid %s: %v", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *TransactionTest) validateFork(fork *ttFork) error {
|
|
|
|
if fork == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fork.Hash == nil && fork.Exception == nil {
|
|
|
|
return fmt.Errorf("missing hash and exception")
|
|
|
|
}
|
|
|
|
if fork.Hash != nil && fork.Sender == nil {
|
|
|
|
return fmt.Errorf("missing sender")
|
|
|
|
}
|
|
|
|
return nil
|
2015-04-10 04:55:31 -05:00
|
|
|
}
|
|
|
|
|
2017-07-11 06:49:14 -05:00
|
|
|
func (tt *TransactionTest) Run(config *params.ChainConfig) error {
|
2025-02-06 05:33:40 -06:00
|
|
|
if err := tt.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
validateTx := func(rlpData hexutil.Bytes, signer types.Signer, isHomestead, isIstanbul, isShanghai bool) (sender common.Address, hash common.Hash, requiredGas uint64, err error) {
|
2019-05-21 06:58:06 -05:00
|
|
|
tx := new(types.Transaction)
|
2025-02-06 05:33:40 -06:00
|
|
|
if err = tx.UnmarshalBinary(rlpData); err != nil {
|
|
|
|
return
|
2015-04-18 17:35:48 -05:00
|
|
|
}
|
2025-02-06 05:33:40 -06:00
|
|
|
sender, err = types.Sender(signer, tx)
|
2019-05-21 06:58:06 -05:00
|
|
|
if err != nil {
|
2025-02-06 05:33:40 -06:00
|
|
|
return
|
2019-05-21 06:58:06 -05:00
|
|
|
}
|
|
|
|
// Intrinsic gas
|
2025-02-06 05:33:40 -06:00
|
|
|
requiredGas, err = core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, isHomestead, isIstanbul, isShanghai)
|
2019-05-21 06:58:06 -05:00
|
|
|
if err != nil {
|
2025-02-06 05:33:40 -06:00
|
|
|
return
|
2019-05-21 06:58:06 -05:00
|
|
|
}
|
|
|
|
if requiredGas > tx.Gas() {
|
2025-02-06 05:33:40 -06:00
|
|
|
return sender, hash, 0, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas)
|
2019-05-21 06:58:06 -05:00
|
|
|
}
|
2025-02-06 05:33:40 -06:00
|
|
|
hash = tx.Hash()
|
|
|
|
return sender, hash, requiredGas, nil
|
2015-04-10 04:55:31 -05:00
|
|
|
}
|
2019-05-21 06:58:06 -05:00
|
|
|
for _, testcase := range []struct {
|
|
|
|
name string
|
|
|
|
signer types.Signer
|
2025-02-06 05:33:40 -06:00
|
|
|
fork *ttFork
|
2019-05-21 06:58:06 -05:00
|
|
|
isHomestead bool
|
2019-08-14 07:53:21 -05:00
|
|
|
isIstanbul bool
|
2025-02-06 05:33:40 -06:00
|
|
|
isShanghai bool
|
2019-05-21 06:58:06 -05:00
|
|
|
}{
|
2025-02-06 05:33:40 -06:00
|
|
|
{"Frontier", types.FrontierSigner{}, tt.Result["Frontier"], false, false, false},
|
|
|
|
{"Homestead", types.HomesteadSigner{}, tt.Result["Homestead"], true, false, false},
|
|
|
|
{"EIP150", types.HomesteadSigner{}, tt.Result["EIP150"], true, false, false},
|
|
|
|
{"EIP158", types.NewEIP155Signer(config.ChainID), tt.Result["EIP158"], true, false, false},
|
|
|
|
{"Byzantium", types.NewEIP155Signer(config.ChainID), tt.Result["Byzantium"], true, false, false},
|
|
|
|
{"Constantinople", types.NewEIP155Signer(config.ChainID), tt.Result["Constantinople"], true, false, false},
|
|
|
|
{"Istanbul", types.NewEIP155Signer(config.ChainID), tt.Result["Istanbul"], true, true, false},
|
|
|
|
{"Berlin", types.NewEIP2930Signer(config.ChainID), tt.Result["Berlin"], true, true, false},
|
|
|
|
{"London", types.NewLondonSigner(config.ChainID), tt.Result["London"], true, true, false},
|
|
|
|
{"Paris", types.NewLondonSigner(config.ChainID), tt.Result["Paris"], true, true, false},
|
|
|
|
{"Shanghai", types.NewLondonSigner(config.ChainID), tt.Result["Shanghai"], true, true, true},
|
|
|
|
{"Cancun", types.NewCancunSigner(config.ChainID), tt.Result["Cancun"], true, true, true},
|
|
|
|
{"Prague", types.NewPragueSigner(config.ChainID), tt.Result["Prague"], true, true, true},
|
2019-05-21 06:58:06 -05:00
|
|
|
} {
|
2025-02-06 05:33:40 -06:00
|
|
|
if testcase.fork == nil {
|
2019-05-21 06:58:06 -05:00
|
|
|
continue
|
|
|
|
}
|
2025-02-06 05:33:40 -06:00
|
|
|
sender, hash, gas, err := validateTx(tt.Txbytes, testcase.signer, testcase.isHomestead, testcase.isIstanbul, testcase.isShanghai)
|
2019-05-21 06:58:06 -05:00
|
|
|
if err != nil {
|
2025-02-06 05:33:40 -06:00
|
|
|
if testcase.fork.Hash != nil {
|
|
|
|
return fmt.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if testcase.fork.Exception != nil {
|
|
|
|
return fmt.Errorf("expected error %v, got none (%v)", *testcase.fork.Exception, err)
|
2019-05-21 06:58:06 -05:00
|
|
|
}
|
2025-02-06 05:33:40 -06:00
|
|
|
if common.Hash(*testcase.fork.Hash) != hash {
|
|
|
|
return fmt.Errorf("hash mismatch: got %x, want %x", hash, common.Hash(*testcase.fork.Hash))
|
2019-05-21 06:58:06 -05:00
|
|
|
}
|
2025-02-06 05:33:40 -06:00
|
|
|
if common.Address(*testcase.fork.Sender) != sender {
|
2019-11-23 05:51:37 -06:00
|
|
|
return fmt.Errorf("sender mismatch: got %x, want %x", sender, testcase.fork.Sender)
|
2019-05-21 06:58:06 -05:00
|
|
|
}
|
2025-02-06 05:33:40 -06:00
|
|
|
if hash != common.Hash(*testcase.fork.Hash) {
|
|
|
|
return fmt.Errorf("hash mismatch: got %x, want %x", hash, testcase.fork.Hash)
|
2019-05-21 06:58:06 -05:00
|
|
|
}
|
2025-02-06 05:33:40 -06:00
|
|
|
if uint64(testcase.fork.IntrinsicGas) != gas {
|
|
|
|
return fmt.Errorf("intrinsic gas mismatch: got %d, want %d", gas, uint64(testcase.fork.IntrinsicGas))
|
2015-04-15 15:37:16 -05:00
|
|
|
}
|
2015-04-10 04:55:31 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|