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

Commit 6e16726

Browse files
authored
Parse state override set (#347)
1 parent 87a6d2d commit 6e16726

File tree

6 files changed

+554
-12
lines changed

6 files changed

+554
-12
lines changed

pkg/client/client.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/stackup-wallet/stackup-bundler/pkg/mempool"
1515
"github.com/stackup-wallet/stackup-bundler/pkg/modules"
1616
"github.com/stackup-wallet/stackup-bundler/pkg/modules/noop"
17+
"github.com/stackup-wallet/stackup-bundler/pkg/state"
1718
"github.com/stackup-wallet/stackup-bundler/pkg/userop"
1819
)
1920

@@ -139,11 +140,15 @@ func (i *Client) SendUserOperation(op map[string]any, ep string) (string, error)
139140
return hash.String(), nil
140141
}
141142

142-
// EstimateUserOperationGas returns estimates for PreVerificationGas, VerificationGas, and CallGasLimit given
143-
// a UserOperation and EntryPoint address. The signature field and current gas values will not be validated
144-
// although there should be dummy values in place for the most reliable results (e.g. a signature with the
145-
// correct length).
146-
func (i *Client) EstimateUserOperationGas(op map[string]any, ep string) (*gas.GasEstimates, error) {
143+
// EstimateUserOperationGas returns estimates for PreVerificationGas, VerificationGasLimit, and CallGasLimit
144+
// given a UserOperation, EntryPoint address, and state OverrideSet. The signature field and current gas
145+
// values will not be validated although there should be dummy values in place for the most reliable results
146+
// (e.g. a signature with the correct length).
147+
func (i *Client) EstimateUserOperationGas(
148+
op map[string]any,
149+
ep string,
150+
os map[string]any,
151+
) (*gas.GasEstimates, error) {
147152
// Init logger
148153
l := i.logger.WithName("eth_estimateUserOperationGas")
149154

@@ -165,6 +170,12 @@ func (i *Client) EstimateUserOperationGas(op map[string]any, ep string) (*gas.Ga
165170
hash := userOp.GetUserOpHash(epAddr, i.chainID)
166171
l = l.WithValues("userop_hash", hash)
167172

173+
_, err = state.ParseOverrideData(os)
174+
if err != nil {
175+
l.Error(err, "eth_estimateUserOperationGas error")
176+
return nil, err
177+
}
178+
168179
// Estimate gas limits
169180
vg, cg, err := i.getGasEstimate(epAddr, userOp)
170181
if err != nil {

pkg/client/rpc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ func (r *RpcAdapter) Eth_sendUserOperation(op userOperation, ep string) (string,
3333
func (r *RpcAdapter) Eth_estimateUserOperationGas(
3434
op userOperation,
3535
ep string,
36-
ov optional_stateOverride,
36+
os optional_stateOverride,
3737
) (*gas.GasEstimates, error) {
38-
return r.client.EstimateUserOperationGas(op, ep)
38+
return r.client.EstimateUserOperationGas(op, ep, os)
3939
}
4040

4141
// Eth_getUserOperationReceipt routes method calls to *Client.GetUserOperationReceipt.

pkg/state/override.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,44 @@
11
package state
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
47
"github.com/ethereum/go-ethereum/common"
58
"github.com/ethereum/go-ethereum/common/hexutil"
69
)
710

811
type OverrideAccount struct {
912
Nonce *hexutil.Uint64 `json:"nonce"`
1013
Code *hexutil.Bytes `json:"code"`
11-
Balance **hexutil.Big `json:"balance"`
14+
Balance *hexutil.Big `json:"balance"`
1215
State *map[common.Hash]common.Hash `json:"state"`
1316
StateDiff *map[common.Hash]common.Hash `json:"stateDiff"`
1417
}
1518

1619
// OverrideSet is a set of accounts with customized state that can be applied during eth_call or
1720
// debug_traceCall.
1821
type OverrideSet map[common.Address]OverrideAccount
22+
23+
// ParseOverrideData decodes a map into an OverrideSet and validates all the fields are correctly typed.
24+
func ParseOverrideData(data map[string]any) (OverrideSet, error) {
25+
os := OverrideSet{}
26+
for key, value := range data {
27+
if !common.IsHexAddress(key) {
28+
return nil, fmt.Errorf("%w: %s", ErrBadKey, key)
29+
}
30+
31+
b, err := json.Marshal(value)
32+
if err != nil {
33+
return nil, fmt.Errorf("%w %s: %w", ErrBadValue, key, err)
34+
}
35+
36+
oa := OverrideAccount{}
37+
if err := json.Unmarshal(b, &oa); err != nil {
38+
return nil, fmt.Errorf("%w %s: %w", ErrBadValue, key, err)
39+
}
40+
41+
os[common.HexToAddress(key)] = oa
42+
}
43+
return os, nil
44+
}

0 commit comments

Comments
 (0)