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

Commit 29c3bcb

Browse files
authored
Pass static value into CalcPreVerificationGasFunc (#158)
1 parent 27e6fd7 commit 29c3bcb

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

pkg/gas/overhead.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ func NewDefaultOverhead() *Overhead {
4848
}
4949
}
5050

51+
// SetCalcPreVerificationGasFunc allows a custom function to be defined that can control how it calculates
52+
// PVG. This is useful for networks that have different models for gas.
5153
func (ov *Overhead) SetCalcPreVerificationGasFunc(fn CalcPreVerificationGasFunc) {
5254
ov.calcPVGFunc = fn
5355
}
@@ -68,29 +70,29 @@ func (ov *Overhead) CalcPreVerificationGas(op *userop.UserOperation) (*big.Int,
6870
return nil, err
6971
}
7072

71-
// Use value from CalcPreVerificationGasFunc if set
72-
g, err := ov.calcPVGFunc(tmp)
73-
if err != nil {
74-
return nil, err
75-
}
76-
if g != nil {
77-
return g, nil
78-
}
79-
73+
// Calculate static value from pre-defined parameters
8074
packed := tmp.Pack()
8175
lengthInWord := float64(len(packed)+31) / 32
8276
callDataCost := float64(0)
83-
8477
for _, b := range packed {
8578
if b == byte(0) {
8679
callDataCost += ov.zeroByte
8780
} else {
8881
callDataCost += ov.nonZeroByte
8982
}
9083
}
91-
9284
pvg := callDataCost + (ov.fixed / ov.minBundleSize) + ov.perUserOp + (ov.perUserOpWord * lengthInWord)
93-
return big.NewInt(int64(math.Round(pvg))), nil
85+
static := big.NewInt(int64(math.Round(pvg)))
86+
87+
// Use value from CalcPreVerificationGasFunc if set, otherwise return the static value.
88+
g, err := ov.calcPVGFunc(tmp, static)
89+
if err != nil {
90+
return nil, err
91+
}
92+
if g != nil {
93+
return g, nil
94+
}
95+
return static, nil
9496
}
9597

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

pkg/gas/pvg.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import (
1414
"github.com/stackup-wallet/stackup-bundler/pkg/userop"
1515
)
1616

17-
type CalcPreVerificationGasFunc = func(op *userop.UserOperation) (*big.Int, error)
17+
// CalcPreVerificationGasFunc defines an interface for a function to calculate PVG given a userOp and a static
18+
// value. The static input is the value derived from the default overheads.
19+
type CalcPreVerificationGasFunc = func(op *userop.UserOperation, static *big.Int) (*big.Int, error)
1820

1921
func calcPVGFuncNoop() CalcPreVerificationGasFunc {
20-
return func(op *userop.UserOperation) (*big.Int, error) {
22+
return func(op *userop.UserOperation, static *big.Int) (*big.Int, error) {
2123
return nil, nil
2224
}
2325
}
@@ -30,7 +32,7 @@ func CalcArbitrumPVGWithEthClient(
3032
) CalcPreVerificationGasFunc {
3133
pk, _ := crypto.GenerateKey()
3234
dummy, _ := signer.New(hexutil.Encode(crypto.FromECDSA(pk))[2:])
33-
return func(op *userop.UserOperation) (*big.Int, error) {
35+
return func(op *userop.UserOperation, static *big.Int) (*big.Int, error) {
3436
// Pack handleOps method inputs
3537
ho, err := methods.HandleOpsMethod.Inputs.Pack(
3638
[]entrypoint.UserOperation{entrypoint.UserOperation(*op)},
@@ -65,11 +67,11 @@ func CalcArbitrumPVGWithEthClient(
6567
return nil, err
6668
}
6769

68-
// Return GasEstimateForL1 as PVG
70+
// Return static + GasEstimateForL1 as PVG
6971
gas, err := nodeinterface.DecodeGasEstimateL1ComponentOutput(out)
7072
if err != nil {
7173
return nil, err
7274
}
73-
return big.NewInt(int64(gas.GasEstimateForL1)), nil
75+
return big.NewInt(0).Add(static, big.NewInt(int64(gas.GasEstimateForL1))), nil
7476
}
7577
}

0 commit comments

Comments
 (0)