Optimized generation transaction building. Should be a bit faster.

This commit is contained in:
Matthew Little 2014-01-15 16:49:22 -07:00
parent cb5d7c7bdc
commit 11338e0f16
2 changed files with 75 additions and 6 deletions

View File

@ -39,7 +39,7 @@ var BlockTemplate = module.exports = function BlockTemplate(rpcData, publicKey,
}));
this.merkleTree = new merkleTree(getTransactionBuffers(rpcData.transactions));
this.merkleBranch = getMerkleHashes(this.merkleTree.steps);
this.generationTransaction = new transactions.Generation(
this.generationTransaction = transactions.CreateGeneration(//new transactions.Generation(
rpcData,
publicKey,
extraNoncePlaceholder
@ -51,10 +51,10 @@ var BlockTemplate = module.exports = function BlockTemplate(rpcData, publicKey,
this.serializeCoinbase = function(extraNonce1, extraNonce2){
return Buffer.concat([
this.generationTransaction.coinbase[0],
this.generationTransaction[0],
extraNonce1,
extraNonce2,
this.generationTransaction.coinbase[1]
this.generationTransaction[1]
]);
};
@ -96,8 +96,8 @@ var BlockTemplate = module.exports = function BlockTemplate(rpcData, publicKey,
this.jobParams = [
this.jobId,
this.prevHashReversed,
this.generationTransaction.coinbase[0].toString('hex'),
this.generationTransaction.coinbase[1].toString('hex'),
this.generationTransaction[0].toString('hex'),
this.generationTransaction[1].toString('hex'),
this.merkleBranch,
binpack.packInt32(this.rpcData.version, 'big').toString('hex'),
this.rpcData.bits,

View File

@ -4,6 +4,7 @@ var buffertools = require('buffertools');
var util = require('./util.js');
/*
function Transaction(params){
var version = params.version || 1,
@ -38,6 +39,7 @@ function TransactionInput(params){
this.toBuffer = function(){
sigScriptBuffer = sigScript.toBuffer();
console.log('scriptSig length ' + sigScriptBuffer.length);
return Buffer.concat([
util.uint256BufferFromHash(prevOutHash),
binpack.packUInt32(prevOutIndex, 'little'),
@ -79,9 +81,9 @@ function ScriptSig(params){
util.serializeString('/nodeStratum/')
]);
}
};
var Generation = exports.Generation = function Generation(rpcData, publicKey, extraNoncePlaceholder){
var tx = new Transaction({
@ -107,4 +109,71 @@ var Generation = exports.Generation = function Generation(rpcData, publicKey, ex
this.transaction = tx;
this.coinbase = [p1, p2];
};
*/
/*
^^^^ The above code was a bit slow. The below code is uglier but optimized.
*/
/*
Creating this function required tons of trial and error and reversing existing
pool server code. I went to write a good comment describing how it works in detail but
at this point I don't even know..
For some (probably outdated and incorrect) documentation about whats kinda going on here,
see: https://en.bitcoin.it/wiki/Protocol_specification#tx
*/
exports.CreateGeneration = function(rpcData, publicKey, extraNoncePlaceholder){
var txVersion = 1;
var txLockTime = 0;
var txInPrevOutHash = 0;
var txInPrevOutIndex = Math.pow(2, 32) - 1;
var txInSequence = 0;
var scriptSigPart1 = Buffer.concat([
util.serializeNumber(rpcData.height),
new Buffer(rpcData.coinbaseaux.flags, 'hex'),
util.serializeNumber(Date.now() / 1000 | 0),
new Buffer([extraNoncePlaceholder.length])
]);
var scriptSigPart2 = util.serializeString('/nodeStratum/');
var p1 = Buffer.concat([
binpack.packUInt32(txVersion, 'little'),
util.varIntBuffer(1),
//transaction input
util.uint256BufferFromHash(txInPrevOutHash),
binpack.packUInt32(txInPrevOutIndex, 'little'),
util.varIntBuffer(scriptSigPart1.length + extraNoncePlaceholder.length + scriptSigPart2.length),
scriptSigPart1
]);
var p2 = Buffer.concat([
scriptSigPart2,
binpack.packUInt32(txInSequence),
//end transaction input
util.varIntBuffer(1),
//transaction output
binpack.packInt64(rpcData.coinbasevalue, 'little'),
util.varIntBuffer(publicKey.length),
publicKey,
//end transaction ouput
binpack.packUInt32(txLockTime, 'little')
]);
return [p1, p2];
};