forked from goatpig/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockObj.cpp
More file actions
396 lines (337 loc) · 12.8 KB
/
BlockObj.cpp
File metadata and controls
396 lines (337 loc) · 12.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
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2011-2015, Armory Technologies, Inc. //
// Distributed under the GNU Affero General Public License (AGPL v3) //
// See LICENSE-ATI or http://www.gnu.org/licenses/agpl.html //
// //
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <thread>
#include "cryptopp/integer.h"
#include "BinaryData.h"
#include "BtcUtils.h"
#include "BlockObj.h"
#include "lmdb_wrapper.h"
////////////////////////////////////////////////////////////////////////////////
void BlockHeader::unserialize(uint8_t const * ptr, uint32_t size)
{
if (size < HEADER_SIZE)
throw BlockDeserializingException();
dataCopy_.copyFrom(ptr, HEADER_SIZE);
BtcUtils::getHash256(dataCopy_.getPtr(), HEADER_SIZE, thisHash_);
difficultyDbl_ = BtcUtils::convertDiffBitsToDouble(
BinaryDataRef(dataCopy_.getPtr()+72, 4));
isInitialized_ = true;
nextHash_ = BinaryData(0);
blockHeight_ = UINT32_MAX;
difficultySum_ = -1;
isMainBranch_ = false;
isOrphan_ = true;
numTx_ = UINT32_MAX;
}
////////////////////////////////////////////////////////////////////////////////
void BlockHeader::unserialize(BinaryDataRef const & str)
{
unserialize(str.getPtr(), str.getSize());
}
////////////////////////////////////////////////////////////////////////////////
void BlockHeader::unserialize(BinaryRefReader & brr)
{
unserialize(brr.get_BinaryDataRef(HEADER_SIZE));
}
/////////////////////////////////////////////////////////////////////////////
void BlockHeader::pprint(ostream & os, int nIndent, bool pBigendian) const
{
string indent = "";
for(int i=0; i<nIndent; i++)
indent = indent + " ";
string endstr = (pBigendian ? " (BE)" : " (LE)");
os << indent << "Block Information: " << blockHeight_ << endl;
os << indent << " Hash: "
<< getThisHash().toHexStr(pBigendian).c_str() << endstr << endl;
os << indent << " Timestamp: " << getTimestamp() << endl;
os << indent << " Prev Hash: "
<< getPrevHash().toHexStr(pBigendian).c_str() << endstr << endl;
os << indent << " MerkleRoot: "
<< getMerkleRoot().toHexStr(pBigendian).c_str() << endstr << endl;
os << indent << " Difficulty: " << (difficultyDbl_)
<< " (" << getDiffBits().toHexStr().c_str() << ")" << endl;
os << indent << " CumulDiff: " << (difficultySum_) << endl;
os << indent << " Nonce: " << getNonce() << endl;
}
////////////////////////////////////////////////////////////////////////////////
void BlockHeader::pprintAlot(ostream & os)
{
cout << "Header: " << getBlockHeight() << endl;
cout << "Hash: " << getThisHash().toHexStr(true) << endl;
cout << "Hash: " << getThisHash().toHexStr(false) << endl;
cout << "PrvHash: " << getPrevHash().toHexStr(true) << endl;
cout << "PrvHash: " << getPrevHash().toHexStr(false) << endl;
cout << "this*: " << this << endl;
cout << "TotSize: " << getBlockSize() << endl;
cout << "Tx Count: " << numTx_ << endl;
}
////////////////////////////////////////////////////////////////////////////////
// Due to SWIG complications, passing in a value by reference really isn't
// feasible. Therefore, we'll use int64 and pass back -1 if we don't find a
// nonce.
int64_t BlockHeader::findNonce(const char* inDiffStr)
{
const BinaryData playHeader(serialize());
const CryptoPP::Integer minBDiff("FFFF0000000000000000000000000000000000000000000000000000h");
const CryptoPP::Integer inDiff(inDiffStr);
if(inDiff > minBDiff) {
cout << "Difficulty " << inDiffStr << " is too high for Bitcoin (bdiff)." << endl;
}
else {
volatile bool stopNow=false;
std::mutex lockSolution;
bool hasSolution=false;
const auto computer = [&] (uint32_t startAt, uint32_t stopAt)->int64_t
{
BinaryData hashResult(32);
for(uint32_t nonce=startAt; nonce<stopAt; nonce++)
{
*(uint32_t*)(playHeader.getPtr()+76) = nonce;
BtcUtils::getHash256_NoSafetyCheck(playHeader.getPtr(), HEADER_SIZE,
hashResult);
const CryptoPP::Integer hashRes((hashResult.swapEndian()).getPtr(),
hashResult.getSize());
if(hashRes < inDiff)
{
unique_lock<mutex> l(lockSolution);
cout << "NONCE FOUND! " << nonce << endl;
unserialize(playHeader);
cout << "Raw Header: " << serialize().toHexStr() << endl;
pprint();
cout << "Hash: " << hashResult.toHexStr() << endl;
hasSolution=true;
stopNow=true;
return nonce;
}
if (stopNow)
{
break;
}
if(startAt==0 && nonce % 10000000 == 0)
{
cout << ".";
cout.flush();
}
}
//needs a return val for windows to build
return -1;
};
const unsigned numThreads = thread::hardware_concurrency();
vector<thread> threads;
threads.reserve(numThreads);
for (unsigned i=0; i < numThreads; i++)
{
threads.emplace_back(
computer,
(uint32_t)(-1)/numThreads*i,
(uint32_t)(-1)/numThreads*(i+1)
);
}
for (unsigned i=0; i < numThreads; i++)
threads[i].join();
if (!hasSolution) {
cout << "No nonce found!" << endl;
}
// We have to change the coinbase script, recompute merkle root, and then
// can cycle through all the nonces again.
}
// If we've landed here for one reason or another, we've failed. Return 0.
return -1;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// DBOutPoint Methods
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
BinaryDataRef DBOutPoint::getDBkey() const
{
if (DBkey_.getSize() == 8)
return DBkey_;
if (db_ != nullptr)
{
DBkey_ = move(db_->getDBKeyForHash(txHash_));
if (DBkey_.getSize() == 6)
{
DBkey_.append(WRITE_UINT16_BE((uint16_t)txOutIndex_));
return DBkey_;
}
}
return BinaryDataRef();
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// DBTxRef Methods
//
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
BinaryData DBTxRef::serialize(void) const
{
return db_->getFullTxCopy(dbKey6B_).serialize();
}
/////////////////////////////////////////////////////////////////////////////
Tx DBTxRef::getTxCopy(void) const
{
return db_->getFullTxCopy(dbKey6B_);
}
/////////////////////////////////////////////////////////////////////////////
bool DBTxRef::isMainBranch(void) const
{
if(dbKey6B_.getSize() != 6)
return false;
else
{
uint8_t dup8 = db_->getValidDupIDForHeight(getBlockHeight());
return (getDuplicateID() == dup8);
}
}
/////////////////////////////////////////////////////////////////////////////
BinaryData DBTxRef::getThisHash(void) const
{
return db_->getTxHashForLdbKey(dbKey6B_);
}
/////////////////////////////////////////////////////////////////////////////
uint32_t DBTxRef::getBlockTimestamp() const
{
StoredHeader sbh;
if(dbKey6B_.getSize() == 6)
{
db_->getStoredHeader(sbh, getBlockHeight(), getDuplicateID(), false);
return READ_UINT32_LE(sbh.dataCopy_.getPtr()+68);
}
else
return UINT32_MAX;
}
/////////////////////////////////////////////////////////////////////////////
BinaryData DBTxRef::getBlockHash(void) const
{
StoredHeader sbh;
if(dbKey6B_.getSize() == 6)
{
db_->getStoredHeader(sbh, getBlockHeight(), getDuplicateID(), false);
return sbh.thisHash_;
}
else
return BtcUtils::EmptyHash();
}
////////////////////////////////////////////////////////////////////////////////
TxIn DBTxRef::getTxInCopy(uint32_t i)
{
return db_->getTxInCopy( dbKey6B_, i);
}
////////////////////////////////////////////////////////////////////////////////
TxOut DBTxRef::getTxOutCopy(uint32_t i)
{
return db_->getTxOutCopy(dbKey6B_, i);
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// UnspentTxOut Methods
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
UnspentTxOut::UnspentTxOut(void) :
txHash_(BtcUtils::EmptyHash()),
txOutIndex_(0),
txHeight_(0),
value_(0),
script_(BinaryData(0)),
isMultisigRef_(false)
{
// Nothing to do here
}
////////////////////////////////////////////////////////////////////////////////
void UnspentTxOut::init(LMDBBlockDatabase *db, TxOut & txout, uint32_t blkNum,
bool isMulti)
{
TxRef txRef = txout.getParentTxRef();
DBTxRef parentTxRef(txRef, db);
txHash_ = parentTxRef.getThisHash();
txOutIndex_ = txout.getIndex();
txIndex_ = txRef.getBlockTxIndex();
txHeight_ = txout.getParentHeight();
value_ = txout.getValue();
script_ = txout.getScript();
isMultisigRef_ = isMulti;
}
////////////////////////////////////////////////////////////////////////////////
BinaryData UnspentTxOut::getRecipientScrAddr(void) const
{
return BtcUtils::getTxOutScrAddr(getScript());
}
////////////////////////////////////////////////////////////////////////////////
uint32_t UnspentTxOut::getNumConfirm(uint32_t currBlkNum) const
{
if (txHeight_ == UINT32_MAX)
throw runtime_error("uninitiliazed UnspentTxOut");
return currBlkNum - txHeight_ + 1;
}
////////////////////////////////////////////////////////////////////////////////
bool UnspentTxOut::CompareNaive(UnspentTxOut const & uto1,
UnspentTxOut const & uto2)
{
float val1 = (float)uto1.getValue();
float val2 = (float)uto2.getValue();
return (val1*uto1.txHeight_ < val2*uto2.txHeight_);
}
////////////////////////////////////////////////////////////////////////////////
bool UnspentTxOut::CompareTech1(UnspentTxOut const & uto1,
UnspentTxOut const & uto2)
{
float val1 = pow((float)uto1.getValue(), 1.0f/3.0f);
float val2 = pow((float)uto2.getValue(), 1.0f/3.0f);
return (val1*uto1.txHeight_ < val2*uto2.txHeight_);
}
////////////////////////////////////////////////////////////////////////////////
bool UnspentTxOut::CompareTech2(UnspentTxOut const & uto1,
UnspentTxOut const & uto2)
{
float val1 = pow(log10((float)uto1.getValue()) + 5, 5);
float val2 = pow(log10((float)uto2.getValue()) + 5, 5);
return (val1*uto1.txHeight_ < val2*uto2.txHeight_);
}
////////////////////////////////////////////////////////////////////////////////
bool UnspentTxOut::CompareTech3(UnspentTxOut const & uto1,
UnspentTxOut const & uto2)
{
float val1 = pow(log10((float)uto1.getValue()) + 5, 4);
float val2 = pow(log10((float)uto2.getValue()) + 5, 4);
return (val1*uto1.txHeight_ < val2*uto2.txHeight_);
}
////////////////////////////////////////////////////////////////////////////////
void UnspentTxOut::sortTxOutVect(vector<UnspentTxOut> & utovect, int sortType)
{
switch(sortType)
{
case 0: sort(utovect.begin(), utovect.end(), CompareNaive); break;
case 1: sort(utovect.begin(), utovect.end(), CompareTech1); break;
case 2: sort(utovect.begin(), utovect.end(), CompareTech2); break;
case 3: sort(utovect.begin(), utovect.end(), CompareTech3); break;
default: break; // do nothing
}
}
////////////////////////////////////////////////////////////////////////////////
void UnspentTxOut::pprintOneLine(uint32_t currBlk)
{
printf(" Tx:%s:%02d BTC:%0.3f nConf:%04d\n",
txHash_.copySwapEndian().getSliceCopy(0,8).toHexStr().c_str(),
txOutIndex_,
value_/1e8,
getNumConfirm(currBlk));
}
// kate: indent-width 3; replace-tabs on;