-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for issue #115: Disable messages on consumer chain
- Loading branch information
1 parent
f3a9966
commit 41279aa
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ante | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
type DisabledModulesDecorator struct { | ||
prefixes []string | ||
} | ||
|
||
func NewDisabledModulesDecorator(disabledModules ...string) DisabledModulesDecorator { | ||
return DisabledModulesDecorator{prefixes: disabledModules} | ||
} | ||
|
||
func (dmd DisabledModulesDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { | ||
currHeight := ctx.BlockHeight() | ||
for _, msg := range tx.GetMsgs() { | ||
msgTypeURL := sdk.MsgTypeURL(msg) | ||
|
||
for _, prefix := range dmd.prefixes { | ||
if strings.HasPrefix(msgTypeURL, prefix) { | ||
return ctx, fmt.Errorf("tx contains message types from unsupported modules at height %d", currHeight) | ||
} | ||
} | ||
} | ||
|
||
return next(ctx, tx, simulate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package ante_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types" | ||
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/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" | ||
) | ||
|
||
func TestDisabledModulesDecorator(t *testing.T) { | ||
txCfg := cosmoscmd.MakeEncodingConfig(appconsumer.ModuleBasics).TxConfig | ||
|
||
testCases := []struct { | ||
name string | ||
ctx sdk.Context | ||
msgs []sdk.Msg | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "IBC module messages supported", | ||
ctx: sdk.Context{}, | ||
msgs: []sdk.Msg{ | ||
&ibcclienttypes.MsgUpdateClient{}, | ||
}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "bank module messages supported", | ||
ctx: sdk.Context{}, | ||
msgs: []sdk.Msg{ | ||
&banktypes.MsgSend{}, | ||
}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "evidence module messages not supported", | ||
ctx: sdk.Context{}, | ||
msgs: []sdk.Msg{ | ||
&evidencetypes.MsgSubmitEvidence{}, | ||
}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "slashing module messages not supported", | ||
ctx: sdk.Context{}, | ||
msgs: []sdk.Msg{ | ||
&slashingtypes.MsgUnjail{}, | ||
}, | ||
expectErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
handler := ante.NewDisabledModulesDecorator("/cosmos.evidence", "/cosmos.slashing") | ||
|
||
txBuilder := txCfg.NewTxBuilder() | ||
require.NoError(t, txBuilder.SetMsgs(tc.msgs...)) | ||
|
||
_, err := handler.AnteHandle(tc.ctx, txBuilder.GetTx(), false, | ||
func(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) { return ctx, nil }) | ||
if tc.expectErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters