/
childDaemon.js
84 lines (76 loc) · 2.16 KB
/
childDaemon.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
let async = require('async');
let apiInterfaces = require('./apiInterfaces.js')(config.daemon, config.wallet, config.api);
let lastHash;
let POOL_NONCE_SIZE = 16 + 1; // +1 for old XMR/new TRTL bugs
let logSystem = 'childDaemon'
require('./exceptionWriter.js')(logSystem);
let pool = config.childPools[process.env.poolId];
let blockData = JSON.stringify({
id: "0",
jsonrpc: "2.0",
method: 'getlastblockheader',
params: {}
})
let templateData = JSON.stringify({
id: "0",
jsonrpc: "2.0",
method: 'getblocktemplate',
params: {
reserve_size: POOL_NONCE_SIZE,
wallet_address: pool.poolAddress
}
})
function runInterval () {
async.waterfall([
function (callback) {
apiInterfaces.jsonHttpRequest(pool.childDaemon.host, pool.childDaemon.port, blockData, function (err, res) {
if (err) {
log('error', logSystem, '%s error from daemon', [pool.coin]);
setTimeout(runInterval, 3000);
return;
}
if (res && res.result && res.result.status === "OK" && res.result.hasOwnProperty('block_header')) {
let hash = res.result.block_header.hash.toString('hex');
if (!lastHash || lastHash !== hash) {
lastHash = hash
log('info', logSystem, '%s found new hash %s', [pool.coin, hash]);
callback(null, true);
return;
} else if (config.daemon.alwaysPoll || false) {
callback(null, true);
return;
} else {
callback(true);
return;
}
} else {
log('error', logSystem, '%s bad reponse from daemon', [pool.coin]);
setTimeout(runInterval, 3000);
return;
}
});
},
function (getbc, callback) {
apiInterfaces.jsonHttpRequest(pool.childDaemon.host, pool.childDaemon.port, templateData, function (err, res) {
if (err) {
log('error', logSystem, '%s Error polling getblocktemplate %j', [pool.coin, err])
callback(null)
return
}
process.send({
type: 'ChildBlockTemplate',
block: res.result,
poolIndex: process.env.poolId
})
callback(null)
})
}
],
function (error) {
if (error) {}
setTimeout(function () {
runInterval()
}, config.poolServer.blockRefreshInterval)
})
}
runInterval()