From fa86136b11733864aeb181bd8223778425e09cd0 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Mon, 30 May 2022 18:22:00 +0200 Subject: [PATCH 1/7] refactor: Mark tips as beta --- docs/core/tips.md | 53 +++++++++++++++++++++------------ simapp/app.go | 7 +---- x/auth/client/testutil/suite.go | 3 ++ x/auth/posthandler/post.go | 18 ++--------- x/auth/posthandler/tips.go | 2 ++ 5 files changed, 43 insertions(+), 40 deletions(-) diff --git a/docs/core/tips.md b/docs/core/tips.md index a76c3564b758..6b21c97e2af9 100644 --- a/docs/core/tips.md +++ b/docs/core/tips.md @@ -4,7 +4,7 @@ order: 14 # Transaction Tips -Transaction tips are a mechanism to pay for transaction fees using another denom than the native fee denom of the chain. {synopsis} +Transaction tips are a mechanism to pay for transaction fees using another denom than the native fee denom of the chain. They are still in beta, and are not included by default in the SDK. {synopsis} ## Context @@ -72,27 +72,42 @@ In both cases, using `SIGN_MODE_LEGACY_AMINO_JSON` is recommended only if hardwa ## Enabling Tips on your Chain -The transaction tips functionality is introduced in Cosmos SDK v0.46, so earlier versions do not have support for tips. If you're using v0.46 or later, then enabling tips on your chain is as simple as adding the `TipMiddleware` in your middleware stack: +The transaction tips functionality is introduced in Cosmos SDK v0.46, so earlier versions do not have support for tips. It is however not included by default in a v0.46 app. Enabling tips on your chain is done by adding the `TipDecorator` in your posthandler chain: ```go -// NewTxHandler defines a TxHandler middleware stack. -func NewTxHandler(options TxHandlerOptions) (tx.Handler, error) { - // --snip-- - - return ComposeMiddlewares( - // base tx handler that executes Msgs - NewRunMsgsTxHandler(options.MsgServiceRouter, options.LegacyRouter), - // --snip other middlewares-- - - // Add the TipMiddleware - NewTipMiddleware(options.BankKeeper), - ) +// HandlerOptions are the options required for constructing a default SDK PostHandler. +type HandlerOptions struct { + BankKeeper types.BankKeeper } -``` -Notice that `NewTipMiddleware` needs a reference to the BankKeeper, for transferring the tip to the fee payer. +// MyPostHandler returns a posthandler chain with the TipDecorator. +func MyPostHandler(options HandlerOptions) (sdk.AnteHandler, error) { + if options.BankKeeper == nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for posthandler") + } + + postDecorators := []sdk.AnteDecorator{ + posthandler.NewTipDecorator(options.bankKeeper), + } + + return sdk.ChainAnteDecorators(postDecorators...), nil +} + +func (app *SimApp) setPostHandler() { + postHandler, err := MyPostHandler( + HandlerOptions{ + BankKeeper: app.BankKeeper, + }, + ) + if err != nil { + panic(err) + } + + app.SetPostHandler(postHandler) +} +``` -If you are using the Cosmos SDK's default middleware stack `NewDefaultTxHandler()`, then the tip middleware is included by default. +Notice that `NewTipDecorator` needs a reference to the BankKeeper, for transferring the tip to the fee payer. ## CLI Usage @@ -170,7 +185,7 @@ For the fee payer, the SDK added a new method on the existing `TxBuilder` to imp txBuilder := clientCtx.TxConfig.NewTxBuilder() err := txBuilder.AddAuxSignerData(auxSignerData) if err != nil { - return err + return err } // A lot of fields will be populated in txBuilder, such as its Msgs, tip @@ -184,6 +199,6 @@ txBuilder.SetGasLimit(...) // Usual signing code err = authclient.SignTx(...) if err != nil { - return err + return err } ``` diff --git a/simapp/app.go b/simapp/app.go index cd6f19bb584a..33e11a0fd943 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -436,12 +436,7 @@ func NewSimApp( // meaning that both `runMsgs` and `postHandler` state will be committed if // both are successful, and both will be reverted if any of the two fails. // - // The SDK exposes a default postHandlers chain, which comprises of only - // one decorator: the Transaction Tips decorator. However, some chains do - // not need it by default, so feel free to comment the next line if you do - // not need tips. - // To read more about tips: - // https://docs.cosmos.network/main/core/tips.html + // The SDK exposes a default empty postHandlers chain. // // Please note that changing any of the anteHandler or postHandler chain is // likely to be a state-machine breaking change, which needs a coordinated diff --git a/x/auth/client/testutil/suite.go b/x/auth/client/testutil/suite.go index a56885920220..86da20fb8be5 100644 --- a/x/auth/client/testutil/suite.go +++ b/x/auth/client/testutil/suite.go @@ -1562,6 +1562,9 @@ func (s *IntegrationTestSuite) TestAuxSigner() { } func (s *IntegrationTestSuite) TestAuxToFeeWithTips() { + // Skipping this test as it needs a simapp with the TipDecorator in post handler. + s.T().Skip() + require := s.Require() val := s.network.Validators[0] diff --git a/x/auth/posthandler/post.go b/x/auth/posthandler/post.go index 604b1203c4e4..8d3fb7776c63 100644 --- a/x/auth/posthandler/post.go +++ b/x/auth/posthandler/post.go @@ -2,26 +2,14 @@ package posthandler import ( sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/x/auth/types" ) // HandlerOptions are the options required for constructing a default SDK PostHandler. -type HandlerOptions struct { - BankKeeper types.BankKeeper -} +type HandlerOptions struct{} -// NewAnteHandler returns an AnteHandler that checks and increments sequence -// numbers, checks signatures & account numbers, and deducts fees from the first -// signer. +// NewPostHandler returns an empty posthandler chain. func NewPostHandler(options HandlerOptions) (sdk.AnteHandler, error) { - if options.BankKeeper == nil { - return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for posthandler") - } - - postDecorators := []sdk.AnteDecorator{ - NewTipDecorator(options.BankKeeper), - } + postDecorators := []sdk.AnteDecorator{} return sdk.ChainAnteDecorators(postDecorators...), nil } diff --git a/x/auth/posthandler/tips.go b/x/auth/posthandler/tips.go index a112a3e1c4a2..f956d53e5442 100644 --- a/x/auth/posthandler/tips.go +++ b/x/auth/posthandler/tips.go @@ -16,6 +16,8 @@ type tipDecorator struct { // NewTipDecorator returns a new decorator for handling transactions with // tips. +// +// IMPORTANT: This decorator is still in beta, please use it at your own risk. func NewTipDecorator(bankKeeper types.BankKeeper) sdk.AnteDecorator { return tipDecorator{ bankKeeper: bankKeeper, From e344d6ebe6d1537ec02c2dbb74945df3c1df95c3 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Mon, 30 May 2022 18:28:54 +0200 Subject: [PATCH 2/7] Fix build --- simapp/app.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 33e11a0fd943..46a641b30f8b 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -475,9 +475,7 @@ func (app *SimApp) setAnteHandler(txConfig client.TxConfig, indexEventsStr []str func (app *SimApp) setPostHandler() { postHandler, err := posthandler.NewPostHandler( - posthandler.HandlerOptions{ - BankKeeper: app.BankKeeper, - }, + posthandler.HandlerOptions{}, ) if err != nil { panic(err) From d956f80455a516040b5f4da01ef660c01d2c196a Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 31 May 2022 14:30:45 +0200 Subject: [PATCH 3/7] Fix events in simulation --- baseapp/baseapp.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 636fd60e61b4..8756c7a8e724 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -707,11 +707,13 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re consumeBlockGas() msCache.Write() + } - if len(anteEvents) > 0 { - // append the events in the order of occurrence - result.Events = append(anteEvents, result.Events...) - } + // append the events in the order of occurrence + // note: in CheckTx, result.Events is empty, so we would only get + // the anteEvents. + if len(anteEvents) > 0 { + result.Events = append(anteEvents, result.Events...) } } From 5738ee1a3c94f0e19141e25653cf0366bf1b9bd9 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 31 May 2022 14:39:11 +0200 Subject: [PATCH 4/7] Add cahgenlog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c9cf3f83191..52178dd1d17a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (cli) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Add the `tendermint key-migrate` to perform Tendermint v0.35 DB key migration. +### Improvements + +* [#12098](https://github.com/cosmos/cosmos-sdk/pull/12089) Mark the `TipDecorator` as beta, don't include it in simapp by default. + ### Bug Fixes * (migrations) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Fix v0.45->v0.46 in-place store migrations. From 2066a877c253fc764eca7250fc7c48abe406937c Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 31 May 2022 14:58:04 +0200 Subject: [PATCH 5/7] Fix simulation events --- baseapp/baseapp.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 8756c7a8e724..99e402c4d6d9 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -709,10 +709,8 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re msCache.Write() } - // append the events in the order of occurrence - // note: in CheckTx, result.Events is empty, so we would only get - // the anteEvents. - if len(anteEvents) > 0 { + if len(anteEvents) > 0 && mode == runTxModeDeliver || mode == runTxModeSimulate { + // append the events in the order of occurrence result.Events = append(anteEvents, result.Events...) } } From e7ac93a602e962a1d370eb7703ba78e18eee276b Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 31 May 2022 14:59:19 +0200 Subject: [PATCH 6/7] update changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52178dd1d17a..0bb227e5b8f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,11 +43,12 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements -* [#12098](https://github.com/cosmos/cosmos-sdk/pull/12089) Mark the `TipDecorator` as beta, don't include it in simapp by default. +* [#12089](https://github.com/cosmos/cosmos-sdk/pull/12089) Mark the `TipDecorator` as beta, don't include it in simapp by default. ### Bug Fixes * (migrations) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Fix v0.45->v0.46 in-place store migrations. +* (baseapp) [#12089](https://github.com/cosmos/cosmos-sdk/pull/12089) Include antehandler and runMsgs events in SimulateTx. ## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23 From dd7c933b8b2d6e70d676b7b9e578684c9430ec4c Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 31 May 2022 15:00:07 +0200 Subject: [PATCH 7/7] Clearer ifs --- baseapp/baseapp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 99e402c4d6d9..9888011c648e 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -709,7 +709,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re msCache.Write() } - if len(anteEvents) > 0 && mode == runTxModeDeliver || mode == runTxModeSimulate { + if len(anteEvents) > 0 && (mode == runTxModeDeliver || mode == runTxModeSimulate) { // append the events in the order of occurrence result.Events = append(anteEvents, result.Events...) }