-
Notifications
You must be signed in to change notification settings - Fork 44
/
alias_functions.go
259 lines (215 loc) · 8.95 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
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
package keeper
import (
"bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/desmos-labs/desmos/v7/x/profiles/types"
)
// IterateProfiles iterates through the Profiles set and performs the provided function
func (k Keeper) IterateProfiles(ctx sdk.Context, fn func(profile *types.Profile) (stop bool)) {
k.ak.IterateAccounts(ctx, func(account authtypes.AccountI) (stop bool) {
profile, ok := account.(*types.Profile)
if ok {
stop = fn(profile)
}
return stop
})
}
// GetProfiles returns all the profiles that are stored inside the given context
func (k Keeper) GetProfiles(ctx sdk.Context) []*types.Profile {
var profiles []*types.Profile
k.IterateProfiles(ctx, func(profile *types.Profile) (stop bool) {
profiles = append(profiles, profile)
return false
})
return profiles
}
// HasProfile returns true iff the given user has a profile, or an error if something is wrong.
func (k Keeper) HasProfile(ctx sdk.Context, user string) bool {
_, found, err := k.GetProfile(ctx, user)
if err != nil {
return false
}
return found
}
// --------------------------------------------------------------------------------------------------------------------
// IterateDTagTransferRequests iterates over all the DTag transfer requests and performs the provided function
func (k Keeper) IterateDTagTransferRequests(
ctx sdk.Context, fn func(dTagTransferRequest types.DTagTransferRequest) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.DTagTransferRequestPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
request := types.MustUnmarshalDTagTransferRequest(k.cdc, iterator.Value())
stop := fn(request)
if stop {
break
}
}
}
// IterateUserIncomingDTagTransferRequests iterates over all the DTag transfer request made to the given user
// and performs the provided function
func (k Keeper) IterateUserIncomingDTagTransferRequests(
ctx sdk.Context, user string, fn func(dTagTransferRequest types.DTagTransferRequest) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.IncomingDTagTransferRequestsPrefix(user))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
request := types.MustUnmarshalDTagTransferRequest(k.cdc, iterator.Value())
stop := fn(request)
if stop {
break
}
}
}
// --------------------------------------------------------------------------------------------------------------------
// IterateApplicationLinks iterates through all the application links and performs the provided function
func (k Keeper) IterateApplicationLinks(ctx sdk.Context, fn func(link types.ApplicationLink) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.ApplicationLinkPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
link := types.MustUnmarshalApplicationLink(k.cdc, iterator.Value())
stop := fn(link)
if stop {
break
}
}
}
// IterateUserApplicationLinks iterates through all the application links related to the given user
// and performs the provided function
func (k Keeper) IterateUserApplicationLinks(ctx sdk.Context, user string, fn func(link types.ApplicationLink) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.UserApplicationLinksPrefix(user))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
link := types.MustUnmarshalApplicationLink(k.cdc, iterator.Value())
stop := fn(link)
if stop {
break
}
}
}
// GetApplicationLinks returns a slice of ApplicationLinkEntry objects containing the details of all the
// applications links entries stored inside the current context
func (k Keeper) GetApplicationLinks(ctx sdk.Context) []types.ApplicationLink {
var links []types.ApplicationLink
k.IterateApplicationLinks(ctx, func(link types.ApplicationLink) (stop bool) {
links = append(links, link)
return false
})
return links
}
// IterateExpiringApplicationLinks iterates through all the expiring application links references.
// The key will be skipped and deleted if the application link has already been deleted.
func (k Keeper) IterateExpiringApplicationLinks(ctx sdk.Context, fn func(link types.ApplicationLink) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.ExpiringAppLinkTimePrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
// This iterator has the following key and value structure:
// ExpiringAppLinkTimePrefix | Expiration Time | Client ID -> Client ID
//
// This means that in order to get the expired application links we need to:
// 1. Extract the Expiration Time from the iterator key
// 2. If the Expiration Time has passed, get the application link by using the Client ID from the iterator value
// First, we remove the prefix from the key, so we are left with the Expiration Time and Client ID
trimmedPrefixKey := bytes.TrimPrefix(iterator.Key(), types.ExpiringAppLinkTimePrefix)
// Second, we remove the Client ID from the trimmed key, so we are left only with the Expiration Time
expiringTime, err := sdk.ParseTimeBytes(bytes.TrimSuffix(trimmedPrefixKey, iterator.Value()))
if err != nil {
panic(err)
}
// Third, we get the Client ID from the iterator value
clientIDKey := types.ApplicationLinkClientIDKey(string(iterator.Value()))
// Skip if application link has been deleted already
if !store.Has(clientIDKey) {
store.Delete(iterator.Key())
continue
}
// Check if the expiration time has passed (the application link is expired)
if ctx.BlockTime().After(expiringTime) {
applicationKey := store.Get(clientIDKey)
link := types.MustUnmarshalApplicationLink(k.cdc, store.Get(applicationKey))
stop := fn(link)
if stop {
break
}
}
}
}
// --------------------------------------------------------------------------------------------------------------------
// IterateChainLinks iterates through the chain links and perform the provided function
func (k Keeper) IterateChainLinks(ctx sdk.Context, fn func(link types.ChainLink) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.ChainLinksPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
link := types.MustUnmarshalChainLink(k.cdc, iterator.Value())
stop := fn(link)
if stop {
break
}
}
}
// IterateUserChainLinks iterates through all the chain links related to the given user and perform the provided function
func (k Keeper) IterateUserChainLinks(ctx sdk.Context, user string, fn func(link types.ChainLink) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.UserChainLinksPrefix(user))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
link := types.MustUnmarshalChainLink(k.cdc, iterator.Value())
stop := fn(link)
if stop {
break
}
}
}
// IterateUserChainLinksByChain iterates through the chain links related to the given user by the chain having the given chain name and perform the provided function
func (k Keeper) IterateUserChainLinksByChain(ctx sdk.Context, user string, chainName string, fn func(link types.ChainLink) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.UserChainLinksChainPrefix(user, chainName))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
link := types.MustUnmarshalChainLink(k.cdc, iterator.Value())
stop := fn(link)
if stop {
break
}
}
}
// GetChainLinks allows to returns the list of all stored chain links
func (k Keeper) GetChainLinks(ctx sdk.Context) []types.ChainLink {
var links []types.ChainLink
k.IterateChainLinks(ctx, func(link types.ChainLink) (stop bool) {
links = append(links, link)
return false
})
return links
}
// IterateDefaultExternalAddresses iterates through the default external addresses and performs the provided function
func (k Keeper) IterateDefaultExternalAddresses(ctx sdk.Context, fn func(entry types.DefaultExternalAddressEntry) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.DefaultExternalAddressPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
owner, chainName := types.GetDefaultExternalAddressData(iterator.Key())
target := string(iterator.Value())
stop := fn(types.NewDefaultExternalAddressEntry(owner, chainName, target))
if stop {
break
}
}
}
// GetDefaultExternalAddressEntries returns a slice of DefaultExternalAddressEntry objects containing the details of all the
// default exnternal address entries stored inside the current context
func (k Keeper) GetDefaultExternalAddressEntries(ctx sdk.Context) []types.DefaultExternalAddressEntry {
var entries []types.DefaultExternalAddressEntry
k.IterateDefaultExternalAddresses(ctx, func(entry types.DefaultExternalAddressEntry) (stop bool) {
entries = append(entries, entry)
return false
})
return entries
}