mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-24 00:39:26 +00:00
internal/ethapi: fix precompile override for eth_estimateGas (#31795)
Fix and close https://github.com/ethereum/go-ethereum/issues/31719. --------- Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
This commit is contained in:
parent
6191f31508
commit
7db6c91254
2 changed files with 51 additions and 1 deletions
|
|
@ -829,7 +829,15 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
|
||||||
if state == nil || err != nil {
|
if state == nil || err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
if err := overrides.Apply(state, nil); err != nil {
|
blockCtx := core.NewEVMBlockContext(header, NewChainContext(ctx, b), nil)
|
||||||
|
if blockOverrides != nil {
|
||||||
|
if err := blockOverrides.Apply(&blockCtx); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rules := b.ChainConfig().Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time)
|
||||||
|
precompiles := vm.ActivePrecompiledContracts(rules)
|
||||||
|
if err := overrides.Apply(state, precompiles); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
// Construct the gas estimator option from the user input
|
// Construct the gas estimator option from the user input
|
||||||
|
|
|
||||||
|
|
@ -3725,3 +3725,45 @@ func TestCreateAccessListWithStateOverrides(t *testing.T) {
|
||||||
}}
|
}}
|
||||||
require.Equal(t, expected, result.Accesslist)
|
require.Equal(t, expected, result.Accesslist)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEstimateGasWithMovePrecompile(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
// Initialize test accounts
|
||||||
|
var (
|
||||||
|
accounts = newAccounts(2)
|
||||||
|
genesis = &core.Genesis{
|
||||||
|
Config: params.MergedTestChainConfig,
|
||||||
|
Alloc: types.GenesisAlloc{
|
||||||
|
accounts[0].addr: {Balance: big.NewInt(params.Ether)},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
backend := newTestBackend(t, 1, genesis, beacon.New(ethash.NewFaker()), func(i int, b *core.BlockGen) {
|
||||||
|
b.SetPoS()
|
||||||
|
})
|
||||||
|
api := NewBlockChainAPI(backend)
|
||||||
|
// Move SHA256 precompile (0x2) to a new address (0x100)
|
||||||
|
// and estimate gas for calling the moved precompile.
|
||||||
|
var (
|
||||||
|
sha256Addr = common.BytesToAddress([]byte{0x2})
|
||||||
|
newSha256Addr = common.BytesToAddress([]byte{0x10, 0})
|
||||||
|
sha256Input = hexutil.Bytes([]byte("hello"))
|
||||||
|
args = TransactionArgs{
|
||||||
|
From: &accounts[0].addr,
|
||||||
|
To: &newSha256Addr,
|
||||||
|
Data: &sha256Input,
|
||||||
|
}
|
||||||
|
overrides = &override.StateOverride{
|
||||||
|
sha256Addr: override.OverrideAccount{
|
||||||
|
MovePrecompileTo: &newSha256Addr,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
gas, err := api.EstimateGas(context.Background(), args, nil, overrides, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("EstimateGas failed: %v", err)
|
||||||
|
}
|
||||||
|
if gas != 21366 {
|
||||||
|
t.Fatalf("mismatched gas: %d, want 21366", gas)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue