/
invariants.go
70 lines (59 loc) · 1.94 KB
/
invariants.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// Copyright 2019 Wireline, Inc.
//
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/vulcanize/dxns/x/nameservice/internal/types"
)
// RegisterInvariants registers all nameservice module invariants.
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
ir.RegisterRoute(types.ModuleName, "record", RecordInvariants(k))
}
// ModuleAccountInvariant checks that the 'bond' module account balance is non-negative.
func ModuleAccountInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
moduleAccount := k.supplyKeeper.GetModuleAccount(ctx, types.RecordRentModuleAccountName)
if moduleAccount.GetCoins().IsAnyNegative() {
return sdk.FormatInvariant(
types.ModuleName,
"module-account",
fmt.Sprintf("Module account '%s' has negative balance.", types.RecordRentModuleAccountName)),
true
}
return "", false
}
}
// RecordInvariants checks that every record:
// (1) has a corresponding naming record &
// (2) associated bond exists, if bondID is not null.
func RecordInvariants(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
store := ctx.KVStore(k.storeKey)
itr := sdk.KVStorePrefixIterator(store, PrefixCIDToRecordIndex)
defer itr.Close()
for ; itr.Valid(); itr.Next() {
bz := store.Get(itr.Key())
if bz != nil {
var obj types.RecordObj
k.cdc.MustUnmarshalBinaryBare(bz, &obj)
record := obj.ToRecord()
if record.BondID != "" && !k.bondKeeper.HasBond(ctx, record.BondID) {
return sdk.FormatInvariant(types.ModuleName, "record-bond", fmt.Sprintf("Bond not found for record ID: '%s'.", record.ID)), true
}
}
}
return "", false
}
}
// AllInvariants runs all invariants of the nameservice module.
func AllInvariants(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
res, stop := RecordInvariants(k)(ctx)
if stop {
return res, stop
}
return ModuleAccountInvariant(k)(ctx)
}
}