2019-03-28 16:27:47 -07:00
|
|
|
package crisis
|
|
|
|
|
|
|
|
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"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keeper - crisis keeper
|
|
|
|
type Keeper struct {
|
2019-05-16 08:25:32 -07:00
|
|
|
routes []InvarRoute
|
|
|
|
paramSpace params.Subspace
|
|
|
|
invCheckPeriod uint
|
2019-03-28 16:27:47 -07:00
|
|
|
|
|
|
|
distrKeeper DistrKeeper
|
|
|
|
bankKeeper BankKeeper
|
|
|
|
feeCollectionKeeper FeeCollectionKeeper
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeeper creates a new Keeper object
|
2019-05-16 08:25:32 -07:00
|
|
|
func NewKeeper(paramSpace params.Subspace, invCheckPeriod uint,
|
2019-03-28 16:27:47 -07:00
|
|
|
distrKeeper DistrKeeper, bankKeeper BankKeeper,
|
|
|
|
feeCollectionKeeper FeeCollectionKeeper) Keeper {
|
|
|
|
|
|
|
|
return Keeper{
|
|
|
|
routes: []InvarRoute{},
|
|
|
|
paramSpace: paramSpace.WithKeyTable(ParamKeyTable()),
|
2019-05-16 08:25:32 -07:00
|
|
|
invCheckPeriod: invCheckPeriod,
|
2019-03-28 16:27:47 -07:00
|
|
|
distrKeeper: distrKeeper,
|
|
|
|
bankKeeper: bankKeeper,
|
|
|
|
feeCollectionKeeper: feeCollectionKeeper,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// register routes for the
|
|
|
|
func (k *Keeper) RegisterRoute(moduleName, route string, invar sdk.Invariant) {
|
|
|
|
invarRoute := NewInvarRoute(moduleName, route, invar)
|
|
|
|
k.routes = append(k.routes, invarRoute)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Routes - return the keeper's invariant routes
|
|
|
|
func (k Keeper) Routes() []InvarRoute {
|
|
|
|
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
|
|
|
|
func (k Keeper) AssertInvariants(ctx sdk.Context, logger log.Logger) {
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
invarRoutes := k.Routes()
|
|
|
|
for _, ir := range invarRoutes {
|
|
|
|
if err := ir.Invar(ctx); err != nil {
|
|
|
|
|
|
|
|
// TODO make "gaiacli" app name a part of context to allow for this to be variable
|
|
|
|
panic(fmt.Errorf("invariant broken: %s\n"+
|
|
|
|
"\tCRITICAL please submit the following transaction:\n"+
|
|
|
|
"\t\t gaiacli tx crisis invariant-broken %v %v", err, ir.ModuleName, ir.Route))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end := time.Now()
|
|
|
|
diff := end.Sub(start)
|
|
|
|
|
|
|
|
logger.With("module", "x/crisis").Info(
|
|
|
|
"Asserted all invariants", "duration", diff, "height", ctx.BlockHeight())
|
|
|
|
}
|
|
|
|
|
2019-04-09 18:34:38 -07:00
|
|
|
// DONTCOVER
|