forked from etotheipi/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoredBlockObj.cpp
More file actions
2588 lines (2196 loc) · 79.8 KB
/
StoredBlockObj.cpp
File metadata and controls
2588 lines (2196 loc) · 79.8 KB
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
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2011-2015, Armory Technologies, Inc. //
// Distributed under the GNU Affero General Public License (AGPL v3) //
// See LICENSE or http://www.gnu.org/licenses/agpl.html //
// //
////////////////////////////////////////////////////////////////////////////////
#include <vector>
#include <list>
#include <map>
#include "StoredBlockObj.h"
/////////////////////////////////////////////////////////////////////////////
BinaryData StoredDBInfo::getDBKey(void)
{
// Return a key that is guaranteed to be before all other non-empty
// DB keys
static BinaryData dbinfokey(0);
if(dbinfokey.getSize() == 0)
{
BinaryWriter bw(1);
bw.put_uint8_t((uint8_t)DB_PREFIX_DBINFO);
dbinfokey = bw.getData();
}
return dbinfokey;
}
/////////////////////////////////////////////////////////////////////////////
void StoredDBInfo::unserializeDBValue(BinaryRefReader & brr)
{
if(brr.getSizeRemaining() < 44)
{
magic_.resize(0);
topBlkHgt_ = UINT32_MAX;
topBlkHash_.resize(0);
return;
}
brr.get_BinaryData(magic_, 4);
BitUnpacker<uint32_t> bitunpack(brr);
topBlkHgt_ = brr.get_uint32_t();
appliedToHgt_ = brr.get_uint32_t();
brr.get_BinaryData(topBlkHash_, 32);
armoryVer_ = bitunpack.getBits(4);
armoryType_ = (ARMORY_DB_TYPE)bitunpack.getBits(4);
pruneType_ = (DB_PRUNE_TYPE) bitunpack.getBits(4);
if (brr.getSizeRemaining() == 32)
brr.get_BinaryData(topScannedBlkHash_, 32);
}
/////////////////////////////////////////////////////////////////////////////
void StoredDBInfo::serializeDBValue(BinaryWriter & bw ) const
{
BitPacker<uint32_t> bitpack;
bitpack.putBits((uint32_t)armoryVer_, 4);
bitpack.putBits((uint32_t)armoryType_, 4);
bitpack.putBits((uint32_t)pruneType_, 4);
bw.put_BinaryData(magic_);
bw.put_BitPacker(bitpack);
bw.put_uint32_t(topBlkHgt_); // top blk height
bw.put_uint32_t(appliedToHgt_); // top blk height
bw.put_BinaryData(topBlkHash_);
if (topScannedBlkHash_.getSize())
bw.put_BinaryData(topScannedBlkHash_);
}
////////////////////////////////////////////////////////////////////////////////
void StoredDBInfo::unserializeDBValue(BinaryData const & bd)
{
BinaryRefReader brr(bd);
unserializeDBValue(brr);
}
////////////////////////////////////////////////////////////////////////////////
void StoredDBInfo::unserializeDBValue(BinaryDataRef bdr)
{
BinaryRefReader brr(bdr);
unserializeDBValue(brr);
}
/////////////////////////////////////////////////////////////////////////////
void StoredDBInfo::pprintOneLine(uint32_t indent)
{
for(uint32_t i=0; i<indent; i++)
cout << " ";
cout << "DBINFO: "
<< " TopBlk: " << topBlkHgt_
<< " , " << topBlkHash_.getSliceCopy(0,4).toHexStr().c_str()
<< endl;
}
/////////////////////////////////////////////////////////////////////////////
void StoredHeader::setKeyData(uint32_t hgt, uint8_t dupID)
{
// Set the params for this SBH object
blockHeight_ = hgt;
duplicateID_ = dupID;
// Then trickle down to each StoredTx object (if any)
map<uint16_t, StoredTx>::iterator iter;
for(iter = stxMap_.begin(); iter != stxMap_.end(); iter++)
iter->second.setKeyData(hgt, dupID, iter->first);
}
/////////////////////////////////////////////////////////////////////////////
void DBBlock::setHeightAndDup(uint32_t hgt, uint8_t dupID)
{
blockHeight_ = hgt;
duplicateID_ = dupID;
}
/////////////////////////////////////////////////////////////////////////////
void DBBlock::setHeightAndDup(BinaryData hgtx)
{
blockHeight_ = DBUtils::hgtxToHeight(hgtx);
duplicateID_ = DBUtils::hgtxToDupID(hgtx);
}
/////////////////////////////////////////////////////////////////////////////
bool StoredHeader::haveFullBlock(void) const
{
if(dataCopy_.getSize() != HEADER_SIZE)
return false;
for(uint16_t tx=0; tx<numTx_; tx++)
{
map<uint16_t, StoredTx>::const_iterator iter = stxMap_.find(tx);
if(ITER_NOT_IN_MAP(iter, stxMap_))
return false;
if(!iter->second.haveAllTxOut())
return false;
}
return true;
}
/////////////////////////////////////////////////////////////////////////////
BinaryData StoredHeader::getSerializedBlock(void) const
{
if(!haveFullBlock())
return BinaryData(0);
BinaryWriter bw;
if(numBytes_>0)
bw.reserve(numBytes_+100); // add extra room for header and var_int
bw.put_BinaryData(dataCopy_);
bw.put_var_int(numTx_);
for(uint16_t tx=0; tx<numTx_; tx++)
bw.put_BinaryData(stxMap_.at(tx).getSerializedTx());
return bw.getData();
}
/////////////////////////////////////////////////////////////////////////////
BinaryData DBBlock::getDBKey(bool withPrefix) const
{
if(blockHeight_==UINT32_MAX || duplicateID_==UINT8_MAX)
{
throw std::range_error("Requesting DB key for incomplete SBH");
}
if(withPrefix)
return DBUtils::getBlkDataKey(blockHeight_, duplicateID_);
else
return DBUtils::getBlkDataKeyNoPrefix(blockHeight_, duplicateID_);
}
/////////////////////////////////////////////////////////////////////////////
void DBBlock::createFromBlockHeader(const BlockHeader & bh)
{
if(!bh.isInitialized())
{
LOGERR << "trying to create from uninitialized block header";
return;
}
setHeaderData(bh.serialize());
numTx_ = bh.getNumTx();
numBytes_ = bh.getBlockSize();
blockHeight_ = bh.getBlockHeight();
duplicateID_ = UINT8_MAX;
isMainBranch_ = bh.isMainBranch();
hasBlockHeader_ = true;
}
////////////////////////////////////////////////////////////////////////////////
Tx StoredHeader::getTxCopy(uint16_t i)
{
if(KEY_IN_MAP(i, stxMap_))
return stxMap_[i].getTxCopy();
else
return Tx();
}
////////////////////////////////////////////////////////////////////////////////
BinaryData StoredHeader::getSerializedTx(uint16_t i)
{
if(KEY_IN_MAP(i, stxMap_))
return stxMap_[i].getSerializedTx();
else
return BinaryData(0);
}
/////////////////////////////////////////////////////////////////////////////
void DBBlock::setHeaderData(BinaryData const & header80B)
{
if(header80B.getSize() != HEADER_SIZE)
{
LOGERR << "Asked to unserialize a non-80-byte header";
return;
}
dataCopy_.copyFrom(header80B);
BtcUtils::getHash256(header80B, thisHash_);
}
/////////////////////////////////////////////////////////////////////////////
void StoredHeader::unserializeFullBlock(BinaryRefReader brr,
bool doFrag,
bool withPrefix)
{
if(withPrefix)
{
BinaryData magic = brr.get_BinaryData(4);
uint32_t nBytes = brr.get_uint32_t();
if(brr.getSizeRemaining() < nBytes)
{
LOGERR << "Not enough bytes remaining in BRR to read block";
return;
}
}
uint32_t height = blockHeight_;
uint8_t dupid = duplicateID_;
vector<BinaryData> allTxHashes;
BlockHeader bh(brr);
uint32_t nTx = (uint32_t)brr.get_var_int();
createFromBlockHeader(bh);
numTx_ = nTx;
blockHeight_ = height;
duplicateID_ = dupid;
numBytes_ = HEADER_SIZE + BtcUtils::calcVarIntSize(numTx_);
if(dataCopy_.getSize() != HEADER_SIZE)
{
LOGERR << "Unserializing header did not produce 80-byte object!";
return;
}
if (numBytes_ > brr.getSize())
{
LOGERR << "Anticipated size of block header is more than what we have";
throw BlockDeserializingException();
}
BtcUtils::getHash256(dataCopy_, thisHash_);
for(uint32_t tx=0; tx<nTx; tx++)
{
// We're going to have to come back to the beginning of the tx, later
uint32_t txStart = brr.getPosition();
// Read a regular tx and then convert it
Tx thisTx(brr);
numBytes_ += thisTx.getSize();
//save the hash for merkle computation
allTxHashes.push_back(thisTx.getThisHash());
// Now add it to the map
stxMap_[tx] = StoredTx();
StoredTx & stx = stxMap_[tx];
// Now copy the appropriate data from the vanilla Tx object
stx.createFromTx(thisTx, doFrag, true);
stx.isFragged_ = doFrag;
stx.version_ = thisTx.getVersion();
stx.txIndex_ = tx;
// Regardless of whether the tx is fragged, we still need the STXO map
// to be updated and consistent
brr.resetPosition();
brr.advance(txStart + thisTx.getTxOutOffset(0));
for(uint32_t txo=0; txo < thisTx.getNumTxOut(); txo++)
{
stx.stxoMap_[txo] = StoredTxOut();
StoredTxOut & stxo = stx.stxoMap_[txo];
stxo.unserialize(brr);
stxo.txVersion_ = thisTx.getVersion();
stxo.blockHeight_ = UINT32_MAX;
stxo.duplicateID_ = UINT8_MAX;
stxo.txIndex_ = tx;
stxo.txOutIndex_ = txo;
stxo.isCoinbase_ = thisTx.getTxInCopy(0).isCoinbase();
stxo.parentHash_ = stx.thisHash_;
}
// Sitting at the nLockTime, 4 bytes before the end
brr.advance(4);
// Finally, add the
stxMap_[tx] = stx;
}
//compute the merkle root and compare to the header's
BinaryData computedMerkleRoot = BtcUtils::calculateMerkleRoot(allTxHashes);
if (computedMerkleRoot != bh.getMerkleRoot())
{
LOGERR << "Merkle root mismatch! Raw block data is corrupt!";
throw BlockDeserializingException();
}
}
/////////////////////////////////////////////////////////////////////////////
void StoredHeader::unserializeFullBlock(BinaryDataRef block,
bool doFrag,
bool withPrefix)
{
BinaryRefReader brr(block);
unserializeFullBlock(brr, doFrag, withPrefix);
}
/////////////////////////////////////////////////////////////////////////////
bool StoredHeader::serializeFullBlock(BinaryWriter & bw) const
{
if(!haveFullBlock())
{
LOGERR << "Attempted to serialize full block, but only have partial";
return false;
}
if(numTx_ == UINT32_MAX)
{
LOGERR << "Number of tx not available while serializing full block";
return false;
}
BinaryWriter bwTemp(1024*1024); // preallocate 1 MB which is the limit
bwTemp.put_BinaryData(dataCopy_);
bwTemp.put_var_int(numTx_);
map<uint16_t, StoredTx>::const_iterator iter;
for(iter = stxMap_.begin(); iter != stxMap_.end(); iter++)
{
if(!iter->second.haveAllTxOut())
{
LOGERR << "Don't have all TxOut in tx during serialize full block";
return false;
}
bwTemp.put_BinaryData(iter->second.getSerializedTx());
}
bw.put_BinaryData(bwTemp.getDataRef());
return true;
}
/////////////////////////////////////////////////////////////////////////////
void StoredHeader::addTxToMap(uint16_t txIdx, Tx & tx)
{
StoredTx storedTx;
storedTx.createFromTx(tx);
addStoredTxToMap(txIdx, storedTx);
}
/////////////////////////////////////////////////////////////////////////////
void StoredHeader::addStoredTxToMap(uint16_t txIdx, StoredTx & stx)
{
if(txIdx >= numTx_)
{
LOGERR << "TxIdx is greater than numTx of stored header";
return;
}
stxMap_[txIdx] = stx;
}
/////////////////////////////////////////////////////////////////////////////
void StoredTx::addTxOutToMap(uint16_t idx, TxOut & txout)
{
if(idx >= numTxOut_)
{
LOGERR << "TxOutIdx is greater than numTxOut of stored tx";
return;
}
StoredTxOut stxo;
stxo.unserialize(txout.serialize());
stxoMap_[idx] = stxo;
}
/////////////////////////////////////////////////////////////////////////////
void StoredTx::addStoredTxOutToMap(uint16_t idx, StoredTxOut & stxo)
{
if(idx >= numTxOut_)
{
LOGERR << "TxOutIdx is greater than numTxOut of stored tx";
return;
}
stxoMap_[idx] = stxo;
}
/////////////////////////////////////////////////////////////////////////////
BlockHeader DBBlock::getBlockHeaderCopy(void) const
{
if(!isInitialized())
return BlockHeader();
BlockHeader bh(dataCopy_);
bh.setNumTx(numTx_);
bh.setBlockSize(numBytes_);
bh.setDuplicateID(duplicateID_);
return bh;
}
/////////////////////////////////////////////////////////////////////////////
BinaryData DBBlock::getSerializedBlockHeader(void) const
{
if(!isInitialized())
return BinaryData(0);
return dataCopy_;
}
////////////////////////////////////////////////////////////////////////////////
void DBBlock::unserializeDBValue(DB_SELECT db,
BinaryData const & bd,
bool ignoreMerkle)
{
BinaryRefReader brr(bd);
unserializeDBValue(db, brr, ignoreMerkle);
}
////////////////////////////////////////////////////////////////////////////////
void DBBlock::unserializeDBValue(DB_SELECT db,
BinaryDataRef bdr,
bool ignoreMerkle)
{
BinaryRefReader brr(bdr);
unserializeDBValue(db, brr, ignoreMerkle);
}
/////////////////////////////////////////////////////////////////////////////
void DBBlock::unserializeDBValue( DB_SELECT db,
BinaryRefReader & brr,
bool ignoreMerkle)
{
if(db==HEADERS)
{
brr.get_BinaryData(dataCopy_, HEADER_SIZE);
BinaryData hgtx = brr.get_BinaryData(4);
blockHeight_ = DBUtils::hgtxToHeight(hgtx);
duplicateID_ = DBUtils::hgtxToDupID(hgtx);
BtcUtils::getHash256(dataCopy_, thisHash_);
numBytes_ = brr.get_uint32_t();
}
else if(db==BLKDATA)
{
// Read the flags byte
BitUnpacker<uint32_t> bitunpack(brr);
unserArmVer_ = bitunpack.getBits(4);
unserBlkVer_ = bitunpack.getBits(4);
unserDbType_ = (ARMORY_DB_TYPE) bitunpack.getBits(4);
unserPrType_ = (DB_PRUNE_TYPE) bitunpack.getBits(2);
unserMkType_ = (MERKLE_SER_TYPE)bitunpack.getBits(2);
blockAppliedToDB_ = bitunpack.getBit();
// Unserialize the raw header into the SBH object
brr.get_BinaryData(dataCopy_, HEADER_SIZE);
BtcUtils::getHash256(dataCopy_, thisHash_);
numTx_ = brr.get_uint32_t();
numBytes_ = brr.get_uint32_t();
if(unserArmVer_ != ARMORY_DB_VERSION)
LOGWARN << "Version mismatch in unserialize DB header";
if( !ignoreMerkle )
{
uint32_t currPos = brr.getPosition();
uint32_t totalSz = brr.getSize();
if(unserMkType_ == MERKLE_SER_NONE)
merkle_.resize(0);
else
{
merkleIsPartial_ = (unserMkType_ == MERKLE_SER_PARTIAL);
brr.get_BinaryData(merkle_, totalSz - currPos);
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
void DBBlock::serializeDBValue(
BinaryWriter & bw,
DB_SELECT db,
ARMORY_DB_TYPE dbType,
DB_PRUNE_TYPE pruneType
) const
{
if(!isInitialized())
{
LOGERR << "Attempted to serialize uninitialized block header";
return;
}
if(db==HEADERS)
{
BinaryData hgtx = DBUtils::heightAndDupToHgtx(blockHeight_, duplicateID_);
bw.put_BinaryData(dataCopy_);
bw.put_BinaryData(hgtx);
bw.put_uint32_t(numBytes_);
}
else if(db==BLKDATA)
{
uint32_t version = READ_UINT32_LE(dataCopy_.getPtr());
// TODO: We define merkle serialization types here, but we're not actually
// enforcing it in this function. Either merkle_ member contains
// the correct form of the merkle data or it doesn't. We should
// figure out whether we need to make sure the correct data is
// already here when this function starts, or guarantee the data
// is in the right form as part of this function. For now I'm
// assuming that it's already in the right form, and thus the
// determination of PARTIAL vs FULL is irrelevant
MERKLE_SER_TYPE mtype;
switch(dbType)
{
// If we store all the tx anyway, don't need any/partial merkle trees
case ARMORY_DB_BARE: mtype = MERKLE_SER_NONE; break;
case ARMORY_DB_LITE: mtype = MERKLE_SER_PARTIAL; break;
case ARMORY_DB_PARTIAL: mtype = MERKLE_SER_FULL; break;
case ARMORY_DB_FULL: mtype = MERKLE_SER_NONE; break;
case ARMORY_DB_SUPER: mtype = MERKLE_SER_NONE; break;
default:
LOGERR << "Invalid DB mode in serializeStoredHeaderValue";
}
// Override the above mtype if the merkle data is zero-length
if(merkle_.getSize()==0)
mtype = MERKLE_SER_NONE;
// Create the flags byte
BitPacker<uint32_t> bitpack;
bitpack.putBits((uint32_t)ARMORY_DB_VERSION, 4);
bitpack.putBits((uint32_t)version, 4);
bitpack.putBits((uint32_t)dbType, 4);
bitpack.putBits((uint32_t)pruneType, 2);
bitpack.putBits((uint32_t)mtype, 2);
bitpack.putBit(blockAppliedToDB_);
bw.put_BitPacker(bitpack);
bw.put_BinaryData(dataCopy_);
bw.put_uint32_t(numTx_);
bw.put_uint32_t(numBytes_);
if( mtype != MERKLE_SER_NONE )
{
bw.put_BinaryData(merkle_);
if(merkle_.getSize()==0)
LOGERR << "Expected to serialize merkle tree, but empty string";
}
}
}
/////////////////////////////////////////////////////////////////////////////
void DBBlock::unserializeDBKey(DB_SELECT db, BinaryDataRef key)
{
if(db==BLKDATA)
{
BinaryRefReader brr(key);
if(key.getSize() == 4)
DBUtils::readBlkDataKeyNoPrefix(brr, blockHeight_, duplicateID_);
else if(key.getSize() == 5)
DBUtils::readBlkDataKey(brr, blockHeight_, duplicateID_);
else
LOGERR << "Invalid key for StoredHeader";
}
else
LOGERR << "This method not intended for HEADERS DB";
}
/////////////////////////////////////////////////////////////////////////////
void DBBlock::pprintOneLine(uint32_t indent)
{
for(uint32_t i=0; i<indent; i++)
cout << " ";
cout << "HEADER: " << thisHash_.getSliceCopy(0,4).toHexStr()
<< " (" << blockHeight_ << "," << (uint32_t)duplicateID_ << ")"
<< " #Tx: " << numTx_
<< " Applied: " << (blockAppliedToDB_ ? "T" : "F")
<< endl;
}
/////////////////////////////////////////////////////////////////////////////
void StoredHeader::pprintFullBlock(uint32_t indent)
{
pprintOneLine(indent);
if(numTx_ > 10000)
{
cout << " <No tx to print>" << endl;
return;
}
for(uint32_t i=0; i<numTx_; i++)
stxMap_[i].pprintFullTx(indent+3);
}
/////////////////////////////////////////////////////////////////////////////
BinaryData DBTx::getDBKey(bool withPrefix) const
{
if(blockHeight_ == UINT32_MAX ||
duplicateID_ == UINT8_MAX ||
txIndex_ == UINT16_MAX)
{
LOGERR << "Requesting DB key for incomplete STX";
return BinaryData(0);
}
if(withPrefix)
return DBUtils::getBlkDataKey(blockHeight_, duplicateID_, txIndex_);
else
return DBUtils::getBlkDataKeyNoPrefix(blockHeight_, duplicateID_, txIndex_);
}
/////////////////////////////////////////////////////////////////////////////
BinaryData DBTx::getDBKeyOfChild(uint16_t i, bool withPrefix) const
{
return (getDBKey(withPrefix) + WRITE_UINT16_BE(i));
}
/////////////////////////////////////////////////////////////////////////////
void DBTx::unserialize(BinaryData const & data, bool fragged)
{
BinaryRefReader brr(data);
unserialize(brr, fragged);
}
/////////////////////////////////////////////////////////////////////////////
void DBTx::unserialize(BinaryDataRef data, bool fragged)
{
BinaryRefReader brr(data);
unserialize(brr, fragged);
}
/////////////////////////////////////////////////////////////////////////////
void DBTx::unserialize(BinaryRefReader & brr, bool fragged)
{
vector<size_t> offsetsIn, offsetsOut;
uint32_t nbytes = BtcUtils::StoredTxCalcLength(brr.getCurrPtr(),
fragged,
&offsetsIn,
&offsetsOut);
if(brr.getSizeRemaining() < nbytes)
{
LOGERR << "Not enough bytes in BRR to unserialize StoredTx";
return;
}
brr.get_BinaryData(dataCopy_, nbytes);
isFragged_ = fragged;
numTxOut_ = offsetsOut.size()-1;
version_ = READ_UINT32_LE(dataCopy_.getPtr());
lockTime_ = READ_UINT32_LE(dataCopy_.getPtr() + nbytes - 4);
if(isFragged_)
{
fragBytes_ = nbytes;
numBytes_ = UINT32_MAX;
}
else
{
numBytes_ = nbytes;
uint32_t span = offsetsOut[numTxOut_] - offsetsOut[0];
fragBytes_ = numBytes_ - span;
BtcUtils::getHash256(dataCopy_, thisHash_);
}
}
////////////////////////////////////////////////////////////////////////////////
void DBTx::unserializeDBValue(BinaryData const & bd)
{
BinaryRefReader brr(bd);
unserializeDBValue(brr);
}
////////////////////////////////////////////////////////////////////////////////
void DBTx::unserializeDBValue(BinaryDataRef bdr)
{
BinaryRefReader brr(bdr);
unserializeDBValue(brr);
}
/////////////////////////////////////////////////////////////////////////////
void DBTx::unserializeDBValue(BinaryRefReader & brr)
{
// flags
// DBVersion 4 bits
// TxVersion 2 bits
// HowTxSer 4 bits (FullTxOut, TxNoTxOuts, numTxOutOnly)
BitUnpacker<uint16_t> bitunpack(brr); // flags
unserArmVer_ = bitunpack.getBits(4);
unserTxVer_ = bitunpack.getBits(2);
unserTxType_ = (TX_SERIALIZE_TYPE)bitunpack.getBits(4);
if(unserArmVer_ != ARMORY_DB_VERSION)
LOGWARN << "Version mismatch in unserialize DB tx";
brr.get_BinaryData(thisHash_, 32);
if(unserTxType_ == TX_SER_FULL || unserTxType_ == TX_SER_FRAGGED)
unserialize(brr, unserTxType_==TX_SER_FRAGGED);
else
numTxOut_ = (uint32_t)brr.get_var_int();
if (brr.getSizeRemaining() == 4)
{
//this is for ZC tx, as regular Tx don't have custom time stamps
unixTime_ = brr.get_uint32_t();
}
}
/////////////////////////////////////////////////////////////////////////////
void StoredTx::serializeDBValue(
BinaryWriter & bw,
ARMORY_DB_TYPE dbType,
DB_PRUNE_TYPE
) const
{
TX_SERIALIZE_TYPE serType;
switch(dbType)
{
// In most cases, if storing separate TxOuts, fragged Tx is fine
// UPDATE: I'm not sure there's a good reason to NOT frag ever
case ARMORY_DB_BARE: serType = TX_SER_FRAGGED; break;
case ARMORY_DB_LITE: serType = TX_SER_FRAGGED; break;
case ARMORY_DB_PARTIAL: serType = TX_SER_FRAGGED; break;
case ARMORY_DB_FULL: serType = TX_SER_FRAGGED; break;
case ARMORY_DB_SUPER: serType = TX_SER_FRAGGED; break;
default:
LOGERR << "Invalid DB mode in serializeStoredTxValue";
}
if(serType==TX_SER_FULL && !haveAllTxOut())
{
LOGERR << "Supposed to write out full Tx, but don't have it";
return;
}
if(thisHash_.getSize() == 0)
{
LOGERR << "Do not know tx hash to be able to DB-serialize StoredTx";
return;
}
uint16_t version = (uint16_t)READ_UINT32_LE(dataCopy_.getPtr());
BitPacker<uint16_t> bitpack;
bitpack.putBits((uint16_t)ARMORY_DB_VERSION, 4);
bitpack.putBits((uint16_t)version, 2);
bitpack.putBits((uint16_t)serType, 4);
bw.put_BitPacker(bitpack);
bw.put_BinaryData(thisHash_);
if(serType == TX_SER_FULL)
bw.put_BinaryData(getSerializedTx());
else if(serType == TX_SER_FRAGGED)
bw.put_BinaryData(getSerializedTxFragged());
else
bw.put_var_int(numTxOut_);
}
////////////////////////////////////////////////////////////////////////////////
bool StoredTx::haveAllTxOut(void) const
{
if(!isInitialized())
return false;
if(!isFragged_)
return true;
return stxoMap_.size()==numTxOut_;
}
////////////////////////////////////////////////////////////////////////////////
BinaryData StoredTx::getSerializedTx(void) const
{
if(!isInitialized())
return BinaryData(0);
if(!isFragged_)
return dataCopy_;
else if(!haveAllTxOut())
return BinaryData(0);
BinaryWriter bw;
bw.reserve(numBytes_);
if(numBytes_ != UINT32_MAX)
bw.reserve(numBytes_);
bw.put_BinaryData(dataCopy_.getPtr(), dataCopy_.getSize()-4);
map<uint16_t, StoredTxOut>::const_iterator iter;
uint16_t i=0;
for(iter = stxoMap_.begin(); iter != stxoMap_.end(); iter++, i++)
{
if(iter->first != i)
{
LOGERR << "Indices out of order accessing stxoMap_...?!";
return BinaryData(0);
}
bw.put_BinaryData(iter->second.getSerializedTxOut());
}
bw.put_BinaryData(dataCopy_.getPtr()+dataCopy_.getSize()-4, 4);
return bw.getData();
}
////////////////////////////////////////////////////////////////////////////////
BinaryData DBTx::getSerializedTxFragged(void) const
{
if(!isInitialized())
return BinaryData(0);
if(isFragged_)
return dataCopy_;
if(numBytes_ == UINT32_MAX)
{
LOGERR << "Do not know size of tx in order to serialize it";
return BinaryData(0);
}
BinaryWriter bw;
vector<size_t> outOffsets;
BtcUtils::StoredTxCalcLength(dataCopy_.getPtr(), false, NULL, &outOffsets);
uint32_t firstOut = outOffsets[0];
uint32_t afterLast = outOffsets[outOffsets.size()-1];
uint32_t span = afterLast - firstOut;
BinaryData output(dataCopy_.getSize() - span);
dataCopy_.getSliceRef(0, firstOut).copyTo(output.getPtr());
dataCopy_.getSliceRef(afterLast, 4).copyTo(output.getPtr()+firstOut);
return output;
}
/////////////////////////////////////////////////////////////////////////////
void DBTx::unserializeDBKey(BinaryDataRef key)
{
BinaryRefReader brr(key);
if(key.getSize() == 6)
DBUtils::readBlkDataKeyNoPrefix(brr, blockHeight_, duplicateID_, txIndex_);
else if(key.getSize() == 7)
DBUtils::readBlkDataKey(brr, blockHeight_, duplicateID_, txIndex_);
else
LOGERR << "Invalid key for StoredTx";
}
////////////////////////////////////////////////////////////////////////////////
void DBTx::pprintOneLine(uint32_t indent)
{
for(uint32_t i=0; i<indent; i++)
cout << " ";
cout << "TX: " << thisHash_.getSliceCopy(0,4).toHexStr()
<< " (" << blockHeight_
<< "," << (uint32_t)duplicateID_
<< "," << txIndex_ << ")"
<< " #TXO: " << numTxOut_
<< endl;
}
////////////////////////////////////////////////////////////////////////////////
void StoredTx::pprintFullTx(uint32_t indent)
{
pprintOneLine(indent);
if(numTxOut_ > 10000)
{
cout << " <No txout to print>" << endl;
return;
}
for(uint32_t i=0; i<numTxOut_; i++)
stxoMap_[i].pprintOneLine(indent+3);
}
////////////////////////////////////////////////////////////////////////////////
void StoredTxOut::unserialize(BinaryData const & data)
{
BinaryRefReader brr(data);
unserialize(brr);
}
////////////////////////////////////////////////////////////////////////////////
void StoredTxOut::unserialize(BinaryDataRef data)
{
BinaryRefReader brr(data);
unserialize(brr);
}
////////////////////////////////////////////////////////////////////////////////
void StoredTxOut::unserialize(BinaryRefReader & brr)
{
if(brr.getSizeRemaining() < 8)
{
LOGERR << "Not enough bytes in BRR to unserialize StoredTxOut";
return;
}
uint32_t numBytes = BtcUtils::TxOutCalcLength(brr.getCurrPtr());
if(brr.getSizeRemaining() < numBytes)
{
LOGERR << "Not enough bytes in BRR to unserialize StoredTxOut";
return;
}
brr.get_BinaryData(dataCopy_, numBytes);
}
////////////////////////////////////////////////////////////////////////////////
void StoredTxOut::unserializeDBValue(BinaryData const & bd)
{
BinaryRefReader brr(bd);
unserializeDBValue(brr);
}
////////////////////////////////////////////////////////////////////////////////
void StoredTxOut::unserializeDBValue(BinaryDataRef bdr)
{
BinaryRefReader brr(bdr);
unserializeDBValue(brr);
}
////////////////////////////////////////////////////////////////////////////////
void StoredTxOut::unserializeDBValue(BinaryRefReader & brr)
{
// Similar to TxValue flags
// DBVersion 4 bits
// TxVersion 2 bits
// Spentness 2 bits
BitUnpacker<uint16_t> bitunpack(brr);
unserArmVer_ = bitunpack.getBits(4);
txVersion_ = bitunpack.getBits(2);
spentness_ = (TXOUT_SPENTNESS)bitunpack.getBits(2);
isCoinbase_ = bitunpack.getBit();
unserialize(brr);
if(spentness_ == TXOUT_SPENT && brr.getSizeRemaining()>=8)
spentByTxInKey_ = brr.get_BinaryData(8);
}
////////////////////////////////////////////////////////////////////////////////
void StoredTxOut::serializeDBValue(BinaryWriter & bw, ARMORY_DB_TYPE dbType, DB_PRUNE_TYPE pruneType,
bool forceSaveSpentness) const
{
TXOUT_SPENTNESS writeSpent = spentness_;
if(!forceSaveSpentness)
{
switch(dbType)
{
//// If the DB is in lite or partial modes, we don't bother recording
// spentness (in fact, if it's spent, this entry probably won't even
// be written to the DB).
case ARMORY_DB_BARE: break;
case ARMORY_DB_LITE: writeSpent = TXOUT_SPENTUNK; break;
case ARMORY_DB_PARTIAL: writeSpent = TXOUT_SPENTUNK; break;
case ARMORY_DB_FULL: break;