From d99aa1118403ce193b5260a68b143cc42a4f2c54 Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 01:15:45 -0800 Subject: [PATCH 01/16] exempt system txs from min gas check and fee deduction in antehandlers. System txs are 7 zetaclient txs from observers. --- app/ante/ante.go | 75 ++++++++++++++++++++++++++----------- app/ante/handler_options.go | 38 +++++++++++++++---- app/app.go | 3 +- 3 files changed, 86 insertions(+), 30 deletions(-) diff --git a/app/ante/ante.go b/app/ante/ante.go index 7d9586edfb..83d8345b4a 100644 --- a/app/ante/ante.go +++ b/app/ante/ante.go @@ -17,10 +17,10 @@ package ante import ( "fmt" + observertypes "github.com/zeta-chain/zetacore/x/observer/types" "runtime/debug" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - ethante "github.com/evmos/ethermint/app/ante" cctxtypes "github.com/zeta-chain/zetacore/x/crosschain/types" tmlog "github.com/tendermint/tendermint/libs/log" @@ -29,9 +29,10 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" errortypes "github.com/cosmos/cosmos-sdk/types/errors" authante "github.com/cosmos/cosmos-sdk/x/auth/ante" + "github.com/cosmos/cosmos-sdk/x/authz" ) -func ValidateHandlerOptions(options ethante.HandlerOptions) error { +func ValidateHandlerOptions(options HandlerOptions) error { if options.AccountKeeper == nil { return errorsmod.Wrap(errortypes.ErrLogic, "account keeper is required for AnteHandler") } @@ -47,6 +48,9 @@ func ValidateHandlerOptions(options ethante.HandlerOptions) error { if options.EvmKeeper == nil { return errorsmod.Wrap(errortypes.ErrLogic, "evm keeper is required for AnteHandler") } + if options.ObserverKeeper == nil { + return errorsmod.Wrap(errortypes.ErrLogic, "observer keeper is required for AnteHandler") + } return nil } @@ -54,7 +58,7 @@ func ValidateHandlerOptions(options ethante.HandlerOptions) error { // Ethereum or SDK transaction to an internal ante handler for performing // transaction-level processing (e.g. fee payment, signature verification) before // being passed onto it's respective handler. -func NewAnteHandler(options ethante.HandlerOptions) (sdk.AnteHandler, error) { +func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { if err := ValidateHandlerOptions(options); err != nil { return nil, err } @@ -94,28 +98,55 @@ func NewAnteHandler(options ethante.HandlerOptions) (sdk.AnteHandler, error) { // handle as totally normal Cosmos SDK tx switch tx.(type) { case sdk.Tx: - found := false - for _, msg := range tx.GetMsgs() { - switch msg.(type) { - // treat these three msg types differently because they might call EVM which results in massive gas consumption - // For these two msg types, we don't check gas limit by using a different ante handler - case *cctxtypes.MsgGasPriceVoter, *cctxtypes.MsgVoteOnObservedInboundTx: - found = true - break - case *stakingtypes.MsgCreateValidator: - if ctx.BlockHeight() == 0 { - found = true - break - } + anteHandler = newCosmosAnteHandler(options) + if len(tx.GetMsgs()) != 1 { + break + } + + msg := tx.GetMsgs()[0] // now we must have len(tx.GetMsgs()) == 1 + var innerMsg sdk.Msg + innerMsg = msg + if mm, ok := msg.(*authz.MsgExec); ok { // authz tx; look inside it + msgs, err := mm.GetMessages() + if err == nil && len(msgs) == 1 { + innerMsg = msgs[0] } } - if len(tx.GetMsgs()) == 1 && found { - // this differs newCosmosAnteHandler only in that it doesn't check gas limit - // by using an Infinite Gas Meter. - anteHandler = newCosmosAnteHandlerNoGasLimit(options) - } else { - anteHandler = newCosmosAnteHandler(options) + + isAuthorize := options.ObserverKeeper.IsAuthorized + if mm, ok := innerMsg.(*cctxtypes.MsgGasPriceVoter); ok && isAuthorize(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerNoGasFee(options) + break + } + if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedInboundTx); ok && isAuthorize(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerNoGasFee(options) + break + } + if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedOutboundTx); ok && isAuthorize(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerNoGasFee(options) + break } + if mm, ok := innerMsg.(*cctxtypes.MsgAddToOutTxTracker); ok && isAuthorize(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerNoGasFee(options) + break + } + if mm, ok := innerMsg.(*cctxtypes.MsgCreateTSSVoter); ok && isAuthorize(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerNoGasFee(options) + break + } + if mm, ok := innerMsg.(*observertypes.MsgAddBlockHeader); ok && isAuthorize(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerNoGasFee(options) + break + } + if mm, ok := innerMsg.(*observertypes.MsgAddBlameVote); ok && isAuthorize(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerNoGasFee(options) + break + } + if _, ok := innerMsg.(*stakingtypes.MsgCreateValidator); ok && ctx.BlockHeight() == 0 { + anteHandler = newCosmosAnteHandlerNoGasFee(options) + break + } + default: return ctx, errorsmod.Wrapf(errortypes.ErrUnknownRequest, "invalid transaction type: %T", tx) } diff --git a/app/ante/handler_options.go b/app/ante/handler_options.go index 5fe40847e9..c1052765b1 100644 --- a/app/ante/handler_options.go +++ b/app/ante/handler_options.go @@ -18,17 +18,39 @@ package ante import ( "fmt" + "github.com/cosmos/cosmos-sdk/types/tx/signing" + authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + evmtypes "github.com/evmos/ethermint/x/evm/types" + observerkeeper "github.com/zeta-chain/zetacore/x/observer/keeper" sdk "github.com/cosmos/cosmos-sdk/types" 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" ibcante "github.com/cosmos/ibc-go/v6/modules/core/ante" + ibckeeper "github.com/cosmos/ibc-go/v6/modules/core/keeper" ethante "github.com/evmos/ethermint/app/ante" ethermint "github.com/evmos/ethermint/types" ) -func NewLegacyCosmosAnteHandlerEip712(options ethante.HandlerOptions) sdk.AnteHandler { +type HandlerOptions struct { + AccountKeeper evmtypes.AccountKeeper + BankKeeper evmtypes.BankKeeper + IBCKeeper *ibckeeper.Keeper + FeeMarketKeeper FeeMarketKeeper + EvmKeeper EVMKeeper + FeegrantKeeper ante.FeegrantKeeper + SignModeHandler authsigning.SignModeHandler + SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params authtypes.Params) error + MaxTxGasWanted uint64 + ExtensionOptionChecker ante.ExtensionOptionChecker + TxFeeChecker ante.TxFeeChecker + DisabledAuthzMsgs []string + ObserverKeeper *observerkeeper.Keeper +} + +func NewLegacyCosmosAnteHandlerEip712(options HandlerOptions) sdk.AnteHandler { return sdk.ChainAnteDecorators( ethante.RejectMessagesDecorator{}, // reject MsgEthereumTxs NewAuthzLimiterDecorator(options.DisabledAuthzMsgs...), @@ -52,7 +74,7 @@ func NewLegacyCosmosAnteHandlerEip712(options ethante.HandlerOptions) sdk.AnteHa ) } -func newEthAnteHandler(options ethante.HandlerOptions) sdk.AnteHandler { +func newEthAnteHandler(options HandlerOptions) sdk.AnteHandler { return sdk.ChainAnteDecorators( ethante.NewEthSetUpContextDecorator(options.EvmKeeper), // outermost AnteDecorator. SetUpContext must be called first ethante.NewEthMempoolFeeDecorator(options.EvmKeeper), // Check eth effective gas price against minimal-gas-prices @@ -68,7 +90,7 @@ func newEthAnteHandler(options ethante.HandlerOptions) sdk.AnteHandler { ) } -func newCosmosAnteHandler(options ethante.HandlerOptions) sdk.AnteHandler { +func newCosmosAnteHandler(options HandlerOptions) sdk.AnteHandler { return sdk.ChainAnteDecorators( ethante.RejectMessagesDecorator{}, // reject MsgEthereumTxs NewAuthzLimiterDecorator(options.DisabledAuthzMsgs...), @@ -92,19 +114,21 @@ func newCosmosAnteHandler(options ethante.HandlerOptions) sdk.AnteHandler { } // this applies to special cosmos tx that calls EVM, in which case the EVM overrides the gas limit -func newCosmosAnteHandlerNoGasLimit(options ethante.HandlerOptions) sdk.AnteHandler { +func newCosmosAnteHandlerNoGasFee(options HandlerOptions) sdk.AnteHandler { return sdk.ChainAnteDecorators( ethante.RejectMessagesDecorator{}, // reject MsgEthereumTxs NewAuthzLimiterDecorator(options.DisabledAuthzMsgs...), NewVestingAccountDecorator(), - NewSetUpContextDecorator(), + ante.NewSetUpContextDecorator(), ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker), ante.NewValidateBasicDecorator(), ante.NewTxTimeoutHeightDecorator(), - NewMinGasPriceDecorator(options.FeeMarketKeeper, options.EvmKeeper), + // system txs are not subject to minimum gas price check + //NewMinGasPriceDecorator(options.FeeMarketKeeper, options.EvmKeeper), ante.NewValidateMemoDecorator(options.AccountKeeper), ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper), - ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker), + // the following decorators are disabled; so no fees are deducted for special system txs. + //ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker), // SetPubKeyDecorator must be called before all signature verification decorators ante.NewSetPubKeyDecorator(options.AccountKeeper), ante.NewValidateSigCountDecorator(options.AccountKeeper), diff --git a/app/app.go b/app/app.go index d78ef88643..a87abb240b 100644 --- a/app/app.go +++ b/app/app.go @@ -575,7 +575,7 @@ func New( app.SetBeginBlocker(app.BeginBlocker) maxGasWanted := cast.ToUint64(appOpts.Get(srvflags.EVMMaxTxGasWanted)) - options := evmante.HandlerOptions{ + options := ante.HandlerOptions{ AccountKeeper: app.AccountKeeper, BankKeeper: app.BankKeeper, EvmKeeper: app.EvmKeeper, @@ -589,6 +589,7 @@ func New( sdk.MsgTypeURL(&vestingtypes.MsgCreatePermanentLockedAccount{}), sdk.MsgTypeURL(&vestingtypes.MsgCreatePeriodicVestingAccount{}), }, + ObserverKeeper: app.ZetaObserverKeeper, } anteHandler, err := ante.NewAnteHandler(options) From caf620a2fd01ebdc787bf8b9e3a9499ab8644f6e Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 01:33:01 -0800 Subject: [PATCH 02/16] update changelog --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index f7049e9a7d..27bbe5a598 100644 --- a/changelog.md +++ b/changelog.md @@ -8,6 +8,7 @@ * [1535](https://github.com/zeta-chain/node/issues/1535) - Avoid voting on wrong ballots due to false blockNumber in EVM tx receipt * [1588](https://github.com/zeta-chain/node/pull/1588) - fix chain params comparison logic +* [1650](https://github.com/zeta-chain/node/pull/1605) - exempt *system txs* from min gas price check and gas fee deduction ## Version: v12.0.0 From 15e32db57b2d093394035a56f2e4dca4eaf1ecbd Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 09:55:58 -0800 Subject: [PATCH 03/16] sort imports --- app/ante/ante.go | 7 +++---- app/ante/handler_options.go | 9 +++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/ante/ante.go b/app/ante/ante.go index 83d8345b4a..f794cf7194 100644 --- a/app/ante/ante.go +++ b/app/ante/ante.go @@ -17,19 +17,18 @@ package ante import ( "fmt" - observertypes "github.com/zeta-chain/zetacore/x/observer/types" "runtime/debug" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" cctxtypes "github.com/zeta-chain/zetacore/x/crosschain/types" - - tmlog "github.com/tendermint/tendermint/libs/log" + observertypes "github.com/zeta-chain/zetacore/x/observer/types" errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" errortypes "github.com/cosmos/cosmos-sdk/types/errors" authante "github.com/cosmos/cosmos-sdk/x/auth/ante" "github.com/cosmos/cosmos-sdk/x/authz" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + tmlog "github.com/tendermint/tendermint/libs/log" ) func ValidateHandlerOptions(options HandlerOptions) error { diff --git a/app/ante/handler_options.go b/app/ante/handler_options.go index c1052765b1..15266f624f 100644 --- a/app/ante/handler_options.go +++ b/app/ante/handler_options.go @@ -18,20 +18,21 @@ package ante import ( "fmt" - "github.com/cosmos/cosmos-sdk/types/tx/signing" - authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - evmtypes "github.com/evmos/ethermint/x/evm/types" + observerkeeper "github.com/zeta-chain/zetacore/x/observer/keeper" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/cosmos-sdk/types/tx/signing" "github.com/cosmos/cosmos-sdk/x/auth/ante" "github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx" + authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ibcante "github.com/cosmos/ibc-go/v6/modules/core/ante" ibckeeper "github.com/cosmos/ibc-go/v6/modules/core/keeper" ethante "github.com/evmos/ethermint/app/ante" ethermint "github.com/evmos/ethermint/types" + evmtypes "github.com/evmos/ethermint/x/evm/types" ) type HandlerOptions struct { From 8bbf97f918b0de0234238545c22dbc2249e34816 Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 09:58:47 -0800 Subject: [PATCH 04/16] simplify conditional branches in ante.go --- app/ante/ante.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/app/ante/ante.go b/app/ante/ante.go index f794cf7194..aaedf00dd1 100644 --- a/app/ante/ante.go +++ b/app/ante/ante.go @@ -116,32 +116,25 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { if mm, ok := innerMsg.(*cctxtypes.MsgGasPriceVoter); ok && isAuthorize(ctx, mm.Creator) { anteHandler = newCosmosAnteHandlerNoGasFee(options) break - } - if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedInboundTx); ok && isAuthorize(ctx, mm.Creator) { + } else if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedInboundTx); ok && isAuthorize(ctx, mm.Creator) { anteHandler = newCosmosAnteHandlerNoGasFee(options) break - } - if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedOutboundTx); ok && isAuthorize(ctx, mm.Creator) { + } else if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedOutboundTx); ok && isAuthorize(ctx, mm.Creator) { anteHandler = newCosmosAnteHandlerNoGasFee(options) break - } - if mm, ok := innerMsg.(*cctxtypes.MsgAddToOutTxTracker); ok && isAuthorize(ctx, mm.Creator) { + } else if mm, ok := innerMsg.(*cctxtypes.MsgAddToOutTxTracker); ok && isAuthorize(ctx, mm.Creator) { anteHandler = newCosmosAnteHandlerNoGasFee(options) break - } - if mm, ok := innerMsg.(*cctxtypes.MsgCreateTSSVoter); ok && isAuthorize(ctx, mm.Creator) { + } else if mm, ok := innerMsg.(*cctxtypes.MsgCreateTSSVoter); ok && isAuthorize(ctx, mm.Creator) { anteHandler = newCosmosAnteHandlerNoGasFee(options) break - } - if mm, ok := innerMsg.(*observertypes.MsgAddBlockHeader); ok && isAuthorize(ctx, mm.Creator) { + } else if mm, ok := innerMsg.(*observertypes.MsgAddBlockHeader); ok && isAuthorize(ctx, mm.Creator) { anteHandler = newCosmosAnteHandlerNoGasFee(options) break - } - if mm, ok := innerMsg.(*observertypes.MsgAddBlameVote); ok && isAuthorize(ctx, mm.Creator) { + } else if mm, ok := innerMsg.(*observertypes.MsgAddBlameVote); ok && isAuthorize(ctx, mm.Creator) { anteHandler = newCosmosAnteHandlerNoGasFee(options) break - } - if _, ok := innerMsg.(*stakingtypes.MsgCreateValidator); ok && ctx.BlockHeight() == 0 { + } else if _, ok := innerMsg.(*stakingtypes.MsgCreateValidator); ok && ctx.BlockHeight() == 0 { anteHandler = newCosmosAnteHandlerNoGasFee(options) break } From 2ba26f31284e268f1f6c93cfde27d429a98a524b Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 10:34:35 -0800 Subject: [PATCH 05/16] add back fee deductor; allow system tx so set 1% min gas price --- app/ante/fees.go | 2 ++ app/ante/handler_options.go | 7 +++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/ante/fees.go b/app/ante/fees.go index adb0fef13d..468d4a72ee 100644 --- a/app/ante/fees.go +++ b/app/ante/fees.go @@ -99,6 +99,8 @@ func (mpd MinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate } } + minGasPrice = minGasPrice.Mul(sdk.NewDecWithPrec(1, 2)) // + evmParams := mpd.evmKeeper.GetParams(ctx) evmDenom := evmParams.GetEvmDenom() minGasPrices := sdk.DecCoins{ diff --git a/app/ante/handler_options.go b/app/ante/handler_options.go index 15266f624f..a9e0d65e74 100644 --- a/app/ante/handler_options.go +++ b/app/ante/handler_options.go @@ -124,12 +124,11 @@ func newCosmosAnteHandlerNoGasFee(options HandlerOptions) sdk.AnteHandler { ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker), ante.NewValidateBasicDecorator(), ante.NewTxTimeoutHeightDecorator(), - // system txs are not subject to minimum gas price check - //NewMinGasPriceDecorator(options.FeeMarketKeeper, options.EvmKeeper), + // system txs pay less gas fee + NewMinGasPriceDecorator(options.FeeMarketKeeper, options.EvmKeeper), ante.NewValidateMemoDecorator(options.AccountKeeper), ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper), - // the following decorators are disabled; so no fees are deducted for special system txs. - //ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker), + ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker), // SetPubKeyDecorator must be called before all signature verification decorators ante.NewSetPubKeyDecorator(options.AccountKeeper), ante.NewValidateSigCountDecorator(options.AccountKeeper), From e0af36ac5b816460dd55376b36bf5c447468698c Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 10:41:45 -0800 Subject: [PATCH 06/16] update changelog --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 27bbe5a598..e671a8b481 100644 --- a/changelog.md +++ b/changelog.md @@ -8,7 +8,7 @@ * [1535](https://github.com/zeta-chain/node/issues/1535) - Avoid voting on wrong ballots due to false blockNumber in EVM tx receipt * [1588](https://github.com/zeta-chain/node/pull/1588) - fix chain params comparison logic -* [1650](https://github.com/zeta-chain/node/pull/1605) - exempt *system txs* from min gas price check and gas fee deduction +* [1650](https://github.com/zeta-chain/node/pull/1605) - exempt (discounted) *system txs* from min gas price check and gas fee deduction ## Version: v12.0.0 From 6b4e3e34c3b454c8337889dab752829097b78e84 Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 10:51:01 -0800 Subject: [PATCH 07/16] update broadcast func to adjust gas price --- zetaclient/broadcast.go | 1 + 1 file changed, 1 insertion(+) diff --git a/zetaclient/broadcast.go b/zetaclient/broadcast.go index f015f2df85..a0cfb13e63 100644 --- a/zetaclient/broadcast.go +++ b/zetaclient/broadcast.go @@ -41,6 +41,7 @@ func (b *ZetaCoreBridge) Broadcast(gaslimit uint64, authzWrappedMsg sdktypes.Msg if baseGasPrice == 0 { baseGasPrice = DefaultBaseGasPrice // shoudn't happen, but just in case } + baseGasPrice = int64(float64(baseGasPrice) * 0.011) if blockHeight > b.blockHeight { b.blockHeight = blockHeight From dd62afebe5e3ee75794086158e9a47558f57f775 Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 18:04:49 -0800 Subject: [PATCH 08/16] refactor per review --- app/ante/ante.go | 50 ++++++++++++++++++------------------- app/ante/handler_options.go | 2 +- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/app/ante/ante.go b/app/ante/ante.go index aaedf00dd1..2de39cf723 100644 --- a/app/ante/ante.go +++ b/app/ante/ante.go @@ -97,12 +97,17 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { // handle as totally normal Cosmos SDK tx switch tx.(type) { case sdk.Tx: + // default: handle as normal Cosmos SDK tx anteHandler = newCosmosAnteHandler(options) - if len(tx.GetMsgs()) != 1 { + + // the following determines whether the tx is a system tx which will uses different handler + // System txs are always single Msg txs, optionally wrapped by one level of MsgExec + if len(tx.GetMsgs()) != 1 { // this is not a system tx break } + msg := tx.GetMsgs()[0] - msg := tx.GetMsgs()[0] // now we must have len(tx.GetMsgs()) == 1 + // if wrapped inside a MsgExec, unwrap it and reveal the innerMsg. var innerMsg sdk.Msg innerMsg = msg if mm, ok := msg.(*authz.MsgExec); ok { // authz tx; look inside it @@ -112,31 +117,24 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { } } - isAuthorize := options.ObserverKeeper.IsAuthorized - if mm, ok := innerMsg.(*cctxtypes.MsgGasPriceVoter); ok && isAuthorize(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerNoGasFee(options) - break - } else if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedInboundTx); ok && isAuthorize(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerNoGasFee(options) - break - } else if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedOutboundTx); ok && isAuthorize(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerNoGasFee(options) - break - } else if mm, ok := innerMsg.(*cctxtypes.MsgAddToOutTxTracker); ok && isAuthorize(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerNoGasFee(options) - break - } else if mm, ok := innerMsg.(*cctxtypes.MsgCreateTSSVoter); ok && isAuthorize(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerNoGasFee(options) - break - } else if mm, ok := innerMsg.(*observertypes.MsgAddBlockHeader); ok && isAuthorize(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerNoGasFee(options) - break - } else if mm, ok := innerMsg.(*observertypes.MsgAddBlameVote); ok && isAuthorize(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerNoGasFee(options) - break + // is authorized checks if the creator of the tx is in the observer set + isAuthorized := options.ObserverKeeper.IsAuthorized + if mm, ok := innerMsg.(*cctxtypes.MsgGasPriceVoter); ok && isAuthorized(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerForSystemTx(options) + } else if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedInboundTx); ok && isAuthorized(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerForSystemTx(options) + } else if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedOutboundTx); ok && isAuthorized(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerForSystemTx(options) + } else if mm, ok := innerMsg.(*cctxtypes.MsgAddToOutTxTracker); ok && isAuthorized(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerForSystemTx(options) + } else if mm, ok := innerMsg.(*cctxtypes.MsgCreateTSSVoter); ok && isAuthorized(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerForSystemTx(options) + } else if mm, ok := innerMsg.(*observertypes.MsgAddBlockHeader); ok && isAuthorized(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerForSystemTx(options) + } else if mm, ok := innerMsg.(*observertypes.MsgAddBlameVote); ok && isAuthorized(ctx, mm.Creator) { + anteHandler = newCosmosAnteHandlerForSystemTx(options) } else if _, ok := innerMsg.(*stakingtypes.MsgCreateValidator); ok && ctx.BlockHeight() == 0 { - anteHandler = newCosmosAnteHandlerNoGasFee(options) - break + anteHandler = newCosmosAnteHandlerForSystemTx(options) } default: diff --git a/app/ante/handler_options.go b/app/ante/handler_options.go index a9e0d65e74..e46f7b728e 100644 --- a/app/ante/handler_options.go +++ b/app/ante/handler_options.go @@ -115,7 +115,7 @@ func newCosmosAnteHandler(options HandlerOptions) sdk.AnteHandler { } // this applies to special cosmos tx that calls EVM, in which case the EVM overrides the gas limit -func newCosmosAnteHandlerNoGasFee(options HandlerOptions) sdk.AnteHandler { +func newCosmosAnteHandlerForSystemTx(options HandlerOptions) sdk.AnteHandler { return sdk.ChainAnteDecorators( ethante.RejectMessagesDecorator{}, // reject MsgEthereumTxs NewAuthzLimiterDecorator(options.DisabledAuthzMsgs...), From d504a2debc45ec7d60c8106c45464512732b9807 Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 18:20:29 -0800 Subject: [PATCH 09/16] factor out the reduction rate in to common variable --- app/ante/fees.go | 7 ++++++- zetaclient/broadcast.go | 7 +++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/ante/fees.go b/app/ante/fees.go index 468d4a72ee..4bccb14fda 100644 --- a/app/ante/fees.go +++ b/app/ante/fees.go @@ -29,6 +29,10 @@ import ( evmtypes "github.com/evmos/ethermint/x/evm/types" ) +var ( + GasPriceReductionRate = "0.01" // 1% of regular tx gas price for system txs +) + // MinGasPriceDecorator will check if the transaction's fee is at least as large // as the MinGasPrices param. If fee is too low, decorator returns error and tx // is rejected. This applies for both CheckTx and DeliverTx @@ -99,7 +103,8 @@ func (mpd MinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate } } - minGasPrice = minGasPrice.Mul(sdk.NewDecWithPrec(1, 2)) // + reductionRate := sdk.MustNewDecFromStr(GasPriceReductionRate) + minGasPrice = minGasPrice.Mul(reductionRate) // discounts min gas price for system tx evmParams := mpd.evmKeeper.GetParams(ctx) evmDenom := evmParams.GetEvmDenom() diff --git a/zetaclient/broadcast.go b/zetaclient/broadcast.go index a0cfb13e63..8c1a86e70d 100644 --- a/zetaclient/broadcast.go +++ b/zetaclient/broadcast.go @@ -14,6 +14,7 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" flag "github.com/spf13/pflag" rpchttp "github.com/tendermint/tendermint/rpc/client/http" + "github.com/zeta-chain/zetacore/app/ante" "github.com/zeta-chain/zetacore/cmd/zetacored/config" "github.com/zeta-chain/zetacore/common/cosmos" "github.com/zeta-chain/zetacore/zetaclient/hsm" @@ -41,7 +42,9 @@ func (b *ZetaCoreBridge) Broadcast(gaslimit uint64, authzWrappedMsg sdktypes.Msg if baseGasPrice == 0 { baseGasPrice = DefaultBaseGasPrice // shoudn't happen, but just in case } - baseGasPrice = int64(float64(baseGasPrice) * 0.011) + reductionRate := sdktypes.MustNewDecFromStr(ante.GasPriceReductionRate) + // multiply gas price by the system tx reduction rate + adjustedBaseGasPrice := sdktypes.NewDec(baseGasPrice).Mul(reductionRate) if blockHeight > b.blockHeight { b.blockHeight = blockHeight @@ -73,7 +76,7 @@ func (b *ZetaCoreBridge) Broadcast(gaslimit uint64, authzWrappedMsg sdktypes.Msg builder.SetGasLimit(gaslimit) // #nosec G701 always in range fee := sdktypes.NewCoins(sdktypes.NewCoin(config.BaseDenom, - cosmos.NewInt(int64(gaslimit)).Mul(cosmos.NewInt(baseGasPrice)))) + cosmos.NewInt(int64(gaslimit)).Mul(adjustedBaseGasPrice.Ceil().RoundInt()))) builder.SetFeeAmount(fee) //fmt.Printf("signing from name: %s\n", ctx.GetFromName()) err = b.SignTx(factory, ctx.GetFromName(), builder, true, ctx.TxConfig) From cdff7a57e7914c0aab9607c5b5844b78123d7e16 Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 18:43:44 -0800 Subject: [PATCH 10/16] factor out the system tx determinator --- app/ante/ante.go | 88 ++++++++++++++++++++++++++++-------------------- app/ante/fees.go | 4 +-- 2 files changed, 53 insertions(+), 39 deletions(-) diff --git a/app/ante/ante.go b/app/ante/ante.go index 2de39cf723..d055d29035 100644 --- a/app/ante/ante.go +++ b/app/ante/ante.go @@ -19,9 +19,6 @@ import ( "fmt" "runtime/debug" - cctxtypes "github.com/zeta-chain/zetacore/x/crosschain/types" - observertypes "github.com/zeta-chain/zetacore/x/observer/types" - errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" errortypes "github.com/cosmos/cosmos-sdk/types/errors" @@ -29,6 +26,8 @@ import ( "github.com/cosmos/cosmos-sdk/x/authz" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" tmlog "github.com/tendermint/tendermint/libs/log" + cctxtypes "github.com/zeta-chain/zetacore/x/crosschain/types" + observertypes "github.com/zeta-chain/zetacore/x/observer/types" ) func ValidateHandlerOptions(options HandlerOptions) error { @@ -100,40 +99,9 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { // default: handle as normal Cosmos SDK tx anteHandler = newCosmosAnteHandler(options) - // the following determines whether the tx is a system tx which will uses different handler - // System txs are always single Msg txs, optionally wrapped by one level of MsgExec - if len(tx.GetMsgs()) != 1 { // this is not a system tx - break - } - msg := tx.GetMsgs()[0] - - // if wrapped inside a MsgExec, unwrap it and reveal the innerMsg. - var innerMsg sdk.Msg - innerMsg = msg - if mm, ok := msg.(*authz.MsgExec); ok { // authz tx; look inside it - msgs, err := mm.GetMessages() - if err == nil && len(msgs) == 1 { - innerMsg = msgs[0] - } - } - - // is authorized checks if the creator of the tx is in the observer set - isAuthorized := options.ObserverKeeper.IsAuthorized - if mm, ok := innerMsg.(*cctxtypes.MsgGasPriceVoter); ok && isAuthorized(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerForSystemTx(options) - } else if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedInboundTx); ok && isAuthorized(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerForSystemTx(options) - } else if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedOutboundTx); ok && isAuthorized(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerForSystemTx(options) - } else if mm, ok := innerMsg.(*cctxtypes.MsgAddToOutTxTracker); ok && isAuthorized(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerForSystemTx(options) - } else if mm, ok := innerMsg.(*cctxtypes.MsgCreateTSSVoter); ok && isAuthorized(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerForSystemTx(options) - } else if mm, ok := innerMsg.(*observertypes.MsgAddBlockHeader); ok && isAuthorized(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerForSystemTx(options) - } else if mm, ok := innerMsg.(*observertypes.MsgAddBlameVote); ok && isAuthorized(ctx, mm.Creator) { - anteHandler = newCosmosAnteHandlerForSystemTx(options) - } else if _, ok := innerMsg.(*stakingtypes.MsgCreateValidator); ok && ctx.BlockHeight() == 0 { + // if tx is a system tx, and singer is authorized, use system tx handler + msgCreator := AssertSystemTxIntoCreatorTx(tx) + if msgCreator != nil && options.ObserverKeeper.IsAuthorized(ctx, msgCreator.GetCreator()) { anteHandler = newCosmosAnteHandlerForSystemTx(options) } @@ -166,3 +134,49 @@ func Recover(logger tmlog.Logger, err *error) { } } } + +type CreatorMsg interface { + GetCreator() string +} + +// if tx is a system tx, return the CreatorMsg of the type asserted innerMsg +// returns nil if tx is not a system tx +func AssertSystemTxIntoCreatorTx(tx sdk.Tx) CreatorMsg { + // the following determines whether the tx is a system tx which will uses different handler + // System txs are always single Msg txs, optionally wrapped by one level of MsgExec + if len(tx.GetMsgs()) != 1 { // this is not a system tx + return nil + } + msg := tx.GetMsgs()[0] + + // if wrapped inside a MsgExec, unwrap it and reveal the innerMsg. + var innerMsg sdk.Msg + innerMsg = msg + if mm, ok := msg.(*authz.MsgExec); ok { // authz tx; look inside it + msgs, err := mm.GetMessages() + if err == nil && len(msgs) == 1 { + innerMsg = msgs[0] + } + } + + // is authorized checks if the creator of the tx is in the observer set + if mm, ok := innerMsg.(*cctxtypes.MsgGasPriceVoter); ok { + return mm + } else if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedInboundTx); ok { + return mm + } else if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedOutboundTx); ok { + return mm + } else if mm, ok := innerMsg.(*cctxtypes.MsgAddToOutTxTracker); ok { + return mm + } else if mm, ok := innerMsg.(*cctxtypes.MsgCreateTSSVoter); ok { + return mm + } else if mm, ok := innerMsg.(*observertypes.MsgAddBlockHeader); ok { + return mm + } else if mm, ok := innerMsg.(*observertypes.MsgAddBlameVote); ok { + return mm + } else if _, ok := innerMsg.(*stakingtypes.MsgCreateValidator); ok { + return mm + } + + return nil +} diff --git a/app/ante/fees.go b/app/ante/fees.go index 4bccb14fda..912ed8e0e2 100644 --- a/app/ante/fees.go +++ b/app/ante/fees.go @@ -96,9 +96,9 @@ func (mpd MinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate return next(ctx, tx, simulate) } - // Short-circuit genesis txs gentx + // Short-circuit genesis txs gentx at block 0 (there is no way to pay fee in genesis file) if len(tx.GetMsgs()) == 1 { - if _, ok := tx.GetMsgs()[0].(*stakingtypes.MsgCreateValidator); ok { + if _, ok := tx.GetMsgs()[0].(*stakingtypes.MsgCreateValidator); ok && ctx.BlockHeight() == 0 { return next(ctx, tx, simulate) } } From f7d8005c30071c97deadf0f8377203688bc5b203 Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 18:47:15 -0800 Subject: [PATCH 11/16] separate out MsgCreateValidator --- app/ante/ante.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/ante/ante.go b/app/ante/ante.go index d055d29035..99bf012cf3 100644 --- a/app/ante/ante.go +++ b/app/ante/ante.go @@ -105,6 +105,14 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { anteHandler = newCosmosAnteHandlerForSystemTx(options) } + // if tx is MsgCreatorValidator, use the newCosmosAnteHandlerForSystemTx handler to + // exempt gas fee requirement in genesis because it's not possible to pay gas fee in genesis + if len(tx.GetMsgs()) == 1 { + if _, ok := tx.GetMsgs()[0].(*stakingtypes.MsgCreateValidator); ok && ctx.BlockHeight() == 0 { + anteHandler = newCosmosAnteHandlerForSystemTx(options) + } + } + default: return ctx, errorsmod.Wrapf(errortypes.ErrUnknownRequest, "invalid transaction type: %T", tx) } @@ -174,8 +182,6 @@ func AssertSystemTxIntoCreatorTx(tx sdk.Tx) CreatorMsg { return mm } else if mm, ok := innerMsg.(*observertypes.MsgAddBlameVote); ok { return mm - } else if _, ok := innerMsg.(*stakingtypes.MsgCreateValidator); ok { - return mm } return nil From 2012859c5d48f342bb704edb563c8aa9c046135e Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 19:02:52 -0800 Subject: [PATCH 12/16] refactor per review comments --- app/ante/ante.go | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/app/ante/ante.go b/app/ante/ante.go index 99bf012cf3..a9dc931102 100644 --- a/app/ante/ante.go +++ b/app/ante/ante.go @@ -100,8 +100,11 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) { anteHandler = newCosmosAnteHandler(options) // if tx is a system tx, and singer is authorized, use system tx handler - msgCreator := AssertSystemTxIntoCreatorTx(tx) - if msgCreator != nil && options.ObserverKeeper.IsAuthorized(ctx, msgCreator.GetCreator()) { + + isAuthorized := func(creator string) bool { + return options.ObserverKeeper.IsAuthorized(ctx, creator) + } + if IsSystemTx(tx, isAuthorized) { anteHandler = newCosmosAnteHandlerForSystemTx(options) } @@ -147,13 +150,15 @@ type CreatorMsg interface { GetCreator() string } -// if tx is a system tx, return the CreatorMsg of the type asserted innerMsg -// returns nil if tx is not a system tx -func AssertSystemTxIntoCreatorTx(tx sdk.Tx) CreatorMsg { +// IsSystemTx determines whether tx is a system tx that's signed by an authorized signer +// system tx are special types of txs (see in the switch below), or such txs wrapped inside a MsgExec +// the parameter isAuthorizedSigner is a caller specified function that determines whether the signer of +// the tx is authorized. +func IsSystemTx(tx sdk.Tx, isAuthorizedSigner func(string) bool) bool { // the following determines whether the tx is a system tx which will uses different handler // System txs are always single Msg txs, optionally wrapped by one level of MsgExec if len(tx.GetMsgs()) != 1 { // this is not a system tx - return nil + return false } msg := tx.GetMsgs()[0] @@ -166,23 +171,19 @@ func AssertSystemTxIntoCreatorTx(tx sdk.Tx) CreatorMsg { innerMsg = msgs[0] } } - - // is authorized checks if the creator of the tx is in the observer set - if mm, ok := innerMsg.(*cctxtypes.MsgGasPriceVoter); ok { - return mm - } else if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedInboundTx); ok { - return mm - } else if mm, ok := innerMsg.(*cctxtypes.MsgVoteOnObservedOutboundTx); ok { - return mm - } else if mm, ok := innerMsg.(*cctxtypes.MsgAddToOutTxTracker); ok { - return mm - } else if mm, ok := innerMsg.(*cctxtypes.MsgCreateTSSVoter); ok { - return mm - } else if mm, ok := innerMsg.(*observertypes.MsgAddBlockHeader); ok { - return mm - } else if mm, ok := innerMsg.(*observertypes.MsgAddBlameVote); ok { - return mm + switch innerMsg.(type) { + case *cctxtypes.MsgGasPriceVoter, + *cctxtypes.MsgVoteOnObservedInboundTx, + *cctxtypes.MsgVoteOnObservedOutboundTx, + *cctxtypes.MsgAddToOutTxTracker, + *cctxtypes.MsgCreateTSSVoter, + *observertypes.MsgAddBlockHeader, + *observertypes.MsgAddBlameVote: + signers := innerMsg.GetSigners() + if len(signers) == 1 { + return isAuthorizedSigner(signers[0].String()) + } } - return nil + return false } From ca2db521980639c2a8f19bc888d12b86cea4a4e6 Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 19:09:22 -0800 Subject: [PATCH 13/16] clean up --- app/ante/ante.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/ante/ante.go b/app/ante/ante.go index a9dc931102..e1077339b4 100644 --- a/app/ante/ante.go +++ b/app/ante/ante.go @@ -146,10 +146,6 @@ func Recover(logger tmlog.Logger, err *error) { } } -type CreatorMsg interface { - GetCreator() string -} - // IsSystemTx determines whether tx is a system tx that's signed by an authorized signer // system tx are special types of txs (see in the switch below), or such txs wrapped inside a MsgExec // the parameter isAuthorizedSigner is a caller specified function that determines whether the signer of From e032572cc841aa40416a0f9c4a0a7ac7b56d7bfc Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 20:51:44 -0800 Subject: [PATCH 14/16] add unit tests --- app/ante/ante_test.go | 170 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 169 insertions(+), 1 deletion(-) diff --git a/app/ante/ante_test.go b/app/ante/ante_test.go index 53a7d95c85..79120a4908 100644 --- a/app/ante/ante_test.go +++ b/app/ante/ante_test.go @@ -1,6 +1,19 @@ package ante_test -import sdk "github.com/cosmos/cosmos-sdk/types" +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/authz" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/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" + crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types" + observertypes "github.com/zeta-chain/zetacore/x/observer/types" +) var _ sdk.AnteHandler = (&MockAnteHandler{}).AnteHandle @@ -16,3 +29,158 @@ func (mah *MockAnteHandler) AnteHandle(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.C mah.CalledCtx = ctx return ctx, nil } + +func TestIsSystemTx(t *testing.T) { + // system tx types: + // *cctxtypes.MsgGasPriceVoter, + // *cctxtypes.MsgVoteOnObservedInboundTx, + // *cctxtypes.MsgVoteOnObservedOutboundTx, + // *cctxtypes.MsgAddToOutTxTracker, + // *cctxtypes.MsgCreateTSSVoter, + // *observertypes.MsgAddBlockHeader, + // *observertypes.MsgAddBlameVote: + buildTxFromMsg := func(msg sdk.Msg) sdk.Tx { + txBuilder := app.MakeEncodingConfig().TxConfig.NewTxBuilder() + txBuilder.SetMsgs(msg) + return txBuilder.GetTx() + } + buildAuthzTxFromMsg := func(msg sdk.Msg) sdk.Tx { + txBuilder := app.MakeEncodingConfig().TxConfig.NewTxBuilder() + msgExec := authz.NewMsgExec(sample.Bech32AccAddress(), []sdk.Msg{msg}) + txBuilder.SetMsgs(&msgExec) + return txBuilder.GetTx() + } + isAuthorized := func(_ string) bool { + return true + } + + tests := []struct { + name string + tx sdk.Tx + wantIs bool + }{ + { + "MsgCreateTSSVoter", + buildTxFromMsg(&crosschaintypes.MsgCreateTSSVoter{ + Creator: sample.AccAddress(), + TssPubkey: "pubkey1234", + }), + true, + }, + { + "MsgExec{MsgCreateTSSVoter}", + buildAuthzTxFromMsg(&crosschaintypes.MsgCreateTSSVoter{ + Creator: sample.AccAddress(), + TssPubkey: "pubkey1234", + }), + true, + }, + { + "MsgSend", + buildTxFromMsg(&banktypes.MsgSend{}), + false, + }, + { + "MsgExec{MsgSend}", + buildAuthzTxFromMsg(&banktypes.MsgSend{}), + false, + }, + { + "MsgCreateValidator", + buildTxFromMsg(&stakingtypes.MsgCreateValidator{}), + false, + }, + + { + "MsgVoteOnObservedInboundTx", + buildTxFromMsg(&crosschaintypes.MsgVoteOnObservedInboundTx{ + Creator: sample.AccAddress(), + }), + true, + }, + { + "MsgExec{MsgVoteOnObservedInboundTx}", + buildAuthzTxFromMsg(&crosschaintypes.MsgVoteOnObservedInboundTx{ + Creator: sample.AccAddress(), + }), + true, + }, + + { + "MsgVoteOnObservedOutboundTx", + buildTxFromMsg(&crosschaintypes.MsgVoteOnObservedOutboundTx{ + Creator: sample.AccAddress(), + }), + true, + }, + { + "MsgExec{MsgVoteOnObservedOutboundTx}", + buildAuthzTxFromMsg(&crosschaintypes.MsgVoteOnObservedOutboundTx{ + Creator: sample.AccAddress(), + }), + true, + }, + { + "MsgAddToOutTxTracker", + buildTxFromMsg(&crosschaintypes.MsgAddToOutTxTracker{ + Creator: sample.AccAddress(), + }), + true, + }, + { + "MsgExec{MsgAddToOutTxTracker}", + buildAuthzTxFromMsg(&crosschaintypes.MsgAddToOutTxTracker{ + Creator: sample.AccAddress(), + }), + true, + }, + { + "MsgCreateTSSVoter", + buildTxFromMsg(&crosschaintypes.MsgCreateTSSVoter{ + Creator: sample.AccAddress(), + }), + true, + }, + { + "MsgExec{MsgCreateTSSVoter}", + buildAuthzTxFromMsg(&crosschaintypes.MsgCreateTSSVoter{ + Creator: sample.AccAddress(), + }), + true, + }, + { + "MsgAddBlockHeader", + buildTxFromMsg(&observertypes.MsgAddBlockHeader{ + Creator: sample.AccAddress(), + }), + true, + }, + { + "MsgExec{MsgAddBlockHeader}", + buildAuthzTxFromMsg(&observertypes.MsgAddBlockHeader{ + Creator: sample.AccAddress(), + }), + true, + }, + { + "MsgAddBlameVote", + buildTxFromMsg(&observertypes.MsgAddBlameVote{ + Creator: sample.AccAddress(), + }), + true, + }, + { + "MsgExec{MsgAddBlameVote}", + buildAuthzTxFromMsg(&observertypes.MsgAddBlameVote{ + Creator: sample.AccAddress(), + }), + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + is := ante.IsSystemTx(tt.tx, isAuthorized) + require.Equal(t, tt.wantIs, is) + }) + } +} From 233ced49ed95c170f5563296e6f4edca1a7b97a6 Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Sat, 20 Jan 2024 21:55:18 -0800 Subject: [PATCH 15/16] add one unit test --- app/ante/ante_test.go | 54 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/app/ante/ante_test.go b/app/ante/ante_test.go index 79120a4908..146e50c6ae 100644 --- a/app/ante/ante_test.go +++ b/app/ante/ante_test.go @@ -53,11 +53,15 @@ func TestIsSystemTx(t *testing.T) { isAuthorized := func(_ string) bool { return true } + isAuthorizedFalse := func(_ string) bool { + return false + } tests := []struct { - name string - tx sdk.Tx - wantIs bool + name string + tx sdk.Tx + isAuthorized func(string) bool + wantIs bool }{ { "MsgCreateTSSVoter", @@ -65,6 +69,16 @@ func TestIsSystemTx(t *testing.T) { Creator: sample.AccAddress(), TssPubkey: "pubkey1234", }), + isAuthorizedFalse, + false, + }, + { + "MsgCreateTSSVoter", + buildTxFromMsg(&crosschaintypes.MsgCreateTSSVoter{ + Creator: sample.AccAddress(), + TssPubkey: "pubkey1234", + }), + isAuthorized, true, }, { @@ -73,21 +87,29 @@ func TestIsSystemTx(t *testing.T) { Creator: sample.AccAddress(), TssPubkey: "pubkey1234", }), + isAuthorized, + true, }, { "MsgSend", buildTxFromMsg(&banktypes.MsgSend{}), + isAuthorized, + false, }, { "MsgExec{MsgSend}", buildAuthzTxFromMsg(&banktypes.MsgSend{}), + isAuthorized, + false, }, { "MsgCreateValidator", buildTxFromMsg(&stakingtypes.MsgCreateValidator{}), + isAuthorized, + false, }, @@ -96,6 +118,8 @@ func TestIsSystemTx(t *testing.T) { buildTxFromMsg(&crosschaintypes.MsgVoteOnObservedInboundTx{ Creator: sample.AccAddress(), }), + isAuthorized, + true, }, { @@ -103,6 +127,8 @@ func TestIsSystemTx(t *testing.T) { buildAuthzTxFromMsg(&crosschaintypes.MsgVoteOnObservedInboundTx{ Creator: sample.AccAddress(), }), + isAuthorized, + true, }, @@ -111,6 +137,8 @@ func TestIsSystemTx(t *testing.T) { buildTxFromMsg(&crosschaintypes.MsgVoteOnObservedOutboundTx{ Creator: sample.AccAddress(), }), + isAuthorized, + true, }, { @@ -118,6 +146,8 @@ func TestIsSystemTx(t *testing.T) { buildAuthzTxFromMsg(&crosschaintypes.MsgVoteOnObservedOutboundTx{ Creator: sample.AccAddress(), }), + isAuthorized, + true, }, { @@ -125,6 +155,8 @@ func TestIsSystemTx(t *testing.T) { buildTxFromMsg(&crosschaintypes.MsgAddToOutTxTracker{ Creator: sample.AccAddress(), }), + isAuthorized, + true, }, { @@ -132,6 +164,8 @@ func TestIsSystemTx(t *testing.T) { buildAuthzTxFromMsg(&crosschaintypes.MsgAddToOutTxTracker{ Creator: sample.AccAddress(), }), + isAuthorized, + true, }, { @@ -139,6 +173,8 @@ func TestIsSystemTx(t *testing.T) { buildTxFromMsg(&crosschaintypes.MsgCreateTSSVoter{ Creator: sample.AccAddress(), }), + isAuthorized, + true, }, { @@ -146,6 +182,8 @@ func TestIsSystemTx(t *testing.T) { buildAuthzTxFromMsg(&crosschaintypes.MsgCreateTSSVoter{ Creator: sample.AccAddress(), }), + isAuthorized, + true, }, { @@ -153,6 +191,8 @@ func TestIsSystemTx(t *testing.T) { buildTxFromMsg(&observertypes.MsgAddBlockHeader{ Creator: sample.AccAddress(), }), + isAuthorized, + true, }, { @@ -160,6 +200,8 @@ func TestIsSystemTx(t *testing.T) { buildAuthzTxFromMsg(&observertypes.MsgAddBlockHeader{ Creator: sample.AccAddress(), }), + isAuthorized, + true, }, { @@ -167,6 +209,8 @@ func TestIsSystemTx(t *testing.T) { buildTxFromMsg(&observertypes.MsgAddBlameVote{ Creator: sample.AccAddress(), }), + isAuthorized, + true, }, { @@ -174,12 +218,14 @@ func TestIsSystemTx(t *testing.T) { buildAuthzTxFromMsg(&observertypes.MsgAddBlameVote{ Creator: sample.AccAddress(), }), + isAuthorized, + true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - is := ante.IsSystemTx(tt.tx, isAuthorized) + is := ante.IsSystemTx(tt.tx, tt.isAuthorized) require.Equal(t, tt.wantIs, is) }) } From 3702dc38dd8b6598acdeb4e4ad22bb85dcb4f571 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Sun, 21 Jan 2024 13:49:54 -0500 Subject: [PATCH 16/16] fix unit tests by using higher gas limits for inbound and outbound voters --- .../client/integrationtests/cli_helpers.go | 16 ++++++++++++---- .../integrationtests/inbound_voter_test.go | 7 +++++-- .../client/cli/tx_deploy_fungible_coin_zrc_4.go | 2 -- .../client/cli/tx_update_zrc20_liquidity_cap.go | 3 --- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/x/crosschain/client/integrationtests/cli_helpers.go b/x/crosschain/client/integrationtests/cli_helpers.go index 626706a627..3195782031 100644 --- a/x/crosschain/client/integrationtests/cli_helpers.go +++ b/x/crosschain/client/integrationtests/cli_helpers.go @@ -165,7 +165,9 @@ func BuildSignedGasPriceVote(t testing.TB, val *network.Validator, denom string, fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(denom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagGas, "400000"), + fmt.Sprintf("--%s=%s", flags.FlagGasAdjustment, "1.5"), + fmt.Sprintf("--%s=%s", flags.FlagGasPrices, fmt.Sprintf("%s%s", "10", denom)), } args := append(inboundVoterArgs, txArgs...) out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args) @@ -188,7 +190,9 @@ func BuildSignedTssVote(t testing.TB, val *network.Validator, denom string, acco fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(denom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagGas, "400000"), + fmt.Sprintf("--%s=%s", flags.FlagGasAdjustment, "1.5"), + fmt.Sprintf("--%s=%s", flags.FlagGasPrices, fmt.Sprintf("%s%s", "10", denom)), } args := append(inboundVoterArgs, txArgs...) out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args) @@ -229,7 +233,9 @@ func BuildSignedOutboundVote( fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(denom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagGas, "400000"), + fmt.Sprintf("--%s=%s", flags.FlagGasAdjustment, "1.5"), + fmt.Sprintf("--%s=%s", flags.FlagGasPrices, fmt.Sprintf("%s%s", "10", denom)), } args := append(outboundVoterArgs, txArgs...) out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args) @@ -262,7 +268,9 @@ func BuildSignedInboundVote(t testing.TB, val *network.Validator, denom string, fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(denom, sdk.NewInt(10))).String()), + fmt.Sprintf("--%s=%s", flags.FlagGas, "4000000"), + fmt.Sprintf("--%s=%s", flags.FlagGasAdjustment, "1.5"), + fmt.Sprintf("--%s=%s", flags.FlagGasPrices, fmt.Sprintf("%s%s", "10", denom)), } args := append(inboundVoterArgs, txArgs...) out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args) diff --git a/x/crosschain/client/integrationtests/inbound_voter_test.go b/x/crosschain/client/integrationtests/inbound_voter_test.go index 424a7e68eb..b838250745 100644 --- a/x/crosschain/client/integrationtests/inbound_voter_test.go +++ b/x/crosschain/client/integrationtests/inbound_voter_test.go @@ -241,10 +241,13 @@ func (s *IntegrationTestSuite) TestCCTXInboundVoter() { s.Require().NoError(s.network.WaitForNBlocks(2)) out, err := clitestutil.ExecTestCLICmd(broadcaster.ClientCtx, observercli.CmdListPendingNonces(), []string{"--output", "json"}) s.Require().NoError(err) - fmt.Println(out.String()) + //fmt.Println(out.String()) out, err = clitestutil.ExecTestCLICmd(broadcaster.ClientCtx, observercli.CmdGetSupportedChains(), []string{"--output", "json"}) s.Require().NoError(err) - fmt.Println(out.String()) + //fmt.Println(out.String()) + out, err = clitestutil.ExecTestCLICmd(broadcaster.ClientCtx, crosschaincli.CmdListGasPrice(), []string{"--output", "json"}) + s.Require().NoError(err) + //fmt.Println(out.String()) // Vote the inbound tx for _, val := range s.network.Validators { diff --git a/x/fungible/client/cli/tx_deploy_fungible_coin_zrc_4.go b/x/fungible/client/cli/tx_deploy_fungible_coin_zrc_4.go index adc638e69f..2be8037801 100644 --- a/x/fungible/client/cli/tx_deploy_fungible_coin_zrc_4.go +++ b/x/fungible/client/cli/tx_deploy_fungible_coin_zrc_4.go @@ -1,7 +1,6 @@ package cli import ( - "fmt" "strconv" "github.com/zeta-chain/zetacore/common" @@ -43,7 +42,6 @@ func CmdDeployFungibleCoinZRC4() *cobra.Command { if err != nil { return err } - fmt.Printf("CLI address: %s\n", clientCtx.GetFromAddress().String()) msg := types.NewMsgDeployFungibleCoinZRC20( clientCtx.GetFromAddress().String(), argERC20, diff --git a/x/fungible/client/cli/tx_update_zrc20_liquidity_cap.go b/x/fungible/client/cli/tx_update_zrc20_liquidity_cap.go index 4944e12d81..4c84c38527 100644 --- a/x/fungible/client/cli/tx_update_zrc20_liquidity_cap.go +++ b/x/fungible/client/cli/tx_update_zrc20_liquidity_cap.go @@ -1,8 +1,6 @@ package cli import ( - "fmt" - "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" @@ -23,7 +21,6 @@ func CmdUpdateZRC20LiquidityCap() *cobra.Command { if err != nil { return err } - fmt.Printf("CLI address: %s\n", clientCtx.GetFromAddress().String()) msg := types.NewMsgUpdateZRC20LiquidityCap( clientCtx.GetFromAddress().String(), args[0],