From f850e3bc84ee450745a81e629ef6017f852a9b23 Mon Sep 17 00:00:00 2001 From: keruch <53012408+keruch@users.noreply.github.com> Date: Tue, 29 Oct 2024 20:21:37 +0300 Subject: [PATCH] feat(ante): whitelisted relayers (#357) --- app/ante/ante.go | 16 +++++--- app/ante/bypass_ibc_fee_decorator.go | 57 ---------------------------- app/ante/create_account_decorator.go | 11 +++--- app/ante/handlers.go | 13 ++++++- app/ante/sigcheck_decorator.go | 3 +- app/app.go | 12 ++++-- app/test_helpers.go | 2 - go.mod | 4 +- go.sum | 8 ++-- 9 files changed, 45 insertions(+), 81 deletions(-) delete mode 100644 app/ante/bypass_ibc_fee_decorator.go diff --git a/app/ante/ante.go b/app/ante/ante.go index 42408397..41aec5bf 100644 --- a/app/ante/ante.go +++ b/app/ante/ante.go @@ -4,6 +4,8 @@ import ( "fmt" "runtime/debug" + distrkeeper "github.com/dymensionxyz/dymension-rdk/x/dist/keeper" + seqkeeper "github.com/dymensionxyz/dymension-rdk/x/sequencers/keeper" cosmosante "github.com/evmos/evmos/v12/app/ante/cosmos" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -19,7 +21,6 @@ import ( ibckeeper "github.com/cosmos/ibc-go/v6/modules/core/keeper" evmosante "github.com/evmos/evmos/v12/app/ante" evmosanteevm "github.com/evmos/evmos/v12/app/ante/evm" - anteutils "github.com/evmos/evmos/v12/app/ante/utils" evmostypes "github.com/evmos/evmos/v12/types" evmtypes "github.com/evmos/evmos/v12/x/evm/types" evmosvestingtypes "github.com/evmos/evmos/v12/x/vesting/types" @@ -38,7 +39,8 @@ func MustCreateHandler(codec codec.BinaryCodec, feeMarketKeeper evmosanteevm.FeeMarketKeeper, evmKeeper evmosanteevm.EVMKeeper, ibcKeeper *ibckeeper.Keeper, - distrKeeper anteutils.DistributionKeeper, + distrKeeper distrkeeper.Keeper, + sequencerKeeper seqkeeper.Keeper, ) sdk.AnteHandler { ethOpts := evmosante.HandlerOptions{ Cdc: codec, @@ -57,8 +59,10 @@ func MustCreateHandler(codec codec.BinaryCodec, } opts := HandlerOptions{ - HandlerOptions: ethOpts, - hasPermission: hasPermission, + HandlerOptions: ethOpts, + hasPermission: hasPermission, + DistrKeeper: distrKeeper, + SequencersKeeper: sequencerKeeper, } h, err := NewHandler(opts) @@ -71,7 +75,9 @@ func MustCreateHandler(codec codec.BinaryCodec, // HandlerOptions are the options required for constructing a default SDK AnteHandler. type HandlerOptions struct { evmosante.HandlerOptions - hasPermission HasPermission + hasPermission HasPermission + DistrKeeper distrkeeper.Keeper + SequencersKeeper seqkeeper.Keeper } func (o HandlerOptions) validate() error { diff --git a/app/ante/bypass_ibc_fee_decorator.go b/app/ante/bypass_ibc_fee_decorator.go deleted file mode 100644 index 3a4862cd..00000000 --- a/app/ante/bypass_ibc_fee_decorator.go +++ /dev/null @@ -1,57 +0,0 @@ -package ante - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types" - conntypes "github.com/cosmos/ibc-go/v6/modules/core/03-connection/types" - channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types" -) - -type anteHandler interface { - AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) -} - -type BypassIBCFeeDecorator struct { - nextAnte anteHandler -} - -func NewBypassIBCFeeDecorator(nextAnte anteHandler) BypassIBCFeeDecorator { - return BypassIBCFeeDecorator{nextAnte: nextAnte} -} - -// SKIP FEE DEDUCT and MIN GAS PRICE Ante handlers for IBC relayer messages -func (n BypassIBCFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { - // ======== HACK ================ - if isIBCRelayerMsg(tx.GetMsgs()) { - return next(ctx, tx, simulate) - } - // ============================== - - // If it's not an IBC Relayer transfer, proceed with the default fee handling - return n.nextAnte.AnteHandle(ctx, tx, simulate, next) -} - -// isIBCRelayerMsg checks if all the messages in the transaction are IBC relayer messages -func isIBCRelayerMsg(msgs []sdk.Msg) bool { - for _, msg := range msgs { - switch msg.(type) { - // IBC Client Messages - case *clienttypes.MsgCreateClient, *clienttypes.MsgUpdateClient, - *clienttypes.MsgUpgradeClient, *clienttypes.MsgSubmitMisbehaviour: - // IBC Connection Messages - case *conntypes.MsgConnectionOpenInit, *conntypes.MsgConnectionOpenTry, - *conntypes.MsgConnectionOpenAck, *conntypes.MsgConnectionOpenConfirm: - // IBC Channel Messages - case *channeltypes.MsgChannelOpenInit, *channeltypes.MsgChannelOpenTry, - *channeltypes.MsgChannelOpenAck, *channeltypes.MsgChannelOpenConfirm, - *channeltypes.MsgChannelCloseInit, *channeltypes.MsgChannelCloseConfirm: - // IBC Packet Messages - case *channeltypes.MsgRecvPacket, *channeltypes.MsgAcknowledgement, - *channeltypes.MsgTimeout, *channeltypes.MsgTimeoutOnClose: - default: - return false - } - } - - return true -} diff --git a/app/ante/create_account_decorator.go b/app/ante/create_account_decorator.go index ed46bb89..31ac86fd 100644 --- a/app/ante/create_account_decorator.go +++ b/app/ante/create_account_decorator.go @@ -7,9 +7,10 @@ import ( authante "github.com/cosmos/cosmos-sdk/x/auth/ante" authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" "github.com/cosmos/cosmos-sdk/x/auth/types" + rdkante "github.com/dymensionxyz/dymension-rdk/server/ante" ) -type createAccountDecorator struct { +type CreateAccountDecorator struct { ak accountKeeper } @@ -18,8 +19,8 @@ type accountKeeper interface { NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) types.AccountI } -func NewCreateAccountDecorator(ak accountKeeper) createAccountDecorator { - return createAccountDecorator{ak: ak} +func NewCreateAccountDecorator(ak accountKeeper) CreateAccountDecorator { + return CreateAccountDecorator{ak: ak} } const newAccountCtxKeyPrefix = "new-account/" @@ -28,7 +29,7 @@ func CtxKeyNewAccount(acc string) string { return newAccountCtxKeyPrefix + acc } -func (cad createAccountDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { +func (cad CreateAccountDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { sigTx, ok := tx.(authsigning.SigVerifiableTx) if !ok { return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid tx type") @@ -39,7 +40,7 @@ func (cad createAccountDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulat return ctx, err } - ibcRelayerMsg := isIBCRelayerMsg(tx.GetMsgs()) + ibcRelayerMsg := rdkante.IsIBCRelayerMsg(tx.GetMsgs()) for i, pk := range pubkeys { if pk == nil { diff --git a/app/ante/handlers.go b/app/ante/handlers.go index fcb21601..e3185646 100644 --- a/app/ante/handlers.go +++ b/app/ante/handlers.go @@ -8,6 +8,7 @@ import ( vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" conntypes "github.com/cosmos/ibc-go/v6/modules/core/03-connection/types" ibcante "github.com/cosmos/ibc-go/v6/modules/core/ante" + rdkante "github.com/dymensionxyz/dymension-rdk/server/ante" cosmosante "github.com/evmos/evmos/v12/app/ante/cosmos" evmante "github.com/evmos/evmos/v12/app/ante/evm" evmtypes "github.com/evmos/evmos/v12/x/evm/types" @@ -70,10 +71,18 @@ func cosmosHandler(options HandlerOptions, sigChecker sdk.AnteDecorator) sdk.Ant sdk.MsgTypeURL(&vestingtypes.MsgCreatePeriodicVestingAccount{}), }), ante.NewValidateMemoDecorator(options.AccountKeeper), - NewBypassIBCFeeDecorator(cosmosante.NewMinGasPriceDecorator(options.FeeMarketKeeper, options.EvmKeeper)), + rdkante.NewBypassIBCFeeDecorator( + cosmosante.NewMinGasPriceDecorator(options.FeeMarketKeeper, options.EvmKeeper), + options.DistrKeeper, + options.SequencersKeeper, + ), NewCreateAccountDecorator(options.AccountKeeper), ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper), - NewBypassIBCFeeDecorator(cosmosante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.DistributionKeeper, options.FeegrantKeeper, options.StakingKeeper, options.TxFeeChecker)), + rdkante.NewBypassIBCFeeDecorator( + cosmosante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.DistributionKeeper, options.FeegrantKeeper, options.StakingKeeper, options.TxFeeChecker), + options.DistrKeeper, + options.SequencersKeeper, + ), // SetPubKeyDecorator must be called before all signature verification decorators ante.NewSetPubKeyDecorator(options.AccountKeeper), ante.NewValidateSigCountDecorator(options.AccountKeeper), diff --git a/app/ante/sigcheck_decorator.go b/app/ante/sigcheck_decorator.go index 90d73494..104b7d8e 100644 --- a/app/ante/sigcheck_decorator.go +++ b/app/ante/sigcheck_decorator.go @@ -8,6 +8,7 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" authante "github.com/cosmos/cosmos-sdk/x/auth/ante" authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" + rdkante "github.com/dymensionxyz/dymension-rdk/server/ante" ) type sigCheckDecorator struct { @@ -41,7 +42,7 @@ func (svd sigCheckDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate boo return ctx, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "invalid number of signer; expected: %d, got %d", len(signerAddrs), len(sigs)) } - ibcRelayerMsg := isIBCRelayerMsg(tx.GetMsgs()) + ibcRelayerMsg := rdkante.IsIBCRelayerMsg(tx.GetMsgs()) for i, sig := range sigs { acc, err := authante.GetSignerAcc(ctx, svd.ak, signerAddrs[i]) diff --git a/app/app.go b/app/app.go index 2fc85d53..e730f161 100644 --- a/app/app.go +++ b/app/app.go @@ -11,7 +11,7 @@ import ( authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module" "github.com/dymensionxyz/dymension-rdk/server/consensus" - "github.com/dymensionxyz/rollapp-evm/app/ante" + "github.com/gogo/protobuf/proto" "github.com/gorilla/mux" "github.com/rakyll/statik/fs" "github.com/spf13/cast" @@ -21,6 +21,8 @@ import ( tmos "github.com/tendermint/tendermint/libs/os" dbm "github.com/tendermint/tm-db" + "github.com/dymensionxyz/rollapp-evm/app/ante" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" nodeservice "github.com/cosmos/cosmos-sdk/client/grpc/node" @@ -487,7 +489,10 @@ func NewRollapp( ) app.SequencersKeeper = *seqkeeper.NewKeeper( - appCodec, keys[seqtypes.StoreKey], app.GetSubspace(seqtypes.ModuleName), + appCodec, + keys[seqtypes.StoreKey], + app.GetSubspace(seqtypes.ModuleName), + authtypes.NewModuleAddress(seqtypes.ModuleName).String(), ) app.TimeUpgradeKeeper = timeupgradekeeper.NewKeeper( @@ -836,13 +841,14 @@ func NewRollapp( app.EvmKeeper, app.IBCKeeper, app.DistrKeeper, + app.SequencersKeeper, ) app.SetAnteHandler(h) app.setPostHandler() // Admission handler for consensus messages app.setAdmissionHandler(consensus.AllowedMessagesHandler([]string{ - // proto.MessageName(&banktypes.MsgSend{}), // Example of message allowed as consensus message + proto.MessageName(new(seqtypes.ConsensusMsgUpsertSequencer)), })) if loadLatest { diff --git a/app/test_helpers.go b/app/test_helpers.go index eeaad63b..0f811bd2 100644 --- a/app/test_helpers.go +++ b/app/test_helpers.go @@ -2,7 +2,6 @@ package app import ( "encoding/json" - "fmt" "testing" "time" @@ -72,7 +71,6 @@ func SetupWithOneValidator(t *testing.T) (*App, authtypes.AccountI) { Address: acc.GetAddress().String(), Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))), } - fmt.Printf("address: %s\n", acc.GetAddress().String()) app := SetupWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, balance) diff --git a/go.mod b/go.mod index d1b1f93d..82582d05 100644 --- a/go.mod +++ b/go.mod @@ -8,8 +8,8 @@ require ( github.com/bcdevtools/evm-block-explorer-rpc-cosmos v1.1.4 github.com/cosmos/cosmos-sdk v0.46.16 github.com/cosmos/ibc-go/v6 v6.2.1 - github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241020152926-b250db7af580 - github.com/dymensionxyz/dymint v1.2.0-rc01.0.20241015102238-a827c3784461 + github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241029165002-545051749bc0 + github.com/dymensionxyz/dymint v1.2.0-rc01.0.20241024144934-db109b2859b4 github.com/ethereum/go-ethereum v1.12.0 github.com/evmos/evmos/v12 v12.1.6 github.com/gogo/protobuf v1.3.3 diff --git a/go.sum b/go.sum index 80a3d6ac..2efa2155 100644 --- a/go.sum +++ b/go.sum @@ -577,10 +577,10 @@ github.com/dymensionxyz/cometbft v0.34.29-0.20241008141942-63af9d24107f h1:CclWJ github.com/dymensionxyz/cometbft v0.34.29-0.20241008141942-63af9d24107f/go.mod h1:L9shMfbkZ8B+7JlwANEr+NZbBcn+hBpwdbeYvA5rLCw= github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20240821081230-b4018b2bac13 h1:u5yeve5jZR6TdRjjR+vYT/8PWKbhwCZxUmAu+/Tnxyg= github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20240821081230-b4018b2bac13/go.mod h1:jabDQYXrccscSE0fXkh7eQFYPWJCRiuWKonFGObVq6s= -github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241020152926-b250db7af580 h1:bgECFCjc/8lVq9/UQ4qCTdD/znUgKEX7a0QBOcGuuNg= -github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241020152926-b250db7af580/go.mod h1:m9D/CACMkMAXKFeuU+1lkUIPWpMFb0EvGl7M5vV0HoA= -github.com/dymensionxyz/dymint v1.2.0-rc01.0.20241015102238-a827c3784461 h1:ZNh5s2dmfcgPE9g6OlhHBFtygiAxCRr+wPKSgpSo/Bw= -github.com/dymensionxyz/dymint v1.2.0-rc01.0.20241015102238-a827c3784461/go.mod h1:FNVdsvVhveAOeL0Mj9VIfGkAUKhGskEr0iRnoM+MAkU= +github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241029165002-545051749bc0 h1:Pshmw95Ej/RBg+ZTgMxpdGB/Bpis2tKEjSmqwVHgaXo= +github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241029165002-545051749bc0/go.mod h1:HH7sGM/4MSI8OLces8axABf7K0ppXNok+G2nxC/l5YI= +github.com/dymensionxyz/dymint v1.2.0-rc01.0.20241024144934-db109b2859b4 h1:DpfHfiqNJ0QgnPNq/nx2fAUDWwLlWORQ605uQyPPODU= +github.com/dymensionxyz/dymint v1.2.0-rc01.0.20241024144934-db109b2859b4/go.mod h1:FNVdsvVhveAOeL0Mj9VIfGkAUKhGskEr0iRnoM+MAkU= github.com/dymensionxyz/evmos/v12 v12.1.6-dymension-v0.4.2 h1:aVP3off7u2vsvRH7lHAUPTLdf9/AfnzC/rvvi0wC/co= github.com/dymensionxyz/evmos/v12 v12.1.6-dymension-v0.4.2/go.mod h1:CI6D89pkoiIm4BjoMFNnEaCLdKBEobLuwvhS0c1zh7Y= github.com/dymensionxyz/gerr-cosmos v1.0.0 h1:oi91rgOkpJWr41oX9JOyjvvBnhGY54tj513x8VlDAEc=