mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-02 17:31:39 +00:00
(no commit message provided)
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package decorators
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/authz"
|
||||
"github.com/cosmos/gogoproto/proto"
|
||||
)
|
||||
|
||||
// MsgFilterDecorator is an ante.go decorator template for filtering messages.
|
||||
type MsgFilterDecorator struct {
|
||||
blockedTypes []sdk.Msg
|
||||
}
|
||||
|
||||
// FilterDecorator returns a new MsgFilterDecorator. This errors if the transaction
|
||||
// contains any of the blocked message types.
|
||||
//
|
||||
// Example:
|
||||
// - decorators.FilterDecorator(&banktypes.MsgSend{})
|
||||
// This would block any MsgSend messages from being included in a transaction if set in ante.go
|
||||
func FilterDecorator(blockedMsgTypes ...sdk.Msg) MsgFilterDecorator {
|
||||
return MsgFilterDecorator{
|
||||
blockedTypes: blockedMsgTypes,
|
||||
}
|
||||
}
|
||||
|
||||
func (mfd MsgFilterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
||||
if mfd.HasDisallowedMessage(ctx, tx.GetMsgs()) {
|
||||
currHeight := ctx.BlockHeight()
|
||||
return ctx, fmt.Errorf("tx contains unsupported message types at height %d", currHeight)
|
||||
}
|
||||
|
||||
return next(ctx, tx, simulate)
|
||||
}
|
||||
|
||||
func (mfd MsgFilterDecorator) HasDisallowedMessage(ctx sdk.Context, msgs []sdk.Msg) bool {
|
||||
for _, msg := range msgs {
|
||||
// check nested messages in a recursive manner
|
||||
if execMsg, ok := msg.(*authz.MsgExec); ok {
|
||||
msgs, err := execMsg.GetMessages()
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if mfd.HasDisallowedMessage(ctx, msgs) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
for _, blockedType := range mfd.blockedTypes {
|
||||
if proto.MessageName(msg) == proto.MessageName(blockedType) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package decorators_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
sdkmath "cosmossdk.io/math"
|
||||
|
||||
"github.com/cometbft/cometbft/crypto/secp256k1"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
app "github.com/onsonr/hway/app"
|
||||
"github.com/onsonr/hway/app/decorators"
|
||||
)
|
||||
|
||||
type AnteTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
ctx sdk.Context
|
||||
app *app.SonrApp
|
||||
}
|
||||
|
||||
func (s *AnteTestSuite) SetupTest() {
|
||||
isCheckTx := false
|
||||
s.app = app.Setup(s.T())
|
||||
s.ctx = s.app.BaseApp.NewContext(isCheckTx)
|
||||
}
|
||||
|
||||
func TestAnteTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(AnteTestSuite))
|
||||
}
|
||||
|
||||
// Test the change rate decorator with standard edit msgs,
|
||||
func (s *AnteTestSuite) TestAnteMsgFilterLogic() {
|
||||
acc := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
|
||||
|
||||
// test blocking any BankSend Messages
|
||||
ante := decorators.FilterDecorator(&banktypes.MsgSend{})
|
||||
msg := banktypes.NewMsgSend(
|
||||
acc,
|
||||
acc,
|
||||
sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(1))),
|
||||
)
|
||||
_, err := ante.AnteHandle(s.ctx, decorators.NewMockTx(msg), false, decorators.EmptyAnte)
|
||||
s.Require().Error(err)
|
||||
|
||||
// validate other messages go through still (such as MsgMultiSend)
|
||||
msgMultiSend := banktypes.NewMsgMultiSend(
|
||||
banktypes.NewInput(acc, sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(1)))),
|
||||
[]banktypes.Output{banktypes.NewOutput(acc, sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(1))))},
|
||||
)
|
||||
_, err = ante.AnteHandle(s.ctx, decorators.NewMockTx(msgMultiSend), false, decorators.EmptyAnte)
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package decorators
|
||||
|
||||
import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
protov2 "google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// Define an empty ante handle
|
||||
var (
|
||||
EmptyAnte = func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) {
|
||||
return ctx, nil
|
||||
}
|
||||
)
|
||||
|
||||
type MockTx struct {
|
||||
msgs []sdk.Msg
|
||||
}
|
||||
|
||||
func NewMockTx(msgs ...sdk.Msg) MockTx {
|
||||
return MockTx{
|
||||
msgs: msgs,
|
||||
}
|
||||
}
|
||||
|
||||
func (tx MockTx) GetMsgs() []sdk.Msg {
|
||||
return tx.msgs
|
||||
}
|
||||
|
||||
func (tx MockTx) GetMsgsV2() ([]protov2.Message, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (tx MockTx) ValidateBasic() error {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user