github.com/cosmos/cosmos-sdk@v0.50.10/x/crisis/keeper/keeper.go (about) 1 package keeper 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 "cosmossdk.io/collections" 9 "cosmossdk.io/core/address" 10 storetypes "cosmossdk.io/core/store" 11 "cosmossdk.io/log" 12 13 "github.com/cosmos/cosmos-sdk/codec" 14 sdk "github.com/cosmos/cosmos-sdk/types" 15 "github.com/cosmos/cosmos-sdk/x/crisis/types" 16 ) 17 18 // Keeper - crisis keeper 19 type Keeper struct { 20 routes []types.InvarRoute 21 invCheckPeriod uint 22 storeService storetypes.KVStoreService 23 cdc codec.BinaryCodec 24 25 // the address capable of executing a MsgUpdateParams message. Typically, this 26 // should be the x/gov module account. 27 authority string 28 29 supplyKeeper types.SupplyKeeper 30 31 feeCollectorName string // name of the FeeCollector ModuleAccount 32 33 addressCodec address.Codec 34 35 Schema collections.Schema 36 ConstantFee collections.Item[sdk.Coin] 37 } 38 39 // NewKeeper creates a new Keeper object 40 func NewKeeper( 41 cdc codec.BinaryCodec, storeService storetypes.KVStoreService, invCheckPeriod uint, 42 supplyKeeper types.SupplyKeeper, feeCollectorName, authority string, ac address.Codec, 43 ) *Keeper { 44 sb := collections.NewSchemaBuilder(storeService) 45 k := &Keeper{ 46 storeService: storeService, 47 cdc: cdc, 48 routes: make([]types.InvarRoute, 0), 49 invCheckPeriod: invCheckPeriod, 50 supplyKeeper: supplyKeeper, 51 feeCollectorName: feeCollectorName, 52 authority: authority, 53 addressCodec: ac, 54 55 ConstantFee: collections.NewItem(sb, types.ConstantFeeKey, "constant_fee", codec.CollValue[sdk.Coin](cdc)), 56 } 57 schema, err := sb.Build() 58 if err != nil { 59 panic(err) 60 } 61 k.Schema = schema 62 return k 63 } 64 65 // GetAuthority returns the x/crisis module's authority. 66 func (k *Keeper) GetAuthority() string { 67 return k.authority 68 } 69 70 // Logger returns a module-specific logger. 71 func (k *Keeper) Logger(ctx context.Context) log.Logger { 72 sdkCtx := sdk.UnwrapSDKContext(ctx) 73 return sdkCtx.Logger().With("module", "x/"+types.ModuleName) 74 } 75 76 // RegisterRoute register the routes for each of the invariants 77 func (k *Keeper) RegisterRoute(moduleName, route string, invar sdk.Invariant) { 78 invarRoute := types.NewInvarRoute(moduleName, route, invar) 79 k.routes = append(k.routes, invarRoute) 80 } 81 82 // Routes - return the keeper's invariant routes 83 func (k *Keeper) Routes() []types.InvarRoute { 84 return k.routes 85 } 86 87 // Invariants returns a copy of all registered Crisis keeper invariants. 88 func (k *Keeper) Invariants() []sdk.Invariant { 89 invars := make([]sdk.Invariant, len(k.routes)) 90 for i, route := range k.routes { 91 invars[i] = route.Invar 92 } 93 return invars 94 } 95 96 // AssertInvariants asserts all registered invariants. If any invariant fails, 97 // the method panics. 98 func (k *Keeper) AssertInvariants(ctx sdk.Context) { 99 logger := k.Logger(ctx) 100 101 start := time.Now() 102 invarRoutes := k.Routes() 103 n := len(invarRoutes) 104 for i, ir := range invarRoutes { 105 logger.Info("asserting crisis invariants", "inv", fmt.Sprint(i+1, "/", n), "name", ir.FullRoute()) 106 107 invCtx, _ := ctx.CacheContext() 108 if res, stop := ir.Invar(invCtx); stop { 109 // TODO: Include app name as part of context to allow for this to be 110 // variable. 111 panic(fmt.Errorf("invariant broken: %s\n"+ 112 "\tCRITICAL please submit the following transaction:\n"+ 113 "\t\t tx crisis invariant-broken %s %s", res, ir.ModuleName, ir.Route)) 114 } 115 } 116 117 diff := time.Since(start) 118 logger.Info("asserted all invariants", "duration", diff, "height", ctx.BlockHeight()) 119 } 120 121 // InvCheckPeriod returns the invariant checks period. 122 func (k *Keeper) InvCheckPeriod() uint { return k.invCheckPeriod } 123 124 // SendCoinsFromAccountToFeeCollector transfers amt to the fee collector account. 125 func (k *Keeper) SendCoinsFromAccountToFeeCollector(ctx context.Context, senderAddr sdk.AccAddress, amt sdk.Coins) error { 126 return k.supplyKeeper.SendCoinsFromAccountToModule(ctx, senderAddr, k.feeCollectorName, amt) 127 }