forked from etotheipi/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockchain.cpp
More file actions
424 lines (363 loc) · 13.4 KB
/
Blockchain.cpp
File metadata and controls
424 lines (363 loc) · 13.4 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
////////////////////////////////////////////////////////////////////////////////
// //
// 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 "Blockchain.h"
#include "util.h"
#ifdef max
#undef max
#endif
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Start Blockchain methods
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Blockchain::Blockchain(const HashString &genesisHash)
: genesisHash_(genesisHash)
{
clear();
}
void Blockchain::clear()
{
newlyParsedBlocks_.clear();
headersByHeight_.resize(0);
headerMap_.clear();
topBlockPtr_ = genesisBlockBlockPtr_ =
&headerMap_[genesisHash_];
}
BlockHeader& Blockchain::addBlock(
const HashString &blockhash,
const BlockHeader &block,
bool suppressVerbose
)
{
if (hasHeaderWithHash(blockhash) && blockhash != genesisHash_ &&
!suppressVerbose)
{ // we don't show this error for the genesis block
LOGWARN << "Somehow tried to add header that's already in map";
LOGWARN << " Header Hash: " << blockhash.copySwapEndian().toHexStr();
}
BlockHeader& bh = headerMap_[blockhash] = block;
return bh;
}
BlockHeader& Blockchain::addBlock(
const HashString &blockhash,
const BlockHeader &block,
uint32_t height, uint8_t dupId)
{
BlockHeader& bh = addBlock(blockhash, block, false);
bh.blockHeight_ = height;
bh.duplicateID_ = dupId;
return bh;
}
BlockHeader& Blockchain::addNewBlock(
const HashString &blockhash,
const BlockHeader &block,
bool suppressVerbose)
{
BlockHeader& bh = addBlock(blockhash, block, suppressVerbose);
newlyParsedBlocks_.push_back(&bh);
return bh;
}
Blockchain::ReorganizationState Blockchain::organize()
{
ReorganizationState st;
st.prevTopBlock = &top();
st.reorgBranchPoint = organizeChain();
st.prevTopBlockStillValid = !st.reorgBranchPoint;
st.hasNewTop = (st.prevTopBlock != &top());
return st;
}
Blockchain::ReorganizationState Blockchain::forceOrganize()
{
ReorganizationState st;
st.prevTopBlock = &top();
st.reorgBranchPoint = organizeChain(true);
st.prevTopBlockStillValid = !st.reorgBranchPoint;
st.hasNewTop = (st.prevTopBlock != &top());
return st;
}
void Blockchain::setDuplicateIDinRAM(
LMDBBlockDatabase* iface, bool forceUpdateDupID)
{
for (const auto& block : headerMap_)
{
if (block.second.isMainBranch_)
iface->setValidDupIDForHeight(
block.second.blockHeight_, block.second.duplicateID_);
}
}
Blockchain::ReorganizationState
Blockchain::findReorgPointFromBlock(const BinaryData& blkHash)
{
BlockHeader *bh = &getHeaderByHash(blkHash);
ReorganizationState st;
st.prevTopBlock = bh;
st.prevTopBlockStillValid = true;
st.hasNewTop = false;
st.reorgBranchPoint = nullptr;
while (!bh->isMainBranch())
{
BinaryData prevHash = bh->getPrevHash();
bh = &getHeaderByHash(prevHash);
}
if (bh != st.prevTopBlock)
{
st.reorgBranchPoint = bh;
st.prevTopBlockStillValid = false;
}
return st;
}
BlockHeader& Blockchain::top() const
{
return *topBlockPtr_;
}
BlockHeader& Blockchain::getGenesisBlock() const
{
return *genesisBlockBlockPtr_;
}
BlockHeader& Blockchain::getHeaderByHeight(unsigned index) const
{
if(index>=headersByHeight_.size())
throw std::range_error("Cannot get block at height " + to_string(index));
return *headersByHeight_[index];
}
bool Blockchain::hasHeaderByHeight(unsigned height) const
{
if (height >= headersByHeight_.size())
return false;
return true;
}
const BlockHeader& Blockchain::getHeaderByHash(HashString const & blkHash) const
{
map<HashString, BlockHeader>::const_iterator it = headerMap_.find(blkHash);
if(ITER_NOT_IN_MAP(it, headerMap_))
throw std::range_error("Cannot find block with hash " + blkHash.copySwapEndian().toHexStr());
else
return it->second;
}
BlockHeader& Blockchain::getHeaderByHash(HashString const & blkHash)
{
map<HashString, BlockHeader>::iterator it = headerMap_.find(blkHash);
if(ITER_NOT_IN_MAP(it, headerMap_))
throw std::range_error("Cannot find block with hash " + blkHash.copySwapEndian().toHexStr());
else
return it->second;
}
bool Blockchain::hasHeaderWithHash(BinaryData const & txHash) const
{
return KEY_IN_MAP(txHash, headerMap_);
}
const BlockHeader& Blockchain::getHeaderPtrForTxRef(const TxRef &txr) const
{
if(txr.isNull())
throw std::range_error("Null TxRef");
uint32_t hgt = txr.getBlockHeight();
uint8_t dup = txr.getDuplicateID();
const BlockHeader& bh= getHeaderByHeight(hgt);
if(bh.getDuplicateID() != dup)
{
throw runtime_error("Requested txref not on main chain (BH dupID is diff)");
}
return bh;
}
////////////////////////////////////////////////////////////////////////////////
// Returns nullptr if the new top block is a direct follower of
// the previous top. Returns the branch point if we had to reorg
// TODO: Figure out if there is an elegant way to deal with a forked
// blockchain containing two equal-length chains
BlockHeader* Blockchain::organizeChain(bool forceRebuild)
{
SCOPED_TIMER("organizeChain");
// Why did this line not through an error? I left here to remind
// myself to go figure it out.
//LOGINFO << ("Organizing chain", (forceRebuild ? "w/ rebuild" : ""));
LOGDEBUG << "Organizing chain " << (forceRebuild ? "w/ rebuild" : "");
// If rebuild, we zero out any original organization data and do a
// rebuild of the chain from scratch. This will need to be done in
// the event that our first call to organizeChain returns false, which
// means part of blockchain that was previously valid, has become
// invalid. Rather than get fancy, just rebuild all which takes less
// than a second, anyway.
if(forceRebuild)
{
map<HashString, BlockHeader>::iterator iter;
for( iter = headerMap_.begin();
iter != headerMap_.end();
iter++)
{
iter->second.difficultySum_ = -1;
iter->second.blockHeight_ = 0;
iter->second.isFinishedCalc_ = false;
iter->second.nextHash_ = BtcUtils::EmptyHash();
iter->second.isMainBranch_ = false;
}
topBlockPtr_ = NULL;
}
// Set genesis block
BlockHeader & genBlock = getGenesisBlock();
genBlock.blockHeight_ = 0;
genBlock.difficultyDbl_ = 1.0;
genBlock.difficultySum_ = 1.0;
genBlock.isMainBranch_ = true;
genBlock.isOrphan_ = false;
genBlock.isFinishedCalc_ = true;
genBlock.isInitialized_ = true;
// If this is the first run, the topBlock is the genesis block
if(topBlockPtr_ == NULL)
topBlockPtr_ = &genBlock;
const BlockHeader& prevTopBlock = top();
// Iterate over all blocks, track the maximum difficulty-sum block
double maxDiffSum = prevTopBlock.getDifficultySum();
for( BlockHeader &header : values(headerMap_))
{
// *** Walk down the chain following prevHash fields, until
// you find a "solved" block. Then walk back up and
// fill in the difficulty-sum values (do not set next-
// hash ptrs, as we don't know if this is the main branch)
// Method returns instantly if block is already "solved"
double thisDiffSum = traceChainDown(header);
if (header.isOrphan_)
{
// disregard this block
}
// Determine if this is the top block. If it's the same diffsum
// as the prev top block, don't do anything
else if(thisDiffSum > maxDiffSum)
{
maxDiffSum = thisDiffSum;
topBlockPtr_ = &header;
}
}
// Walk down the list one more time, set nextHash fields
// Also set headersByHeight_;
bool prevChainStillValid = (topBlockPtr_ == &prevTopBlock);
topBlockPtr_->nextHash_ = BtcUtils::EmptyHash();
BlockHeader* thisHeaderPtr = topBlockPtr_;
headersByHeight_.resize(topBlockPtr_->getBlockHeight()+1);
while( !thisHeaderPtr->isFinishedCalc_ )
{
thisHeaderPtr->isFinishedCalc_ = true;
thisHeaderPtr->isMainBranch_ = true;
thisHeaderPtr->isOrphan_ = false;
headersByHeight_[thisHeaderPtr->getBlockHeight()] = thisHeaderPtr;
// This loop not necessary anymore with the DB implementation
// We need to guarantee that the txs are pointing to the right block
// header, because they could've been linked to an invalidated block
//for(uint32_t i=0; i<thisHeaderPtr->getTxRefPtrList().size(); i++)
//{
//TxRef & tx = *(thisHeaderPtr->getTxRefPtrList()[i]);
//tx.setHeaderPtr(thisHeaderPtr);
//tx.setMainBranch(true);
//}
HashString & childHash = thisHeaderPtr->thisHash_;
thisHeaderPtr = &(headerMap_[thisHeaderPtr->getPrevHash()]);
thisHeaderPtr->nextHash_ = childHash;
if(thisHeaderPtr == &prevTopBlock)
prevChainStillValid = true;
}
// Last header in the loop didn't get added (the genesis block on first run)
thisHeaderPtr->isMainBranch_ = true;
headersByHeight_[thisHeaderPtr->getBlockHeight()] = thisHeaderPtr;
// Force a full rebuild to make sure everything is marked properly
// On a full rebuild, prevChainStillValid should ALWAYS be true
if( !prevChainStillValid )
{
LOGWARN << "Reorg detected!";
organizeChain(true); // force-rebuild blockchain (takes less than 1s)
return thisHeaderPtr;
}
// Let the caller know that there was no reorg
//LOGDEBUG << "Done organizing chain";
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// Start from a node, trace down to the highest solved block, accumulate
// difficulties and difficultySum values. Return the difficultySum of
// this block.
double Blockchain::traceChainDown(BlockHeader & bhpStart)
{
if(bhpStart.difficultySum_ > 0)
return bhpStart.difficultySum_;
// Prepare some data structures for walking down the chain
vector<BlockHeader*> headerPtrStack(headerMap_.size());
vector<double> difficultyStack(headerMap_.size());
uint32_t blkIdx = 0;
// Walk down the chain of prevHash_ values, until we find a block
// that has a definitive difficultySum value (i.e. >0).
BlockHeader* thisPtr = &bhpStart;
while( thisPtr->difficultySum_ < 0)
{
double thisDiff = thisPtr->difficultyDbl_;
difficultyStack[blkIdx] = thisDiff;
headerPtrStack[blkIdx] = thisPtr;
blkIdx++;
map<HashString, BlockHeader>::iterator iter = headerMap_.find(thisPtr->getPrevHash());
if(ITER_IN_MAP(iter, headerMap_))
{
thisPtr = &(iter->second);
}
else
{
thisPtr->isOrphan_ = true;
// this block is an orphan, possibly caused by a HeadersFirst
// blockchain. Nothing to do about that
return numeric_limits<double>::max();
}
}
// Now we have a stack of difficulties and pointers. Walk back up
// (by pointer) and accumulate the difficulty values
double seedDiffSum = thisPtr->difficultySum_;
uint32_t blkHeight = thisPtr->blockHeight_;
for(int32_t i=blkIdx-1; i>=0; i--)
{
seedDiffSum += difficultyStack[i];
blkHeight++;
thisPtr = headerPtrStack[i];
thisPtr->difficultyDbl_ = difficultyStack[i];
thisPtr->difficultySum_ = seedDiffSum;
thisPtr->blockHeight_ = blkHeight;
thisPtr->isOrphan_ = false;
}
// Finally, we have all the difficulty sums calculated, return this one
return bhpStart.difficultySum_;
}
/////////////////////////////////////////////////////////////////////////////
void Blockchain::putBareHeaders(LMDBBlockDatabase *db, bool updateDupID)
{
/***
Duplicated block heights (forks and orphans) have to saved to the headers
DB.
The current code detects the next unkown block by comparing the block
hashes in the last parsed block file to the list saved in the DB. If
the DB doesn't keep a record of duplicated or orphaned blocks, it will
consider the next dup to be the first unknown block in DB until a new
block file is created by Core.
***/
for (auto& block : headerMap_)
{
StoredHeader sbh;
sbh.createFromBlockHeader(block.second);
uint8_t dup = db->putBareHeader(sbh, updateDupID);
block.second.setDuplicateID(dup); // make sure headerMap_ and DB agree
}
}
/////////////////////////////////////////////////////////////////////////////
void Blockchain::putNewBareHeaders(LMDBBlockDatabase *db)
{
for (auto& block : newlyParsedBlocks_)
{
StoredHeader sbh;
sbh.createFromBlockHeader(*block);
uint8_t dup = db->putBareHeader(sbh, true);
block->setDuplicateID(dup); // make sure headerMap_ and DB agree
}
//once commited to the DB, they aren't considered new anymore,
//so clean up the container
newlyParsedBlocks_.clear();
}