-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdata-downloader.js
593 lines (457 loc) · 14.7 KB
/
data-downloader.js
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
//Downloads certain blockchain data of the given time period and saves it as the given file (.json)
const https = require("https");
const fs = require('fs');
const Stopwatch = require("node-stopwatch").Stopwatch;
const util = require('util')
const assert = require('assert').strict;
const ArgumentParser = require("argparse").ArgumentParser;
const big = require("bignumber.js");
const Cacher = require("./js/cacher.js");
const cacher = new Cacher("/storage/programming/chain/cache");
const JsonUtil = require("./js/json_util.js");
const jsonUtil = new JsonUtil();
const sw = Stopwatch.create();
var debug = true;
var datadir = 'data/'
const maxAsyncRequests = 8;
const saveSpace = true;
function JSONRequest(request, callback){
https.get(request, (res) => {
console.log('Status code from exchange:', res.statusCode);
var responseString = "";
res.on('data', function (chunk) {
responseString += chunk;
});
res.on('end', function(){
console.log("Received "+responseString.length+" bytes");
callback(JSON.parse(responseString));
});
}).on('error', (e) => {
console.error(e);
callback(undefined);
});
}
function downloadCourseCryptocompare(start, end, callback){
var data = []
const firstTimestamp = 1438959600 //this timestamp is the first data that this source has
return new Promise(function(resolve, reject){
var sendRequest = function(stB){
var request = 'https://min-api.cryptocompare.com/data/histohour?fsym=ETH&tsym=USD&limit=2000&e=CCCAGG&toTs='+stB;
JSONRequest(request, function(part){
var partData = part.Data.reverse().filter(tick => tick.time >= firstTimestamp) //remove any invalid data that is before the first timestamps
data.push.apply(data, partData)
console.log(util.format("Received data chunk that starts at %d and ends at %d.", part.TimeTo, part.TimeFrom))
if(part.TimeFrom > start && part.TimeFrom > firstTimestamp)
sendRequest(part.TimeFrom-1) //request the next batch
else{
for(var i=0; i<data.length-1; i++){ //validate that we have all data in the correct hourly intervals
if (data[i].time - 3600 != data[i+1].time){
console.log(util.format("Mismatch between dates %d and %d with difference %d.", data[i].time, data[i+1].time, data[i+1].time-data[i].time))
}
}
data.reverse()
resolve(data)
}
})
}
sendRequest(end)
});
}
function downloadCoursePoloniex(start, end){
var request = "https://poloniex.com/public?command=returnChartData¤cyPair=USDT_ETH&start="+start+"&end="+end+"&period=300"
return new Promise(function(resolve, reject){
JSONRequest(request, function(res){
resolve(res)
});
});
}
function downloadCourse(start, end, source){
var request = "";
return new Promise(function(resolve, reject){
if(source == "poloniex"){
downloadCoursePoloniex(start, end).then(res => {
resolve(res);
});
} else if(source == "cryptocompare"){
downloadCourseCryptocompare(start, end).then(res => {
resolve(res);
});
} else reject("Invalid source specified: "+source);
});
}
function downloadWholeCourse(){
return downloadCourse(1, 999999999999999, "cryptocompare");
}
function preprocessBlocks(blocks) {
for(let key in blocks) {
cleanBlock(blocks[key]);
}
return blocks;
}
function preprocessContractLogs(logs) {
for(var i=0; i<logs.length; i++){ //clean the logs
cleanLog(logs[i]);
}
return logs;
}
function preprocessTransactionTraces(traces, noErrors=true, noEmpty=true, noSuicideSpam=true){
let result = [];
let emptyRemovals = 0;
let errorRemovals = 0;
let suicideRemovals = 0;
//key -> value of failed transactions
//because in traces, only the first trace is errored
let failedTransactions = {};
//addr -> bool whether a contract has been killed
//to filter any further calls to this contract
//see: https://etherscan.io/tx/0x5c8628001bcfca3f2f4e8e3e345491d3971c61af36b6d37b57117ac97e711ab5 for example
let killedContracts = {};
let rawResult = traces;
for(let i=0; i<rawResult.length; i++) {
let el = rawResult[i];
if(noErrors) {
//block rewards don't have a transaction hash
if(el.transactionHash && (el.error || failedTransactions[el.transactionHash.toLowerCase()])) {
failedTransactions[el.transactionHash.toLowerCase()] = true;
//TODO: What about gas spendings and this depleting the account balance?
errorRemovals++;
continue;
}
}
cleanAndNormalizeTrace(el);
if(noSuicideSpam) {
if(killedContracts[el.to] || killedContracts[el.from]) {
suicideRemovals++;
continue;
}
}
if(el.type == 'suicide') {
killedContracts[el.from] = true;
}
if(noEmpty && el.value == '0x0' && el.gasUsed == '0x0' && el.gas == '0x0') {
//Warning: There are transactions that use 0 gas and transfer 0 wei, but have provided gas.
//not sure if we should discard those or not.
//they don't look like to change blockchain state though
emptyRemovals++;
continue;
}
result.push(el);
}
console.log("Cleaned traces with "+emptyRemovals+" empty, "+errorRemovals+" error and "+suicideRemovals+" suicide removals.");
return result;
}
function cleanLog(log){
log.hash = log.transactionHash; //rename to just hash, so it falls in line with 'hash' in a transaction
delete log.transactionHash;
delete log.transactionIndex;
delete log.blockHash;
delete log.logIndex;
//unpack the topics array into four properties
log.topic0 = null;
log.topic1 = null;
log.topic2 = null;
log.topic3 = null;
for(let i=0; i<log.topics.length; i++){
log['topic' + i] = log.topics[i];
}
delete log.topics;
//parity-related properties
delete log.transactionLogIndex;
if(saveSpace){
log.dataLen = log.data.length; //keep at least some info about the data.
delete log.data; //these are the non-indexed log arguments. They can be arbitrary in length, and so take up a lot of space.
}
return log;
}
function cleanReceipt(receipt){
delete receipt.blockHash;
delete receipt.logsBloom;
delete receipt.root;
for(i=0; i<receipt.logs.length; i++){
log = receipt.logs[i];
cleanLog(log);
}
return receipt;
}
function cleanTransaction(tx){
delete tx.blockHash;
delete tx.nonce;
delete tx.v;
delete tx.r;
delete tx.s;
if(saveSpace){
tx.dataLen = tx.input.length;
delete tx.input;
}
delete tx.transactionIndex;
//parity-related properties
delete tx.standard_v;
delete tx.standardV;
delete tx.raw;
delete tx.publicKey;
delete tx.chainId;
delete tx.condition;
delete tx.creates;
//since this was loaded from a JSON, the BigNumber objects are no longer present
//tx.gasPrice = tx.gasPrice.toFixed()
//tx.value = tx.value.toFixed()
return tx;
}
function cleanAndNormalizeTrace(trace){
let type = trace.type;
//normalize to the same object property set
if(type == 'suicide'){
assertHasProperties(trace.action, ['address', 'balance', 'refundAddress']);
assert(trace.result == null);
renameProp(trace.action, 'address', 'from');
renameProp(trace.action, 'balance', 'value');
renameProp(trace.action, 'refundAddress', 'to');
trace.action.gas = null;
} else if(type == 'reward'){
assertHasProperties(trace.action, ['author', 'rewardType', 'value']);
assert(trace.result == null);
renameProp(trace.action, 'author', 'to');
trace.action.from = null;
trace.action.gas = null;
trace.subtype = trace.action.rewardType;
delete trace.action.rewardType;
} else if(type == 'create'){
assertHasProperties(trace.action, ['from', 'gas', 'init', 'value']);
assertHasProperties(trace.result, ['gasUsed', 'address', 'code']);
delete trace.action.init;
trace.action.to = trace.result.address;
delete trace.result.address;
delete trace.result.code;
} else if(type == 'call'){
assertHasProperties(trace.action, ['callType', 'from', 'gas', 'input', 'to', 'value']);
try{
assertHasProperties(trace.result, ['gasUsed', 'output']);
} catch(e){
console.log(trace);
throw new DOMException();
}
trace.subtype = trace.action.callType;
delete trace.action.callType;
delete trace.action.input;
delete trace.result.output;
} else {
console.error("Received unknown trace type: ", type);
assert(false);
}
//merge the action properties into the trace object
let actionProps = ['from', 'gas', 'to', 'value'];
assertHasProperties(trace.action, actionProps);
for(let i=0; i<actionProps.length; i++){
let prop = actionProps[i];
trace[prop] = trace.action[prop];
}
delete trace.action;
//merge the result properties into the trace object
let resultProps = ['gasUsed'];
if(!trace.result){ //create an empty object with null-valued properties
trace.result = {};
for(let i=0; i<resultProps.length; i++){
let prop = resultProps[i];
trace.result[prop] = null;
}
}
assertHasProperties(trace.result, resultProps);
for(let i=0; i<resultProps.length; i++){
let prop = resultProps[i];
trace[prop] = trace.result[prop];
}
delete trace.result;
if(!trace.subtype){
trace.subtype = null;
}
delete trace.blockHash;
//we'll need that
//delete trace.blockNumber;
delete trace.subtraces;
//do we really need the position of this trace in the whole tx?
//for that, we'll need something more than arctic DB
delete trace.traceAddress;
//we may need this
//delete trace.transactionHash;
delete trace.transactionPosition;
//this should be the final property set of the trace
assertHasProperties(trace, ['transactionHash', 'type', 'subtype', 'from', 'gas', 'to', 'value', 'gasUsed']);
return trace;
}
function assertHasProperties(obj, propertyList) {
if(typeof propertyList !== 'object'){
propertyList = [propertyList];
}
for(let i=0; i<propertyList.length; i++){
assert(obj.hasOwnProperty(propertyList[i]));
}
}
function renameProp(obj, prop, newProp) {
assert(obj.hasOwnProperty(prop));
obj[newProp] = obj[prop];
delete obj[prop];
return obj;
}
function cleanBlock(block){ //this function removes many useless for our cases fields in the block and txs
delete block.extraData;
delete block.hash;
delete block.logsBloom;
delete block.mixHash;
delete block.nonce;
delete block.parentHash;
delete block.receiptsRoot;
delete block.sha3Uncles;
delete block.stateRoot;
delete block.transactionsRoot;
delete block.uncles;
block.date = block.timestamp;
delete block.timestamp;
//parity-related properties
delete block.sealFields;
delete block.author;
//since this was loaded from a JSON, the BigNumber objects are no longer present
//block.difficulty = block.difficulty.toFixed()
//block.totalDifficulty = block.totalDifficulty.toFixed()
if(block.transactions.length > 0 && typeof block.transactions[0] !== 'string'){
for(i=0; i<block.transactions.length; i++){
tx = block.transactions[i];
cleanTransaction(tx);
}
}
return block;
}
//sets a 'date' field for each log and each trace, according to their block timestamp
//renames block.timestamp to block.date
//takes in a variable number of time series object arguments
function copyDateFromBlocks(blocks, objects){
assert(arguments.length >= 2);
for(let j=1; j<arguments.length; j++){
let obj = arguments[j];
for(let i=0; i<obj.length; i++){
obj[i].date = blocks[obj[i].blockNumber].date;
}
}
}
function sortByField(obj, field, ascending=true){
obj.sort(function(a, b){
if(ascending) {
return a[field] - b[field];
} else {
return b[field] - a[field];
}
});
return obj;
}
function dictToList(obj){
let list = [];
for(let key in obj) {
list.push(obj[key]);
}
return list;
}
function extractTxsFromBlockDict(blockDict){
let transactions = [];
for(let key in blockDict){
let txs = blockDict[key].transactions;
transactions.push.apply(transactions, txs);
delete blockDict[key].transactions;
}
return transactions;
}
function structureBlockchainData(blockDict, logs, traces){
let transactions = extractTxsFromBlockDict(blockDict);
if(typeof transactions[0] === 'string'){
transactions = []; //we don't need to store transactions if they consist only of tx hashes!
}
copyDateFromBlocks(blockDict, logs, traces, transactions);
let blockList = dictToList(blockDict);
sortByField(blockList, 'date');
sortByField(logs, 'date');
sortByField(traces, 'date');
sortByField(transactions, 'date');
console.log("Successfully loaded "+blockList.length+" blocks");
console.log("First block is "+blockList[0].number+" and last one is "+blockList[blockList.length-1].number);
let blockchain = {log: logs, trace: traces, block: blockList, tx: transactions};
//remove if we don't have TXs
if(transactions.length == 0){
delete blockchain.transactions;
}
return blockchain;
}
async function saveBlockchain(start, end, filename) {
let preprocessCallbacks = {'block': preprocessBlocks, 'log': preprocessContractLogs, 'trace': preprocessTransactionTraces};
let res = cacher.getBlockRange(start, end, preprocessCallbacks);
let txCount = 0;
let blCount = 0;
for(let bl in res['block']) {
txCount += res['block'][bl].transactions.length;
blCount++;
}
let traceCount = res['trace'].length;
console.log("The resulting package contains "+txCount+" transactions, "+res['log'].length+" logs and "+traceCount+" traces.");
//check if there have been too many traces, but only if we actually had enough transactions
if(txCount > blCount && traceCount > txCount * 5) {
console.error("Too many traces compared to transactions! Breaking, take a look.");
assert(false);
}
let blockchain = structureBlockchainData(res['block'], res['log'], res['trace']);
if(filename == null){
filename = datadir+"blocks "+blockchain['block'][0].number+"-"+blockchain['block'][blockchain['block'].length-1].number+".json";
}
jsonUtil.save(blockchain, filename);
}
async function saveCourse(filename) {
let course = await downloadWholeCourse()
if(!filename){
filename = 'data/course.json';
}
jsonUtil.save(course, filename)
}
function main() {
var parser = new ArgumentParser({
version: '0.1.0',
addHelp:true,
description: 'A tool to download cryptocurrency course and blockchain data and save them as a json'
});
parser.addArgument(
[ '--blockchain' ],
{
help: 'Download blockchain data with for a block range of given start and end block number',
nargs: 2,
defaultValue: false,
type: Number,
},
);
parser.addArgument(
['--course'],
{
help: 'Downloads the whole history of the token course from an exchange',
defaultValue: false,
action: "storeTrue",
},
)
parser.addArgument(
['--filename'],
{
help: 'Location to save downloaded data',
type: String,
},
)
var args = parser.parseArgs();
if(!args.course && !args.blockchain) {
console.error("Please choose an action! Run with '-h' for a help page.");
return;
}
if(args.course){
saveCourse(args.filename);
} else if (args.blockchain != null) {
let start = args.blockchain[0];
let end = args.blockchain[1];
if(isNaN(start) || isNaN(end)){
console.log("Enter the start block and block count to download.");
return;
}
saveBlockchain(start, end, args.filename);
}
}
main();