Merge PR #3656: Broken-Invar Tx - aka. Crisis module
* beginning thinking on issue * ... * working * working * working fee pool distribution * spek outline * spec update * gas refund calculations * simulation saved to ~/.gaiad/simulations/ * lean simulation output int * cleanup bank simulation messages * operation messges int * lint * move simulation to its own module * move simulation log code to log.go * logger overhaul int * distribution comments * fix compiling * cleanup modifications to x/distribution/keeper/allocation.go int int int * gov bug * result.IsOK() minimization * importExport typo bug * pending * address @alexanderbez comments * simple @cwgoes comments addressed * event logging unified approach * distr module name constant * implementing * compiles * gaia integration * proper constant fee removal * crisis genesis * go.sum update * ... * debugging * fix sum errors * missing err checks * working implementing CLI * remove query command * crisis expected keepers in other modules * crisis testing infrastructure * working * tests complete * modify handler to still panic if not enough pool coins, docs working * spec tags * docs complete * CL * assert invariants on a blockly basis gaiad functionality * gaiad CL * transaction details in runtime invariance panic * Apply suggestions from code review Co-Authored-By: rigelrozanski <rigel.rozanski@gmail.com> * sender tags * @mossid suggestions int * @cwgoes comments final * Apply suggestions from code review Co-Authored-By: rigelrozanski <rigel.rozanski@gmail.com> * bug seems fixed (#3998) * delete unused line in zero height export bug
This commit is contained in:
@@ -68,6 +68,12 @@ var (
|
||||
UnbondingQueueKey = keeper.UnbondingQueueKey
|
||||
RedelegationQueueKey = keeper.RedelegationQueueKey
|
||||
ValidatorQueueKey = keeper.ValidatorQueueKey
|
||||
RegisterInvariants = keeper.RegisterInvariants
|
||||
AllInvariants = keeper.AllInvariants
|
||||
SupplyInvariants = keeper.SupplyInvariants
|
||||
NonNegativePowerInvariant = keeper.NonNegativePowerInvariant
|
||||
PositiveDelegationInvariant = keeper.PositiveDelegationInvariant
|
||||
DelegatorSharesInvariant = keeper.DelegatorSharesInvariant
|
||||
|
||||
DefaultParamspace = keeper.DefaultParamspace
|
||||
KeyUnbondingTime = types.KeyUnbondingTime
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package simulation
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -6,15 +6,26 @@ import (
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
// register all staking invariants
|
||||
func RegisterInvariants(c types.CrisisKeeper, k Keeper, f types.FeeCollectionKeeper,
|
||||
d types.DistributionKeeper, am auth.AccountKeeper) {
|
||||
|
||||
c.RegisterRoute(types.ModuleName, "supply",
|
||||
SupplyInvariants(k, f, d, am))
|
||||
c.RegisterRoute(types.ModuleName, "nonnegative-power",
|
||||
NonNegativePowerInvariant(k))
|
||||
c.RegisterRoute(types.ModuleName, "positive-delegation",
|
||||
PositiveDelegationInvariant(k))
|
||||
c.RegisterRoute(types.ModuleName, "delegator-shares",
|
||||
DelegatorSharesInvariant(k))
|
||||
}
|
||||
|
||||
// AllInvariants runs all invariants of the staking module.
|
||||
// Currently: total supply, positive power
|
||||
func AllInvariants(k staking.Keeper,
|
||||
f staking.FeeCollectionKeeper, d staking.DistributionKeeper,
|
||||
am auth.AccountKeeper) sdk.Invariant {
|
||||
func AllInvariants(k Keeper, f types.FeeCollectionKeeper,
|
||||
d types.DistributionKeeper, am auth.AccountKeeper) sdk.Invariant {
|
||||
|
||||
return func(ctx sdk.Context) error {
|
||||
err := SupplyInvariants(k, f, d, am)(ctx)
|
||||
@@ -43,8 +54,9 @@ func AllInvariants(k staking.Keeper,
|
||||
|
||||
// SupplyInvariants checks that the total supply reflects all held not-bonded tokens, bonded tokens, and unbonding delegations
|
||||
// nolint: unparam
|
||||
func SupplyInvariants(k staking.Keeper,
|
||||
f staking.FeeCollectionKeeper, d staking.DistributionKeeper, am auth.AccountKeeper) sdk.Invariant {
|
||||
func SupplyInvariants(k Keeper, f types.FeeCollectionKeeper,
|
||||
d types.DistributionKeeper, am auth.AccountKeeper) sdk.Invariant {
|
||||
|
||||
return func(ctx sdk.Context) error {
|
||||
pool := k.GetPool(ctx)
|
||||
|
||||
@@ -54,7 +66,7 @@ func SupplyInvariants(k staking.Keeper,
|
||||
loose = loose.Add(acc.GetCoins().AmountOf(k.BondDenom(ctx)).ToDec())
|
||||
return false
|
||||
})
|
||||
k.IterateUnbondingDelegations(ctx, func(_ int64, ubd staking.UnbondingDelegation) bool {
|
||||
k.IterateUnbondingDelegations(ctx, func(_ int64, ubd types.UnbondingDelegation) bool {
|
||||
for _, entry := range ubd.Entries {
|
||||
loose = loose.Add(entry.Balance.ToDec())
|
||||
}
|
||||
@@ -98,7 +110,7 @@ func SupplyInvariants(k staking.Keeper,
|
||||
}
|
||||
|
||||
// NonNegativePowerInvariant checks that all stored validators have >= 0 power.
|
||||
func NonNegativePowerInvariant(k staking.Keeper) sdk.Invariant {
|
||||
func NonNegativePowerInvariant(k Keeper) sdk.Invariant {
|
||||
return func(ctx sdk.Context) error {
|
||||
iterator := k.ValidatorsPowerStoreIterator(ctx)
|
||||
|
||||
@@ -108,7 +120,7 @@ func NonNegativePowerInvariant(k staking.Keeper) sdk.Invariant {
|
||||
panic(fmt.Sprintf("validator record not found for address: %X\n", iterator.Value()))
|
||||
}
|
||||
|
||||
powerKey := keeper.GetValidatorsByPowerIndexKey(validator)
|
||||
powerKey := GetValidatorsByPowerIndexKey(validator)
|
||||
|
||||
if !bytes.Equal(iterator.Key(), powerKey) {
|
||||
return fmt.Errorf("power store invariance:\n\tvalidator.Power: %v"+
|
||||
@@ -126,7 +138,7 @@ func NonNegativePowerInvariant(k staking.Keeper) sdk.Invariant {
|
||||
}
|
||||
|
||||
// PositiveDelegationInvariant checks that all stored delegations have > 0 shares.
|
||||
func PositiveDelegationInvariant(k staking.Keeper) sdk.Invariant {
|
||||
func PositiveDelegationInvariant(k Keeper) sdk.Invariant {
|
||||
return func(ctx sdk.Context) error {
|
||||
delegations := k.GetAllDelegations(ctx)
|
||||
for _, delegation := range delegations {
|
||||
@@ -145,7 +157,7 @@ func PositiveDelegationInvariant(k staking.Keeper) sdk.Invariant {
|
||||
// DelegatorSharesInvariant checks whether all the delegator shares which persist
|
||||
// in the delegator object add up to the correct total delegator shares
|
||||
// amount stored in each validator
|
||||
func DelegatorSharesInvariant(k staking.Keeper) sdk.Invariant {
|
||||
func DelegatorSharesInvariant(k Keeper) sdk.Invariant {
|
||||
return func(ctx sdk.Context) error {
|
||||
validators := k.GetAllValidators(ctx)
|
||||
for _, validator := range validators {
|
||||
@@ -18,3 +18,8 @@ type BankKeeper interface {
|
||||
DelegateCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) (sdk.Tags, sdk.Error)
|
||||
UndelegateCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) (sdk.Tags, sdk.Error)
|
||||
}
|
||||
|
||||
// expected crisis keeper
|
||||
type CrisisKeeper interface {
|
||||
RegisterRoute(moduleName, route string, invar sdk.Invariant)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user