From 5b577295bef4fff2719836670aeb36621d6fca05 Mon Sep 17 00:00:00 2001 From: Oleg Nikonychev Date: Sat, 28 Dec 2024 13:45:10 +0400 Subject: [PATCH] fix: moved gas comsumption from call_contract to msg_server --- x/evm/keeper/call_contract.go | 6 ------ x/evm/keeper/funtoken_from_coin.go | 8 +++++++- x/evm/keeper/keeper.go | 3 ++- x/evm/keeper/msg_server.go | 7 +++++++ 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/x/evm/keeper/call_contract.go b/x/evm/keeper/call_contract.go index d9dc889e8..8bf199785 100644 --- a/x/evm/keeper/call_contract.go +++ b/x/evm/keeper/call_contract.go @@ -108,15 +108,9 @@ func (k Keeper) CallContractWithInput( ctx, evmMsg, evm.NewNoOpTracer(), commit, evmCfg, txConfig, true, ) if err != nil { - k.ResetGasMeterAndConsumeGas(ctx, ctx.GasMeter().Limit()) err = errors.Wrap(err, "failed to apply ethereum core message") return } - blockGasUsed, errBlockGasUsed := k.AddToBlockGasUsed(ctx, evmResp.GasUsed) - if errBlockGasUsed != nil { - return nil, nil, errors.Wrap(errBlockGasUsed, "error adding transient gas used to block") - } - k.ResetGasMeterAndConsumeGas(ctx, blockGasUsed) if evmResp.Failed() { if strings.Contains(evmResp.VmError, vm.ErrOutOfGas.Error()) { diff --git a/x/evm/keeper/funtoken_from_coin.go b/x/evm/keeper/funtoken_from_coin.go index 4dfaa5c92..4cd8e765b 100644 --- a/x/evm/keeper/funtoken_from_coin.go +++ b/x/evm/keeper/funtoken_from_coin.go @@ -78,12 +78,18 @@ func (k *Keeper) deployERC20ForBankCoin( bytecodeForCall := append(embeds.SmartContract_ERC20Minter.Bytecode, packedArgs...) // nil address for contract creation - _, _, err = k.CallContractWithInput( + evmResp, _, err := k.CallContractWithInput( ctx, evm.EVM_MODULE_ADDRESS, nil, true, bytecodeForCall, Erc20GasLimitDeploy, ) if err != nil { + k.ResetGasMeterAndConsumeGas(ctx, ctx.GasMeter().Limit()) return gethcommon.Address{}, errors.Wrap(err, "failed to deploy ERC20 contract") } + blockGasUsed, errBlockGasUsed := k.AddToBlockGasUsed(ctx, evmResp.GasUsed) + if errBlockGasUsed != nil { + return gethcommon.Address{}, errors.Wrap(errBlockGasUsed, "error adding transient gas used") + } + k.ResetGasMeterAndConsumeGas(ctx, blockGasUsed) return erc20Addr, nil } diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index 79899bd89..086e2c805 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -104,7 +104,7 @@ func (k *Keeper) AddToBlockGasUsed( ) (blockGasUsed uint64, err error) { // Either k.EvmState.BlockGasUsed.GetOr() or k.EvmState.BlockGasUsed.Set() // also consume gas and could panic. - defer HandleOutOfGasPanic(&err, "") + defer HandleOutOfGasPanic(&err, "")() blockGasUsed = k.EvmState.BlockGasUsed.GetOr(ctx, 0) + gasUsed if blockGasUsed < gasUsed { @@ -147,6 +147,7 @@ func HandleOutOfGasPanic(err *error, format string) func() { if r := recover(); r != nil { switch r.(type) { case sdk.ErrorOutOfGas: + *err = vm.ErrOutOfGas default: panic(r) diff --git a/x/evm/keeper/msg_server.go b/x/evm/keeper/msg_server.go index 8d07c4e9b..d88d2e47a 100644 --- a/x/evm/keeper/msg_server.go +++ b/x/evm/keeper/msg_server.go @@ -562,8 +562,15 @@ func (k Keeper) convertCoinToEvmBornCoin( coin.Amount.BigInt(), ) if err != nil { + k.ResetGasMeterAndConsumeGas(ctx, ctx.GasMeter().Limit()) return nil, err } + blockGasUsed, errBlockGasUsed := k.AddToBlockGasUsed(ctx, evmResp.GasUsed) + if errBlockGasUsed != nil { + return nil, errors.Wrap(errBlockGasUsed, "error adding transient gas used") + } + k.ResetGasMeterAndConsumeGas(ctx, blockGasUsed) + if evmResp.Failed() { return nil, fmt.Errorf("failed to mint erc-20 tokens of contract %s", erc20Addr.String())