added config and node model
This commit is contained in:
parent
3475330fcf
commit
b69b6a31b3
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
.DS_Store
|
||||
*/**/.DS_Store
|
||||
node_modules
|
||||
npm-debug.log
|
||||
*~
|
||||
*.swp
|
||||
*.pem
|
||||
.node-xmlhttprequest-*
|
4
app.js
4
app.js
@ -5,7 +5,9 @@ var fs = require('fs');
|
||||
var app = express();
|
||||
app.http().io();
|
||||
|
||||
//
|
||||
var config = require('./config');
|
||||
|
||||
var node = new require('./lib/node')(config);
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function(req, res, next) {
|
||||
|
9
config.js
Normal file
9
config.js
Normal file
@ -0,0 +1,9 @@
|
||||
var config = {
|
||||
name: 'Node',
|
||||
type: 'C++',
|
||||
os: 'linux',
|
||||
rpcHost: 'localhost',
|
||||
rpcPort: '8080'
|
||||
};
|
||||
|
||||
module.exports = config;
|
65
lib/node.js
Normal file
65
lib/node.js
Normal file
@ -0,0 +1,65 @@
|
||||
var Node = function Node(options)
|
||||
{
|
||||
this.options = options;
|
||||
this.info = {
|
||||
name: options.name,
|
||||
ip: options.rpcHost,
|
||||
type: options.type,
|
||||
os: options.os
|
||||
};
|
||||
|
||||
this.info.id = this.info.ip;
|
||||
this.info.stats = {
|
||||
active: false,
|
||||
peers: 0,
|
||||
mining: false,
|
||||
block: {
|
||||
number: 0,
|
||||
hash: '?',
|
||||
timestamp: 0
|
||||
},
|
||||
uptime: {
|
||||
down: 0,
|
||||
inc: 0,
|
||||
total: 0
|
||||
}
|
||||
}
|
||||
|
||||
this.web3 = require('ethereum.js');
|
||||
|
||||
var sock = new this.web3.providers.HttpSyncProvider('http://' + this.options.rpcHost + ':' + this.options.rpcPort);
|
||||
this.web3.setProvider(sock);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
Node.prototype.update = function()
|
||||
{
|
||||
var eth = this.web3.eth;
|
||||
|
||||
try {
|
||||
this.info.stats.peers = eth.peerCount;
|
||||
}
|
||||
catch (err) {
|
||||
this.info.stats.peers = null;
|
||||
}
|
||||
|
||||
if(this.info.stats.peers != null) {
|
||||
this.info.stats.block = eth.block(parseInt(eth.number));
|
||||
if(this.info.stats.block.hash != '?'){
|
||||
this.info.stats.block.difficulty = this.web3.toDecimal(this.info.stats.block.difficulty);
|
||||
}
|
||||
this.info.stats.mining = eth.mining;
|
||||
this.info.stats.active = true;
|
||||
} else {
|
||||
this.info.stats.active = false;
|
||||
this.info.stats.uptime.down++;
|
||||
}
|
||||
|
||||
this.info.stats.uptime.inc++;
|
||||
this.info.stats.uptime.total = ((this.info.stats.uptime.inc - this.info.stats.uptime.down) / this.info.stats.uptime.inc) * 100;
|
||||
|
||||
return this.info;
|
||||
};
|
||||
|
||||
module.exports = Node;
|
Loading…
Reference in New Issue
Block a user