forked from ming08108/EtherPool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shareHandler.js
151 lines (110 loc) · 3.82 KB
/
shareHandler.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
151
var ethUtil = require('ethereumjs-util');
var ethRPC = require('./ethRPC.js');
var Ethash = require('ethashjs');
var fs = require('fs');
var redis = require('redis');
var BloomFilter = require('bloom-filter');
var config = require('./config.js');
const levelup = require('levelup');
const memdown = require('memdown');
const BN = require('bn.js');
var cacheDB = levelup('', {
db: memdown
});
var ethash = new Ethash(cacheDB);
var numberOfElements = 100000;
var falsePositiveRate = 0.001;
var filter = BloomFilter.create(numberOfElements, falsePositiveRate);
var client = redis.createClient();
client.auth(config.redisPassword, redis.print);
var blockTarget;
var blockHeaderHash;
var verifyShare = function(nonce, headerHash, mixDigest, target, mh, address){
ethRPC.blockNumber(function (json){
var blockHex = json.result;
var block = parseInt(blockHex, 16);
console.log("block number (hex) " + blockHex);
var bloomData = new Buffer(nonce + headerHash, "hex");
ethash.loadEpoc(block, function(){
var a = ethash.run(new Buffer(headerHash, 'hex'), new Buffer(nonce, 'hex'));
var result = new BN(a.hash);
//if the result is smaller than the target diff then its valid and we should save it to a db
if(target.cmp(result) === 1 && blockHeaderHash.substring(2) === headerHash && !filter.contains(bloomData)){
console.log("Valid Share");
console.log("result " + result.toString(16));
console.log("blockTarget " + blockTarget);
console.log("address " + address);
filter.insert(bloomData);
//TODO test
client.zadd("shares:" + address, new Date().getTime(), mh + ":" + nonce + ":" + headerHash, function(err, response){
if(err){
console.log(err);
}
});
client.HINCRBY("totalRoundShares", address, mh, function(err, response){
if(err){
console.log(err);
}
});
if(new BN(blockTarget.substring(2), 16).cmp(result) === 1){
console.log("BLOCK FOUND!!!!!!!");
ethRPC.submitWork("0x"+nonce, "0x"+headerHash, "0x"+mixDigest, function(response){
console.log(response);
});
client.zadd("blocks", new Date().getTime(), block + ":" + nonce + ":" + headerHash + ":" + mixDigest, function(err, response){
if(err){
console.log(err);
}
});
}
}
else{
console.log("Invalid Share");
console.log("result " + result.toString(16));
}
});
});
}
var calculateTarget = function(mh){
var targetSeconds = new BN(30);
var h = new BN(mh).mul(new BN(1000000));
var d = targetSeconds.mul(h);
var target = ethUtil.TWO_POW256.div(d);
return target.toString(16);
}
//TODO
//json.result[2] = "0x4796721ca09033bd0d1dc3106155dc7a549d5e09e80000000000000000"
module.exports = {
getWork : function (res, mh) {
ethRPC.getWork(function(json){
blockTarget = json.result[2];
blockHeaderHash = json.result[0];
json.result[2] = calculateTarget(mh);
res.send(json);
});
},
submitShare : function (req, res, mh){
//console.log("submitWork");
var BNShareTarget = new BN(calculateTarget(req.params.mh), 16);
var mh = mh;
var address = req.params.address;
var nonce = req.body.params[0].substring(2);
var headerHash = req.body.params[1].substring(2);
var mixDigest = req.body.params[2].substring(2);
//console.log("nonce " + nonce + " headerHash " + headerHash + " mixDigest " + mixDigest);
verifyShare(nonce, headerHash, mixDigest, BNShareTarget, mh, address);
//send this to appease the mining client
res.send('{"id": 1, "jsonrpc":"2.0", "result": true}');
},
submitHashrate : function(req, res){
var mh = req.params.mh;
var address = req.params.address;
var hashrate = req.body.params[0]
client.zadd("reportedHashrate", parseInt(hashrate.substring(2), 16), address, function(err, r){
if(err){
console.log(err)
}
res.send('{"id":73,"jsonrpc":"2.0","result": true}');
});
}
}