-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathblockTemplate.js
184 lines (151 loc) · 6.68 KB
/
blockTemplate.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var bignum = require('bignum');
var crypto = require('crypto');
var SHA3 = require('sha3');
var merkle = require('./merkleTree.js');
var transactions = require('./transactions.js');
var util = require('./util.js');
/**
* The BlockTemplate class holds a single job.
* and provides several methods to validate and submit it to the daemon coin
**/
var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, reward, recipients, poolAddress){
//epoch length
const EPOCH_LENGTH = 7500;
//private members
var submits = [];
//public members
this.rpcData = rpcData;
this.jobId = jobId;
// get target info
this.target = bignum(rpcData.target, 16);
this.target_hex = rpcData.target;
this.difficulty = parseFloat((diff1 / this.target.toNumber()).toFixed(9));
console.log("In BlockTemplate difficulty is "+this.difficulty);
//nTime
var nTime = util.packUInt32BE(rpcData.curtime).toString('hex');
//current time of issuing the template
var curTime = Date.now() / 1000 | 0;
// generate the fees and coinbase tx
var blockReward = this.rpcData.coinbasevalue;
var fees = [];
rpcData.transactions.forEach(function(value) {
fees.push(value);
});
this.rewardFees = transactions.getFees(fees);
rpcData.rewardFees = this.rewardFees;
if (typeof this.genTx === 'undefined') {
this.genTx = transactions.createGeneration(rpcData, blockReward, this.rewardFees, recipients, poolAddress).toString('hex');
this.genTxHash = transactions.txHash();
// console.log('this.genTxHash: ' + transactions.txHash());
// console.log('this.merkleRoot: ' + merkle.getRoot(rpcData, this.genTxHash));
}
// generate the merkle root
this.prevHashReversed = util.reverseBuffer(new Buffer(rpcData.previousblockhash, 'hex')).toString('hex');
this.merkleRoot = merkle.getRoot(rpcData, this.genTxHash);
this.txCount = this.rpcData.transactions.length + 1; // add total txs and new coinbase
this.merkleRootReversed = util.reverseBuffer(new Buffer(this.merkleRoot, 'hex')).toString('hex');
// we can't do anything else until we have a submission
// console.log('this.prevHashReversed: ' + this.prevHashReversed);
this.serializeHeader = function() {
var header = new Buffer(80);
var position = 0;
header.write(util.packUInt32BE(this.rpcData.height).toString('hex'), position, 4, 'hex'); // height 42-46
header.write(this.rpcData.bits, position += 4, 4, 'hex'); // bits 47-50
header.write(nTime, position += 4, 4, 'hex'); // nTime 51-54
header.write(this.merkleRoot, position += 4, 32, 'hex'); // merkelRoot 55-87
header.write(this.rpcData.previousblockhash, position += 32, 32, 'hex'); // prevblockhash 88-120
header.writeUInt32BE(this.rpcData.version, position + 32, 4); // version 121-153
header = util.reverseBuffer(header);
return header;
};
// join the header and txs together
this.serializeBlock = function(header_hash, nonce, mixhash) {
header = this.serializeHeader();
var foo = new Buffer(40);
foo.write(util.reverseBuffer(nonce).toString('hex'), 0, 8, 'hex');
foo.write(util.reverseBuffer(mixhash).toString('hex'), 8, 32,'hex');
buf = new Buffer.concat([
header,
foo,
util.varIntBuffer(this.rpcData.transactions.length + 1),
new Buffer(this.genTx, 'hex')
]);
if (this.rpcData.transactions.length > 0) {
this.rpcData.transactions.forEach(function (value) {
tmpBuf = new Buffer.concat([buf, new Buffer(value.data, 'hex')]);
buf = tmpBuf;
});
}
/*
console.log('header: ' + header.toString('hex'));
console.log('soln: ' + soln.toString('hex'));
console.log('varInt: ' + varInt.toString('hex'));
console.log('this.genTx: ' + this.genTx);
console.log('data: ' + value.data);
console.log('buf_block: ' + buf.toString('hex'));
console.log('blockhex: ' + buf.toString('hex'));
*/
return buf;
};
// submit header_hash and nonce
this.registerSubmit = function(header, nonce){
var submission = header + nonce;
if (submits.indexOf(submission) === -1){
submits.push(submission);
return true;
}
return false;
};
//powLimit * difficulty
var powLimit = algos.kawpow.diff; // TODO: Get algos object from argument
var adjPow = (powLimit / this.difficulty);
if ((64 - adjPow.toString(16).length) === 0) {
var zeroPad = '';
}
else {
var zeroPad = '0';
zeroPad = zeroPad.repeat((64 - (adjPow.toString(16).length)));
}
var target = (zeroPad + adjPow.toString(16)).substr(0,64);
//this.target_share_hex = target;
let d = new SHA3.SHA3Hash(256);
var seedhash_buf = new Buffer(32);
var seedhash = seedhash_buf.toString('hex');
this.epoch_number = Math.floor(this.rpcData.height / EPOCH_LENGTH);
for (var i=0; i<this.epoch_number; i++) {
d = new SHA3.SHA3Hash(256);
d.update(seedhash_buf);
seedhash_buf = d.digest();
seedhash = d.digest('hex');
// console.log("seedhash(#"+i+")= "+seedhash.toString('hex'));
}
var header_hash = this.serializeHeader(); // 140 bytes (doesn't contain nonce or mixhash)
// console.log("****************************");
// console.log("Header hash sent to miners: "+header_hash.toString('hex'));
// console.log("****************************");
header_hash = util.reverseBuffer(util.sha256d(header_hash)).toString('hex');
//change override_target to a minimum wanted target. This is useful for e.g. testing on testnet.
var override_target = 0;
//override_target = 0x0000000FFFFF0000000000000000000000000000000000000000000000000000;
if ((override_target != 0) && (adjPow > override_target)) {
zeroPad = '0';
zeroPad = zeroPad.repeat((64 - (override_target.toString(16).length)));
target = (zeroPad + override_target.toString(16)).substr(0,64);
}
// used for mining.notify
this.getJobParams = function(){
// console.log("RPC DATA IN job params: "+JSON.stringify(this.rpcData));
if (!this.jobParams){
this.jobParams = [
this.jobId,
header_hash,
seedhash,
target, //target is overridden later to match miner varDiff
true,
this.rpcData.height,
this.rpcData.bits
];
}
return this.jobParams;
};
};