github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/crisis/internal/keeper/keeper.go (about)

     1  package keeper
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/fibonacci-chain/fbc/libs/tendermint/libs/log"
     8  
     9  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/crisis/internal/types"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/params"
    12  )
    13  
    14  // Keeper - crisis keeper
    15  type Keeper struct {
    16  	routes         []types.InvarRoute
    17  	paramSpace     params.Subspace
    18  	invCheckPeriod uint
    19  
    20  	supplyKeeper types.SupplyKeeper
    21  
    22  	feeCollectorName string // name of the FeeCollector ModuleAccount
    23  }
    24  
    25  // NewKeeper creates a new Keeper object
    26  func NewKeeper(
    27  	paramSpace params.Subspace, invCheckPeriod uint, supplyKeeper types.SupplyKeeper,
    28  	feeCollectorName string,
    29  ) Keeper {
    30  
    31  	return Keeper{
    32  		routes:           make([]types.InvarRoute, 0),
    33  		paramSpace:       paramSpace.WithKeyTable(types.ParamKeyTable()),
    34  		invCheckPeriod:   invCheckPeriod,
    35  		supplyKeeper:     supplyKeeper,
    36  		feeCollectorName: feeCollectorName,
    37  	}
    38  }
    39  
    40  // Logger returns a module-specific logger.
    41  func (k Keeper) Logger(ctx sdk.Context) log.Logger {
    42  	return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
    43  }
    44  
    45  // RegisterRoute register the routes for each of the invariants
    46  func (k *Keeper) RegisterRoute(moduleName, route string, invar sdk.Invariant) {
    47  	invarRoute := types.NewInvarRoute(moduleName, route, invar)
    48  	k.routes = append(k.routes, invarRoute)
    49  }
    50  
    51  // Routes - return the keeper's invariant routes
    52  func (k Keeper) Routes() []types.InvarRoute {
    53  	return k.routes
    54  }
    55  
    56  // Invariants returns all the registered Crisis keeper invariants.
    57  func (k Keeper) Invariants() []sdk.Invariant {
    58  	invars := make([]sdk.Invariant, len(k.routes))
    59  	for i, route := range k.routes {
    60  		invars[i] = route.Invar
    61  	}
    62  	return invars
    63  }
    64  
    65  // AssertInvariants asserts all registered invariants. If any invariant fails,
    66  // the method panics.
    67  func (k Keeper) AssertInvariants(ctx sdk.Context) {
    68  	logger := k.Logger(ctx)
    69  
    70  	start := time.Now()
    71  	invarRoutes := k.Routes()
    72  
    73  	for _, ir := range invarRoutes {
    74  		if res, stop := ir.Invar(ctx); stop {
    75  			// TODO: Include app name as part of context to allow for this to be
    76  			// variable.
    77  			panic(fmt.Errorf("invariant broken: %s\n"+
    78  				"\tCRITICAL please submit the following transaction:\n"+
    79  				"\t\t tx crisis invariant-broken %s %s", res, ir.ModuleName, ir.Route))
    80  		}
    81  	}
    82  
    83  	end := time.Now()
    84  	diff := end.Sub(start)
    85  
    86  	logger.Info("asserted all invariants", "duration", diff, "height", ctx.BlockHeight())
    87  }
    88  
    89  // InvCheckPeriod returns the invariant checks period.
    90  func (k Keeper) InvCheckPeriod() uint { return k.invCheckPeriod }
    91  
    92  // SendCoinsFromAccountToFeeCollector transfers amt to the fee collector account.
    93  func (k Keeper) SendCoinsFromAccountToFeeCollector(ctx sdk.Context, senderAddr sdk.AccAddress, amt sdk.Coins) error {
    94  	return k.supplyKeeper.SendCoinsFromAccountToModule(ctx, senderAddr, k.feeCollectorName, amt)
    95  }