Skip to content

Commit

Permalink
imp(ante): Refactor ante handlers to be easier to use in partner chai…
Browse files Browse the repository at this point in the history
…ns (target main) (#52)
  • Loading branch information
MalteHerrmann authored Oct 2, 2024
1 parent bb4b78d commit 02a9121
Showing 19 changed files with 323 additions and 395 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ This changelog was created using the `clu` binary

### Improvements

- (ante) [#52](https://github.com/evmos/os/pull/52) Refactor ante handlers to be easier to use in partner chains.
- (all) [#48](https://github.com/evmos/os/pull/48) Move latest changes from evmOS main (f943af3b incl. SDK v50).
- (all) [#43](https://github.com/evmos/os/pull/43) Update with latest evmOS main changes (2b7a8e2).
- (tests) [#41](https://github.com/evmos/os/pull/41) Add Solidity and Ledger tests.
6 changes: 3 additions & 3 deletions ante/cosmos/eip712.go
Original file line number Diff line number Diff line change
@@ -19,10 +19,10 @@ import (
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
anteinterfaces "github.com/evmos/os/ante/interfaces"
"github.com/evmos/os/crypto/ethsecp256k1"
"github.com/evmos/os/ethereum/eip712"
"github.com/evmos/os/types"
evmtypes "github.com/evmos/os/x/evm/types"
)

var evmosCodec codec.ProtoCodecMarshaler
@@ -40,12 +40,12 @@ func init() {
// CONTRACT: Pubkeys are set in context for all signers before this decorator runs
// CONTRACT: Tx must implement SigVerifiableTx interface
type LegacyEip712SigVerificationDecorator struct {
ak evmtypes.AccountKeeper
ak anteinterfaces.AccountKeeper
}

// Deprecated: NewLegacyEip712SigVerificationDecorator creates a new LegacyEip712SigVerificationDecorator
func NewLegacyEip712SigVerificationDecorator(
ak evmtypes.AccountKeeper,
ak anteinterfaces.AccountKeeper,
) LegacyEip712SigVerificationDecorator {
return LegacyEip712SigVerificationDecorator{
ak: ak,
7 changes: 6 additions & 1 deletion ante/cosmos/reject_msgs.go
Original file line number Diff line number Diff line change
@@ -10,9 +10,14 @@ import (
evmtypes "github.com/evmos/os/x/evm/types"
)

// RejectMessagesDecorator prevents invalid msg types from being executed
// RejectMessagesDecorator prevents invalid msg types from being executed.
type RejectMessagesDecorator struct{}

// NewRejectMessagesDecorator creates a new RejectMessagesDecorator.
func NewRejectMessagesDecorator() sdk.AnteDecorator {
return RejectMessagesDecorator{}
}

// AnteHandle rejects messages that requires ethereum-specific authentication.
// For example `MsgEthereumTx` requires fee to be deducted in the antehandler in
// order to perform the refund.
3 changes: 2 additions & 1 deletion ante/evm/06_account_verification.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
anteinterfaces "github.com/evmos/os/ante/interfaces"
"github.com/evmos/os/x/evm/keeper"
"github.com/evmos/os/x/evm/statedb"
evmtypes "github.com/evmos/os/x/evm/types"
@@ -21,7 +22,7 @@ import (
// - account balance is lower than the transaction cost
func VerifyAccountBalance(
ctx sdk.Context,
accountKeeper evmtypes.AccountKeeper,
accountKeeper anteinterfaces.AccountKeeper,
account *statedb.Account,
from common.Address,
txData evmtypes.TxData,
17 changes: 5 additions & 12 deletions ante/evm/08_gas_consume.go
Original file line number Diff line number Diff line change
@@ -36,23 +36,16 @@ func UpdateCumulativeGasWanted(
return cumulativeGasWanted
}

type ConsumeGasKeepers struct {
Bank anteinterfaces.BankKeeper
Distribution anteinterfaces.DistributionKeeper
Evm anteinterfaces.EVMKeeper
Staking anteinterfaces.StakingKeeper
}

// ConsumeFeesAndEmitEvent deduces fees from sender and emits the event
func ConsumeFeesAndEmitEvent(
ctx sdktypes.Context,
keepers *ConsumeGasKeepers,
evmKeeper anteinterfaces.EVMKeeper,
fees sdktypes.Coins,
from sdktypes.AccAddress,
) error {
if err := deductFees(
ctx,
keepers,
evmKeeper,
fees,
from,
); err != nil {
@@ -69,24 +62,24 @@ func ConsumeFeesAndEmitEvent(
}

// deductFee checks if the fee payer has enough funds to pay for the fees and deducts them.
// If the spendable balance is not enough, it tries to claim enough staking rewards to cover the fees.
func deductFees(
ctx sdktypes.Context,
keepers *ConsumeGasKeepers,
evmKeeper anteinterfaces.EVMKeeper,
fees sdktypes.Coins,
feePayer sdktypes.AccAddress,
) error {
if fees.IsZero() {
return nil
}

if err := keepers.Evm.DeductTxCostsFromUserBalance(
if err := evmKeeper.DeductTxCostsFromUserBalance(
ctx,
fees,
common.BytesToAddress(feePayer),
); err != nil {
return errorsmod.Wrapf(err, "failed to deduct transaction costs from user balance")
}

return nil
}

8 changes: 1 addition & 7 deletions ante/evm/08_gas_consume_test.go
Original file line number Diff line number Diff line change
@@ -146,12 +146,6 @@ func (suite *EvmAnteTestSuite) TestConsumeGasAndEmitEvent() {

for _, tc := range testCases {
suite.Run(tc.name, func() {
keepers := &evmante.ConsumeGasKeepers{
Bank: unitNetwork.App.BankKeeper,
Distribution: unitNetwork.App.DistrKeeper,
Evm: unitNetwork.App.EVMKeeper,
Staking: unitNetwork.App.StakingKeeper,
}
sender := tc.getSender()
prevBalance, err := grpcHandler.GetAllBalances(
sender,
@@ -161,7 +155,7 @@ func (suite *EvmAnteTestSuite) TestConsumeGasAndEmitEvent() {
// Function under test
err = evmante.ConsumeFeesAndEmitEvent(
unitNetwork.GetContext(),
keepers,
unitNetwork.App.EVMKeeper,
tc.fees,
sender,
)
4 changes: 2 additions & 2 deletions ante/evm/09_increment_sequence.go
Original file line number Diff line number Diff line change
@@ -7,13 +7,13 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
evmtypes "github.com/evmos/os/x/evm/types"
anteinterfaces "github.com/evmos/os/ante/interfaces"
)

// IncrementNonce increments the sequence of the account.
func IncrementNonce(
ctx sdk.Context,
accountKeeper evmtypes.AccountKeeper,
accountKeeper anteinterfaces.AccountKeeper,
account sdk.AccountI,
txNonce uint64,
) error {
8 changes: 1 addition & 7 deletions ante/evm/eth_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -65,12 +65,6 @@ func BenchmarkEthGasConsumeDecorator(b *testing.B) {
vmdb = testutil.NewStateDB(cacheCtx, s.GetNetwork().App.EVMKeeper)
cacheCtx = s.prepareAccount(cacheCtx, addr.Bytes(), tc.balance, tc.rewards)
s.Require().NoError(vmdb.Commit())
keepers := ethante.ConsumeGasKeepers{
Bank: s.GetNetwork().App.BankKeeper,
Distribution: s.GetNetwork().App.DistrKeeper,
Evm: s.GetNetwork().App.EVMKeeper,
Staking: s.GetNetwork().App.StakingKeeper,
}

baseFee := s.GetNetwork().App.FeeMarketKeeper.GetParams(ctx).BaseFee
fee := tx.GetEffectiveFee(baseFee.BigInt())
@@ -83,7 +77,7 @@ func BenchmarkEthGasConsumeDecorator(b *testing.B) {

err := ethante.ConsumeFeesAndEmitEvent(
cacheCtx.WithIsCheckTx(true).WithGasMeter(storetypes.NewInfiniteGasMeter()),
&keepers,
s.GetNetwork().App.EVMKeeper,
fees,
bechAddr,
)
Loading

0 comments on commit 02a9121

Please sign in to comment.