-
Notifications
You must be signed in to change notification settings - Fork 28
/
daemon.js
187 lines (130 loc) · 5.07 KB
/
daemon.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
var assert = require('better-assert');
var async = require('async');
var bc = require('./src/bitcoin_client');
var db = require('./src/db');
var lib = require('./src/lib');
var client;
// Mapping of deposit_address -> user_id
var depositAddresses = {};
var count = process.env.GENERATE_ADDRESSES ? parseInt(process.env.GENERATE_ADDRESSES) : 100; // how many addresses to watch
console.log('Generating ', count, ' addresses');
for (var i = 1; i <= count; ++i) {
var address = lib.deriveAddress(i);
depositAddresses[address] = i;
}
console.log('Finished generating addresses', depositAddresses);
startBlockLoop();
function processTransactionIds(txids, callback) {
bc.getTransactionIdsAddresses(txids, function(err, addressToAmountLists) {
if (err) return callback(err);
assert(txids.length === addressToAmountLists.length);
var tasks = [];
addressToAmountLists.forEach(function(addressToAmount, i) {
var txid = txids[i];
assert(txid);
var usersToAmounts = {};
Object.keys(addressToAmount).forEach(function(address) {
var userId = depositAddresses[address];
if (userId) {
usersToAmounts[userId] = addressToAmount[address];
}
});
if (Object.keys(usersToAmounts).length > 0) {
console.log('Transactions: ', txid, ' matches: ', usersToAmounts);
Object.keys(usersToAmounts).forEach(function(userId) {
tasks.push(function(callback) {
db.addDeposit(userId, txid, usersToAmounts[userId], callback);
});
});
}
});
async.parallelLimit(tasks, 3, callback);
});
}
// Handling the block...
/// block chain loop
var lastBlockCount;
var lastBlockHash;
function startBlockLoop() {
// initialize...
db.getLastBlock(function (err, block) {
if (err)
throw new Error('Unable to get initial last block: ', err);
lastBlockCount = block.height;
lastBlockHash = block.hash;
console.log('Initialized on block: ', lastBlockCount, ' with hash: ', lastBlockHash);
blockLoop();
});
}
function scheduleBlockLoop() {
setTimeout(blockLoop, 20000);
}
function blockLoop() {
bc.getBlockCount(function(err, num) {
if (err) {
console.error('Unable to get block count');
return scheduleBlockLoop();
}
if (num === lastBlockCount) {
console.log('Block chain still ', num, ' length. No need to do anything');
return scheduleBlockLoop();
}
bc.getBlockHash(lastBlockCount, function(err, hash) {
if (err) {
console.error('Could not get block hash, error: ' + err);
return scheduleBlockLoop();
}
if (lastBlockHash !== hash) {
// There was a block-chain reshuffle. So let's just jump back a block
db.getBlock(lastBlockCount - 1, function(err, block) {
if (err) {
console.error('ERROR: Unable jump back ', err);
return scheduleBlockLoop();
}
--lastBlockCount;
lastBlockHash = block.hash;
blockLoop();
});
return;
}
bc.getBlockHash(lastBlockCount+1, function(err, hash) {
if (err) {
console.error('Unable to get block hash: ', lastBlockCount+1);
return scheduleBlockLoop();
}
processBlock(hash, function(err) {
if (err) {
console.error('Unable to process block: ', hash, ' because: ', err);
return scheduleBlockLoop();
}
++lastBlockCount;
lastBlockHash = hash;
db.insertBlock(lastBlockCount, lastBlockHash, function(err) {
if (err)
console.error('Danger, unable to save results in database...');
// All good! Loop immediately!
blockLoop();
});
});
});
});
});
}
function processBlock(hash, callback) {
console.log('Processing block: ', hash);
var start = new Date();
bc.getBlock(hash, function(err, blockInfo) {
if (err) {
console.error('Unable to get block info for: ', hash, ' got error: ', err);
return callback(err);
}
var transactionsIds = blockInfo.tx;
processTransactionIds(transactionsIds, function(err) {
if (err)
console.log('Unable to process block (in ', (new Date() - start ) / 1000, ' seconds)');
else
console.log('Processed ', transactionsIds.length, ' transactions in ', (new Date() - start ) / 1000, ' seconds');
callback(err)
});
});
}