forked from znort987/blockparser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.h
493 lines (413 loc) · 12.3 KB
/
util.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
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
#ifndef __UTIL_H__
#define __UTIL_H__
#include <string>
#include <vector>
#include <common.h>
#include <errlog.h>
#include <rmd160.h>
#include <sha256.h>
#if defined(_WIN64)
#include <io.h>
#define lseek64 _lseeki64
typedef int64_t int128_t;
typedef uint64_t uint128_t;
#else
#include <unistd.h>
typedef signed int int128_t __attribute__((mode(TI)));
typedef unsigned int uint128_t __attribute__((mode(TI)));
#endif
typedef const uint8_t *Hash160;
typedef const uint8_t *Hash256;
struct uint160_t { uint8_t v[kRIPEMD160ByteSize]; };
struct uint256_t { uint8_t v[ kSHA256ByteSize]; };
struct Hash160Hasher { uint64_t operator()( const Hash160 &hash160) const { uintptr_t i = reinterpret_cast<uintptr_t>(hash160); const uint64_t *p = reinterpret_cast<const uint64_t*>(i); return p[0]; } };
struct Hash256Hasher { uint64_t operator()( const Hash256 &hash256) const { uintptr_t i = reinterpret_cast<uintptr_t>(hash256); const uint64_t *p = reinterpret_cast<const uint64_t*>(i); return p[0]; } };
struct Hash160Equal {
bool operator()(
const Hash160 &ha,
const Hash160 &hb
) const {
uintptr_t ia = reinterpret_cast<uintptr_t>(ha);
uintptr_t ib = reinterpret_cast<uintptr_t>(hb);
const uint64_t *a0 = reinterpret_cast<const uint64_t *>(ia);
const uint64_t *b0 = reinterpret_cast<const uint64_t *>(ib);
if(unlikely(a0[0]!=b0[0])) return false;
if(unlikely(a0[1]!=b0[1])) return false;
const uint32_t *a1 = reinterpret_cast<const uint32_t *>(ia);
const uint32_t *b1 = reinterpret_cast<const uint32_t *>(ib);
if(unlikely(a1[4]!=b1[4])) return false;
return true;
}
};
struct Hash256Equal {
bool operator()(
const Hash256 &ha,
const Hash256 &hb
) const {
uintptr_t ia = reinterpret_cast<uintptr_t>(ha);
uintptr_t ib = reinterpret_cast<uintptr_t>(hb);
const uint64_t *a = reinterpret_cast<const uint64_t *>(ia);
const uint64_t *b = reinterpret_cast<const uint64_t *>(ib);
if(unlikely(a[0]!=b[0])) return false;
if(unlikely(a[1]!=b[1])) return false;
if(unlikely(a[2]!=b[2])) return false;
if(unlikely(a[3]!=b[3])) return false;
return true;
}
};
template<
typename T,
size_t kPageSize = 16384
>
struct PagedAllocator {
static uint8_t *pool;
static uint8_t *poolEnd;
enum { kPageByteSize = sizeof(T)*kPageSize };
static uint8_t *alloc() {
if(unlikely(poolEnd<=pool)) {
pool = (uint8_t*)malloc(kPageByteSize);
poolEnd = kPageByteSize + pool;
}
uint8_t *result = pool;
pool += sizeof(T);
return result;
}
};
static inline uint8_t *allocHash256() { return PagedAllocator<uint256_t>::alloc(); }
static inline uint8_t *allocHash160() { return PagedAllocator<uint160_t>::alloc(); }
struct BlockFile {
int fd;
uint64_t size;
std::string name;
};
struct Chunk {
private:
size_t size;
size_t offset;
mutable uint8_t *data;
const BlockFile *blockFile;
public:
void init(
const BlockFile *_blockFile,
size_t _size,
size_t _offset
) {
data = 0;
size = _size;
offset = _offset;
blockFile = _blockFile;
}
const uint8_t *getData() const {
if(likely(0==data)) {
auto where = lseek64(blockFile->fd, offset, SEEK_SET);
if(where!=(signed)offset) {
sysErrFatal(
"failed to seek into block chain file %s",
blockFile->name.c_str()
);
}
data = (uint8_t*)malloc(size);
auto sz = read(blockFile->fd, data, size);
if(sz!=(signed)size) {
//fatal("can't read block");
}
}
return data;
}
void releaseData() const {
free(data);
data = 0;
}
size_t getSize() const { return size; }
size_t getOffset() const { return offset; }
const BlockFile *getBlockFile() const { return blockFile; }
static Chunk *alloc() {
return (Chunk*)PagedAllocator<Chunk>::alloc();
}
};
struct Block {
Chunk *chunk;
const uint8_t *hash;
int64_t height;
Block *prev;
Block *next;
void init(
const uint8_t *_hash,
const BlockFile *_blockFile,
size_t _size,
Block *_prev,
uint64_t _offset
) {
chunk = Chunk::alloc();
chunk->init(_blockFile, _size, _offset);
hash = _hash;
height = -1;
prev = _prev;
next = 0;
}
static Block *alloc() {
return (Block*)PagedAllocator<Block>::alloc();
}
};
#if defined NO_GOOGLE_MAP
#include <unordered_map>
template<
typename Key,
typename Value,
typename Hasher,
typename Equal
>
struct GoogMap {
typedef std::unordered_map<
Key,
Value,
Hasher,
Equal
> MapBase;
struct Map:public MapBase {
void setEmptyKey(
const Key &
) {
}
void resize(
const size_t &size
) {
MapBase::reserve(size);
}
};
};
#else
#if defined(WANT_DENSE)
// Faster, uses more RAM
#include <google/dense_hash_map>
template<
typename Key,
typename Value,
typename Hasher,
typename Equal
>
struct GoogMap {
typedef google::dense_hash_map<
Key,
Value,
Hasher,
Equal
> MapBase;
struct Map:public MapBase {
void setEmptyKey(
const Key &empty
) {
this->set_empty_key(empty);
}
};
};
#else
// Slower, uses less RAM
#include <google/sparse_hash_map>
template<
typename Key,
typename Value,
typename Hasher,
typename Equal
>
struct GoogMap {
typedef google::sparse_hash_map<
Key,
Value,
Hasher,
Equal
> MapBase;
struct Map:public MapBase {
void setEmptyKey(
const Key &empty
) {
}
};
};
#endif
#endif
#if defined(DEBUG)
#define DEBUG_SKIP(type, var, p) type dummy_##var = *(type*)p;
#else
#define DEBUG_SKIP(type, var, p)
#endif
#define SKIP(type, var, p) \
DEBUG_SKIP(type, var, p) \
p += sizeof(type) \
#define LOAD(type, var, p) \
type var = *(type*)p; \
p += sizeof(type) \
#define LOAD_VARINT(var, p) \
uint64_t var = loadVarInt(p) \
static inline uint64_t loadVarInt(
const uint8_t *&p
) {
uint64_t r = *(p++);
if(likely(r<0xFD)) { return r; }
if(likely(0xFD==r)) { LOAD(uint16_t, v, p); return v; }
if(likely(0xFE==r)) { LOAD(uint32_t, v, p); return v; }
LOAD(uint64_t, v, p); return v;
}
void toHex(
uint8_t *dst,
const uint8_t *src,
size_t size = kSHA256ByteSize,
bool rev = true
);
void showHex(
const uint8_t *src,
size_t size = kSHA256ByteSize,
bool rev = true
);
uint8_t fromHexDigit(
uint8_t h,
bool abortOnErr = true
);
bool fromHex(
uint8_t *dst,
const uint8_t *src,
size_t dstSize = kSHA256ByteSize,
bool rev = true,
bool abortOnErr = true
);
void showScript(
const uint8_t *p,
size_t scriptSize,
const char *header = 0,
const char *indent = 0,
bool showAscii = false
);
bool compressPublicKey(
uint8_t *result,
const uint8_t *decompressedKey
);
bool decompressPublicKey(
uint8_t *result,
const uint8_t *compressedKey
);
int solveOutputScript(
uint8_t *pubKeyHash,
const uint8_t *script,
uint64_t scriptSize,
uint8_t *addrType
);
#if defined(DARKCOIN)
void h9(
uint8_t *h9r,
const uint8_t *buf,
uint64_t size
);
#endif
#if defined(PAYCON)
void h13(
uint8_t *h9r,
const uint8_t *buf,
uint64_t size
);
#endif
#if defined(CLAM) || defined(JUMBUCKS)
void scrypt(
uint8_t *h9r,
const uint8_t *buf,
uint64_t size
);
#endif
static inline void sha256Twice(
uint8_t *sha,
const uint8_t *buf,
uint64_t size
) {
sha256(sha, buf, size);
sha256(sha, sha, kSHA256ByteSize);
}
extern const uint8_t hexDigits[];
extern const uint8_t b58Digits[];
uint8_t fromB58Digit(
uint8_t digit,
bool abortOnErr = true
);
void hash160ToAddr(
uint8_t *addr,
const uint8_t *hash160,
bool pad = false,
uint8_t type = 0
);
static inline double satoshisToNormaForm(
uint64_t satoshis
) {
return satoshis*
#if defined(PEERCOIN)
1e-6
#else
1e-8
#endif
;
}
bool addrToHash160(
uint8_t *hash160,
const uint8_t *addr,
bool checkHash = false,
bool verbose = true
);
bool guessHash160(
uint8_t *hash160,
const uint8_t *addr
);
const uint8_t *loadKeyHash(
const uint8_t *hexHash = 0,
bool verbose = false
);
void loadKeyList(
std::vector<uint160_t> &result,
const char *str,
bool verbose = false
);
void loadHash256List(
std::vector<uint256_t> &result,
const char *str,
bool verbose = false
);
std::string pr128(
const uint128_t &y
);
void showFullAddr(
const Hash160 &addr,
bool both = false
);
uint64_t getBaseReward(
uint64_t h
);
void canonicalHexDump(
const uint8_t *p,
size_t size,
const char *indent
);
void showScriptInfo(
const uint8_t *outputScript,
uint64_t outputScriptSize,
const uint8_t *indent
);
void writeEscapedBinaryBufferRev(
FILE *f,
const uint8_t *p,
size_t n
);
void writeEscapedBinaryBuffer(
FILE *f,
const uint8_t *p,
size_t n
);
static inline void printEscapedBinaryBufferRev(
const uint8_t *p,
size_t n
) {
writeEscapedBinaryBufferRev(stdout, p, n);
}
static inline void printEscapedBinaryBuffer(
const uint8_t *p,
size_t n
) {
writeEscapedBinaryBuffer(stdout, p, n);
}
const char *getInterestingAddr();
#endif // __UTIL_H__