Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster calculation of J in share verification. #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions lib/jobManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ const ExtraNonceCounter = function (configInstanceId, extraNonce1Size) {

function genIndexes(seed, height) {
const hash = blake2b(seed);
const extendedHash = new Uint8Array(hash.length * 2);
extendedHash.__proto__ = hash.__proto__;
// Buffer alloc is faster then using the UInt8Array and changing out protos.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto
const extendedHash = Buffer.alloc(hash.length * 2);
// const extendedHash = new Uint8Array(hash.length * 2);
// extendedHash.__proto__ = hash.__proto__;
extendedHash.set(hash);
extendedHash.set(hash, hash.length);
return Array.from({length: 32}).map((_, index) => extendedHash.readUIntBE(index, 4) % parseInt(N(height)))
Expand Down Expand Up @@ -165,6 +168,13 @@ const JobManager = module.exports = function JobManager(options) {
};

this.processShare = function (jobId, previousDifficulty, difficulty, extraNonce1, extraNonce2, nTime, nonce, ipAddress, port, workerName) {

console.log({
jobId,
extraNonce2: [extraNonce2, extraNonce2.length],
nonce: [nonce, nonce.length]
})

const shareError = function (error) {
_this.emit('share', {
job: jobId,
Expand All @@ -175,9 +185,8 @@ const JobManager = module.exports = function JobManager(options) {
});
return {error: error, result: null};
};

if (extraNonce2.length / 2 !== _this.extraNonce2Size)
return shareError([20, 'incorrect size of extra nonce2']);
return shareError([20, `incorrect size of extra nonce2 ${extraNonce2.length} vs ${_this.extraNonce2Size}`]);

const job = this.validJobs[jobId];

Expand All @@ -200,6 +209,7 @@ const JobManager = module.exports = function JobManager(options) {

const coinbaseBuffer = job.serializeCoinbase(extraNonce1Buffer, extraNonce2Buffer);
const h = BigIntBuffer.toBufferBE(BigInt(job.rpcData.height), 4);

const i = BigIntBuffer.toBufferBE(BigIntBuffer.toBigIntBE(blake2b(coinbaseBuffer).slice(24, 32)) % N(h), 4);
const e = blake2b(Buffer.concat([
i,
Expand All @@ -212,15 +222,19 @@ const JobManager = module.exports = function JobManager(options) {
Buffer.concat([item, h, M])
).slice(1, 32)
)).reduce((a, b) => a + b);

console.log("f", f)
const fh = BigIntBuffer.toBigIntBE(blake2b(BigIntBuffer.toBufferBE(f, 32)));
console.log("calculated FH", fh)
let blockHash = "";

//Check if share is a block candidate (matched network difficulty)
if (job.rpcData.b.gte(fh)) {
// must submit solution
blockHash = BigIntBuffer.toBufferBE(f, 32)
} else {
//Check if share didn't reached the miner's difficulty)

//console.log((new BigNumber(job.jobParams[6])).multipliedBy(difficulty).toString(), (fh))
if ((new BigNumber(job.jobParams[6])).multipliedBy(difficulty).lte(fh)) {
return shareError([23, 'Low Difficulty share']);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ var pool = module.exports = function pool(options, authorizeFn){
client.socket.localPort,
params.name
);

resultCallback(result.error, result.result ? true : null);

}).on('malformedMessage', function (message) {
Expand Down
6 changes: 5 additions & 1 deletion lib/stratum.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ var StratumClient = function(options){
return;
}
_this.extraNonce1 = extraNonce1;

sendJson({
id: message.id,
result: [
Expand Down Expand Up @@ -157,6 +158,8 @@ var StratumClient = function(options){
considerBan(false);
return;
}


_this.emit('submit',
{
name : message.params[0],
Expand Down Expand Up @@ -277,7 +280,7 @@ var StratumClient = function(options){
};

this.sendMiningJob = function(jobParams){

console.log("SENDING MINING JOB")
var lastActivityAgo = Date.now() - _this.lastActivity;
if (lastActivityAgo > options.connectionTimeout * 1000){
_this.emit('socketTimeout', 'last submitted a share was ' + (lastActivityAgo / 1000 | 0) + ' seconds ago');
Expand All @@ -293,6 +296,7 @@ var StratumClient = function(options){
}
}
var params = [...jobParams]
console.log(params)
if(typeof params[6] !== 'string') {
if (options.multiplyDifficulty) {
var b = params[6].multipliedBy(this.difficulty);
Expand Down
10 changes: 5 additions & 5 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var pool = Stratum.createPool({
"coin": myCoin,
"extraNonce1Size": 1,

"address": "9fs2EgK8MD8ALEEjsE9oVFjexZdYFQoQWxBTPnxCdsU6ubxVLqX", //Address to where block rewards are given
"address": "9iLrHXnsEMjN3r6CFAWRAquPB9BRbSys33zvZ79CEkDZYjaTzFm", //Address to where block rewards are given

/* Block rewards go to the configured pool wallet address to later be paid out to miners,
except for a percentage that can go to, for examples, pool operator(s) as pool fees or
Expand Down Expand Up @@ -86,15 +86,15 @@ var pool = Stratum.createPool({
/* Variable difficulty is a feature that will automatically adjust difficulty for
individual miners based on their hashrate in order to lower networking overhead */
"varDiff": {
"minDiff": 8, //Minimum difficulty
"maxDiff": 16431986528747520, //Network difficulty will be used if it is lower than this
"minDiff": 164319, //Minimum difficulty
"maxDiff": 1643198652874752, //Network difficulty will be used if it is lower than this
"targetTime": 15, //Try to get 1 share per this many seconds
"retargetTime": 10, //Check to see if we should retarget every this many seconds
"variancePercent": 30 //Allow time to very this % from target without retargeting
}
},
"3256": { //Another port for your miners to connect to, this port does not use varDiff
"diff": 256 //The pool difficulty
"diff": 12800000 //The pool difficulty
}
},

Expand All @@ -107,7 +107,7 @@ var pool = Stratum.createPool({
- https://en.bitcoin.it/wiki/Running_bitcoind */
"daemons": [
{ //Main daemon instance
"host": "213.239.193.208",
"host": "143.198.188.87",
"port": 9053,
"user": "litecoinrpc",
"password": "testnet"
Expand Down