From d0953c4196c24100a1b0174611ff564634014573 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 18 Feb 2014 13:35:23 -0700 Subject: [PATCH] Removed binpack as dependency. Possible fix with invalid height in coinbase problem. --- lib/blockTemplate.js | 7 +-- lib/jobManager.js | 5 +- lib/stratum.js | 5 +- lib/transactions.js | 137 ++++++++++++++++++++++++++++++++++++++++++- lib/util.js | 58 ++++++++++++++++-- package.json | 2 - 6 files changed, 194 insertions(+), 20 deletions(-) diff --git a/lib/blockTemplate.js b/lib/blockTemplate.js index 04dbbe0..9f8069f 100644 --- a/lib/blockTemplate.js +++ b/lib/blockTemplate.js @@ -1,4 +1,3 @@ -var binpack = require('binpack'); var bignum = require('bignum'); var merkleTree = require('./merkleTree.js'); @@ -88,7 +87,7 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, publ util.varIntBuffer(this.rpcData.transactions.length + 1), coinbase, this.transactionData, - new Buffer(reward === 'POS' ? [0] : []) + new Buffer(reward === 'POS' ? [0] : []) //POS coins require a zero byte appended to block which the daemon replaces with the signature ]); }; @@ -109,9 +108,9 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, publ this.generationTransaction[0].toString('hex'), this.generationTransaction[1].toString('hex'), this.merkleBranch, - binpack.packInt32(this.rpcData.version, 'big').toString('hex'), + util.packInt32BE(this.rpcData.version).toString('hex'),//binpack.packInt32(this.rpcData.version, 'big').toString('hex'), this.rpcData.bits, - binpack.packUInt32(this.rpcData.curtime, 'big').toString('hex'), + util.packUInt32BE(this.rpcData.curtime).toString('hex'), //binpack.packUInt32(this.rpcData.curtime, 'big').toString('hex'), true ]; } diff --git a/lib/jobManager.js b/lib/jobManager.js index 9d9ff73..4a151e5 100644 --- a/lib/jobManager.js +++ b/lib/jobManager.js @@ -1,6 +1,5 @@ var events = require('events'); -var binpack = require('binpack'); var bignum = require('bignum'); var scrypt = require('scrypt256-hash'); @@ -17,10 +16,10 @@ var blockTemplate = require('./blockTemplate.js'); var ExtraNonceCounter = function(){ var instanceId = 31; var counter = instanceId << 27; - var size = binpack.packUInt32(counter, 'big').length; + var size = util.packUInt32BE(counter).length; //binpack.packUInt32(counter, 'big').length; this.next = function(){ - var extraNonce = binpack.packUInt32(counter++, 'big'); + var extraNonce = util.packUInt32BE(counter++);//binpack.packUInt32(counter++, 'big'); return extraNonce.toString('hex'); }; this.size = function(){ diff --git a/lib/stratum.js b/lib/stratum.js index 524b740..4431f7d 100644 --- a/lib/stratum.js +++ b/lib/stratum.js @@ -1,8 +1,6 @@ var net = require('net'); var events = require('events'); -var binpack = require('binpack'); - var util = require('./util.js'); @@ -13,7 +11,8 @@ var SubscriptionCounter = function(){ next: function(){ count++; if (Number.MAX_VALUE === count) count = 0; - return padding + binpack.packUInt64(count, 'big').toString('hex'); + //return padding + binpack.packUInt64(count, 'big').toString('hex'); + return padding + util.packInt64LE(count).toString('hex'); } }; }; diff --git a/lib/transactions.js b/lib/transactions.js index c3e31be..7c43a91 100644 --- a/lib/transactions.js +++ b/lib/transactions.js @@ -1,6 +1,3 @@ -var binpack = require('binpack'); -var buffertools = require('buffertools'); - var util = require('./util.js'); @@ -129,6 +126,10 @@ For some (probably outdated and incorrect) documentation about whats kinda going see: https://en.bitcoin.it/wiki/Protocol_specification#tx */ + + +//Replaced binpack with native utils +/* exports.CreateGeneration = function(rpcData, publicKey, extraNoncePlaceholder, reward, txMessages){ var txInputsCount = 1; @@ -185,4 +186,134 @@ exports.CreateGeneration = function(rpcData, publicKey, extraNoncePlaceholder, r return [p1, p2]; +}; +*/ + + +//Utilize bufferput +/* +exports.CreateGeneration = function(rpcData, publicKey, extraNoncePlaceholder, reward, txMessages){ + + var txInputsCount = 1; + var txOutputsCount = 1; + var txVersion = txMessages === true ? 2 : 1; + var txLockTime = 0; + + var txInPrevOutHash = 0; + var txInPrevOutIndex = Math.pow(2, 32) - 1; + var txInSequence = 0; + + var txComment = txMessages === true ? + util.serializeString('https://github.com/zone117x/node-stratum') : + new Buffer([]); + + var encodedHeight = new Buffer(4); + encodedHeight.writeUInt32LE(rpcData.height << 8 | 3, 0); + + + var scriptSigPart1 = Buffer.concat([ + encodedHeight, + 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([ + util.packUInt32LE(txVersion),//binpack.packUInt32(txVersion, 'little'), + + //reward === 'POS' ? binpack.packUInt32(rpcData.curtime, 'little') : new Buffer([]), + reward === 'POS' ? util.packUInt32LE(rpcData.curtime) : new Buffer([]), + + util.varIntBuffer(txInputsCount), + + //transaction input + util.uint256BufferFromHash(txInPrevOutHash), + util.packUInt32LE(txInPrevOutIndex), //binpack.packUInt32(txInPrevOutIndex, 'little'), + util.varIntBuffer(scriptSigPart1.length + extraNoncePlaceholder.length + scriptSigPart2.length), + scriptSigPart1 + ]); + + var p2 = Buffer.concat([ + scriptSigPart2, + util.packUInt32LE, //binpack.packUInt32(txInSequence), + //end transaction input + + util.varIntBuffer(txOutputsCount), + + //transaction output + util.packInt64LE(rpcData.coinbasevalue), //binpack.packInt64(rpcData.coinbasevalue, 'little'), + util.varIntBuffer(publicKey.length), + publicKey, + //end transaction ouput + + util.packUInt32LE(txLockTime), //binpack.packUInt32(txLockTime, 'little'), + txComment + ]); + + return [p1, p2]; + +}; +*/ + + +exports.CreateGeneration = function(rpcData, publicKey, extraNoncePlaceholder, reward, txMessages){ + + var txInputsCount = 1; + var txOutputsCount = 1; + var txVersion = txMessages === true ? 2 : 1; + var txLockTime = 0; + + var txInPrevOutHash = 0; + var txInPrevOutIndex = Math.pow(2, 32) - 1; + var txInSequence = 0; + + var txComment = txMessages === true ? + util.serializeString('https://github.com/zone117x/node-stratum') : + new Buffer([]); + + + 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([ + util.packUInt32LE(txVersion), + reward === 'POS' ? util.packUInt32LE(rpcData.curtime) : new Buffer([]), + util.varIntBuffer(txInputsCount), + + //transaction input + util.uint256BufferFromHash(txInPrevOutHash), + util.packUInt32LE(txInPrevOutIndex), + util.varIntBuffer(scriptSigPart1.length + extraNoncePlaceholder.length + scriptSigPart2.length), + scriptSigPart1 + ]); + + var p2 = Buffer.concat([ + scriptSigPart2, + util.packUInt32LE(txInSequence), + //end transaction input + + util.varIntBuffer(txOutputsCount), + + //transaction output + util.packInt64LE(rpcData.coinbasevalue), + util.varIntBuffer(publicKey.length), + publicKey, + //end transaction ouput + + util.packUInt32LE(txLockTime), + txComment + ]); + + return [p1, p2]; + }; \ No newline at end of file diff --git a/lib/util.js b/lib/util.js index f916447..196e747 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,6 +1,5 @@ var crypto = require('crypto'); -var binpack = require('binpack'); var base58 = require('base58-native'); var bignum = require('bignum'); @@ -92,7 +91,7 @@ exports.varIntBuffer = function(n){ else{ var buff = new Buffer(9); buff[0] = 0xff; - binpack.packUInt64(n, 'little').copy(buff, 1); + exports.packUInt16LE(n).copy(buff, 1);//binpack.packUInt64(n, 'little').copy(buff, 1); return buff; } }; @@ -105,6 +104,8 @@ Used to format height and date when putting into script signature: https://en.bitcoin.it/wiki/Script */ exports.serializeNumber = function(n){ + + /* Old version that is bugged if (n < 0xfd){ var buff = new Buffer(2); buff[0] = 0x1; @@ -125,7 +126,20 @@ exports.serializeNumber = function(n){ } else{ return Buffer.concat([new Buffer([0x9]), binpack.packUInt64(n, 'little')]); + }*/ + + //New version from TheSeven + var l = 1; + var buff = new Buffer(9); + while (n > 0xff) + { + buff.writeUInt8(n & 0xff, l++); + n >>= 8; } + buff.writeUInt8(l, 0); + buff.writeUInt8(n, l++); + return buff.slice(0, l); + }; @@ -142,24 +156,58 @@ exports.serializeString = function(s){ else if (s.length < 0x10000) return Buffer.concat([ new Buffer([253]), - binpack.packUInt16(s.length, 'little'), + exports.packUInt16LE(s.length),//binpack.packUInt16(s.length, 'little'), new Buffer(s) ]); else if (s.length < 0x100000000) return Buffer.concat([ new Buffer([254]), - binpack.packUInt32(s.length, 'little'), + exports.packUInt32LE(s.length),//binpack.packUInt32(s.length, 'little'), new Buffer(s) ]); else return Buffer.concat([ new Buffer([255]), - binpack.packUInt64(s.length), + exports.packUInt16LE(s.length),//binpack.packUInt64(s.length), new Buffer(s) ]); }; + +exports.packUInt16LE = function(num){ + var buff = new Buffer(4); + buff.writeUInt16LE(num, 0); + return buff; +}; +exports.packInt32LE = function(num){ + var buff = new Buffer(4); + buff.writeInt32LE(num, 0); + return buff; +}; +exports.packInt32BE = function(num){ + var buff = new Buffer(4); + buff.writeInt32BE(num, 0); + return buff; +}; +exports.packUInt32LE = function(num){ + var buff = new Buffer(4); + buff.writeUInt32LE(num, 0); + return buff; +}; +exports.packUInt32BE = function(num){ + var buff = new Buffer(4); + buff.writeUInt32BE(num, 0); + return buff; +}; +exports.packInt64LE = function(num){ + var buff = new Buffer(8); + buff.writeUInt32LE(num % Math.pow(2, 32), 0); + buff.writeUInt32LE(Math.floor(num / Math.pow(2, 32)), 5); + return buff; +}; + + /* An exact copy of python's range feature. Written by Tadeck: http://stackoverflow.com/a/8273091 diff --git a/package.json b/package.json index de91504..779b9dd 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,7 @@ "scrypt256-hash": "*", "scrypt-jane-hash": "*", "quark-hash": "*", - "binpack": "*", "bignum": "*", - "buffertools": "*", "base58-native": "*", "async": "*" },