Skip to content

Commit

Permalink
fix: eth_estimateGas error in zero balance address (#537)
Browse files Browse the repository at this point in the history
* fix: eth_estimateGas zero balance error

* bump version

* revert irrelevant auto format

* remove isEstimateGas in DoCall

* fix CI

* bump version

---------

Co-authored-by: maskpp <[email protected]>
  • Loading branch information
colinlyguo and mask-pp authored Oct 25, 2023
1 parent adde9ce commit bbf4204
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 46 deletions.
51 changes: 6 additions & 45 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,12 +849,11 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, address common.A
// if statDiff is set, all diff will be applied first and then execute the call
// message.
type OverrideAccount struct {
Nonce *hexutil.Uint64 `json:"nonce"`
Code *hexutil.Bytes `json:"code"`
Balance **hexutil.Big `json:"balance"`
BalanceAdd **hexutil.Big `json:"balanceAdd"`
State *map[common.Hash]common.Hash `json:"state"`
StateDiff *map[common.Hash]common.Hash `json:"stateDiff"`
Nonce *hexutil.Uint64 `json:"nonce"`
Code *hexutil.Bytes `json:"code"`
Balance **hexutil.Big `json:"balance"`
State *map[common.Hash]common.Hash `json:"state"`
StateDiff *map[common.Hash]common.Hash `json:"stateDiff"`
}

// StateOverride is the collection of overridden accounts.
Expand All @@ -875,16 +874,9 @@ func (diff *StateOverride) Apply(state *state.StateDB) error {
state.SetCode(addr, *account.Code)
}
// Override account balance.
if account.Balance != nil && account.BalanceAdd != nil {
return fmt.Errorf("account %s has both 'balance' and 'balanceAdd'", addr.Hex())
}
if account.Balance != nil {
state.SetBalance(addr, (*big.Int)(*account.Balance))
}
if account.BalanceAdd != nil {
balance := big.NewInt(0).Add(state.GetBalance(addr), (*big.Int)(*account.BalanceAdd))
state.SetBalance(addr, balance)
}
if account.State != nil && account.StateDiff != nil {
return fmt.Errorf("account %s has both 'state' and 'stateDiff'", addr.Hex())
}
Expand All @@ -902,11 +894,6 @@ func (diff *StateOverride) Apply(state *state.StateDB) error {
return nil
}

func newRPCBalance(balance *big.Int) **hexutil.Big {
rpcBalance := (*hexutil.Big)(balance)
return &rpcBalance
}

func EstimateL1MsgFee(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *StateOverride, timeout time.Duration, globalGasCap uint64, config *params.ChainConfig) (*big.Int, error) {
if !config.Scroll.FeeVaultEnabled() {
return big.NewInt(0), nil
Expand Down Expand Up @@ -992,13 +979,7 @@ func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash
// Execute the message.
gp := new(core.GasPool).AddGas(math.MaxUint64)

signer := types.MakeSigner(b.ChainConfig(), header.Number)
l1DataFee, err := fees.EstimateL1DataFeeForMessage(msg, header.BaseFee, b.ChainConfig().ChainID, signer, state)
if err != nil {
return nil, err
}

result, err := core.ApplyMessage(evm, msg, gp, l1DataFee)
result, err := core.ApplyMessage(evm, msg, gp, common.Big0)
if err := vmError(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -1050,26 +1031,6 @@ func (e *revertError) ErrorData() interface{} {
// Note, this function doesn't make and changes in the state/blockchain and is
// useful to execute and retrieve values.
func (s *PublicBlockChainAPI) Call(ctx context.Context, args TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *StateOverride) (hexutil.Bytes, error) {
// If gasPrice is 0 and no state override is set, make sure
// that the account has sufficient balance to cover `l1DataFee`.
isGasPriceZero := args.GasPrice == nil || args.GasPrice.ToInt().Cmp(big.NewInt(0)) == 0

if overrides == nil {
overrides = &StateOverride{}
}
_, isOverrideSet := (*overrides)[args.from()]

if isGasPriceZero && !isOverrideSet {
l1DataFee, err := EstimateL1MsgFee(ctx, s.b, args, blockNrOrHash, overrides, s.b.RPCEVMTimeout(), s.b.RPCGasCap(), s.b.ChainConfig())
if err != nil {
return nil, err
}

(*overrides)[args.from()] = OverrideAccount{
BalanceAdd: newRPCBalance(l1DataFee),
}
}

result, err := DoCall(ctx, s.b, args, blockNrOrHash, overrides, s.b.RPCEVMTimeout(), s.b.RPCGasCap())
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
const (
VersionMajor = 5 // Major version component of the current release
VersionMinor = 0 // Minor version component of the current release
VersionPatch = 2 // Patch version component of the current release
VersionPatch = 3 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string
)

Expand Down

0 comments on commit bbf4204

Please sign in to comment.