GR support

This commit is contained in:
MoneroOcean
2021-11-30 16:12:59 +00:00
parent acc0039b14
commit f4ff9bc8cb
3 changed files with 22 additions and 71 deletions

View File

@@ -87,7 +87,7 @@ module.exports.RavenBlockTemplate = function(rpcData, poolAddress) {
// will be used for our reserved_offset extra_nonce // will be used for our reserved_offset extra_nonce
Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'), Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'),
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
Buffer.concat([serializedBlockHeight, Buffer('CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC', 'hex')]) // 17 bytes Buffer.concat([serializedBlockHeight, Buffer.alloc(17, 0xCC)]) // 17 bytes
); );
txCoinbase.addOutput(scriptCompile(poolAddrHash), Math.floor(rpcData.coinbasevalue)); txCoinbase.addOutput(scriptCompile(poolAddrHash), Math.floor(rpcData.coinbasevalue));
@@ -152,27 +152,28 @@ module.exports.RavenBlockTemplate = function(rpcData, poolAddress) {
}; };
}; };
function update_merkle_root_hash(blob_in, blob_out) { function update_merkle_root_hash(offset, payload, blob_in, blob_out) {
let offset = 80 + 8 + 32;
const nTransactions = varuint.decode(blob_in, offset); const nTransactions = varuint.decode(blob_in, offset);
console.log("2x" + nTransactions);
offset += varuint.decode.bytes; offset += varuint.decode.bytes;
let transactions = []; let transactions = [];
for (let i = 0; i < nTransactions; ++i) { for (let i = 0; i < nTransactions; ++i) {
const tx = bitcoin.Transaction.fromBuffer(blob_in.slice(offset), true); const tx = bitcoin.Transaction.fromBuffer(blob_in.slice(offset), true, payload && i == 0);
transactions.push(tx); transactions.push(tx);
offset += tx.byteLength(); offset += tx.byteLength();
console.log("2xx " + tx.byteLength());
} }
getMerkleRoot(transactions).copy(blob_out, 4 + 32); getMerkleRoot(transactions).copy(blob_out, 4 + 32);
}; };
module.exports.convertRavenBlob = function(blobBuffer) { module.exports.convertRavenBlob = function(blobBuffer) {
let header = blobBuffer.slice(0, 80); let header = blobBuffer.slice(0, 80);
update_merkle_root_hash(blobBuffer, header); update_merkle_root_hash(80 + 8 + 32, false, blobBuffer, header);
return reverseBuffer(hash256(header)); return reverseBuffer(hash256(header));
}; };
module.exports.constructNewRavenBlob = function(blockTemplate, nonceBuff, mixhashBuff) { module.exports.constructNewRavenBlob = function(blockTemplate, nonceBuff, mixhashBuff) {
update_merkle_root_hash(blockTemplate, blockTemplate); update_merkle_root_hash(80 + 8 + 32, false, blockTemplate, blockTemplate);
nonceBuff.copy (blockTemplate, 80, 0, 8); nonceBuff.copy (blockTemplate, 80, 0, 8);
mixhashBuff.copy(blockTemplate, 88, 0, 32); mixhashBuff.copy(blockTemplate, 88, 0, 32);
return blockTemplate; return blockTemplate;
@@ -207,10 +208,14 @@ module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
return rtm.RtmBlockTemplate(rpcData, poolAddress); return rtm.RtmBlockTemplate(rpcData, poolAddress);
}; };
module.exports.convertRtmBlob = function(buffer) { module.exports.convertRtmBlob = function(blobBuffer) {
return rtm.convertRtmBlob(buffer); let header = blobBuffer.slice(0, 80);
update_merkle_root_hash(80, true, blobBuffer, header);
return header;
}; };
module.exports.constructNewRtmBlob = function(blockTemplate, nonceBuff) { module.exports.constructNewRtmBlob = function(blockTemplate, nonceBuff) {
return rtm.constructNewRtmBlob(blockTemplate, nonceBuff); update_merkle_root_hash(80, true, blockTemplate, blockTemplate);
nonceBuff.copy(blockTemplate, 76, 0, 4);
return blockTemplate;
}; };

View File

