Skip to content

Commit 9469d55

Browse files
Update blockTemplate.js
1 parent 4f05ef8 commit 9469d55

File tree

1 file changed

+35
-83
lines changed

1 file changed

+35
-83
lines changed

lib/blockTemplate.js

+35-83
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,45 @@
11
var bignum = require('bignum');
2-
32
var merkleTree = require('./merkleTree.js');
43
var transactions = require('./transactions.js');
54
var util = require('./util.js');
65

7-
86
/**
97
* The BlockTemplate class holds a single job.
108
* and provides several methods to validate and submit it to the daemon coin
119
**/
1210
var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, poolAddressScript, extraNoncePlaceholder, reward, txMessages, recipients, network) {
1311

1412
//private members
15-
1613
var submits = [];
1714

1815
function getMerkleHashes(steps) {
19-
return steps.map(function (step) {
20-
return step.toString('hex');
21-
});
16+
return steps.map(step => step.toString('hex'));
2217
}
2318

2419
function getTransactionBuffers(txs) {
25-
var txHashes = txs.map(function (tx) {
26-
if (tx.txid !== undefined) {
27-
return util.uint256BufferFromHash(tx.txid);
28-
}
29-
return util.uint256BufferFromHash(tx.hash);
30-
});
31-
return [null].concat(txHashes);
20+
return [null].concat(txs.map(tx => Buffer.from(util.uint256BufferFromHash(tx.txid || tx.hash))));
3221
}
3322

3423
function getVoteData() {
35-
if (!rpcData.masternode_payments) return new Buffer([]);
36-
37-
return Buffer.concat(
38-
[util.varIntBuffer(rpcData.votes.length)].concat(
39-
rpcData.votes.map(function (vt) {
40-
return new Buffer(vt, 'hex');
41-
})
42-
)
43-
);
24+
if (!rpcData.masternode_payments) return Buffer.alloc(0);
25+
return Buffer.concat([
26+
util.varIntBuffer(rpcData.votes.length),
27+
...rpcData.votes.map(vt => Buffer.from(vt, 'hex'))
28+
]);
4429
}
4530

4631
//public members
47-
4832
this.rpcData = rpcData;
4933
this.jobId = jobId;
50-
51-
52-
this.target = rpcData.target ?
53-
bignum(rpcData.target, 16) :
54-
util.bignumFromBitsHex(rpcData.bits);
55-
34+
this.target = rpcData.target ? bignum(rpcData.target, 16) : util.bignumFromBitsHex(rpcData.bits);
5635
this.difficulty = parseFloat((diff1 / this.target.toNumber()).toFixed(9));
36+
this.prevHashReversed = util.reverseByteOrder(Buffer.from(rpcData.previousblockhash, 'hex')).toString('hex');
5737

58-
59-
60-
this.prevHashReversed = util.reverseByteOrder(new Buffer(rpcData.previousblockhash, 'hex')).toString('hex');
6138
if (rpcData.finalsaplingroothash) {
62-
this.finalsaplingroothashReversed = util.reverseByteOrder(new Buffer(rpcData.finalsaplingroothash, 'hex')).toString('hex');
63-
}
64-
if (rpcData.hashstateroot) {
65-
this.hashstaterootReversed = util.reverseBuffer(new Buffer(rpcData.hashstateroot, 'hex')).toString('hex');
39+
this.finalsaplingroothashReversed = util.reverseByteOrder(Buffer.from(rpcData.finalsaplingroothash, 'hex')).toString('hex');
6640
}
67-
if (rpcData.hashutxoroot) {
68-
this.hashutxorootReversed = util.reverseBuffer(new Buffer(rpcData.hashutxoroot, 'hex')).toString('hex');
69-
}
70-
this.transactionData = Buffer.concat(rpcData.transactions.map(function (tx) {
71-
return new Buffer(tx.data, 'hex');
72-
}));
41+
42+
this.transactionData = Buffer.concat(rpcData.transactions.map(tx => Buffer.from(tx.data, 'hex')));
7343
this.merkleTree = new merkleTree(getTransactionBuffers(rpcData.transactions));
7444
this.merkleBranch = getMerkleHashes(this.merkleTree.steps);
7545
this.generationTransaction = transactions.CreateGeneration(
@@ -83,31 +53,23 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, pool
8353
);
8454

8555
this.serializeCoinbase = function (extraNonce1, extraNonce2) {
56+
const nonce1Buffer = Buffer.isBuffer(extraNonce1) ? extraNonce1 : Buffer.from(extraNonce1);
57+
const nonce2Buffer = Buffer.isBuffer(extraNonce2) ? extraNonce2 : Buffer.from(extraNonce2);
58+
8659
return Buffer.concat([
8760
this.generationTransaction[0],
88-
extraNonce1,
89-
extraNonce2,
61+
nonce1Buffer,
62+
nonce2Buffer,
9063
this.generationTransaction[1]
9164
]);
9265
};
9366

9467

9568
//https://en.bitcoin.it/wiki/Protocol_specification#Block_Headers
96-
this.serializeHeader = function (merkleRoot, nTime, nonce) {
97-
98-
var headerSize;
99-
if (rpcData.version == 5 && rpcData.finalsaplingroothash) {
100-
headerSize = 112;
101-
/*} else if (rpcData.hashstateroot && rpcData.hashutxoroot) {
102-
headerSize = 181;
103-
*/
104-
} else {
105-
headerSize = 80;
106-
}
107-
108-
var header = new Buffer(headerSize);
69+
this.serializeHeader = function(merkleRoot, nTime, nonce) {
70+
var headerSize = rpcData.version == 5 && rpcData.finalsaplingroothash ? 112 : 80;
71+
var header = Buffer.alloc(headerSize);
10972
var position = 0;
110-
11173
/*
11274
console.log('headerSize:' + headerSize);
11375
console.log('rpcData.finalsaplingroothash:' + rpcData.finalsaplingroothash);
@@ -120,7 +82,6 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, pool
12082
console.log('rpcData.previousblockhash: ' + rpcData.previousblockhash);
12183
console.log('rpcData.version: ' + rpcData.version);
12284
*/
123-
12485
if (rpcData.version == 5 && rpcData.finalsaplingroothash) {
12586
header.write(rpcData.finalsaplingroothash, position, 32, 'hex');
12687
position += 32;
@@ -132,44 +93,41 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, pool
13293
}
13394
*/
13495
header.write(nonce, position, 4, 'hex');
135-
header.write(rpcData.bits, position += 4, 4, 'hex');
136-
header.write(nTime, position += 4, 4, 'hex');
137-
header.write(merkleRoot, position += 4, 32, 'hex');
138-
header.write(rpcData.previousblockhash, position += 32, 32, 'hex');
96+
position += 4;
97+
header.write(rpcData.bits, position, 4, 'hex');
98+
position += 4;
99+
header.write(nTime, position, 4, 'hex');
100+
position += 4;
101+
header.write(merkleRoot, position, 32, 'hex');
102+
position += 32;
103+
header.write(rpcData.previousblockhash, position, 32, 'hex');
139104
header.writeUInt32BE(rpcData.version, position + 32);
140-
var header = util.reverseBuffer(header);
141-
return header;
105+
return util.reverseBuffer(header);
142106
};
143107

144-
this.serializeBlock = function (header, coinbase) {
108+
this.serializeBlock = function(header, coinbase) {
145109
return Buffer.concat([
146110
header,
147-
148111
util.varIntBuffer(this.rpcData.transactions.length + 1),
149112
coinbase,
150113
this.transactionData,
151-
152114
getVoteData(),
153-
154115
//POS coins require a zero byte appended to block which the daemon replaces with the signature
155-
new Buffer(reward === 'POS' ? [0] : [])
116+
Buffer.from(reward === 'POS' ? [0] : [])
156117
]);
157118
};
158119

159120
this.registerSubmit = function (extraNonce1, extraNonce2, nTime, nonce) {
160-
var submission = extraNonce1 + extraNonce2 + nTime + nonce;
161-
if (submits.indexOf(submission) === -1) {
121+
const submission = `${extraNonce1}${extraNonce2}${nTime}${nonce}`;
122+
if (!submits.includes(submission)) {
162123
submits.push(submission);
163124
return true;
164125
}
165126
return false;
166127
};
167128

168129
this.getOdoKey = function () {
169-
if (this.rpcData && this.rpcData.odokey !== undefined) {
170-
return this.rpcData.odokey;
171-
}
172-
return null;
130+
return this.rpcData?.odokey || null;
173131
};
174132

175133
this.getJobParams = function () {
@@ -188,13 +146,7 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, pool
188146
if (this.finalsaplingroothashReversed) {
189147
this.jobParams.push(this.finalsaplingroothashReversed);
190148
}
191-
/*if (this.hashstaterootReversed && this.hashutxorootReversed) {
192-
this.jobParams.push(this.hashstaterootReversed);
193-
this.jobParams.push(this.hashutxorootReversed);
194-
this.jobParams.push('0000000000000000000000000000000000000000000000000000000000000000ffffffff00');
195-
}
196-
*/
197149
}
198150
return this.jobParams;
199151
};
200-
};
152+
};

0 commit comments

Comments
 (0)