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

create consumer-democracy application #214

Merged
merged 4 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ install: go.sum
export GOFLAGS='-buildmode=pie'
export CGO_CPPFLAGS="-D_FORTIFY_SOURCE=2"
export CGO_LDFLAGS="-Wl,-z,relro,-z,now -fstack-protector"
go install $(BUILD_FLAGS) ./cmd/interchain-security-pd
go install $(BUILD_FLAGS) ./cmd/interchain-security-cd
# go install $(BUILD_FLAGS) ./cmd/interchain-security-pd
# go install $(BUILD_FLAGS) ./cmd/interchain-security-cd
go install $(BUILD_FLAGS) ./cmd/interchain-security-cdd

test:
go test ./...
Expand Down
58 changes: 58 additions & 0 deletions app/consumer-democracy/ante/msg_filter_ante.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ante

import (
"fmt"
"strings"

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

var validMsgsCCVDisabled = map[string]struct{}{}

type (
// ConsumerKeeper defines the interface required by a consumer module keeper.
ConsumerKeeper interface {
GetProviderChannel(ctx sdk.Context) (string, bool)
}

// MsgFilterDecorator defines an AnteHandler decorator that enables message
// filtering based on certain criteria.
MsgFilterDecorator struct {
ConsumerKeeper ConsumerKeeper
}
)

func NewMsgFilterDecorator(k ConsumerKeeper) MsgFilterDecorator {
return MsgFilterDecorator{
ConsumerKeeper: k,
}
}

func (mfd MsgFilterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
currHeight := ctx.BlockHeight()

// If the CCV channel has not yet been established, then we must only allow certain
// message types.
if _, ok := mfd.ConsumerKeeper.GetProviderChannel(ctx); !ok {
if !hasValidMsgsPreCCV(tx.GetMsgs()) {
return ctx, fmt.Errorf("tx contains unsupported message types at height %d", currHeight)
}
}

return next(ctx, tx, simulate)
}

func hasValidMsgsPreCCV(msgs []sdk.Msg) bool {
for _, msg := range msgs {
msgType := sdk.MsgTypeURL(msg)

// Only accept IBC messages prior to the CCV channel being established.
// Note, rather than listing out all possible IBC message types, we assume
// all IBC message types have a correct and canonical prefix -- /ibc.*
if !strings.HasPrefix(msgType, "/ibc.") {
return false
}
}

return true
}
85 changes: 85 additions & 0 deletions app/consumer-democracy/ante/msg_filter_ante_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package ante_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
ibcclienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
appconsumer "github.com/cosmos/interchain-security/app/consumer"
"github.com/cosmos/interchain-security/app/consumer/ante"
"github.com/stretchr/testify/require"
"github.com/tendermint/spm/cosmoscmd"
)

type consumerKeeper struct {
channelExists bool
}

func (k consumerKeeper) GetProviderChannel(_ sdk.Context) (string, bool) {
return "", k.channelExists
}

func noOpAnteDecorator() sdk.AnteHandler {
return func(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) {
return ctx, nil
}
}

func TestMsgFilterDecorator(t *testing.T) {
txCfg := cosmoscmd.MakeEncodingConfig(appconsumer.ModuleBasics).TxConfig

testCases := []struct {
name string
ctx sdk.Context
consumerKeeper ante.ConsumerKeeper
msgs []sdk.Msg
expectErr bool
}{
{
name: "valid tx pre-CCV",
ctx: sdk.Context{},
consumerKeeper: consumerKeeper{channelExists: false},
msgs: []sdk.Msg{
&ibcclienttypes.MsgUpdateClient{},
},
expectErr: false,
},
{
name: "invalid tx pre-CCV",
ctx: sdk.Context{},
consumerKeeper: consumerKeeper{channelExists: false},
msgs: []sdk.Msg{
&banktypes.MsgSend{},
},
expectErr: true,
},
{
name: "valid tx post-CCV",
ctx: sdk.Context{},
consumerKeeper: consumerKeeper{channelExists: true},
msgs: []sdk.Msg{
&banktypes.MsgSend{},
},
expectErr: false,
},
}

for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
handler := ante.NewMsgFilterDecorator(tc.consumerKeeper)

txBuilder := txCfg.NewTxBuilder()
require.NoError(t, txBuilder.SetMsgs(tc.msgs...))

_, err := handler.AnteHandle(tc.ctx, txBuilder.GetTx(), false, noOpAnteDecorator())
if tc.expectErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
58 changes: 58 additions & 0 deletions app/consumer-democracy/ante_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package app

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
ibcante "github.com/cosmos/ibc-go/v3/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v3/modules/core/keeper"
consumerante "github.com/cosmos/interchain-security/app/consumer/ante"
ibcconsumerkeeper "github.com/cosmos/interchain-security/x/ccv/consumer/keeper"
)

// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
// channel keeper.
type HandlerOptions struct {
ante.HandlerOptions

IBCKeeper *ibckeeper.Keeper
ConsumerKeeper ibcconsumerkeeper.Keeper
}

func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
if options.AccountKeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "account keeper is required for AnteHandler")
}
if options.BankKeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for AnteHandler")
}
if options.SignModeHandler == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}

var sigGasConsumer = options.SigGasConsumer
if sigGasConsumer == nil {
sigGasConsumer = ante.DefaultSigVerificationGasConsumer
}

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(),
ante.NewRejectExtensionOptionsDecorator(),
consumerante.NewMsgFilterDecorator(options.ConsumerKeeper),
ante.NewMempoolFeeDecorator(),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper),
// SetPubKeyDecorator must be called before all signature verification decorators
ante.NewSetPubKeyDecorator(options.AccountKeeper),
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, sigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewAnteDecorator(options.IBCKeeper),
}

return sdk.ChainAnteDecorators(anteDecorators...), nil
}
Loading