forked from xcps/gostexplr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyncBlockchain.js
290 lines (251 loc) · 6.72 KB
/
syncBlockchain.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
var http = require('http');
const fs = require('fs-ext');
const moment = require('moment');
var models = require('../models');
var rpcConfig = require('../config/config')['rpc'];
var HeightOffset = require('../config/config')['syncHeightOffset'] || 0;
const {username, password, hostname, port} = rpcConfig;
const keepAliveAgent = new http.Agent({ keepAlive: true });
let sync_sql = '',
coolstrs = [],
starttime = 0;
function MakeRPCRequest(postData) {
return new Promise(function(resolve, reject) {
var post_options = {
host: hostname,
port: port,
auth: `${username}:${password}`,
path: '/',
method: 'POST',
agent: keepAliveAgent,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
var post_req = http.request(post_options, function(res) {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
resolve(body);
});
});
post_req.on('error', function(err) {
if (err.code == 'ECONNREFUSED') {
console.log('\x1b[36m%s\x1b[0m', "Couldn't make request to wallet")
}
reject(err);
});
post_req.write(postData);
post_req.end();
});
}
async function saveTransaction(txid, blockHeight) {
const res_tx = await MakeRPCRequest(JSON.stringify({
method: 'getrawtransaction',
params: [txid, 1],
id: 1
}));
const tx = JSON.parse(res_tx)['result'];
if (tx === null) {
await models.Failure.create({
msg: `${txid} fetching failed`,
});
return;
}
sync_sql += `
INSERT INTO Transactions (
txid,
BlockHeight
)
VALUES (
"${txid}",
${blockHeight}
);
SET @txid = LAST_INSERT_ID();
`;
// Loop over vout's
for (var i = 0; i < tx.vout.length; i++) {
const vout = tx.vout[i];
sync_sql += `
INSERT INTO Vouts (n, value)
VALUES ("${vout.n}", "${vout.value}");
SET @voutid= LAST_INSERT_ID();
`;
// Loop over addresses in vout
for (var y = 0; y < vout.scriptPubKey.addresses.length; y++) {
const address = vout.scriptPubKey.addresses[y];
sync_sql += `
INSERT IGNORE INTO Addresses (address) VALUES ("${address}");
SET @addrid = (
SELECT IF(
ROW_COUNT() > 0,
LAST_INSERT_ID(),
(
SELECT id
FROM Addresses
WHERE address='${address}'
)
)
);
`;
sync_sql += `
INSERT INTO AddressVouts (AddressId, VoutId)
VALUES (@addrid, @voutid);
`;
}
sync_sql += `
INSERT INTO TransactionVouts (TransactionId, VoutId, direction)
VALUES (@txid, @voutid, 1);
`;
}
// Loop over vin's
for (var i = 0; i < tx.vin.length; i++) {
const vin = tx.vin[i];
if (vin.txid) {
sync_sql += `
SET @vin = (
SELECT Vouts.id
FROM Vouts
INNER JOIN TransactionVouts
ON Vouts.id=TransactionVouts.VoutId
INNER JOIN Transactions
ON Transactions.id=TransactionVouts.TransactionId
WHERE
TransactionVouts.direction=1 AND
Transactions.txid="${vin.txid}" AND
Vouts.n=${vin.vout}
);
INSERT INTO TransactionVouts (TransactionId, VoutId, direction)
VALUES (@txid, @vin, 0);
`
}
}
}
async function syncNextBlock(syncedHeight) {
const height = syncedHeight + 1;
sync_sql = '';
const res_hash = await MakeRPCRequest(JSON.stringify({
method: 'getblockhash',
params: [height],
id: 1
}));
const blockHash = JSON.parse(res_hash)['result'];
const res_block = await MakeRPCRequest(JSON.stringify({
method: 'getblock',
params: [blockHash],
id: 1
}));
const block = JSON.parse(res_block)['result'];
const res_blockhr = await MakeRPCRequest(JSON.stringify({
method: 'getnetworkhashps',
params: [120, height],
id: 1
}));
block.hashrate = JSON.parse(res_blockhr)['result'];
block.time = moment(block.time*1000).utc().format('YYYY-MM-DD HH:mm:ss Z');
sync_sql = `
SET autocommit = 0;
START TRANSACTION;
INSERT INTO Block (
hash,
height,
size,
version,
merkleroot,
time,
nonce,
bits,
difficulty,
hashrate,
previousblockhash
)
VALUES (
"${block.hash}",
"${block.height}",
"${block.size}",
"${block.version}",
"${block.merkleroot}",
"${block.time}",
"${block.nonce}",
"${block.bits}",
"${block.difficulty}",
"${block.hashrate}",
"${block.previousblockhash}"
);
`
coolstrs = []
for (var i = 0; i < block.tx.length; i++) {
await saveTransaction(block.tx[i], block.height);
coolstrs.push(`${block.tx[i]} - ${block.time}`);
}
if (block.height > 1) {
sync_sql += `
UPDATE Block
SET nextblockhash="${block.hash}"
WHERE hash="${block.previousblockhash}";
`
}
sync_sql += 'COMMIT;'
await models.sequelize.query(sync_sql);
return height;
}
async function getCurrentHeight() {
const result = await MakeRPCRequest(JSON.stringify({
method: 'getblockcount',
params: [],
id: 1
}));
return JSON.parse(result)['result'] - HeightOffset;
}
async function getSyncedHeight() {
const result = await models.Block.findOne({
attributes: ['height'],
order: [['height', 'DESC']],
limit: 1
});
const height = result ? result.height : -1;
return height;
}
async function acquireLock() {
let fd = fs.openSync('sync.lock', 'w');
try {
fs.flockSync(fd, 'exnb');
} catch(ex) {
if (ex.code === 'EAGAIN') {
console.log('Synchronization is already running');
} else {
console.log('Could\'nt lock file', ex);
}
process.exit(0);
}
}
async function syncBlockchain() {
try {
await acquireLock();
let syncedHeight = await getSyncedHeight();
console.log('\x1b[36m%s\x1b[0m', 'syncedHeight is', syncedHeight);
let currentHeight = await getCurrentHeight();
console.log('\x1b[34m%s\x1b[0m', 'currentHeight is', currentHeight);
while (syncedHeight < currentHeight) {
starttime = new Date().getTime();
syncedHeight = await syncNextBlock(syncedHeight);
if (coolstrs) {
for(str of coolstrs) {
console.log('\x1b[36m%s\x1b[0m', `syncedHeight: ${syncedHeight}/${currentHeight}`, str, ' [', new Date().getTime() - starttime, 'ms ]')
}
} else {
console.log('\x1b[36m%s\x1b[0m', 'syncedHeight: ', syncedHeight)
}
}
} catch (e) {
console.log(e);
} finally {
await models.sequelize.close();
process.exit(0);
}
}
syncBlockchain();