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

Commit

Permalink
Pass static value into CalcPreVerificationGasFunc (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
hazim-j authored Apr 28, 2023
1 parent 27e6fd7 commit 29c3bcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
26 changes: 14 additions & 12 deletions pkg/gas/overhead.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func NewDefaultOverhead() *Overhead {
}
}

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

// Use value from CalcPreVerificationGasFunc if set
g, err := ov.calcPVGFunc(tmp)
if err != nil {
return nil, err
}
if g != nil {
return g, nil
}

// Calculate static value from pre-defined parameters
packed := tmp.Pack()
lengthInWord := float64(len(packed)+31) / 32
callDataCost := float64(0)

for _, b := range packed {
if b == byte(0) {
callDataCost += ov.zeroByte
} else {
callDataCost += ov.nonZeroByte
}
}

pvg := callDataCost + (ov.fixed / ov.minBundleSize) + ov.perUserOp + (ov.perUserOpWord * lengthInWord)
return big.NewInt(int64(math.Round(pvg))), nil
static := big.NewInt(int64(math.Round(pvg)))

// Use value from CalcPreVerificationGasFunc if set, otherwise return the static value.
g, err := ov.calcPVGFunc(tmp, static)
if err != nil {
return nil, err
}
if g != nil {
return g, nil
}
return static, nil
}

// NonZeroValueCall returns an expected gas cost of using the CALL opcode in the context of EIP-4337.
Expand Down
12 changes: 7 additions & 5 deletions pkg/gas/pvg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import (
"github.com/stackup-wallet/stackup-bundler/pkg/userop"
)

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

func calcPVGFuncNoop() CalcPreVerificationGasFunc {
return func(op *userop.UserOperation) (*big.Int, error) {
return func(op *userop.UserOperation, static *big.Int) (*big.Int, error) {
return nil, nil
}
}
Expand All @@ -30,7 +32,7 @@ func CalcArbitrumPVGWithEthClient(
) CalcPreVerificationGasFunc {
pk, _ := crypto.GenerateKey()
dummy, _ := signer.New(hexutil.Encode(crypto.FromECDSA(pk))[2:])
return func(op *userop.UserOperation) (*big.Int, error) {
return func(op *userop.UserOperation, static *big.Int) (*big.Int, error) {
// Pack handleOps method inputs
ho, err := methods.HandleOpsMethod.Inputs.Pack(
[]entrypoint.UserOperation{entrypoint.UserOperation(*op)},
Expand Down Expand Up @@ -65,11 +67,11 @@ func CalcArbitrumPVGWithEthClient(
return nil, err
}

// Return GasEstimateForL1 as PVG
// Return static + GasEstimateForL1 as PVG
gas, err := nodeinterface.DecodeGasEstimateL1ComponentOutput(out)
if err != nil {
return nil, err
}
return big.NewInt(int64(gas.GasEstimateForL1)), nil
return big.NewInt(0).Add(static, big.NewInt(int64(gas.GasEstimateForL1))), nil
}
}

0 comments on commit 29c3bcb

Please sign in to comment.