diff --git a/x/circuit/ante/circuit.go b/x/circuit/ante/circuit.go index 9d3ea0a49f..67ce0021df 100644 --- a/x/circuit/ante/circuit.go +++ b/x/circuit/ante/circuit.go @@ -4,6 +4,8 @@ import ( "context" "errors" + "cosmossdk.io/core/transaction" + sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -28,17 +30,29 @@ func NewCircuitBreakerDecorator(ck CircuitBreaker) CircuitBreakerDecorator { // - or error early if a nested authz grant is found. // The circuit AnteHandler handles this with baseapp's service router: https://github.com/cosmos/cosmos-sdk/issues/18632. func (cbd CircuitBreakerDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { - // loop through all the messages and check if the message type is allowed - for _, msg := range tx.GetMsgs() { - isAllowed, err := cbd.circuitKeeper.IsAllowed(ctx, sdk.MsgTypeURL(msg)) - if err != nil { - return ctx, err - } - - if !isAllowed { - return ctx, errors.New("tx type not allowed") - } + if err := cbd.ValidateTx(ctx, tx); err != nil { + return ctx, err } return next(ctx, tx, simulate) } + +func (cbd CircuitBreakerDecorator) ValidateTx(ctx context.Context, tx transaction.Tx) error { + // loop through all the messages and check if the message type is allowed + msgs, err := tx.GetMessages() + if err != nil { + return err + } + + for _, msg := range msgs { + isAllowed, err := cbd.circuitKeeper.IsAllowed(ctx, sdk.MsgTypeURL(msg)) + if err != nil { + return err + } + + if !isAllowed { + return errors.New("tx type not allowed") + } + } + return nil +} diff --git a/x/circuit/module.go b/x/circuit/module.go index 3f2d74fa3d..91645aaee6 100644 --- a/x/circuit/module.go +++ b/x/circuit/module.go @@ -9,7 +9,10 @@ import ( "google.golang.org/grpc" "cosmossdk.io/core/appmodule" + appmodulev2 "cosmossdk.io/core/appmodule/v2" "cosmossdk.io/core/registry" + "cosmossdk.io/core/transaction" + "cosmossdk.io/x/circuit/ante" "cosmossdk.io/x/circuit/keeper" "cosmossdk.io/x/circuit/types" @@ -24,9 +27,10 @@ const ConsensusVersion = 1 var ( _ module.HasGRPCGateway = AppModule{} - _ appmodule.AppModule = AppModule{} - _ appmodule.HasGenesis = AppModule{} - _ appmodule.HasRegisterInterfaces = AppModule{} + _ appmodule.AppModule = AppModule{} + _ appmodule.HasGenesis = AppModule{} + _ appmodule.HasRegisterInterfaces = AppModule{} + _ appmodulev2.HasTxValidator[transaction.Tx] = AppModule{} ) // AppModule implements an application module for the circuit module. @@ -107,3 +111,9 @@ func (am AppModule) ExportGenesis(ctx context.Context) (json.RawMessage, error) } return am.cdc.MarshalJSON(gs) } + +// TxValidator implements appmodule.HasTxValidator. +func (am AppModule) TxValidator(ctx context.Context, tx transaction.Tx) error { + validator := ante.NewCircuitBreakerDecorator(&am.keeper) + return validator.ValidateTx(ctx, tx) +}