From 4ea656a5214c3625df1dfa47e9604fded96ba1d9 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Wed, 29 Jan 2025 19:28:21 +0100 Subject: [PATCH] tests: add EEST transaction type tests --- tests/init_test.go | 21 +++-- tests/transaction_test.go | 27 ++++++ tests/transaction_test_util.go | 165 ++++++++++++++++++++++++--------- 3 files changed, 159 insertions(+), 54 deletions(-) diff --git a/tests/init_test.go b/tests/init_test.go index effeec2b86..b933c9808c 100644 --- a/tests/init_test.go +++ b/tests/init_test.go @@ -34,16 +34,17 @@ import ( ) var ( - baseDir = filepath.Join(".", "testdata") - blockTestDir = filepath.Join(baseDir, "BlockchainTests") - stateTestDir = filepath.Join(baseDir, "GeneralStateTests") - legacyStateTestDir = filepath.Join(baseDir, "LegacyTests", "Constantinople", "GeneralStateTests") - transactionTestDir = filepath.Join(baseDir, "TransactionTests") - rlpTestDir = filepath.Join(baseDir, "RLPTests") - difficultyTestDir = filepath.Join(baseDir, "BasicTests") - executionSpecBlockchainTestDir = filepath.Join(".", "spec-tests", "fixtures", "blockchain_tests") - executionSpecStateTestDir = filepath.Join(".", "spec-tests", "fixtures", "state_tests") - benchmarksDir = filepath.Join(".", "evm-benchmarks", "benchmarks") + baseDir = filepath.Join(".", "testdata") + blockTestDir = filepath.Join(baseDir, "BlockchainTests") + stateTestDir = filepath.Join(baseDir, "GeneralStateTests") + legacyStateTestDir = filepath.Join(baseDir, "LegacyTests", "Constantinople", "GeneralStateTests") + transactionTestDir = filepath.Join(baseDir, "TransactionTests") + rlpTestDir = filepath.Join(baseDir, "RLPTests") + difficultyTestDir = filepath.Join(baseDir, "BasicTests") + executionSpecBlockchainTestDir = filepath.Join(".", "spec-tests", "fixtures", "blockchain_tests") + executionSpecStateTestDir = filepath.Join(".", "spec-tests", "fixtures", "state_tests") + executionSpecTransactionTestDir = filepath.Join(".", "spec-tests", "fixtures", "transaction_tests") + benchmarksDir = filepath.Join(".", "evm-benchmarks", "benchmarks") ) func readJSON(reader io.Reader, value interface{}) error { diff --git a/tests/transaction_test.go b/tests/transaction_test.go index 5179fc9afe..d061d8bdba 100644 --- a/tests/transaction_test.go +++ b/tests/transaction_test.go @@ -19,6 +19,7 @@ package tests import ( "testing" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/params" ) @@ -42,6 +43,18 @@ func TestTransaction(t *testing.T) { // Geth accepts it, which is not a consensus issue since we use big.Int's // internally to calculate the cost txt.skipLoad("^ttValue/TransactionWithHighValueOverflow.json") + + // Test requires EVM execution and should be probably done as a state test. + txt.skipLoad("^ttEIP3860/DataTestInitCodeTooBig.json") + + // The following tests require the tx precheck to be performed. + txt.skipLoad("^ttEIP1559/maxPriorityFeePerGass32BytesValue.json") + txt.skipLoad("^ttEIP1559/maxPriorityFeePerGasOverflow.json") + txt.skipLoad("^ttEIP1559/maxFeePerGas32BytesValue.json") + txt.skipLoad("^ttEIP1559/maxFeePerGasOverflow.json") + txt.skipLoad("^ttEIP1559/GasLimitPriceProductPlusOneOverflow.json") + txt.skipLoad("^ttEIP1559/GasLimitPriceProductOverflow.json") + txt.walk(t, transactionTestDir, func(t *testing.T, name string, test *TransactionTest) { cfg := params.MainnetChainConfig if err := txt.checkFailure(t, test.Run(cfg)); err != nil { @@ -49,3 +62,17 @@ func TestTransaction(t *testing.T) { } }) } + +func TestExecutionSpecTransaction(t *testing.T) { + if !common.FileExist(executionSpecStateTestDir) { + t.Skipf("directory %s does not exist", executionSpecStateTestDir) + } + st := new(testMatcher) + + st.walk(t, executionSpecTransactionTestDir, func(t *testing.T, name string, test *TransactionTest) { + cfg := params.MainnetChainConfig + if err := st.checkFailure(t, test.Run(cfg)); err != nil { + t.Error(err) + } + }) +} diff --git a/tests/transaction_test_util.go b/tests/transaction_test_util.go index 55b76df89c..7cc45ec6d7 100644 --- a/tests/transaction_test_util.go +++ b/tests/transaction_test_util.go @@ -21,10 +21,10 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" - "github.com/ethereum/go-ethereum/rlp" ) // TransactionTest checks RLP decoding and sender derivation of transactions. @@ -34,80 +34,157 @@ type TransactionTest struct { } type ttResult struct { - Byzantium ttFork - Constantinople ttFork - Istanbul ttFork - EIP150 ttFork - EIP158 ttFork - Frontier ttFork - Homestead ttFork + Prague *ttFork + Cancun *ttFork + Shanghai *ttFork + Paris *ttFork + London *ttFork + Berlin *ttFork + Byzantium *ttFork + Constantinople *ttFork + Istanbul *ttFork + EIP150 *ttFork + EIP158 *ttFork + Frontier *ttFork + Homestead *ttFork } type ttFork struct { - Sender common.UnprefixedAddress `json:"sender"` - Hash common.UnprefixedHash `json:"hash"` + Sender *common.UnprefixedAddress `json:"sender"` + Hash *common.UnprefixedHash `json:"hash"` + Exception *string `json:"exception"` + IntrinsicGas math.HexOrDecimal64 `json:"intrinsicGas"` +} + +func (tt *TransactionTest) validate() error { + if tt.Txbytes == nil { + return fmt.Errorf("missing txbytes") + } + if err := tt.validateFork(tt.Result.Prague); err != nil { + return fmt.Errorf("invalid Prague: %v", err) + } + if err := tt.validateFork(tt.Result.Cancun); err != nil { + return fmt.Errorf("invalid Cancun: %v", err) + } + if err := tt.validateFork(tt.Result.Shanghai); err != nil { + return fmt.Errorf("invalid Shanghai: %v", err) + } + if err := tt.validateFork(tt.Result.Paris); err != nil { + return fmt.Errorf("invalid Paris: %v", err) + } + if err := tt.validateFork(tt.Result.London); err != nil { + return fmt.Errorf("invalid London: %v", err) + } + if err := tt.validateFork(tt.Result.Berlin); err != nil { + return fmt.Errorf("invalid Berlin: %v", err) + } + if err := tt.validateFork(tt.Result.Byzantium); err != nil { + return fmt.Errorf("invalid Byzantium: %v", err) + } + if err := tt.validateFork(tt.Result.Constantinople); err != nil { + return fmt.Errorf("invalid Constantinople: %v", err) + } + if err := tt.validateFork(tt.Result.Istanbul); err != nil { + return fmt.Errorf("invalid Istanbul: %v", err) + } + if err := tt.validateFork(tt.Result.EIP150); err != nil { + return fmt.Errorf("invalid EIP150: %v", err) + } + if err := tt.validateFork(tt.Result.EIP158); err != nil { + return fmt.Errorf("invalid EIP158: %v", err) + } + if err := tt.validateFork(tt.Result.Frontier); err != nil { + return fmt.Errorf("invalid Frontier: %v", err) + } + if err := tt.validateFork(tt.Result.Homestead); err != nil { + return fmt.Errorf("invalid Homestead: %v", 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 } func (tt *TransactionTest) Run(config *params.ChainConfig) error { - validateTx := func(rlpData hexutil.Bytes, signer types.Signer, isHomestead bool, isIstanbul bool) (*common.Address, *common.Hash, error) { + 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) { tx := new(types.Transaction) - if err := rlp.DecodeBytes(rlpData, tx); err != nil { - return nil, nil, err + if err = tx.UnmarshalBinary(rlpData); err != nil { + return } - sender, err := types.Sender(signer, tx) + sender, err = types.Sender(signer, tx) if err != nil { - return nil, nil, err + return } // Intrinsic gas - requiredGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, isHomestead, isIstanbul, false) + requiredGas, err = core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, isHomestead, isIstanbul, isShanghai) if err != nil { - return nil, nil, err + return } if requiredGas > tx.Gas() { - return nil, nil, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas) + return sender, hash, 0, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas) } - h := tx.Hash() - return &sender, &h, nil + hash = tx.Hash() + return sender, hash, requiredGas, nil } - for _, testcase := range []struct { name string signer types.Signer - fork ttFork + fork *ttFork isHomestead bool isIstanbul bool + isShanghai bool }{ - {"Frontier", types.FrontierSigner{}, tt.Result.Frontier, false, false}, - {"Homestead", types.HomesteadSigner{}, tt.Result.Homestead, true, false}, - {"EIP150", types.HomesteadSigner{}, tt.Result.EIP150, true, false}, - {"EIP158", types.NewEIP155Signer(config.ChainID), tt.Result.EIP158, true, false}, - {"Byzantium", types.NewEIP155Signer(config.ChainID), tt.Result.Byzantium, true, false}, - {"Constantinople", types.NewEIP155Signer(config.ChainID), tt.Result.Constantinople, true, false}, - {"Istanbul", types.NewEIP155Signer(config.ChainID), tt.Result.Istanbul, true, true}, + {"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}, } { - sender, txhash, err := validateTx(tt.Txbytes, testcase.signer, testcase.isHomestead, testcase.isIstanbul) - - if testcase.fork.Sender == (common.UnprefixedAddress{}) { - if err == nil { - return fmt.Errorf("expected error, got none (address %v)[%v]", sender.String(), testcase.name) - } + if testcase.fork == nil { continue } - // Should resolve the right address + sender, hash, gas, err := validateTx(tt.Txbytes, testcase.signer, testcase.isHomestead, testcase.isIstanbul, testcase.isShanghai) if err != nil { - return fmt.Errorf("got error, expected none: %v", err) + 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) } - if sender == nil { - return fmt.Errorf("sender was nil, should be %x", common.Address(testcase.fork.Sender)) + if common.Hash(*testcase.fork.Hash) != hash { + return fmt.Errorf("hash mismatch: got %x, want %x", hash, common.Hash(*testcase.fork.Hash)) } - if *sender != common.Address(testcase.fork.Sender) { + if common.Address(*testcase.fork.Sender) != sender { return fmt.Errorf("sender mismatch: got %x, want %x", sender, testcase.fork.Sender) } - if txhash == nil { - return fmt.Errorf("txhash was nil, should be %x", common.Hash(testcase.fork.Hash)) + if hash != common.Hash(*testcase.fork.Hash) { + return fmt.Errorf("hash mismatch: got %x, want %x", hash, testcase.fork.Hash) } - if *txhash != common.Hash(testcase.fork.Hash) { - return fmt.Errorf("hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash) + if uint64(testcase.fork.IntrinsicGas) != gas { + return fmt.Errorf("intrinsic gas mismatch: got %d, want %d", gas, uint64(testcase.fork.IntrinsicGas)) } } return nil