forked from binaryguru/blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
412 lines (379 loc) · 9.96 KB
/
main.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
#include "BlockChain.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <math.h>
#include <time.h>
#ifdef WIN32
#include <conio.h>
#endif
#include "HeapSort.h"
#ifdef _MSC_VER
#pragma warning(disable:4996 4702 4505)
#endif
time_t zombieDate(0x510B56CB); // right around January 1, 2013
static bool isESC(void)
{
bool ret = false;
#ifdef _MSC_VER
if ( kbhit() )
{
int c = getch();
if ( c == 27 )
{
return true;
}
}
#endif
return ret;
}
enum StatResolution
{
SR_DAY,
SR_MONTH,
SR_YEAR,
SR_LAST
};
enum CommandMode
{
CM_NONE, //
CM_SCAN, // scanning.
CM_PROCESS,
CM_EXIT
};
class BlockChainCommand
{
public:
BlockChainCommand(const char *dataPath,
uint32_t maxBlock,
bool processTransactions,
StatResolution resolution,
uint32_t searchText,
uint32_t zombieDays)
{
mTransactionValue = true;
mAnalyze = false;
mExportTransactions = false;
mBlockChain = createBlockChain(dataPath); // Create the block-chain parser using this root path
mZombieDays = zombieDays;
mSearchText = searchText;
if ( mBlockChain )
{
mBlockChain->searchForText(mSearchText);
mBlockChain->setZombieDays(mZombieDays);
}
mStatResolution = resolution;
mMaxBlock = maxBlock;
printf("Running the BlockChain parser. Written by John W. Ratcliff on January 4, 2014 : TipJar: 1NY8SuaXfh8h5WHd4QnYwpgL1mNu9hHVBT\r\n");
printf("Registered DataDirectory: %s to scan for the blockchain.\r\n", dataPath );
printf("\r\n");
printf("You may press the ESC key to cleanly exit the processing wherever it is at currently.\r\n");
printf("\r\n");
mProcessTransactions = processTransactions;
mProcessBlock = 0;
mLastBlockScan = 0;
mLastBlockPrint = 0;
mFinishedScanning = false;
mCurrentBlock = NULL;
mLastTime = 0;
mSatoshiTime = 0;
mMinBalance = 1;
mRecordAddresses = false;
mMode = CM_SCAN;
if ( mBlockChain == NULL )
{
printf("Failed to open file: blk00000.dat in directory: %s\r\n", dataPath );
mMode = CM_EXIT;
}
}
~BlockChainCommand(void)
{
if ( mBlockChain )
{
mBlockChain->release();
}
}
bool process(void)
{
switch ( mMode )
{
case CM_PROCESS:
if ( mProcessBlock < mBlockChain->getBlockCount() && !isESC() )
{
mCurrentBlock = mBlockChain->readBlock(mProcessBlock);
if ( mCurrentBlock )
{
if ( mLastTime == 0 )
{
mLastTime = mCurrentBlock->timeStamp;
mSatoshiTime = mCurrentBlock->timeStamp;
}
else
{
uint32_t currentTime = mCurrentBlock->timeStamp;
time_t tnow(currentTime);
struct tm beg;
beg = *localtime(&tnow);
time_t tbefore(mLastTime);
struct tm before;
before = *localtime(&tbefore);
bool getStats = false;
switch ( mStatResolution )
{
case SR_DAY:
if ( beg.tm_yday != before.tm_yday && currentTime > mLastTime )
{
getStats = true;
}
break;
case SR_MONTH:
if ( beg.tm_mon != before.tm_mon && currentTime > mLastTime )
{
getStats = true;
}
break;
case SR_YEAR:
if ( beg.tm_year != before.tm_year && currentTime > mLastTime )
{
getStats = true;
}
break;
default:
break;
}
if ( getStats )
{
if ( mProcessTransactions )
{
const char *months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
printf("Gathering statistics for %s %d, %d to %s %d, %d\r\n",
months[before.tm_mon], before.tm_mday, before.tm_year+1900,
months[beg.tm_mon], beg.tm_mday, beg.tm_year+1900);
mBlockChain->gatherStatistics(mLastTime,(uint32_t)zombieDate,mRecordAddresses);
}
if ( mTransactionValue )
{
mBlockChain->reportTransactionValues(mLastTime);
}
mLastTime = currentTime;
}
}
if ( mProcessTransactions )
{
mBlockChain->processTransactions(mCurrentBlock); // process transactions into individual addresses
}
if ( mTransactionValue )
{
mBlockChain->accumulateTransactionValues(mCurrentBlock);
}
}
mProcessBlock++;
if ( (mProcessBlock%1000) == 0 )
{
printf("Processed block #%d of %d total.\r\n", mProcessBlock, mBlockChain->getBlockCount() );
}
}
else
{
printf("Finished processing all blocks in the blockchain.\r\n");
mBlockChain->reportCounts();
if ( mProcessTransactions )
{
printf("Gathering final statistics.\r\n");
mBlockChain->gatherStatistics(mLastTime,(uint32_t)zombieDate,mRecordAddresses);
printf("Saving statistics to file 'stats.csv\r\n");
mBlockChain->saveStatistics(mRecordAddresses,mMinBalance);
}
mMode = CM_EXIT;
mProcessBlock = 0;
}
break;
case CM_SCAN:
if ( isESC() )
{
mMode = CM_EXIT;
printf("ESC hit, exiting before processing any blocks.\r\n");
}
else
{
bool ok = mBlockChain->readBlockHeaders(mMaxBlock,mLastBlockScan);
if ( !ok )
{
mFinishedScanning = true;
mMode = CM_PROCESS; // done scanning.
mLastBlockScan = mBlockChain->buildBlockChain();
printf("Finished scanning block headers. Built block-chain with %d blocks found..\r\n", mLastBlockScan);
}
}
break;
default:
break;
}
return mMode != CM_EXIT;
}
const BlockChain::Block *getBlock(uint32_t index)
{
mCurrentBlock = mBlockChain->readBlock(index);
return mCurrentBlock;
}
void setMaxBlocks(uint32_t maxBlocks)
{
mMaxBlock = maxBlocks;
}
CommandMode mMode;
bool mExportTransactions;
bool mAnalyze;
bool mRecordAddresses;
bool mFinishedScanning;
bool mProcessTransactions;
StatResolution mStatResolution;
uint32_t mProcessBlock;
uint32_t mMaxBlock;
uint32_t mLastBlockScan;
uint32_t mLastBlockPrint;
const BlockChain::Block *mCurrentBlock;
BlockChain *mBlockChain;
uint32_t mLastTime;
uint32_t mSatoshiTime;
float mMinBalance;
bool mTransactionValue;
uint32_t mZombieDays;
uint32_t mSearchText;
};
int main(int argc,const char **argv)
{
if ( argc < 2 )
{
printf("Usage: blockchain (options) <dataDir>\n");
printf("\n");
printf("Options:\n");
printf("\n");
printf("-max_blocks <n> : Maximum number of blocks in the blockchain to analyze\n");
printf("-statistics : Whether or not to perform full statistics processing of every single address\n");
printf("-by_day : Accumulate breakdown of all bitcoin addresses by day\n");
printf("-by_month : Accumulate breakdown of all bitcoin addresses by month\n");
printf("-by_year : Accumulate breakdown of all bitcoin addresses by year\n");
printf("-zombie_days <n> : Number of days to consider a bitcoin address as a zombie, default value is 3 years.\n");
printf("-transactions_only : Don't accumulate full statistics, just report transaction data.\n");
printf("-find_text <n> : Search for occurrences of ASCII text in the blockchain and output it to the log file. <n> is how many ASCII characters in a row to report text.\n");
}
else
{
uint32_t zombieDays = 365*3;
uint32_t maxBlocks = 10000000;
const char *dataPath = ".";
int i = 1;
StatResolution resolution = SR_YEAR;
bool processTransactions = false;
bool transactionsOnly = false;
uint32_t searchText = 0;
while ( i < argc )
{
const char *option = argv[i];
if ( *option == '-' )
{
if ( strcmp(option,"-max_blocks") == 0 )
{
i++;
if ( i < argc )
{
maxBlocks = atoi(argv[i]);
if ( maxBlocks < 1 )
{
printf("Invalid max_blocks value '%s'\n", argv[i] );
maxBlocks = 1000000;
}
else
{
printf("Maximum blocks set to %d\r\n", maxBlocks);
}
}
else
{
printf("Error parsing option '-max_blocks', missing block number.\n");
}
}
else if ( strcmp(option,"-transactions_only") == 0 )
{
transactionsOnly = true;
printf("Will only process transactions but not full statistics.\r\n");
}
else if ( strcmp(option,"-statistics") == 0 )
{
processTransactions = true;
printf("Enabling statistics processing\r\n");
}
else if ( strcmp(option,"-by_day") == 0 )
{
resolution = SR_DAY;
processTransactions = true;
printf("Gathering statistics by day\n");
}
else if ( strcmp(option,"-by_month") == 0 )
{
resolution = SR_MONTH;
processTransactions = true;
printf("Gathering statistics by month\n");
}
else if ( strcmp(option,"-by_year") == 0 )
{
resolution = SR_YEAR;
processTransactions = true;
printf("Gathering statistics by year\n");
}
else if ( strcmp(option,"-find_text") == 0 )
{
i++;
if ( i < argc )
{
searchText = atoi(argv[i]);
printf("Searching for ASCII text in the blockchain using a character limit of %d\n", searchText );
}
else
{
printf("Error parsing option '-find_text', missing character length.\n");
}
}
else if ( strcmp(option,"-zombie_days") == 0 )
{
i++;
if ( i < argc )
{
zombieDays = atoi(argv[i]);
printf("Zombie Days set to %d\n", zombieDays );
}
else
{
printf("Error parsing option '-zombie_days', missing day count.\n");
}
}
else
{
printf("Unknown option '%s'\n", option );
}
}
else
{
if ( (i+1) == argc )
{
printf("Using directory: %s to locate bitcoin data blocks.\n", option );
dataPath = option;
}
else
{
printf("%s not a valid option.\n", option );
}
}
i++;
}
if ( transactionsOnly )
{
processTransactions = false;
}
BlockChainCommand bc(dataPath,maxBlocks,processTransactions,resolution,searchText,zombieDays);
bc.setMaxBlocks(maxBlocks);
while ( bc.process() );
}
return 0;
}