Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
105 lines (89 loc) · 2.65 KB
/
Copy pathserver.js
File metadata and controls
105 lines (89 loc) · 2.65 KB
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
var argv = require('optimist')
.usage('Usage: $0 -p [mining port]')
.demand(['p'])
.argv;
var rpc = require('./libs/jsonrpc');
var config = require('./config.json');
if(config.bitcoind_ip=='127.0.0.1') {
// load bitcoin.conf if using local bitcoind
var confparser=require('./libs/confparser');
var conf={};
try{
conf=confparser.parseSync(config.bitcoind_path);
} catch(e) {
console.log("Can't find bitcoin.conf");
process.exit(1);
};
if(conf.testnet=='1') {
console.log("bitcoind is running on testnet!");
process.exit(1);
};
if(conf.server!='1') {
console.log("Set 'server=1' in bitcoin.conf");
process.exit(1);
};
if(!conf.rpcport || !conf.rpcuser || !conf.rpcpassword) {
console.log("Missing rpcport,rpcuser,rpcpassword in bitcoin.conf")
process.exit(1);
};
config.bitcoind_port = conf.rpcport;
config.bitcoind_user = conf.rpcuser;
config.bitcoind_pwd = conf.rpcpassword;
};
var bitcoind = require('./libs/kapitalize')({
'user': config.bitcoind_user,
'pass': config.bitcoind_pwd,
'host': config.bitcoind_ip,
'port': config.bitcoind_port
});
var job = new (require('./libs/jobs'))(bitcoind);
job.update_block();
var server = new rpc.Server();
var shares = 0;
var jobs = 0;
var blocks = [];
var start = +new Date();
var workers = {};
function getwork(args, opt, callback) {
var ip = opt.req.connection.remoteAddress;
// console.log( ip + " Asks for job\n");
if(!workers[ip]) workers[ip] = {"shares":0,"jobs":0,"last_seen":+new Date()};
if(args.length==0) {
workers[ip].jobs++;
callback(null,job.getwork());
} else {
shares++;
// console.log( ip + " Submits share");
workers[ip].shares++;
workers[ip].last_seen = +new Date();
var res = job.submit(args[0].slice(0,160));
// console.log(res);
if(res.found) {
var block = {};
block.hash = res.hash;
block.timestamp = (new Date()).toLocaleString();
if(!res.staled) {
blocks.push(block);
};
}
callback(null,"truetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetruetrue");
}
};
function update(args,opt,callback) {
job.update_block();
console.log("BitCoin Updated At:%s",new Date());
callback(null,true);
};
function stats(args,opt,callback) {
var response = {};
response.blocks = blocks;
response.start = start;
response.height = job.height;
response.shares = shares;
response.workers = JSON.stringify(workers);
callback(null,JSON.stringify(response));
};
server.expose('getwork', getwork);
server.expose('update',update);
server.expose('stats',stats);
server.listen(argv.p, '0.0.0.0');