-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_view_types.go
4467 lines (3814 loc) · 155 KB
/
block_view_types.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"
"encoding/json"
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/golang/glog"
"github.com/holiman/uint256"
"github.com/pkg/errors"
"io"
"math"
"math/big"
"reflect"
"sort"
"strings"
)
type UtxoType uint8
const (
// UTXOs can come from different sources. We document all of those sources
// in the UTXOEntry using these types.
UtxoTypeOutput UtxoType = 0
UtxoTypeBlockReward UtxoType = 1
UtxoTypeBitcoinBurn UtxoType = 2
// TODO(DELETEME): Remove the StakeReward txn type
UtxoTypeStakeReward UtxoType = 3
UtxoTypeCreatorCoinSale UtxoType = 4
UtxoTypeCreatorCoinFounderReward UtxoType = 5
UtxoTypeNFTSeller UtxoType = 6
UtxoTypeNFTBidderChange UtxoType = 7
UtxoTypeNFTCreatorRoyalty UtxoType = 8
UtxoTypeNFTAdditionalDESORoyalty UtxoType = 9
UtxoTypeDAOCoinLimitOrderPayout UtxoType = 10
// NEXT_TAG = 11
)
func (mm UtxoType) String() string {
switch mm {
case UtxoTypeOutput:
return "UtxoTypeOutput"
case UtxoTypeBlockReward:
return "UtxoTypeBlockReward"
case UtxoTypeBitcoinBurn:
return "UtxoTypeBitcoinBurn"
case UtxoTypeStakeReward:
return "UtxoTypeStakeReward"
case UtxoTypeCreatorCoinSale:
return "UtxoTypeCreatorCoinSale"
case UtxoTypeCreatorCoinFounderReward:
return "UtxoTypeCreatorCoinFounderReward"
case UtxoTypeNFTSeller:
return "UtxoTypeNFTSeller"
case UtxoTypeNFTBidderChange:
return "UtxoTypeNFTBidderChange"
case UtxoTypeNFTCreatorRoyalty:
return "UtxoTypeNFTCreatorRoyalty"
case UtxoTypeNFTAdditionalDESORoyalty:
return "UtxoTypeNFTAdditionalDESORoyalty"
default:
return "UtxoTypeUnknown"
}
}
type EncoderType uint32
// Block view encoder types. These types to different structs implementing the DeSoEncoder interface.
const (
EncoderTypeUtxoEntry EncoderType = iota
EncoderTypeUtxoOperation
EncoderTypeUtxoOperationBundle
EncoderTypeMessageEntry
EncoderTypeGroupKeyName
EncoderTypeMessagingGroupEntry
EncoderTypeMessagingGroupMember
EncoderTypeForbiddenPubKeyEntry
EncoderTypeLikeEntry
EncoderTypeNFTEntry
EncoderTypeNFTBidEntry
EncoderTypeNFTBidEntryBundle
EncoderTypeDerivedKeyEntry
EncoderTypeDiamondEntry
EncoderTypeRepostEntry
EncoderTypeGlobalParamsEntry
EncoderTypePostEntry
EncoderTypeBalanceEntry
EncoderTypeCoinEntry
EncoderTypePublicKeyRoyaltyPair
EncoderTypePKIDEntry
EncoderTypeProfileEntry
EncoderTypeAffectedPublicKey
EncoderTypeUtxoKey
EncoderTypeDeSoOutput
EncoderTypePKID
EncoderTypePublicKey
EncoderTypeBlockHash
EncoderTypeDAOCoinLimitOrderEntry
EncoderTypeFilledDAOCoinLimitOrder
// EncoderTypeEndBlockView encoder type should be at the end and is used for automated tests.
EncoderTypeEndBlockView
)
// Txindex encoder types.
const (
EncoderTypeTransactionMetadata EncoderType = 1000000 + iota
EncoderTypeBasicTransferTxindexMetadata
EncoderTypeBitcoinExchangeTxindexMetadata
EncoderTypeCreatorCoinTxindexMetadata
EncoderTypeCreatorCoinTransferTxindexMetadata
EncoderTypeDAOCoinTransferTxindexMetadata
EncoderTypeFilledDAOCoinLimitOrderMetadata
EncoderTypeDAOCoinLimitOrderTxindexMetadata
EncoderTypeUpdateProfileTxindexMetadata
EncoderTypeSubmitPostTxindexMetadata
EncoderTypeLikeTxindexMetadata
EncoderTypeFollowTxindexMetadata
EncoderTypePrivateMessageTxindexMetadata
EncoderTypeSwapIdentityTxindexMetadata
EncoderTypeNFTRoyaltiesMetadata
EncoderTypeNFTBidTxindexMetadata
EncoderTypeAcceptNFTBidTxindexMetadata
EncoderTypeNFTTransferTxindexMetadata
EncoderTypeAcceptNFTTransferTxindexMetadata
EncoderTypeBurnNFTTxindexMetadata
EncoderTypeDAOCoinTxindexMetadata
EncoderTypeCreateNFTTxindexMetadata
EncoderTypeUpdateNFTTxindexMetadata
// EncoderTypeEndTxIndex encoder type should be at the end and is used for automated tests.
EncoderTypeEndTxIndex
)
// This function translates the EncoderType into an empty DeSoEncoder struct.
func (encoderType EncoderType) New() DeSoEncoder {
// Block view encoder types
switch encoderType {
case EncoderTypeUtxoEntry:
return &UtxoEntry{}
case EncoderTypeUtxoOperation:
return &UtxoOperation{}
case EncoderTypeUtxoOperationBundle:
return &UtxoOperationBundle{}
case EncoderTypeMessageEntry:
return &MessageEntry{}
case EncoderTypeGroupKeyName:
return &GroupKeyName{}
case EncoderTypeMessagingGroupEntry:
return &MessagingGroupEntry{}
case EncoderTypeMessagingGroupMember:
return &MessagingGroupMember{}
case EncoderTypeForbiddenPubKeyEntry:
return &ForbiddenPubKeyEntry{}
case EncoderTypeLikeEntry:
return &LikeEntry{}
case EncoderTypeNFTEntry:
return &NFTEntry{}
case EncoderTypeNFTBidEntry:
return &NFTBidEntry{}
case EncoderTypeNFTBidEntryBundle:
return &NFTBidEntryBundle{}
case EncoderTypeDerivedKeyEntry:
return &DerivedKeyEntry{}
case EncoderTypeDiamondEntry:
return &DiamondEntry{}
case EncoderTypeRepostEntry:
return &RepostEntry{}
case EncoderTypeGlobalParamsEntry:
return &GlobalParamsEntry{}
case EncoderTypePostEntry:
return &PostEntry{}
case EncoderTypeBalanceEntry:
return &BalanceEntry{}
case EncoderTypeCoinEntry:
return &CoinEntry{}
case EncoderTypePublicKeyRoyaltyPair:
return &PublicKeyRoyaltyPair{}
case EncoderTypePKIDEntry:
return &PKIDEntry{}
case EncoderTypeProfileEntry:
return &ProfileEntry{}
case EncoderTypeAffectedPublicKey:
return &AffectedPublicKey{}
case EncoderTypeUtxoKey:
return &UtxoKey{}
case EncoderTypeDeSoOutput:
return &DeSoOutput{}
case EncoderTypePKID:
return &PKID{}
case EncoderTypePublicKey:
return &PublicKey{}
case EncoderTypeBlockHash:
return &BlockHash{}
case EncoderTypeDAOCoinLimitOrderEntry:
return &DAOCoinLimitOrderEntry{}
case EncoderTypeFilledDAOCoinLimitOrder:
return &FilledDAOCoinLimitOrder{}
}
// Txindex encoder types
switch encoderType {
case EncoderTypeTransactionMetadata:
return &TransactionMetadata{}
case EncoderTypeBasicTransferTxindexMetadata:
return &BasicTransferTxindexMetadata{}
case EncoderTypeBitcoinExchangeTxindexMetadata:
return &BitcoinExchangeTxindexMetadata{}
case EncoderTypeCreatorCoinTxindexMetadata:
return &CreatorCoinTxindexMetadata{}
case EncoderTypeCreatorCoinTransferTxindexMetadata:
return &CreatorCoinTransferTxindexMetadata{}
case EncoderTypeDAOCoinTransferTxindexMetadata:
return &DAOCoinTransferTxindexMetadata{}
case EncoderTypeFilledDAOCoinLimitOrderMetadata:
return &FilledDAOCoinLimitOrderMetadata{}
case EncoderTypeDAOCoinLimitOrderTxindexMetadata:
return &DAOCoinLimitOrderTxindexMetadata{}
case EncoderTypeUpdateProfileTxindexMetadata:
return &UpdateProfileTxindexMetadata{}
case EncoderTypeSubmitPostTxindexMetadata:
return &SubmitPostTxindexMetadata{}
case EncoderTypeLikeTxindexMetadata:
return &LikeTxindexMetadata{}
case EncoderTypeFollowTxindexMetadata:
return &FollowTxindexMetadata{}
case EncoderTypePrivateMessageTxindexMetadata:
return &PrivateMessageTxindexMetadata{}
case EncoderTypeSwapIdentityTxindexMetadata:
return &SwapIdentityTxindexMetadata{}
case EncoderTypeNFTRoyaltiesMetadata:
return &NFTRoyaltiesMetadata{}
case EncoderTypeNFTBidTxindexMetadata:
return &NFTBidTxindexMetadata{}
case EncoderTypeAcceptNFTBidTxindexMetadata:
return &AcceptNFTBidTxindexMetadata{}
case EncoderTypeNFTTransferTxindexMetadata:
return &NFTTransferTxindexMetadata{}
case EncoderTypeAcceptNFTTransferTxindexMetadata:
return &AcceptNFTTransferTxindexMetadata{}
case EncoderTypeBurnNFTTxindexMetadata:
return &BurnNFTTxindexMetadata{}
case EncoderTypeDAOCoinTxindexMetadata:
return &DAOCoinTxindexMetadata{}
case EncoderTypeCreateNFTTxindexMetadata:
return &CreateNFTTxindexMetadata{}
case EncoderTypeUpdateNFTTxindexMetadata:
return &UpdateNFTTxindexMetadata{}
default:
return nil
}
}
// DeSoEncoder is an interface handling our custom, deterministic byte encodings.
type DeSoEncoder interface {
// RawEncodeWithoutMetadata and RawDecodeWithoutMetadata methods should encode/decode a DeSoEncoder struct into a
// byte array. The encoding must always be deterministic, and readable by the corresponding RawDecodeWithoutMetadata.
// We decided to call these methods with terms like: "RawEncode" and "WithoutMetadata" so that these functions sound
// scary enough to not be directly called. They shouldn't be! EncodeToBytes and DecodeFromBytes wrappers should be
// used instead. In particular, the implementation shouldn't worry about the DeSoEncoder being nil, nor about encoding
// the blockHeight. All of this is handled by the wrappers. The blockHeights are passed to the encoder methods to support
// encoder migrations, which allow upgrading existing badgerDB entries without requiring a resync. Lookup EncoderMigrationHeights
// for more information on how this works. skipMetadata shouldn't be used directly by the methods; however, it must
// always be passed if we're nesting EncodeToBytes in the method implementation.
RawEncodeWithoutMetadata(blockHeight uint64, skipMetadata ...bool) []byte
RawDecodeWithoutMetadata(blockHeight uint64, rr *bytes.Reader) error
// GetVersionByte should return the version of the DeSoEncoder as a function of EncoderMigrationHeights and blockHeight.
// For instance, if we added a new migration at height H and version byte V, we should implement the GetVersionByte
// method so that V is returned whenever blockHeight >= H.
GetVersionByte(blockHeight uint64) byte
// GetEncoderType should return the EncoderType corresponding to the DeSoEncoder.
GetEncoderType() EncoderType
}
// EncodeToBytes encodes a DeSoEncoder type to bytes, including encoder metadata such as existence byte, the encoder
// type, and the current blockHeight. The skipMetadata parameter should be always passed if we're nesting DeSoEncoders,
// but in general it shouldn't be passed. This parameter is only used when we're computing the state checksum.
func EncodeToBytes(blockHeight uint64, encoder DeSoEncoder, skipMetadata ...bool) []byte {
var data []byte
// Encoding without metadata is used in the checksum computation. We do this because metadata is kind of arbitrary.
shouldSkipMetadata := false
if len(skipMetadata) > 0 {
shouldSkipMetadata = skipMetadata[0]
}
// We will encode the DeSoEncoder type with some additional metadata. This metadata consists of:
// <existenceByte [1]byte> <encoderType [4]byte> <encoderVersion [1]byte> <encodedBytes []byte>
//
// existenceByte - is a boolean that tells us if the encoder entry was initialized (true) or if it was nil (false)
// encoderType - is a uint32 that encodes information of DeSoEncoder's type.
// encoderVersion - is a byte that says which migration was used by the encoder. Migrations increment encoder's version.
// encodedBytes - encoder's bytes
if encoder != nil && !reflect.ValueOf(encoder).IsNil() {
data = append(data, BoolToByte(true))
// Encode metadata
if !shouldSkipMetadata {
data = append(data, UintToBuf(uint64(encoder.GetEncoderType()))...)
data = append(data, UintToBuf(uint64(encoder.GetVersionByte(blockHeight)))...)
}
data = append(data, encoder.RawEncodeWithoutMetadata(blockHeight, skipMetadata...)...)
} else {
data = append(data, BoolToByte(false))
}
return data
}
// DecodeFromBytes decodes a DeSoEncoder type from bytes. We check
// for the existence byte, which tells us whether actual data was encoded, or a nil pointer.
func DecodeFromBytes(encoder DeSoEncoder, rr *bytes.Reader) (_existenceByte bool, _error error) {
if existenceByte, err := ReadBoolByte(rr); existenceByte && err == nil {
encoderType, err := ReadUvarint(rr)
if err != nil {
return false, errors.Wrapf(err, "DecodeFromBytes: Problem decoding encoder type")
}
if encoderType > math.MaxUint32 {
return false, errors.Wrapf(err, "DecodeFromBytes: Encoder type "+
"value exceeds max uint32: %v", encoderType)
}
// Because encoder is provided as a parameter, we just verify that the entry type matches the encoder type.
if !reflect.DeepEqual(EncoderType(encoderType), encoder.GetEncoderType()) {
return false, fmt.Errorf("DecodeFromBytes: encoder type (%v) doesn't match the "+
"entry type (%v)", encoderType, encoder.GetEncoderType())
}
versionByte, err := ReadUvarint(rr)
if err != nil {
return false, errors.Wrapf(err, "DecodeFromBytes: Problem decoding version bytes")
}
if versionByte > math.MaxUint8 {
return false, errors.Wrapf(err, "DecodeFromBytes: versionByte "+
"value exceeds max uint8: %v", versionByte)
}
// TODO: We should pass DeSoParams to this function instead of using GlobalParams.
// We don't do this for now because it's a massive refactor.
blockHeight := VersionByteToMigrationHeight(uint8(versionByte), &GlobalDeSoParams)
err = encoder.RawDecodeWithoutMetadata(blockHeight, rr)
if err != nil {
return false, errors.Wrapf(err, "DecodeFromBytes: Problem reading encoder")
}
return true, nil
} else if err != nil {
return false, errors.Wrapf(err, "DecodeFromBytes: Problem reading existence byte")
}
return false, nil
}
// MigrationTriggered is a suggested conditional check to be called within RawEncodeWithoutMetadata and
// RawDecodeWithoutMetadata when defining the encoding migrations for DeSoEncoders. Consult constants.go for more info.
func MigrationTriggered(blockHeight uint64, migrationName MigrationName) bool {
for _, migration := range GlobalDeSoParams.EncoderMigrationHeightsList {
if migration.Name == migrationName {
return blockHeight >= migration.Height
}
}
panic(fmt.Sprintf("Problem finding a migration corresponding to migrationName (%v) "+
"check your code!", migrationName))
}
// GetMigrationVersion can be returned in GetVersionByte when implementing DeSoEncoders. The way to do it is simply
// calling `return GetMigrationVersion(blockHeight, [Migration Names])` where migration names are all EncoderMigrationHeights
// that were used in RawEncodeWithoutMetadata and RawDecodeWithoutMetadata. [Migration Names] can be simply a list of
// MigrationName strings corresponding to these EncodeMigrationHeights.
func GetMigrationVersion(blockHeight uint64, appliedMigrationNames ...MigrationName) byte {
maxMigrationVersion := byte(0)
for _, migration := range GlobalDeSoParams.EncoderMigrationHeightsList {
for _, appliedMigration := range appliedMigrationNames {
// Select the applied migrations.
if migration.Name == appliedMigration {
// Make sure the migration is satisfied by the current blockHeight.
if migration.Height <= blockHeight {
// Find the migration with the greatest version.
if migration.Version > maxMigrationVersion {
maxMigrationVersion = migration.Version
}
}
}
}
}
return maxMigrationVersion
}
// UtxoEntry identifies the data associated with a UTXO.
type UtxoEntry struct {
AmountNanos uint64
PublicKey []byte
BlockHeight uint32
UtxoType UtxoType
// The fields below aren't serialized or hashed. They are only kept
// around for in-memory bookkeeping purposes.
// Whether or not the UTXO is spent. This is not used by the database,
// (in fact it's not even stored in the db) it's used
// only by the in-memory data structure. The database is simple: A UTXO
// is unspent if and only if it exists in the db. However, for the view,
// a UTXO is unspent if it (exists in memory and is unspent) OR (it does not
// exist in memory at all but does exist in the database).
//
// Note that we are relying on the code that serializes the entry to the
// db to ignore private fields, which is why this variable is lowerCamelCase
// rather than UpperCamelCase. We are also relying on it defaulting to
// false when newly-read from the database.
isSpent bool
// A back-reference to the utxo key associated with this entry.
UtxoKey *UtxoKey
}
func (utxoEntry *UtxoEntry) String() string {
return fmt.Sprintf("< OwnerPublicKey: %v, BlockHeight: %d, AmountNanos: %d, UtxoType: %v, "+
"isSpent: %v, utxoKey: %v>", PkToStringMainnet(utxoEntry.PublicKey),
utxoEntry.BlockHeight, utxoEntry.AmountNanos,
utxoEntry.UtxoType, utxoEntry.isSpent, utxoEntry.UtxoKey)
}
func (utxo *UtxoEntry) RawEncodeWithoutMetadata(blockHeight uint64, skipMetadata ...bool) []byte {
var data []byte
data = append(data, UintToBuf(utxo.AmountNanos)...)
data = append(data, EncodeByteArray(utxo.PublicKey)...)
data = append(data, UintToBuf(uint64(utxo.BlockHeight))...)
data = append(data, byte(utxo.UtxoType))
data = append(data, EncodeToBytes(blockHeight, utxo.UtxoKey, skipMetadata...)...)
return data
}
func (utxo *UtxoEntry) RawDecodeWithoutMetadata(blockHeight uint64, rr *bytes.Reader) error {
var err error
utxo.AmountNanos, err = ReadUvarint(rr)
if err != nil {
return errors.Wrapf(err, "UtxoEntry.Decode: Problem reading AmountNanos")
}
utxo.PublicKey, err = DecodeByteArray(rr)
if err != nil {
return errors.Wrapf(err, "UtxoEntry.Decode: Problem reading PublicKey")
}
utxoBlockHeight, err := ReadUvarint(rr)
if err != nil {
return errors.Wrapf(err, "UtxoEntry.Decode: Problem reading blockHeight")
}
utxo.BlockHeight = uint32(utxoBlockHeight)
utxoType, err := rr.ReadByte()
if err != nil {
return errors.Wrapf(err, "UtxoEntry.Decode: Problem reading UtxoType")
}
utxo.UtxoType = UtxoType(utxoType)
utxoKey := &UtxoKey{}
if exist, err := DecodeFromBytes(utxoKey, rr); exist && err == nil {
utxo.UtxoKey = utxoKey
} else if err != nil {
return errors.Wrapf(err, "UtxoEntry.Decode: Problem reading UtxoKey")
}
return nil
}
func (utxo *UtxoEntry) GetVersionByte(blockHeight uint64) byte {
return 0
}
func (utxo *UtxoEntry) GetEncoderType() EncoderType {
return EncoderTypeUtxoEntry
}
type OperationType uint
const (
// Every operation has a type that we document here. This information is
// used when rolling back a txn to determine what kind of operations need
// to be performed. For example, rolling back a BitcoinExchange may require
// rolling back an AddUtxo operation.
OperationTypeAddUtxo OperationType = 0
OperationTypeSpendUtxo OperationType = 1
OperationTypeBitcoinExchange OperationType = 2
OperationTypePrivateMessage OperationType = 3
OperationTypeSubmitPost OperationType = 4
OperationTypeUpdateProfile OperationType = 5
OperationTypeDeletePost OperationType = 7
OperationTypeUpdateBitcoinUSDExchangeRate OperationType = 8
OperationTypeFollow OperationType = 9
OperationTypeLike OperationType = 10
OperationTypeCreatorCoin OperationType = 11
OperationTypeSwapIdentity OperationType = 12
OperationTypeUpdateGlobalParams OperationType = 13
OperationTypeCreatorCoinTransfer OperationType = 14
OperationTypeCreateNFT OperationType = 15
OperationTypeUpdateNFT OperationType = 16
OperationTypeAcceptNFTBid OperationType = 17
OperationTypeNFTBid OperationType = 18
OperationTypeDeSoDiamond OperationType = 19
OperationTypeNFTTransfer OperationType = 20
OperationTypeAcceptNFTTransfer OperationType = 21
OperationTypeBurnNFT OperationType = 22
OperationTypeAuthorizeDerivedKey OperationType = 23
OperationTypeMessagingKey OperationType = 24
OperationTypeDAOCoin OperationType = 25
OperationTypeDAOCoinTransfer OperationType = 26
OperationTypeSpendingLimitAccounting OperationType = 27
OperationTypeDAOCoinLimitOrder OperationType = 28
// NEXT_TAG = 29
)
func (op OperationType) String() string {
switch op {
case OperationTypeAddUtxo:
{
return "OperationTypeAddUtxo"
}
case OperationTypeSpendUtxo:
{
return "OperationTypeSpendUtxo"
}
case OperationTypeBitcoinExchange:
{
return "OperationTypeBitcoinExchange"
}
case OperationTypePrivateMessage:
{
return "OperationTypePrivateMessage"
}
case OperationTypeSubmitPost:
{
return "OperationTypeSubmitPost"
}
case OperationTypeUpdateProfile:
{
return "OperationTypeUpdateProfile"
}
case OperationTypeDeletePost:
{
return "OperationTypeDeletePost"
}
case OperationTypeUpdateBitcoinUSDExchangeRate:
{
return "OperationTypeUpdateBitcoinUSDExchangeRate"
}
case OperationTypeFollow:
{
return "OperationTypeFollow"
}
case OperationTypeLike:
{
return "OperationTypeLike"
}
case OperationTypeCreatorCoin:
{
return "OperationTypeCreatorCoin"
}
case OperationTypeSwapIdentity:
{
return "OperationTypeSwapIdentity"
}
case OperationTypeUpdateGlobalParams:
{
return "OperationTypeUpdateGlobalParams"
}
case OperationTypeCreatorCoinTransfer:
{
return "OperationTypeCreatorCoinTransfer"
}
case OperationTypeCreateNFT:
{
return "OperationTypeCreateNFT"
}
case OperationTypeUpdateNFT:
{
return "OperationTypeUpdateNFT"
}
case OperationTypeAcceptNFTBid:
{
return "OperationTypeAcceptNFTBid"
}
case OperationTypeNFTBid:
{
return "OperationTypeNFTBid"
}
case OperationTypeDeSoDiamond:
{
return "OperationTypeDeSoDiamond"
}
case OperationTypeNFTTransfer:
{
return "OperationTypeNFTTransfer"
}
case OperationTypeAcceptNFTTransfer:
{
return "OperationTypeAcceptNFTTransfer"
}
case OperationTypeBurnNFT:
{
return "OperationTypeBurnNFT"
}
case OperationTypeAuthorizeDerivedKey:
{
return "OperationTypeAuthorizeDerivedKey"
}
case OperationTypeMessagingKey:
{
return "OperationTypeMessagingKey"
}
case OperationTypeDAOCoin:
{
return "OperationTypeDAOCoin"
}
case OperationTypeDAOCoinTransfer:
{
return "OperationTypeDAOCoinTransfer"
}
case OperationTypeSpendingLimitAccounting:
{
return "OperationTypeSpendingLimitAccounting"
}
case OperationTypeDAOCoinLimitOrder:
{
return "OperationTypeDAOCoinLimitOrder"
}
}
return "OperationTypeUNKNOWN"
}
type UtxoOperation struct {
Type OperationType
// Only set for OperationTypeSpendUtxo
//
// When we SPEND a UTXO entry we delete it from the utxo set but we still
// store its info in case we want to reverse
// it in the future. This information is not needed for ADD since
// reversing an ADD just means deleting an entry from the end of our list.
//
// SPEND works by swapping the UTXO we want to spend with the UTXO at
// the end of the list and then deleting from the end of the list. Obviously
// this is more efficient than deleting the element in-place and then shifting
// over everything after it. In order to be able to undo this operation,
// however, we need to store the original index of the item we are
// spending/deleting. Reversing the operation then amounts to adding a utxo entry
// at the end of the list and swapping with this index. Given this, the entry
// we store here has its position set to the position it was at right before the
// SPEND operation was performed.
Entry *UtxoEntry
// Only set for OperationTypeSpendUtxo
//
// Store the UtxoKey as well. This isn't necessary but it helps
// with error-checking during a roll-back so we just keep it.
//
// TODO: We can probably delete this at some point and save some space. UTXOs
// are probably our biggest disk hog so getting rid of this should materially
// improve disk usage.
Key *UtxoKey
// Used to revert BitcoinExchange transaction.
PrevNanosPurchased uint64
// Used to revert UpdateBitcoinUSDExchangeRate transaction.
PrevUSDCentsPerBitcoin uint64
// Save the previous post entry when making an update to a post.
PrevPostEntry *PostEntry
PrevParentPostEntry *PostEntry
PrevGrandparentPostEntry *PostEntry
PrevRepostedPostEntry *PostEntry
// Save the previous profile entry when making an update.
PrevProfileEntry *ProfileEntry
// Save the previous like entry and like count when making an update.
PrevLikeEntry *LikeEntry
PrevLikeCount uint64
// For disconnecting diamonds.
PrevDiamondEntry *DiamondEntry
// For disconnecting NFTs.
PrevNFTEntry *NFTEntry
PrevNFTBidEntry *NFTBidEntry
DeletedNFTBidEntries []*NFTBidEntry
NFTPaymentUtxoKeys []*UtxoKey
NFTSpentUtxoEntries []*UtxoEntry
PrevAcceptedNFTBidEntries *[]*NFTBidEntry
// For disconnecting AuthorizeDerivedKey transactions.
PrevDerivedKeyEntry *DerivedKeyEntry
// For disconnecting MessagingGroupKey transactions.
PrevMessagingKeyEntry *MessagingGroupEntry
// Save the previous repost entry and repost count when making an update.
PrevRepostEntry *RepostEntry
PrevRepostCount uint64
// Save the state of a creator coin prior to updating it due to a
// buy/sell/add transaction.
PrevCoinEntry *CoinEntry
// Save the state of coin entries associated with a PKID prior to updating
// it due to an additional coin royalty when an NFT is sold.
PrevCoinRoyaltyCoinEntries map[PKID]CoinEntry
// Save the creator coin balance of both the transactor and the creator.
// We modify the transactor's balances when they buys/sell a creator coin
// and we modify the creator's balance when we pay them a founder reward.
PrevTransactorBalanceEntry *BalanceEntry
PrevCreatorBalanceEntry *BalanceEntry
// We use this to revert founder's reward UTXOs created by creator coin buys.
FounderRewardUtxoKey *UtxoKey
// Save balance entries for the sender and receiver when creator coins are transferred.
PrevSenderBalanceEntry *BalanceEntry
PrevReceiverBalanceEntry *BalanceEntry
// Save the global params when making an update.
PrevGlobalParamsEntry *GlobalParamsEntry
PrevForbiddenPubKeyEntry *ForbiddenPubKeyEntry
// This value is used by Rosetta to adjust for a bug whereby a ParamUpdater
// CoinEntry could get clobbered if updating a profile on someone else's
// behalf. This is super confusing.
ClobberedProfileBugDESOLockedNanos uint64
// This value is used by Rosetta to return the amount of DESO that was added
// or removed from a profile during a CreatorCoin transaction. It's needed
// in order to avoid having to reconnect all transactions.
CreatorCoinDESOLockedNanosDiff int64
// This value is used by Rosetta to create a proper input/output when we
// encounter a SwapIdentity txn. This makes it so that we don't have to
// reconnect all txns in order to get these values.
SwapIdentityFromDESOLockedNanos uint64
SwapIdentityToDESOLockedNanos uint64
// These values are used by Rosetta in order to create input and output
// operations. They make it so that we don't have to reconnect all txns
// in order to get these values.
AcceptNFTBidCreatorPublicKey []byte
AcceptNFTBidBidderPublicKey []byte
AcceptNFTBidCreatorRoyaltyNanos uint64
AcceptNFTBidCreatorDESORoyaltyNanos uint64
AcceptNFTBidAdditionalCoinRoyalties []*PublicKeyRoyaltyPair
AcceptNFTBidAdditionalDESORoyalties []*PublicKeyRoyaltyPair
// These values are used by Rosetta in order to create input and output
// operations. They make it so that we don't have to reconnect all txns
// in order to get these values for NFT bid transactions on Buy Now NFTs.
NFTBidCreatorPublicKey []byte
NFTBidBidderPublicKey []byte
NFTBidCreatorRoyaltyNanos uint64
NFTBidCreatorDESORoyaltyNanos uint64
NFTBidAdditionalCoinRoyalties []*PublicKeyRoyaltyPair
NFTBidAdditionalDESORoyalties []*PublicKeyRoyaltyPair
// DAO coin limit order
// PrevTransactorDAOCoinLimitOrderEntry is the previous version of the
// transactor's DAO Coin Limit Order before this transaction was connected.
// Note: This is only set if the transactor is cancelling an existing order.
PrevTransactorDAOCoinLimitOrderEntry *DAOCoinLimitOrderEntry
// PrevBalanceEntries is a map of User PKID, Creator PKID to DAO Coin Balance
// Entry. When disconnecting a DAO Coin Limit Order, we will revert to these
// BalanceEntries.
PrevBalanceEntries map[PKID]map[PKID]*BalanceEntry
// PrevMatchingOrder is a slice of DAOCoinLimitOrderEntries that were deleted
// in the DAO Coin Limit Order Transaction. In order to revert the state in
// the event of a disconnect, we restore all the deleted Order Entries
PrevMatchingOrders []*DAOCoinLimitOrderEntry
// FilledDAOCoinLimitOrder is a slice of FilledDAOCoinLimitOrder structs
// that represent all orders fulfilled by the DAO Coin Limit Order transaction.
// These are used to construct notifications for order fulfillment.
FilledDAOCoinLimitOrders []*FilledDAOCoinLimitOrder
}
func (op *UtxoOperation) RawEncodeWithoutMetadata(blockHeight uint64, skipMetadata ...bool) []byte {
var data []byte
// Type
data = append(data, UintToBuf(uint64(op.Type))...)
// Entry
data = append(data, EncodeToBytes(blockHeight, op.Entry, skipMetadata...)...)
// Key
data = append(data, EncodeToBytes(blockHeight, op.Key, skipMetadata...)...)
// PrevNanosPurchased
data = append(data, UintToBuf(op.PrevNanosPurchased)...)
// PrevUSDCentsPerBitcoin
data = append(data, UintToBuf(op.PrevUSDCentsPerBitcoin)...)
// PrevPostEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevPostEntry, skipMetadata...)...)
// PrevParentPostEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevParentPostEntry, skipMetadata...)...)
// PrevGrandparentPostEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevGrandparentPostEntry, skipMetadata...)...)
// PrevRepostedPostEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevRepostedPostEntry, skipMetadata...)...)
// PrevProfileEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevProfileEntry, skipMetadata...)...)
// PrevLikeEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevLikeEntry, skipMetadata...)...)
// PrevLikeCount
data = append(data, UintToBuf(op.PrevLikeCount)...)
// PrevDiamondEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevDiamondEntry, skipMetadata...)...)
// PrevNFTEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevNFTEntry, skipMetadata...)...)
// PrevNFTBidEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevNFTBidEntry, skipMetadata...)...)
// DeletedNFTBidEntries
data = append(data, UintToBuf(uint64(len(op.DeletedNFTBidEntries)))...)
for _, bidEntry := range op.DeletedNFTBidEntries {
data = append(data, EncodeToBytes(blockHeight, bidEntry, skipMetadata...)...)
}
// NFTPaymentUtxoKeys
data = append(data, UintToBuf(uint64(len(op.NFTPaymentUtxoKeys)))...)
for _, utxoKey := range op.NFTPaymentUtxoKeys {
data = append(data, EncodeToBytes(blockHeight, utxoKey, skipMetadata...)...)
}
// NFTSpentUtxoEntries
data = append(data, UintToBuf(uint64(len(op.NFTSpentUtxoEntries)))...)
for _, utxoEntry := range op.NFTSpentUtxoEntries {
data = append(data, EncodeToBytes(blockHeight, utxoEntry, skipMetadata...)...)
}
// PrevAcceptedNFTBidEntries
// Similarly to op.Entry, we encode an existence flag for the PrevAcceptedNFTBidEntries.
if op.PrevAcceptedNFTBidEntries != nil {
data = append(data, BoolToByte(true))
data = append(data, UintToBuf(uint64(len(*op.PrevAcceptedNFTBidEntries)))...)
for _, bidEntry := range *op.PrevAcceptedNFTBidEntries {
data = append(data, EncodeToBytes(blockHeight, bidEntry, skipMetadata...)...)
}
} else {
data = append(data, BoolToByte(false))
}
// PrevDerivedKeyEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevDerivedKeyEntry, skipMetadata...)...)
// PrevMessagingKeyEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevMessagingKeyEntry, skipMetadata...)...)
// PrevRepostEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevRepostEntry, skipMetadata...)...)
// PrevRepostCount
data = append(data, UintToBuf(op.PrevRepostCount)...)
// PrevCoinEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevCoinEntry, skipMetadata...)...)
// Encode the PrevCoinRoyaltyCoinEntries map. We define a helper struct to store the <PKID, CoinEntry>
// objects as byte arrays. For coin entry, we first encode the struct, and then encode it as byte array.
type royaltyEntry struct {
pkid []byte
coinEntry []byte
}
encodeRoyaltyEntry := func(entry *royaltyEntry) []byte {
var data []byte
data = append(data, EncodeByteArray(entry.pkid)...)
data = append(data, EncodeByteArray(entry.coinEntry)...)
return data
}
var royaltyCoinEntries []*royaltyEntry
if op.PrevCoinRoyaltyCoinEntries != nil {
data = append(data, BoolToByte(true))
data = append(data, UintToBuf(uint64(len(op.PrevCoinRoyaltyCoinEntries)))...)
for pkid, coinEntry := range op.PrevCoinRoyaltyCoinEntries {
newPKID := pkid
newCoin := coinEntry
royaltyCoinEntries = append(royaltyCoinEntries, &royaltyEntry{
pkid: newPKID.ToBytes(),
coinEntry: EncodeToBytes(blockHeight, &newCoin, skipMetadata...),
})
}
sort.Slice(royaltyCoinEntries, func(i int, j int) bool {
switch bytes.Compare(royaltyCoinEntries[i].pkid, royaltyCoinEntries[j].pkid) {
case 0:
return true
case -1:
return true
case 1:
return false
}
return false
})
for _, entry := range royaltyCoinEntries {
data = append(data, encodeRoyaltyEntry(entry)...)
}
} else {
data = append(data, BoolToByte(false))
}
// PrevTransactorBalanceEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevTransactorBalanceEntry, skipMetadata...)...)
// PrevCreatorBalanceEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevCreatorBalanceEntry, skipMetadata...)...)
// FounderRewardUtxoKey
data = append(data, EncodeToBytes(blockHeight, op.FounderRewardUtxoKey, skipMetadata...)...)
// PrevSenderBalanceEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevSenderBalanceEntry, skipMetadata...)...)
// PrevReceiverBalanceEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevReceiverBalanceEntry, skipMetadata...)...)
// PrevGlobalParamsEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevGlobalParamsEntry, skipMetadata...)...)
// PrevForbiddenPubKeyEntry
data = append(data, EncodeToBytes(blockHeight, op.PrevForbiddenPubKeyEntry, skipMetadata...)...)
// ClobberedProfileBugDESOLockedNanos
data = append(data, UintToBuf(op.ClobberedProfileBugDESOLockedNanos)...)
// CreatorCoinDESOLockedNanosDiff
// Note that int64 is encoded identically to uint64, the sign bit is just interpreted differently.
data = append(data, UintToBuf(uint64(op.CreatorCoinDESOLockedNanosDiff))...)
// SwapIdentityFromDESOLockedNanos
data = append(data, UintToBuf(op.SwapIdentityFromDESOLockedNanos)...)
// SwapIdentityToDESOLockedNanos
data = append(data, UintToBuf(op.SwapIdentityToDESOLockedNanos)...)
// AcceptNFTBidCreatorPublicKey
data = append(data, EncodeByteArray(op.AcceptNFTBidCreatorPublicKey)...)
// AcceptNFTBidBidderPublicKey
data = append(data, EncodeByteArray(op.AcceptNFTBidBidderPublicKey)...)
// AcceptNFTBidCreatorRoyaltyNanos
data = append(data, UintToBuf(op.AcceptNFTBidCreatorRoyaltyNanos)...)
// AcceptNFTBidCreatorDESORoyaltyNanos
data = append(data, UintToBuf(op.AcceptNFTBidCreatorDESORoyaltyNanos)...)
// AcceptNFTBidAdditionalCoinRoyalties
data = append(data, UintToBuf(uint64(len(op.AcceptNFTBidAdditionalCoinRoyalties)))...)
for _, pair := range op.AcceptNFTBidAdditionalCoinRoyalties {
data = append(data, EncodeToBytes(blockHeight, pair, skipMetadata...)...)
}
// AcceptNFTBidAdditionalDESORoyalties
data = append(data, UintToBuf(uint64(len(op.AcceptNFTBidAdditionalDESORoyalties)))...)
for _, pair := range op.AcceptNFTBidAdditionalDESORoyalties {
data = append(data, EncodeToBytes(blockHeight, pair, skipMetadata...)...)
}
// NFTBidCreatorPublicKey
data = append(data, EncodeByteArray(op.NFTBidCreatorPublicKey)...)
// NFTBidBidderPublicKey
data = append(data, EncodeByteArray(op.NFTBidBidderPublicKey)...)
// NFTBidCreatorRoyaltyNanos
data = append(data, UintToBuf(op.NFTBidCreatorRoyaltyNanos)...)
// NFTBidCreatorDESORoyaltyNanos
data = append(data, UintToBuf(op.NFTBidCreatorDESORoyaltyNanos)...)
// NFTBidAdditionalCoinRoyalties
data = append(data, UintToBuf(uint64(len(op.NFTBidAdditionalCoinRoyalties)))...)
for _, pair := range op.NFTBidAdditionalCoinRoyalties {
data = append(data, EncodeToBytes(blockHeight, pair, skipMetadata...)...)