mirror of https://github.com/ethereum/go-ethereum
internal/ethapi: add block override to estimateGas (#30695)
Add block overrides to `eth_estimateGas` to align consistency with `eth_call`. https://github.com/ethereum/go-ethereum/issues/27800#issuecomment-1658186166 Fixes https://github.com/ethereum/go-ethereum/issues/28175 --------- Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>pull/30904/head^2
parent
c1c2507148
commit
88cbfab332
@ -0,0 +1,195 @@ |
|||||||
|
// Copyright 2024 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package override |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"math/big" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/common/hexutil" |
||||||
|
"github.com/ethereum/go-ethereum/core/state" |
||||||
|
"github.com/ethereum/go-ethereum/core/tracing" |
||||||
|
"github.com/ethereum/go-ethereum/core/types" |
||||||
|
"github.com/ethereum/go-ethereum/core/vm" |
||||||
|
"github.com/holiman/uint256" |
||||||
|
) |
||||||
|
|
||||||
|
// OverrideAccount indicates the overriding fields of account during the execution
|
||||||
|
// of a message call.
|
||||||
|
// Note, state and stateDiff can't be specified at the same time. If state is
|
||||||
|
// set, message execution will only use the data in the given state. Otherwise
|
||||||
|
// if stateDiff 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"` |
||||||
|
State map[common.Hash]common.Hash `json:"state"` |
||||||
|
StateDiff map[common.Hash]common.Hash `json:"stateDiff"` |
||||||
|
MovePrecompileTo *common.Address `json:"movePrecompileToAddress"` |
||||||
|
} |
||||||
|
|
||||||
|
// StateOverride is the collection of overridden accounts.
|
||||||
|
type StateOverride map[common.Address]OverrideAccount |
||||||
|
|
||||||
|
func (diff *StateOverride) has(address common.Address) bool { |
||||||
|
_, ok := (*diff)[address] |
||||||
|
return ok |
||||||
|
} |
||||||
|
|
||||||
|
// Apply overrides the fields of specified accounts into the given state.
|
||||||
|
func (diff *StateOverride) Apply(statedb *state.StateDB, precompiles vm.PrecompiledContracts) error { |
||||||
|
if diff == nil { |
||||||
|
return nil |
||||||
|
} |
||||||
|
// Tracks destinations of precompiles that were moved.
|
||||||
|
dirtyAddrs := make(map[common.Address]struct{}) |
||||||
|
for addr, account := range *diff { |
||||||
|
// If a precompile was moved to this address already, it can't be overridden.
|
||||||
|
if _, ok := dirtyAddrs[addr]; ok { |
||||||
|
return fmt.Errorf("account %s has already been overridden by a precompile", addr.Hex()) |
||||||
|
} |
||||||
|
p, isPrecompile := precompiles[addr] |
||||||
|
// The MoveTo feature makes it possible to move a precompile
|
||||||
|
// code to another address. If the target address is another precompile
|
||||||
|
// the code for the latter is lost for this session.
|
||||||
|
// Note the destination account is not cleared upon move.
|
||||||
|
if account.MovePrecompileTo != nil { |
||||||
|
if !isPrecompile { |
||||||
|
return fmt.Errorf("account %s is not a precompile", addr.Hex()) |
||||||
|
} |
||||||
|
// Refuse to move a precompile to an address that has been
|
||||||
|
// or will be overridden.
|
||||||
|
if diff.has(*account.MovePrecompileTo) { |
||||||
|
return fmt.Errorf("account %s is already overridden", account.MovePrecompileTo.Hex()) |
||||||
|
} |
||||||
|
precompiles[*account.MovePrecompileTo] = p |
||||||
|
dirtyAddrs[*account.MovePrecompileTo] = struct{}{} |
||||||
|
} |
||||||
|
if isPrecompile { |
||||||
|
delete(precompiles, addr) |
||||||
|
} |
||||||
|
// Override account nonce.
|
||||||
|
if account.Nonce != nil { |
||||||
|
statedb.SetNonce(addr, uint64(*account.Nonce)) |
||||||
|
} |
||||||
|
// Override account(contract) code.
|
||||||
|
if account.Code != nil { |
||||||
|
statedb.SetCode(addr, *account.Code) |
||||||
|
} |
||||||
|
// Override account balance.
|
||||||
|
if account.Balance != nil { |
||||||
|
u256Balance, _ := uint256.FromBig((*big.Int)(account.Balance)) |
||||||
|
statedb.SetBalance(addr, u256Balance, tracing.BalanceChangeUnspecified) |
||||||
|
} |
||||||
|
if account.State != nil && account.StateDiff != nil { |
||||||
|
return fmt.Errorf("account %s has both 'state' and 'stateDiff'", addr.Hex()) |
||||||
|
} |
||||||
|
// Replace entire state if caller requires.
|
||||||
|
if account.State != nil { |
||||||
|
statedb.SetStorage(addr, account.State) |
||||||
|
} |
||||||
|
// Apply state diff into specified accounts.
|
||||||
|
if account.StateDiff != nil { |
||||||
|
for key, value := range account.StateDiff { |
||||||
|
statedb.SetState(addr, key, value) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
// Now finalize the changes. Finalize is normally performed between transactions.
|
||||||
|
// By using finalize, the overrides are semantically behaving as
|
||||||
|
// if they were created in a transaction just before the tracing occur.
|
||||||
|
statedb.Finalise(false) |
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
// BlockOverrides is a set of header fields to override.
|
||||||
|
type BlockOverrides struct { |
||||||
|
Number *hexutil.Big |
||||||
|
Difficulty *hexutil.Big // No-op if we're simulating post-merge calls.
|
||||||
|
Time *hexutil.Uint64 |
||||||
|
GasLimit *hexutil.Uint64 |
||||||
|
FeeRecipient *common.Address |
||||||
|
PrevRandao *common.Hash |
||||||
|
BaseFeePerGas *hexutil.Big |
||||||
|
BlobBaseFee *hexutil.Big |
||||||
|
} |
||||||
|
|
||||||
|
// Apply overrides the given header fields into the given block context.
|
||||||
|
func (o *BlockOverrides) Apply(blockCtx *vm.BlockContext) { |
||||||
|
if o == nil { |
||||||
|
return |
||||||
|
} |
||||||
|
if o.Number != nil { |
||||||
|
blockCtx.BlockNumber = o.Number.ToInt() |
||||||
|
} |
||||||
|
if o.Difficulty != nil { |
||||||
|
blockCtx.Difficulty = o.Difficulty.ToInt() |
||||||
|
} |
||||||
|
if o.Time != nil { |
||||||
|
blockCtx.Time = uint64(*o.Time) |
||||||
|
} |
||||||
|
if o.GasLimit != nil { |
||||||
|
blockCtx.GasLimit = uint64(*o.GasLimit) |
||||||
|
} |
||||||
|
if o.FeeRecipient != nil { |
||||||
|
blockCtx.Coinbase = *o.FeeRecipient |
||||||
|
} |
||||||
|
if o.PrevRandao != nil { |
||||||
|
blockCtx.Random = o.PrevRandao |
||||||
|
} |
||||||
|
if o.BaseFeePerGas != nil { |
||||||
|
blockCtx.BaseFee = o.BaseFeePerGas.ToInt() |
||||||
|
} |
||||||
|
if o.BlobBaseFee != nil { |
||||||
|
blockCtx.BlobBaseFee = o.BlobBaseFee.ToInt() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// MakeHeader returns a new header object with the overridden
|
||||||
|
// fields.
|
||||||
|
// Note: MakeHeader ignores BlobBaseFee if set. That's because
|
||||||
|
// header has no such field.
|
||||||
|
func (o *BlockOverrides) MakeHeader(header *types.Header) *types.Header { |
||||||
|
if o == nil { |
||||||
|
return header |
||||||
|
} |
||||||
|
h := types.CopyHeader(header) |
||||||
|
if o.Number != nil { |
||||||
|
h.Number = o.Number.ToInt() |
||||||
|
} |
||||||
|
if o.Difficulty != nil { |
||||||
|
h.Difficulty = o.Difficulty.ToInt() |
||||||
|
} |
||||||
|
if o.Time != nil { |
||||||
|
h.Time = uint64(*o.Time) |
||||||
|
} |
||||||
|
if o.GasLimit != nil { |
||||||
|
h.GasLimit = uint64(*o.GasLimit) |
||||||
|
} |
||||||
|
if o.FeeRecipient != nil { |
||||||
|
h.Coinbase = *o.FeeRecipient |
||||||
|
} |
||||||
|
if o.PrevRandao != nil { |
||||||
|
h.MixDigest = *o.PrevRandao |
||||||
|
} |
||||||
|
if o.BaseFeePerGas != nil { |
||||||
|
h.BaseFee = o.BaseFeePerGas.ToInt() |
||||||
|
} |
||||||
|
return h |
||||||
|
} |
@ -0,0 +1,125 @@ |
|||||||
|
// Copyright 2024 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package override |
||||||
|
|
||||||
|
import ( |
||||||
|
"maps" |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/common/hexutil" |
||||||
|
"github.com/ethereum/go-ethereum/core/rawdb" |
||||||
|
"github.com/ethereum/go-ethereum/core/state" |
||||||
|
"github.com/ethereum/go-ethereum/core/vm" |
||||||
|
"github.com/ethereum/go-ethereum/triedb" |
||||||
|
) |
||||||
|
|
||||||
|
type precompileContract struct{} |
||||||
|
|
||||||
|
func (p *precompileContract) RequiredGas(input []byte) uint64 { return 0 } |
||||||
|
|
||||||
|
func (p *precompileContract) Run(input []byte) ([]byte, error) { return nil, nil } |
||||||
|
|
||||||
|
func TestStateOverrideMovePrecompile(t *testing.T) { |
||||||
|
db := state.NewDatabase(triedb.NewDatabase(rawdb.NewMemoryDatabase(), nil), nil) |
||||||
|
statedb, err := state.New(common.Hash{}, db) |
||||||
|
if err != nil { |
||||||
|
t.Fatalf("failed to create statedb: %v", err) |
||||||
|
} |
||||||
|
precompiles := map[common.Address]vm.PrecompiledContract{ |
||||||
|
common.BytesToAddress([]byte{0x1}): &precompileContract{}, |
||||||
|
common.BytesToAddress([]byte{0x2}): &precompileContract{}, |
||||||
|
} |
||||||
|
bytes2Addr := func(b []byte) *common.Address { |
||||||
|
a := common.BytesToAddress(b) |
||||||
|
return &a |
||||||
|
} |
||||||
|
var testSuite = []struct { |
||||||
|
overrides StateOverride |
||||||
|
expectedPrecompiles map[common.Address]struct{} |
||||||
|
fail bool |
||||||
|
}{ |
||||||
|
{ |
||||||
|
overrides: StateOverride{ |
||||||
|
common.BytesToAddress([]byte{0x1}): { |
||||||
|
Code: hex2Bytes("0xff"), |
||||||
|
MovePrecompileTo: bytes2Addr([]byte{0x2}), |
||||||
|
}, |
||||||
|
common.BytesToAddress([]byte{0x2}): { |
||||||
|
Code: hex2Bytes("0x00"), |
||||||
|
}, |
||||||
|
}, |
||||||
|
// 0x2 has already been touched by the moveTo.
|
||||||
|
fail: true, |
||||||
|
}, { |
||||||
|
overrides: StateOverride{ |
||||||
|
common.BytesToAddress([]byte{0x1}): { |
||||||
|
Code: hex2Bytes("0xff"), |
||||||
|
MovePrecompileTo: bytes2Addr([]byte{0xff}), |
||||||
|
}, |
||||||
|
common.BytesToAddress([]byte{0x3}): { |
||||||
|
Code: hex2Bytes("0x00"), |
||||||
|
MovePrecompileTo: bytes2Addr([]byte{0xfe}), |
||||||
|
}, |
||||||
|
}, |
||||||
|
// 0x3 is not a precompile.
|
||||||
|
fail: true, |
||||||
|
}, { |
||||||
|
overrides: StateOverride{ |
||||||
|
common.BytesToAddress([]byte{0x1}): { |
||||||
|
Code: hex2Bytes("0xff"), |
||||||
|
MovePrecompileTo: bytes2Addr([]byte{0xff}), |
||||||
|
}, |
||||||
|
common.BytesToAddress([]byte{0x2}): { |
||||||
|
Code: hex2Bytes("0x00"), |
||||||
|
MovePrecompileTo: bytes2Addr([]byte{0xfe}), |
||||||
|
}, |
||||||
|
}, |
||||||
|
expectedPrecompiles: map[common.Address]struct{}{common.BytesToAddress([]byte{0xfe}): {}, common.BytesToAddress([]byte{0xff}): {}}, |
||||||
|
}, |
||||||
|
} |
||||||
|
|
||||||
|
for i, tt := range testSuite { |
||||||
|
cpy := maps.Clone(precompiles) |
||||||
|
// Apply overrides
|
||||||
|
err := tt.overrides.Apply(statedb, cpy) |
||||||
|
if tt.fail { |
||||||
|
if err == nil { |
||||||
|
t.Errorf("test %d: want error, have nothing", i) |
||||||
|
} |
||||||
|
continue |
||||||
|
} |
||||||
|
if err != nil { |
||||||
|
t.Errorf("test %d: want no error, have %v", i, err) |
||||||
|
continue |
||||||
|
} |
||||||
|
// Precompile keys
|
||||||
|
if len(cpy) != len(tt.expectedPrecompiles) { |
||||||
|
t.Errorf("test %d: precompile mismatch, want %d, have %d", i, len(tt.expectedPrecompiles), len(cpy)) |
||||||
|
} |
||||||
|
for k := range tt.expectedPrecompiles { |
||||||
|
if _, ok := cpy[k]; !ok { |
||||||
|
t.Errorf("test %d: precompile not found: %s", i, k.String()) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func hex2Bytes(str string) *hexutil.Bytes { |
||||||
|
rpcBytes := hexutil.Bytes(common.FromHex(str)) |
||||||
|
return &rpcBytes |
||||||
|
} |
Loading…
Reference in new issue