Add invariants for bond and registry modules
This commit is contained in:
parent
d5be22ae65
commit
639b4e6333
@ -10,10 +10,10 @@ import (
|
|||||||
|
|
||||||
// RegisterInvariants registers all auction invariants
|
// RegisterInvariants registers all auction invariants
|
||||||
func RegisterInvariants(ir sdk.InvariantRegistry, k *Keeper) {
|
func RegisterInvariants(ir sdk.InvariantRegistry, k *Keeper) {
|
||||||
ir.RegisterRoute(types.ModuleName, "module-accounts", ModuleAccountInvariant(k))
|
ir.RegisterRoute(types.ModuleName, "module-account", ModuleAccountInvariant(k))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AllInvariants runs all invariants of the auctions module.
|
// AllInvariants runs all invariants of the auction module.
|
||||||
func AllInvariants(k *Keeper) sdk.Invariant {
|
func AllInvariants(k *Keeper) sdk.Invariant {
|
||||||
return func(ctx sdk.Context) (string, bool) {
|
return func(ctx sdk.Context) (string, bool) {
|
||||||
return ModuleAccountInvariant(k)(ctx)
|
return ModuleAccountInvariant(k)(ctx)
|
||||||
@ -23,7 +23,6 @@ func AllInvariants(k *Keeper) sdk.Invariant {
|
|||||||
// ModuleAccountInvariant checks that the 'auction' module account balance is non-negative.
|
// ModuleAccountInvariant checks that the 'auction' module account balance is non-negative.
|
||||||
func ModuleAccountInvariant(k *Keeper) sdk.Invariant {
|
func ModuleAccountInvariant(k *Keeper) sdk.Invariant {
|
||||||
return func(ctx sdk.Context) (string, bool) {
|
return func(ctx sdk.Context) (string, bool) {
|
||||||
fmt.Println("running auction module invariant")
|
|
||||||
moduleAddress := k.accountKeeper.GetModuleAddress(types.ModuleName)
|
moduleAddress := k.accountKeeper.GetModuleAddress(types.ModuleName)
|
||||||
if k.bankKeeper.GetAllBalances(ctx, moduleAddress).IsAnyNegative() {
|
if k.bankKeeper.GetAllBalances(ctx, moduleAddress).IsAnyNegative() {
|
||||||
return sdk.FormatInvariant(
|
return sdk.FormatInvariant(
|
||||||
|
37
x/bond/keeper/invariants.go
Normal file
37
x/bond/keeper/invariants.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package keeper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
|
types "git.vdb.to/cerc-io/laconic2d/x/bond"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RegisterInvariants registers all bond invariants
|
||||||
|
func RegisterInvariants(ir sdk.InvariantRegistry, k *Keeper) {
|
||||||
|
ir.RegisterRoute(types.ModuleName, "module-account", ModuleAccountInvariant(k))
|
||||||
|
}
|
||||||
|
|
||||||
|
// AllInvariants runs all invariants of the bond module.
|
||||||
|
func AllInvariants(k *Keeper) sdk.Invariant {
|
||||||
|
return func(ctx sdk.Context) (string, bool) {
|
||||||
|
return ModuleAccountInvariant(k)(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ModuleAccountInvariant checks that the 'bond' module account balance is non-negative.
|
||||||
|
func ModuleAccountInvariant(k *Keeper) sdk.Invariant {
|
||||||
|
return func(ctx sdk.Context) (string, bool) {
|
||||||
|
moduleAddress := k.accountKeeper.GetModuleAddress(types.ModuleName)
|
||||||
|
if k.bankKeeper.GetAllBalances(ctx, moduleAddress).IsAnyNegative() {
|
||||||
|
return sdk.FormatInvariant(
|
||||||
|
types.ModuleName,
|
||||||
|
"module-account",
|
||||||
|
fmt.Sprintf("Module account '%s' has negative balance.", types.ModuleName)),
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
}
|
@ -24,6 +24,7 @@ var (
|
|||||||
_ module.HasGenesis = AppModule{}
|
_ module.HasGenesis = AppModule{}
|
||||||
_ module.HasServices = AppModule{}
|
_ module.HasServices = AppModule{}
|
||||||
_ module.HasConsensusVersion = AppModule{}
|
_ module.HasConsensusVersion = AppModule{}
|
||||||
|
_ module.HasInvariants = AppModule{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConsensusVersion defines the current module consensus version
|
// ConsensusVersion defines the current module consensus version
|
||||||
@ -116,3 +117,9 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
|
|||||||
bond.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
|
bond.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
|
||||||
bond.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))
|
bond.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// module.HasInvariants
|
||||||
|
|
||||||
|
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
|
||||||
|
keeper.RegisterInvariants(ir, am.keeper)
|
||||||
|
}
|
||||||
|
71
x/registry/keeper/invariants.go
Normal file
71
x/registry/keeper/invariants.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package keeper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
|
types "git.vdb.to/cerc-io/laconic2d/x/registry"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RegisterInvariants registers all registry invariants
|
||||||
|
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
|
||||||
|
ir.RegisterRoute(types.ModuleName, "module-account", ModuleAccountInvariant(&k))
|
||||||
|
ir.RegisterRoute(types.ModuleName, "record-bond", RecordBondInvariant(&k))
|
||||||
|
}
|
||||||
|
|
||||||
|
// AllInvariants runs all invariants of the registry module.
|
||||||
|
func AllInvariants(k *Keeper) sdk.Invariant {
|
||||||
|
return func(ctx sdk.Context) (string, bool) {
|
||||||
|
res, stop := ModuleAccountInvariant(k)(ctx)
|
||||||
|
if stop {
|
||||||
|
return res, stop
|
||||||
|
}
|
||||||
|
|
||||||
|
return RecordBondInvariant(k)(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ModuleAccountInvariant checks that the 'registry' module account balance is non-negative.
|
||||||
|
func ModuleAccountInvariant(k *Keeper) sdk.Invariant {
|
||||||
|
return func(ctx sdk.Context) (string, bool) {
|
||||||
|
moduleAddress := k.accountKeeper.GetModuleAddress(types.ModuleName)
|
||||||
|
if k.bankKeeper.GetAllBalances(ctx, moduleAddress).IsAnyNegative() {
|
||||||
|
return sdk.FormatInvariant(
|
||||||
|
types.ModuleName,
|
||||||
|
"module-account",
|
||||||
|
fmt.Sprintf("Module account '%s' has negative balance.", types.ModuleName)),
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RecordBondInvariant checks that for every record:
|
||||||
|
// if bondId is not null, associated bond exists
|
||||||
|
func RecordBondInvariant(k *Keeper) sdk.Invariant {
|
||||||
|
return func(ctx sdk.Context) (string, bool) {
|
||||||
|
err := k.Records.Walk(ctx, nil, func(key string, record types.Record) (bool, error) {
|
||||||
|
if record.BondId != "" {
|
||||||
|
bondExists, err := k.bondKeeper.HasBond(ctx, record.BondId)
|
||||||
|
if err != nil {
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bondExists {
|
||||||
|
return true, errors.New(record.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return sdk.FormatInvariant(types.ModuleName, "record-bond", fmt.Sprintf("Bond not found for record id: '%s'.", err.Error())), true
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
}
|
@ -27,6 +27,7 @@ var (
|
|||||||
_ module.HasServices = AppModule{}
|
_ module.HasServices = AppModule{}
|
||||||
_ module.HasConsensusVersion = AppModule{}
|
_ module.HasConsensusVersion = AppModule{}
|
||||||
_ appmodule.HasEndBlocker = AppModule{}
|
_ appmodule.HasEndBlocker = AppModule{}
|
||||||
|
_ module.HasInvariants = AppModule{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConsensusVersion defines the current module consensus version.
|
// ConsensusVersion defines the current module consensus version.
|
||||||
@ -127,6 +128,12 @@ func (am AppModule) EndBlock(ctx context.Context) error {
|
|||||||
return EndBlocker(ctx, am.keeper)
|
return EndBlocker(ctx, am.keeper)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// module.HasInvariants
|
||||||
|
|
||||||
|
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
|
||||||
|
keeper.RegisterInvariants(ir, am.keeper)
|
||||||
|
}
|
||||||
|
|
||||||
// Get the root tx command of this module
|
// Get the root tx command of this module
|
||||||
func (AppModule) GetTxCmd() *cobra.Command {
|
func (AppModule) GetTxCmd() *cobra.Command {
|
||||||
return cli.GetTxCmd()
|
return cli.GetTxCmd()
|
||||||
|
Loading…
Reference in New Issue
Block a user