-
Notifications
You must be signed in to change notification settings - Fork 2
/
admin_nft.go
349 lines (303 loc) · 12 KB
/
admin_nft.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
package routes
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"io"
"net/http"
"reflect"
"time"
"github.com/deso-smart/deso-core/v2/lib"
)
type AdminGetNFTDropRequest struct {
// "-1" is used to request the next planned drop.
DropNumber int `safeForLogging:"true"`
}
type AdminGetNFTDropResponse struct {
DropEntry *NFTDropEntry
Posts []*PostEntryResponse
}
// Check global state and get the latest drop entry if available.
// If no drop entry is found in global state, returns a default-initialized NFTDropEntry.
func (fes *APIServer) GetLatestNFTDropEntry() (_dropEntry *NFTDropEntry, _err error) {
seekKey := _GlobalStatePrefixNFTDropNumberToNFTDropEntry
maxKeyLen := 9 // These keys are 1 prefix byte + 8 bytes for the uint64 drop number.
_, vals, err := fes.GlobalState.Seek(seekKey, seekKey, maxKeyLen, 1, true, true)
if err != nil {
return nil, fmt.Errorf("AdminGetNFTDrop: Error getting latest drop: %v", err)
}
if len(vals) > 1 {
return nil, fmt.Errorf(
"AdminGetNFTDrop: Unexpected number of drop entries (%d) returned.", len(vals))
}
dropEntry := &NFTDropEntry{}
if len(vals) != 0 {
// If we got here, we found a drop entry. Save the bytes to decode later.
dropEntryBytes := vals[0]
err = gob.NewDecoder(bytes.NewReader(dropEntryBytes)).Decode(&dropEntry)
if err != nil {
return nil, fmt.Errorf("AdminGetNFTDrop: Problem decoding bytes for latest drop entry: %v", err)
}
}
return dropEntry, nil
}
func (fes *APIServer) GetNFTDropEntry(nftDropNumber uint64) (_dropEntry *NFTDropEntry, _err error) {
keyBytes := GlobalStateKeyForNFTDropEntry(uint64(nftDropNumber))
dropEntryBytes, err := fes.GlobalState.Get(keyBytes)
if err != nil {
return nil, fmt.Errorf("GetNFTDropEntry: %v", err)
}
dropEntry := &NFTDropEntry{}
err = gob.NewDecoder(bytes.NewReader(dropEntryBytes)).Decode(&dropEntry)
if err != nil {
return nil, fmt.Errorf("GetNFTDropEntry: %v", err)
}
return dropEntry, nil
}
func (fes *APIServer) GetPostsForNFTDropEntry(dropEntryToReturn *NFTDropEntry,
) (_posts []*PostEntryResponse, _err error) {
profileEntryResponseMap := make(map[lib.PkMapKey]*ProfileEntryResponse)
var postEntryResponses []*PostEntryResponse
// Grab a view (needed for getting global params, etc).
utxoView, err := fes.backendServer.GetMempool().GetAugmentedUniversalView()
if err != nil {
return nil, fmt.Errorf("AdminGetPostsForNFTDropEntry: Error getting utxoView: %v", err)
}
for _, postHash := range dropEntryToReturn.NFTHashes {
postEntry := utxoView.GetPostEntryForPostHash(postHash)
postEntryResponse, err := fes._postEntryToResponse(postEntry, false, fes.Params, utxoView, nil, 2)
if err != nil {
return nil, fmt.Errorf(
"AdminGetPostsForNFTDropEntry: Error building postEntryResponse: %v, %s", err, postHash.String())
}
// Add the profile entry to the post entry.
profileEntryResponse, entryFound := profileEntryResponseMap[lib.MakePkMapKey(postEntry.PosterPublicKey)]
if !entryFound {
// If we didn't find the entry in our map, we need to make it...
profileEntry := utxoView.GetProfileEntryForPublicKey(postEntry.PosterPublicKey)
if profileEntry == nil {
// If we didn't find a profile entry, skip this post.
continue
} else {
profileEntryResponse = fes._profileEntryToResponse(profileEntry, utxoView)
profileEntryResponseMap[lib.MakePkMapKey(postEntry.PosterPublicKey)] = profileEntryResponse
}
}
postEntryResponse.ProfileEntryResponse = profileEntryResponse
postEntryResponses = append(postEntryResponses, postEntryResponse)
}
return postEntryResponses, nil
}
func (fes *APIServer) AdminGetNFTDrop(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := AdminGetNFTDropRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminGetNFTDrop: Error parsing request body: %v", err))
return
}
var err error
var dropEntryToReturn *NFTDropEntry
if requestData.DropNumber < 0 {
dropEntryToReturn, err = fes.GetLatestNFTDropEntry()
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminGetNFTDrop: Error getting latest drop: %v", err))
return
}
} else {
// Look up the drop entry for the drop number given.
dropEntryToReturn, err = fes.GetNFTDropEntry(uint64(requestData.DropNumber))
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminGetNFTDrop: Error getting NFT drop #%d: %v", requestData.DropNumber, err))
return
}
}
// Note that "dropEntryToReturn" can be nil if there are no entries in global state.
var postEntryResponses []*PostEntryResponse
if dropEntryToReturn != nil {
postEntryResponses, err = fes.GetPostsForNFTDropEntry(dropEntryToReturn)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminGetNFTDrop: : %v", err))
return
}
}
// Return all the data associated with the transaction in the response
res := AdminGetNFTDropResponse{
DropEntry: dropEntryToReturn,
Posts: postEntryResponses,
}
if err = json.NewEncoder(ww).Encode(res); err != nil {
_AddInternalServerError(ww, fmt.Sprintf("AdminGetNFTDrop: Problem serializing object to JSON: %v", err))
return
}
}
type AdminUpdateNFTDropRequest struct {
DropNumber int `safeForLogging:"true"`
DropTstampNanos int `safeForLogging:"true"`
IsActive bool `safeForLogging:"true"`
NFTHashHexToAdd string `safeForLogging:"true"`
NFTHashHexToRemove string `safeForLogging:"true"`
}
type AdminUpdateNFTDropResponse struct {
DropEntry *NFTDropEntry
Posts []*PostEntryResponse
}
func (fes *APIServer) AdminUpdateNFTDrop(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := AdminUpdateNFTDropRequest{}
err := decoder.Decode(&requestData)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateNFTDrop: Error parsing request body: %v", err))
return
}
if requestData.DropNumber < 1 {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminUpdateNFTDrop: Drop number must be greater than zero, received: %d", requestData.DropNumber))
return
}
if requestData.DropTstampNanos < 0 {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminUpdateNFTDrop: Drop timestamp cannot be negative, received: %d", requestData.DropTstampNanos))
return
}
if requestData.NFTHashHexToAdd != "" && requestData.NFTHashHexToRemove != "" {
_AddBadRequestError(ww, fmt.Sprint(
"AdminUpdateNFTDrop: Cannot add and remove an NFT in the same operation."))
return
}
var latestDropEntry *NFTDropEntry
latestDropEntry, err = fes.GetLatestNFTDropEntry()
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateNFTDrop: Error getting latest drop: %v", err))
return
}
// Now for the business.
var updatedDropEntry *NFTDropEntry
currentTime := uint64(time.Now().UnixNano())
if uint64(requestData.DropNumber) > latestDropEntry.DropNumber {
// If we make it here, we are making a new drop. Run some checks to make sure that the
// timestamp provided make sense.
if latestDropEntry.DropTstampNanos > currentTime {
_AddBadRequestError(ww, fmt.Sprint(
"AdminUpdateNFTDrop: Cannot create a new drop when one is already pending."))
return
}
if uint64(requestData.DropTstampNanos) < currentTime {
_AddBadRequestError(ww, fmt.Sprint(
"AdminUpdateNFTDrop: Cannot create a new drop with a tstamp in the past."))
return
}
if uint64(requestData.DropTstampNanos) < latestDropEntry.DropTstampNanos {
_AddBadRequestError(ww, fmt.Sprint(
"AdminUpdateNFTDrop: Cannot create a new drop with a tstamp before the previous drop."))
return
}
// Regardless of the drop number provided, we force the new drop to be the previous number + 1.
updatedDropEntry = &NFTDropEntry{
DropNumber: uint64(latestDropEntry.DropNumber + 1),
DropTstampNanos: uint64(requestData.DropTstampNanos),
}
} else {
// In this case, we are updating an existing drop.
updatedDropEntry = latestDropEntry
if uint64(requestData.DropNumber) != latestDropEntry.DropNumber {
updatedDropEntry, err = fes.GetNFTDropEntry(uint64(requestData.DropNumber))
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminUpdateNFTDrop: Error getting drop #%d: %v", requestData.DropNumber, err))
return
}
}
// There are only two possible drops that can be updated (you can't update past drops):
// - The current "active" drop.
// - The next "pending" drop.
canUpdateDrop := false
latestDropIsPending := latestDropEntry.DropTstampNanos > currentTime
if latestDropIsPending && uint64(requestData.DropNumber) >= latestDropEntry.DropNumber-1 {
// In this case their is a pending drop so the latest drop and the previous drop are editable.
canUpdateDrop = true
} else if !latestDropIsPending && uint64(requestData.DropNumber) == latestDropEntry.DropNumber {
// In this case there is no pending drop so you can only update the latest drop.
canUpdateDrop = true
}
if !canUpdateDrop {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminUpdateNFTDrop: Cannot edit past drop #%d.", requestData.DropNumber))
return
}
// Update IsActive.
updatedDropEntry.IsActive = requestData.IsActive
// Consider updating DropTstampNanos.
if uint64(requestData.DropTstampNanos) > currentTime &&
uint64(requestData.DropNumber) == latestDropEntry.DropNumber {
updatedDropEntry.DropTstampNanos = uint64(requestData.DropTstampNanos)
} else if uint64(requestData.DropTstampNanos) != updatedDropEntry.DropTstampNanos {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminUpdateNFTDrop: Can only update latest drop with tstamp in the future."))
return
}
utxoView, err := fes.backendServer.GetMempool().GetAugmentedUniversalView()
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateNFTDrop: Error getting utxoView: %v", err))
return
}
// Add new NFT hashes.
if requestData.NFTHashHexToAdd != "" {
// Decode the hash and make sure it is a valid NFT so that we can add it to the entry.
postHash, err := GetPostHashFromPostHashHex(requestData.NFTHashHexToAdd)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateNFTDrop: Error getting post hash: %v", err))
return
}
postEntry := utxoView.GetPostEntryForPostHash(postHash)
if !postEntry.IsNFT {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminUpdateNFTDrop: Cannot add non-NFT to drop: %v", postHash.String()))
return
}
updatedDropEntry.NFTHashes = append(updatedDropEntry.NFTHashes, postHash)
}
// Remove unwanted NFT hashes.
if requestData.NFTHashHexToRemove != "" {
// Decode the hash and make sure it is a valid NFT.
nftHashToRemove, err := GetPostHashFromPostHashHex(requestData.NFTHashHexToRemove)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf(
"AdminUpdateNFTDrop: Error getting post hash to remove: %v", err))
return
}
for nftHashIdx, nftHash := range updatedDropEntry.NFTHashes {
if reflect.DeepEqual(nftHash, nftHashToRemove) {
updatedDropEntry.NFTHashes = append(
updatedDropEntry.NFTHashes[:nftHashIdx], updatedDropEntry.NFTHashes[nftHashIdx+1:]...)
break
}
}
}
}
// Set the updated drop entry.
globalStateKey := GlobalStateKeyForNFTDropEntry(uint64(requestData.DropNumber))
updatedDropEntryBuf := bytes.NewBuffer([]byte{})
gob.NewEncoder(updatedDropEntryBuf).Encode(updatedDropEntry)
err = fes.GlobalState.Put(globalStateKey, updatedDropEntryBuf.Bytes())
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminUpdateNFTDrop: Error encoding updated drop: %v", err))
return
}
// Note that "dropEntryToReturn" can be nil if there are no entries in global state.
postEntryResponses, err := fes.GetPostsForNFTDropEntry(updatedDropEntry)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminGetNFTDrop: : %v", err))
return
}
// Return all the data associated with the transaction in the response
res := AdminUpdateNFTDropResponse{
DropEntry: updatedDropEntry,
Posts: postEntryResponses,
}
if err = json.NewEncoder(ww).Encode(res); err != nil {
_AddInternalServerError(ww, fmt.Sprintf("AdminUpdateNFTDrop: Problem serializing object to JSON: %v", err))
return
}
}