-
Notifications
You must be signed in to change notification settings - Fork 14
/
keeper.go
406 lines (331 loc) · 11.4 KB
/
keeper.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package claim
import (
"net/url"
"time"
app "github.com/TruStory/truchain/types"
"github.com/TruStory/truchain/x/community"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/gaskv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
log "github.com/tendermint/tendermint/libs/log"
)
// Keeper is the model object for the module
type Keeper struct {
storeKey sdk.StoreKey
codec *codec.Codec
paramStore params.Subspace
accountKeeper AccountKeeper
communityKeeper community.Keeper
}
// NewKeeper creates a new claim keeper
func NewKeeper(storeKey sdk.StoreKey, paramStore params.Subspace, codec *codec.Codec, accountKeeper AccountKeeper, communityKeeper community.Keeper) Keeper {
return Keeper{
storeKey,
codec,
paramStore.WithKeyTable(ParamKeyTable()),
accountKeeper,
communityKeeper,
}
}
// SubmitClaim creates a new claim in the claim key-value store
func (k Keeper) SubmitClaim(ctx sdk.Context, body, communityID string,
creator sdk.AccAddress, source url.URL) (claim Claim, err sdk.Error) {
err = k.validateLength(ctx, body)
if err != nil {
return
}
jailed, err := k.accountKeeper.IsJailed(ctx, creator)
if err != nil {
return
}
if jailed {
return claim, ErrCreatorJailed(creator)
}
community, err := k.communityKeeper.Community(ctx, communityID)
if err != nil {
return claim, ErrInvalidCommunityID(community.ID)
}
claimID, err := k.claimID(ctx)
if err != nil {
return
}
claim = NewClaim(claimID, communityID, body, creator, source,
ctx.BlockHeader().Time,
)
// persist claim
k.setClaim(ctx, claim)
// increment claimID (primary key) for next claim
k.setClaimID(ctx, claimID+1)
// persist associations
k.setCommunityClaim(ctx, claim.CommunityID, claimID)
k.setCreatorClaim(ctx, claim.Creator, claimID)
k.setCreatedTimeClaim(ctx, claim.CreatedTime, claimID)
logger(ctx).Info("Submitted " + claim.String())
return claim, nil
}
// EditClaim allows admins to edit the body of a claim
func (k Keeper) EditClaim(ctx sdk.Context, id uint64, body string, editor sdk.AccAddress) (claim Claim, err sdk.Error) {
if !k.isAdmin(ctx, editor) {
err = ErrAddressNotAuthorised()
return
}
err = k.validateLength(ctx, body)
if err != nil {
return
}
claim, ok := k.Claim(ctx, id)
if !ok {
err = ErrUnknownClaim(id)
return
}
claim.Body = body
k.setClaim(ctx, claim)
return
}
// Claim gets a single claim by its ID
func (k Keeper) Claim(ctx sdk.Context, id uint64) (claim Claim, ok bool) {
store := k.store(ctx)
claimBytes := store.Get(key(id))
if claimBytes == nil {
return claim, false
}
k.codec.MustUnmarshalBinaryLengthPrefixed(claimBytes, &claim)
return claim, true
}
// Claims gets all the claims in reverse order
func (k Keeper) Claims(ctx sdk.Context) (claims Claims) {
store := k.store(ctx)
iterator := sdk.KVStoreReversePrefixIterator(store, ClaimsKeyPrefix)
return k.iterate(iterator)
}
// ClaimsBetweenIDs gets all claims between startClaimID to endClaimID
func (k Keeper) ClaimsBetweenIDs(ctx sdk.Context, startClaimID, endClaimID uint64) (claims Claims) {
iterator := k.claimsIterator(ctx, startClaimID, endClaimID)
return k.iterate(iterator)
}
// ClaimsBetweenTimes gets all claims between startTime and endTime
func (k Keeper) ClaimsBetweenTimes(ctx sdk.Context, startTime time.Time, endTime time.Time) (claims Claims) {
iterator := k.createdTimeRangeClaimsIterator(ctx, startTime, endTime)
return k.iterateAssociated(ctx, iterator)
}
// ClaimsBeforeTime gets all claims after a certain CreatedTime
func (k Keeper) ClaimsBeforeTime(ctx sdk.Context, createdTime time.Time) (claims Claims) {
iterator := k.beforeCreatedTimeClaimsIterator(ctx, createdTime)
return k.iterateAssociated(ctx, iterator)
}
// ClaimsAfterTime gets all claims after a certain CreatedTime
func (k Keeper) ClaimsAfterTime(ctx sdk.Context, createdTime time.Time) (claims Claims) {
iterator := k.afterCreatedTimeClaimsIterator(ctx, createdTime)
return k.iterateAssociated(ctx, iterator)
}
// CommunityClaims gets all the claims for a given community
func (k Keeper) CommunityClaims(ctx sdk.Context, communityID string) (claims Claims) {
return k.associatedClaims(ctx, communityClaimsKey(communityID))
}
// CreatorClaims gets all the claims for a given creator
func (k Keeper) CreatorClaims(ctx sdk.Context, creator sdk.AccAddress) (claims Claims) {
return k.associatedClaims(ctx, creatorClaimsKey(creator))
}
// AddBackingStake adds a stake amount to the total backing amount
func (k Keeper) AddBackingStake(ctx sdk.Context, id uint64, stake sdk.Coin) sdk.Error {
claim, ok := k.Claim(ctx, id)
if !ok {
return ErrUnknownClaim(id)
}
claim.TotalBacked = claim.TotalBacked.Add(stake)
claim.TotalStakers++
k.setClaim(ctx, claim)
return nil
}
// AddChallengeStake adds a stake amount to the total challenge amount
func (k Keeper) AddChallengeStake(ctx sdk.Context, id uint64, stake sdk.Coin) sdk.Error {
claim, ok := k.Claim(ctx, id)
if !ok {
return ErrUnknownClaim(id)
}
claim.TotalChallenged = claim.TotalChallenged.Add(stake)
claim.TotalStakers++
k.setClaim(ctx, claim)
return nil
}
// SubtractBackingStake adds a stake amount to the total backing amount
func (k Keeper) SubtractBackingStake(ctx sdk.Context, id uint64, stake sdk.Coin) sdk.Error {
claim, ok := k.Claim(ctx, id)
if !ok {
return ErrUnknownClaim(id)
}
claim.TotalBacked = claim.TotalBacked.Sub(stake)
k.setClaim(ctx, claim)
return nil
}
// SubtractChallengeStake adds a stake amount to the total challenge amount
func (k Keeper) SubtractChallengeStake(ctx sdk.Context, id uint64, stake sdk.Coin) sdk.Error {
claim, ok := k.Claim(ctx, id)
if !ok {
return ErrUnknownClaim(id)
}
claim.TotalChallenged = claim.TotalChallenged.Sub(stake)
k.setClaim(ctx, claim)
return nil
}
// SetFirstArgumentTime sets time when first argument was created on a claim
func (k Keeper) SetFirstArgumentTime(ctx sdk.Context, id uint64, firstArgumentTime time.Time) sdk.Error {
claim, ok := k.Claim(ctx, id)
if !ok {
return ErrUnknownClaim(id)
}
claim.FirstArgumentTime = firstArgumentTime
k.setClaim(ctx, claim)
return nil
}
// AddAdmin adds a new admin
func (k Keeper) AddAdmin(ctx sdk.Context, admin, creator sdk.AccAddress) (err sdk.Error) {
params := k.GetParams(ctx)
// first admin can be added without any authorisation
if len(params.ClaimAdmins) > 0 && !k.isAdmin(ctx, creator) {
err = ErrAddressNotAuthorised()
}
// if already present, don't add again
for _, currentAdmin := range params.ClaimAdmins {
if currentAdmin.Equals(admin) {
return
}
}
params.ClaimAdmins = append(params.ClaimAdmins, admin)
k.SetParams(ctx, params)
return
}
// RemoveAdmin removes an admin
func (k Keeper) RemoveAdmin(ctx sdk.Context, admin, remover sdk.AccAddress) (err sdk.Error) {
if !k.isAdmin(ctx, remover) {
err = ErrAddressNotAuthorised()
}
params := k.GetParams(ctx)
for i, currentAdmin := range params.ClaimAdmins {
if currentAdmin.Equals(admin) {
params.ClaimAdmins = append(params.ClaimAdmins[:i], params.ClaimAdmins[i+1:]...)
}
}
k.SetParams(ctx, params)
return
}
func (k Keeper) isAdmin(ctx sdk.Context, address sdk.AccAddress) bool {
for _, admin := range k.GetParams(ctx).ClaimAdmins {
if address.Equals(admin) {
return true
}
}
return false
}
func (k Keeper) validateLength(ctx sdk.Context, body string) sdk.Error {
var minClaimLength int
var maxClaimLength int
k.paramStore.Get(ctx, KeyMinClaimLength, &minClaimLength)
k.paramStore.Get(ctx, KeyMaxClaimLength, &maxClaimLength)
len := len([]rune(body))
if len < minClaimLength {
return ErrInvalidBodyTooShort(body)
}
if len > maxClaimLength {
return ErrInvalidBodyTooLong()
}
return nil
}
// claimID gets the highest claim ID
func (k Keeper) claimID(ctx sdk.Context) (claimID uint64, err sdk.Error) {
store := k.store(ctx)
bz := store.Get(ClaimIDKey)
if bz == nil {
return 0, ErrUnknownClaim(claimID)
}
k.codec.MustUnmarshalBinaryLengthPrefixed(bz, &claimID)
return claimID, nil
}
// set the claim ID
func (k Keeper) setClaimID(ctx sdk.Context, claimID uint64) {
store := k.store(ctx)
bz := k.codec.MustMarshalBinaryLengthPrefixed(claimID)
store.Set(ClaimIDKey, bz)
}
// setClaim sets a claim in store
func (k Keeper) setClaim(ctx sdk.Context, claim Claim) {
store := k.store(ctx)
bz := k.codec.MustMarshalBinaryLengthPrefixed(claim)
store.Set(key(claim.ID), bz)
}
// setCommunityClaim sets a community <-> claim association in store
func (k Keeper) setCommunityClaim(ctx sdk.Context, communityID string, claimID uint64) {
store := k.store(ctx)
bz := k.codec.MustMarshalBinaryLengthPrefixed(claimID)
store.Set(communityClaimKey(communityID, claimID), bz)
}
func (k Keeper) setCreatorClaim(ctx sdk.Context, creator sdk.AccAddress, claimID uint64) {
store := k.store(ctx)
bz := k.codec.MustMarshalBinaryLengthPrefixed(claimID)
store.Set(creatorClaimKey(creator, claimID), bz)
}
func (k Keeper) setCreatedTimeClaim(ctx sdk.Context, createdTime time.Time, claimID uint64) {
store := k.store(ctx)
bz := k.codec.MustMarshalBinaryLengthPrefixed(claimID)
store.Set(createdTimeClaimKey(createdTime, claimID), bz)
}
// claimsIterator returns an sdk.Iterator for claims from startClaimID to endClaimID
func (k Keeper) claimsIterator(ctx sdk.Context, startClaimID, endClaimID uint64) sdk.Iterator {
store := k.store(ctx)
return store.Iterator(key(startClaimID), sdk.PrefixEndBytes(key(endClaimID)))
}
func (k Keeper) beforeCreatedTimeClaimsIterator(ctx sdk.Context, createdTime time.Time) sdk.Iterator {
store := k.store(ctx)
return store.Iterator(CreatedTimeClaimsPrefix, sdk.PrefixEndBytes(createdTimeClaimsKey(createdTime)))
}
func (k Keeper) afterCreatedTimeClaimsIterator(ctx sdk.Context, createdTime time.Time) sdk.Iterator {
store := k.store(ctx)
return store.Iterator(createdTimeClaimsKey(createdTime), sdk.PrefixEndBytes(CreatedTimeClaimsPrefix))
}
// createdTimeRangeClaimsIterator returns an sdk.Iterator for all claims between startCreatedTime and endCreatedTime
func (k Keeper) createdTimeRangeClaimsIterator(ctx sdk.Context, startCreatedTime, endCreatedTime time.Time) sdk.Iterator {
store := k.store(ctx)
return store.Iterator(createdTimeClaimsKey(startCreatedTime), sdk.PrefixEndBytes(createdTimeClaimsKey(endCreatedTime)))
}
func (k Keeper) associatedClaims(ctx sdk.Context, prefix []byte) (claims Claims) {
store := k.store(ctx)
iterator := sdk.KVStoreReversePrefixIterator(store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var claimID uint64
k.codec.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &claimID)
claim, ok := k.Claim(ctx, claimID)
if ok {
claims = append(claims, claim)
}
}
return
}
func (k Keeper) iterate(iterator sdk.Iterator) (claims Claims) {
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var claim Claim
k.codec.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &claim)
claims = append(claims, claim)
}
return
}
func (k Keeper) iterateAssociated(ctx sdk.Context, iterator sdk.Iterator) (claims Claims) {
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var claimID uint64
k.codec.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &claimID)
claim, ok := k.Claim(ctx, claimID)
if ok {
claims = append(claims, claim)
}
}
return
}
func (k Keeper) store(ctx sdk.Context) sdk.KVStore {
return gaskv.NewStore(ctx.MultiStore().GetKVStore(k.storeKey), ctx.GasMeter(), app.KVGasConfig())
}
func logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", ModuleName)
}