/
block_view_flush.go
1410 lines (1219 loc) · 55.8 KB
/
block_view_flush.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 (
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/dgraph-io/badger/v3"
"github.com/golang/glog"
"github.com/pkg/errors"
"reflect"
)
func (bav *UtxoView) FlushToDb(blockHeight uint64) error {
// Make sure everything happens inside a single transaction.
var err error
if bav.Postgres != nil {
err = bav.Postgres.FlushView(bav, blockHeight)
if err != nil {
return err
}
}
err = bav.Handle.Update(func(txn *badger.Txn) error {
return bav.FlushToDbWithTxn(txn, blockHeight)
})
if err != nil {
return err
}
// After a successful flush, reset the in-memory mappings for the view
// so that it can be re-used if desired.
//
// Note that the TipHash does not get reset as part of _ResetViewMappingsAfterFlush because
// it is not something that is affected by a flush operation. Moreover, its value
// is consistent with the view regardless of whether or not the view is flushed or
// not.
bav._ResetViewMappingsAfterFlush()
return nil
}
func (bav *UtxoView) FlushToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
// We're about to flush records to the main DB, so we initiate the snapshot update.
// This function prepares the data structures in the snapshot.
if bav.Snapshot != nil {
bav.Snapshot.PrepareAncestralRecordsFlush()
// When we finish flushing to the main DB, we'll also flush to ancestral records.
// This happens concurrently, which is why we have the 2-phase prepare-flush happening for snapshot.
defer bav.Snapshot.StartAncestralRecordsFlush(true)
}
// Only flush to BadgerDB if Postgres is disabled
if bav.Postgres == nil {
if err := bav._flushUtxosToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushProfileEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushPKIDEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushPostEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushLikeEntriesToDbWithTxn(txn); err != nil {
return err
}
if err := bav._flushFollowEntriesToDbWithTxn(txn); err != nil {
return err
}
if err := bav._flushDiamondEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushMessageEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushBalanceEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushDAOCoinBalanceEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushDeSoBalancesToDbWithTxn(txn); err != nil {
return err
}
if err := bav._flushForbiddenPubKeyEntriesToDbWithTxn(txn); err != nil {
return err
}
if err := bav._flushNFTEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushNFTBidEntriesToDbWithTxn(txn); err != nil {
return err
}
if err := bav._flushDerivedKeyEntryToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushAccessGroupEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushAccessGroupMembersToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushNewMessageEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
// Temporarily flush all DAO Coin Limit orders to badger
//if err := bav._flushDAOCoinLimitOrderEntriesToDbWithTxn(txn, blockHeight); err != nil {
// return err
//}
if err := bav._flushUserAssociationEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushPostAssociationEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
}
// Always flush to BadgerDB.
if err := bav._flushBitcoinExchangeDataWithTxn(txn); err != nil {
return err
}
if err := bav._flushGlobalParamsEntryToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushAcceptedBidEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushRepostEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
if err := bav._flushMessagingGroupEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
// Temporarily flush all DAO Coin Limit orders to badger
if err := bav._flushDAOCoinLimitOrderEntriesToDbWithTxn(txn, blockHeight); err != nil {
return err
}
return nil
}
func (bav *UtxoView) _flushUtxosToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
glog.V(2).Infof("_flushUtxosToDbWithTxn: flushing %d mappings", len(bav.UtxoKeyToUtxoEntry))
for utxoKeyIter, utxoEntry := range bav.UtxoKeyToUtxoEntry {
// Make a copy of the iterator since it might change from under us.
utxoKey := utxoKeyIter
// As a sanity-check, make sure the back-reference for each entry
// points to its key.
if utxoEntry.UtxoKey == nil || *utxoEntry.UtxoKey != utxoKey {
return fmt.Errorf("_flushUtxosToDbWithTxn: Found utxoEntry %+v for "+
"utxoKey %v has invalid back-refernce utxoKey %v",
utxoEntry, utxoKey, utxoEntry.UtxoKey)
}
// Start by deleting the pre-existing mappings in the db for this key if they
// have not yet been modified.
if err := DeleteUnmodifiedMappingsForUtxoWithTxn(txn, bav.Snapshot, &utxoKey); err != nil {
return err
}
}
numDeleted := 0
numPut := 0
for utxoKeyIter, utxoEntry := range bav.UtxoKeyToUtxoEntry {
// Make a copy of the iterator since it might change from under us.
utxoKey := utxoKeyIter
if utxoEntry.isSpent {
numDeleted++
// If an entry is spent then there's nothing to do, since the mappings in
// the db have already been deleted.
} else {
numPut++
// If the entry is unspent, then we need to re-set its mappings in the db
// appropriately.
if err := PutMappingsForUtxoWithTxn(txn, bav.Snapshot, blockHeight, &utxoKey, utxoEntry); err != nil {
return err
}
}
}
glog.V(2).Infof("_flushUtxosToDbWithTxn: deleted %d mappings, put %d mappings", numDeleted, numPut)
// Now update the number of entries in the db with confidence.
if err := PutUtxoNumEntriesWithTxn(txn, bav.Snapshot, bav.NumUtxoEntries); err != nil {
return err
}
// At this point, the db's position index should be updated and the (key -> entry)
// index should be updated to remove all spent utxos. The number of entries field
// in the db should also be accurate.
return nil
}
func (bav *UtxoView) _flushDeSoBalancesToDbWithTxn(txn *badger.Txn) error {
glog.V(2).Infof("_flushDeSoBalancesToDbWithTxn: flushing %d mappings",
len(bav.PublicKeyToDeSoBalanceNanos))
for pubKeyIter := range bav.PublicKeyToDeSoBalanceNanos {
// Make a copy of the iterator since it might change from under us.
pubKey := pubKeyIter[:]
// Start by deleting the pre-existing mappings in the db for this key if they
// have not yet been modified.
if err := DbDeletePublicKeyToDeSoBalanceWithTxn(txn, bav.Snapshot, pubKey); err != nil {
return err
}
}
for pubKeyIter, balanceNanos := range bav.PublicKeyToDeSoBalanceNanos {
// Make a copy of the iterator since it might change from under us.
pubKey := pubKeyIter[:]
if balanceNanos > 0 {
if err := DbPutDeSoBalanceForPublicKeyWithTxn(txn, bav.Snapshot, pubKey, balanceNanos); err != nil {
return err
}
}
}
return nil
}
func (bav *UtxoView) _flushGlobalParamsEntryToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
globalParamsEntry := bav.GlobalParamsEntry
if err := DbPutGlobalParamsEntryWithTxn(txn, bav.Snapshot, blockHeight, *globalParamsEntry); err != nil {
return errors.Wrapf(err, "_flushGlobalParamsEntryToDbWithTxn: Problem putting global params entry in DB")
}
return nil
}
func (bav *UtxoView) _flushForbiddenPubKeyEntriesToDbWithTxn(txn *badger.Txn) error {
// Go through all the entries in the KeyTorepostEntry map.
for _, forbiddenPubKeyEntry := range bav.ForbiddenPubKeyToForbiddenPubKeyEntry {
// Delete the existing mappings in the db for this ForbiddenPubKeyEntry. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DbDeleteForbiddenBlockSignaturePubKeyWithTxn(txn,
bav.Snapshot, forbiddenPubKeyEntry.PubKey[:]); err != nil {
return errors.Wrapf(
err, "_flushForbiddenPubKeyEntriesToDbWithTxn: Problem deleting "+
"forbidden public key: %v: ", &forbiddenPubKeyEntry.PubKey)
}
}
for _, forbiddenPubKeyEntry := range bav.ForbiddenPubKeyToForbiddenPubKeyEntry {
if forbiddenPubKeyEntry.isDeleted {
// If the ForbiddenPubKeyEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
// If the ForbiddenPubKeyEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DbPutForbiddenBlockSignaturePubKeyWithTxn(txn, bav.Snapshot,
forbiddenPubKeyEntry.PubKey); err != nil {
return err
}
}
}
return nil
}
func (bav *UtxoView) _flushBitcoinExchangeDataWithTxn(txn *badger.Txn) error {
// Iterate through our in-memory map. If anything has a value of false it means
// that particular mapping should be expunged from the db. If anything has a value
// of true it means that mapping should be added to the db.
for bitcoinBurnTxIDIter, mappingExists := range bav.BitcoinBurnTxIDs {
// Be paranoid and copy the iterator in case anything takes a reference below.
bitcoinBurnTxID := bitcoinBurnTxIDIter
if mappingExists {
// In this case we should add the mapping to the db.
if err := DbPutBitcoinBurnTxIDWithTxn(txn, bav.Snapshot, &bitcoinBurnTxID); err != nil {
return errors.Wrapf(err, "UtxoView._flushBitcoinExchangeDataWithTxn: "+
"Problem putting BitcoinBurnTxID %v to db", &bitcoinBurnTxID)
}
} else {
// In this case we should delete the mapping from the db.
if err := DbDeleteBitcoinBurnTxIDWithTxn(txn, bav.Snapshot, &bitcoinBurnTxID); err != nil {
return errors.Wrapf(err, "UtxoView._flushBitcoinExchangeDataWithTxn: "+
"Problem deleting BitcoinBurnTxID %v to db", &bitcoinBurnTxID)
}
}
}
// Update NanosPurchased
if err := DbPutNanosPurchasedWithTxn(txn, bav.Snapshot, bav.NanosPurchased); err != nil {
errors.Wrapf(err, "UtxoView._flushBitcoinExchangeDataWithTxn: "+
"Problem putting NanosPurchased %d to db", bav.NanosPurchased)
}
// Update the BitcoinUSDExchangeRate in the db
if err := DbPutUSDCentsPerBitcoinExchangeRateWithTxn(txn, bav.Snapshot, bav.USDCentsPerBitcoin); err != nil {
errors.Wrapf(err, "UtxoView.FlushToDBWithTxn: "+
"Problem putting USDCentsPerBitcoin %d to db", bav.USDCentsPerBitcoin)
}
// DB should be fully up to date as far as BitcoinBurnTxIDs and NanosPurchased go.
return nil
}
func (bav *UtxoView) _flushMessageEntriesToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
// Go through all the entries in the MessageKeyToMessageEntry map.
for messageKeyIter, messageEntry := range bav.MessageKeyToMessageEntry {
// Make a copy of the iterator since we take references to it below.
messageKey := messageKeyIter
// Delete the existing mappings in the db for this MessageKey. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DBDeleteMessageEntryMappingsWithTxn(txn, bav.Snapshot,
messageKey.PublicKey[:], messageKey.TstampNanos); err != nil {
return errors.Wrapf(
err, "_flushMessageEntriesToDbWithTxn: Problem deleting mappings "+
"for MessageKey: %v: ", &messageKey)
}
if messageEntry.isDeleted {
// If the MessageEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
// If the MessageEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DBPutMessageEntryWithTxn(txn, bav.Snapshot, blockHeight, messageKey, messageEntry); err != nil {
return err
}
}
}
// At this point all of the MessageEntry mappings in the db should be up-to-date.
return nil
}
func (bav *UtxoView) _flushRepostEntriesToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
// Go through all the entries in the repostKeyTorepostEntry map.
for repostKeyIter, repostEntry := range bav.RepostKeyToRepostEntry {
// Make a copy of the iterator since we make references to it below.
repostKey := repostKeyIter
// Sanity-check that the RepostKey computed from the RepostEntry is
// equal to the RepostKey that maps to that entry.
repostKeyInEntry := MakeRepostKey(repostEntry.ReposterPubKey, *repostEntry.RepostedPostHash)
if repostKeyInEntry != repostKey {
return fmt.Errorf("_flushRepostEntriesToDbWithTxn: RepostEntry has "+
"RepostKey: %v, which doesn't match the RepostKeyToRepostEntry map key %v",
&repostKeyInEntry, &repostKey)
}
// Delete the existing mappings in the db for this RepostKey. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DbDeleteRepostMappingsWithTxn(txn, bav.Snapshot, *repostEntry); err != nil {
return errors.Wrapf(
err, "_flushRepostEntriesToDbWithTxn: Problem deleting mappings "+
"for RepostKey: %v: ", &repostKey)
}
}
for _, repostEntry := range bav.RepostKeyToRepostEntry {
if repostEntry.isDeleted {
// If the RepostedEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
// If the RepostEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DbPutRepostMappingsWithTxn(txn, bav.Snapshot, blockHeight, *repostEntry); err != nil {
return err
}
}
}
// At this point all of the RepostEntry mappings in the db should be up-to-date.
return nil
}
func (bav *UtxoView) _flushLikeEntriesToDbWithTxn(txn *badger.Txn) error {
// Go through all the entries in the LikeKeyToLikeEntry map.
for likeKeyIter, likeEntry := range bav.LikeKeyToLikeEntry {
// Make a copy of the iterator since we make references to it below.
likeKey := likeKeyIter
// Sanity-check that the LikeKey computed from the LikeEntry is
// equal to the LikeKey that maps to that entry.
likeKeyInEntry := MakeLikeKey(likeEntry.LikerPubKey, *likeEntry.LikedPostHash)
if likeKeyInEntry != likeKey {
return fmt.Errorf("_flushLikeEntriesToDbWithTxn: LikeEntry has "+
"LikeKey: %v, which doesn't match the LikeKeyToLikeEntry map key %v",
&likeKeyInEntry, &likeKey)
}
// Delete the existing mappings in the db for this LikeKey. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DbDeleteLikeMappingsWithTxn(txn, bav.Snapshot,
likeKey.LikerPubKey[:], likeKey.LikedPostHash); err != nil {
return errors.Wrapf(
err, "_flushLikeEntriesToDbWithTxn: Problem deleting mappings "+
"for LikeKey: %v: ", &likeKey)
}
}
// Go through all the entries in the LikeKeyToLikeEntry map.
for _, likeEntry := range bav.LikeKeyToLikeEntry {
if likeEntry.isDeleted {
// If the LikeEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
// If the LikeEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DbPutLikeMappingsWithTxn(txn, bav.Snapshot,
likeEntry.LikerPubKey, *likeEntry.LikedPostHash); err != nil {
return err
}
}
}
return nil
}
func (bav *UtxoView) _flushFollowEntriesToDbWithTxn(txn *badger.Txn) error {
// Go through all the entries in the FollowKeyToFollowEntry map.
for followKeyIter, followEntry := range bav.FollowKeyToFollowEntry {
// Make a copy of the iterator since we make references to it below.
followKey := followKeyIter
// Sanity-check that the FollowKey computed from the FollowEntry is
// equal to the FollowKey that maps to that entry.
followKeyInEntry := MakeFollowKey(
followEntry.FollowerPKID, followEntry.FollowedPKID)
if followKeyInEntry != followKey {
return fmt.Errorf("_flushFollowEntriesToDbWithTxn: FollowEntry has "+
"FollowKey: %v, which doesn't match the FollowKeyToFollowEntry map key %v",
&followKeyInEntry, &followKey)
}
// Delete the existing mappings in the db for this FollowKey. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DbDeleteFollowMappingsWithTxn(txn, bav.Snapshot,
followEntry.FollowerPKID, followEntry.FollowedPKID); err != nil {
return errors.Wrapf(
err, "_flushFollowEntriesToDbWithTxn: Problem deleting mappings "+
"for FollowKey: %v: ", &followKey)
}
}
// Go through all the entries in the FollowKeyToFollowEntry map.
for _, followEntry := range bav.FollowKeyToFollowEntry {
if followEntry.isDeleted {
// If the FollowEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
// If the FollowEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DbPutFollowMappingsWithTxn(txn, bav.Snapshot,
followEntry.FollowerPKID, followEntry.FollowedPKID); err != nil {
return err
}
}
}
return nil
}
func (bav *UtxoView) _flushNFTEntriesToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
// Go through and delete all the entries so they can be added back fresh.
for nftKeyIter, nftEntry := range bav.NFTKeyToNFTEntry {
// Make a copy of the iterator since we make references to it below.
nftKey := nftKeyIter
// Sanity-check that the NFTKey computed from the NFTEntry is
// equal to the NFTKey that maps to that entry.
nftKeyInEntry := MakeNFTKey(nftEntry.NFTPostHash, nftEntry.SerialNumber)
if nftKeyInEntry != nftKey {
return fmt.Errorf("_flushNFTEntriesToDbWithTxn: NFTEntry has "+
"NFTKey: %v, which doesn't match the NFTKeyToNFTEntry map key %v",
&nftKeyInEntry, &nftKey)
}
// Delete the existing mappings in the db for this NFTKey. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DBDeleteNFTMappingsWithTxn(txn, bav.Snapshot,
nftEntry.NFTPostHash, nftEntry.SerialNumber); err != nil {
return errors.Wrapf(
err, "_flushNFTEntriesToDbWithTxn: Problem deleting mappings "+
"for NFTKey: %v: ", &nftKey)
}
}
// Add back all of the entries that aren't deleted.
for _, nftEntry := range bav.NFTKeyToNFTEntry {
if nftEntry.isDeleted {
// If the NFTEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
// If the NFTEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DBPutNFTEntryMappingsWithTxn(txn, bav.Snapshot, blockHeight, nftEntry); err != nil {
return err
}
}
}
return nil
}
func (bav *UtxoView) _flushAcceptedBidEntriesToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
// Go through and delete all the entries so they can be added back fresh.
for nftKeyIter := range bav.NFTKeyToAcceptedNFTBidHistory {
// Make a copy of the iterator since we make references to it below.
nftKey := nftKeyIter
// We skip the standard sanity check. Since it is possible to accept a bid on serial number 0, it is possible
// that none of the accepted bids have the same serial number as the key.
// Delete the existing mappings in the db for this NFTKey. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DBDeleteAcceptedNFTBidEntriesMappingsWithTxn(txn, bav.Snapshot,
&nftKey.NFTPostHash, nftKey.SerialNumber); err != nil {
return errors.Wrapf(
err, "_flushAcceptedBidEntriesToDbWithTxn: Problem deleting mappings "+
"for NFTKey: %v: ", &nftKey)
}
}
// Add back all of the entries that aren't nil or of length 0
for nftKeyIter, acceptedNFTBidEntries := range bav.NFTKeyToAcceptedNFTBidHistory {
nftKey := nftKeyIter
if acceptedNFTBidEntries == nil || len(*acceptedNFTBidEntries) == 0 {
// If the acceptedNFTBidEntries is nil or has length 0 then there's nothing to do because
// we already deleted the entry above. length 0 means that there are no accepted bids yet.
} else {
// If the NFTEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DBPutAcceptedNFTBidEntriesMappingWithTxn(txn, bav.Snapshot, blockHeight,
nftKey, acceptedNFTBidEntries); err != nil {
return err
}
}
}
return nil
}
func (bav *UtxoView) _flushNFTBidEntriesToDbWithTxn(txn *badger.Txn) error {
// Go through and delete all the entries so they can be added back fresh.
for nftBidKeyIter, nftBidEntry := range bav.NFTBidKeyToNFTBidEntry {
// Make a copy of the iterator since we make references to it below.
nftBidKey := nftBidKeyIter
// Sanity-check that the NFTBidKey computed from the NFTBidEntry is
// equal to the NFTBidKey that maps to that entry.
nftBidKeyInEntry := MakeNFTBidKey(
nftBidEntry.BidderPKID, nftBidEntry.NFTPostHash, nftBidEntry.SerialNumber)
if nftBidKeyInEntry != nftBidKey {
return fmt.Errorf("_flushNFTBidEntriesToDbWithTxn: NFTBidEntry has "+
"NFTBidKey: %v, which doesn't match the NFTBidKeyToNFTEntry map key %v",
&nftBidKeyInEntry, &nftBidKey)
}
// Delete the existing mappings in the db for this NFTBidKey. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DBDeleteNFTBidMappingsWithTxn(txn, bav.Snapshot, &nftBidKey); err != nil {
return errors.Wrapf(
err, "_flushNFTBidEntriesToDbWithTxn: Problem deleting mappings "+
"for NFTBidKey: %v: ", &nftBidKey)
}
}
// Add back all of the entries that aren't deleted.
for _, nftBidEntry := range bav.NFTBidKeyToNFTBidEntry {
if nftBidEntry.isDeleted {
// If the NFTEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
// If the NFTEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DBPutNFTBidEntryMappingsWithTxn(txn, bav.Snapshot, nftBidEntry); err != nil {
return err
}
}
}
return nil
}
func (bav *UtxoView) _flushDiamondEntriesToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
// Go through and delete all the entries so they can be added back fresh.
for diamondKeyIter, diamondEntry := range bav.DiamondKeyToDiamondEntry {
// Make a copy of the iterator since we make references to it below.
diamondKey := diamondKeyIter
// Sanity-check that the DiamondKey computed from the DiamondEntry is
// equal to the DiamondKey that maps to that entry.
diamondKeyInEntry := MakeDiamondKey(
diamondEntry.SenderPKID, diamondEntry.ReceiverPKID, diamondEntry.DiamondPostHash)
if diamondKeyInEntry != diamondKey {
return fmt.Errorf("_flushDiamondEntriesToDbWithTxn: DiamondEntry has "+
"DiamondKey: %v, which doesn't match the DiamondKeyToDiamondEntry map key %v",
&diamondKeyInEntry, &diamondKey)
}
// Delete the existing mappings in the db for this DiamondKey. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DbDeleteDiamondMappingsWithTxn(txn, bav.Snapshot, diamondEntry); err != nil {
return errors.Wrapf(
err, "_flushDiamondEntriesToDbWithTxn: Problem deleting mappings "+
"for DiamondKey: %v: ", &diamondKey)
}
}
// Add back all of the entries that aren't deleted.
for _, diamondEntry := range bav.DiamondKeyToDiamondEntry {
if diamondEntry.isDeleted {
// If the DiamondEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
// If the DiamondEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DbPutDiamondMappingsWithTxn(txn,
bav.Snapshot, blockHeight, diamondEntry); err != nil {
return err
}
}
}
// At this point all of the MessageEntry mappings in the db should be up-to-date.
return nil
}
func (bav *UtxoView) _flushPostEntriesToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
// TODO(DELETEME): Remove flush logging after debugging MarkBlockInvalid bug.
glog.V(2).Infof("_flushPostEntriesToDbWithTxn: flushing %d mappings", len(bav.PostHashToPostEntry))
// Go through all the entries in the PostHashToPostEntry map.
for postHashIter, postEntry := range bav.PostHashToPostEntry {
// Make a copy of the iterator since we take references to it below.
postHash := postHashIter
// Sanity-check that the hash in the post is the same as the hash in the
// entry
if postHash != *postEntry.PostHash {
return fmt.Errorf("_flushPostEntriesToDbWithTxn: PostEntry has "+
"PostHash: %v, neither of which match "+
"the PostHashToPostEntry map key %v",
postHash, postEntry.PostHash)
}
// Delete the existing mappings in the db for this PostHash. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DBDeletePostEntryMappingsWithTxn(txn,
bav.Snapshot, &postHash, bav.Params); err != nil {
return errors.Wrapf(
err, "_flushPostEntriesToDbWithTxn: Problem deleting mappings "+
"for PostHash: %v: ", postHash)
}
}
numDeleted := 0
numPut := 0
for _, postEntry := range bav.PostHashToPostEntry {
if postEntry.isDeleted {
numDeleted++
// If the PostEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
numPut++
// If the PostEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DBPutPostEntryMappingsWithTxn(txn, bav.Snapshot, blockHeight,
postEntry, bav.Params); err != nil {
return err
}
}
}
// TODO(DELETEME): Remove flush logging after debugging MarkBlockInvalid bug.
glog.V(2).Infof("_flushPostEntriesToDbWithTxn: deleted %d mappings, put %d mappings", numDeleted, numPut)
// At this point all of the PostEntry mappings in the db should be up-to-date.
return nil
}
func (bav *UtxoView) _flushPKIDEntriesToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
for pubKeyIter, pkidEntry := range bav.PublicKeyToPKIDEntry {
pubKeyCopy := make([]byte, btcec.PubKeyBytesLenCompressed)
copy(pubKeyCopy, pubKeyIter[:])
// Delete the existing mappings in the db for this PKID. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DBDeletePKIDMappingsWithTxn(txn, bav.Snapshot,
pubKeyCopy, bav.Params); err != nil {
return errors.Wrapf(
err, "_flushPKIDEntriesToDbWithTxn: Problem deleting mappings "+
"for pkid: %v, public key: %v: ", PkToString(pkidEntry.PKID[:], bav.Params),
PkToString(pubKeyCopy, bav.Params))
}
}
// Go through all the entries in the ProfilePublicKeyToProfileEntry map.
for pubKeyIter, pkidEntry := range bav.PublicKeyToPKIDEntry {
pubKeyCopy := make([]byte, btcec.PubKeyBytesLenCompressed)
copy(pubKeyCopy, pubKeyIter[:])
if pkidEntry.isDeleted {
// If the ProfileEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
// Sanity-check that the public key in the entry matches the public key in
// the mapping.
if !reflect.DeepEqual(pubKeyCopy, pkidEntry.PublicKey) {
return fmt.Errorf("_flushPKIDEntriesToDbWithTxn: Sanity-check failed. "+
"Public key in entry %v does not match public key in mapping %v ",
PkToString(pkidEntry.PublicKey[:], bav.Params),
PkToString(pubKeyCopy, bav.Params))
}
// Sanity-check that the mapping in the public key map lines up with the mapping
// in the PKID map.
if _, pkidEntryExists := bav.PKIDToPublicKey[*pkidEntry.PKID]; !pkidEntryExists {
return fmt.Errorf("_flushPKIDEntriesToDbWithTxn: Sanity-check failed. "+
"PKID %v for public key %v does not exist in PKIDToPublicKey map.",
PkToString(pkidEntry.PKID[:], bav.Params),
PkToString(pubKeyCopy, bav.Params))
}
// If the ProfileEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DBPutPKIDMappingsWithTxn(txn, bav.Snapshot, blockHeight,
pubKeyCopy, pkidEntry, bav.Params); err != nil {
return err
}
}
}
// At this point all of the PKIDEntry mappings in the db should be up-to-date.
return nil
}
func (bav *UtxoView) _flushProfileEntriesToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
glog.V(2).Infof("_flushProfilesToDbWithTxn: flushing %d mappings", len(bav.ProfilePKIDToProfileEntry))
// Go through all the entries in the ProfilePublicKeyToProfileEntry map.
for profilePKIDIter, profileEntry := range bav.ProfilePKIDToProfileEntry {
// Make a copy of the iterator since we take references to it below.
profilePKID := profilePKIDIter
// Delete the existing mappings in the db for this PKID. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DBDeleteProfileEntryMappingsWithTxn(txn, bav.Snapshot,
&profilePKID, bav.Params); err != nil {
return errors.Wrapf(
err, "_flushProfileEntriesToDbWithTxn: Problem deleting mappings "+
"for pkid: %v, public key: %v: ", PkToString(profilePKID[:], bav.Params),
PkToString(profileEntry.PublicKey, bav.Params))
}
}
numDeleted := 0
numPut := 0
for profilePKIDIter, profileEntry := range bav.ProfilePKIDToProfileEntry {
// Make a copy of the iterator since we take references to it below.
profilePKID := profilePKIDIter
if profileEntry.isDeleted {
numDeleted++
// If the ProfileEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
numPut++
// Get the PKID according to another map in the view and
// sanity-check that it lines up.
viewPKIDEntry := bav.GetPKIDForPublicKey(profileEntry.PublicKey)
if viewPKIDEntry == nil || viewPKIDEntry.isDeleted || *viewPKIDEntry.PKID != profilePKID {
return fmt.Errorf("_flushProfileEntriesToDbWithTxn: Sanity-check failed: PKID %v does "+
"not exist in view mapping for profile with public key %v",
PkToString(profilePKID[:], bav.Params),
PkToString(profileEntry.PublicKey, bav.Params))
}
// If the ProfileEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DBPutProfileEntryMappingsWithTxn(txn, bav.Snapshot, blockHeight,
profileEntry, &profilePKID, bav.Params); err != nil {
return err
}
}
}
glog.V(2).Infof("_flushProfilesToDbWithTxn: deleted %d mappings, put %d mappings", numDeleted, numPut)
// At this point all of the PostEntry mappings in the db should be up-to-date.
return nil
}
// TODO: All of these functions should be renamed "CreatorCoinBalanceEntry" to
// distinguish them from DAOCoinBalanceEntry, which is a different but similar index
// that got introduced later.
func (bav *UtxoView) _flushBalanceEntriesToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
glog.V(2).Infof("_flushBalanceEntriesToDbWithTxn: flushing %d mappings", len(bav.HODLerPKIDCreatorPKIDToBalanceEntry))
// Go through all the entries in the HODLerPubKeyCreatorPubKeyToBalanceEntry map.
for balanceKeyIter, balanceEntry := range bav.HODLerPKIDCreatorPKIDToBalanceEntry {
// Make a copy of the iterator since we take references to it below.
balanceKey := balanceKeyIter
// Sanity-check that the balance key in the map is the same
// as the public key in the entry.
computedBalanceKey := MakeBalanceEntryKey(
balanceEntry.HODLerPKID, balanceEntry.CreatorPKID)
if !reflect.DeepEqual(balanceKey, computedBalanceKey) {
return fmt.Errorf("_flushBalanceEntriesToDbWithTxn: BalanceEntry has "+
"map key: %v which does not match match "+
"the HODLerPubKeyCreatorPubKeyToBalanceEntry map key %v",
balanceKey, computedBalanceKey)
}
// Delete the existing mappings in the db for this balance key. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DBDeleteBalanceEntryMappingsWithTxn(txn, bav.Snapshot,
&(balanceKey.HODLerPKID), &(balanceKey.CreatorPKID), false); err != nil {
return errors.Wrapf(
err, "_flushBalanceEntriesToDbWithTxn: Problem deleting mappings "+
"for public key: %v: ", balanceKey)
}
}
numDeleted := 0
numPut := 0
// Go through all the entries in the HODLerPubKeyCreatorPubKeyToBalanceEntry map.
for _, balanceEntry := range bav.HODLerPKIDCreatorPKIDToBalanceEntry {
// Make a copy of the iterator since we take references to it below.
if balanceEntry.isDeleted {
numDeleted++
// If the ProfileEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
numPut++
// If the ProfileEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DBPutBalanceEntryMappingsWithTxn(txn, bav.Snapshot, blockHeight,
balanceEntry, false); err != nil {
return err
}
}
}
glog.V(2).Infof("_flushBalanceEntriesToDbWithTxn: deleted %d mappings, put %d mappings", numDeleted, numPut)
// At this point all of the creator coin mappings in the db should be up-to-date.
return nil
}
// TODO: This could theoretically be consolidated with the other BalanceEntry flusher.
func (bav *UtxoView) _flushDAOCoinBalanceEntriesToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
glog.V(2).Infof("_flushDAOCoinBalanceEntriesToDbWithTxn: flushing %d mappings", len(bav.HODLerPKIDCreatorPKIDToDAOCoinBalanceEntry))
// Go through all the entries in the HODLerPubKeyCreatorPubKeyToBalanceEntry map.
for balanceKeyIter, balanceEntry := range bav.HODLerPKIDCreatorPKIDToDAOCoinBalanceEntry {
// Make a copy of the iterator since we take references to it below.
balanceKey := balanceKeyIter
// Sanity-check that the balance key in the map is the same
// as the public key in the entry.
computedBalanceKey := MakeBalanceEntryKey(
balanceEntry.HODLerPKID, balanceEntry.CreatorPKID)
if !reflect.DeepEqual(balanceKey, computedBalanceKey) {
return fmt.Errorf("_flushDAOCoinBalanceEntriesToDbWithTxn: BalanceEntry has "+
"map key: %v which does not match match "+
"the HODLerPubKeyCreatorPubKeyToBalanceEntry map key %v",
balanceKey, computedBalanceKey)
}
// Delete the existing mappings in the db for this balance key. They will be re-added
// if the corresponding entry in memory has isDeleted=false.
if err := DBDeleteBalanceEntryMappingsWithTxn(txn, bav.Snapshot,
&(balanceKey.HODLerPKID), &(balanceKey.CreatorPKID), true); err != nil {
return errors.Wrapf(
err, "_flushDAOCoinBalanceEntriesToDbWithTxn: Problem deleting mappings "+
"for public key: %v: ", balanceKey)
}
}
numDeleted := 0
numPut := 0
// Go through all the entries in the HODLerPKIDCreatorPKIDToDAOCoinBalanceEntry map.
for _, balanceEntry := range bav.HODLerPKIDCreatorPKIDToDAOCoinBalanceEntry {
// Make a copy of the iterator since we take references to it below.
if balanceEntry.isDeleted {
numDeleted++
// If the ProfileEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
numPut++
// If the ProfileEntry has (isDeleted = false) then we put the corresponding
// mappings for it into the db.
if err := DBPutBalanceEntryMappingsWithTxn(txn, bav.Snapshot, blockHeight,
balanceEntry, true); err != nil {
return err
}
}
}
glog.V(2).Infof("_flushDAOCoinBalanceEntriesToDbWithTxn: deleted %d mappings, put %d mappings", numDeleted, numPut)
// At this point all of the DAO coin mappings in the db should be up-to-date.
return nil
}
func (bav *UtxoView) _flushDerivedKeyEntryToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
glog.V(2).Infof("_flushDerivedKeyEntryToDbWithTxn: flushing %d mappings", len(bav.DerivedKeyToDerivedEntry))
numDeleted := 0
numPut := 0
// Go through all entries in the DerivedKeyToDerivedEntry map and add them to the DB.
for derivedKeyMapKey, derivedKeyEntry := range bav.DerivedKeyToDerivedEntry {
// Delete the existing mapping in the DB for this map key, this will be re-added
// later if isDeleted=false.
if err := DBDeleteDerivedKeyMappingWithTxn(txn, bav.Snapshot,
derivedKeyMapKey.OwnerPublicKey, derivedKeyMapKey.DerivedPublicKey); err != nil {
return errors.Wrapf(err, "UtxoView._flushDerivedKeyEntryToDbWithTxn: "+
"Problem deleting DerivedKeyEntry %v from db", *derivedKeyEntry)
}
if derivedKeyEntry.isDeleted {
// Since entry is deleted, there's nothing to do.
numDeleted++
} else {
// In this case we add the mapping to the DB.
if err := DBPutDerivedKeyMappingWithTxn(txn, bav.Snapshot, blockHeight,
derivedKeyMapKey.OwnerPublicKey, derivedKeyMapKey.DerivedPublicKey, derivedKeyEntry); err != nil {
return errors.Wrapf(err, "UtxoView._flushDerivedKeyEntryToDbWithTxn: "+
"Problem putting DerivedKeyEntry %v to db", *derivedKeyEntry)
}
numPut++
}
}
glog.V(2).Infof("_flushDerivedKeyEntryToDbWithTxn: deleted %d mappings, put %d mappings", numDeleted, numPut)
return nil
}
func (bav *UtxoView) _flushMessagingGroupEntriesToDbWithTxn(txn *badger.Txn, blockHeight uint64) error {
glog.V(2).Infof("_flushMessagingGroupEntriesToDbWithTxn: flushing %d mappings", len(bav.MessagingGroupKeyToMessagingGroupEntry))
numDeleted := 0
numPut := 0
// Go through all entries in MessagingGroupKeyToMessagingGroupEntry and add them to the DB.
// These records are part of the DeSo V3 Messages.
for messagingGroupKey, messagingGroupEntry := range bav.MessagingGroupKeyToMessagingGroupEntry {
// Delete the existing mapping in the DB for this map key, this will be re-added
// later if isDeleted=false. Messaging entries can have a list of members, and
// we store these members under a separate prefix. To delete a messaging group
// we also have to go delete all of the recipients.
//
// TODO: We should have a single DeleteMappings function in db_utils.go that we push this
// complexity into.
existingMessagingGroupEntry := DBGetMessagingGroupEntryWithTxn(txn, bav.Snapshot, &messagingGroupKey)
if existingMessagingGroupEntry != nil {
if err := DBDeleteMessagingGroupEntryWithTxn(txn, bav.Snapshot, &messagingGroupKey); err != nil {
return errors.Wrapf(err, "UtxoView._flushMessagingGroupEntriesToDbWithTxn: "+
"Problem deleting MessagingGroupEntry %v from db", *messagingGroupEntry)
}
for _, member := range existingMessagingGroupEntry.MessagingGroupMembers {
if err := DBDeleteMessagingGroupMemberMappingWithTxn(txn, bav.Snapshot,
member, existingMessagingGroupEntry); err != nil {
return errors.Wrapf(err, "UtxoView._flushMessagingGroupEntriesToDbWithTxn: "+