-
Notifications
You must be signed in to change notification settings - Fork 0
/
parserThread.js
176 lines (152 loc) · 6.55 KB
/
parserThread.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
/**
* Created by ksollner on 15.02.14.
*/
/*
* This is the javascript file that pulls the transaction data from protosharesd via rpc
* and fills the sql database with it. You need this thread permanently to keep the database
* up to date. I recomment pm2(https://github.com/Unitech/pm2)
* You also need to setup the mysql server, database and tables and change the login data accordingly
*/
async = require("async");
mysql = require("mysql");
connection = mysql.createConnection({
host: "localhost",
user: "parse_user",
password: "mQhURtm4qaLbsxl",
database: "parse_db",
multipleStatements: true
});
connection.connect();
function err(err) {
if (err)
throw(err);
}
var bitcoin = require('bitcoin');
var client = new bitcoin.Client({
host: 'localhost',
port: 3838,
user: 'mytestuser',
pass: 'alibabatrinktmeinenkaba!'
});
var addresses = {};
var addresscount = 0;
var currentBlockCount = 0;
var parsedBlocks = 1;
var currentBlockTime = 0;
var transactions = {};
var coin = function (val) {
return Math.round(val * 100000000);
}
var query1Values = [];
var query2Values = [];
var query3Values = [];
currentDay = 0;
addressId = function (address) {
if (!addresses[address]) {
addresses[address] = ++addresscount;
query3Values.push([address, addresscount]);
return addresscount;
}
return addresses[address];
}
function getNextBlock() {
// get number of blocks
var block = {};
client.cmd('getblockcount', function (err, block_count) {
if (err)
return console.log(err);
// if we already parsed all blocks try again in 10 seconds
if (parsedBlocks >= block_count) {
return setTimeout(getNextBlock, 10000);
}
block.height = parsedBlocks;
currentBlockCount = block_count;
// get the block hash
client.cmd('getblockhash', parsedBlocks, function (err, block_hash) {
if (err)
return console.log(err);
// get the block
block.hash = block_hash;
client.cmd('getblock', block_hash, function (err, block_info) {
if (err)
return console.log(err);
// update current block time
currentBlockTime = block_info.time;
block.time = block_info.time;
if (Math.ceil(block_info.time / 86400 - 16015) > currentDay) {
currentDay++;
// Do "daily" bulk inserts for better performance
if (query1Values.length > 0) {
connection.query("INSERT INTO transactions2 (id_address, block, time, day, `change`) VALUES ?", [query1Values], function (err, result) {
if (err)
console.log("q1", err);
});
query1Values = [];
}
if (query3Values.length > 0) {
connection.query("INSERT INTO addresses (address, id) VALUES ?", [query3Values], function (err, result) {
if (err)
console.log("q2", err);
});
query3Values = [];
}
if (query2Values.length > 0) {
connection.query("INSERT INTO donations (address, block, time, day, amount) VALUES ?", [query2Values], function (err, result) {
if (err)
console.log("q3", err);
});
query2Values = [];
}
console.log("Day:", currentDay);
}
// get all transactions
block.rawtransactions = [];
block.transactions = [];
// Query this block's transaction infos before processing them
async.mapSeries(block_info.tx, function (txid, txInfoCallback) {
client.cmd('getrawtransaction', txid, function (err, rawtransaction) {
if (err)
console.log(err, txid);
client.cmd('decoderawtransaction', rawtransaction, txInfoCallback);
});
// process them
}, function (err, block_transactions) {
if (err)
console.log(err);
block_transactions.forEach(function (transaction_info) {
var outputs = [];
var inputs = [];
transaction_info.vin.forEach(function (input) {
// ignore mined coins as inputs
if (!input.txid)
return;
var input_address = transactions[input.txid].outputs[input.vout].address;
var input_value = transactions[input.txid].outputs[input.vout].value;
delete transactions[input.txid].outputs[input.vout];
inputs.push({address: input_address});
query1Values.push([addressId(input_address), parsedBlocks, block_info.time, currentDay, coin(-input_value)]);
});
transaction_info.vout.forEach(function (output) {
var output_address = output.scriptPubKey.addresses[0];
outputs.push({address: output_address, value: output.value});
query1Values.push([addressId(output_address), parsedBlocks, block_info.time, currentDay, coin(output.value)]);
if (output_address == "PaNGELmZgzRQCKeEKM6ifgTqNkC4ceiAWw") {
var donation_address = inputs[0].address;
query2Values.push([addressId(donation_address), parsedBlocks, block_info.time, Math.ceil(block_info.time / 86400 - 16015), coin(output.value)]);
}
});
transactions[transaction_info.txid] = {outputs: outputs};
});
parsedBlocks++;
setImmediate(getNextBlock);
});
});
});
});
}
// on startup: empty database and start over filling the database
connection.query("TRUNCATE TABLE transactions2; TRUNCATE TABLE donations; TRUNCATE TABLE addresses", function (err, reuslt) {
if (err)
throw(err);
getNextBlock();
});