forked from goatpig/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockchainScanner.cpp
More file actions
1751 lines (1421 loc) · 50.7 KB
/
BlockchainScanner.cpp
File metadata and controls
1751 lines (1421 loc) · 50.7 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) 2016, goatpig. //
// Distributed under the MIT license //
// See LICENSE-MIT or https://opensource.org/licenses/MIT //
// //
////////////////////////////////////////////////////////////////////////////////
#include "BlockchainScanner.h"
#include "log.h"
////////////////////////////////////////////////////////////////////////////////
void BlockchainScanner::scan(int32_t scanFrom)
{
scanFrom = check_merkle(scanFrom);
if (scanFrom == INT32_MIN)
return;
scan_nocheck(scanFrom);
}
////////////////////////////////////////////////////////////////////////////////
int32_t BlockchainScanner::check_merkle(int32_t scanFrom)
{
auto& topBlock = blockchain_->top();
scrAddrFilter_->updateAddressMerkleInDB();
auto&& subsshSdbi = scrAddrFilter_->getSubSshSDBI();
BlockHeader* sdbiblock = nullptr;
//check if we need to scan anything
try
{
sdbiblock =
&blockchain_->getHeaderByHash(subsshSdbi.topScannedBlkHash_);
}
catch (...)
{
sdbiblock = &blockchain_->getHeaderByHeight(0);
}
if (sdbiblock->isMainBranch())
{
//this will set scanFrom to 0 before an initial scan
if ((int)sdbiblock->getBlockHeight() > scanFrom)
scanFrom = sdbiblock->getBlockHeight();
if (scanFrom > (int)topBlock.getBlockHeight() ||
scrAddrFilter_->getScrAddrSet()->size() == 0)
{
LOGINFO << "no history to scan";
topScannedBlockHash_ = topBlock.getThisHash();
return INT32_MIN;
}
}
return scanFrom;
}
////////////////////////////////////////////////////////////////////////////////
void BlockchainScanner::scan_nocheck(int32_t scanFrom)
{
TIMER_START("scan_nocheck");
startAt_ = scanFrom;
auto& topBlock = blockchain_->top();
preloadUtxos();
shared_ptr<BatchLink> batchLinkPtr;
//lambdas
auto&& scrRefMap = scrAddrFilter_->getOutScrRefMap();
auto scanBlockDataLambda = [&](shared_ptr<BlockDataBatch> batch)
{ scanBlockData(batch, scrRefMap); };
auto writeBlockDataLambda = [&](void)
{ writeBlockData(batchLinkPtr); };
auto startHeight = scanFrom;
unsigned endHeight = 0;
//start write thread
thread writeThreadID;
shared_ptr<unique_lock<mutex>> batchLock;
{
batchLinkPtr = make_shared<BatchLink>();
batchLock = make_shared<unique_lock<mutex>>(batchLinkPtr->readyToWrite_);
writeThreadID = thread(writeBlockDataLambda);
}
//loop until there are no more blocks available
try
{
while (startHeight <= topBlock.getBlockHeight())
{
//figure out how many blocks to pull for this batch
//batches try to grab up nBlockFilesPerBatch_ worth of block data
unsigned targetHeight = 0;
try
{
BlockHeader* currentHeader =
&(blockchain_->getHeaderByHeight(startHeight));
auto currentBlkFileNum = currentHeader->getBlockFileNum();
auto targetBlkFileNum = currentBlkFileNum + nBlockFilesPerBatch_;
targetHeight = startHeight;
while (currentHeader->getBlockFileNum() < targetBlkFileNum)
currentHeader = &(blockchain_->getHeaderByHeight(++targetHeight));
}
catch (range_error& e)
{
//if getHeaderByHeight throws before targetHeight is topBlock's height,
//something went wrong. Otherwise we just hit the end of the chain.
if (targetHeight < topBlock.getBlockHeight())
throw e;
else
targetHeight = topBlock.getBlockHeight();
}
endHeight = targetHeight;
//start batch reader threads
vector<thread> tIDs;
vector<shared_ptr<BlockDataBatch>> batchVec;
atomic<unsigned> blockCounter;
blockCounter.store(startHeight, memory_order_relaxed);
//start batch scanner threads
vector<unique_lock<mutex>> lockVec;
for (unsigned i = 0; i < totalThreadCount_; i++)
{
shared_ptr<BlockDataBatch> batch
= make_shared<BlockDataBatch>(endHeight, &blockCounter);
batchVec.push_back(batch);
//lock each batch mutex before start scan thread
lockVec.push_back(unique_lock<mutex>(batchVec[i]->parseTxinMutex_));
tIDs.push_back(thread(scanBlockDataLambda, batch));
}
//wait for utxo scan to complete
for (unsigned i = 0; i < totalThreadCount_; i++)
{
auto utxoScanFlag = batchVec[i]->doneScanningUtxos_;
utxoScanFlag.get();
if (batchVec[i]->exceptionPtr_ != nullptr)
rethrow_exception(batchVec[i]->exceptionPtr_);
}
//update utxoMap_
for (auto& batch : batchVec)
{
for (auto& txidMap : batch->utxos_)
{
utxoMap_[txidMap.first].insert(
txidMap.second.begin(), txidMap.second.end());
}
}
//signal txin scan by releasing all mutexes
lockVec.clear();
//wait until txins are scanned
for (auto& tID : tIDs)
{
if (tID.joinable())
tID.join();
}
//push scanned batch to write thread
accumulateDataBeforeBatchWrite(batchVec);
auto currentBatchPtr = batchLinkPtr;
batchLinkPtr = make_shared<BatchLink>();
auto currentBatchLock = batchLock;
batchLock = make_shared<unique_lock<mutex>>(batchLinkPtr->readyToWrite_);
currentBatchPtr->topScannedBlockHash_ = topScannedBlockHash_;
currentBatchPtr->batchVec_ = batchVec;
currentBatchPtr->next_ = batchLinkPtr;
currentBatchPtr->start_ = startHeight;
currentBatchPtr->end_ = endHeight;
currentBatchLock.reset();
//TODO: add a mechanism to wait on the write thread so as to not
//exhaust RAM with batches queued for writing
//increment startBlock
startHeight = endHeight + 1;
}
}
catch (range_error&)
{
LOGERR << "failed to grab block data starting height: " << startHeight;
if (startHeight == scanFrom)
LOGERR << "no block data was scanned";
}
catch (...)
{
LOGWARN << "scanning halted unexpectedly";
//let the scan terminate
}
//push termination batch to write thread and wait till it exits
batchLinkPtr->next_ = nullptr;
batchLock.reset();
if (writeThreadID.joinable())
writeThreadID.join();
TIMER_STOP("scan_nocheck");
if (topBlock.getBlockHeight() - scanFrom > 100)
{
auto timeSpent = TIMER_READ_SEC("scan_nocheck");
LOGINFO << "scanned transaction history in " << timeSpent << "s";
}
}
////////////////////////////////////////////////////////////////////////////////
void BlockchainScanner::scanBlockData(shared_ptr<BlockDataBatch> batch,
const map<TxOutScriptRef, int>& scrRefSet)
{
//getBlock lambda
auto getBlock = [&](unsigned height)->const BlockData&
{
try
{
//grab block file map
BlockHeader* blockheader = nullptr;
blockheader = &blockchain_->getHeaderByHeight(height);
auto filenum = blockheader->getBlockFileNum();
auto mapIter = batch->fileMaps_.find(filenum);
if (mapIter == batch->fileMaps_.end())
{
//we haven't grabbed that file map yet
auto insertPair = batch->fileMaps_.insert(
make_pair(filenum, move(blockDataLoader_.get(filenum, true))));
mapIter = insertPair.first;
}
auto filemap = mapIter->second.get();
//find block and deserialize it
auto getID = [blockheader](void)->unsigned int
{
return blockheader->getThisID();
};
auto& bdata = batch->blocks_[height];
bdata.deserialize(
filemap->getPtr() + blockheader->getOffset(),
blockheader->getBlockSize(),
blockheader, getID, false);
return bdata;
}
catch (BlockDeserializingException& e)
{
LOGERR << e.what();
batch->exceptionPtr_ = current_exception();
throw e;
}
catch (...)
{
LOGERR << "unknown block deser error during scan at height #" << height;
batch->exceptionPtr_ = current_exception();
rethrow_exception(current_exception());
}
};
//txout parser lambda
auto txoutLoop = [&](function<void(const BlockData&)> callback)
{
unsigned currentBlock;
while (1)
{
currentBlock =
batch->blockCounter_->fetch_add(1, memory_order_relaxed);
if (currentBlock > batch->end_)
break;
auto& bdata = getBlock(currentBlock);
if (!bdata.isInitialized())
return;
callback(bdata);
}
};
//txin parser lambda
auto txinLoop = [&](function<void(const BlockData&)> callback)
{
for (auto& bdata : batch->blocks_)
callback(bdata.second);
};
//txout lambda
auto txoutParser = [&](const BlockData& blockdata)->void
{
//TODO: flag isMultisig
const BlockHeader* header = blockdata.header();
//update processed height
auto topHeight = header->getBlockHeight();
batch->highestProcessedHeight_ = topHeight;
auto& txns = blockdata.getTxns();
for (unsigned i = 0; i < txns.size(); i++)
{
const BCTX& txn = *(txns[i].get());
for (unsigned y = 0; y < txn.txouts_.size(); y++)
{
auto& txout = txn.txouts_[y];
BinaryRefReader brr(
txn.data_ + txout.first, txout.second);
brr.advance(8);
unsigned scriptSize = (unsigned)brr.get_var_int();
auto&& scrRef = BtcUtils::getTxOutScrAddrNoCopy(
brr.get_BinaryDataRef(scriptSize));
auto saIter = scrRefSet.find(scrRef);
if (saIter == scrRefSet.end())
continue;
if (saIter->second >= (int)blockdata.header()->getBlockHeight())
continue;
//if we got this far, this txout is ours
//get tx hash
auto& txHash = txn.getHash();
auto&& scrAddr = scrRef.getScrAddr();
//construct StoredTxOut
StoredTxOut stxo;
stxo.dataCopy_ = BinaryData(
txn.data_ + txout.first, txout.second);
stxo.parentHash_ = txHash;
stxo.blockHeight_ = header->getBlockHeight();
stxo.duplicateID_ = header->getDuplicateID();
stxo.txIndex_ = i;
stxo.txOutIndex_ = y;
stxo.scrAddr_ = scrAddr;
stxo.spentness_ = TXOUT_UNSPENT;
stxo.parentTxOutCount_ = txn.txouts_.size();
stxo.isCoinbase_ = txn.isCoinbase_;
auto value = stxo.getValue();
auto&& hgtx = DBUtils::heightAndDupToHgtx(
stxo.blockHeight_, stxo.duplicateID_);
auto&& txioKey = DBUtils::getBlkDataKeyNoPrefix(
stxo.blockHeight_, stxo.duplicateID_,
i, y);
//update utxos_
auto& stxoHashMap = batch->utxos_[txHash];
stxoHashMap.insert(make_pair(y, move(stxo)));
//update ssh_
auto& ssh = batch->ssh_[scrAddr];
auto& subssh = ssh.subHistMap_[hgtx];
//deal with txio count in subssh at serialization
TxIOPair txio;
txio.setValue(value);
txio.setTxOut(txioKey);
txio.setFromCoinbase(txn.isCoinbase_);
subssh.txioMap_.insert(make_pair(txioKey, move(txio)));
}
}
};
//txin lambda
auto txinParser = [&](const BlockData& blockdata)->void
{
const BlockHeader* header = blockdata.header();
auto& txns = blockdata.getTxns();
for (unsigned i = 0; i < txns.size(); i++)
{
const BCTX& txn = *(txns[i].get());
for (unsigned y = 0; y < txn.txins_.size(); y++)
{
auto& txin = txn.txins_[y];
BinaryDataRef outHash(
txn.data_ + txin.first, 32);
auto utxoIter = utxoMap_.find(outHash);
if (utxoIter == utxoMap_.end())
continue;
unsigned txOutId = READ_UINT32_LE(
txn.data_ + txin.first + 32);
auto idIter = utxoIter->second.find(txOutId);
if (idIter == utxoIter->second.end())
continue;
//if we got this far, this txins consumes one of our utxos
//create spent txout
auto&& hgtx = DBUtils::getBlkDataKeyNoPrefix(
header->getBlockHeight(), header->getDuplicateID());
auto&& txinkey = DBUtils::getBlkDataKeyNoPrefix(
header->getBlockHeight(), header->getDuplicateID(),
i, y);
StoredTxOut stxo = idIter->second;
stxo.spentness_ = TXOUT_SPENT;
stxo.spentByTxInKey_ = txinkey;
//set spenderHash and parentTxOutCount to count and hash tallying
//of spent txouts
stxo.spenderHash_ = txn.getHash();
stxo.parentTxOutCount_ = txn.txouts_.size();
//add to ssh_
auto& ssh = batch->ssh_[stxo.getScrAddress()];
auto& subssh = ssh.subHistMap_[hgtx];
//deal with txio count in subssh at serialization
TxIOPair txio;
auto&& txoutkey = stxo.getDBKey(false);
txio.setTxOut(txoutkey);
txio.setTxIn(txinkey);
txio.setValue(stxo.getValue());
subssh.txioMap_[txoutkey] = move(txio);
//add to spentTxOuts_
batch->spentTxOuts_.push_back(move(stxo));
}
}
};
//txout loop
txoutLoop(txoutParser);
//done with txouts, fill the future flag and wait on the mutex
//to move to txins processing
batch->flagUtxoScanDone();
unique_lock<mutex> txinLock(batch->parseTxinMutex_);
//txins loop
txinLoop(txinParser);
}
////////////////////////////////////////////////////////////////////////////////
void BlockchainScanner::accumulateDataBeforeBatchWrite(
vector<shared_ptr<BlockDataBatch>>& batchVec)
{
//build list of all spent txouts
vector<StoredTxOut> spentTxOuts;
for (auto& batch : batchVec)
{
spentTxOuts.insert(spentTxOuts.end(),
batch->spentTxOuts_.begin(), batch->spentTxOuts_.end());
}
//prune spent txouts from utxoMap_
for (auto& spentTxOut : spentTxOuts)
{
auto utxoIter = utxoMap_.find(spentTxOut.parentHash_);
if (utxoIter == utxoMap_.end())
{
LOGERR << "stxo parent hash not in utxo map";
continue;
}
auto idIter = utxoIter->second.find(spentTxOut.txOutIndex_);
if (idIter == utxoIter->second.end())
{
LOGERR << "stxo txoutid not in utxo map";
continue;
}
utxoIter->second.erase(idIter);
if (utxoIter->second.size() == 0)
utxoMap_.erase(utxoIter);
}
//figure out top scanned block hash
unsigned topScannedBlockHeight = 0;
for (auto& batch : batchVec)
{
if (batch->end_ > topScannedBlockHeight)
topScannedBlockHeight = batch->end_;
}
auto& header = blockchain_->getHeaderByHeight(topScannedBlockHeight);
topScannedBlockHash_ = header.getThisHash();
}
////////////////////////////////////////////////////////////////////////////////
void BlockchainScanner::writeBlockData(
shared_ptr<BatchLink> batchLinkPtr)
{
auto getGlobalOffsetForBlock = [&](unsigned height)->size_t
{
auto& header = blockchain_->getHeaderByHeight(height);
size_t val = header.getBlockFileNum();
val *= 128 * 1024 * 1024;
val += header.getOffset();
return val;
};
ProgressCalculator calc(getGlobalOffsetForBlock(
blockchain_->top().getBlockHeight()));
auto initVal = getGlobalOffsetForBlock(startAt_);
calc.init(initVal);
if (reportProgress_)
progress_(BDMPhase_Rescan,
calc.fractionCompleted(), UINT32_MAX,
initVal);
auto writeHintsLambda =
[&](const vector<shared_ptr<BlockDataBatch>>& batchVec)->void
{ processAndCommitTxHints(batchVec); };
while (1)
{
if (batchLinkPtr == nullptr)
break;
{
unique_lock<mutex> batchIsReady(batchLinkPtr->readyToWrite_);
}
if (batchLinkPtr->next_ == nullptr)
break;
//start txhint writer thread
thread writeHintsThreadId =
thread(writeHintsLambda, batchLinkPtr->batchVec_);
auto& topheader =
blockchain_->getHeaderByHash(batchLinkPtr->topScannedBlockHash_);
auto topHeight = topheader.getBlockHeight();
//serialize data
map<BinaryData, BinaryWriter> serializedSubSSH;
map<BinaryData, BinaryWriter> serializedStxo;
{
for (auto& batchPtr : batchLinkPtr->batchVec_)
{
for (auto& ssh : batchPtr->ssh_)
{
for (auto& subssh : ssh.second.subHistMap_)
{
//TODO: modify subssh serialization to fit our needs
BinaryWriter subsshkey;
subsshkey.put_uint8_t(DB_PREFIX_SCRIPT);
subsshkey.put_BinaryData(ssh.first);
subsshkey.put_BinaryData(subssh.first);
auto& bw = serializedSubSSH[subsshkey.getDataRef()];
subssh.second.serializeDBValue(
bw, db_, ARMORY_DB_BARE);
}
}
for (auto& utxomap : batchPtr->utxos_)
{
auto&& txHashPrefix = utxomap.first.getSliceCopy(0, 4);
for (auto& utxo : utxomap.second)
{
auto& bw = serializedStxo[utxo.second.getDBKey()];
utxo.second.serializeDBValue(
bw, ARMORY_DB_BARE, true);
}
}
}
}
//we've serialized utxos, now let's do another pass for spent txouts
//to make sure they overwrite utxos that were found and spent within
//the same batch
for (auto& batchPtr : batchLinkPtr->batchVec_)
{
for (auto& stxo : batchPtr->spentTxOuts_)
{
auto& bw = serializedStxo[stxo.getDBKey()];
if (bw.getSize() > 0)
bw.reset();
stxo.serializeDBValue(
bw, ARMORY_DB_BARE, true);
}
}
//write data
{
//txouts
LMDBEnv::Transaction tx;
db_->beginDBTransaction(&tx, STXO, LMDB::ReadWrite);
for (auto& stxo : serializedStxo)
{
//TODO: dont rewrite utxos, check if they are already in DB first
db_->putValue(STXO,
stxo.first.getRef(),
stxo.second.getDataRef());
}
}
{
//subssh
LMDBEnv::Transaction tx;
db_->beginDBTransaction(&tx, SUBSSH, LMDB::ReadWrite);
for (auto& subssh : serializedSubSSH)
{
db_->putValue(
SUBSSH,
subssh.first.getRef(),
subssh.second.getDataRef());
}
//update SUBSSH sdbi
auto&& sdbi = scrAddrFilter_->getSubSshSDBI();
sdbi.topBlkHgt_ = batchLinkPtr->batchVec_[0]->end_;
sdbi.topScannedBlkHash_ = batchLinkPtr->topScannedBlockHash_;
scrAddrFilter_->putSubSshSDBI(sdbi);
}
//wait on writeHintsThreadId
if (writeHintsThreadId.joinable())
writeHintsThreadId.join();
LOGINFO << "scanned from height #" << batchLinkPtr->start_
<< " to #" << batchLinkPtr->end_;
size_t progVal = getGlobalOffsetForBlock(batchLinkPtr->batchVec_[0]->end_);
calc.advance(progVal);
if (reportProgress_)
progress_(BDMPhase_Rescan,
calc.fractionCompleted(), calc.remainingSeconds(),
progVal);
batchLinkPtr = batchLinkPtr->next_;
}
}
////////////////////////////////////////////////////////////////////////////////
void BlockchainScanner::processAndCommitTxHints(
const vector<shared_ptr<BlockDataBatch>>& batchVec)
{
map<BinaryData, StoredTxHints> txHints;
map<BinaryData, BinaryWriter> countAndHash;
auto addTxHint =
[&](StoredTxHints& stxh, const StoredTxOut& utxo)->void
{
auto&& utxokey = utxo.getDBKeyOfParentTx(false);
//make sure key isn't already in there
for (auto& key : stxh.dbKeyList_)
{
if (key == utxokey)
return;
}
stxh.dbKeyList_.push_back(move(utxokey));
};
auto addTxHintMap =
[&](const pair<BinaryData, map<unsigned, StoredTxOut>>& utxomap)->void
{
auto&& txHashPrefix = utxomap.first.getSliceCopy(0, 4);
StoredTxHints& stxh = txHints[txHashPrefix];
//pull txHint from DB first, don't want to override
//existing hints
if (stxh.isNull())
db_->getStoredTxHints(stxh, txHashPrefix);
for (auto& utxo : utxomap.second)
{
addTxHint(stxh, utxo.second);
}
stxh.preferredDBKey_ = stxh.dbKeyList_.front();
//count and hash
auto& stxo = utxomap.second.begin()->second;
auto& bw = countAndHash[stxo.getDBKeyOfParentTx(true)];
if (bw.getSize() != 0)
return;
bw.put_uint32_t(stxo.parentTxOutCount_);
bw.put_BinaryData(utxomap.first);
};
{
LMDBEnv::Transaction hintdbtx;
db_->beginDBTransaction(&hintdbtx, TXHINTS, LMDB::ReadOnly);
for (auto& batchPtr : batchVec)
{
for (auto& utxomap : batchPtr->utxos_)
{
addTxHintMap(utxomap);
}
map<BinaryData, map<unsigned, StoredTxOut>> spentTxOutMap;
for (auto& stxo : batchPtr->spentTxOuts_)
{
auto& stxomap = spentTxOutMap[stxo.spenderHash_];
StoredTxOut spentstxo;
spentstxo.parentHash_ = stxo.spenderHash_;
spentstxo.blockHeight_ =
DBUtils::hgtxToHeight(stxo.spentByTxInKey_.getSliceRef(0, 4));
spentstxo.duplicateID_ =
DBUtils::hgtxToDupID(stxo.spentByTxInKey_.getSliceRef(0, 4));
spentstxo.txIndex_ =
READ_UINT16_BE(stxo.spentByTxInKey_.getSliceRef(4, 2));
spentstxo.txOutIndex_ =
READ_UINT16_BE(stxo.spentByTxInKey_.getSliceRef(6, 2));
spentstxo.parentTxOutCount_ = stxo.parentTxOutCount_;
stxomap.insert(move(
make_pair(spentstxo.txOutIndex_, move(spentstxo))));
}
for (auto& stxomap : spentTxOutMap)
addTxHintMap(stxomap);
}
}
map<BinaryData, BinaryWriter> serializedHints;
//serialize
for (auto& txhint : txHints)
{
auto& bw = serializedHints[txhint.second.getDBKey()];
txhint.second.serializeDBValue(bw);
}
//write
{
LMDBEnv::Transaction hintdbtx;
db_->beginDBTransaction(&hintdbtx, TXHINTS, LMDB::ReadWrite);
for (auto& txhint : serializedHints)
{
db_->putValue(TXHINTS,
txhint.first.getRef(),
txhint.second.getDataRef());
}
for (auto& cah : countAndHash)
{
db_->putValue(TXHINTS,
cah.first.getRef(),
cah.second.getDataRef());
}
}
}
////////////////////////////////////////////////////////////////////////////////
void BlockchainScanner::updateSSH(bool force)
{
//loop over all subssh entiers in SUBSSH db,
//compile balance, txio count and summary map for each address
//now also resolves unhinted tx hashes
if (reportProgress_)
progress_(BDMPhase_Balance, 0, 0, 0);
StoredDBInfo sdbi = scrAddrFilter_->getSshSDBI();
{
BlockHeader* sdbiblock = nullptr;
try
{
sdbiblock = &blockchain_->getHeaderByHash(sdbi.topScannedBlkHash_);
}
catch (...)
{
sdbiblock = &blockchain_->getHeaderByHeight(0);
}
if (sdbiblock->isMainBranch())
{
if (sdbi.topBlkHgt_ != 0 &&
sdbi.topBlkHgt_ >= blockchain_->top().getBlockHeight())
{
if (!force)
{
LOGINFO << "no SSH to scan";
return;
}
}
}
}
bool resolveHashes = false;
{
//check for db mode against HEADERS db since it the only one that
//doesn't change through rescans
auto headersSdbi = db_->getStoredDBInfo(HEADERS, 0);
if (headersSdbi.armoryType_ == ARMORY_DB_FULL)
resolveHashes = true;
}
set<BinaryData> txnsToResolve;
//process ssh, list missing hashes for hash resolver
map<BinaryData, StoredScriptHistory> sshMap_;
auto scrAddrSet = scrAddrFilter_->getScrAddrSet();
{
StoredScriptHistory* sshPtr = nullptr;
LMDBEnv::Transaction historyTx, sshTx;
db_->beginDBTransaction(&historyTx, SSH, LMDB::ReadOnly);
db_->beginDBTransaction(&sshTx, SUBSSH, LMDB::ReadOnly);
auto sshIter = db_->getIterator(SUBSSH);
sshIter.seekToStartsWith(DB_PREFIX_SCRIPT);
do
{
while (sshIter.isValid())
{
if (sshPtr != nullptr &&
sshIter.getKeyRef().contains(sshPtr->uniqueKey_))
break;
//new address
auto&& subsshkey = sshIter.getKey();
if (subsshkey.getSize() < 5)
{
LOGWARN << "invalid scrAddr in SUBSSH db";
sshIter.advanceAndRead();
continue;
}
auto sshKey = subsshkey.getSliceRef(1, subsshkey.getSize() - 5);
auto saIter = scrAddrSet->find(sshKey);
if (saIter == scrAddrSet->end())
{
sshPtr = nullptr;
sshIter.advanceAndRead();
continue;
}
//get what's already in the db
sshPtr = &sshMap_[sshKey];
db_->getStoredScriptHistorySummary(*sshPtr, sshKey);
if (sshPtr->isInitialized())
{
//set iterator at unscanned height
auto hgtx = sshIter.getKeyRef().getSliceRef(-4, 4);
int height = DBUtils::hgtxToHeight(hgtx);
if (sshPtr->tallyHeight_ >= height)
{
//this ssh has already been scanned beyond the height sshIter is at,
//let's set the iterator to the correct height (or the next key)
auto&& newKey = sshIter.getKey().getSliceCopy(0, subsshkey.getSize() - 4);
auto&& newHgtx = DBUtils::heightAndDupToHgtx(
sshPtr->tallyHeight_ + 1, 0);
newKey.append(newHgtx);
sshIter.seekTo(newKey);
continue;
}
}
else
{
sshPtr->uniqueKey_ = sshKey;
break;
}
}
//sanity checks
if (!sshIter.isValid())
break;
//deser subssh
StoredSubHistory subssh;
subssh.unserializeDBKey(sshIter.getKeyRef());
//check dupID
if (db_->getValidDupIDForHeight(subssh.height_) != subssh.dupID_)
continue;
subssh.unserializeDBValue(sshIter.getValueRef());
set<BinaryData> txSet;
for (auto& txioPair : subssh.txioMap_)
{
auto&& keyOfOutput = txioPair.second.getDBKeyOfOutput();
if (resolveHashes)
{
auto&& txKey = keyOfOutput.getSliceRef(0, 6);
txnsToResolve.insert(txKey);
}
if (!txioPair.second.isMultisig())
{
//add up balance
if (txioPair.second.hasTxIn())
{
//check for same block fund&spend
auto&& keyOfInput = txioPair.second.getDBKeyOfInput();
if (keyOfOutput.startsWith(keyOfInput.getSliceRef(0, 4)))
{
//both output and input are part of the same block, skip
continue;
}
if (resolveHashes)
{
//this is to resolve output references in transaction build from
//multiple wallets (i.ei coinjoin)
txnsToResolve.insert(keyOfInput.getSliceRef(0, 6));
}
sshPtr->totalUnspent_ -= txioPair.second.getValue();
}
else
{
sshPtr->totalUnspent_ += txioPair.second.getValue();
}
}
}
//txio count
sshPtr->totalTxioCount_ += subssh.txioCount_;
//build subssh summary
sshPtr->subsshSummary_[subssh.height_] = subssh.txioCount_;
}
while (sshIter.advanceAndRead(DB_PREFIX_SCRIPT));
}
//build txHash refs from listed txins
if (resolveHashes && txnsToResolve.size() > 0)
{
set<BinaryData> allMissingTxHashes;
try
{
allMissingTxHashes = move(scrAddrFilter_->getMissingHashes());
}
catch (runtime_error&)
{
//no missing hashes entry yet, move on
}
for (auto& txid : txnsToResolve)
{
//grab tx
Tx tx;
try
{
tx = move(db_->getFullTxCopy(txid));
}
catch (exception&)
{
continue;
}
//build list of all referred hashes in txins
auto txinCount = tx.getNumTxIn();
auto dataPtr = tx.getPtr();
for (auto i = 0; i < txinCount; i++)
{
auto offset = tx.getTxInOffset(i);