-
Notifications
You must be signed in to change notification settings - Fork 5
/
external.go
297 lines (249 loc) · 7.89 KB
/
external.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package subspaces
import (
"context"
"encoding/hex"
"fmt"
"sort"
"github.com/rs/zerolog/log"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/forbole/juno/v5/node/remote"
subspacestypes "github.com/desmos-labs/desmos/v7/x/subspaces/types"
"github.com/desmos-labs/athena/v2/types"
"github.com/desmos-labs/athena/v2/utils"
)
// RefreshSubspacesData refreshes all the subspaces user data
func (m *Module) RefreshSubspacesData(height int64) error {
subspaces, err := m.QueryAllSubspaces(height)
if err != nil {
return fmt.Errorf("error while querying subspaces: %s", err)
}
err = m.db.DeleteAllSubspaces(height)
if err != nil {
return fmt.Errorf("error while deleting subspaces: %s", err)
}
for _, subspace := range subspaces {
err = m.refreshSubspaceData(height, subspace)
if err != nil {
return err
}
}
return nil
}
// RefreshSubspaceData refreshes all the data related to the subspace with the given id
func (m *Module) RefreshSubspaceData(height int64, subspaceID uint64) error {
err := m.db.DeleteSubspace(height, subspaceID)
if err != nil {
return fmt.Errorf("error while deleting subspace: %s", err)
}
subspace, err := m.QuerySubspace(height, subspaceID)
if err != nil {
return fmt.Errorf("error while querying subspace from gRPC: %s", err)
}
log.Info().Uint64("subspace", subspace.ID).Msg("refreshing subspace")
return m.refreshSubspaceData(height, subspace)
}
// refreshSubspaceData refreshes all the data related to the given subspace, storing them inside the database
func (m *Module) refreshSubspaceData(height int64, subspace types.Subspace) error {
// Save the subspace
err := m.db.SaveSubspace(subspace)
if err != nil {
return fmt.Errorf("error while saving subspace: %s", err)
}
// Update the sections
sections, err := m.queryAllSections(height, subspace.ID)
if err != nil {
return fmt.Errorf("error while querying subspace sections: %s", err)
}
for _, section := range sections {
err = m.db.SaveSection(section)
if err != nil {
return fmt.Errorf("error while saving subspace section: %s", err)
}
}
// Update the user groups
groups, err := m.queryAllUserGroups(height, subspace.ID)
if err != nil {
return fmt.Errorf("error while querying subspace user groups: %s", err)
}
for _, group := range groups {
err = m.db.SaveUserGroup(group)
if err != nil {
return fmt.Errorf("error while saving subspace user group: %s", err)
}
// Update the members
members, err := m.queryAllUserGroupMembers(height, group.SubspaceID, group.ID)
if err != nil {
return fmt.Errorf("error while querying user group members: %s", err)
}
// Save the members
for _, member := range members {
err = m.db.AddUserToGroup(member)
if err != nil {
return fmt.Errorf("error while saving user group member: %s", err)
}
}
}
// Update the user permissions
permissions, err := m.queryAllUserPermissions(height, subspace.ID)
if err != nil {
return fmt.Errorf("error while querying user permissions: %s", err)
}
for _, permission := range permissions {
err = m.db.SaveUserPermission(permission)
if err != nil {
return fmt.Errorf("error while saving user permissions: %s", err)
}
}
return nil
}
// QuerySubspace queries all the subspace data present on the node at the given height
func (m *Module) QuerySubspace(height int64, subspaceID uint64) (types.Subspace, error) {
res, err := m.client.Subspace(
remote.GetHeightRequestContext(context.Background(), height),
&subspacestypes.QuerySubspaceRequest{
SubspaceId: subspaceID,
},
)
if err != nil {
return types.Subspace{}, err
}
return types.NewSubspace(res.Subspace, height), nil
}
// QueryAllSubspaces queries all the subspaces present on the node at the given height
func (m *Module) QueryAllSubspaces(height int64) ([]types.Subspace, error) {
var subspaces []types.Subspace
var nextKey []byte
var stop = false
for !stop {
res, err := m.client.Subspaces(
remote.GetHeightRequestContext(context.Background(), height),
&subspacestypes.QuerySubspacesRequest{
Pagination: &query.PageRequest{
Key: nextKey,
},
},
)
if err != nil {
return nil, err
}
for _, subspace := range res.Subspaces {
subspaces = append(subspaces, types.NewSubspace(subspace, height))
}
nextKey = res.Pagination.NextKey
stop = nextKey == nil
}
return subspaces, nil
}
// queryAllSections queries all the sections for the given subspace present on the node at the given height
func (m *Module) queryAllSections(height int64, subspaceID uint64) ([]types.Section, error) {
var sections []types.Section
var nextKey []byte
var stop = false
for !stop {
res, err := m.client.Sections(
remote.GetHeightRequestContext(context.Background(), height),
&subspacestypes.QuerySectionsRequest{
SubspaceId: subspaceID,
Pagination: &query.PageRequest{
Key: nextKey,
},
},
)
if err != nil {
return nil, err
}
for _, section := range res.Sections {
sections = append(sections, types.NewSection(section, height))
}
nextKey = res.Pagination.NextKey
stop = nextKey == nil
}
return sections, nil
}
// queryAllUserGroups queries all the user groups for the given subspace present on the node at the given height
func (m *Module) queryAllUserGroups(height int64, subspaceID uint64) ([]types.UserGroup, error) {
var groups []types.UserGroup
var nextKey []byte
var stop = false
for !stop {
res, err := m.client.UserGroups(
remote.GetHeightRequestContext(context.Background(), height),
&subspacestypes.QueryUserGroupsRequest{
SubspaceId: subspaceID,
Pagination: &query.PageRequest{
Key: nextKey,
},
},
)
if err != nil {
return nil, err
}
for _, group := range res.Groups {
groups = append(groups, types.NewUserGroup(group, height))
}
nextKey = res.Pagination.NextKey
stop = nextKey == nil
}
return groups, nil
}
// queryAllUserGroupMembers queries all the user group members for the given group present on the node at the given height
func (m *Module) queryAllUserGroupMembers(height int64, subspaceID uint64, groupID uint32) ([]types.UserGroupMember, error) {
var members []types.UserGroupMember
var nextKey []byte
var stop = false
for !stop {
res, err := m.client.UserGroupMembers(
remote.GetHeightRequestContext(context.Background(), height),
&subspacestypes.QueryUserGroupMembersRequest{
SubspaceId: subspaceID,
GroupId: groupID,
Pagination: &query.PageRequest{
Key: nextKey,
},
},
)
if err != nil {
return nil, err
}
for _, member := range res.Members {
members = append(members, types.NewUserGroupMember(subspaceID, groupID, member, height))
}
nextKey = res.Pagination.NextKey
stop = nextKey == nil
}
return members, nil
}
// queryAllUserPermissions queries all the user permissions for the given subspace present on the node at the given height
func (m *Module) queryAllUserPermissions(height int64, subspaceID uint64) ([]types.UserPermission, error) {
// Query all the transactions
permissionsQuery := fmt.Sprintf("%s.%s='%d' AND tx.height <= %d",
subspacestypes.EventTypeSetUserPermissions,
subspacestypes.AttributeKeySubspaceID,
subspaceID,
height,
)
txs, err := utils.QueryTxs(m.node, permissionsQuery)
if err != nil {
return nil, err
}
// Sort the txs based on their ascending height
sort.Slice(txs, func(i, j int) bool {
return txs[i].Height < txs[j].Height
})
// Parse all the transactions' messages
var permissions []types.UserPermission
for _, tx := range txs {
transaction, err := m.node.Tx(hex.EncodeToString(tx.Tx.Hash()))
if err != nil {
return nil, err
}
// Handle only the MsgSetUserPermissions
for _, msg := range transaction.GetMsgs() {
if msg, ok := msg.(*subspacestypes.MsgSetUserPermissions); ok {
permission := subspacestypes.NewUserPermission(msg.SubspaceID, msg.SectionID, msg.User, msg.Permissions)
permissions = append(permissions, types.NewUserPermission(permission, height))
}
}
}
return permissions, nil
}