From b6613f9163f8b379d558ab90fa0b5916d3758a75 Mon Sep 17 00:00:00 2001 From: Marko Date: Thu, 25 May 2023 17:35:48 +0200 Subject: [PATCH] chore: set circuitbreaker in app.go as example (#16288) --- baseapp/baseapp.go | 3 +++ simapp/app.go | 1 + x/circuit/module.go | 19 +++++++++++++------ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index dfaa1022e4..1890ef5103 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -491,6 +491,9 @@ func (app *BaseApp) GetFinalizeBlockStateCtx() sdk.Context { // SetCircuitBreaker sets the circuit breaker for the BaseApp. // The circuit breaker is checked on every message execution to verify if a transaction should be executed or not. func (app *BaseApp) SetCircuitBreaker(cb CircuitBreaker) { + if app.msgServiceRouter == nil { + panic("cannot set circuit breaker with no msg service router set") + } app.msgServiceRouter.SetCircuit(cb) } diff --git a/simapp/app.go b/simapp/app.go index 12a34e5094..25e122a8c8 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -302,6 +302,7 @@ func NewSimApp( ) app.CircuitKeeper = circuitkeeper.NewKeeper(keys[circuittypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AccountKeeper.AddressCodec()) + app.BaseApp.SetCircuitBreaker(&app.CircuitKeeper) app.AuthzKeeper = authzkeeper.NewKeeper(runtime.NewKVStoreService(keys[authzkeeper.StoreKey]), appCodec, app.MsgServiceRouter(), app.AccountKeeper) diff --git a/x/circuit/module.go b/x/circuit/module.go index b9518afc2e..8107197bd1 100644 --- a/x/circuit/module.go +++ b/x/circuit/module.go @@ -13,9 +13,11 @@ import ( store "cosmossdk.io/store/types" "cosmossdk.io/x/circuit/client/cli" abci "github.com/cometbft/cometbft/abci/types" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" @@ -149,7 +151,7 @@ func init() { ) } -type Inputs struct { +type ModuleInputs struct { depinject.In Config *modulev1.Module @@ -159,14 +161,15 @@ type Inputs struct { AddressCodec address.Codec } -type Outputs struct { +type ModuleOutputs struct { depinject.Out - CircuitKeeper keeper.Keeper - Module appmodule.AppModule + CircuitKeeper keeper.Keeper + Module appmodule.AppModule + BaseappOptions runtime.BaseAppOption } -func ProvideModule(in Inputs) Outputs { +func ProvideModule(in ModuleInputs) ModuleOutputs { // default to governance authority if not provided authority := authtypes.NewModuleAddress("gov") if in.Config.Authority != "" { @@ -180,5 +183,9 @@ func ProvideModule(in Inputs) Outputs { ) m := NewAppModule(in.Cdc, circuitkeeper) - return Outputs{CircuitKeeper: circuitkeeper, Module: m} + baseappOpt := func(app *baseapp.BaseApp) { + app.SetCircuitBreaker(&circuitkeeper) + } + + return ModuleOutputs{CircuitKeeper: circuitkeeper, Module: m, BaseappOptions: baseappOpt} }