Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit 7f196ce

Browse files
chore: verify fees refactor (#1496)
* chore: verify fees refactor * adjust call structure in rest of repo after splitting up DeductTxCostsFromUserBalance * adjust test logic after splitting DeductTxCostsFromUserBalance up * remove outdated TODO * address PR comments - remove import name for evm keeper * remove misleading comment * address review comments - only handover boolean instead of context * remove TODO Co-authored-by: MalteHerrmann <[email protected]> Co-authored-by: MalteHerrmann <[email protected]>
1 parent a5c927b commit 7f196ce

File tree

5 files changed

+187
-167
lines changed

5 files changed

+187
-167
lines changed

app/ante/eth.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
1212

1313
ethermint "github.com/evmos/ethermint/types"
14-
evmkeeper "github.com/evmos/ethermint/x/evm/keeper"
14+
"github.com/evmos/ethermint/x/evm/keeper"
1515
"github.com/evmos/ethermint/x/evm/statedb"
1616
evmtypes "github.com/evmos/ethermint/x/evm/types"
1717

@@ -79,7 +79,7 @@ func (avd EthAccountVerificationDecorator) AnteHandle(
7979
"the sender is not EOA: address %s, codeHash <%s>", fromAddr, acct.CodeHash)
8080
}
8181

82-
if err := evmkeeper.CheckSenderBalance(sdkmath.NewIntFromBigInt(acct.Balance), txData); err != nil {
82+
if err := keeper.CheckSenderBalance(sdkmath.NewIntFromBigInt(acct.Balance), txData); err != nil {
8383
return ctx, errorsmod.Wrap(err, "failed to check sender balance")
8484
}
8585
}
@@ -132,7 +132,6 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
132132
blockHeight := big.NewInt(ctx.BlockHeight())
133133
homestead := ethCfg.IsHomestead(blockHeight)
134134
istanbul := ethCfg.IsIstanbul(blockHeight)
135-
london := ethCfg.IsLondon(blockHeight)
136135
gasWanted := uint64(0)
137136
var events sdk.Events
138137

@@ -164,16 +163,12 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
164163

165164
evmDenom := egcd.evmKeeper.GetEVMDenom(ctx)
166165

167-
fees, err := egcd.evmKeeper.DeductTxCostsFromUserBalance(
168-
ctx,
169-
*msgEthTx,
170-
txData,
171-
evmDenom,
172-
baseFee,
173-
homestead,
174-
istanbul,
175-
london,
176-
)
166+
fees, err := keeper.VerifyFee(txData, evmDenom, baseFee, homestead, istanbul, ctx.IsCheckTx())
167+
if err != nil {
168+
return ctx, errorsmod.Wrapf(err, "failed to verify the fees")
169+
}
170+
171+
err = egcd.evmKeeper.DeductTxCostsFromUserBalance(ctx, fees, common.HexToAddress(msgEthTx.From))
177172
if err != nil {
178173
return ctx, errorsmod.Wrapf(err, "failed to deduct transaction costs from user balance")
179174
}