@@ -18,7 +18,7 @@
"sha3": "*", "sha3": "*",
"base58-native": "*", "base58-native": "*",
"varuint-bitcoin": "^1.0.4", "varuint-bitcoin": "^1.0.4",
"bitcoinjs-lib": "git+https://github.com/bitcoinjs/bitcoinjs-lib.git#533d6c2e6d0aa4111f7948b1c12003cf6ef83137" "bitcoinjs-lib": "git+https://github.com/MoneroOcean/bitcoinjs-lib.git"
}, },
"keywords": [ "keywords": [
"cryptonight", "cryptonight",

68
rtm.js
View File

@@ -1,4 +1,3 @@
const crypto = require('crypto');
const bignum = require('bignum'); const bignum = require('bignum');
const base58 = require('base58-native'); const base58 = require('base58-native');
@@ -106,16 +105,6 @@ function serializeString(s) {
} }
} }
function sha256(buffer) {
let hash1 = crypto.createHash('sha256');
hash1.update(buffer);
return hash1.digest();
}
function sha256d(buffer) {
return sha256(sha256(buffer));
}
// An exact copy of python's range feature. Written by Tadeck: // An exact copy of python's range feature. Written by Tadeck:
// http://stackoverflow.com/a/8273091 // http://stackoverflow.com/a/8273091
function range(start, stop, step) { function range(start, stop, step) {
@@ -136,36 +125,6 @@ function range(start, stop, step) {
return result; return result;
} }
function merkleJoin(h1, h2) {
let joined = Buffer.concat([h1, h2]);
let dhashed = sha256d(joined);
return dhashed;
}
function merkleTreeBranches(data) {
let L = data;
let steps = [];
let PreL = [null];
let StartL = 2;
let Ll = L.length;
if (Ll > 1) {
while (true) {
if (Ll === 1) break;
steps.push(L[1]);
if (Ll % 2) L.push(L[L.length - 1]);
let Ld = [];
let r = range(StartL, Ll, 2);
r.forEach(function(i) {
Ld.push(merkleJoin(L[i], L[i + 1]));
});
L = PreL.concat(Ld);
Ll = L.length;
}
}
return steps;
}
function uint256BufferFromHash(hex) { function uint256BufferFromHash(hex) {
let fromHex = Buffer.from(hex, 'hex'); let fromHex = Buffer.from(hex, 'hex');
if (fromHex.length != 32) { if (fromHex.length != 32) {
@@ -255,7 +214,7 @@ function generateOutputTransactions(rpcData, poolAddress) {
} }
module.exports.RtmBlockTemplate = function(rpcData, poolAddress) { module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
const extraNoncePlaceholderLength = 16; const extraNoncePlaceholderLength = 17;
const coinbaseVersion = Buffer.concat([packUInt16LE(3), packUInt16LE(5)]); const coinbaseVersion = Buffer.concat([packUInt16LE(3), packUInt16LE(5)]);
const scriptSigPart1 = Buffer.concat([ const scriptSigPart1 = Buffer.concat([
@@ -294,29 +253,16 @@ module.exports.RtmBlockTemplate = function(rpcData, poolAddress) {
let bits = Buffer.from(rpcData.bits, 'hex'); let bits = Buffer.from(rpcData.bits, 'hex');
bits.writeUInt32LE(bits.readUInt32BE()); bits.writeUInt32LE(bits.readUInt32BE());
console.log("1x" + (rpcData.transactions.length + 1) + " " + (blob1.length + extraNoncePlaceholderLength + blob2.length));
return { return {
difficulty: parseFloat((diff1 / bignum(rpcData.target, 16).toNumber()).toFixed(9)), difficulty: parseFloat((diff1 / bignum(rpcData.target, 16).toNumber()).toFixed(9)),
height: rpcData.height, height: rpcData.height,
prev_hash: rpcData.previousblockhash, prev_hash: rpcData.previousblockhash,
blocktemplate_blob: version + rpcData.previousblockhash + Buffer.alloc(32, 0).toString('hex') + curtime + bits.toString('hex') + Buffer.alloc(4, 0).toString('hex') + blocktemplate_blob: version + rpcData.previousblockhash + Buffer.alloc(32, 0).toString('hex') + curtime + bits.toString('hex') + Buffer.alloc(4, 0).toString('hex') +
packUInt32LE(blob1.length + extraNoncePlaceholderLength + blob2.length).toString('hex') + varIntBuffer(rpcData.transactions.length + 1).toString('hex') +
blob1.toString('hex') + Buffer.alloc(extraNoncePlaceholderLength, 0).toString('hex') + blob2.toString('hex') + blob1.toString('hex') + Buffer.alloc(extraNoncePlaceholderLength, 0xCC).toString('hex') + blob2.toString('hex') +
Buffer.concat(merkleTreeBranches(getTransactionBuffers(rpcData.transactions))).toString('hex'), Buffer.concat(rpcData.transactions.map(function(tx) { return Buffer.from(tx.data, 'hex'); })).toString('hex'),
reserved_offset: 80 + 4 + blob1.length reserved_offset: 80 + 4 + 1 + blob1.length
} }
} }
module.exports.convertRtmBlob = function(buffer) {
let header = buffer.slice(0, 80);
const blobSize = buffer.slice(80, 80 + 4).readUInt32LE();
let merkle_root = sha256d(buffer.slice(84, 84 + blobSize));
for (let i = 84 + blobSize; i < buffer.length; i += 32)
merkle_root = merkleJoin(merkle_root, buffer.slice(i, i + 32));
merkle_root.copy(header, 4 + 32, 0, 32);
return header;
}
module.exports.constructNewRtmBlob = function(blockTemplate, nonceBuff) {
nonceBuff.copy(blockTemplate, 76, 0, 4);
return blockTemplate;
}