From 466cab54c2e4a8b67a297168f13b907c0f226bc6 Mon Sep 17 00:00:00 2001 From: lumtis Date: Tue, 8 Aug 2023 18:11:28 +0200 Subject: [PATCH 1/8] add disabled messages --- app/app.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/app.go b/app/app.go index 6940a09798..7789e1582f 100644 --- a/app/app.go +++ b/app/app.go @@ -572,6 +572,10 @@ func New( SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), SigGasConsumer: evmante.DefaultSigVerificationGasConsumer, MaxTxGasWanted: maxGasWanted, + DisabledAuthzMsgs: []string{ + sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}), + sdk.MsgTypeURL(&vestingtypes.MsgCreateVestingAccount{}), + }, } anteHandler, err := ante.NewAnteHandler(options) From 897df3241a3149a1b1d41f7f654d861d283eace7 Mon Sep 17 00:00:00 2001 From: lumtis Date: Tue, 8 Aug 2023 18:13:46 +0200 Subject: [PATCH 2/8] add other vesting messages --- app/app.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/app.go b/app/app.go index 7789e1582f..6dc0f1493a 100644 --- a/app/app.go +++ b/app/app.go @@ -575,6 +575,8 @@ func New( DisabledAuthzMsgs: []string{ sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}), sdk.MsgTypeURL(&vestingtypes.MsgCreateVestingAccount{}), + sdk.MsgTypeURL(&vestingtypes.MsgCreatePermanentLockedAccount{}), + sdk.MsgTypeURL(&vestingtypes.MsgCreatePeriodicVestingAccount{}), }, } From 2df319b952ce4bbcd49a976b2c996b5b5908a1cd Mon Sep 17 00:00:00 2001 From: lumtis Date: Wed, 9 Aug 2023 15:21:35 +0200 Subject: [PATCH 3/8] option refactor --- app/ante/handler_options.go | 3 +-- app/app.go | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/ante/handler_options.go b/app/ante/handler_options.go index 13185207d9..a50e93ae7d 100644 --- a/app/ante/handler_options.go +++ b/app/ante/handler_options.go @@ -73,8 +73,7 @@ func newEthAnteHandler(options ethante.HandlerOptions) sdk.AnteHandler { func newCosmosAnteHandler(options ethante.HandlerOptions) sdk.AnteHandler { return sdk.ChainAnteDecorators( ethante.RejectMessagesDecorator{}, // reject MsgEthereumTxs - NewAuthzLimiterDecorator(sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}), // disable the Msg types that cannot be included on an authz.MsgExec msgs field - sdk.MsgTypeURL(&types.MsgCreateVestingAccount{})), + NewAuthzLimiterDecorator(options.DisabledAuthzMsgs...), ante.NewSetUpContextDecorator(), ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker), ante.NewValidateBasicDecorator(), diff --git a/app/app.go b/app/app.go index 8139b2aaf4..27096bf226 100644 --- a/app/app.go +++ b/app/app.go @@ -567,7 +567,7 @@ func New( SigGasConsumer: evmante.DefaultSigVerificationGasConsumer, MaxTxGasWanted: maxGasWanted, DisabledAuthzMsgs: []string{ - sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}), + sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}), // disable the Msg types that cannot be included on an authz.MsgExec msgs field sdk.MsgTypeURL(&vestingtypes.MsgCreateVestingAccount{}), sdk.MsgTypeURL(&vestingtypes.MsgCreatePermanentLockedAccount{}), sdk.MsgTypeURL(&vestingtypes.MsgCreatePeriodicVestingAccount{}), From 578ff9fc2b995938466c99529f3b0ff7fb698a13 Mon Sep 17 00:00:00 2001 From: lumtis Date: Wed, 9 Aug 2023 15:39:38 +0200 Subject: [PATCH 4/8] add vesting ante handler --- app/ante/handler_options.go | 11 ++++---- app/ante/vesting.go | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 app/ante/vesting.go diff --git a/app/ante/handler_options.go b/app/ante/handler_options.go index a50e93ae7d..7e0d0355b9 100644 --- a/app/ante/handler_options.go +++ b/app/ante/handler_options.go @@ -23,18 +23,16 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/auth/ante" "github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx" - "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" ibcante "github.com/cosmos/ibc-go/v6/modules/core/ante" ethante "github.com/evmos/ethermint/app/ante" ethermint "github.com/evmos/ethermint/types" - evmtypes "github.com/evmos/ethermint/x/evm/types" ) func NewLegacyCosmosAnteHandlerEip712(options ethante.HandlerOptions) sdk.AnteHandler { return sdk.ChainAnteDecorators( ethante.RejectMessagesDecorator{}, // reject MsgEthereumTxs - NewAuthzLimiterDecorator(sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}), // disable the Msg types that cannot be included on an authz.MsgExec msgs field - sdk.MsgTypeURL(&types.MsgCreateVestingAccount{})), + NewAuthzLimiterDecorator(options.DisabledAuthzMsgs...), + NewVestingAccountDecorator(), ante.NewSetUpContextDecorator(), ante.NewValidateBasicDecorator(), ante.NewTxTimeoutHeightDecorator(), @@ -74,6 +72,7 @@ func newCosmosAnteHandler(options ethante.HandlerOptions) sdk.AnteHandler { return sdk.ChainAnteDecorators( ethante.RejectMessagesDecorator{}, // reject MsgEthereumTxs NewAuthzLimiterDecorator(options.DisabledAuthzMsgs...), + NewVestingAccountDecorator(), ante.NewSetUpContextDecorator(), ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker), ante.NewValidateBasicDecorator(), @@ -96,8 +95,8 @@ func newCosmosAnteHandler(options ethante.HandlerOptions) sdk.AnteHandler { func newCosmosAnteHandlerNoGasLimit(options ethante.HandlerOptions) sdk.AnteHandler { return sdk.ChainAnteDecorators( ethante.RejectMessagesDecorator{}, // reject MsgEthereumTxs - NewAuthzLimiterDecorator(sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}), // disable the Msg types that cannot be included on an authz.MsgExec msgs field - sdk.MsgTypeURL(&types.MsgCreateVestingAccount{})), + NewAuthzLimiterDecorator(options.DisabledAuthzMsgs...), + NewVestingAccountDecorator(), NewSetUpContextDecorator(), ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker), ante.NewValidateBasicDecorator(), diff --git a/app/ante/vesting.go b/app/ante/vesting.go new file mode 100644 index 0000000000..23cef571bf --- /dev/null +++ b/app/ante/vesting.go @@ -0,0 +1,50 @@ +package ante + +import ( + errorsmod "cosmossdk.io/errors" + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" +) + +var _ sdk.AnteDecorator = VestingAccountDecorator{} + +// VestingAccountDecorator blocks vesting messages from reaching the mempool +type VestingAccountDecorator struct { + disabledMsgTypeUrls []string +} + +// NewVestingAccountDecorator creates a decorator to block vesting messages from reaching the mempool +func NewVestingAccountDecorator() VestingAccountDecorator { + return VestingAccountDecorator{ + disabledMsgTypeUrls: []string{ + sdk.MsgTypeURL(&vesting.MsgCreateVestingAccount{}), + sdk.MsgTypeURL(&vesting.MsgCreatePermanentLockedAccount{}), + sdk.MsgTypeURL(&vesting.MsgCreatePeriodicVestingAccount{}), + }, + } +} + +// AnteHandle implements AnteDecorator +func (vad VestingAccountDecorator) AnteHandle( + ctx sdk.Context, + tx sdk.Tx, + simulate bool, + next sdk.AnteHandler, +) (newCtx sdk.Context, err error) { + for _, msg := range tx.GetMsgs() { + typeUrl := sdk.MsgTypeURL(msg) + + for _, disabledTypeUrl := range vad.disabledMsgTypeUrls { + if typeUrl == disabledTypeUrl { + return ctx, errorsmod.Wrapf( + sdkerrors.ErrUnauthorized, + "MsgTypeURL %s not supported", + typeUrl, + ) + } + } + } + + return next(ctx, tx, simulate) +} From 468ee984c14820f9ead2d8aeed28bbb4c129e55a Mon Sep 17 00:00:00 2001 From: lumtis Date: Wed, 9 Aug 2023 15:58:51 +0200 Subject: [PATCH 5/8] implement test for vesting ante handler --- app/ante/ante_test.go | 18 +++++++ app/ante/vesting_test.go | 104 ++++++++++++++++++++++++++++++++++++++ testutil/sample/sample.go | 8 +++ 3 files changed, 130 insertions(+) create mode 100644 app/ante/ante_test.go create mode 100644 app/ante/vesting_test.go diff --git a/app/ante/ante_test.go b/app/ante/ante_test.go new file mode 100644 index 0000000000..53a7d95c85 --- /dev/null +++ b/app/ante/ante_test.go @@ -0,0 +1,18 @@ +package ante_test + +import sdk "github.com/cosmos/cosmos-sdk/types" + +var _ sdk.AnteHandler = (&MockAnteHandler{}).AnteHandle + +// MockAnteHandler mocks an AnteHandler +type MockAnteHandler struct { + WasCalled bool + CalledCtx sdk.Context +} + +// AnteHandle implements AnteHandler +func (mah *MockAnteHandler) AnteHandle(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) { + mah.WasCalled = true + mah.CalledCtx = ctx + return ctx, nil +} diff --git a/app/ante/vesting_test.go b/app/ante/vesting_test.go new file mode 100644 index 0000000000..fac1cb16d5 --- /dev/null +++ b/app/ante/vesting_test.go @@ -0,0 +1,104 @@ +package ante_test + +import ( + "math/rand" + "testing" + "time" + + "github.com/cosmos/cosmos-sdk/simapp/helpers" + sdk "github.com/cosmos/cosmos-sdk/types" + vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/stretchr/testify/require" + + "github.com/zeta-chain/zetacore/app" + "github.com/zeta-chain/zetacore/app/ante" + "github.com/zeta-chain/zetacore/testutil/sample" +) + +func TestVesting_AnteHandle(t *testing.T) { + txConfig := app.MakeEncodingConfig().TxConfig + + testPrivKey, testAddress := sample.PrivKeyAddressPair() + _, testAddress2 := sample.PrivKeyAddressPair() + + decorator := ante.NewVestingAccountDecorator() + + tests := []struct { + name string + msg sdk.Msg + wantHasErr bool + wantErr string + }{ + { + "MsgCreateVestingAccount", + vesting.NewMsgCreateVestingAccount( + testAddress, testAddress2, + sdk.NewCoins(sdk.NewInt64Coin("azeta", 100_000_000)), + time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC).Unix(), + false, + ), + true, + "MsgTypeURL /cosmos.vesting.v1beta1.MsgCreateVestingAccount not supported", + }, + { + "MsgCreatePermanentLockedAccount", + vesting.NewMsgCreatePermanentLockedAccount( + testAddress, testAddress2, + sdk.NewCoins(sdk.NewInt64Coin("azeta", 100_000_000)), + ), + true, + "MsgTypeURL /cosmos.vesting.v1beta1.MsgCreatePermanentLockedAccount not supported", + }, + { + "MsgCreatePeriodicVestingAccount", + vesting.NewMsgCreatePeriodicVestingAccount( + testAddress, testAddress2, + time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC).Unix(), + nil, + ), + true, + "MsgTypeURL /cosmos.vesting.v1beta1.MsgCreatePeriodicVestingAccount not supported", + }, + { + "Non blocked message", + banktypes.NewMsgSend( + testAddress, testAddress2, + sdk.NewCoins(sdk.NewInt64Coin("azeta", 100_000_000)), + ), + false, + "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tx, err := helpers.GenSignedMockTx( + rand.New(rand.NewSource(time.Now().UnixNano())), + txConfig, + []sdk.Msg{ + tt.msg, + }, + sdk.NewCoins(), + helpers.DefaultGenTxGas, + "testing-chain-id", + []uint64{0}, + []uint64{0}, + testPrivKey, + ) + require.NoError(t, err) + + mmd := MockAnteHandler{} + ctx := sdk.Context{}.WithIsCheckTx(true) + + _, err = decorator.AnteHandle(ctx, tx, false, mmd.AnteHandle) + + if tt.wantHasErr { + require.Error(t, err) + require.Contains(t, err.Error(), tt.wantErr) + } else { + require.NoError(t, err) + } + }) + } +} diff --git a/testutil/sample/sample.go b/testutil/sample/sample.go index 98f2153ed4..a09d3a7e44 100644 --- a/testutil/sample/sample.go +++ b/testutil/sample/sample.go @@ -11,3 +11,11 @@ func AccAddress() string { addr := pk.Address() return sdk.AccAddress(addr).String() } + +// PrivKeyAddressPair returns a private key, address pair +func PrivKeyAddressPair() (*ed25519.PrivKey, sdk.AccAddress) { + privKey := ed25519.GenPrivKey() + addr := privKey.PubKey().Address() + + return privKey, sdk.AccAddress(addr) +} From dbc3220c943d1677c00b0dd12d99edf988e0f33a Mon Sep 17 00:00:00 2001 From: lumtis Date: Wed, 9 Aug 2023 16:01:38 +0200 Subject: [PATCH 6/8] lint issues --- app/ante/vesting.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/ante/vesting.go b/app/ante/vesting.go index 23cef571bf..2c3ac5fb54 100644 --- a/app/ante/vesting.go +++ b/app/ante/vesting.go @@ -33,14 +33,14 @@ func (vad VestingAccountDecorator) AnteHandle( next sdk.AnteHandler, ) (newCtx sdk.Context, err error) { for _, msg := range tx.GetMsgs() { - typeUrl := sdk.MsgTypeURL(msg) + typeURL := sdk.MsgTypeURL(msg) for _, disabledTypeUrl := range vad.disabledMsgTypeUrls { - if typeUrl == disabledTypeUrl { + if typeURL == disabledTypeUrl { return ctx, errorsmod.Wrapf( sdkerrors.ErrUnauthorized, "MsgTypeURL %s not supported", - typeUrl, + typeURL, ) } } From 5eb2212b1b8695bb9b3d46548f358746deb68315 Mon Sep 17 00:00:00 2001 From: lumtis Date: Wed, 9 Aug 2023 16:03:44 +0200 Subject: [PATCH 7/8] fix lint 2 --- app/ante/vesting.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/ante/vesting.go b/app/ante/vesting.go index 2c3ac5fb54..7ba2497a15 100644 --- a/app/ante/vesting.go +++ b/app/ante/vesting.go @@ -11,13 +11,13 @@ var _ sdk.AnteDecorator = VestingAccountDecorator{} // VestingAccountDecorator blocks vesting messages from reaching the mempool type VestingAccountDecorator struct { - disabledMsgTypeUrls []string + disabledMsgTypeURLs []string } // NewVestingAccountDecorator creates a decorator to block vesting messages from reaching the mempool func NewVestingAccountDecorator() VestingAccountDecorator { return VestingAccountDecorator{ - disabledMsgTypeUrls: []string{ + disabledMsgTypeURLs: []string{ sdk.MsgTypeURL(&vesting.MsgCreateVestingAccount{}), sdk.MsgTypeURL(&vesting.MsgCreatePermanentLockedAccount{}), sdk.MsgTypeURL(&vesting.MsgCreatePeriodicVestingAccount{}), @@ -35,7 +35,7 @@ func (vad VestingAccountDecorator) AnteHandle( for _, msg := range tx.GetMsgs() { typeURL := sdk.MsgTypeURL(msg) - for _, disabledTypeUrl := range vad.disabledMsgTypeUrls { + for _, disabledTypeUrl := range vad.disabledMsgTypeURLs { if typeURL == disabledTypeUrl { return ctx, errorsmod.Wrapf( sdkerrors.ErrUnauthorized, From 9bbcd4d3d8a5392fdfc4ce459a0149e3f02c3e63 Mon Sep 17 00:00:00 2001 From: lumtis Date: Wed, 9 Aug 2023 16:13:17 +0200 Subject: [PATCH 8/8] fix lint 3 --- app/ante/vesting.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/ante/vesting.go b/app/ante/vesting.go index 7ba2497a15..b24f169bbb 100644 --- a/app/ante/vesting.go +++ b/app/ante/vesting.go @@ -35,8 +35,8 @@ func (vad VestingAccountDecorator) AnteHandle( for _, msg := range tx.GetMsgs() { typeURL := sdk.MsgTypeURL(msg) - for _, disabledTypeUrl := range vad.disabledMsgTypeURLs { - if typeURL == disabledTypeUrl { + for _, disabledTypeURL := range vad.disabledMsgTypeURLs { + if typeURL == disabledTypeURL { return ctx, errorsmod.Wrapf( sdkerrors.ErrUnauthorized, "MsgTypeURL %s not supported",