forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.js
72 lines (52 loc) · 1.56 KB
/
monitor.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
var Web3= require('web3');
var config=require('./config');
var fs=require('fs');
var BigNumber = require('bignumber.js');
var web3sync = require('./web3sync');
var post=require('./post');
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
web3 = new Web3(new Web3.providers.HttpProvider(config.HttpProvider));
}
/*
* npm install --save-dev babel-cli babel-preset-es2017
* echo '{ "presets": ["es2017"] }' > .babelrc
* npm install secp256k1 keccak rlp
*/
async function getPeerCount() {
return new Promise((resolve, reject) => {
web3.net.getPeerCount(function(e,d){
resolve(d);
});
});
}
async function sleep(timeout) {
return new Promise((resolve, reject) => {
setTimeout(function() {
resolve();
}, timeout);
});
}
(async function() {
while(1){
console.log("已连接节点数:"+ await getPeerCount() );
var peers=await post.post("admin_peers",[]);
try{
peers=JSON.parse(peers);
if( peers.result )
{
for( var i=0;i<peers.result.length;i++){
console.log("...........Node "+i+".........");
console.log("NodeId:"+peers.result[i].id);
console.log("Host:"+peers.result[i].network.remoteAddress);
}
}
console.log("当前块高"+await web3sync.getBlockNumber());
console.log("--------------------------------------------------------------");
await sleep(1500);
}catch(e){
console.log("admin_peers 返回解析失败!"+peers);
}
}
})()