Skip to content

Commit 7db6c91

Browse files
MonkeyMarcels1na
andauthored
internal/ethapi: fix precompile override for eth_estimateGas (#31795)
Fix and close #31719. --------- Co-authored-by: Sina Mahmoodi <[email protected]>
1 parent 6191f31 commit 7db6c91

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

internal/ethapi/api.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,15 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
829829
if state == nil || err != nil {
830830
return 0, err
831831
}
832-
if err := overrides.Apply(state, nil); err != nil {
832+
blockCtx := core.NewEVMBlockContext(header, NewChainContext(ctx, b), nil)
833+
if blockOverrides != nil {
834+
if err := blockOverrides.Apply(&blockCtx); err != nil {
835+
return 0, err
836+
}
837+
}
838+
rules := b.ChainConfig().Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time)
839+
precompiles := vm.ActivePrecompiledContracts(rules)
840+
if err := overrides.Apply(state, precompiles); err != nil {
833841
return 0, err
834842
}
835843
// Construct the gas estimator option from the user input

internal/ethapi/api_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3725,3 +3725,45 @@ func TestCreateAccessListWithStateOverrides(t *testing.T) {
37253725
}}
37263726
require.Equal(t, expected, result.Accesslist)
37273727
}
3728+
3729+
func TestEstimateGasWithMovePrecompile(t *testing.T) {
3730+
t.Parallel()
3731+
// Initialize test accounts
3732+
var (
3733+
accounts = newAccounts(2)
3734+
genesis = &core.Genesis{
3735+
Config: params.MergedTestChainConfig,
3736+
Alloc: types.GenesisAlloc{
3737+
accounts[0].addr: {Balance: big.NewInt(params.Ether)},
3738+
},
3739+
}
3740+
)
3741+
backend := newTestBackend(t, 1, genesis, beacon.New(ethash.NewFaker()), func(i int, b *core.BlockGen) {
3742+
b.SetPoS()
3743+
})
3744+
api := NewBlockChainAPI(backend)
3745+
// Move SHA256 precompile (0x2) to a new address (0x100)
3746+
// and estimate gas for calling the moved precompile.
3747+
var (
3748+
sha256Addr = common.BytesToAddress([]byte{0x2})
3749+
newSha256Addr = common.BytesToAddress([]byte{0x10, 0})
3750+
sha256Input = hexutil.Bytes([]byte("hello"))
3751+
args = TransactionArgs{
3752+
From: &accounts[0].addr,
3753+
To: &newSha256Addr,
3754+
Data: &sha256Input,
3755+
}
3756+
overrides = &override.StateOverride{
3757+
sha256Addr: override.OverrideAccount{
3758+
MovePrecompileTo: &newSha256Addr,
3759+
},
3760+
}
3761+
)
3762+
gas, err := api.EstimateGas(context.Background(), args, nil, overrides, nil)
3763+
if err != nil {
3764+
t.Fatalf("EstimateGas failed: %v", err)
3765+
}
3766+
if gas != 21366 {
3767+
t.Fatalf("mismatched gas: %d, want 21366", gas)
3768+
}
3769+
}

0 commit comments

Comments
 (0)