refactor(x/circuit): add TxValidator (backport #21441) (#21445)

Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
This commit is contained in:
mergify[bot] 2024-08-28 16:09:36 +00:00 committed by GitHub
parent 4e9f0bc7b1
commit 69878d8e6f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 13 deletions

View File

@ -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
}

View File

@ -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)
}