mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-02 17:31:39 +00:00
Regular → Executable
+17
-3
@@ -1,3 +1,5 @@
|
||||
// Package decorators provides custom ante handler decorators for transaction processing
|
||||
// in the Sonr blockchain application.
|
||||
package decorators
|
||||
|
||||
import (
|
||||
@@ -8,7 +10,8 @@ import (
|
||||
"github.com/cosmos/gogoproto/proto"
|
||||
)
|
||||
|
||||
// MsgFilterDecorator is an ante.go decorator template for filtering messages.
|
||||
// MsgFilterDecorator is an ante handler decorator that filters out specific message types
|
||||
// from transactions. It prevents certain message types from being processed by the chain.
|
||||
type MsgFilterDecorator struct {
|
||||
blockedTypes []sdk.Msg
|
||||
}
|
||||
@@ -17,7 +20,8 @@ type MsgFilterDecorator struct {
|
||||
// contains any of the blocked message types.
|
||||
//
|
||||
// Example:
|
||||
// - decorators.FilterDecorator(&banktypes.MsgSend{})
|
||||
// - 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{
|
||||
@@ -25,7 +29,14 @@ func FilterDecorator(blockedMsgTypes ...sdk.Msg) MsgFilterDecorator {
|
||||
}
|
||||
}
|
||||
|
||||
func (mfd MsgFilterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
||||
// AnteHandle implements the AnteDecorator interface. It checks if the transaction
|
||||
// contains any disallowed message types and rejects it if found.
|
||||
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)
|
||||
@@ -34,6 +45,9 @@ func (mfd MsgFilterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo
|
||||
return next(ctx, tx, simulate)
|
||||
}
|
||||
|
||||
// HasDisallowedMessage recursively checks if any of the provided messages or their
|
||||
// nested messages (in case of authz.MsgExec) match the blocked message types.
|
||||
// Returns true if a disallowed message is found.
|
||||
func (mfd MsgFilterDecorator) HasDisallowedMessage(ctx sdk.Context, msgs []sdk.Msg) bool {
|
||||
for _, msg := range msgs {
|
||||
// check nested messages in a recursive manner
|
||||
|
||||
Regular → Executable
+4
-10
@@ -10,21 +10,13 @@ import (
|
||||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
app "github.com/sonr-io/snrd/app"
|
||||
"github.com/sonr-io/snrd/app/decorators"
|
||||
"github.com/sonr-io/sonr/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) {
|
||||
@@ -48,7 +40,9 @@ func (s *AnteTestSuite) TestAnteMsgFilterLogic() {
|
||||
// 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))))},
|
||||
[]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)
|
||||
|
||||
Regular → Executable
+9
-1
@@ -5,31 +5,39 @@ import (
|
||||
protov2 "google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// Define an empty ante handle
|
||||
// EmptyAnte is a no-op ante handler used for testing purposes.
|
||||
// It simply returns the context without performing any operations.
|
||||
var (
|
||||
EmptyAnte = func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) {
|
||||
return ctx, nil
|
||||
}
|
||||
)
|
||||
|
||||
// MockTx is a mock transaction implementation used for testing decorators.
|
||||
// It implements the sdk.Tx interface with minimal functionality.
|
||||
type MockTx struct {
|
||||
msgs []sdk.Msg
|
||||
}
|
||||
|
||||
// NewMockTx creates a new mock transaction with the provided messages.
|
||||
// This is useful for testing ante handler decorators in isolation.
|
||||
func NewMockTx(msgs ...sdk.Msg) MockTx {
|
||||
return MockTx{
|
||||
msgs: msgs,
|
||||
}
|
||||
}
|
||||
|
||||
// GetMsgs returns the messages contained in the mock transaction.
|
||||
func (tx MockTx) GetMsgs() []sdk.Msg {
|
||||
return tx.msgs
|
||||
}
|
||||
|
||||
// GetMsgsV2 implements the sdk.Tx interface. Returns nil as this is a mock.
|
||||
func (tx MockTx) GetMsgsV2() ([]protov2.Message, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// ValidateBasic implements the sdk.Tx interface. Always returns nil for the mock.
|
||||
func (tx MockTx) ValidateBasic() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user