-
Notifications
You must be signed in to change notification settings - Fork 44
/
invariants.go
170 lines (135 loc) · 4.76 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/desmos/v7/x/reports/types"
subspaceskeeper "github.com/desmos-labs/desmos/v7/x/subspaces/keeper"
subspacestypes "github.com/desmos-labs/desmos/v7/x/subspaces/types"
)
// RegisterInvariants registers all posts invariants
func RegisterInvariants(ir sdk.InvariantRegistry, keeper Keeper) {
ir.RegisterRoute(types.ModuleName, "valid-subspaces",
ValidSubspacesInvariant(keeper))
ir.RegisterRoute(types.ModuleName, "valid-reasons",
ValidReasonsInvariant(keeper))
ir.RegisterRoute(types.ModuleName, "valid-reports",
ValidReportsInvariant(keeper))
}
// --------------------------------------------------------------------------------------------------------------------
// ValidSubspacesInvariant checks that all the subspaces have a valid reason id and report id to them
func ValidSubspacesInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (message string, broken bool) {
var invalidSubspaces []subspacestypes.Subspace
k.sk.IterateSubspaces(ctx, func(subspace subspacestypes.Subspace) (stop bool) {
invalid := false
// Make sure the next reason id exists
if !k.HasNextReasonID(ctx, subspace.ID) {
invalid = true
}
if !k.HasNextReportID(ctx, subspace.ID) {
invalid = true
}
if invalid {
invalidSubspaces = append(invalidSubspaces, subspace)
}
return false
})
return sdk.FormatInvariant(types.ModuleName, "invalid subspaces",
fmt.Sprintf("the following subspaces are invalid:\n%s", subspaceskeeper.FormatOutputSubspaces(invalidSubspaces)),
), invalidSubspaces != nil
}
}
// --------------------------------------------------------------------------------------------------------------------
// ValidReasonsInvariant checks that all the reasons are valid
func ValidReasonsInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var invalidReasons []types.Reason
k.IterateReasons(ctx, func(reason types.Reason) (stop bool) {
invalid := false
// Make sure the subspace exists
if !k.HasSubspace(ctx, reason.SubspaceID) {
invalid = true
}
nextReasonID, err := k.GetNextReasonID(ctx, reason.SubspaceID)
if err != nil {
invalid = true
}
// Make sure the reason id is always less than the next one
if reason.ID >= nextReasonID {
invalid = true
}
// Validate the reason
err = reason.Validate()
if err != nil {
invalid = true
}
if invalid {
invalidReasons = append(invalidReasons, reason)
}
return false
})
return sdk.FormatInvariant(types.ModuleName, "invalid reasons",
fmt.Sprintf("the following reasons are invalid:\n%s", formatOutputReasons(invalidReasons)),
), invalidReasons != nil
}
}
// formatOutputReasons concatenates the given reasons information into a string
func formatOutputReasons(reasons []types.Reason) (output string) {
for _, reason := range reasons {
output += fmt.Sprintf("SuspaceID: %d, ReasonID: %d\n", reason.SubspaceID, reason.ID)
}
return output
}
// --------------------------------------------------------------------------------------------------------------------
// ValidReportsInvariant checks that all the reports are valid
func ValidReportsInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var invalidReports []types.Report
k.IterateReports(ctx, func(report types.Report) (stop bool) {
invalid := false
// Make sure the subspace exists
if !k.HasSubspace(ctx, report.SubspaceID) {
invalid = true
}
// Make sure the reason exists
for _, reasonID := range report.ReasonsIDs {
if !k.HasReason(ctx, report.SubspaceID, reasonID) {
invalid = true
}
}
nextReportID, err := k.GetNextReportID(ctx, report.SubspaceID)
if err != nil {
invalid = true
}
// Make sure the report id is always less than the next one
if report.ID >= nextReportID {
invalid = true
}
if data, ok := report.Target.GetCachedValue().(*types.PostTarget); ok {
// Make sure the reported post exists
if !k.HasPost(ctx, report.SubspaceID, data.PostID) {
invalid = true
}
}
// Validate the report
err = report.Validate()
if err != nil {
invalid = true
}
if invalid {
invalidReports = append(invalidReports, report)
}
return false
})
return sdk.FormatInvariant(types.ModuleName, "invalid reports",
fmt.Sprintf("the following reasons are invalid:\n%s", formatOutputReports(invalidReports)),
), invalidReports != nil
}
}
// formatOutputReports concatenates the given reports information into a string
func formatOutputReports(reports []types.Report) (output string) {
for _, report := range reports {
output += fmt.Sprintf("SuspaceID: %d, ReportID: %d\n", report.SubspaceID, report.ID)
}
return output
}