2019-07-01 09:50:04 -07:00
|
|
|
package keeper
|
2019-03-28 16:27:47 -07:00
|
|
|
|
|
|
|
import (
|
2019-05-16 08:25:32 -07:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
|
2019-03-28 16:27:47 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-07-01 09:50:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/crisis/internal/types"
|
2019-03-28 16:27:47 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keeper - crisis keeper
|
|
|
|
type Keeper struct {
|
2019-06-05 16:26:17 -07:00
|
|
|
routes []types.InvarRoute
|
2019-05-16 08:25:32 -07:00
|
|
|
paramSpace params.Subspace
|
|
|
|
invCheckPeriod uint
|
2019-03-28 16:27:47 -07:00
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
supplyKeeper types.SupplyKeeper
|
|
|
|
|
|
|
|
feeCollectorName string // name of the FeeCollector ModuleAccount
|
2019-03-28 16:27:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeeper creates a new Keeper object
|
2019-05-16 08:25:32 -07:00
|
|
|
func NewKeeper(paramSpace params.Subspace, invCheckPeriod uint,
|
2019-06-28 13:11:27 -07:00
|
|
|
supplyKeeper types.SupplyKeeper, feeCollectorName string) Keeper {
|
2019-03-28 16:27:47 -07:00
|
|
|
|
|
|
|
return Keeper{
|
2019-06-28 13:11:27 -07:00
|
|
|
routes: []types.InvarRoute{},
|
|
|
|
paramSpace: paramSpace.WithKeyTable(types.ParamKeyTable()),
|
|
|
|
invCheckPeriod: invCheckPeriod,
|
|
|
|
supplyKeeper: supplyKeeper,
|
|
|
|
feeCollectorName: feeCollectorName,
|
2019-03-28 16:27:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 09:41:14 -07:00
|
|
|
// Logger returns a module-specific logger.
|
|
|
|
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
|
|
|
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
// RegisterRoute register the routes for each of the invariants
|
2019-03-28 16:27:47 -07:00
|
|
|
func (k *Keeper) RegisterRoute(moduleName, route string, invar sdk.Invariant) {
|
2019-06-05 16:26:17 -07:00
|
|
|
invarRoute := types.NewInvarRoute(moduleName, route, invar)
|
2019-03-28 16:27:47 -07:00
|
|
|
k.routes = append(k.routes, invarRoute)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Routes - return the keeper's invariant routes
|
2019-06-05 16:26:17 -07:00
|
|
|
func (k Keeper) Routes() []types.InvarRoute {
|
2019-03-28 16:27:47 -07:00
|
|
|
return k.routes
|
|
|
|
}
|
2019-04-09 18:34:38 -07:00
|
|
|
|
|
|
|
// Invariants returns all the registered Crisis keeper invariants.
|
|
|
|
func (k Keeper) Invariants() []sdk.Invariant {
|
|
|
|
var invars []sdk.Invariant
|
|
|
|
for _, route := range k.routes {
|
|
|
|
invars = append(invars, route.Invar)
|
|
|
|
}
|
|
|
|
return invars
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
// assert all invariants
|
2019-06-26 09:41:14 -07:00
|
|
|
func (k Keeper) AssertInvariants(ctx sdk.Context) {
|
|
|
|
logger := k.Logger(ctx)
|
2019-05-16 08:25:32 -07:00
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
invarRoutes := k.Routes()
|
|
|
|
for _, ir := range invarRoutes {
|
|
|
|
if err := ir.Invar(ctx); err != nil {
|
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
// TODO: Include app name as part of context to allow for this to be
|
|
|
|
// variable.
|
2019-05-16 08:25:32 -07:00
|
|
|
panic(fmt.Errorf("invariant broken: %s\n"+
|
|
|
|
"\tCRITICAL please submit the following transaction:\n"+
|
2019-05-18 01:42:24 -07:00
|
|
|
"\t\t tx crisis invariant-broken %v %v", err, ir.ModuleName, ir.Route))
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
}
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
end := time.Now()
|
|
|
|
diff := end.Sub(start)
|
|
|
|
|
2019-06-26 09:41:14 -07:00
|
|
|
logger.Info("asserted all invariants", "duration", diff, "height", ctx.BlockHeight())
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
|
|
|
|
2019-07-01 09:50:04 -07:00
|
|
|
// InvCheckPeriod returns the invariant checks period.
|
|
|
|
func (k Keeper) InvCheckPeriod() uint { return k.invCheckPeriod }
|
|
|
|
|
|
|
|
// SendCoinsFromAccountToFeeCollector transfers amt to the fee collector account.
|
|
|
|
func (k Keeper) SendCoinsFromAccountToFeeCollector(ctx sdk.Context, senderAddr sdk.AccAddress, amt sdk.Coins) sdk.Error {
|
|
|
|
return k.supplyKeeper.SendCoinsFromAccountToModule(ctx, senderAddr, k.feeCollectorName, amt)
|
|
|
|
}
|
|
|
|
|
2019-04-09 18:34:38 -07:00
|
|
|
// DONTCOVER
|