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

Commit 30dfee6

Browse files
authored
Account for network specific PVG buffer values (#187)
1 parent 01381df commit 30dfee6

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

internal/start/private.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ func PrivateMode() {
6060
ov := gas.NewDefaultOverhead()
6161
if chain.Cmp(config.ArbitrumOneChainID) == 0 || chain.Cmp(config.ArbitrumGoerliChainID) == 0 {
6262
ov.SetCalcPreVerificationGasFunc(gas.CalcArbitrumPVGWithEthClient(rpc, conf.SupportedEntryPoints[0]))
63+
ov.SetPreVerificationGasBufferFactor(16)
6364
}
6465
if chain.Cmp(config.OptimismChainID) == 0 || chain.Cmp(config.OptimismGoerliChainID) == 0 {
6566
ov.SetCalcPreVerificationGasFunc(
6667
gas.CalcOptimismPVGWithEthClient(rpc, chain, conf.SupportedEntryPoints[0]),
6768
)
69+
ov.SetPreVerificationGasBufferFactor(1)
6870
}
6971

7072
mem, err := mempool.New(db)

pkg/client/client.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,12 @@ func (i *Client) EstimateUserOperationGas(op map[string]any, ep string) (*gas.Ga
173173
}
174174

175175
// Calculate PreVerificationGas
176-
pvg, err := i.ov.CalcPreVerificationGas(userOp)
176+
pvg, err := i.ov.CalcPreVerificationGasWithBuffer(userOp)
177177
if err != nil {
178178
l.Error(err, "eth_estimateUserOperationGas error")
179179
return nil, err
180180
}
181181

182-
// Add a buffer to PVG for non-static values
183-
static, err := gas.NewDefaultOverhead().CalcPreVerificationGas(userOp)
184-
if err != nil {
185-
return nil, err
186-
}
187-
if pvg.Cmp(static) == 1 {
188-
// Using a custom PVG calculation. Add a small buffer to account for variability.
189-
pvg = addBuffer(pvg, 1)
190-
}
191-
192182
l.Info("eth_estimateUserOperationGas ok")
193183
return &gas.GasEstimates{
194184
PreVerificationGas: pvg,

pkg/client/buffer.go renamed to pkg/gas/buffer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package gas
22

33
import "math/big"
44

pkg/gas/overhead.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Overhead struct {
2626
sanitizedVGL *big.Int
2727
sanitizedCGL *big.Int
2828
calcPVGFunc CalcPreVerificationGasFunc
29+
pvgBufferFactor int64
2930
}
3031

3132
// NewDefaultOverhead returns an instance of Overhead using parameters defined by the Ethereum protocol.
@@ -45,6 +46,7 @@ func NewDefaultOverhead() *Overhead {
4546
sanitizedVGL: big.NewInt(1000000),
4647
sanitizedCGL: big.NewInt(1000000),
4748
calcPVGFunc: calcPVGFuncNoop(),
49+
pvgBufferFactor: 0,
4850
}
4951
}
5052

@@ -54,6 +56,14 @@ func (ov *Overhead) SetCalcPreVerificationGasFunc(fn CalcPreVerificationGasFunc)
5456
ov.calcPVGFunc = fn
5557
}
5658

59+
// SetPreVerificationGasBufferFactor defines the percentage to increase the preVerificationGas by during an
60+
// estimation. This is useful for rollups that use 2D gas values where the L1 gas component is
61+
// non-deterministic. This buffer accounts for any variability in-between eth_estimateUserOperationGas and
62+
// eth_sendUserOperation. Defaults to 0.
63+
func (ov *Overhead) SetPreVerificationGasBufferFactor(factor int64) {
64+
ov.pvgBufferFactor = factor
65+
}
66+
5767
// CalcPreVerificationGas returns an expected gas cost for processing a UserOperation from a batch.
5868
func (ov *Overhead) CalcPreVerificationGas(op *userop.UserOperation) (*big.Int, error) {
5969
// Sanitize fields to reduce as much variability due to length and zero bytes
@@ -95,6 +105,15 @@ func (ov *Overhead) CalcPreVerificationGas(op *userop.UserOperation) (*big.Int,
95105
return static, nil
96106
}
97107

108+
// CalcPreVerificationGasWithBuffer returns CalcPreVerificationGas increased by the set PVG buffer factor.
109+
func (ov *Overhead) CalcPreVerificationGasWithBuffer(op *userop.UserOperation) (*big.Int, error) {
110+
pvg, err := ov.CalcPreVerificationGas(op)
111+
if err != nil {
112+
return nil, err
113+
}
114+
return addBuffer(pvg, ov.pvgBufferFactor), nil
115+
}
116+
98117
// NonZeroValueCall returns an expected gas cost of using the CALL opcode in the context of EIP-4337.
99118
// See https://github.com/wolflo/evm-opcodes/blob/main/gas.md#aa-1-call.
100119
func (ov *Overhead) NonZeroValueCall() *big.Int {

0 commit comments

Comments
 (0)