forked from znort987/blockparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
631 lines (530 loc) · 14.9 KB
/
parser.cpp
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
#include <util.h>
#include <common.h>
#include <errlog.h>
#include <callback.h>
#include <string>
#include <vector>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#if !defined(O_DIRECT)
# define O_DIRECT 0
#endif
struct Map
{
int fd;
uint64_t size;
const uint8_t *p;
std::string name;
};
typedef GoogMap<Hash256, const uint8_t*, Hash256Hasher, Hash256Equal>::Map TXMap;
typedef GoogMap<Hash256, Block*, Hash256Hasher, Hash256Equal>::Map BlockMap;
static bool gNeedTXHash;
static Callback *gCallback;
static const Map *gCurMap;
static std::vector<Map> mapVec;
static TXMap gTXMap;
static BlockMap gBlockMap;
static uint8_t empty[kSHA256ByteSize] = { 0x42 };
static Block *gMaxBlock;
static Block *gNullBlock;
static uint64_t gChainSize;
static uint64_t gMaxHeight;
static uint256_t gNullHash;
#define DO(x) x
static inline void startBlock(const uint8_t *p) { DO(gCallback->startBlock(p)); }
static inline void endBlock(const uint8_t *p) { DO(gCallback->endBlock(p)); }
static inline void startTX(const uint8_t *p, const uint8_t *hash) { DO(gCallback->startTX(p, hash)); }
static inline void endTX(const uint8_t *p) { DO(gCallback->endTX(p)); }
static inline void startInputs(const uint8_t *p) { DO(gCallback->startInputs(p)); }
static inline void endInputs(const uint8_t *p) { DO(gCallback->endInputs(p)); }
static inline void startInput(const uint8_t *p) { DO(gCallback->startInput(p)); }
static inline void endInput(const uint8_t *p) { DO(gCallback->endInput(p)); }
static inline void startOutputs(const uint8_t *p) { DO(gCallback->startOutputs(p)); }
static inline void endOutputs(const uint8_t *p) { DO(gCallback->endOutputs(p)); }
static inline void startOutput(const uint8_t *p) { DO(gCallback->startOutput(p)); }
static inline void start(const Block *s, const Block *e) { DO(gCallback->start(s, e)); }
#undef DO
static inline void startMap(const uint8_t *p) { gCallback->startMap(p); }
static inline void endMap(const uint8_t *p) { gCallback->endMap(p); }
static inline void startBlock(const Block *b) { gCallback->startBlock(b, gChainSize); }
static inline void endBlock(const Block *b) { gCallback->endBlock(b); }
static inline void endOutput(
const uint8_t *p,
uint64_t value,
const uint8_t *txHash,
uint64_t outputIndex,
const uint8_t *outputScript,
uint64_t outputScriptSize
)
{
gCallback->endOutput(
p,
value,
txHash,
outputIndex,
outputScript,
outputScriptSize
);
}
static inline void edge(
uint64_t value,
const uint8_t *upTXHash,
uint64_t outputIndex,
const uint8_t *outputScript,
uint64_t outputScriptSize,
const uint8_t *downTXHash,
uint64_t inputIndex,
const uint8_t *inputScript,
uint64_t inputScriptSize
)
{
gCallback->edge(
value,
upTXHash,
outputIndex,
outputScript,
outputScriptSize,
downTXHash,
inputIndex,
inputScript,
inputScriptSize
);
}
template<
bool skip,
bool fullContext
>
static void parseOutput(
const uint8_t *&p,
const uint8_t *txHash,
uint64_t outputIndex,
const uint8_t *downTXHash,
uint64_t downInputIndex,
const uint8_t *downInputScript,
uint64_t downInputScriptSize,
bool found = false
)
{
if(!skip && !fullContext) startOutput(p);
LOAD(uint64_t, value, p);
LOAD_VARINT(outputScriptSize, p);
const uint8_t *outputScript = p;
p += outputScriptSize;
if(!skip && fullContext && found) {
edge(
value,
txHash,
outputIndex,
outputScript,
outputScriptSize,
downTXHash,
downInputIndex,
downInputScript,
downInputScriptSize
);
}
if(!skip && !fullContext) {
endOutput(
p,
value,
txHash,
outputIndex,
outputScript,
outputScriptSize
);
}
}
template<
bool skip,
bool fullContext
>
static void parseOutputs(
const uint8_t *&p,
const uint8_t *txHash,
uint64_t stopAtIndex = -1,
const uint8_t *downTXHash = 0,
uint64_t downInputIndex = 0,
const uint8_t *downInputScript = 0,
uint64_t downInputScriptSize = 0
)
{
if(!skip && !fullContext) startOutputs(p);
LOAD_VARINT(nbOutputs, p);
for(uint64_t outputIndex=0; outputIndex<nbOutputs; ++outputIndex) {
bool found = fullContext && !skip && (stopAtIndex==outputIndex);
parseOutput<skip, fullContext>(
p,
txHash,
outputIndex,
downTXHash,
downInputIndex,
downInputScript,
downInputScriptSize,
found
);
if(found) break;
}
if(!skip && !fullContext) endOutputs(p);
}
template<
bool skip
>
static void parseInput(
const uint8_t *&p,
const uint8_t *txHash,
uint64_t inputIndex
)
{
if(!skip) startInput(p);
const uint8_t *upTXHash = p;
const uint8_t *upTXOutputs = 0;
if(gNeedTXHash && !skip) {
bool isGenTX = (0==memcmp(gNullHash.v, upTXHash, sizeof(gNullHash)));
if(likely(false==isGenTX)) {
auto i = gTXMap.find(upTXHash);
if(unlikely(gTXMap.end()==i))
errFatal("failed to locate upstream TX");
upTXOutputs = i->second;
}
}
SKIP(uint256_t, dummyUpTXhash, p);
LOAD(uint32_t, upOutputIndex, p);
LOAD_VARINT(inputScriptSize, p);
if(!skip && 0!=upTXOutputs) {
const uint8_t *inputScript = p;
parseOutputs<false, true>(
upTXOutputs,
upTXHash,
upOutputIndex,
txHash,
inputIndex,
inputScript,
inputScriptSize
);
}
p += inputScriptSize;
SKIP(uint32_t, sequence, p);
if(!skip) endInput(p);
}
template<
bool skip
>
static void parseInputs(
const uint8_t *&p,
const uint8_t *txHash
)
{
if(!skip) startInputs(p);
LOAD_VARINT(nbInputs, p);
for(uint64_t inputIndex=0; inputIndex<nbInputs; ++inputIndex)
parseInput<skip>(p, txHash, inputIndex);
if(!skip) endInputs(p);
}
template<
bool skip
>
static void parseTX(
const uint8_t *&p
)
{
uint8_t *txHash = 0;
const uint8_t *txStart = p;
if(gNeedTXHash && !skip) {
const uint8_t *txEnd = p;
parseTX<true>(txEnd);
txHash = allocHash256();
sha256Twice(txHash, txStart, txEnd - txStart);
}
if(!skip) startTX(p, txHash);
SKIP(uint32_t, version, p);
parseInputs<skip>(p, txHash);
if(gNeedTXHash && !skip)
gTXMap[txHash] = p;
parseOutputs<skip, false>(p, txHash);
SKIP(uint32_t, lockTime, p);
if(!skip) endTX(p);
}
static void parseBlock(
const Block *block
)
{
startBlock(block);
const uint8_t *p = block->data;
const uint8_t *header = p;
SKIP(uint32_t, version, p);
SKIP(uint256_t, prevBlkHash, p);
SKIP(uint256_t, blkMerkleRoot, p);
SKIP(uint32_t, blkTime, p);
SKIP(uint32_t, blkBits, p);
SKIP(uint32_t, blkNonce, p);
LOAD_VARINT(nbTX, p);
for(uint64_t txIndex=0; likely(txIndex<nbTX); ++txIndex)
parseTX<false>(p);
endBlock(block);
}
static void parseLongestChain()
{
Block *blk = gNullBlock->next;
start(blk, gMaxBlock);
while(likely(0!=blk)) {
parseBlock(blk);
blk = blk->next;
}
}
static void findLongestChain()
{
Block *block = gMaxBlock;
while(1) {
if(likely(0!=block->data)) {
const uint8_t *p = -4 + (block->data);
LOAD(uint32_t, size, p);
gChainSize += size;
}
Block *prev = block->prev;
if(unlikely(0==prev)) break;
prev->next = block;
block = prev;
}
}
static void initCallback(
int argc,
char *argv[]
)
{
const char *methodName = 0;
if(0<argc) methodName = argv[1];
if(0==methodName) methodName = "";
if(0==methodName[0]) methodName = "help";
gCallback = Callback::find(methodName);
fprintf(stderr, "\n");
info("starting command \"%s\"", gCallback->name());
if(argv[1]) {
int i = 0;
while('-'==argv[1][i]) argv[1][i++] = 'x';
}
int ir = gCallback->init(argc, (const char **)argv);
if(ir<0) errFatal("callback init failed");
gNeedTXHash = gCallback->needTXHash();
}
static void mapBlockChainFiles()
{
std::string coinName(
#if defined LITECOIN
"/.litecoin/"
#else
"/.bitcoin/"
#endif
);
const char *home = getenv("HOME");
if(0==home) {
warning("could not getenv(\"HOME\"), using \".\" instead.");
home = ".";
}
std::string homeDir(home);
std::string blockDir = homeDir + coinName + std::string("blocks");
struct stat statBuf;
int r = stat(blockDir.c_str(), &statBuf);
bool oldStyle = (r<0 || !S_ISDIR(statBuf.st_mode));
int blkDatId = oldStyle ? 1 : 0;
const char *fmt = oldStyle ? "blk%04d.dat" : "blocks/blk%05d.dat";
while(1) {
char buf[64];
sprintf(buf, fmt, blkDatId++);
std::string blockMapFileName =
homeDir +
coinName +
std::string(buf)
;
int blockMapFD = open(blockMapFileName.c_str(), O_DIRECT | O_RDONLY);
if(blockMapFD<0) {
if(1<blkDatId) break;
sysErrFatal(
"failed to open block chain file %s",
blockMapFileName.c_str()
);
}
struct stat statBuf;
int r = fstat(blockMapFD, &statBuf);
if(r<0) sysErrFatal( "failed to fstat block chain file %s", blockMapFileName.c_str());
size_t mapSize = statBuf.st_size;
void *pMap = mmap(0, mapSize, PROT_READ, MAP_PRIVATE, blockMapFD, 0);
if(((void*)-1)==pMap) {
sysErrFatal(
"failed to mmap block chain file %s",
blockMapFileName.c_str()
);
}
Map map;
map.size = mapSize;
map.fd = blockMapFD;
map.name = blockMapFileName;
map.p = (const uint8_t*)pMap;
mapVec.push_back(map);
}
}
static void initHashtables()
{
gTXMap.setEmptyKey(empty);
gBlockMap.setEmptyKey(empty);
auto e = mapVec.end();
uint64_t totalSize = 0;
auto i = mapVec.begin();
while(i!=e) totalSize += (i++)->size;
double txPerBytes = (3976774.0 / 1713189944.0);
size_t nbTxEstimate = (1.5 * txPerBytes * totalSize);
gTXMap.resize(nbTxEstimate);
double blocksPerBytes = (184284.0 / 1713189944.0);
size_t nbBlockEstimate = (1.5 * blocksPerBytes * totalSize);
gBlockMap.resize(nbBlockEstimate);
}
static void linkBlock(
Block *block
)
{
if(unlikely(0==block->data)) {
block->height = 0;
block->prev = 0;
block->next = 0;
return;
}
int depth = 0;
Block *b = block;
while(b->height<0) {
auto i = gBlockMap.find(4 + b->data);
if(unlikely(gBlockMap.end()==i)) {
uint8_t buf[2*kSHA256ByteSize + 1];
toHex(buf, 4 + b->data);
warning("at depth %d in chain, failed to locate parent block %s", depth, buf);
return;
}
Block *prev = i->second;
prev->next = b;
b->prev = prev;
b = prev;
++depth;
}
uint64_t h = b->height;
while(block!=b) {
Block *next = b->next;
b->height = h;
b->next = 0;
if(likely(gMaxHeight<h)) {
gMaxHeight = h;
gMaxBlock = b;
}
b = next;
++h;
}
}
static void linkAllBlocks()
{
auto e = gBlockMap.end();
auto i = gBlockMap.begin();
while(i!=e) {
Block *block = (i++)->second;
linkBlock(block);
}
}
static bool buildBlock(
const uint8_t *&p,
const uint8_t *e
)
{
static const uint32_t expected =
#if defined(LITECOIN)
0xdbb6c0fb
#else
0xd9b4bef9
#endif
;
if(unlikely(e<=(8+p))) {
//printf("end of map, reason : pointer past EOF\n");
return true;
}
LOAD(uint32_t, magic, p);
if(unlikely(expected!=magic)) {
//printf("end of map, reason : magic is fucked %d away from EOF\n", (int)(e-p));
return true;
}
LOAD(uint32_t, size, p);
if(unlikely(e<(p+size))) {
//printf("end of map, reason : end of block past EOF, %d past EOF\n", (int)((p+size)-e));
return true;
}
Block *block = allocBlock();
block->height = -1;
block->data = p;
block->prev = 0;
block->next = 0;
uint8_t *hash = allocHash256();
sha256Twice(hash, p, 80);
gBlockMap[hash] = block;
p += size;
return false;
}
static void buildAllBlocks()
{
auto e = mapVec.end();
auto i = mapVec.begin();
while(i!=e) {
const Map *map = gCurMap = &(*(i++));
const uint8_t *end = map->size + map->p;
const uint8_t *p = map->p;
startMap(p);
while(1) {
if(unlikely(end<=p)) break;
bool done = buildBlock(p, end);
if(done) break;
}
endMap(p);
}
}
static void buildNullBlock()
{
gBlockMap[gNullHash.v] = gNullBlock = allocBlock();
gNullBlock->data = 0;
}
static void firstPass()
{
buildNullBlock();
buildAllBlocks();
linkAllBlocks();
}
static void secondPass()
{
findLongestChain();
parseLongestChain();
gCallback->wrapup();
}
static void cleanMaps()
{
auto e = mapVec.end();
auto i = mapVec.begin();
while(i!=e) {
const Map &map = *(i++);
int r = munmap((void*)map.p, map.size);
if(r<0) sysErr("failed to unmap block chain file %s", map.name.c_str());
r = close(map.fd);
if(r<0) sysErr("failed to unmap block chain file %s", map.name.c_str());
}
}
int main(
int argc,
char *argv[]
)
{
double start = usecs();
initCallback(argc, argv);
mapBlockChainFiles();
initHashtables();
firstPass();
secondPass();
cleanMaps();
double elapsed = (usecs()-start)*1e-6;
info("all done in %.3f seconds\n", elapsed);
return 0;
}