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

Commit 3c3cacb

Browse files
authored
Refactor: Move VerificationGas checks to own file with tests (#68)
1 parent ed1d2f6 commit 3c3cacb

File tree

4 files changed

+105
-18
lines changed

4 files changed

+105
-18
lines changed

pkg/modules/checks/sanitychecks.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"math/big"
87

98
"github.com/ethereum/go-ethereum/common"
109
"github.com/ethereum/go-ethereum/ethclient"
@@ -13,22 +12,6 @@ import (
1312
"github.com/stackup-wallet/stackup-bundler/pkg/userop"
1413
)
1514

16-
// Checks that the verificationGasLimit is sufficiently low (<= MAX_VERIFICATION_GAS) and the
17-
// preVerificationGas is sufficiently high (enough to pay for the calldata gas cost of serializing
18-
// the UserOperation plus PRE_VERIFICATION_OVERHEAD_GAS)
19-
func checkVerificationGas(maxVerificationGas *big.Int, op *userop.UserOperation) error {
20-
if op.VerificationGasLimit.Cmp(maxVerificationGas) > 0 {
21-
return fmt.Errorf("verificationGasLimit: exceeds maxVerificationGas of %s", maxVerificationGas.String())
22-
}
23-
24-
pvg := gas.NewDefaultOverhead().CalcPreVerificationGas(op)
25-
if op.PreVerificationGas.Cmp(pvg) < 0 {
26-
return fmt.Errorf("preVerificationGas: below expected gas of %s", pvg.String())
27-
}
28-
29-
return nil
30-
}
31-
3215
// Checks the paymasterAndData is either zero bytes or the first 20 bytes contain an address that
3316
//
3417
// 1. is not the zero address

pkg/modules/checks/standalone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (s *Standalone) ValidateOpValues() modules.UserOpHandlerFunc {
4848
g := new(errgroup.Group)
4949
g.Go(func() error { return ValidateSender(ctx.UserOp, getCode) })
5050
g.Go(func() error { return ValidateInitCode(ctx.UserOp, getStake) })
51-
g.Go(func() error { return checkVerificationGas(s.maxVerificationGas, ctx.UserOp) })
51+
g.Go(func() error { return ValidateVerificationGas(ctx.UserOp, s.maxVerificationGas) })
5252
g.Go(func() error { return checkPaymasterAndData(s.eth, ep, ctx.UserOp) })
5353
g.Go(func() error { return checkCallGasLimit(ctx.UserOp) })
5454
g.Go(func() error { return checkFeePerGas(s.eth, ctx.UserOp) })

pkg/modules/checks/verificationgas.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package checks
2+
3+
import (
4+
"fmt"
5+
"math/big"
6+
7+
"github.com/stackup-wallet/stackup-bundler/pkg/gas"
8+
"github.com/stackup-wallet/stackup-bundler/pkg/userop"
9+
)
10+
11+
// ValidateVerificationGas checks that the verificationGasLimit is sufficiently low (<= MAX_VERIFICATION_GAS)
12+
// and the preVerificationGas is sufficiently high (enough to pay for the calldata gas cost of serializing
13+
// the UserOperation plus PRE_VERIFICATION_OVERHEAD_GAS).
14+
func ValidateVerificationGas(op *userop.UserOperation, maxVerificationGas *big.Int) error {
15+
if op.VerificationGasLimit.Cmp(maxVerificationGas) > 0 {
16+
return fmt.Errorf("verificationGasLimit: exceeds maxVerificationGas of %s", maxVerificationGas.String())
17+
}
18+
19+
pvg := gas.NewDefaultOverhead().CalcPreVerificationGas(op)
20+
if op.PreVerificationGas.Cmp(pvg) < 0 {
21+
return fmt.Errorf("preVerificationGas: below expected gas of %s", pvg.String())
22+
}
23+
24+
return nil
25+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
// TestOpVGlessThanMaxVG calls checks.ValidateVerificationGas where verificationGas < MAX_VERIFICATION_GAS.
13+
// Expects nil.
14+
func TestOpVGlessThanMaxVG(t *testing.T) {
15+
op := testutils.MockValidInitUserOp()
16+
mvg := big.NewInt(0).Add(op.VerificationGasLimit, common.Big1)
17+
18+
if err := ValidateVerificationGas(op, mvg); err != nil {
19+
t.Fatalf("got %v, want nil", err)
20+
}
21+
}
22+
23+
// TestOpVGEqualMaxVG calls checks.ValidateVerificationGas where verificationGas == MAX_VERIFICATION_GAS.
24+
// Expects nil.
25+
func TestOpVGEqualMaxVG(t *testing.T) {
26+
op := testutils.MockValidInitUserOp()
27+
mvg := big.NewInt(0).Add(op.VerificationGasLimit, common.Big0)
28+
29+
if err := ValidateVerificationGas(op, mvg); err != nil {
30+
t.Fatalf("got %v, want nil", err)
31+
}
32+
}
33+
34+
// TestOpVGMoreThanMaxVG calls checks.ValidateVerificationGas where verificationGas > MAX_VERIFICATION_GAS.
35+
// Expects error.
36+
func TestOpVGMoreThanMaxVG(t *testing.T) {
37+
op := testutils.MockValidInitUserOp()
38+
mvg := big.NewInt(0).Sub(op.VerificationGasLimit, common.Big1)
39+
40+
if err := ValidateVerificationGas(op, mvg); err == nil {
41+
t.Fatal("got nil, want err")
42+
}
43+
}
44+
45+
// TestOpPVGMoreThanOH calls checks.ValidateVerificationGas where the preVerificationGas > overhead gas.
46+
// Expect nil.
47+
func TestOpPVGMoreThanOH(t *testing.T) {
48+
op := testutils.MockValidInitUserOp()
49+
pvg := gas.NewDefaultOverhead().CalcPreVerificationGas(op)
50+
op.PreVerificationGas = big.NewInt(0).Add(pvg, common.Big1)
51+
52+
if err := ValidateVerificationGas(op, op.VerificationGasLimit); err != nil {
53+
t.Fatalf("got %v, want nil", err)
54+
}
55+
}
56+
57+
// TestOpPVGEqualOH calls checks.ValidateVerificationGas where the preVerificationGas == overhead gas. Expect
58+
// nil.
59+
func TestOpPVGEqualOH(t *testing.T) {
60+
op := testutils.MockValidInitUserOp()
61+
pvg := gas.NewDefaultOverhead().CalcPreVerificationGas(op)
62+
op.PreVerificationGas = big.NewInt(0).Add(pvg, common.Big0)
63+
64+
if err := ValidateVerificationGas(op, op.VerificationGasLimit); err != nil {
65+
t.Fatalf("got %v, want nil", err)
66+
}
67+
}
68+
69+
// TestOpPVGLessThanOH calls checks.ValidateVerificationGas where the preVerificationGas < overhead gas.
70+
// Expect error.
71+
func TestOpPVGLessThanOH(t *testing.T) {
72+
op := testutils.MockValidInitUserOp()
73+
pvg := gas.NewDefaultOverhead().CalcPreVerificationGas(op)
74+
op.PreVerificationGas = big.NewInt(0).Sub(pvg, common.Big1)
75+
76+
if err := ValidateVerificationGas(op, op.VerificationGasLimit); err == nil {
77+
t.Fatal("got nil, want err")
78+
}
79+
}

0 commit comments

Comments
 (0)