/
block_view_post.go
1263 lines (1095 loc) · 46.7 KB
/
block_view_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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package lib
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/davecgh/go-spew/spew"
"github.com/dgraph-io/badger/v3"
"github.com/golang/glog"
"github.com/pkg/errors"
"math"
"reflect"
"sort"
)
func (bav *UtxoView) _getRepostEntryForRepostKey(repostKey *RepostKey) *RepostEntry {
// If an entry exists in the in-memory map, return the value of that mapping.
mapValue, existsMapValue := bav.RepostKeyToRepostEntry[*repostKey]
if existsMapValue {
return mapValue
}
// If we get here it means no value exists in our in-memory map. In this case,
// defer to the db. If a mapping exists in the db, return it. If not, return
// nil. Either way, save the value to the in-memory view mapping got later.
repostEntry := DbReposterPubKeyRepostedPostHashToRepostEntry(bav.Handle, bav.Snapshot,
repostKey.ReposterPubKey[:], repostKey.RepostedPostHash)
if repostEntry != nil {
bav._setRepostEntryMappings(repostEntry)
}
return repostEntry
}
func (bav *UtxoView) _setRepostEntryMappings(repostEntry *RepostEntry) {
// This function shouldn't be called with nil.
if repostEntry == nil {
glog.Errorf("_setRepostEntryMappings: Called with nil RepostEntry; " +
"this should never happen.")
return
}
repostKey := MakeRepostKey(repostEntry.ReposterPubKey, *repostEntry.RepostedPostHash)
bav.RepostKeyToRepostEntry[repostKey] = repostEntry
}
func (bav *UtxoView) _deleteRepostEntryMappings(repostEntry *RepostEntry) {
if repostEntry == nil {
glog.Errorf("_deleteRepostEntryMappings: called with nil RepostEntry; " +
"this should never happen")
return
}
// Create a tombstone entry.
tombstoneRepostEntry := *repostEntry
tombstoneRepostEntry.isDeleted = true
// Set the mappings to point to the tombstone entry.
bav._setRepostEntryMappings(&tombstoneRepostEntry)
}
func (bav *UtxoView) _setDiamondEntryMappings(diamondEntry *DiamondEntry) {
// This function shouldn't be called with nil.
if diamondEntry == nil {
glog.Errorf("_setDiamondEntryMappings: Called with nil DiamondEntry; " +
"this should never happen.")
return
}
diamondKey := MakeDiamondKey(
diamondEntry.SenderPKID, diamondEntry.ReceiverPKID, diamondEntry.DiamondPostHash)
bav.DiamondKeyToDiamondEntry[diamondKey] = diamondEntry
}
func (bav *UtxoView) _deleteDiamondEntryMappings(diamondEntry *DiamondEntry) {
// Create a tombstone entry.
tombstoneDiamondEntry := *diamondEntry
tombstoneDiamondEntry.isDeleted = true
// Set the mappings to point to the tombstone entry.
bav._setDiamondEntryMappings(&tombstoneDiamondEntry)
}
func (bav *UtxoView) GetDiamondEntryForDiamondKey(diamondKey *DiamondKey) *DiamondEntry {
// If an entry exists in the in-memory map, return the value of that mapping.
bavDiamondEntry, existsMapValue := bav.DiamondKeyToDiamondEntry[*diamondKey]
if existsMapValue {
return bavDiamondEntry
}
// If we get here it means no value exists in our in-memory map. In this case,
// defer to the db. If a mapping exists in the db, return it. If not, return
// nil.
var diamondEntry *DiamondEntry
if bav.Postgres != nil {
diamond := bav.Postgres.GetDiamond(&diamondKey.SenderPKID, &diamondKey.ReceiverPKID, &diamondKey.DiamondPostHash)
if diamond != nil {
diamondEntry = &DiamondEntry{
SenderPKID: diamond.SenderPKID,
ReceiverPKID: diamond.ReceiverPKID,
DiamondPostHash: diamond.DiamondPostHash,
DiamondLevel: int64(diamond.DiamondLevel),
}
}
} else {
diamondEntry = DbGetDiamondMappings(bav.Handle, bav.Snapshot,
&diamondKey.ReceiverPKID, &diamondKey.SenderPKID, &diamondKey.DiamondPostHash)
}
if diamondEntry != nil {
bav._setDiamondEntryMappings(diamondEntry)
}
return diamondEntry
}
func (bav *UtxoView) GetPostEntryForPostHash(postHash *BlockHash) *PostEntry {
// If an entry exists in the in-memory map, return the value of that mapping.
mapValue, existsMapValue := bav.PostHashToPostEntry[*postHash]
if existsMapValue {
return mapValue
}
// If we get here it means no value exists in our in-memory map. In this case,
// defer to the db. If a mapping exists in the db, return it. If not, return
// nil.
if bav.Postgres != nil {
post := bav.Postgres.GetPost(postHash)
if post != nil {
return bav.setPostMappings(post)
}
return nil
} else {
dbPostEntry := DBGetPostEntryByPostHash(bav.Handle, bav.Snapshot, postHash)
if dbPostEntry != nil {
bav._setPostEntryMappings(dbPostEntry)
}
return dbPostEntry
}
}
func (bav *UtxoView) GetDiamondEntryMapForPublicKey(publicKey []byte, fetchYouDiamonded bool,
) (_pkidToDiamondsMap map[PKID][]*DiamondEntry, _err error) {
pkidEntry := bav.GetPKIDForPublicKey(publicKey)
dbPKIDToDiamondsMap, err := DbGetPKIDsThatDiamondedYouMap(bav.Handle, pkidEntry.PKID, fetchYouDiamonded)
if err != nil {
return nil, errors.Wrapf(err, "GetDiamondEntryMapForPublicKey: Error Getting "+
"PKIDs that diamonded you map from the DB.")
}
// Load all of the diamondEntries into the view.
for _, diamondEntryList := range dbPKIDToDiamondsMap {
for _, diamondEntry := range diamondEntryList {
diamondKey := &DiamondKey{
SenderPKID: *diamondEntry.SenderPKID,
ReceiverPKID: *diamondEntry.ReceiverPKID,
DiamondPostHash: *diamondEntry.DiamondPostHash,
}
// If the diamond key is not in the view, add it to the view.
if _, ok := bav.DiamondKeyToDiamondEntry[*diamondKey]; !ok {
bav._setDiamondEntryMappings(diamondEntry)
}
}
}
// Iterate over all the diamondEntries in the view and build the final map.
pkidToDiamondsMap := make(map[PKID][]*DiamondEntry)
for _, diamondEntry := range bav.DiamondKeyToDiamondEntry {
if diamondEntry.isDeleted {
continue
}
// Make sure the diamondEntry we are looking at is for the correct receiver public key.
if !fetchYouDiamonded && reflect.DeepEqual(diamondEntry.ReceiverPKID, pkidEntry.PKID) {
pkidToDiamondsMap[*diamondEntry.SenderPKID] = append(
pkidToDiamondsMap[*diamondEntry.SenderPKID], diamondEntry)
}
// Make sure the diamondEntry we are looking at is for the correct sender public key.
if fetchYouDiamonded && reflect.DeepEqual(diamondEntry.SenderPKID, pkidEntry.PKID) {
pkidToDiamondsMap[*diamondEntry.ReceiverPKID] = append(
pkidToDiamondsMap[*diamondEntry.ReceiverPKID], diamondEntry)
}
}
return pkidToDiamondsMap, nil
}
func (bav *UtxoView) GetDiamondEntriesForSenderToReceiver(receiverPublicKey []byte, senderPublicKey []byte,
) (_diamondEntries []*DiamondEntry, _err error) {
receiverPKIDEntry := bav.GetPKIDForPublicKey(receiverPublicKey)
senderPKIDEntry := bav.GetPKIDForPublicKey(senderPublicKey)
dbDiamondEntries, err := DbGetDiamondEntriesForSenderToReceiver(bav.Handle, receiverPKIDEntry.PKID, senderPKIDEntry.PKID)
if err != nil {
return nil, errors.Wrapf(err, "GetDiamondEntriesForGiverToReceiver: Error getting diamond entries from DB.")
}
// Load all of the diamondEntries into the view
for _, diamondEntry := range dbDiamondEntries {
diamondKey := &DiamondKey{
SenderPKID: *diamondEntry.SenderPKID,
ReceiverPKID: *diamondEntry.ReceiverPKID,
DiamondPostHash: *diamondEntry.DiamondPostHash,
}
// If the diamond key is not in the view, add it to the view.
if _, ok := bav.DiamondKeyToDiamondEntry[*diamondKey]; !ok {
bav._setDiamondEntryMappings(diamondEntry)
}
}
var diamondEntries []*DiamondEntry
for _, diamondEntry := range bav.DiamondKeyToDiamondEntry {
if diamondEntry.isDeleted {
continue
}
// Make sure the diamondEntry we are looking at is for the correct sender and receiver pair
if reflect.DeepEqual(diamondEntry.ReceiverPKID, receiverPKIDEntry.PKID) &&
reflect.DeepEqual(diamondEntry.SenderPKID, senderPKIDEntry.PKID) {
diamondEntries = append(diamondEntries, diamondEntry)
}
}
return diamondEntries, nil
}
func (bav *UtxoView) _setPostEntryMappings(postEntry *PostEntry) {
// This function shouldn't be called with nil.
if postEntry == nil {
glog.Errorf("_setPostEntryMappings: Called with nil PostEntry; this should never happen.")
return
}
// Add a mapping for the post.
bav.PostHashToPostEntry[*postEntry.PostHash] = postEntry
}
func (bav *UtxoView) _deletePostEntryMappings(postEntry *PostEntry) {
// Create a tombstone entry.
tombstonePostEntry := *postEntry
tombstonePostEntry.isDeleted = true
// Set the mappings to point to the tombstone entry.
bav._setPostEntryMappings(&tombstonePostEntry)
}
func (bav *UtxoView) setPostMappings(post *PGPost) *PostEntry {
postEntry := post.NewPostEntry()
// Add a mapping for the post.
bav.PostHashToPostEntry[*post.PostHash] = postEntry
return postEntry
}
func (bav *UtxoView) GetPostEntryReaderState(
readerPK []byte, postEntry *PostEntry) *PostEntryReaderState {
postEntryReaderState := &PostEntryReaderState{}
// Get like state.
postEntryReaderState.LikedByReader = bav.GetLikedByReader(readerPK, postEntry.PostHash)
// Get repost state.
postEntryReaderState.RepostPostHashHex, postEntryReaderState.RepostedByReader = bav.GetRepostPostEntryStateForReader(readerPK, postEntry.PostHash)
// Get diamond state.
senderPKID := bav.GetPKIDForPublicKey(readerPK)
receiverPKID := bav.GetPKIDForPublicKey(postEntry.PosterPublicKey)
if senderPKID == nil || receiverPKID == nil {
glog.V(2).Infof(
"GetPostEntryReaderState: Could not find PKID for reader PK: %s or poster PK: %s",
PkToString(readerPK, bav.Params), PkToString(postEntry.PosterPublicKey, bav.Params))
} else {
diamondKey := MakeDiamondKey(senderPKID.PKID, receiverPKID.PKID, postEntry.PostHash)
diamondEntry := bav.GetDiamondEntryForDiamondKey(&diamondKey)
if diamondEntry != nil {
postEntryReaderState.DiamondLevelBestowed = diamondEntry.DiamondLevel
}
}
return postEntryReaderState
}
func (bav *UtxoView) GetRepostPostEntryStateForReader(readerPK []byte, postHash *BlockHash) (string, bool) {
repostKey := MakeRepostKey(readerPK, *postHash)
repostEntry := bav._getRepostEntryForRepostKey(&repostKey)
if repostEntry == nil {
return "", false
}
repostPostEntry := bav.GetPostEntryForPostHash(repostEntry.RepostPostHash)
if repostPostEntry == nil {
glog.Errorf("Could not find repost post entry from post hash: %v", repostEntry.RepostedPostHash)
return "", false
}
// We include the PostHashHex of this user's post that reposts the current post to
// handle undo-ing (AKA hiding) a repost.
// If the user's repost of this post is hidden, we set RepostedByReader to false.
return hex.EncodeToString(repostEntry.RepostPostHash[:]), !repostPostEntry.IsHidden
}
func (bav *UtxoView) GetCommentEntriesForParentStakeID(parentStakeID []byte) ([]*PostEntry, error) {
if bav.Postgres != nil {
posts := bav.Postgres.GetComments(NewBlockHash(parentStakeID))
for _, post := range posts {
bav.setPostMappings(post)
}
} else {
_, dbCommentHashes, _, err := DBGetCommentPostHashesForParentStakeID(
bav.Handle, bav.Snapshot, parentStakeID, false)
if err != nil {
return nil, errors.Wrapf(err, "GetCommentEntriesForParentStakeID: Problem fetching comments: %v", err)
}
// Load comment hashes into the view.
for _, commentHash := range dbCommentHashes {
bav.GetPostEntryForPostHash(commentHash)
}
}
commentEntries := []*PostEntry{}
for _, postEntry := range bav.PostHashToPostEntry {
// Ignore deleted or rolled-back posts.
if postEntry.isDeleted {
continue
}
if len(postEntry.ParentStakeID) == 0 || !reflect.DeepEqual(postEntry.ParentStakeID, parentStakeID) {
continue // Skip posts that are not comments on the given parentStakeID.
} else {
// Add the comment to our map.
commentEntries = append(commentEntries, postEntry)
}
}
return commentEntries, nil
}
// Accepts a postEntry and returns as many parent posts as it can find up to maxDepth.
// This function never returns an error, only an empty list if it hits a non-post parentStakeID.
// If "rootFirst" is passed, the root of the tree will be returned first, not the 1st parent.
// _truncatedTree is a flag that is true when the root post was not reached before the maxDepth was hit.
func (bav *UtxoView) GetParentPostEntriesForPostEntry(postEntry *PostEntry, maxDepth uint32, rootFirst bool,
) (_parentPostEntries []*PostEntry, _truncatedTree bool) {
parentStakeID := postEntry.ParentStakeID
parentPostEntries := []*PostEntry{}
// If the post passed has no parent or isn't a post, we return the empty list.
if len(parentStakeID) != HashSizeBytes {
return parentPostEntries, false
}
iterations := uint32(0)
for len(parentStakeID) == HashSizeBytes && iterations < maxDepth {
parentPostHash := &BlockHash{}
copy(parentPostHash[:], parentStakeID)
parentPostEntry := bav.GetPostEntryForPostHash(parentPostHash)
if postEntry == nil {
break
}
if rootFirst {
parentPostEntries = append([]*PostEntry{parentPostEntry}, parentPostEntries...)
} else {
parentPostEntries = append(parentPostEntries, parentPostEntry)
}
// Set up the next iteration of the loop.
parentStakeID = parentPostEntry.ParentStakeID
iterations += 1
}
return parentPostEntries, iterations >= maxDepth
}
// Just fetch all the posts from the db and join them with all the posts
// in the mempool. Then sort them by their timestamp. This can be called
// on an empty view or a view that already has a lot of transactions
// applied to it.
func (bav *UtxoView) GetAllPosts() (_corePosts []*PostEntry, _commentsByPostHash map[BlockHash][]*PostEntry, _err error) {
// Start by fetching all the posts we have in the db.
//
// TODO(performance): This currently fetches all posts. We should implement
// some kind of pagination instead though.
_, _, dbPostEntries, err := DBGetAllPostsByTstamp(bav.Handle, bav.Snapshot, true)
if err != nil {
return nil, nil, errors.Wrapf(err, "GetAllPosts: Problem fetching PostEntry's from db: ")
}
// Iterate through the entries found in the db and force the view to load them.
// This fills in any gaps in the view so that, after this, the view should contain
// the union of what it had before plus what was in the db.
for _, dbPostEntry := range dbPostEntries {
bav.GetPostEntryForPostHash(dbPostEntry.PostHash)
}
// Do one more pass to load all the comments from the DB.
for _, postEntry := range bav.PostHashToPostEntry {
// Ignore deleted or rolled-back posts.
if postEntry.isDeleted {
continue
}
// If we have a post in the view and if that post is not a comment
// then fetch its attached comments from the db. We need to do this
// because the tstamp index above only fetches "core" posts not
// comments.
if len(postEntry.ParentStakeID) == 0 {
_, dbCommentHashes, _, err := DBGetCommentPostHashesForParentStakeID(
bav.Handle, bav.Snapshot, postEntry.ParentStakeID, false)
if err != nil {
return nil, nil, errors.Wrapf(err, "GetAllPosts: Problem fetching comment PostEntry's from db: ")
}
for _, commentHash := range dbCommentHashes {
bav.GetPostEntryForPostHash(commentHash)
}
}
}
allCorePosts := []*PostEntry{}
commentsByPostHash := make(map[BlockHash][]*PostEntry)
for _, postEntry := range bav.PostHashToPostEntry {
// Ignore deleted or rolled-back posts.
if postEntry.isDeleted {
continue
}
// Every post is either a core post or a comment. If it has a stake ID
// its a comment, and if it doesn't then it's a core post.
if len(postEntry.ParentStakeID) == 0 {
allCorePosts = append(allCorePosts, postEntry)
} else {
// Add the comment to our map.
commentsForPost := commentsByPostHash[*NewBlockHash(postEntry.ParentStakeID)]
commentsForPost = append(commentsForPost, postEntry)
commentsByPostHash[*NewBlockHash(postEntry.ParentStakeID)] = commentsForPost
}
}
// Sort all the comment lists as well. Here we put the latest comment at the
// end.
for _, commentList := range commentsByPostHash {
sort.Slice(commentList, func(ii, jj int) bool {
return commentList[ii].TimestampNanos < commentList[jj].TimestampNanos
})
}
return allCorePosts, commentsByPostHash, nil
}
func (bav *UtxoView) GetPostsPaginatedForPublicKeyOrderedByTimestamp(publicKey []byte, startPostHash *BlockHash, limit uint64, mediaRequired bool, onlyNFTs bool, onlyPosts bool) (_posts []*PostEntry, _err error) {
if onlyNFTs && onlyPosts {
return nil, fmt.Errorf("GetPostsPaginatedForPublicKeyOrderedByTimestamp: onlyNFTs and onlyPosts can not be enabled both")
}
if bav.Postgres != nil {
var startTime uint64 = math.MaxUint64
if startPostHash != nil {
startPostEntry := bav.GetPostEntryForPostHash(startPostHash)
startTime = startPostEntry.TimestampNanos
}
posts := bav.Postgres.GetPostsForPublicKey(publicKey, startTime, limit)
for _, post := range posts {
// TODO: Normalize this field so we get the correct number of results from the DB
if mediaRequired && !post.HasMedia() {
continue
}
// onlyNFTs set to determine if we only want posts that are NFTs
if onlyNFTs && !post.NFT {
continue
}
if onlyPosts && post.NFT {
continue
}
bav.setPostMappings(post)
}
} else {
handle := bav.Handle
// FIXME: Db operation like this shouldn't happen in utxoview.
dbPrefix := append([]byte{}, Prefixes.PrefixPosterPublicKeyTimestampPostHash...)
dbPrefix = append(dbPrefix, publicKey...)
var prefix []byte
if startPostHash != nil {
startPostEntry := bav.GetPostEntryForPostHash(startPostHash)
if startPostEntry == nil {
return nil, fmt.Errorf("GetPostsPaginatedForPublicKeyOrderedByTimestamp: Invalid start post hash")
}
prefix = append(dbPrefix, EncodeUint64(startPostEntry.TimestampNanos)...)
prefix = append(prefix, startPostEntry.PostHash[:]...)
} else {
maxBigEndianUint64Bytes := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
prefix = append(dbPrefix, maxBigEndianUint64Bytes...)
}
timestampSizeBytes := 8
var posts []*PostEntry
err := handle.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
// Go in reverse order
opts.Reverse = true
it := txn.NewIterator(opts)
defer it.Close()
it.Seek(prefix)
if startPostHash != nil {
// Skip the first post if we have a startPostHash.
it.Next()
}
for ; it.ValidForPrefix(dbPrefix) && uint64(len(posts)) < limit; it.Next() {
rawKey := it.Item().Key()
keyWithoutPrefix := rawKey[1:]
//posterPublicKey := keyWithoutPrefix[:HashSizeBytes]
publicKeySizeBytes := HashSizeBytes + 1
//tstampNanos := DecodeUint64(keyWithoutPrefix[publicKeySizeBytes:(publicKeySizeBytes + timestampSizeBytes)])
postHash := &BlockHash{}
copy(postHash[:], keyWithoutPrefix[(publicKeySizeBytes+timestampSizeBytes):])
postEntry := bav.GetPostEntryForPostHash(postHash)
if postEntry == nil {
return fmt.Errorf("Missing post entry")
}
if postEntry.isDeleted || postEntry.ParentStakeID != nil || postEntry.IsHidden {
continue
}
// mediaRequired set to determine if we only want posts that include media and ignore posts without
if mediaRequired && !postEntry.HasMedia() {
continue
}
// onlyNFTs set to determine if we only want posts that are NFTs
if onlyNFTs && !postEntry.IsNFT {
continue
}
if onlyPosts && postEntry.IsNFT {
continue
}
posts = append(posts, postEntry)
}
return nil
})
if err != nil {
return nil, err
}
}
var postEntries []*PostEntry
// Iterate over the view. Put all posts authored by the public key into our mempool posts slice
for _, postEntry := range bav.PostHashToPostEntry {
// Ignore deleted or hidden posts and any comments.
if postEntry.isDeleted || postEntry.IsHidden || len(postEntry.ParentStakeID) != 0 {
continue
}
// mediaRequired set to determine if we only want posts that include media and ignore posts without
if mediaRequired && !postEntry.HasMedia() {
continue
}
// onlyNFTs set to determine if we only want posts that are NFTs
if onlyNFTs && !postEntry.IsNFT {
continue
}
if onlyPosts && postEntry.IsNFT {
continue
}
if reflect.DeepEqual(postEntry.PosterPublicKey, publicKey) {
postEntries = append(postEntries, postEntry)
}
}
return postEntries, nil
}
func (bav *UtxoView) GetDiamondSendersForPostHash(postHash *BlockHash) (_pkidToDiamondLevel map[PKID]int64, _err error) {
handle := bav.Handle
// FIXME: Db operation like this shouldn't happen in utxoview.
dbPrefix := append([]byte{}, Prefixes.PrefixDiamondedPostHashDiamonderPKIDDiamondLevel...)
dbPrefix = append(dbPrefix, postHash[:]...)
keysFound, _ := EnumerateKeysForPrefix(handle, dbPrefix)
diamondPostEntry := bav.GetPostEntryForPostHash(postHash)
receiverPKIDEntry := bav.GetPKIDForPublicKey(diamondPostEntry.PosterPublicKey)
// Iterate over all the db keys & values and load them into the view.
expectedKeyLength := 1 + HashSizeBytes + btcec.PubKeyBytesLenCompressed + 8
for _, key := range keysFound {
// Sanity check that this is a reasonable key.
if len(key) != expectedKeyLength {
return nil, fmt.Errorf("UtxoView.GetDiamondsForPostHash: Invalid key length found: %d", len(key))
}
senderPKID := &PKID{}
copy(senderPKID[:], key[1+HashSizeBytes:])
diamondKey := &DiamondKey{
SenderPKID: *senderPKID,
ReceiverPKID: *receiverPKIDEntry.PKID,
DiamondPostHash: *postHash,
}
bav.GetDiamondEntryForDiamondKey(diamondKey)
}
// Iterate over the view and create the final map to return.
pkidToDiamondLevel := make(map[PKID]int64)
for _, diamondEntry := range bav.DiamondKeyToDiamondEntry {
if !diamondEntry.isDeleted && reflect.DeepEqual(diamondEntry.DiamondPostHash[:], postHash[:]) {
pkidToDiamondLevel[*diamondEntry.SenderPKID] = diamondEntry.DiamondLevel
}
}
return pkidToDiamondLevel, nil
}
func (bav *UtxoView) GetRepostsForPostHash(postHash *BlockHash) (_reposterPubKeys [][]byte, _err error) {
handle := bav.Handle
// FIXME: Db operation like this shouldn't happen in utxoview.
dbPrefix := append([]byte{}, Prefixes.PrefixRepostedPostHashReposterPubKey...)
dbPrefix = append(dbPrefix, postHash[:]...)
keysFound, _ := EnumerateKeysForPrefix(handle, dbPrefix)
// Iterate over all the db keys & values and load them into the view.
expectedKeyLength := 1 + HashSizeBytes + btcec.PubKeyBytesLenCompressed
for _, key := range keysFound {
// Sanity check that this is a reasonable key.
if len(key) != expectedKeyLength {
return nil, fmt.Errorf("UtxoView.GetRepostersForPostHash: Invalid key length found: %d", len(key))
}
reposterPubKey := key[1+HashSizeBytes:]
repostKey := &RepostKey{
ReposterPubKey: MakePkMapKey(reposterPubKey),
RepostedPostHash: *postHash,
}
bav._getRepostEntryForRepostKey(repostKey)
}
// Iterate over the view and create the final list to return.
reposterPubKeys := [][]byte{}
for _, repostEntry := range bav.RepostKeyToRepostEntry {
if !repostEntry.isDeleted && reflect.DeepEqual(repostEntry.RepostedPostHash[:], postHash[:]) {
reposterPubKeys = append(reposterPubKeys, repostEntry.ReposterPubKey)
}
}
return reposterPubKeys, nil
}
func (bav *UtxoView) GetQuoteRepostsForPostHash(postHash *BlockHash,
) (_quoteReposterPubKeys [][]byte, _quoteReposterPubKeyToPosts map[PkMapKey][]*PostEntry, _err error) {
handle := bav.Handle
// FIXME: Db operation like this shouldn't happen in utxoview.
dbPrefix := append([]byte{}, Prefixes.PrefixRepostedPostHashReposterPubKeyRepostPostHash...)
dbPrefix = append(dbPrefix, postHash[:]...)
keysFound, _ := EnumerateKeysForPrefix(handle, dbPrefix)
// Iterate over all the db keys & values and load them into the view.
expectedKeyLength := 1 + HashSizeBytes + btcec.PubKeyBytesLenCompressed + HashSizeBytes
repostPostHashIdx := 1 + HashSizeBytes + btcec.PubKeyBytesLenCompressed
for _, key := range keysFound {
// Sanity check that this is a reasonable key.
if len(key) != expectedKeyLength {
return nil, nil, fmt.Errorf("UtxoView.GetQuoteRepostsForPostHash: Invalid key length found: %d", len(key))
}
repostPostHash := &BlockHash{}
copy(repostPostHash[:], key[repostPostHashIdx:])
bav.GetPostEntryForPostHash(repostPostHash)
}
// Iterate over the view and create the final map to return.
quoteReposterPubKeys := [][]byte{}
quoteReposterPubKeyToPosts := make(map[PkMapKey][]*PostEntry)
for _, postEntry := range bav.PostHashToPostEntry {
if !postEntry.isDeleted && postEntry.IsQuotedRepost && reflect.DeepEqual(postEntry.RepostedPostHash[:], postHash[:]) {
quoteReposterPubKeys = append(quoteReposterPubKeys, postEntry.PosterPublicKey)
quoteRepostPosts, _ := quoteReposterPubKeyToPosts[MakePkMapKey(postEntry.PosterPublicKey)]
quoteRepostPosts = append(quoteRepostPosts, postEntry)
quoteReposterPubKeyToPosts[MakePkMapKey(postEntry.PosterPublicKey)] = quoteRepostPosts
}
}
return quoteReposterPubKeys, quoteReposterPubKeyToPosts, nil
}
func (bav *UtxoView) _connectSubmitPost(
txn *MsgDeSoTxn, txHash *BlockHash, blockHeight uint32,
verifySignatures bool, ignoreUtxos bool) (
_totalInput uint64, _totalOutput uint64, _utxoOps []*UtxoOperation, _err error) {
// Check that the transaction has the right TxnType.
if txn.TxnMeta.GetTxnType() != TxnTypeSubmitPost {
return 0, 0, nil, fmt.Errorf("_connectSubmitPost: called with bad TxnType %s",
txn.TxnMeta.GetTxnType().String())
}
txMeta := txn.TxnMeta.(*SubmitPostMetadata)
// Connect basic txn to get the total input and the total output without
// considering the transaction metadata.
//
// The ignoreUtxos flag is used to connect "seed" transactions when initializing
// the blockchain. It allows us to "seed" the database with posts and profiles
// when we do a hard fork, without having the transactions rejected due to their
// not being spendable.
var totalInput, totalOutput uint64
var utxoOpsForTxn = []*UtxoOperation{}
var err error
if !ignoreUtxos {
totalInput, totalOutput, utxoOpsForTxn, err = bav._connectBasicTransfer(
txn, txHash, blockHeight, verifySignatures)
if err != nil {
return 0, 0, nil, errors.Wrapf(err, "_connectSubmitPost: ")
}
// Force the input to be non-zero so that we can prevent replay attacks.
if totalInput == 0 {
return 0, 0, nil, RuleErrorSubmitPostRequiresNonZeroInput
}
}
// Transaction extra data contains both consensus-related, such as repost info, and additional information about a post,
// whereas PostExtraData is an attribute of a PostEntry that contains only non-consensus related
// information about a post, such as a link to a video that is embedded.
extraData := make(map[string][]byte)
for k, v := range txn.ExtraData {
extraData[k] = v
}
// Set the IsQuotedRepost attribute of postEntry based on extra data
isQuotedRepost := false
if quotedRepost, hasQuotedRepost := extraData[IsQuotedRepostKey]; hasQuotedRepost {
if reflect.DeepEqual(quotedRepost, QuotedRepostVal) {
isQuotedRepost = true
}
// Delete key since it is not needed in the PostExtraData map as IsQuotedRepost is involved in consensus code.
delete(extraData, IsQuotedRepostKey)
}
var repostedPostHash *BlockHash
if repostedPostHashBytes, isRepost := extraData[RepostedPostHash]; isRepost {
repostedPostHash = &BlockHash{}
copy(repostedPostHash[:], repostedPostHashBytes)
delete(extraData, RepostedPostHash)
}
isFrozen := false
if blockHeight >= bav.Params.ForkHeights.AssociationsAndAccessGroupsBlockHeight {
if extraDataIsFrozen, exists := extraData[IsFrozenKey]; exists {
isFrozen = bytes.Equal(extraDataIsFrozen, IsFrozenPostVal)
delete(extraData, IsFrozenKey)
}
}
// At this point the inputs and outputs have been processed. Now we
// need to handle the metadata.
// If the metadata has a PostHashToModify then treat it as modifying an
// existing post rather than creating a new post.
var prevPostEntry *PostEntry
var prevParentPostEntry *PostEntry
var prevGrandparentPostEntry *PostEntry
var prevRepostedPostEntry *PostEntry
var prevRepostEntry *RepostEntry
var newPostEntry *PostEntry
var newParentPostEntry *PostEntry
var newGrandparentPostEntry *PostEntry
var newRepostedPostEntry *PostEntry
var newRepostEntry *RepostEntry
if len(txMeta.PostHashToModify) != 0 {
// Make sure the post hash is valid
if len(txMeta.PostHashToModify) != HashSizeBytes {
return 0, 0, nil, errors.Wrapf(
RuleErrorSubmitPostInvalidPostHashToModify,
"_connectSubmitPost: Bad post hash: %#v", txMeta.PostHashToModify)
}
// Get the existing post entry, which must exist and be undeleted.
postHash := &BlockHash{}
copy(postHash[:], txMeta.PostHashToModify[:])
existingPostEntryy := bav.GetPostEntryForPostHash(postHash)
if existingPostEntryy == nil || existingPostEntryy.isDeleted {
return 0, 0, nil, errors.Wrapf(
RuleErrorSubmitPostModifyingNonexistentPost,
"_connectSubmitPost: Post hash: %v", postHash)
}
// Post modification is only allowed by the original poster.
if !reflect.DeepEqual(txn.PublicKey, existingPostEntryy.PosterPublicKey) {
return 0, 0, nil, errors.Wrapf(
RuleErrorSubmitPostPostModificationNotAuthorized,
"_connectSubmitPost: Post hash: %v, poster public key: %v, "+
"txn public key: %v, paramUpdater: %v", postHash,
PkToStringBoth(existingPostEntryy.PosterPublicKey),
PkToStringBoth(txn.PublicKey), spew.Sdump(GetParamUpdaterPublicKeys(blockHeight, bav.Params)))
}
if blockHeight >= bav.Params.ForkHeights.AssociationsAndAccessGroupsBlockHeight {
// Modification of a frozen post is not allowed after the above block height.
if existingPostEntryy.IsFrozen {
return 0, 0, nil, errors.Wrapf(RuleErrorSubmitPostModifyingFrozenPost, "_connectSubmitPost: ")
}
} else {
// Modification of an NFT is not allowed before the above block height.
if existingPostEntryy.IsNFT {
return 0, 0, nil, errors.Wrapf(RuleErrorSubmitPostCannotUpdateNFT, "_connectSubmitPost: ")
}
}
// It's an error if we are updating the value of RepostedPostHash. A post can only ever repost a single post.
if !reflect.DeepEqual(repostedPostHash, existingPostEntryy.RepostedPostHash) {
return 0, 0, nil, errors.Wrapf(
RuleErrorSubmitPostUpdateRepostHash,
"_connectSubmitPost: cannot update reposted post hash when updating a post")
}
// It's an error if we are updating the value of IsQuotedRepost.
if isQuotedRepost != existingPostEntryy.IsQuotedRepost {
return 0, 0, nil, errors.Wrapf(
RuleErrorSubmitPostUpdateIsQuotedRepost,
"_connectSubmitPost: cannot update isQuotedRepost attribute of post when updating a post")
}
// Save the data from the post. Note that we don't make a deep copy
// because all the fields that we modify are non-pointer fields.
prevPostEntry = &PostEntry{}
*prevPostEntry = *existingPostEntryy
// Set the newPostEntry pointer to the existing entry
newPostEntry = existingPostEntryy
// The field values should have already been validated so set
// them.
if len(txMeta.Body) != 0 {
newPostEntry.Body = txMeta.Body
}
// Merge the remaining attributes of the transaction's ExtraData into the postEntry's PostExtraData map.
if len(extraData) > 0 {
newPostExtraData := make(map[string][]byte)
for k, v := range existingPostEntryy.PostExtraData {
newPostExtraData[k] = v
}
for k, v := range extraData {
// If we're given a value with length greater than 0, add it to the map.
if len(v) > 0 {
newPostExtraData[k] = v
} else {
// If the value we're given has a length of 0, this indicates that we should delete it if it exists.
delete(newPostExtraData, k)
}
}
newPostEntry.PostExtraData = newPostExtraData
}
// TODO: Right now a post can be undeleted by the owner of the post,
// which seems like undesired behavior if a paramUpdater is trying to reduce
// spam
newPostEntry.IsHidden = txMeta.IsHidden
newPostEntry.IsFrozen = isFrozen
// Obtain the parent posts
newParentPostEntry, newGrandparentPostEntry, err = bav._getParentAndGrandparentPostEntry(newPostEntry)
if err != nil {
return 0, 0, nil, errors.Wrapf(err, "_connectSubmitPost: error with _getParentAndGrandparentPostEntry: %v", postHash)
}
if newPostEntry.RepostedPostHash != nil {
newRepostedPostEntry = bav.GetPostEntryForPostHash(newPostEntry.RepostedPostHash)
}
// Figure out how much we need to change the parent / grandparent's comment count by
var commentCountUpdateAmount int
repostCountUpdateAmount := 0
quoteRepostCountUpdateAmount := 0
hidingPostEntry := !prevPostEntry.IsHidden && newPostEntry.IsHidden
if hidingPostEntry {
// If we're hiding a post then we need to decrement the comment count of the parent
// and grandparent posts.
commentCountUpdateAmount = -1 * int(1+prevPostEntry.CommentCount)
// If we're hiding a post that is a vanilla repost of another post, we decrement the repost count of the
// post that was reposted.
if IsVanillaRepost(newPostEntry) {
repostCountUpdateAmount = -1
} else if isQuotedRepost {
quoteRepostCountUpdateAmount = -1
}
}
unhidingPostEntry := prevPostEntry.IsHidden && !newPostEntry.IsHidden
if unhidingPostEntry {
// If we're unhiding a post then we need to increment the comment count of the parent
// and grandparent posts.
commentCountUpdateAmount = int(1 + prevPostEntry.CommentCount)
// If we are unhiding a post that is a vanilla repost of another post, we increment the repost count of
// the post that was reposted.
if IsVanillaRepost(newPostEntry) {
repostCountUpdateAmount = 1
} else if isQuotedRepost {
quoteRepostCountUpdateAmount = 1
}
}
// Save the data from the parent post. Note that we don't make a deep copy
// because all the fields that we modify are non-pointer fields.
if newParentPostEntry != nil {
prevParentPostEntry = &PostEntry{}
*prevParentPostEntry = *newParentPostEntry
bav._updateParentCommentCountForPost(newPostEntry, newParentPostEntry, commentCountUpdateAmount)
}
// Save the data from the grandparent post. Note that we don't make a deep copy
// because all the fields that we modify are non-pointer fields.
if newGrandparentPostEntry != nil {
prevGrandparentPostEntry = &PostEntry{}
*prevGrandparentPostEntry = *newGrandparentPostEntry
bav._updateParentCommentCountForPost(newPostEntry, newGrandparentPostEntry, commentCountUpdateAmount)
}
if newRepostedPostEntry != nil {
prevRepostedPostEntry = &PostEntry{}
*prevRepostedPostEntry = *newRepostedPostEntry
// If the previous post entry is a vanilla repost, we can set the prevRepostEntry.
if IsVanillaRepost(prevPostEntry) {
prevRepostKey := MakeRepostKey(prevPostEntry.PosterPublicKey, *prevPostEntry.RepostedPostHash)
prevRepostEntry = bav._getRepostEntryForRepostKey(&prevRepostKey)
if prevRepostEntry == nil {
return 0, 0, nil, fmt.Errorf("prevRepostEntry not found for prevPostEntry")
}
// Generally prevRepostEntry is identical to newRepostEntry. Currently, we enforce a check that
// the RepostedPostHash does not get modified when attempting to connect a submitPost transaction
newRepostEntry = &RepostEntry{
ReposterPubKey: newPostEntry.PosterPublicKey,
RepostedPostHash: newPostEntry.RepostedPostHash,
RepostPostHash: newPostEntry.PostHash,
}
// Update the repost count if it has changed.
bav._updateRepostCount(newRepostedPostEntry, repostCountUpdateAmount)
} else {
// Update the quote repost count if it has changed.
bav._updateQuoteRepostCount(newRepostedPostEntry, quoteRepostCountUpdateAmount)
}
}
} else {
// In this case we are creating a post from scratch so validate
// all the fields.
// StakeMultipleBasisPoints > 0 < max
// Between 1x = 100% and 10x = 10,000%
if txMeta.StakeMultipleBasisPoints < 100*100 ||
txMeta.StakeMultipleBasisPoints > bav.Params.MaxStakeMultipleBasisPoints {
return 0, 0, nil, errors.Wrapf(RuleErrorSubmitPostStakeMultipleSize,
"_connectSubmitPost: Invalid StakeMultipleSize: %d",
txMeta.StakeMultipleBasisPoints)
}
// CreatorBasisPoints > 0 < max
if txMeta.CreatorBasisPoints < 0 ||
txMeta.CreatorBasisPoints > bav.Params.MaxCreatorBasisPoints {
return 0, 0, nil, errors.Wrapf(RuleErrorSubmitPostCreatorPercentageSize,
"_connectSubmitPost: Invalid CreatorPercentageSize: %d",
txMeta.CreatorBasisPoints)
}
// TstampNanos != 0
if txMeta.TimestampNanos == 0 {
return 0, 0, nil, errors.Wrapf(RuleErrorSubmitPostTimestampIsZero,
"_connectSubmitPost: Invalid Timestamp: %d",
txMeta.TimestampNanos)
}
// The parent stake id should be a block hash or profile public key if it's set.
if len(txMeta.ParentStakeID) != 0 && len(txMeta.ParentStakeID) != HashSizeBytes &&
len(txMeta.ParentStakeID) != btcec.PubKeyBytesLenCompressed {
return 0, 0, nil, errors.Wrapf(RuleErrorSubmitPostInvalidParentStakeIDLength,
"_connectSubmitPost: Parent stake ID length %v must be either 0 or %v or %v",
len(txMeta.ParentStakeID), HashSizeBytes, btcec.PubKeyBytesLenCompressed)
}
// The PostHash is just the transaction hash.
postHash := txHash
existingPostEntry := bav.GetPostEntryForPostHash(postHash)
if existingPostEntry != nil && !existingPostEntry.isDeleted {
return 0, 0, nil, errors.Wrapf(
RuleErrorPostAlreadyExists,
"_connectSubmitPost: Post hash: %v", postHash)
}