Skip to content

Commit

Permalink
Merge branch 'main' into unsafe.String
Browse files Browse the repository at this point in the history
  • Loading branch information
cuiweixie authored Aug 28, 2024
2 parents 7d1df8a + 43fa816 commit 1ce5b1d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
24 changes: 19 additions & 5 deletions x/circuit/ante/circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"

"cosmossdk.io/core/transaction"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -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) {
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
for _, msg := range tx.GetMsgs() {
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 ctx, err
return err
}

if !isAllowed {
return ctx, errors.New("tx type not allowed")
return errors.New("tx type not allowed")
}
}

return next(ctx, tx, simulate)
return nil
}
16 changes: 13 additions & 3 deletions x/circuit/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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.
Expand Down Expand Up @@ -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)
}

0 comments on commit 1ce5b1d

Please sign in to comment.