-
Notifications
You must be signed in to change notification settings - Fork 118
/
distributionRecords.go
304 lines (288 loc) · 10.9 KB
/
distributionRecords.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
298
299
300
301
302
303
304
package keeper
import (
"fmt"
"github.com/Sifchain/sifnode/x/dispensation/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
)
// This package adds set and get operations for DistributionRecord
// A distribution record is responsible for distributing funds to the recipients
func (k Keeper) SetDistributionRecord(ctx sdk.Context, dr types.DistributionRecord) error {
if !dr.Validate() {
return errors.Wrapf(types.ErrInvalid, "unable to set record : %s", dr.String())
}
store := ctx.KVStore(k.storeKey)
key := types.GetDistributionRecordKey(dr.DistributionStatus, dr.DistributionName, dr.RecipientAddress, dr.DistributionType)
store.Set(key, k.cdc.MustMarshal(&dr))
return nil
}
func (k Keeper) GetDistributionRecord(ctx sdk.Context, airdropName string, recipientAddress string, status types.DistributionStatus, distributionType types.DistributionType) (*types.DistributionRecord, error) {
var dr types.DistributionRecord
store := ctx.KVStore(k.storeKey)
key := types.GetDistributionRecordKey(status, airdropName, recipientAddress, distributionType)
if !k.Exists(ctx, key) {
return &dr, errors.Wrapf(types.ErrInvalid, "record Does not exist : %s", dr.String())
}
bz := store.Get(key)
k.cdc.MustUnmarshal(bz, &dr)
return &dr, nil
}
func (k Keeper) ExistsDistributionRecord(ctx sdk.Context, airdropName string, recipientAddress string, status types.DistributionStatus, distributionType types.DistributionType) bool {
key := types.GetDistributionRecordKey(status, airdropName, recipientAddress, distributionType)
return k.Exists(ctx, key)
}
func (k Keeper) GetDistributionRecordsIterator(ctx sdk.Context, status types.DistributionStatus) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
switch status {
case types.DistributionStatus_DISTRIBUTION_STATUS_PENDING:
return sdk.KVStorePrefixIterator(store, types.DistributionRecordPrefixPending)
case types.DistributionStatus_DISTRIBUTION_STATUS_COMPLETED:
return sdk.KVStorePrefixIterator(store, types.DistributionRecordPrefixCompleted)
case types.DistributionStatus_DISTRIBUTION_STATUS_FAILED:
return sdk.KVStorePrefixIterator(store, types.DistributionRecordPrefixFailed)
default:
return nil
}
}
func (k Keeper) DeleteDistributionRecord(ctx sdk.Context, distributionName string, recipientAddress string, status types.DistributionStatus, distributionType types.DistributionType) error {
var dr types.DistributionRecord
store := ctx.KVStore(k.storeKey)
key := types.GetDistributionRecordKey(status, distributionName, recipientAddress, distributionType)
if !k.Exists(ctx, key) {
return errors.Wrapf(types.ErrInvalid, "record Does not exist : %s", dr.String())
}
store.Delete(key)
return nil
}
func (k Keeper) GetRecordsForNameAndStatus(ctx sdk.Context, name string, status types.DistributionStatus) *types.DistributionRecords {
var res types.DistributionRecords
iterator := k.GetDistributionRecordsIterator(ctx, status)
defer func(iterator sdk.Iterator) {
err := iterator.Close()
if err != nil {
panic("Failed to close iterator")
}
}(iterator)
for ; iterator.Valid(); iterator.Next() {
var dr types.DistributionRecord
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &dr)
if dr.DistributionName == name {
res.DistributionRecords = append(res.DistributionRecords, &dr)
}
}
return &res
}
func (k Keeper) GetRecordsForNameStatusAndType(ctx sdk.Context, name string, status types.DistributionStatus, distributionType types.DistributionType) *types.DistributionRecords {
var res types.DistributionRecords
iterator := k.GetDistributionRecordsIterator(ctx, status)
defer func(iterator sdk.Iterator) {
err := iterator.Close()
if err != nil {
panic("Failed to close iterator")
}
}(iterator)
for ; iterator.Valid(); iterator.Next() {
var dr types.DistributionRecord
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &dr)
if dr.DistributionName == name && dr.DistributionType == distributionType {
res.DistributionRecords = append(res.DistributionRecords, &dr)
}
}
return &res
}
func (k Keeper) GetRecordsForRecipient(ctx sdk.Context, recipient string) *types.DistributionRecords {
var res types.DistributionRecords
iterator := k.GetDistributionRecordsIterator(ctx, types.DistributionStatus_DISTRIBUTION_STATUS_PENDING)
defer func(iterator sdk.Iterator) {
err := iterator.Close()
if err != nil {
panic("Failed to close iterator")
}
}(iterator)
for ; iterator.Valid(); iterator.Next() {
var dr types.DistributionRecord
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &dr)
if dr.RecipientAddress == recipient {
res.DistributionRecords = append(res.DistributionRecords, &dr)
}
}
iterator = k.GetDistributionRecordsIterator(ctx, types.DistributionStatus_DISTRIBUTION_STATUS_COMPLETED)
defer func(iterator sdk.Iterator) {
err := iterator.Close()
if err != nil {
panic("Failed to close iterator")
}
}(iterator)
for ; iterator.Valid(); iterator.Next() {
var dr types.DistributionRecord
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &dr)
if dr.RecipientAddress == recipient {
res.DistributionRecords = append(res.DistributionRecords, &dr)
}
}
iterator = k.GetDistributionRecordsIterator(ctx, types.DistributionStatus_DISTRIBUTION_STATUS_FAILED)
defer func(iterator sdk.Iterator) {
err := iterator.Close()
if err != nil {
panic("Failed to close iterator")
}
}(iterator)
for ; iterator.Valid(); iterator.Next() {
var dr types.DistributionRecord
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &dr)
if dr.RecipientAddress == recipient {
res.DistributionRecords = append(res.DistributionRecords, &dr)
}
}
return &res
}
func (k Keeper) GetLimitedRecordsForStatus(ctx sdk.Context, status types.DistributionStatus) *types.DistributionRecords {
var res types.DistributionRecords
iterator := k.GetDistributionRecordsIterator(ctx, status)
count := 0
defer func(iterator sdk.Iterator) {
err := iterator.Close()
if err != nil {
panic("Failed to close iterator")
}
}(iterator)
for ; iterator.Valid(); iterator.Next() {
var dr types.DistributionRecord
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &dr)
res.DistributionRecords = append(res.DistributionRecords, &dr)
count++
if count == types.MaxRecordsPerBlock {
break
}
}
return &res
}
func (k Keeper) GetLimitedRecordsForRunner(ctx sdk.Context,
distributionName string,
authorizedRunner string,
distributionType types.DistributionType,
status types.DistributionStatus,
distributionCount int64) *types.DistributionRecords {
var res types.DistributionRecords
iterator := k.GetDistributionRecordsIterator(ctx, status)
count := int64(0)
defer func(iterator sdk.Iterator) {
err := iterator.Close()
if err != nil {
panic("Failed to close iterator")
}
}(iterator)
for ; iterator.Valid(); iterator.Next() {
if count == distributionCount {
break
}
var dr types.DistributionRecord
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &dr)
if dr.DistributionName == distributionName &&
dr.DistributionStatus == types.DistributionStatus_DISTRIBUTION_STATUS_PENDING &&
dr.AuthorizedRunner == authorizedRunner &&
dr.DistributionType == distributionType {
res.DistributionRecords = append(res.DistributionRecords, &dr)
count = count + 1
}
}
return &res
}
func (k Keeper) GetRecords(ctx sdk.Context) *types.DistributionRecords {
var res types.DistributionRecords
iterator := k.GetDistributionRecordsIterator(ctx, types.DistributionStatus_DISTRIBUTION_STATUS_PENDING)
defer func(iterator sdk.Iterator) {
err := iterator.Close()
if err != nil {
panic("Failed to close iterator")
}
}(iterator)
for ; iterator.Valid(); iterator.Next() {
var dr types.DistributionRecord
bytesValue := iterator.Value()
err := k.cdc.Unmarshal(bytesValue, &dr)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("Unmarshal failed for record bytes : %s ", bytesValue))
// Not panicking here .
// Records data is not that important . We can ignore a record if it is causing an issue for chain upgrade .
// Logging data out for investigation
continue
}
res.DistributionRecords = append(res.DistributionRecords, &dr)
}
iterator = k.GetDistributionRecordsIterator(ctx, types.DistributionStatus_DISTRIBUTION_STATUS_COMPLETED)
for ; iterator.Valid(); iterator.Next() {
var dr types.DistributionRecord
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &dr)
res.DistributionRecords = append(res.DistributionRecords, &dr)
}
iterator = k.GetDistributionRecordsIterator(ctx, types.DistributionStatus_DISTRIBUTION_STATUS_FAILED)
for ; iterator.Valid(); iterator.Next() {
var dr types.DistributionRecord
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &dr)
res.DistributionRecords = append(res.DistributionRecords, &dr)
}
return &res
}
func (k Keeper) GetRecordsForName(ctx sdk.Context, name string) *types.DistributionRecords {
var res types.DistributionRecords
iterator := k.GetDistributionRecordsIterator(ctx, types.DistributionStatus_DISTRIBUTION_STATUS_PENDING)
defer func(iterator sdk.Iterator) {
err := iterator.Close()
if err != nil {
panic("Failed to close iterator")
}
}(iterator)
for ; iterator.Valid(); iterator.Next() {
var dr types.DistributionRecord
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &dr)
if dr.DistributionName == name {
res.DistributionRecords = append(res.DistributionRecords, &dr)
}
}
iterator = k.GetDistributionRecordsIterator(ctx, types.DistributionStatus_DISTRIBUTION_STATUS_COMPLETED)
for ; iterator.Valid(); iterator.Next() {
var dr types.DistributionRecord
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &dr)
if dr.DistributionName == name {
res.DistributionRecords = append(res.DistributionRecords, &dr)
}
}
iterator = k.GetDistributionRecordsIterator(ctx, types.DistributionStatus_DISTRIBUTION_STATUS_FAILED)
for ; iterator.Valid(); iterator.Next() {
var dr types.DistributionRecord
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &dr)
if dr.DistributionName == name {
res.DistributionRecords = append(res.DistributionRecords, &dr)
}
}
return &res
}
func (k Keeper) ChangeRecordStatus(ctx sdk.Context, dr types.DistributionRecord, height int64, newStatus types.DistributionStatus) error {
oldStatus := dr.DistributionStatus
dr.DistributionStatus = newStatus
dr.DistributionCompletedHeight = height
// Setting to completed prefix
err := k.SetDistributionRecord(ctx, dr)
if err != nil {
return errors.Wrapf(types.ErrDistribution, "error setting distribution record : %s", dr.String())
}
// Deleting from old prefix
err = k.DeleteDistributionRecord(ctx, dr.DistributionName, dr.RecipientAddress, oldStatus, dr.DistributionType) // Delete the record in the pending prefix so the iteration is cheaper.
if err != nil {
return errors.Wrapf(types.ErrDistribution, "error deleting distribution record : %s", dr.String())
}
return nil
}