Fixed raven block processing
This commit is contained in:
25
index.js
25
index.js
@@ -7,7 +7,6 @@ const varuint = require('varuint-bitcoin');
|
|||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const fastMerkleRoot = require('merkle-lib/fastRoot');
|
const fastMerkleRoot = require('merkle-lib/fastRoot');
|
||||||
const promise = require('promise');
|
const promise = require('promise');
|
||||||
const merklebitcoin = promise.denodeify(require('merkle-bitcoin'));
|
|
||||||
|
|
||||||
function scriptCompile(addrHash) {
|
function scriptCompile(addrHash) {
|
||||||
return bitcoin.script.compile([
|
return bitcoin.script.compile([
|
||||||
@@ -25,15 +24,6 @@ function reverseBuffer(buff) {
|
|||||||
return reversed;
|
return reversed;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMerkleRoot(rpcData, generateTxRaw) {
|
|
||||||
hashes = [ reverseBuffer(new Buffer(generateTxRaw, 'hex')).toString('hex') ];
|
|
||||||
rpcData.transactions.forEach(function (value) {
|
|
||||||
hashes.push(value.txid ? value.txid : value.hash);
|
|
||||||
});
|
|
||||||
if (hashes.length === 1) return hashes[0];
|
|
||||||
return Object.values(merklebitcoin(hashes))[2].root;
|
|
||||||
}
|
|
||||||
|
|
||||||
function txesHaveWitnessCommit(transactions) {
|
function txesHaveWitnessCommit(transactions) {
|
||||||
return (
|
return (
|
||||||
transactions instanceof Array &&
|
transactions instanceof Array &&
|
||||||
@@ -47,11 +37,14 @@ function txesHaveWitnessCommit(transactions) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMerkleRoot2(transactions) {
|
function hash256(buffer) {
|
||||||
|
return crypto.createHash('sha256').update(buffer).digest();
|
||||||
|
};
|
||||||
|
|
||||||
|
function getMerkleRoot(transactions) {
|
||||||
if (transactions.length === 0) return null;
|
if (transactions.length === 0) return null;
|
||||||
const forWitness = txesHaveWitnessCommit(transactions);
|
const forWitness = txesHaveWitnessCommit(transactions);
|
||||||
const hashes = transactions.map(transaction => transaction.getHash(forWitness));
|
const hashes = transactions.map(transaction => transaction.getHash(forWitness));
|
||||||
const hash256 = function (buffer) { return crypto.createHash('sha256').update(buffer).digest(); };
|
|
||||||
const rootHash = fastMerkleRoot(hashes, hash256);
|
const rootHash = fastMerkleRoot(hashes, hash256);
|
||||||
return forWitness ? hash256(Buffer.concat([rootHash, transactions[0].ins[0].witness[0]])) : rootHash;
|
return forWitness ? hash256(Buffer.concat([rootHash, transactions[0].ins[0].witness[0]])) : rootHash;
|
||||||
}
|
}
|
||||||
@@ -91,7 +84,7 @@ module.exports.RavenBlockTemplate = function(rpcData, poolAddress) {
|
|||||||
txCoinbase.addOutput(new Buffer(rpcData.default_witness_commitment, 'hex'), 0);
|
txCoinbase.addOutput(new Buffer(rpcData.default_witness_commitment, 'hex'), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const merkleRoot = getMerkleRoot(rpcData, txCoinbase.getHash().toString('hex'));
|
const merkleRoot = new Buffer('DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD', 'hex'); // 32 bytes
|
||||||
|
|
||||||
let header = new Buffer(80);
|
let header = new Buffer(80);
|
||||||
{ let position = 0;
|
{ let position = 0;
|
||||||
@@ -157,15 +150,13 @@ function update_merkle_root_hash(blob_in, blob_out) {
|
|||||||
transactions.push(tx);
|
transactions.push(tx);
|
||||||
offset += tx.byteLength();
|
offset += tx.byteLength();
|
||||||
}
|
}
|
||||||
const rpcData = { transactions: transactions.slice(1) };
|
reverseBuffer(getMerkleRoot(transactions)).copy(blob_out, 4 + 32);
|
||||||
reverseBuffer(new Buffer(getMerkleRoot(rpcData, transactions[0].getHash().toString('hex')), 'hex')).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(blobBuffer, header);
|
||||||
const hash256 = function (buffer) { return crypto.createHash('sha256').update(buffer).digest(); };
|
return reverseBuffer(hash256(hash256(header)));
|
||||||
return reverseBuffer(hash256(crypto.createHash('sha256').update(header).digest()));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.constructNewRavenBlob = function(blockTemplate, nonceBuff, mixhashBuff) {
|
module.exports.constructNewRavenBlob = function(blockTemplate, nonceBuff, mixhashBuff) {
|
||||||
|
|||||||
@@ -18,7 +18,6 @@
|
|||||||
"sha3": "*",
|
"sha3": "*",
|
||||||
"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/bitcoinjs/bitcoinjs-lib.git#533d6c2e6d0aa4111f7948b1c12003cf6ef83137",
|
||||||
"merkle-bitcoin": "git+https://github.com/CloudMining/merkle-bitcoin.git#ec00ae20ba60d444e150ead03c747695ddaa83a1"
|
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"cryptonight",
|
"cryptonight",
|
||||||
|
|||||||
Reference in New Issue
Block a user