-
Notifications
You must be signed in to change notification settings - Fork 44
/
alias_functions.go
172 lines (142 loc) · 5.23 KB
/
alias_functions.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
171
172
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/desmos-labs/desmos/v2/x/staging/subspaces/types"
)
// IterateSubspaces iterates through the subspaces set and performs the given function
func (k Keeper) IterateSubspaces(ctx sdk.Context, fn func(index int64, subspace types.Subspace) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.SubspaceStorePrefix)
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
var subspace types.Subspace
k.cdc.MustUnmarshal(iterator.Value(), &subspace)
stop := fn(i, subspace)
if stop {
break
}
i++
}
}
// GetAllSubspaces returns a list of all the subspaces that have been store inside the given context
func (k Keeper) GetAllSubspaces(ctx sdk.Context) []types.Subspace {
var subspaces []types.Subspace
k.IterateSubspaces(ctx, func(_ int64, subspace types.Subspace) (stop bool) {
subspaces = append(subspaces, subspace)
return false
})
return subspaces
}
// IterateSubspaceAdmins iterates over all the admins of the subspace having the given id
func (k Keeper) IterateSubspaceAdmins(ctx sdk.Context, id string, fn func(index int64, admin string) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.SubspaceAdminsPrefix(id))
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
stop := fn(i, string(iterator.Value()))
if stop {
break
}
i++
}
}
// GetAllAdmins returns the entries containing the data of all the admins of all the subspaces
func (k Keeper) GetAllAdmins(ctx sdk.Context) []types.UsersEntry {
var entries []types.UsersEntry
k.IterateSubspaces(ctx, func(_ int64, subspace types.Subspace) (stop bool) {
var admins []string
k.IterateSubspaceAdmins(ctx, subspace.ID, func(_ int64, admin string) (stop bool) {
admins = append(admins, admin)
return false
})
entries = append(entries, types.NewUsersEntry(subspace.ID, admins))
return false
})
return entries
}
// IterateSubspaceRegisteredUsers iterates over all the registered users of the subspace having the given id
func (k Keeper) IterateSubspaceRegisteredUsers(ctx sdk.Context, id string, fn func(index int64, user string) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.SubspaceRegisteredUsersPrefix(id))
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
stop := fn(i, string(iterator.Value()))
if stop {
break
}
i++
}
}
// GetAllRegisteredUsers returns the entries containing the data of all the registered users of all the subspaces
func (k Keeper) GetAllRegisteredUsers(ctx sdk.Context) []types.UsersEntry {
var entries []types.UsersEntry
k.IterateSubspaces(ctx, func(_ int64, subspace types.Subspace) (stop bool) {
var users []string
k.IterateSubspaceRegisteredUsers(ctx, subspace.ID, func(_ int64, user string) (stop bool) {
users = append(users, user)
return false
})
entries = append(entries, types.NewUsersEntry(subspace.ID, users))
return false
})
return entries
}
// IterateSubspaceBannedUsers iterates over all the banned users of the subspace having the given id
func (k Keeper) IterateSubspaceBannedUsers(ctx sdk.Context, id string, fn func(index int64, user string) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.SubspaceBannedUsersPrefix(id))
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
stop := fn(i, string(iterator.Value()))
if stop {
break
}
i++
}
}
// GetAllBannedUsers returns the entries containing the data of all the banned users of all the subspaces
func (k Keeper) GetAllBannedUsers(ctx sdk.Context) []types.UsersEntry {
var entries []types.UsersEntry
k.IterateSubspaces(ctx, func(_ int64, subspace types.Subspace) (stop bool) {
var users []string
k.IterateSubspaceBannedUsers(ctx, subspace.ID, func(_ int64, user string) (stop bool) {
users = append(users, user)
return false
})
entries = append(entries, types.NewUsersEntry(subspace.ID, users))
return false
})
return entries
}
// checkSubspaceAdmin checks if the subspace with the given id exists and
// if the address belongs to the owner of the subspace or one of its admins.
func (k Keeper) checkSubspaceAdmin(ctx sdk.Context, id, address string) error {
subspace, found := k.GetSubspace(ctx, id)
if !found {
return sdkerrors.Wrapf(types.ErrInvalidSubspaceID, "the subspace with id %s doesn't exist", id)
}
if subspace.Owner != address {
store := ctx.KVStore(k.storeKey)
if !store.Has(types.SubspaceAdminKey(subspace.ID, address)) {
return sdkerrors.Wrapf(types.ErrInvalidSubspaceAdmin, address)
}
}
return nil
}
// checkSubspaceOwner checks if the subspace with the given id exists and
// if the address belongs to the owner of the subspace.
func (k Keeper) checkSubspaceOwner(ctx sdk.Context, id, address string) error {
subspace, found := k.GetSubspace(ctx, id)
if !found {
return sdkerrors.Wrapf(types.ErrInvalidSubspaceID, "the subspace with id %s doesn't exist", id)
}
if subspace.Owner != address {
return sdkerrors.Wrapf(types.ErrInvalidSubspaceOwner, address)
}
return nil
}