Skip to content

Commit

Permalink
fix types.ChainAnteDecorators() panic (#5742) (#6669)
Browse files Browse the repository at this point in the history
ChainAnteDecorators() panics when no arguments are supplied.
This change its behaviour and the function now returns a nil
AnteHandler in case no AnteDecorator instances are supplied.

Closes: #5741
  • Loading branch information
Alessio Treglia committed Jul 10, 2020
1 parent 4bc422d commit 8ba2440
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ options `pruning-keep-recent`, `pruning-keep-every`, and `pruning-interval`. The
dictate how many recent versions are kept on disk and the offset of what versions are kept after that
respectively, and the latter defines the height interval in which versions are deleted in a batch.
**Note, there are some client-facing API breaking changes with regard to IAVL, stores, and pruning settings.**
* (types) [\#5741](https://github.com/cosmos/cosmos-sdk/issues/5741) Prevent `ChainAnteDecorators()` from panicking when empty `AnteDecorator` slice is supplied.

## [v0.38.4] - 2020-05-21

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ update-swagger-docs: statik

mocks: $(MOCKS_DIR)
mockgen -source=x/auth/types/account_retriever.go -package mocks -destination tests/mocks/account_retriever.go
mockgen -source=types/handler.go -package mocks -destination tests/mocks/types_handler.go
.PHONY: mocks

$(MOCKS_DIR):
Expand Down
13 changes: 11 additions & 2 deletions tests/mocks/account_retriever.go

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

49 changes: 49 additions & 0 deletions tests/mocks/types_handler.go

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

12 changes: 6 additions & 6 deletions types/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ type AnteDecorator interface {
// MUST set GasMeter with the FIRST AnteDecorator. Failing to do so will cause
// transactions to be processed with an infinite gasmeter and open a DOS attack vector.
// Use `ante.SetUpContextDecorator` or a custom Decorator with similar functionality.
// Returns nil when no AnteDecorator are supplied.
func ChainAnteDecorators(chain ...AnteDecorator) AnteHandler {
if (chain[len(chain)-1] != Terminator{}) {
chain = append(chain, Terminator{})
if len(chain) == 0 {
return nil
}

if len(chain) == 1 {
return func(ctx Context, tx Tx, simulate bool) (Context, error) {
return chain[0].AnteHandle(ctx, tx, simulate, nil)
}
// handle non-terminated decorators chain
if (chain[len(chain)-1] != Terminator{}) {
chain = append(chain, Terminator{})
}

return func(ctx Context, tx Tx, simulate bool) (Context, error) {
Expand Down
28 changes: 28 additions & 0 deletions types/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package types_test

import (
"testing"

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

"github.com/cosmos/cosmos-sdk/tests/mocks"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestChainAnteDecorators(t *testing.T) {
t.Parallel()
// test panic
require.Nil(t, sdk.ChainAnteDecorators([]sdk.AnteDecorator{}...))

ctx, tx := sdk.Context{}, sdk.Tx(nil)
mockCtrl := gomock.NewController(t)
mockAnteDecorator1 := mocks.NewMockAnteDecorator(mockCtrl)
mockAnteDecorator1.EXPECT().AnteHandle(gomock.Eq(ctx), gomock.Eq(tx), true, gomock.Any()).Times(1)
sdk.ChainAnteDecorators(mockAnteDecorator1)(ctx, tx, true)

mockAnteDecorator2 := mocks.NewMockAnteDecorator(mockCtrl)
mockAnteDecorator1.EXPECT().AnteHandle(gomock.Eq(ctx), gomock.Eq(tx), true, mockAnteDecorator2).Times(1)
mockAnteDecorator2.EXPECT().AnteHandle(gomock.Eq(ctx), gomock.Eq(tx), true, nil).Times(1)
sdk.ChainAnteDecorators(mockAnteDecorator1, mockAnteDecorator2)
}

0 comments on commit 8ba2440

Please sign in to comment.