From 70c93178c6d99001400de804233414ba07043f55 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 30 Mar 2014 03:09:44 -0600 Subject: [PATCH] Merged all hashing algos into one native addon, hurray! Diffs are dynamically calculated based off values in coin source code. --- diff1.js | 28 ++++++++-- lib/algoProperties.js | 127 +++++++++++++++++++++++++++--------------- lib/jobManager.js | 5 +- lib/pool.js | 4 +- package.json | 9 +-- 5 files changed, 111 insertions(+), 62 deletions(-) diff --git a/diff1.js b/diff1.js index 940088b..3ad9197 100644 --- a/diff1.js +++ b/diff1.js @@ -30,6 +30,12 @@ var bignum = require('bignum'); galleon [keccak] (~uint256(0) >> 20) https://github.com/GalleonBank/galleon/blob/master/src/main.cpp#L51 + cryptometh [keccak] (~uint256(0) >> 24) + https://github.com/cryptometh/cryptometh/blob/master/src/main.cpp#L43 + + 365coin [keccak] (~uint256(0) >> 24) + https://github.com/365-Coin/365coin/blob/master/src/main.cpp#L42 + slothcoin [keccak] (~uint256(0) >> 24) https://github.com/thimod/Slothcoin/blob/master/src/main.cpp#L40 @@ -57,11 +63,17 @@ var bignum = require('bignum'); yacoin [scrypt-jane] (~uint256(0) >> 20) https://github.com/yacoin/yacoin/blob/master/src/main.cpp#L36 + ultracoin [scrypt-jane] (~uint256(0) >> 20) + https://github.com/ziggy909/ultracoin/blob/master/src/main.cpp#L39 + vertcoin [scrypt-n] (~uint256(0) >> 20) https://github.com/vertcoin/vertcoin/blob/master-0.8/src/main.cpp#L35 - inkcoin [shavite] (~uint256(0) >> 20) - https://github.com/inkcoin/inkcoin-project/blob/master/src/main.cpp#L38 + skiencoin [skein] (~uint256(0) >> 20) + https://github.com/skeincoin/skeincoin/blob/master/src/chainparams.cpp#L33 + + nigeriacoin [skein] (~uint256(0) >> 20) + https://github.com/nigeriacoin/nigeriacoin/blob/master/src/chainparams.cpp#L35 bitcoin [sha256d] 0x1d00ffff https://github.com/bitcoin/bitcoin/blob/b8d9058a4d1ce28eefa65aa3339bcc52b3c014e9/src/chainparams.cpp#L136 @@ -112,7 +124,8 @@ function startAsking() { } function askBitshift(){ - rl.question('\nEnter the right bitshift integer, for example with "uint256(0) >> 24", enter in the number 24\nNumber: ', function (answer) { + console.log('\nEnter the right bitshift integer, for example with "uint256(0) >> 24", enter in the number 24'); + rl.question('Number: ', function (answer) { var shiftRight; try { @@ -132,9 +145,10 @@ function askBitshift(){ } function askBitsConvert(){ - rl.question('\n(Note that this will always give truncated results as diff1 precision is ' + + console.log('\n(Note that this will always give truncated results as diff1 precision is ' + 'lost when compacting to nBits. Enter the 8 character nBit hex code, for example ' + - 'with BTC its 0x1d00ffff so enter 1d00ffff\nHex code: ', function (answer) { + 'with BTC its 0x1d00ffff so enter 1d00ffff\n'); + rl.question('Hex code: ', function (answer) { if (answer.length !== 8){ console.log('Must be an 8 character hex string'); @@ -310,6 +324,10 @@ else { shift: 20, nBits: '1f00f0ff' }, + skein: { + shift: 20, + nBits: '1f00f0ff' + }, keccak: { shift: 24, nBits: '1e00ffff' diff --git a/lib/algoProperties.js b/lib/algoProperties.js index a6499e9..11a3eb7 100644 --- a/lib/algoProperties.js +++ b/lib/algoProperties.js @@ -1,81 +1,116 @@ -var scrypt = require('scrypt256-hash'); -var quark = require('quark-hash'); -var scryptJane = require('scrypt-jane-hash'); -var x11 = require('x11-hash'); -var keccak = require('keccak-hash'); -var SHA3 = require('sha3'); +var bignum = require('bignum'); + +var multiHashing = require('multi-hashing'); var util = require('./util.js'); -global.algos = { - 'sha256': { - diff: '00000000ffff0000000000000000000000000000000000000000000000000000', +var maxInt256 = global.maxDiff = bignum('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16); + +var algos = global.algos = { + sha256: { + shift: 32, + multiplier: Math.pow(2, 32), hash: function(){ return util.doublesha.apply(this, arguments); } }, - 'scrypt': { - diff: '0000ffff00000000000000000000000000000000000000000000000000000000', + scrypt: { + shift: 20, + multiplier: Math.pow(2, 16), hash: function(){ - return scrypt.digest.apply(this, arguments); + return multiHashing.scrypt.apply(this, arguments); } }, 'scrypt-jane': { - diff: '0000ffff00000000000000000000000000000000000000000000000000000000', + shift: 20, + multiplier: Math.pow(2, 16), hash: function(){ - return scryptJane.digest.apply(this, arguments); + return multiHashing.scryptjane.apply(this, arguments); } }, - 'x11': { - diff: '0000ffff00000000000000000000000000000000000000000000000000000000', + 'scrypt-n': { + shift: 20, + multiplier: Math.pow(2, 16), hash: function(){ - return x11.digest.apply(this, arguments); + return multiHashing.scryptn.apply(this, arguments); + } + }, + x11: { + shift: 20, + multiplier: 3.1 * Math.pow(2, 16), + hash: function(){ + return multiHashing.x11.apply(this, arguments); } }, quark: { - diff: '000000ffff000000000000000000000000000000000000000000000000000000', + shift: 20, + multipler: Math.pow(2, 16), hash: function(){ - return quark.digest.apply(this, arguments); + return multiHashing.quark.apply(this, arguments); } }, - 'keccak': { - //CBigNum(~uint256(0) >> 24) is nBits so we should try to calculate it.. - //https://github.com/wecoin/wecoin/blob/master/src/main.cpp#L44 - //https://github.com/GalleonBank/galleon/blob/master/src/main.cpp#L51 - diff: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000', - hash: function(headerBuff, nTimeInt){ - var a = new SHA3.SHA3Hash(256); - //a.update(headerBuff.toString('utf8') + nTimeInt.toString(), 'utf8'); - a.update(headerBuff.toString('utf8'), 'utf8'); - var round1 = new Buffer(a.digest('hex'), 'hex'); - return round1.slice(0, 33); - /*var b = new SHA3.SHA3Hash(256); - b.update(round1.toString('utf8'), 'utf8'); - var round2 = new Buffer(b.digest('hex'), 'hex'); - return round2.slice(0, 33);*/ - } - }, - 'skein': { - diff: '00000000ffff0000000000000000000000000000000000000000000000000000', + skein: { + shift: 20, hash: function(){ - + return multiHashing.skein.apply(this, arguments); } }, - 'hefty1': { - diff: '00000000ffff0000000000000000000000000000000000000000000000000000', + bcrypt: { + shift: 11, hash: function(){ - + return multiHashing.bcrypt.apply(this, arguments); } }, - max: { - diff: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000', + keccak: { + shift: 24, + multiplier: Math.pow(2, 8), + hash: function(){ + return multiHashing.bcrypt.apply(this, arguments); + } + }, + blake: { + shift: 24, hash: function(){ } }, fugue: { - diff: '0000ffff00000000000000000000000000000000000000000000000000000000', + shift: 24, + hash: function(){ + + } + }, + shavite: { + shift: 20, + hash: function(){ + + } + }, + hefty1: { + shift: 16, hash: function(){ } } -}; \ No newline at end of file +}; + +function ShiftMax256Right(shiftRight){ + var arr256 = Array.apply(null, new Array(256)).map(Number.prototype.valueOf, 1); + var arrLeft = Array.apply(null, new Array(shiftRight)).map(Number.prototype.valueOf, 0); + var preShift = arrLeft.concat(arr256); + var trimmed = preShift.slice(0, 256); + var octets = []; + for (var i = 0; i < 32; i++){ + octets[i] = 0; + var bits = trimmed.slice(i * 8, i * 8 + 8); + for (var f = 0; f < bits.length; f++){ + var multiplier = Math.pow(2, f); + octets[i] += bits[f] * multiplier; + } + } + var buff = new Buffer(octets); + return buff; +} + +for (var algo in algos){ + algos[algo].diff = ShiftMax256Right(algos[algo].shift); +} \ No newline at end of file diff --git a/lib/jobManager.js b/lib/jobManager.js index 65c8c78..114e0f3 100644 --- a/lib/jobManager.js +++ b/lib/jobManager.js @@ -81,7 +81,7 @@ var JobManager = module.exports = function JobManager(maxDifficulty, hashDigest, /* If block isn't new, lets see if the transactions have updated */ var updatedTransactions = !isNewBlock && - (_this.currentJob.rpcData.transactions.length != rpcData.transactions.length) + (_this.currentJob.rpcData.transactions.length != rpcData.transactions.length); if (updatedTransactions && (Date.now() - lastTransactionUpdateCheck <= options.txRefreshInterval)){ @@ -173,7 +173,7 @@ var JobManager = module.exports = function JobManager(maxDifficulty, hashDigest, var merkleRoot = util.reverseBuffer(job.merkleTree.withFirst(coinbaseHash)).toString('hex'); var headerBuffer = job.serializeHeader(merkleRoot, nTime, nonce); - var headerHash = hashDigest(headerBuffer, nTimeInt); + var headerHash = hashDigest(headerBuffer, nTimeInt, options.coin.chainTime); var headerBigNum = bignum.fromBuffer(headerHash, {endian: 'little', size: 32}); var blockHash; @@ -181,6 +181,7 @@ var JobManager = module.exports = function JobManager(maxDifficulty, hashDigest, if (job.target.ge(headerBigNum)){ + blockHex = job.serializeBlock(headerBuffer, coinbaseBuffer).toString('hex'); blockHash = util.reverseBuffer(util.doublesha(headerBuffer)).toString('hex'); } diff --git a/lib/pool.js b/lib/pool.js index 2dc543f..eb9c69a 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -45,7 +45,7 @@ var pool = module.exports = function pool(options, authorizeFn){ } //Which number to use as dividend when converting difficulty to target - var maxDifficulty = bignum(algos[options.coin.algorithm].diff, 16); + var maxDifficulty = bignum.fromBuffer(algos[options.coin.algorithm].diff); var hashDigest = algos[options.coin.algorithm].hash; @@ -337,7 +337,7 @@ var pool = module.exports = function pool(options, authorizeFn){ var allValid = results.every(function(result){ if (result.error || !result.response){ emitErrorLog('validateaddress rpc error on daemon instance ' + - result.instance.index + ', error +' + JSON.stringify(result.error)); + result.instance.index + ' - ' + JSON.stringify(result.error)); } else if (!result.response.isvalid) emitErrorLog('Daemon instance ' + result.instance.index + diff --git a/package.json b/package.json index e78ede8..6d42b2d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "stratum-pool", - "version": "0.1.3", + "version": "0.1.4", "description": "High performance Stratum poolserver in Node.js", "keywords": [ "stratum", @@ -28,12 +28,7 @@ "url": "https://github.com/zone117x/node-stratum-pool.git" }, "dependencies": { - "scrypt256-hash": "https://github.com/zone117x/node-scrypt256-hash/archive/master.tar.gz", - "scrypt-jane-hash": "https://github.com/zone117x/node-scrypt-jane-hash/archive/master.tar.gz", - "quark-hash": "https://github.com/zone117x/node-quark-hash/archive/master.tar.gz", - "x11-hash": "https://github.com/zone117x/node-x11-hash/archive/master.tar.gz", - "keccak-hash": "https://github.com/zone117x/node-keccak-hash/archive/master.tar.gz", - "sha3": "*", + "multi-hashing": "https://github.com/zone117x/node-multi-hashing/archive/master.tar.gz", "bignum": "*", "base58-native": "*", "async": "*"