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

Commit b28208e

Browse files
authored
Refactor: Move callGasLimit check to own file with test (#70)
1 parent 0763a82 commit b28208e

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

pkg/modules/checks/callgas.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package checks
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/stackup-wallet/stackup-bundler/pkg/gas"
7+
"github.com/stackup-wallet/stackup-bundler/pkg/userop"
8+
)
9+
10+
// ValidateCallGasLimit checks the callGasLimit is at least the cost of a CALL with non-zero value.
11+
func ValidateCallGasLimit(op *userop.UserOperation) error {
12+
cg := gas.NewDefaultOverhead().NonZeroValueCall()
13+
if op.CallGasLimit.Cmp(cg) < 0 {
14+
return fmt.Errorf("callGasLimit: below expected gas of %s", cg.String())
15+
}
16+
17+
return nil
18+
}

pkg/modules/checks/callgas_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
"github.com/stackup-wallet/stackup-bundler/pkg/gas"
10+
)
11+
12+
// TestOpCGLessThanOH calls checks.ValidateCallGasLimit where callGas < overhead. Expect error.
13+
func TestOpCGLessThanOH(t *testing.T) {
14+
op := testutils.MockValidInitUserOp()
15+
cg := gas.NewDefaultOverhead().NonZeroValueCall()
16+
op.CallGasLimit = big.NewInt(0).Sub(cg, common.Big1)
17+
err := ValidateCallGasLimit(op)
18+
19+
if err == nil {
20+
t.Fatalf("got nil, want err")
21+
}
22+
}
23+
24+
// TestOpCGEqualOH calls checks.ValidateCallGasLimit where callGas == overhead. Expect nil.
25+
func TestOpCGEqualOH(t *testing.T) {
26+
op := testutils.MockValidInitUserOp()
27+
cg := gas.NewDefaultOverhead().NonZeroValueCall()
28+
op.CallGasLimit = big.NewInt(0).Add(cg, common.Big0)
29+
err := ValidateCallGasLimit(op)
30+
31+
if err != nil {
32+
t.Fatalf("got %v, want nil", err)
33+
}
34+
}
35+
36+
// TestOpCGMoreThanOH calls checks.ValidateCallGasLimit where callGas > overhead. Expect nil.
37+
func TestOpCGMoreThanOH(t *testing.T) {
38+
op := testutils.MockValidInitUserOp()
39+
cg := gas.NewDefaultOverhead().NonZeroValueCall()
40+
op.CallGasLimit = big.NewInt(0).Add(cg, common.Big1)
41+
err := ValidateCallGasLimit(op)
42+
43+
if err != nil {
44+
t.Fatalf("got %v, want nil", err)
45+
}
46+
}

pkg/modules/checks/sanitychecks.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,9 @@ import (
55
"fmt"
66

77
"github.com/ethereum/go-ethereum/ethclient"
8-
"github.com/stackup-wallet/stackup-bundler/pkg/gas"
98
"github.com/stackup-wallet/stackup-bundler/pkg/userop"
109
)
1110

12-
// Checks the callGasLimit is at least the cost of a CALL with non-zero value.
13-
func checkCallGasLimit(op *userop.UserOperation) error {
14-
cg := gas.NewDefaultOverhead().NonZeroValueCall()
15-
if op.CallGasLimit.Cmp(cg) < 0 {
16-
return fmt.Errorf("callGasLimit: below expected gas of %s", cg.String())
17-
}
18-
19-
return nil
20-
}
21-
2211
// The maxFeePerGas and maxPriorityFeePerGas are above a configurable minimum value that the client
2312
// is willing to accept. At the minimum, they are sufficiently high to be included with the current
2413
// block.basefee.

pkg/modules/checks/standalone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (s *Standalone) ValidateOpValues() modules.UserOpHandlerFunc {
4545
g.Go(func() error { return ValidateInitCode(ctx.UserOp, gs) })
4646
g.Go(func() error { return ValidateVerificationGas(ctx.UserOp, s.maxVerificationGas) })
4747
g.Go(func() error { return ValidatePaymasterAndData(ctx.UserOp, gc, gs) })
48-
g.Go(func() error { return checkCallGasLimit(ctx.UserOp) })
48+
g.Go(func() error { return ValidateCallGasLimit(ctx.UserOp) })
4949
g.Go(func() error { return checkFeePerGas(s.eth, ctx.UserOp) })
5050

5151
return g.Wait()

0 commit comments

Comments
 (0)