Skip to content

Commit

Permalink
feat: access to msg events result on postHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Nov 6, 2023
1 parent 5635117 commit b833516
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ func (app *BaseApp) runTx(mode execMode, txBytes []byte) (gInfo sdk.GasInfo, res
// Note that the state is still preserved.
postCtx := runMsgCtx.WithEventManager(sdk.NewEventManager())

newCtx, err := app.postHandler(postCtx, tx, mode == execModeSimulate, err == nil)
newCtx, err := app.postHandler(postCtx, tx, result.Events, mode == execModeSimulate, err == nil)
if err != nil {
return gInfo, nil, anteEvents, err
}
Expand Down
11 changes: 6 additions & 5 deletions testutil/mock/types_handler.go

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

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

import abci "github.com/cometbft/cometbft/abci/types"

// AnteHandler authenticates transactions, before their internal messages are handled.
// If newCtx.IsZero(), ctx is used instead.
type AnteHandler func(ctx Context, tx Tx, simulate bool) (newCtx Context, err error)

// 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, simulate, success bool) (newCtx Context, err error)
type PostHandler func(ctx Context, tx Tx, txEvents []abci.Event, simulate, success bool) (newCtx Context, err error)

// AnteDecorator wraps the next AnteHandler to perform custom pre-processing.
type AnteDecorator interface {
Expand All @@ -15,7 +17,7 @@ type AnteDecorator interface {

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

// ChainAnteDecorators ChainDecorator chains AnteDecorators together with each AnteDecorator
Expand Down Expand Up @@ -67,13 +69,13 @@ func ChainPostDecorators(chain ...PostDecorator) PostHandler {

handlerChain := make([]PostHandler, len(chain)+1)
// set the terminal PostHandler decorator
handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate, success bool) (Context, error) {
handlerChain[len(chain)] = func(ctx Context, tx Tx, txEvents []abci.Event, simulate, success bool) (Context, error) {
return ctx, nil
}
for i := 0; i < len(chain); i++ {
ii := i
handlerChain[ii] = func(ctx Context, tx Tx, simulate, success bool) (Context, error) {
return chain[ii].PostHandle(ctx, tx, simulate, success, handlerChain[ii+1])
handlerChain[ii] = func(ctx Context, tx Tx, txEvents []abci.Event, simulate, success bool) (Context, error) {
return chain[ii].PostHandle(ctx, tx, txEvents, simulate, success, handlerChain[ii+1])
}
}
return handlerChain[0]
Expand Down
19 changes: 9 additions & 10 deletions types/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package types_test
import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/testutil/mock"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)

func TestChainAnteDecorators(t *testing.T) {
Expand Down Expand Up @@ -40,27 +39,27 @@ func TestChainPostDecorators(t *testing.T) {

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

// Create mocks
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), true, gomock.Eq(true), gomock.Any()).Times(1)
_, err := sdk.ChainPostDecorators(mockPostDecorator1)(ctx, tx, true, true)
mockPostDecorator1.EXPECT().PostHandle(gomock.Eq(ctx), gomock.Eq(tx), gomock.Any(), true, gomock.Eq(true), gomock.Any()).Times(1)
_, err := sdk.ChainPostDecorators(mockPostDecorator1)(ctx, tx, nil, true, true)
require.NoError(t, err)

// Tests chaining multiple post decorators
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)
mockPostDecorator1.EXPECT().PostHandle(gomock.Eq(ctx), gomock.Eq(tx), gomock.Any(), true, gomock.Eq(true), gomock.Any()).Times(1)
mockPostDecorator2.EXPECT().PostHandle(gomock.Eq(ctx), gomock.Eq(tx), gomock.Any(), 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
// ChainAnteDecorators wraps the decorators into closures, so each decorator is<
// receiving a closure.
_, err = sdk.ChainPostDecorators(
mockPostDecorator1,
mockPostDecorator2,
)(ctx, tx, true, true)
)(ctx, tx, nil, true, true)
require.NoError(t, err)
}

0 comments on commit b833516

Please sign in to comment.