Skip to content

Commit 43fa816

Browse files
refactor(x/circuit): add TxValidator (#21441)
Co-authored-by: Julien Robert <julien@rbrt.fr>
1 parent 355f4d7 commit 43fa816

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

x/circuit/ante/circuit.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"errors"
66

7+
"cosmossdk.io/core/transaction"
8+
79
sdk "github.com/cosmos/cosmos-sdk/types"
810
)
911

@@ -28,17 +30,29 @@ func NewCircuitBreakerDecorator(ck CircuitBreaker) CircuitBreakerDecorator {
2830
// - or error early if a nested authz grant is found.
2931
// The circuit AnteHandler handles this with baseapp's service router: https://github.com/cosmos/cosmos-sdk/issues/18632.
3032
func (cbd CircuitBreakerDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
33+
if err := cbd.ValidateTx(ctx, tx); err != nil {
34+
return ctx, err
35+
}
36+
37+
return next(ctx, tx, simulate)
38+
}
39+
40+
func (cbd CircuitBreakerDecorator) ValidateTx(ctx context.Context, tx transaction.Tx) error {
3141
// loop through all the messages and check if the message type is allowed
32-
for _, msg := range tx.GetMsgs() {
42+
msgs, err := tx.GetMessages()
43+
if err != nil {
44+
return err
45+
}
46+
47+
for _, msg := range msgs {
3348
isAllowed, err := cbd.circuitKeeper.IsAllowed(ctx, sdk.MsgTypeURL(msg))
3449
if err != nil {
35-
return ctx, err
50+
return err
3651
}
3752

3853
if !isAllowed {
39-
return ctx, errors.New("tx type not allowed")
54+
return errors.New("tx type not allowed")
4055
}
4156
}
42-
43-
return next(ctx, tx, simulate)
57+
return nil
4458
}

x/circuit/module.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
"google.golang.org/grpc"
1010

1111
"cosmossdk.io/core/appmodule"
12+
appmodulev2 "cosmossdk.io/core/appmodule/v2"
1213
"cosmossdk.io/core/registry"
14+
"cosmossdk.io/core/transaction"
15+
"cosmossdk.io/x/circuit/ante"
1316
"cosmossdk.io/x/circuit/keeper"
1417
"cosmossdk.io/x/circuit/types"
1518

@@ -24,9 +27,10 @@ const ConsensusVersion = 1
2427
var (
2528
_ module.HasGRPCGateway = AppModule{}
2629

27-
_ appmodule.AppModule = AppModule{}
28-
_ appmodule.HasGenesis = AppModule{}
29-
_ appmodule.HasRegisterInterfaces = AppModule{}
30+
_ appmodule.AppModule = AppModule{}
31+
_ appmodule.HasGenesis = AppModule{}
32+
_ appmodule.HasRegisterInterfaces = AppModule{}
33+
_ appmodulev2.HasTxValidator[transaction.Tx] = AppModule{}
3034
)
3135

3236
// AppModule implements an application module for the circuit module.
@@ -107,3 +111,9 @@ func (am AppModule) ExportGenesis(ctx context.Context) (json.RawMessage, error)
107111
}
108112
return am.cdc.MarshalJSON(gs)
109113
}
114+
115+
// TxValidator implements appmodule.HasTxValidator.
116+
func (am AppModule) TxValidator(ctx context.Context, tx transaction.Tx) error {
117+
validator := ante.NewCircuitBreakerDecorator(&am.keeper)
118+
return validator.ValidateTx(ctx, tx)
119+
}

0 commit comments

Comments
 (0)