Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove MsgResponses from PostHandler #14522

2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
// Note: If the postHandler fails, we also revert the runMsgs state.
if app.postHandler != nil {
// Follow-up Ref: https://github.com/cosmos/cosmos-sdk/pull/13941
newCtx, err := app.postHandler(runMsgCtx, tx, result.MsgResponses, mode == runTxModeSimulate, err == nil)
newCtx, err := app.postHandler(runMsgCtx, tx, mode == runTxModeSimulate, err == nil)
if err != nil {
return gInfo, nil, anteEvents, priority, err
}
Expand Down
11 changes: 5 additions & 6 deletions testutil/mock/types_handler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions types/handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package types

import codectypes "github.com/cosmos/cosmos-sdk/codec/types"

// Handler defines the core of the state transition function of an application.
type Handler func(ctx Context, msg Msg) (*Result, error)

Expand All @@ -11,7 +9,7 @@ type AnteHandler func(ctx Context, tx Tx, simulate bool) (newCtx Context, err er

// PostHandler like AnteHandler but it executes after RunMsgs. Runs on success
// or failure and enables use cases like gas refunding.
type PostHandler func(ctx Context, tx Tx, msgResponses []*codectypes.Any, simulate, success bool) (newCtx Context, err error)
type PostHandler func(ctx Context, tx Tx, simulate, success bool) (newCtx Context, err error)

// AnteDecorator wraps the next AnteHandler to perform custom pre-processing.
type AnteDecorator interface {
Expand All @@ -20,10 +18,10 @@ type AnteDecorator interface {

// PostDecorator wraps the next PostHandler to perform custom post-processing.
type PostDecorator interface {
PostHandle(ctx Context, tx Tx, msgResponses []*codectypes.Any, simulate, success bool, next PostHandler) (newCtx Context, err error)
PostHandle(ctx Context, tx Tx, simulate, success bool, next PostHandler) (newCtx Context, err error)
}

// ChainDecorator chains AnteDecorators together with each AnteDecorator
// ChainAnteDecorators ChainDecorator chains AnteDecorators together with each AnteDecorator
// wrapping over the decorators further along chain and returns a single AnteHandler.
//
// NOTE: The first element is outermost decorator, while the last element is innermost
Expand Down Expand Up @@ -70,8 +68,8 @@ func ChainPostDecorators(chain ...PostDecorator) PostHandler {
chain = append(chain, Terminator{})
}

return func(ctx Context, tx Tx, msgResponses []*codectypes.Any, simulate, success bool) (Context, error) {
return chain[0].PostHandle(ctx, tx, msgResponses, simulate, success, ChainPostDecorators(chain[1:]...))
return func(ctx Context, tx Tx, simulate, success bool) (Context, error) {
return chain[0].PostHandle(ctx, tx, simulate, success, ChainPostDecorators(chain[1:]...))
}
}

Expand Down Expand Up @@ -100,7 +98,7 @@ func (t Terminator) AnteHandle(ctx Context, _ Tx, _ bool, _ AnteHandler) (Contex
return ctx, nil
}

// PostHandler returns the provided Context and nil error
func (t Terminator) PostHandle(ctx Context, _ Tx, _ []*codectypes.Any, _, _ bool, _ PostHandler) (Context, error) {
// PostHandle returns the provided Context and nil error
func (t Terminator) PostHandle(ctx Context, _ Tx, _, _ bool, _ PostHandler) (Context, error) {
return ctx, nil
}
21 changes: 10 additions & 11 deletions types/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,33 @@ func TestChainAnteDecorators(t *testing.T) {
require.NoError(t, err)
}

func (s *handlerTestSuite) TestChainPostDecorators() {
func TestChainPostDecorators(t *testing.T) {
// test panic when passing an empty sclice of PostDecorators
s.Require().Nil(sdk.ChainPostDecorators([]sdk.PostDecorator{}...))
require.Nil(t, sdk.ChainPostDecorators([]sdk.PostDecorator{}...))

// Create empty context as well as transaction
ctx := sdk.Context{}
tx := sdk.Tx(nil)
res := &sdk.Result{}

// Create mocks
mockCtrl := gomock.NewController(s.T())
mockCtrl := gomock.NewController(t)
mockPostDecorator1 := mock.NewMockPostDecorator(mockCtrl)
mockPostDecorator2 := mock.NewMockPostDecorator(mockCtrl)

// Test chaining only one post decorator
mockPostDecorator1.EXPECT().PostHandle(gomock.Eq(ctx), gomock.Eq(tx), gomock.Eq(res.MsgResponses), true, gomock.Eq(true), gomock.Any()).Times(1)
_, err := sdk.ChainPostDecorators(mockPostDecorator1)(ctx, tx, res.MsgResponses, true, true)
s.Require().NoError(err)
mockPostDecorator1.EXPECT().PostHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Eq(true), gomock.Any()).Times(1)
_, err := sdk.ChainPostDecorators(mockPostDecorator1)(ctx, tx, true, true)
require.NoError(t, err)

// Tests chaining multiple post decorators
mockPostDecorator1.EXPECT().PostHandle(gomock.Eq(ctx), gomock.Eq(tx), gomock.Eq(res.MsgResponses), true, gomock.Eq(true), gomock.Any()).Times(1)
mockPostDecorator2.EXPECT().PostHandle(gomock.Eq(ctx), gomock.Eq(tx), gomock.Eq(res.MsgResponses), true, gomock.Eq(true), gomock.Any()).Times(1)
mockPostDecorator1.EXPECT().PostHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Eq(true), gomock.Any()).Times(1)
mockPostDecorator2.EXPECT().PostHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Eq(true), gomock.Any()).Times(1)
// NOTE: we can't check that mockAnteDecorator2 is passed as the last argument because
// ChainAnteDecorators wraps the decorators into closures, so each decorator is
// receiving a closure.
_, err = sdk.ChainPostDecorators(
mockPostDecorator1,
mockPostDecorator2,
)(ctx, tx, res.MsgResponses, true, true)
s.Require().NoError(err)
)(ctx, tx, true, true)
require.NoError(t, err)
}