-
Notifications
You must be signed in to change notification settings - Fork 79
/
post.go
276 lines (235 loc) · 7.56 KB
/
post.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
package keeper
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/public-awesome/stargaze/x/curating/types"
)
// GetPosts returns all posts on chain based on vendor id
func (k Keeper) GetPosts(ctx sdk.Context, vendorID uint32) (posts []types.Post) {
k.IteratePosts(ctx, vendorID, func(post types.Post) bool {
posts = append(posts, post)
return false
})
return
}
// GetPost returns post if one exists
func (k Keeper) GetPost(
ctx sdk.Context, vendorID uint32, postID types.PostID) (post types.Post, found bool, err error) {
store := ctx.KVStore(k.storeKey)
key := types.PostKey(vendorID, postID)
value := store.Get(key)
if value == nil {
return post, false, nil
}
k.MustUnmarshalPost(value, &post)
return post, true, nil
}
// CreatePost registers a post on-chain and starts the curation period.
// It can be called from CreateUpvote() when a post doesn't exist yet.
func (k Keeper) CreatePost(
ctx sdk.Context,
vendorID uint32,
postID *types.PostID,
bodyHash types.BodyHash,
body string,
creator, rewardAccount sdk.AccAddress,
chainID string,
contractAddress sdk.AccAddress,
metadata string,
parentID *types.PostID,
) (post types.Post, err error) {
err = k.validateVendorID(ctx, vendorID)
if err != nil {
return post, err
}
err = k.validatePostBodyLength(ctx, body)
if err != nil {
return post, err
}
err = k.validatePostBodyLength(ctx, body)
if err != nil {
return post, err
}
if rewardAccount.Empty() {
rewardAccount = creator
}
if postID != nil {
_, found, err := k.GetPost(ctx, vendorID, *postID)
if err != nil {
return post, err
}
if found {
return post, types.ErrDuplicatePost
}
}
if vendorID == 0 {
rawPostID := k.GetPostID(ctx) + 1
k.SetPostID(ctx, rawPostID)
id := types.PostIDFromInt64(int64(rawPostID))
postID = &id
}
if postID == nil {
return post, types.ErrInvalidPostID
}
curationWindow := k.GetParams(ctx).CurationWindow
curationEndTime := ctx.BlockTime().Add(curationWindow)
post = types.NewPost(
vendorID,
*postID,
bodyHash,
body,
creator,
rewardAccount,
curationEndTime,
chainID,
creator,
contractAddress,
metadata,
false,
parentID,
)
k.SetPost(ctx, post)
k.InsertCurationQueue(ctx, vendorID, *postID, curationEndTime)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypePost,
sdk.NewAttribute(types.AttributeKeyVendorID, fmt.Sprintf("%d", vendorID)),
sdk.NewAttribute(types.AttributeKeyPostID, postID.String()),
sdk.NewAttribute(types.AttributeKeyCreator, creator.String()),
sdk.NewAttribute(types.AttributeKeyRewardAccount, rewardAccount.String()),
sdk.NewAttribute(types.AttributeKeyBodyHash, bodyHash.String()),
sdk.NewAttribute(types.AttributeKeyBody, body),
sdk.NewAttribute(types.AttributeCurationEndTime, curationEndTime.Format(time.RFC3339)),
sdk.NewAttribute(types.AttributeKeyVoteDenom, types.DefaultVoteDenom),
sdk.NewAttribute(types.AttributeKeyChainID, chainID),
sdk.NewAttribute(types.AttributeKeyContractAddress, contractAddress.String()),
sdk.NewAttribute(types.AttributeKeyMetadata, metadata),
sdk.NewAttribute(types.AttributeKeyLocked, "false"),
),
})
if parentID != nil {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypePost,
sdk.NewAttribute(types.AttributeKeyVendorID, fmt.Sprintf("%d", vendorID)),
sdk.NewAttribute(types.AttributeKeyPostID, postID.String()),
sdk.NewAttribute(types.AttributeKeyParentID, parentID.String()),
),
})
}
return post, nil
}
// SetPost sets a post in the store
func (k Keeper) SetPost(ctx sdk.Context, post types.Post) {
store := ctx.KVStore(k.storeKey)
key := types.PostKey(post.VendorID, post.PostID)
value := k.MustMarshalPost(post)
store.Set(key, value)
}
// DeletePost removes a post
func (k Keeper) DeletePost(ctx sdk.Context, vendorID uint32, postID types.PostID) error {
err := k.validateVendorID(ctx, vendorID)
if err != nil {
return err
}
store := ctx.KVStore(k.storeKey)
key := types.PostKey(vendorID, postID)
store.Delete(key)
return nil
}
// InsertCurationQueue inserts a VPPair into the right timeslot in the curation queue
func (k Keeper) InsertCurationQueue(
ctx sdk.Context, vendorID uint32, postID types.PostID, curationEndTime time.Time) {
vpPair := types.VPPair{VendorID: vendorID, PostID: postID}
timeSlice := k.GetCurationQueueTimeSlice(ctx, curationEndTime)
if len(timeSlice) == 0 {
k.SetCurationQueueTimeSlice(ctx, curationEndTime, []types.VPPair{vpPair})
return
}
timeSlice = append(timeSlice, vpPair)
k.SetCurationQueueTimeSlice(ctx, curationEndTime, timeSlice)
}
// RemoveFromCurationQueue will remove an entire VPPair set from the queue
func (k Keeper) RemoveFromCurationQueue(ctx sdk.Context, curationEndTime time.Time) {
ctx.KVStore(k.storeKey).Delete(types.CurationQueueByTimeKey(curationEndTime))
}
// GetCurationQueueTimeSlice returns a slice of Vendor/PostID pairs for a give time
func (k Keeper) GetCurationQueueTimeSlice(
ctx sdk.Context, timestamp time.Time) (vpPairs []types.VPPair) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.CurationQueueByTimeKey(timestamp))
if bz == nil {
return []types.VPPair{}
}
vps := types.VPPairs{}
k.cdc.MustUnmarshalBinaryBare(bz, &vps)
return vps.Pairs
}
// SetCurationQueueTimeSlice sets a slice of Vendor/PostIDs in the curation queue
func (k Keeper) SetCurationQueueTimeSlice(
ctx sdk.Context, timestamp time.Time, vps []types.VPPair) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&types.VPPairs{Pairs: vps})
store.Set(types.CurationQueueByTimeKey(timestamp), bz)
}
// IterateExpiredPosts iterates over posts that have finished their
// curation period, and performs a callback fuction.
func (k Keeper) IterateExpiredPosts(
ctx sdk.Context, cb func(post types.Post) (stop bool)) {
it := k.CurationQueueIterator(ctx, ctx.BlockTime())
defer it.Close()
for ; it.Valid(); it.Next() {
vps := types.VPPairs{}
k.cdc.MustUnmarshalBinaryBare(it.Value(), &vps)
for _, vp := range vps.Pairs {
post, found, err := k.GetPost(ctx, vp.VendorID, vp.PostID)
if err != nil {
// Do want to panic here because if a post doesn't exist for an upvote
// it means there's some kind of consensus failure, so halt the chain.
panic(err)
}
if found {
cb(post)
}
}
}
}
// CurationQueueIterator returns an sdk.Iterator for all the posts
// in the queue that expire by endTime
func (k Keeper) CurationQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return store.Iterator(
types.KeyPrefixCurationQueue,
sdk.PrefixEndBytes(types.CurationQueueByTimeKey(endTime)))
}
// IteratePosts iterates over the all the posts by vendor_id and performs a callback function
func (k Keeper) IteratePosts(ctx sdk.Context, vendorID uint32, cb func(post types.Post) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.PostsKey(vendorID))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var post types.Post
k.MustUnmarshalPost(iterator.Value(), &post)
if cb(post) {
break
}
}
}
// ----- The PostID for native posts (vendor = 0)
// GetPostID gets the highest postl ID
func (k Keeper) GetPostID(ctx sdk.Context) (postID uint64) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.PostIDKey)
if bz == nil {
k.SetPostID(ctx, 0)
return 0
}
postID = types.GetPostIDFromBytes(bz)
return postID
}
// SetPostID sets the new post ID to the store
func (k Keeper) SetPostID(ctx sdk.Context, postID uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.PostIDKey, types.GetPostIDBytes(postID))
}