forked from etotheipi/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 171
/
BlockchainScanner.h
177 lines (137 loc) · 4.9 KB
/
BlockchainScanner.h
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
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2016-17, goatpig. //
// Distributed under the MIT license //
// See LICENSE-MIT or https://opensource.org/licenses/MIT //
// //
////////////////////////////////////////////////////////////////////////////////
#ifndef _BLOCKCHAINSCANNER_H
#define _BLOCKCHAINSCANNER_H
#include "Blockchain.h"
#include "lmdb_wrapper.h"
#include "BDM_supportClasses.h"
#include "BlockDataMap.h"
#include "Progress.h"
#include "bdmenums.h"
#include "ThreadSafeClasses.h"
#include <future>
#include <atomic>
#include <exception>
#define BATCH_SIZE 1024 * 1024 * 512ULL
class ScanningException : public runtime_error
{
private:
const unsigned badHeight_;
public:
ScanningException(unsigned badHeight, const string &what = "")
: runtime_error(what), badHeight_(badHeight)
{ }
};
////////////////////////////////////////////////////////////////////////////////
struct ParserBatch
{
public:
map<unsigned, shared_ptr<BlockDataFileMap>> fileMaps_;
atomic<unsigned> blockCounter_;
mutex mergeMutex_;
const unsigned start_;
const unsigned end_;
const unsigned startBlockFileID_;
const unsigned targetBlockFileID_;
map<unsigned, shared_ptr<BlockData>> blockMap_;
map<BinaryData, map<unsigned, StoredTxOut>> outputMap_;
map<BinaryData, map<BinaryData, StoredSubHistory>> sshMap_;
vector<StoredTxOut> spentOutputs_;
const shared_ptr<map<TxOutScriptRef, int>> scriptRefMap_;
promise<bool> completedPromise_;
unsigned count_;
public:
ParserBatch(unsigned start, unsigned end,
unsigned startID, unsigned endID,
shared_ptr<map<TxOutScriptRef, int>> scriptRefMap) :
start_(start), end_(end),
startBlockFileID_(startID), targetBlockFileID_(endID),
scriptRefMap_(scriptRefMap)
{
if (end < start)
throw runtime_error("end > start");
blockCounter_.store(start_, memory_order_relaxed);
}
};
////////////////////////////////////////////////////////////////////////////////
class BlockchainScanner
{
private:
struct TxFilterResults
{
BinaryData hash_;
//map<blockId, set<tx offset>>
map<uint32_t, set<uint32_t>> filterHits_;
bool operator < (const TxFilterResults& rhs) const
{
return hash_ < rhs.hash_;
}
};
shared_ptr<Blockchain> blockchain_;
LMDBBlockDatabase* db_;
ScrAddrFilter* scrAddrFilter_;
BlockDataLoader blockDataLoader_;
const unsigned totalThreadCount_;
const unsigned writeQueueDepth_;
const unsigned totalBlockFileCount_;
BinaryData topScannedBlockHash_;
ProgressCallback progress_ =
[](BDMPhase, double, unsigned, unsigned)->void{};
bool reportProgress_ = false;
//only for relevant utxos
map<BinaryData, map<unsigned, StoredTxOut>> utxoMap_;
unsigned startAt_ = 0;
mutex resolverMutex_;
BlockingStack<unique_ptr<ParserBatch>> outputQueue_;
BlockingStack<unique_ptr<ParserBatch>> inputQueue_;
BlockingStack<unique_ptr<ParserBatch>> commitQueue_;
atomic<unsigned> completedBatches_;
private:
void writeBlockData(void);
void processAndCommitTxHints(ParserBatch*);
void preloadUtxos(void);
int32_t check_merkle(int32_t startHeight);
void getFilterHitsThread(
const set<BinaryData>& hashSet,
atomic<int>& counter,
map<uint32_t, set<TxFilterResults>>& resultMap);
void processFilterHitsThread(
map<uint32_t, map<uint32_t,
set<const TxFilterResults*>>>& filtersResultMap,
TransactionalSet<BinaryData>& missingHashes,
atomic<int>& counter, map<BinaryData, BinaryData>& results,
function<void(size_t)> prog);
shared_ptr<BlockData> getBlockData(
ParserBatch*, unsigned);
void processOutputs(void);
void processOutputsThread(ParserBatch*);
void processInputs(void);
void processInputsThread(ParserBatch*);
public:
BlockchainScanner(shared_ptr<Blockchain> bc, LMDBBlockDatabase* db,
ScrAddrFilter* saf,
BlockFiles& bf,
unsigned threadcount, unsigned queue_depth,
ProgressCallback prg, bool reportProgress) :
blockchain_(bc), db_(db), scrAddrFilter_(saf),
totalThreadCount_(threadcount), writeQueueDepth_(queue_depth),
blockDataLoader_(bf.folderPath()),
progress_(prg), reportProgress_(reportProgress),
totalBlockFileCount_(bf.fileCount())
{}
void scan(int32_t startHeight);
void scan_nocheck(int32_t startHeight);
void undo(Blockchain::ReorganizationState& reorgState);
void updateSSH(bool);
bool resolveTxHashes();
const BinaryData& getTopScannedBlockHash(void) const
{
return topScannedBlockHash_;
}
};
#endif