ante: move deduct gas to EVM keeper (#584)

This commit is contained in:
Federico Kunze Küllmer 2021-09-22 12:26:29 +02:00 committed by GitHub
parent e65582df71
commit 692fd9548a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 23 deletions

View File

@ -62,7 +62,7 @@ func NewAnteHandler(
NewEthSigVerificationDecorator(evmKeeper), NewEthSigVerificationDecorator(evmKeeper),
NewEthAccountVerificationDecorator(ak, bankKeeper, evmKeeper), NewEthAccountVerificationDecorator(ak, bankKeeper, evmKeeper),
NewEthNonceVerificationDecorator(ak), NewEthNonceVerificationDecorator(ak),
NewEthGasConsumeDecorator(ak, bankKeeper, evmKeeper), NewEthGasConsumeDecorator(evmKeeper),
NewCanTransferDecorator(evmKeeper), NewCanTransferDecorator(evmKeeper),
NewEthIncrementSenderSequenceDecorator(ak), // innermost AnteDecorator. NewEthIncrementSenderSequenceDecorator(ak), // innermost AnteDecorator.
) )

View File

@ -30,6 +30,9 @@ type EVMKeeper interface {
ResetRefundTransient(ctx sdk.Context) ResetRefundTransient(ctx sdk.Context)
NewEVM(msg core.Message, config *params.ChainConfig, params evmtypes.Params, coinbase common.Address, tracer vm.Tracer) *vm.EVM NewEVM(msg core.Message, config *params.ChainConfig, params evmtypes.Params, coinbase common.Address, tracer vm.Tracer) *vm.EVM
GetCodeHash(addr common.Address) common.Hash GetCodeHash(addr common.Address) common.Hash
DeductTxCostsFromUserBalance(
ctx sdk.Context, msgEthTx evmtypes.MsgEthereumTx, txData evmtypes.TxData, denom string, homestead, istanbul bool,
) (sdk.Coins, error)
} }
// EthSigVerificationDecorator validates an ethereum signatures // EthSigVerificationDecorator validates an ethereum signatures
@ -227,17 +230,13 @@ func (nvd EthNonceVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx,
// EthGasConsumeDecorator validates enough intrinsic gas for the transaction and // EthGasConsumeDecorator validates enough intrinsic gas for the transaction and
// gas consumption. // gas consumption.
type EthGasConsumeDecorator struct { type EthGasConsumeDecorator struct {
ak evmtypes.AccountKeeper evmKeeper EVMKeeper
bankKeeper evmtypes.BankKeeper
evmKeeper EVMKeeper
} }
// NewEthGasConsumeDecorator creates a new EthGasConsumeDecorator // NewEthGasConsumeDecorator creates a new EthGasConsumeDecorator
func NewEthGasConsumeDecorator(ak evmtypes.AccountKeeper, bankKeeper evmtypes.BankKeeper, ek EVMKeeper) EthGasConsumeDecorator { func NewEthGasConsumeDecorator(evmKeeper EVMKeeper) EthGasConsumeDecorator {
return EthGasConsumeDecorator{ return EthGasConsumeDecorator{
ak: ak, evmKeeper: evmKeeper,
bankKeeper: bankKeeper,
evmKeeper: ek,
} }
} }
@ -284,10 +283,8 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
return ctx, stacktrace.Propagate(err, "failed to unpack tx data") return ctx, stacktrace.Propagate(err, "failed to unpack tx data")
} }
fees, err := evmkeeper.DeductTxCostsFromUserBalance( fees, err := egcd.evmKeeper.DeductTxCostsFromUserBalance(
ctx, ctx,
egcd.bankKeeper,
egcd.ak,
*msgEthTx, *msgEthTx,
txData, txData,
evmDenom, evmDenom,

View File

@ -195,9 +195,7 @@ func (suite AnteTestSuite) TestEthNonceVerificationDecorator() {
} }
func (suite AnteTestSuite) TestEthGasConsumeDecorator() { func (suite AnteTestSuite) TestEthGasConsumeDecorator() {
dec := ante.NewEthGasConsumeDecorator( dec := ante.NewEthGasConsumeDecorator(suite.app.EvmKeeper)
suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.EvmKeeper,
)
addr := tests.GenerateAddress() addr := tests.GenerateAddress()

View File

@ -12,12 +12,9 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types" ethtypes "github.com/ethereum/go-ethereum/core/types"
) )
// AccountKeeper defines an expected keeper interface for the auth module's AccountKeeper
// DeductTxCostsFromUserBalance it calculates the tx costs and deducts the fees // DeductTxCostsFromUserBalance it calculates the tx costs and deducts the fees
func DeductTxCostsFromUserBalance( func (k Keeper) DeductTxCostsFromUserBalance(
ctx sdk.Context, ctx sdk.Context,
bankKeeper evmtypes.BankKeeper,
accountKeeper evmtypes.AccountKeeper,
msgEthTx evmtypes.MsgEthereumTx, msgEthTx evmtypes.MsgEthereumTx,
txData evmtypes.TxData, txData evmtypes.TxData,
denom string, denom string,
@ -27,7 +24,7 @@ func DeductTxCostsFromUserBalance(
isContractCreation := txData.GetTo() == nil isContractCreation := txData.GetTo() == nil
// fetch sender account from signature // fetch sender account from signature
signerAcc, err := authante.GetSignerAcc(ctx, accountKeeper, msgEthTx.GetFrom()) signerAcc, err := authante.GetSignerAcc(ctx, k.accountKeeper, msgEthTx.GetFrom())
if err != nil { if err != nil {
return nil, stacktrace.Propagate(err, "account not found for sender %s", msgEthTx.From) return nil, stacktrace.Propagate(err, "account not found for sender %s", msgEthTx.From)
} }
@ -62,7 +59,7 @@ func DeductTxCostsFromUserBalance(
fees := sdk.Coins{sdk.NewCoin(denom, sdk.NewIntFromBigInt(feeAmt))} fees := sdk.Coins{sdk.NewCoin(denom, sdk.NewIntFromBigInt(feeAmt))}
// deduct the full gas cost from the user balance // deduct the full gas cost from the user balance
if err := authante.DeductFees(bankKeeper, ctx, signerAcc, fees); err != nil { if err := authante.DeductFees(k.bankKeeper, ctx, signerAcc, fees); err != nil {
return nil, stacktrace.Propagate( return nil, stacktrace.Propagate(
err, err,
"failed to deduct full gas cost %s from the user %s balance", "failed to deduct full gas cost %s from the user %s balance",

View File

@ -235,10 +235,8 @@ func (suite *KeeperTestSuite) TestDeductTxCostsFromUserBalance() {
txData, _ := evmtypes.UnpackTxData(tx.Data) txData, _ := evmtypes.UnpackTxData(tx.Data)
fees, err := evmkeeper.DeductTxCostsFromUserBalance( fees, err := suite.app.EvmKeeper.DeductTxCostsFromUserBalance(
suite.app.EvmKeeper.Ctx(), suite.app.EvmKeeper.Ctx(),
suite.app.BankKeeper,
suite.app.AccountKeeper,
*tx, *tx,
txData, txData,
evmtypes.DefaultEVMDenom, evmtypes.DefaultEVMDenom,