add in data floor gas check

This commit is contained in:
Sina Mahmoodi 2025-03-14 11:10:17 +01:00
parent 0642d743e6
commit 48c2cab486
2 changed files with 55 additions and 41 deletions

View file

@ -20,7 +20,6 @@ import (
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
) )
func TestTransaction(t *testing.T) { func TestTransaction(t *testing.T) {
@ -58,8 +57,7 @@ func TestTransaction(t *testing.T) {
txt.skipLoad("^ttEIP1559/GasLimitPriceProductOverflow.json") txt.skipLoad("^ttEIP1559/GasLimitPriceProductOverflow.json")
txt.walk(t, transactionTestDir, func(t *testing.T, name string, test *TransactionTest) { txt.walk(t, transactionTestDir, func(t *testing.T, name string, test *TransactionTest) {
cfg := params.MainnetChainConfig if err := txt.checkFailure(t, test.Run()); err != nil {
if err := txt.checkFailure(t, test.Run(cfg)); err != nil {
t.Error(err) t.Error(err)
} }
}) })
@ -75,8 +73,7 @@ func TestExecutionSpecTransaction(t *testing.T) {
st.skipLoad("^prague/eip7702_set_code_tx/invalid_tx/empty_authorization_list.json") st.skipLoad("^prague/eip7702_set_code_tx/invalid_tx/empty_authorization_list.json")
st.walk(t, executionSpecTransactionTestDir, func(t *testing.T, name string, test *TransactionTest) { st.walk(t, executionSpecTransactionTestDir, func(t *testing.T, name string, test *TransactionTest) {
cfg := params.MainnetChainConfig if err := st.checkFailure(t, test.Run()); err != nil {
if err := st.checkFailure(t, test.Run(cfg)); err != nil {
t.Error(err) t.Error(err)
} }
}) })

View file

@ -18,6 +18,7 @@ package tests
import ( import (
"fmt" "fmt"
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -65,11 +66,11 @@ func (tt *TransactionTest) validateFork(fork *ttFork) error {
return nil return nil
} }
func (tt *TransactionTest) Run(config *params.ChainConfig) error { func (tt *TransactionTest) Run() error {
if err := tt.validate(); err != nil { if err := tt.validate(); err != nil {
return err return err
} }
validateTx := func(rlpData hexutil.Bytes, signer types.Signer, isHomestead, isIstanbul, isShanghai bool) (sender common.Address, hash common.Hash, requiredGas uint64, err error) { validateTx := func(rlpData hexutil.Bytes, signer types.Signer, rules *params.Rules) (sender common.Address, hash common.Hash, requiredGas uint64, err error) {
tx := new(types.Transaction) tx := new(types.Transaction)
if err = tx.UnmarshalBinary(rlpData); err != nil { if err = tx.UnmarshalBinary(rlpData); err != nil {
return return
@ -79,62 +80,78 @@ func (tt *TransactionTest) Run(config *params.ChainConfig) error {
return return
} }
// Intrinsic gas // Intrinsic gas
requiredGas, err = core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, isHomestead, isIstanbul, isShanghai) requiredGas, err = core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai)
if err != nil { if err != nil {
return return
} }
if requiredGas > tx.Gas() { if requiredGas > tx.Gas() {
return sender, hash, 0, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas) return sender, hash, 0, fmt.Errorf("insufficient gas ( %d < %d )", tx.Gas(), requiredGas)
} }
if rules.IsPrague {
var floorDataGas uint64
floorDataGas, err = core.FloorDataGas(tx.Data())
if err != nil {
return
}
if tx.Gas() < floorDataGas {
return sender, hash, 0, fmt.Errorf("%w: have %d, want %d", core.ErrFloorDataGas, tx.Gas(), floorDataGas)
}
}
hash = tx.Hash() hash = tx.Hash()
return sender, hash, requiredGas, nil return sender, hash, requiredGas, nil
} }
for _, testcase := range []struct { for _, testcase := range []struct {
name string name string
signer types.Signer isMerge bool
fork *ttFork
isHomestead bool
isIstanbul bool
isShanghai bool
}{ }{
{"Frontier", types.FrontierSigner{}, tt.Result["Frontier"], false, false, false}, {"Frontier", false},
{"Homestead", types.HomesteadSigner{}, tt.Result["Homestead"], true, false, false}, {"Homestead", false},
{"EIP150", types.HomesteadSigner{}, tt.Result["EIP150"], true, false, false}, {"EIP150", false},
{"EIP158", types.NewEIP155Signer(config.ChainID), tt.Result["EIP158"], true, false, false}, {"EIP158", false},
{"Byzantium", types.NewEIP155Signer(config.ChainID), tt.Result["Byzantium"], true, false, false}, {"Byzantium", false},
{"Constantinople", types.NewEIP155Signer(config.ChainID), tt.Result["Constantinople"], true, false, false}, {"Constantinople", false},
{"Istanbul", types.NewEIP155Signer(config.ChainID), tt.Result["Istanbul"], true, true, false}, {"Istanbul", false},
{"Berlin", types.NewEIP2930Signer(config.ChainID), tt.Result["Berlin"], true, true, false}, {"Berlin", false},
{"London", types.NewLondonSigner(config.ChainID), tt.Result["London"], true, true, false}, {"London", false},
{"Paris", types.NewLondonSigner(config.ChainID), tt.Result["Paris"], true, true, false}, {"Paris", true},
{"Shanghai", types.NewLondonSigner(config.ChainID), tt.Result["Shanghai"], true, true, true}, {"Shanghai", true},
{"Cancun", types.NewCancunSigner(config.ChainID), tt.Result["Cancun"], true, true, true}, {"Cancun", true},
{"Prague", types.NewPragueSigner(config.ChainID), tt.Result["Prague"], true, true, true}, {"Prague", true},
} { } {
if testcase.fork == nil { expected := tt.Result[testcase.name]
if expected == nil {
continue continue
} }
sender, hash, gas, err := validateTx(tt.Txbytes, testcase.signer, testcase.isHomestead, testcase.isIstanbul, testcase.isShanghai) config, ok := Forks[testcase.name]
if !ok || config == nil {
return UnsupportedForkError{Name: testcase.name}
}
var (
rules = config.Rules(new(big.Int), testcase.isMerge, 0)
signer = types.MakeSigner(config, new(big.Int), 0)
)
sender, hash, gas, err := validateTx(tt.Txbytes, signer, &rules)
if err != nil { if err != nil {
if testcase.fork.Hash != nil { if expected.Hash != nil {
return fmt.Errorf("unexpected error: %v", err) return fmt.Errorf("unexpected error fork %s: %v", testcase.name, err)
} }
continue continue
} }
if testcase.fork.Exception != nil { if expected.Exception != nil {
return fmt.Errorf("expected error %v, got none (%v)", *testcase.fork.Exception, err) return fmt.Errorf("expected error %v, got none (%v), fork %s", *expected.Exception, err, testcase.name)
} }
if common.Hash(*testcase.fork.Hash) != hash { if common.Hash(*expected.Hash) != hash {
return fmt.Errorf("hash mismatch: got %x, want %x", hash, common.Hash(*testcase.fork.Hash)) return fmt.Errorf("hash mismatch: got %x, want %x", hash, common.Hash(*expected.Hash))
} }
if common.Address(*testcase.fork.Sender) != sender { if common.Address(*expected.Sender) != sender {
return fmt.Errorf("sender mismatch: got %x, want %x", sender, testcase.fork.Sender) return fmt.Errorf("sender mismatch: got %x, want %x", sender, expected.Sender)
} }
if hash != common.Hash(*testcase.fork.Hash) { if hash != common.Hash(*expected.Hash) {
return fmt.Errorf("hash mismatch: got %x, want %x", hash, testcase.fork.Hash) return fmt.Errorf("hash mismatch: got %x, want %x", hash, expected.Hash)
} }
if uint64(testcase.fork.IntrinsicGas) != gas { if uint64(expected.IntrinsicGas) != gas {
return fmt.Errorf("intrinsic gas mismatch: got %d, want %d", gas, uint64(testcase.fork.IntrinsicGas)) return fmt.Errorf("intrinsic gas mismatch: got %d, want %d", gas, uint64(expected.IntrinsicGas))
} }
} }
return nil return nil