github.com/Finschia/finschia-sdk@v0.48.1/x/crisis/keeper/keeper.go (about)

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