/
invariants.go
123 lines (103 loc) · 3.08 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package keeper
import (
"fmt"
"strings"
"github.com/commercionetwork/commercionetwork/x/docs/internal/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
docsReceiptsInvName string = "docs-receipts"
docsSchemasInvName string = "docs-schemas-valid"
)
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
ir.RegisterRoute(types.ModuleName, docsReceiptsInvName,
DocsReceiptsInvariants(k))
ir.RegisterRoute(types.ModuleName, docsSchemasInvName,
DocsSchemasValidInvariant(k))
}
// DocsReceiptsInvariants checks that every receipt points to an
// existing Document.
func DocsReceiptsInvariants(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
// Keep a map of all documents we've seen already
docsLookup := make(map[string]struct{})
sentReceipts, receivedReceipts := k.ReceiptsIterators(ctx)
defer sentReceipts.Close()
defer receivedReceipts.Close()
// sent receipts
for ; sentReceipts.Valid(); sentReceipts.Next() {
receipt, _, err := k.ExtractReceipt(ctx, sentReceipts.Value())
if err != nil {
panic("could not extract sent receipt during invariant")
}
if _, found := docsLookup[receipt.DocumentUUID]; found {
continue
}
_, err = k.GetDocumentByID(ctx, receipt.DocumentUUID)
if err != nil {
return sdk.FormatInvariant(
types.ModuleName,
docsReceiptsInvName,
fmt.Sprintf(
"found sent receipt %s which refers to non-existent document %s",
receipt.UUID,
receipt.DocumentUUID,
),
), true
}
docsLookup[receipt.DocumentUUID] = struct{}{}
}
// received receipts
for ; receivedReceipts.Valid(); receivedReceipts.Next() {
receipt, _, err := k.ExtractReceipt(ctx, receivedReceipts.Value())
if err != nil {
panic("could not extract received receipt during invariant")
}
if _, found := docsLookup[receipt.DocumentUUID]; found {
continue
}
_, err = k.GetDocumentByID(ctx, receipt.DocumentUUID)
if err != nil {
return sdk.FormatInvariant(
types.ModuleName,
docsReceiptsInvName,
fmt.Sprintf(
"found received receipt %s which refers to non-existent document %s",
receipt.UUID,
receipt.DocumentUUID,
),
), true
}
docsLookup[receipt.DocumentUUID] = struct{}{}
}
return "", false
}
}
// DocsSchemasValidInvariant checks that every Document SchemaType
// is a supported one.
func DocsSchemasValidInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
di := k.DocumentsIterator(ctx)
defer di.Close()
for ; di.Valid(); di.Next() {
doc, _, err := k.ExtractDocument(ctx, di.Key())
if err != nil {
panic("could not extract document during invariant: " + err.Error())
}
if len(strings.TrimSpace(doc.Metadata.SchemaType)) != 0 {
if !k.IsMetadataSchemeTypeSupported(ctx, doc.Metadata.SchemaType) {
return sdk.FormatInvariant(
types.ModuleName,
docsSchemasInvName,
fmt.Sprintf(
"found document %s with invalid metadata schema type %s",
doc.UUID,
doc.Metadata.SchemaType,
),
), true
}
}
}
return "", false
}
}