Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit 941d00a

Browse files
authored
Refactor: Move gas fee check to own file with test (#71)
1 parent b28208e commit 941d00a

File tree

5 files changed

+105
-8
lines changed

5 files changed

+105
-8
lines changed

internal/testutils/checksmock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package testutils
22

33
import (
4+
"math/big"
5+
46
"github.com/ethereum/go-ethereum/common"
57
"github.com/stackup-wallet/stackup-bundler/pkg/entrypoint"
68
)
@@ -28,3 +30,9 @@ func MockGetNotStake(addr common.Address) (*entrypoint.IStakeManagerDepositInfo,
2830
func MockGetNotStakeZeroDeposit(addr common.Address) (*entrypoint.IStakeManagerDepositInfo, error) {
2931
return NonStakedZeroDepositInfo, nil
3032
}
33+
34+
func GetMockGasTipFunc(val *big.Int) func() (*big.Int, error) {
35+
return func() (*big.Int, error) {
36+
return val, nil
37+
}
38+
}

pkg/modules/checks/sanitychecks.go renamed to pkg/modules/checks/gasfee.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package checks
22

33
import (
4-
"context"
54
"fmt"
65

7-
"github.com/ethereum/go-ethereum/ethclient"
86
"github.com/stackup-wallet/stackup-bundler/pkg/userop"
97
)
108

11-
// The maxFeePerGas and maxPriorityFeePerGas are above a configurable minimum value that the client
12-
// is willing to accept. At the minimum, they are sufficiently high to be included with the current
13-
// block.basefee.
14-
func checkFeePerGas(eth *ethclient.Client, op *userop.UserOperation) error {
15-
tip, err := eth.SuggestGasTipCap(context.Background())
9+
// ValidateFeePerGas checks the maxFeePerGas and maxPriorityFeePerGas are above a configurable minimum value
10+
// that the client is willing to accept. At the minimum, they are sufficiently high to be included with the
11+
// current block.basefee.
12+
func ValidateFeePerGas(op *userop.UserOperation, gt GetGasTipFunc) error {
13+
tip, err := gt()
1614
if err != nil {
1715
return err
1816
}

pkg/modules/checks/gasfee_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package checks
2+
3+
import (
4+
"math/big"
5+
"testing"
6+
7+
"github.com/ethereum/go-ethereum/common"
8+
"github.com/stackup-wallet/stackup-bundler/internal/testutils"
9+
)
10+
11+
// TestMPFLessThanGasTip calls checks.ValidateFeePerGas with a maxPriorityFeePerGas < gas tip. Expect error.
12+
func TestMPFLessThanGasTip(t *testing.T) {
13+
op := testutils.MockValidInitUserOp()
14+
gt := testutils.GetMockGasTipFunc(big.NewInt(0).Add(op.MaxPriorityFeePerGas, common.Big1))
15+
err := ValidateFeePerGas(op, gt)
16+
17+
if err == nil {
18+
t.Fatal("got nil, want err")
19+
}
20+
}
21+
22+
// TestMPFEqualGasTip calls checks.ValidateFeePerGas with a maxPriorityFeePerGas == gas tip. Expect nil.
23+
func TestMPFEqualGasTip(t *testing.T) {
24+
op := testutils.MockValidInitUserOp()
25+
gt := testutils.GetMockGasTipFunc(big.NewInt(0).Add(op.MaxPriorityFeePerGas, common.Big0))
26+
err := ValidateFeePerGas(op, gt)
27+
28+
if err != nil {
29+
t.Fatalf("got %v, want nil", err)
30+
}
31+
}
32+
33+
// TestMPFMoreThanGasTip calls checks.ValidateFeePerGas with a maxPriorityFeePerGas > gas tip. Expect nil.
34+
func TestMPFMoreThanGasTip(t *testing.T) {
35+
op := testutils.MockValidInitUserOp()
36+
gt := testutils.GetMockGasTipFunc(big.NewInt(0).Sub(op.MaxPriorityFeePerGas, common.Big1))
37+
err := ValidateFeePerGas(op, gt)
38+
39+
if err != nil {
40+
t.Fatalf("got %v, want nil", err)
41+
}
42+
}
43+
44+
// TestMFLessThanMPF calls checks.ValidateFeePerGas with a MaxFeePerGas < maxPriorityFeePerGas. Expect error.
45+
func TestMFLessThanMPF(t *testing.T) {
46+
op := testutils.MockValidInitUserOp()
47+
gt := testutils.GetMockGasTipFunc(big.NewInt(0).Add(op.MaxPriorityFeePerGas, common.Big0))
48+
op.MaxFeePerGas = big.NewInt(0).Sub(op.MaxPriorityFeePerGas, common.Big1)
49+
err := ValidateFeePerGas(op, gt)
50+
51+
if err == nil {
52+
t.Fatal("got nil, want err")
53+
}
54+
}
55+
56+
// TestMFEqualMPF calls checks.ValidateFeePerGas with a MaxFeePerGas == maxPriorityFeePerGas. Expect nil.
57+
func TestMFEqualMPF(t *testing.T) {
58+
op := testutils.MockValidInitUserOp()
59+
gt := testutils.GetMockGasTipFunc(big.NewInt(0).Add(op.MaxPriorityFeePerGas, common.Big0))
60+
op.MaxFeePerGas = big.NewInt(0).Add(op.MaxPriorityFeePerGas, common.Big0)
61+
err := ValidateFeePerGas(op, gt)
62+
63+
if err != nil {
64+
t.Fatalf("got %v, want nil", err)
65+
}
66+
}
67+
68+
// TestMFMoreThanMPF calls checks.ValidateFeePerGas with a MaxFeePerGas > maxPriorityFeePerGas. Expect nil.
69+
func TestMFMoreThanMPF(t *testing.T) {
70+
op := testutils.MockValidInitUserOp()
71+
gt := testutils.GetMockGasTipFunc(big.NewInt(0).Add(op.MaxPriorityFeePerGas, common.Big0))
72+
op.MaxFeePerGas = big.NewInt(0).Add(op.MaxPriorityFeePerGas, common.Big1)
73+
err := ValidateFeePerGas(op, gt)
74+
75+
if err != nil {
76+
t.Fatalf("got %v, want nil", err)
77+
}
78+
}

pkg/modules/checks/standalone.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func New(rpc *rpc.Client, maxVerificationGas *big.Int, tracer string) *Standalon
3535
func (s *Standalone) ValidateOpValues() modules.UserOpHandlerFunc {
3636
return func(ctx *modules.UserOpHandlerCtx) error {
3737
gc := getCodeWithEthClient(s.eth)
38+
gt := getGasTipWithEthClient(s.eth)
3839
gs, err := getStakeWithEthClient(ctx, s.eth)
3940
if err != nil {
4041
return err
@@ -46,7 +47,7 @@ func (s *Standalone) ValidateOpValues() modules.UserOpHandlerFunc {
4647
g.Go(func() error { return ValidateVerificationGas(ctx.UserOp, s.maxVerificationGas) })
4748
g.Go(func() error { return ValidatePaymasterAndData(ctx.UserOp, gc, gs) })
4849
g.Go(func() error { return ValidateCallGasLimit(ctx.UserOp) })
49-
g.Go(func() error { return checkFeePerGas(s.eth, ctx.UserOp) })
50+
g.Go(func() error { return ValidateFeePerGas(ctx.UserOp, gt) })
5051

5152
return g.Wait()
5253
}

pkg/modules/checks/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package checks
22

33
import (
44
"context"
5+
"math/big"
56

67
"github.com/ethereum/go-ethereum/common"
78
"github.com/ethereum/go-ethereum/ethclient"
@@ -15,6 +16,10 @@ type GetCodeFunc = func(addr common.Address) ([]byte, error)
1516
// GetStakeFunc provides a general interface for retrieving the EntryPoint stake for a given address.
1617
type GetStakeFunc = func(entity common.Address) (*entrypoint.IStakeManagerDepositInfo, error)
1718

19+
// GetGasTipFunc provides a general interface for retrieving an EIP-1559 style gas tip to allow for timely
20+
// execution of a transaction.
21+
type GetGasTipFunc = func() (*big.Int, error)
22+
1823
// getCodeWithEthClient returns a GetCodeFunc that uses an eth client to call eth_getCode.
1924
func getCodeWithEthClient(eth *ethclient.Client) GetCodeFunc {
2025
return func(addr common.Address) ([]byte, error) {
@@ -40,3 +45,10 @@ func getStakeWithEthClient(ctx *modules.UserOpHandlerCtx, eth *ethclient.Client)
4045
return &dep, nil
4146
}, nil
4247
}
48+
49+
// getGasTipWithEthClient returns a GetGasTipFunc that uses an eth client to call eth_maxPriorityFeePerGas.
50+
func getGasTipWithEthClient(eth *ethclient.Client) GetGasTipFunc {
51+
return func() (*big.Int, error) {
52+
return eth.SuggestGasTipCap(context.Background())
53+
}
54+
}

0 commit comments

Comments
 (0)