mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
add tests
This commit is contained in:
parent
f7adcc12ea
commit
64956f846a
1 changed files with 75 additions and 34 deletions
|
|
@ -43,11 +43,11 @@ import (
|
||||||
// TestSetFeeDefaults tests the logic for filling in default fee values works as expected.
|
// TestSetFeeDefaults tests the logic for filling in default fee values works as expected.
|
||||||
func TestSetFeeDefaults(t *testing.T) {
|
func TestSetFeeDefaults(t *testing.T) {
|
||||||
type test struct {
|
type test struct {
|
||||||
name string
|
name string
|
||||||
isLondon bool
|
fork string // options: legacy, london, cancun
|
||||||
in *TransactionArgs
|
in *TransactionArgs
|
||||||
want *TransactionArgs
|
want *TransactionArgs
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -62,28 +62,28 @@ func TestSetFeeDefaults(t *testing.T) {
|
||||||
// Legacy txs
|
// Legacy txs
|
||||||
{
|
{
|
||||||
"legacy tx pre-London",
|
"legacy tx pre-London",
|
||||||
false,
|
"legacy",
|
||||||
&TransactionArgs{},
|
&TransactionArgs{},
|
||||||
&TransactionArgs{GasPrice: fortytwo},
|
&TransactionArgs{GasPrice: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"legacy tx pre-London with zero price",
|
"legacy tx pre-London with zero price",
|
||||||
false,
|
"legacy",
|
||||||
&TransactionArgs{GasPrice: zero},
|
&TransactionArgs{GasPrice: zero},
|
||||||
&TransactionArgs{GasPrice: zero},
|
&TransactionArgs{GasPrice: zero},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"legacy tx post-London, explicit gas price",
|
"legacy tx post-London, explicit gas price",
|
||||||
true,
|
"london",
|
||||||
&TransactionArgs{GasPrice: fortytwo},
|
&TransactionArgs{GasPrice: fortytwo},
|
||||||
&TransactionArgs{GasPrice: fortytwo},
|
&TransactionArgs{GasPrice: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"legacy tx post-London with zero price",
|
"legacy tx post-London with zero price",
|
||||||
true,
|
"london",
|
||||||
&TransactionArgs{GasPrice: zero},
|
&TransactionArgs{GasPrice: zero},
|
||||||
nil,
|
nil,
|
||||||
errors.New("gasPrice must be non-zero after london fork"),
|
errors.New("gasPrice must be non-zero after london fork"),
|
||||||
|
|
@ -92,35 +92,35 @@ func TestSetFeeDefaults(t *testing.T) {
|
||||||
// Access list txs
|
// Access list txs
|
||||||
{
|
{
|
||||||
"access list tx pre-London",
|
"access list tx pre-London",
|
||||||
false,
|
"legacy",
|
||||||
&TransactionArgs{AccessList: al},
|
&TransactionArgs{AccessList: al},
|
||||||
&TransactionArgs{AccessList: al, GasPrice: fortytwo},
|
&TransactionArgs{AccessList: al, GasPrice: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"access list tx post-London, explicit gas price",
|
"access list tx post-London, explicit gas price",
|
||||||
false,
|
"legacy",
|
||||||
&TransactionArgs{AccessList: al, GasPrice: fortytwo},
|
&TransactionArgs{AccessList: al, GasPrice: fortytwo},
|
||||||
&TransactionArgs{AccessList: al, GasPrice: fortytwo},
|
&TransactionArgs{AccessList: al, GasPrice: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"access list tx post-London",
|
"access list tx post-London",
|
||||||
true,
|
"london",
|
||||||
&TransactionArgs{AccessList: al},
|
&TransactionArgs{AccessList: al},
|
||||||
&TransactionArgs{AccessList: al, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
&TransactionArgs{AccessList: al, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"access list tx post-London, only max fee",
|
"access list tx post-London, only max fee",
|
||||||
true,
|
"london",
|
||||||
&TransactionArgs{AccessList: al, MaxFeePerGas: maxFee},
|
&TransactionArgs{AccessList: al, MaxFeePerGas: maxFee},
|
||||||
&TransactionArgs{AccessList: al, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
&TransactionArgs{AccessList: al, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"access list tx post-London, only priority fee",
|
"access list tx post-London, only priority fee",
|
||||||
true,
|
"london",
|
||||||
&TransactionArgs{AccessList: al, MaxFeePerGas: maxFee},
|
&TransactionArgs{AccessList: al, MaxFeePerGas: maxFee},
|
||||||
&TransactionArgs{AccessList: al, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
&TransactionArgs{AccessList: al, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
|
|
@ -129,56 +129,56 @@ func TestSetFeeDefaults(t *testing.T) {
|
||||||
// Dynamic fee txs
|
// Dynamic fee txs
|
||||||
{
|
{
|
||||||
"dynamic tx post-London",
|
"dynamic tx post-London",
|
||||||
true,
|
"london",
|
||||||
&TransactionArgs{},
|
&TransactionArgs{},
|
||||||
&TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
&TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"dynamic tx post-London, only max fee",
|
"dynamic tx post-London, only max fee",
|
||||||
true,
|
"london",
|
||||||
&TransactionArgs{MaxFeePerGas: maxFee},
|
&TransactionArgs{MaxFeePerGas: maxFee},
|
||||||
&TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
&TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"dynamic tx post-London, only priority fee",
|
"dynamic tx post-London, only priority fee",
|
||||||
true,
|
"london",
|
||||||
&TransactionArgs{MaxFeePerGas: maxFee},
|
&TransactionArgs{MaxFeePerGas: maxFee},
|
||||||
&TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
&TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"dynamic fee tx pre-London, maxFee set",
|
"dynamic fee tx pre-London, maxFee set",
|
||||||
false,
|
"legacy",
|
||||||
&TransactionArgs{MaxFeePerGas: maxFee},
|
&TransactionArgs{MaxFeePerGas: maxFee},
|
||||||
nil,
|
nil,
|
||||||
errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active"),
|
errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"dynamic fee tx pre-London, priorityFee set",
|
"dynamic fee tx pre-London, priorityFee set",
|
||||||
false,
|
"legacy",
|
||||||
&TransactionArgs{MaxPriorityFeePerGas: fortytwo},
|
&TransactionArgs{MaxPriorityFeePerGas: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active"),
|
errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"dynamic fee tx, maxFee < priorityFee",
|
"dynamic fee tx, maxFee < priorityFee",
|
||||||
true,
|
"london",
|
||||||
&TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: (*hexutil.Big)(big.NewInt(1000))},
|
&TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: (*hexutil.Big)(big.NewInt(1000))},
|
||||||
nil,
|
nil,
|
||||||
errors.New("maxFeePerGas (0x3e) < maxPriorityFeePerGas (0x3e8)"),
|
errors.New("maxFeePerGas (0x3e) < maxPriorityFeePerGas (0x3e8)"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"dynamic fee tx, maxFee < priorityFee while setting default",
|
"dynamic fee tx, maxFee < priorityFee while setting default",
|
||||||
true,
|
"london",
|
||||||
&TransactionArgs{MaxFeePerGas: (*hexutil.Big)(big.NewInt(7))},
|
&TransactionArgs{MaxFeePerGas: (*hexutil.Big)(big.NewInt(7))},
|
||||||
nil,
|
nil,
|
||||||
errors.New("maxFeePerGas (0x7) < maxPriorityFeePerGas (0x2a)"),
|
errors.New("maxFeePerGas (0x7) < maxPriorityFeePerGas (0x2a)"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"dynamic fee tx post-London, explicit gas price",
|
"dynamic fee tx post-London, explicit gas price",
|
||||||
true,
|
"london",
|
||||||
&TransactionArgs{MaxFeePerGas: zero, MaxPriorityFeePerGas: zero},
|
&TransactionArgs{MaxFeePerGas: zero, MaxPriorityFeePerGas: zero},
|
||||||
nil,
|
nil,
|
||||||
errors.New("maxFeePerGas must be non-zero"),
|
errors.New("maxFeePerGas must be non-zero"),
|
||||||
|
|
@ -187,33 +187,60 @@ func TestSetFeeDefaults(t *testing.T) {
|
||||||
// Misc
|
// Misc
|
||||||
{
|
{
|
||||||
"set all fee parameters",
|
"set all fee parameters",
|
||||||
false,
|
"legacy",
|
||||||
&TransactionArgs{GasPrice: fortytwo, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
&TransactionArgs{GasPrice: fortytwo, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"),
|
errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"set gas price and maxPriorityFee",
|
"set gas price and maxPriorityFee",
|
||||||
false,
|
"legacy",
|
||||||
&TransactionArgs{GasPrice: fortytwo, MaxPriorityFeePerGas: fortytwo},
|
&TransactionArgs{GasPrice: fortytwo, MaxPriorityFeePerGas: fortytwo},
|
||||||
nil,
|
nil,
|
||||||
errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"),
|
errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"set gas price and maxFee",
|
"set gas price and maxFee",
|
||||||
true,
|
"london",
|
||||||
&TransactionArgs{GasPrice: fortytwo, MaxFeePerGas: maxFee},
|
&TransactionArgs{GasPrice: fortytwo, MaxFeePerGas: maxFee},
|
||||||
nil,
|
nil,
|
||||||
errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"),
|
errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"),
|
||||||
},
|
},
|
||||||
|
// EIP-4844
|
||||||
|
{
|
||||||
|
"set maxFeePerBlobGas pre cancun",
|
||||||
|
"london",
|
||||||
|
&TransactionArgs{MaxFeePerBlobGas: fortytwo},
|
||||||
|
nil,
|
||||||
|
errors.New("maxFeePerBlobGas is not valid before Cancun is active"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"set maxFeePerBlobGas pre london",
|
||||||
|
"legacy",
|
||||||
|
&TransactionArgs{MaxFeePerBlobGas: fortytwo},
|
||||||
|
nil,
|
||||||
|
errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"set gas price and maxFee for blob-tx",
|
||||||
|
"cancun",
|
||||||
|
&TransactionArgs{GasPrice: fortytwo, MaxFeePerGas: maxFee, BlobVersionedHashes: []common.Hash{}},
|
||||||
|
nil,
|
||||||
|
errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fill maxFeePerBlobGas",
|
||||||
|
"cancun",
|
||||||
|
&TransactionArgs{BlobVersionedHashes: []common.Hash{}},
|
||||||
|
&TransactionArgs{BlobVersionedHashes: []common.Hash{}, MaxFeePerBlobGas: (*hexutil.Big)(big.NewInt(4)), MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
if test.isLondon {
|
if err := b.setFork(test.fork); err != nil {
|
||||||
b.activateLondon()
|
t.Fatalf("failed to set fork: %v", err)
|
||||||
} else {
|
|
||||||
b.deactivateLondon()
|
|
||||||
}
|
}
|
||||||
got := test.in
|
got := test.in
|
||||||
err := got.setFeeDefaults(ctx, b)
|
err := got.setFeeDefaults(ctx, b)
|
||||||
|
|
@ -235,6 +262,7 @@ type backendMock struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newBackendMock() *backendMock {
|
func newBackendMock() *backendMock {
|
||||||
|
var cancunTime uint64 = 600
|
||||||
config := ¶ms.ChainConfig{
|
config := ¶ms.ChainConfig{
|
||||||
ChainID: big.NewInt(42),
|
ChainID: big.NewInt(42),
|
||||||
HomesteadBlock: big.NewInt(0),
|
HomesteadBlock: big.NewInt(0),
|
||||||
|
|
@ -250,6 +278,7 @@ func newBackendMock() *backendMock {
|
||||||
MuirGlacierBlock: big.NewInt(0),
|
MuirGlacierBlock: big.NewInt(0),
|
||||||
BerlinBlock: big.NewInt(0),
|
BerlinBlock: big.NewInt(0),
|
||||||
LondonBlock: big.NewInt(1000),
|
LondonBlock: big.NewInt(1000),
|
||||||
|
CancunTime: &cancunTime,
|
||||||
}
|
}
|
||||||
return &backendMock{
|
return &backendMock{
|
||||||
current: &types.Header{
|
current: &types.Header{
|
||||||
|
|
@ -265,13 +294,25 @@ func newBackendMock() *backendMock {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *backendMock) activateLondon() {
|
func (b *backendMock) setFork(fork string) error {
|
||||||
b.current.Number = big.NewInt(1100)
|
if fork == "legacy" {
|
||||||
|
b.current.Number = big.NewInt(900)
|
||||||
|
b.current.Time = 555
|
||||||
|
} else if fork == "london" {
|
||||||
|
b.current.Number = big.NewInt(1100)
|
||||||
|
b.current.Time = 555
|
||||||
|
} else if fork == "cancun" {
|
||||||
|
b.current.Number = big.NewInt(1100)
|
||||||
|
b.current.Time = 700
|
||||||
|
// Blob base fee will be 2
|
||||||
|
excess := uint64(2314058)
|
||||||
|
b.current.ExcessBlobGas = &excess
|
||||||
|
} else {
|
||||||
|
return errors.New("invalid fork")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *backendMock) deactivateLondon() {
|
|
||||||
b.current.Number = big.NewInt(900)
|
|
||||||
}
|
|
||||||
func (b *backendMock) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
|
func (b *backendMock) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
|
||||||
return big.NewInt(42), nil
|
return big.NewInt(42), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue