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

Commit 27e6fd7

Browse files
authored
Add support for Arbitrum (#156)
1 parent 2a046c9 commit 27e6fd7

File tree

9 files changed

+177
-33
lines changed

9 files changed

+177
-33
lines changed
File renamed without changes.

internal/start/private.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func PrivateMode() {
5959

6060
ov := gas.NewDefaultOverhead()
6161
if chain.Cmp(config.ArbitrumOneChainID) == 0 || chain.Cmp(config.ArbitrumGoerliChainID) == 0 {
62-
ov.SetCalcPreVerificationGasFunc(gas.CalcArbitrumPVGWithEthClient(eth))
62+
ov.SetCalcPreVerificationGasFunc(gas.CalcArbitrumPVGWithEthClient(rpc))
6363
}
6464

6565
mem, err := mempool.New(db)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package nodeinterface
2+
3+
import "github.com/ethereum/go-ethereum/common"
4+
5+
var (
6+
ERC4337GasHelperAddress = common.HexToAddress("0x559e3c6A74678FDBE1Fcc54153A8D7Dd7049FBCA")
7+
PrecompileAddress = common.HexToAddress("0x00000000000000000000000000000000000000C8")
8+
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package nodeinterface
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"math/big"
7+
8+
"github.com/ethereum/go-ethereum/accounts/abi"
9+
"github.com/ethereum/go-ethereum/common/hexutil"
10+
)
11+
12+
var (
13+
addressT, _ = abi.NewType("address", "", nil)
14+
boolT, _ = abi.NewType("bool", "", nil)
15+
bytesT, _ = abi.NewType("bytes", "", nil)
16+
uint64T, _ = abi.NewType("uint64", "", nil)
17+
uint256T, _ = abi.NewType("uint256", "", nil)
18+
19+
GasEstimateL1ComponentMethod = abi.NewMethod(
20+
"gasEstimateL1Component",
21+
"gasEstimateL1Component",
22+
abi.Function,
23+
"",
24+
false,
25+
true,
26+
abi.Arguments{
27+
{Name: "to", Type: addressT},
28+
{Name: "contractCreation", Type: boolT},
29+
{Name: "data", Type: bytesT},
30+
},
31+
abi.Arguments{
32+
{Name: "gasEstimateForL1", Type: uint64T},
33+
{Name: "baseFee", Type: uint256T},
34+
{Name: "l1BaseFeeEstimate", Type: uint256T},
35+
},
36+
)
37+
)
38+
39+
type GasEstimateL1ComponentOutput struct {
40+
GasEstimateForL1 uint64
41+
BaseFee *big.Int
42+
L1BaseFeeEstimate *big.Int
43+
}
44+
45+
func DecodeGasEstimateL1ComponentOutput(out any) (*GasEstimateL1ComponentOutput, error) {
46+
hex, ok := out.(string)
47+
if !ok {
48+
return nil, errors.New("gasEstimateL1Component: cannot assert type: hex is not of type string")
49+
}
50+
data, err := hexutil.Decode(hex)
51+
if err != nil {
52+
return nil, fmt.Errorf("gasEstimateL1Component: %s", err)
53+
}
54+
55+
args, err := GasEstimateL1ComponentMethod.Outputs.Unpack(data)
56+
if err != nil {
57+
return nil, fmt.Errorf("gasEstimateL1Component: %s", err)
58+
}
59+
60+
return &GasEstimateL1ComponentOutput{
61+
GasEstimateForL1: args[0].(uint64),
62+
BaseFee: args[1].(*big.Int),
63+
L1BaseFeeEstimate: args[2].(*big.Int),
64+
}, nil
65+
}

pkg/client/client.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package client
33

44
import (
5-
"bytes"
65
"errors"
76
"math/big"
87

@@ -173,28 +172,18 @@ func (i *Client) EstimateUserOperationGas(op map[string]any, ep string) (*gas.Ga
173172
return nil, err
174173
}
175174

176-
// Create a new op with updated gas limits
177-
data, err := userOp.ToMap()
178-
if err != nil {
179-
l.Error(err, "eth_estimateUserOperationGas error")
180-
return nil, err
181-
}
182-
data["verificationGasLimit"] = hexutil.EncodeBig(big.NewInt(int64(vg)))
183-
data["callGasLimit"] = hexutil.EncodeBig(big.NewInt(int64(cg)))
184-
data["signature"] = hexutil.Encode(bytes.Repeat([]byte{1}, len(userOp.Signature)))
185-
userOp, err = userop.New(data)
175+
// Calculate PreVerificationGas
176+
pvg, err := i.ov.CalcPreVerificationGas(userOp)
186177
if err != nil {
187178
l.Error(err, "eth_estimateUserOperationGas error")
188179
return nil, err
189180
}
190181

191-
// Return gas values with a PVG calculation that takes into account updated gas limits and a signature
192-
// with no zero bytes.
193182
l.Info("eth_estimateUserOperationGas ok")
194183
return &gas.GasEstimates{
195-
PreVerificationGas: i.ov.CalcPreVerificationGas(userOp),
196-
VerificationGas: userOp.VerificationGasLimit,
197-
CallGasLimit: userOp.CallGasLimit,
184+
PreVerificationGas: pvg,
185+
VerificationGas: big.NewInt(int64(vg)),
186+
CallGasLimit: big.NewInt(int64(cg)),
198187
}, nil
199188
}
200189

pkg/gas/overhead.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
package gas
33

44
import (
5+
"bytes"
56
"math"
67
"math/big"
78

9+
"github.com/ethereum/go-ethereum/common/hexutil"
810
"github.com/stackup-wallet/stackup-bundler/pkg/userop"
911
)
1012

@@ -20,6 +22,9 @@ type Overhead struct {
2022
nonZeroValueCall float64
2123
callOpcode float64
2224
nonZeroValueStipend float64
25+
sanitizedPVG *big.Int
26+
sanitizedVGL *big.Int
27+
sanitizedCGL *big.Int
2328
calcPVGFunc CalcPreVerificationGasFunc
2429
}
2530

@@ -36,6 +41,9 @@ func NewDefaultOverhead() *Overhead {
3641
nonZeroValueCall: 9000,
3742
callOpcode: 700,
3843
nonZeroValueStipend: 2300,
44+
sanitizedPVG: big.NewInt(100000),
45+
sanitizedVGL: big.NewInt(1000000),
46+
sanitizedCGL: big.NewInt(1000000),
3947
calcPVGFunc: calcPVGFuncNoop(),
4048
}
4149
}
@@ -45,13 +53,31 @@ func (ov *Overhead) SetCalcPreVerificationGasFunc(fn CalcPreVerificationGasFunc)
4553
}
4654

4755
// CalcPreVerificationGas returns an expected gas cost for processing a UserOperation from a batch.
48-
func (ov *Overhead) CalcPreVerificationGas(op *userop.UserOperation) *big.Int {
49-
g := ov.calcPVGFunc(op)
56+
func (ov *Overhead) CalcPreVerificationGas(op *userop.UserOperation) (*big.Int, error) {
57+
// Sanitize fields to reduce as much variability due to length and zero bytes
58+
data, err := op.ToMap()
59+
if err != nil {
60+
return nil, err
61+
}
62+
data["preVerificationGas"] = hexutil.EncodeBig(ov.sanitizedPVG)
63+
data["verificationGasLimit"] = hexutil.EncodeBig(ov.sanitizedVGL)
64+
data["callGasLimit"] = hexutil.EncodeBig(ov.sanitizedCGL)
65+
data["signature"] = hexutil.Encode(bytes.Repeat([]byte{1}, len(op.Signature)))
66+
tmp, err := userop.New(data)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
// Use value from CalcPreVerificationGasFunc if set
72+
g, err := ov.calcPVGFunc(tmp)
73+
if err != nil {
74+
return nil, err
75+
}
5076
if g != nil {
51-
return g
77+
return g, nil
5278
}
5379

54-
packed := op.Pack()
80+
packed := tmp.Pack()
5581
lengthInWord := float64(len(packed)+31) / 32
5682
callDataCost := float64(0)
5783

@@ -64,7 +90,7 @@ func (ov *Overhead) CalcPreVerificationGas(op *userop.UserOperation) *big.Int {
6490
}
6591

6692
pvg := callDataCost + (ov.fixed / ov.minBundleSize) + ov.perUserOp + (ov.perUserOpWord * lengthInWord)
67-
return big.NewInt(int64(math.Round(pvg)))
93+
return big.NewInt(int64(math.Round(pvg))), nil
6894
}
6995

7096
// NonZeroValueCall returns an expected gas cost of using the CALL opcode in the context of EIP-4337.

pkg/gas/pvg.go

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,73 @@ package gas
33
import (
44
"math/big"
55

6-
"github.com/ethereum/go-ethereum/ethclient"
6+
"github.com/ethereum/go-ethereum/common"
7+
"github.com/ethereum/go-ethereum/common/hexutil"
8+
"github.com/ethereum/go-ethereum/crypto"
9+
"github.com/ethereum/go-ethereum/rpc"
10+
"github.com/stackup-wallet/stackup-bundler/pkg/arbitrum/nodeinterface"
11+
"github.com/stackup-wallet/stackup-bundler/pkg/entrypoint"
12+
"github.com/stackup-wallet/stackup-bundler/pkg/entrypoint/methods"
13+
"github.com/stackup-wallet/stackup-bundler/pkg/signer"
714
"github.com/stackup-wallet/stackup-bundler/pkg/userop"
815
)
916

10-
type CalcPreVerificationGasFunc = func(op *userop.UserOperation) *big.Int
17+
type CalcPreVerificationGasFunc = func(op *userop.UserOperation) (*big.Int, error)
1118

1219
func calcPVGFuncNoop() CalcPreVerificationGasFunc {
13-
return func(op *userop.UserOperation) *big.Int {
14-
return nil
20+
return func(op *userop.UserOperation) (*big.Int, error) {
21+
return nil, nil
1522
}
1623
}
1724

18-
func CalcArbitrumPVGWithEthClient(eth *ethclient.Client) CalcPreVerificationGasFunc {
19-
return func(op *userop.UserOperation) *big.Int {
20-
return big.NewInt(0)
25+
// CalcArbitrumPVGWithEthClient uses Arbitrum's NodeInterface precompile to get an estimate for
26+
// preVerificationGas that takes into account the L1 gas component. see
27+
// https://medium.com/offchainlabs/understanding-arbitrum-2-dimensional-fees-fd1d582596c9.
28+
func CalcArbitrumPVGWithEthClient(
29+
rpc *rpc.Client,
30+
) CalcPreVerificationGasFunc {
31+
pk, _ := crypto.GenerateKey()
32+
dummy, _ := signer.New(hexutil.Encode(crypto.FromECDSA(pk))[2:])
33+
return func(op *userop.UserOperation) (*big.Int, error) {
34+
// Pack handleOps method inputs
35+
ho, err := methods.HandleOpsMethod.Inputs.Pack(
36+
[]entrypoint.UserOperation{entrypoint.UserOperation(*op)},
37+
dummy.Address,
38+
)
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
// Encode function data for gasEstimateL1Component
44+
create := false
45+
if op.Nonce.Cmp(common.Big0) == 0 {
46+
create = true
47+
}
48+
ge, err := nodeinterface.GasEstimateL1ComponentMethod.Inputs.Pack(
49+
nodeinterface.ERC4337GasHelperAddress,
50+
create,
51+
append(methods.HandleOpsMethod.ID, ho...),
52+
)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
// Use eth_call to call the NodeInterface precompile
58+
req := map[string]any{
59+
"from": common.HexToAddress("0x"),
60+
"to": nodeinterface.PrecompileAddress,
61+
"data": hexutil.Encode(append(nodeinterface.GasEstimateL1ComponentMethod.ID, ge...)),
62+
}
63+
var out any
64+
if err := rpc.Call(&out, "eth_call", &req, "latest"); err != nil {
65+
return nil, err
66+
}
67+
68+
// Return GasEstimateForL1 as PVG
69+
gas, err := nodeinterface.DecodeGasEstimateL1ComponentOutput(out)
70+
if err != nil {
71+
return nil, err
72+
}
73+
return big.NewInt(int64(gas.GasEstimateForL1)), nil
2174
}
2275
}

pkg/modules/checks/verificationgas.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ func ValidateVerificationGas(op *userop.UserOperation, ov *gas.Overhead, maxVeri
1919
)
2020
}
2121

22-
pvg := ov.CalcPreVerificationGas(op)
22+
pvg, err := ov.CalcPreVerificationGas(op)
23+
if err != nil {
24+
return err
25+
}
2326
if op.PreVerificationGas.Cmp(pvg) < 0 {
2427
return fmt.Errorf("preVerificationGas: below expected gas of %s", pvg.String())
2528
}

pkg/modules/checks/verificationgas_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestOpVGMoreThanMaxVG(t *testing.T) {
5050
func TestOpPVGMoreThanOH(t *testing.T) {
5151
op := testutils.MockValidInitUserOp()
5252
ov := gas.NewDefaultOverhead()
53-
pvg := ov.CalcPreVerificationGas(op)
53+
pvg, _ := ov.CalcPreVerificationGas(op)
5454
op.PreVerificationGas = big.NewInt(0).Add(pvg, common.Big1)
5555

5656
if err := ValidateVerificationGas(op, ov, op.VerificationGasLimit); err != nil {
@@ -63,7 +63,7 @@ func TestOpPVGMoreThanOH(t *testing.T) {
6363
func TestOpPVGEqualOH(t *testing.T) {
6464
op := testutils.MockValidInitUserOp()
6565
ov := gas.NewDefaultOverhead()
66-
pvg := ov.CalcPreVerificationGas(op)
66+
pvg, _ := ov.CalcPreVerificationGas(op)
6767
op.PreVerificationGas = big.NewInt(0).Add(pvg, common.Big0)
6868

6969
if err := ValidateVerificationGas(op, ov, op.VerificationGasLimit); err != nil {
@@ -76,7 +76,7 @@ func TestOpPVGEqualOH(t *testing.T) {
7676
func TestOpPVGLessThanOH(t *testing.T) {
7777
op := testutils.MockValidInitUserOp()
7878
ov := gas.NewDefaultOverhead()
79-
pvg := ov.CalcPreVerificationGas(op)
79+
pvg, _ := ov.CalcPreVerificationGas(op)
8080
op.PreVerificationGas = big.NewInt(0).Sub(pvg, common.Big1)
8181

8282
if err := ValidateVerificationGas(op, ov, op.VerificationGasLimit); err == nil {

0 commit comments

Comments
 (0)