app/ante/interfaces.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ type EVMKeeper interface {
2828
DynamicFeeEVMKeeper
2929

3030
NewEVM(ctx sdk.Context, msg core.Message, cfg *evmtypes.EVMConfig, tracer vm.EVMLogger, stateDB vm.StateDB) evm.EVM
31-
DeductTxCostsFromUserBalance(
32-
ctx sdk.Context, msgEthTx evmtypes.MsgEthereumTx, txData evmtypes.TxData, denom string, baseFee *big.Int, homestead, istanbul, london bool,
33-
) (fees sdk.Coins, err error)
31+
DeductTxCostsFromUserBalance(ctx sdk.Context, fees sdk.Coins, from common.Address) error
3432
GetBalance(ctx sdk.Context, addr common.Address) *big.Int
3533
ResetTransientGasUsed(ctx sdk.Context)
3634
GetTxIndexTransient(ctx sdk.Context) uint64

x/evm/handler_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package evm_test
22

33
import (
44
"errors"
5+
"github.com/evmos/ethermint/x/evm/keeper"
56
"math/big"
67
"testing"
78
"time"
@@ -599,7 +600,9 @@ func (suite *EvmTestSuite) TestERC20TransferReverted() {
599600

600601
txData, err := types.UnpackTxData(tx.Data)
601602
suite.Require().NoError(err)
602-
_, err = k.DeductTxCostsFromUserBalance(suite.ctx, *tx, txData, "aphoton", baseFee, true, true, true)
603+
fees, err := keeper.VerifyFee(txData, "aphoton", baseFee, true, true, suite.ctx.IsCheckTx())
604+
suite.Require().NoError(err)
605+
err = k.DeductTxCostsFromUserBalance(suite.ctx, fees, common.HexToAddress(tx.From))
603606
suite.Require().NoError(err)
604607

605608
res, err := k.EthereumTx(sdk.WrapSDKContext(suite.ctx), tx)

x/evm/keeper/utils.go

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,41 @@ import (
1111

1212
evmtypes "github.com/evmos/ethermint/x/evm/types"
1313

14+
"github.com/ethereum/go-ethereum/common"
1415
"github.com/ethereum/go-ethereum/core"
1516
ethtypes "github.com/ethereum/go-ethereum/core/types"
1617
)
1718

18-
// DeductTxCostsFromUserBalance it calculates the tx costs and deducts the fees
19-
func (k Keeper) DeductTxCostsFromUserBalance(
19+
// DeductTxCostsFromUserBalance deducts the fees from the user balance. Returns an
20+
// error if the specified sender address does not exist or the account balance is not sufficient.
21+
func (k *Keeper) DeductTxCostsFromUserBalance(
2022
ctx sdk.Context,
21-
msgEthTx evmtypes.MsgEthereumTx,
23+
fees sdk.Coins,
24+
from common.Address,
25+
) error {
26+
// fetch sender account
27+
signerAcc, err := authante.GetSignerAcc(ctx, k.accountKeeper, from.Bytes())
28+
if err != nil {
29+
return errorsmod.Wrapf(err, "account not found for sender %s", from)
30+
}
31+
32+
// deduct the full gas cost from the user balance
33+
if err := authante.DeductFees(k.bankKeeper, ctx, signerAcc, fees); err != nil {
34+
return errorsmod.Wrapf(err, "failed to deduct full gas cost %s from the user %s balance", fees, from)
35+
}
36+
37+
return nil
38+
}
39+
40+
// VerifyFee is used to return the fee for the given transaction data in sdk.Coins. It checks that the
41+
// gas limit is not reached, the gas limit is higher than the intrinsic gas and that the
42+
// base fee is higher than the gas fee cap.
43+
func VerifyFee(
2244
txData evmtypes.TxData,
2345
denom string,
2446
baseFee *big.Int,
25-
homestead, istanbul, london bool,
26-
) (fees sdk.Coins, err error) {
47+
homestead, istanbul, isCheckTx bool,
48+
) (sdk.Coins, error) {
2749
isContractCreation := txData.GetTo() == nil
2850

2951
gasLimit := txData.GetGas()
@@ -43,7 +65,7 @@ func (k Keeper) DeductTxCostsFromUserBalance(
4365
}
4466

4567
// intrinsic gas verification during CheckTx
46-
if ctx.IsCheckTx() && gasLimit < intrinsicGas {
68+
if isCheckTx && gasLimit < intrinsicGas {
4769
return nil, errorsmod.Wrapf(
4870
errortypes.ErrOutOfGas,
4971
"gas limit too low: %d (gas limit) < %d (intrinsic gas)", gasLimit, intrinsicGas,
@@ -63,24 +85,7 @@ func (k Keeper) DeductTxCostsFromUserBalance(
6385
return sdk.Coins{}, nil
6486
}
6587

66-
fees = sdk.Coins{{Denom: denom, Amount: sdkmath.NewIntFromBigInt(feeAmt)}}
67-
68-
// fetch sender account from signature
69-
signerAcc, err := authante.GetSignerAcc(ctx, k.accountKeeper, msgEthTx.GetFrom())
70-
if err != nil {
71-
return nil, errorsmod.Wrapf(err, "account not found for sender %s", msgEthTx.From)
72-
}
73-
74-
// deduct the full gas cost from the user balance
75-
if err := authante.DeductFees(k.bankKeeper, ctx, signerAcc, fees); err != nil {
76-
return nil, errorsmod.Wrapf(
77-
err,
78-
"failed to deduct full gas cost %s from the user %s balance",
79-
fees, msgEthTx.From,
80-
)
81-
}
82-
83-
return fees, nil
88+
return sdk.Coins{{Denom: denom, Amount: sdkmath.NewIntFromBigInt(feeAmt)}}, nil
8489
}
8590

8691
// CheckSenderBalance validates that the tx cost value is positive and that the

0 commit comments

Comments
 (0)