/
admin_feed.go
254 lines (224 loc) · 8.63 KB
/
admin_feed.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
package routes
import (
"bytes"
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/deso-protocol/core/lib"
)
// AdminPinPostRequest ...
type AdminPinPostRequest struct {
// The post hash of the post to pin or unpin from the global feed
PostHashHex string `safeForLogging:"true"`
// If true, remove the given post hash hex from the list of pinned posts
UnpinPost bool `safeForLogging:"true"`
}
// AdminPinPostResponse ...
type AdminPinPostResponse struct{}
// AdminPinPost ...
func (fes *APIServer) AdminPinPost(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := AdminPinPostRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateGlobalFeed: Problem parsing request body: %v", err))
return
}
// Decode the postHash.
postHash := &lib.BlockHash{}
if requestData.PostHashHex != "" {
postHashBytes, err := hex.DecodeString(requestData.PostHashHex)
if err != nil || len(postHashBytes) != lib.HashSizeBytes {
_AddBadRequestError(ww, fmt.Sprintf("AdminPinPost: Error parsing post hash %v: %v",
requestData.PostHashHex, err))
return
}
copy(postHash[:], postHashBytes)
} else {
_AddBadRequestError(ww, fmt.Sprintf("AdminPinPost: Request missing PostHashHex"))
return
}
utxoView, err := fes.backendServer.GetMempool().GetAugmentedUniversalView()
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminPinPost: Problem fetching utxoView: %v", err))
return
}
// Get the post entry.
postEntry := utxoView.GetPostEntryForPostHash(postHash)
if postEntry == nil {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminPinPost: Problem getting postEntry for post hash: %v : %s", err, requestData.PostHashHex))
return
}
// Create a key to access the global state object.
dbKey := GlobalStateKeyForTstampPinnedPostHash(postEntry.TimestampNanos, postHash)
if requestData.UnpinPost {
err = fes.GlobalState.Delete(dbKey)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminPinPost: Problem deleting post from global state: %v", err))
return
}
} else {
// Encode the post entry and stick it in the database.
err = fes.GlobalState.Put(dbKey, []byte{1})
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminPinPost: Problem putting updated user metadata: %v", err))
return
}
}
// If we made it this far we were successful, return without error.
res := AdminPinPostResponse{}
if err := json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminPinPost: Problem encoding response as JSON: %v", err))
return
}
}
// AdminUpdateGlobalFeedRequest...
type AdminUpdateGlobalFeedRequest struct {
// The post hash of the post to add or remove from the global feed.
PostHashHex string `safeForLogging:"true"`
// If true, remove the given post hash hex from the global feed.
RemoveFromGlobalFeed bool `safeForLogging:"true"`
}
// AdminUpdateGlobalFeedResponse ...
type AdminUpdateGlobalFeedResponse struct{}
// AdminUpdateGlobalFeed ...
// NOTE: This function adds posts to the global feed as well as to the hot feed approved posts.
func (fes *APIServer) AdminUpdateGlobalFeed(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := AdminUpdateGlobalFeedRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateGlobalFeed: Problem parsing request body: %v", err))
return
}
// Decode the postHash.
postHash := &lib.BlockHash{}
if requestData.PostHashHex != "" {
postHashBytes, err := hex.DecodeString(requestData.PostHashHex)
if err != nil || len(postHashBytes) != lib.HashSizeBytes {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateGlobalFeed: Error parsing post hash %v: %v",
requestData.PostHashHex, err))
return
}
copy(postHash[:], postHashBytes)
} else {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateGlobalFeed: Request missing PostHashHex"))
return
}
utxoView, err := fes.backendServer.GetMempool().GetAugmentedUniversalView()
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateGlobalFeed: Problem fetching utxoView: %v", err))
return
}
// Get the post entry.
postEntry := utxoView.GetPostEntryForPostHash(postHash)
if postEntry == nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateGlobalFeed: Problem getting postEntry for post hash: %v : %s", err, requestData.PostHashHex))
return
}
// Create a key to access the global state object.
dbKey := GlobalStateKeyForTstampPostHash(postEntry.TimestampNanos, postHash)
if requestData.RemoveFromGlobalFeed {
err = fes.GlobalState.Delete(dbKey)
if err != nil {
_AddInternalServerError(ww, fmt.Sprintf("AdminUpdateGlobalFeed: Problem deleting post from global state: %v", err))
return
}
} else {
// Encode the post entry and stick it in the database.
err = fes.GlobalState.Put(dbKey, []byte{1})
if err != nil {
_AddInternalServerError(ww, fmt.Sprintf("AdminUpdateGlobalFeed: Problem putting updated user metadata: %v", err))
return
}
}
// Add a hot feed op for this post.
hotFeedOp := HotFeedApprovedPostOp{
IsRemoval: requestData.RemoveFromGlobalFeed,
Multiplier: 1,
}
hotFeedOpDataBuf := bytes.NewBuffer([]byte{})
gob.NewEncoder(hotFeedOpDataBuf).Encode(hotFeedOp)
opTimestamp := uint64(time.Now().UnixNano())
hotFeedOpKey := GlobalStateKeyForHotFeedApprovedPostOp(opTimestamp, postHash)
err = fes.GlobalState.Put(hotFeedOpKey, hotFeedOpDataBuf.Bytes())
if err != nil {
_AddInternalServerError(ww, fmt.Sprintf("AdminUpdateGlobalFeed: Problem putting hotFeedOp: %v", err))
return
}
// If we made it this far we were successful, return without error.
res := AdminUpdateGlobalFeedResponse{}
if err := json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateGlobalFeed: Problem encoding response as JSON: %v", err))
return
}
}
// AdminRemoveNilPostsRequest...
type AdminRemoveNilPostsRequest struct {
// Number of posts to try to fetch from global state, starting from the most recent post
// added to the global feed.
NumPostsToSearch int `safeForLogging:"true"`
}
// AdminUpdateGlobalFeedResponse ...
type AdminRemoveNilPostsResponse struct{}
func (fes *APIServer) AdminRemoveNilPosts(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := AdminRemoveNilPostsRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateGlobalFeed: Problem parsing request body: %v", err))
return
}
// Get a view with all the mempool transactions (used to get all posts / reader state).
utxoView, err := fes.backendServer.GetMempool().GetAugmentedUniversalView()
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminRemoveNilPosts: Error getting augmented universal view: #{err}"))
return
}
postsToFetchCount := 1000
if requestData.NumPostsToSearch != 0 {
postsToFetchCount = requestData.NumPostsToSearch
}
maxBigEndianUint64Bytes := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
maxKeyLen := 1 + len(maxBigEndianUint64Bytes) + lib.HashSizeBytes
// Get postHashes for posts in the globalFeed.
keys, _, err := fes.GlobalState.Seek(
_GlobalStatePrefixTstampNanosPostHash, /*startPrefix*/
_GlobalStatePrefixTstampNanosPostHash, /*validForPrefix*/
maxKeyLen, /*maxKeyLen*/
postsToFetchCount, /*numToFetch*/
true, /*reverse*/
false)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminRemoveNilPosts: Problem seeking through global state keys: #{err}"))
return
}
for _, dbKeyBytes := range keys {
// Chop the public key out of the db key.
// The dbKeyBytes are: [One Prefix Byte][Uint64 Tstamp Bytes][PostHash]
postHash := &lib.BlockHash{}
copy(postHash[:], dbKeyBytes[1+len(maxBigEndianUint64Bytes):][:])
// Get the postEntry from the utxoView.
postEntry := utxoView.GetPostEntryForPostHash(postHash)
// If the postEntry doesn't exist, clear the entry for the map of global feed posts
// from global state
if postEntry == nil {
err = fes.GlobalState.Delete(dbKeyBytes)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminRemoveNilPosts: Problem deleting missing key in GlobalState Key-value store for global feed: #{err}"))
return
}
}
}
res := &AdminRemoveNilPostsResponse{}
if err := json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminRemoveNilPosts: Problem encoding response as JSON: #{err}"))
return
}
}