Skip to content

Commit

Permalink
Update blockTemplate.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ROZ-MOFUMOFU-ME authored Apr 30, 2024
1 parent 4f05ef8 commit 9469d55
Showing 1 changed file with 35 additions and 83 deletions.
118 changes: 35 additions & 83 deletions lib/blockTemplate.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,45 @@
var bignum = require('bignum');

var merkleTree = 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, poolAddressScript, extraNoncePlaceholder, reward, txMessages, recipients, network) {

//private members

var submits = [];

function getMerkleHashes(steps) {
return steps.map(function (step) {
return step.toString('hex');
});
return steps.map(step => step.toString('hex'));
}

function getTransactionBuffers(txs) {
var txHashes = txs.map(function (tx) {
if (tx.txid !== undefined) {
return util.uint256BufferFromHash(tx.txid);
}
return util.uint256BufferFromHash(tx.hash);
});
return [null].concat(txHashes);
return [null].concat(txs.map(tx => Buffer.from(util.uint256BufferFromHash(tx.txid || tx.hash))));
}

function getVoteData() {
if (!rpcData.masternode_payments) return new Buffer([]);

return Buffer.concat(
[util.varIntBuffer(rpcData.votes.length)].concat(
rpcData.votes.map(function (vt) {
return new Buffer(vt, 'hex');
})
)
);
if (!rpcData.masternode_payments) return Buffer.alloc(0);
return Buffer.concat([
util.varIntBuffer(rpcData.votes.length),
...rpcData.votes.map(vt => Buffer.from(vt, 'hex'))
]);
}

//public members

this.rpcData = rpcData;
this.jobId = jobId;


this.target = rpcData.target ?
bignum(rpcData.target, 16) :
util.bignumFromBitsHex(rpcData.bits);

this.target = rpcData.target ? bignum(rpcData.target, 16) : util.bignumFromBitsHex(rpcData.bits);
this.difficulty = parseFloat((diff1 / this.target.toNumber()).toFixed(9));
this.prevHashReversed = util.reverseByteOrder(Buffer.from(rpcData.previousblockhash, 'hex')).toString('hex');



this.prevHashReversed = util.reverseByteOrder(new Buffer(rpcData.previousblockhash, 'hex')).toString('hex');
if (rpcData.finalsaplingroothash) {
this.finalsaplingroothashReversed = util.reverseByteOrder(new Buffer(rpcData.finalsaplingroothash, 'hex')).toString('hex');
}
if (rpcData.hashstateroot) {
this.hashstaterootReversed = util.reverseBuffer(new Buffer(rpcData.hashstateroot, 'hex')).toString('hex');
this.finalsaplingroothashReversed = util.reverseByteOrder(Buffer.from(rpcData.finalsaplingroothash, 'hex')).toString('hex');
}
if (rpcData.hashutxoroot) {
this.hashutxorootReversed = util.reverseBuffer(new Buffer(rpcData.hashutxoroot, 'hex')).toString('hex');
}
this.transactionData = Buffer.concat(rpcData.transactions.map(function (tx) {
return new Buffer(tx.data, 'hex');
}));

this.transactionData = Buffer.concat(rpcData.transactions.map(tx => Buffer.from(tx.data, 'hex')));
this.merkleTree = new merkleTree(getTransactionBuffers(rpcData.transactions));
this.merkleBranch = getMerkleHashes(this.merkleTree.steps);
this.generationTransaction = transactions.CreateGeneration(
Expand All @@ -83,31 +53,23 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, pool
);

this.serializeCoinbase = function (extraNonce1, extraNonce2) {
const nonce1Buffer = Buffer.isBuffer(extraNonce1) ? extraNonce1 : Buffer.from(extraNonce1);
const nonce2Buffer = Buffer.isBuffer(extraNonce2) ? extraNonce2 : Buffer.from(extraNonce2);

return Buffer.concat([
this.generationTransaction[0],
extraNonce1,
extraNonce2,
nonce1Buffer,
nonce2Buffer,
this.generationTransaction[1]
]);
};


//https://en.bitcoin.it/wiki/Protocol_specification#Block_Headers
this.serializeHeader = function (merkleRoot, nTime, nonce) {

var headerSize;
if (rpcData.version == 5 && rpcData.finalsaplingroothash) {
headerSize = 112;
/*} else if (rpcData.hashstateroot && rpcData.hashutxoroot) {
headerSize = 181;
*/
} else {
headerSize = 80;
}

var header = new Buffer(headerSize);
this.serializeHeader = function(merkleRoot, nTime, nonce) {
var headerSize = rpcData.version == 5 && rpcData.finalsaplingroothash ? 112 : 80;
var header = Buffer.alloc(headerSize);
var position = 0;

/*
console.log('headerSize:' + headerSize);
console.log('rpcData.finalsaplingroothash:' + rpcData.finalsaplingroothash);
Expand All @@ -120,7 +82,6 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, pool
console.log('rpcData.previousblockhash: ' + rpcData.previousblockhash);
console.log('rpcData.version: ' + rpcData.version);
*/

if (rpcData.version == 5 && rpcData.finalsaplingroothash) {
header.write(rpcData.finalsaplingroothash, position, 32, 'hex');
position += 32;
Expand All @@ -132,44 +93,41 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, pool
}
*/
header.write(nonce, position, 4, 'hex');
header.write(rpcData.bits, position += 4, 4, 'hex');
header.write(nTime, position += 4, 4, 'hex');
header.write(merkleRoot, position += 4, 32, 'hex');
header.write(rpcData.previousblockhash, position += 32, 32, 'hex');
position += 4;
header.write(rpcData.bits, position, 4, 'hex');
position += 4;
header.write(nTime, position, 4, 'hex');
position += 4;
header.write(merkleRoot, position, 32, 'hex');
position += 32;
header.write(rpcData.previousblockhash, position, 32, 'hex');
header.writeUInt32BE(rpcData.version, position + 32);
var header = util.reverseBuffer(header);
return header;
return util.reverseBuffer(header);
};

this.serializeBlock = function (header, coinbase) {
this.serializeBlock = function(header, coinbase) {
return Buffer.concat([
header,

util.varIntBuffer(this.rpcData.transactions.length + 1),
coinbase,
this.transactionData,

getVoteData(),

//POS coins require a zero byte appended to block which the daemon replaces with the signature
new Buffer(reward === 'POS' ? [0] : [])
Buffer.from(reward === 'POS' ? [0] : [])
]);
};

this.registerSubmit = function (extraNonce1, extraNonce2, nTime, nonce) {
var submission = extraNonce1 + extraNonce2 + nTime + nonce;
if (submits.indexOf(submission) === -1) {
const submission = `${extraNonce1}${extraNonce2}${nTime}${nonce}`;
if (!submits.includes(submission)) {
submits.push(submission);
return true;
}
return false;
};

this.getOdoKey = function () {
if (this.rpcData && this.rpcData.odokey !== undefined) {
return this.rpcData.odokey;
}
return null;
return this.rpcData?.odokey || null;
};

this.getJobParams = function () {
Expand All @@ -188,13 +146,7 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, pool
if (this.finalsaplingroothashReversed) {
this.jobParams.push(this.finalsaplingroothashReversed);
}
/*if (this.hashstaterootReversed && this.hashutxorootReversed) {
this.jobParams.push(this.hashstaterootReversed);
this.jobParams.push(this.hashutxorootReversed);
this.jobParams.push('0000000000000000000000000000000000000000000000000000000000000000ffffffff00');
}
*/
}
return this.jobParams;
};
};
};

0 comments on commit 9469d55

Please sign in to comment.