-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstatus.js
150 lines (132 loc) · 3.34 KB
/
status.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
'use strict';
var Common = require('./common');
function StatusController(node) {
this.node = node;
this.common = new Common({log: this.node.log});
}
StatusController.prototype.show = function(req, res) {
var self = this;
var option = req.query.q;
switch(option) {
case 'getDifficulty':
this.getDifficulty(function(err, result) {
if (err) {
return self.common.handleErrors(err, res);
}
res.jsonp(result);
});
break;
case 'getLastBlockHash':
res.jsonp(this.getLastBlockHash());
break;
case 'getBestBlockHash':
this.getBestBlockHash(function(err, result) {
if (err) {
return self.common.handleErrors(err, res);
}
res.jsonp(result);
});
break;
case 'getInfo':
default:
this.getInfo(function(err, result) {
if (err) {
return self.common.handleErrors(err, res);
}
res.jsonp({
info: result
});
});
}
};
StatusController.prototype.getInfo = function(callback) {
this.node.services.bitcoind.getInfo(function(err, result) {
if (err) {
return callback(err);
}
var info = {
version: result.version,
protocolversion: result.protocolVersion,
KMDversion: result.KMDversion,
blocks: result.blocks,
timeoffset: result.timeOffset,
connections: result.connections,
proxy: result.proxy,
difficulty: result.difficulty,
testnet: result.testnet,
relayfee: result.relayFee,
errors: result.errors,
notarized: result.notarized,
network: result.network
};
callback(null, info);
});
};
StatusController.prototype.getLastBlockHash = function() {
var hash = this.node.services.bitcoind.tiphash;
return {
syncTipHash: hash,
lastblockhash: hash
};
};
StatusController.prototype.getBestBlockHash = function(callback) {
this.node.services.bitcoind.getBestBlockHash(function(err, hash) {
if (err) {
return callback(err);
}
callback(null, {
bestblockhash: hash
});
});
};
StatusController.prototype.getDifficulty = function(callback) {
this.node.services.bitcoind.getInfo(function(err, info) {
if (err) {
return callback(err);
}
callback(null, {
difficulty: info.difficulty
});
});
};
StatusController.prototype.sync = function(req, res) {
var self = this;
var status = 'syncing';
this.node.services.bitcoind.isSynced(function(err, synced) {
if (err) {
return self.common.handleErrors(err, res);
}
if (synced) {
status = 'finished';
}
self.node.services.bitcoind.syncPercentage(function(err, percentage) {
if (err) {
return self.common.handleErrors(err, res);
}
var info = {
status: status,
blockChainHeight: self.node.services.bitcoind.height,
syncPercentage: Math.round(percentage),
height: self.node.services.bitcoind.height,
error: null,
type: 'bitcore node'
};
res.jsonp(info);
});
});
};
// Hard coded to make insight ui happy, but not applicable
StatusController.prototype.peer = function(req, res) {
res.jsonp({
connected: true,
host: '127.0.0.1',
port: null
});
};
StatusController.prototype.version = function(req, res) {
var pjson = require('../package.json');
res.jsonp({
version: pjson.version
});
};
module.exports = StatusController;