From c1c5d236cbf661b32775ae70bbb3634b9ae7692c Mon Sep 17 00:00:00 2001 From: KorkyMonster Date: Fri, 24 Aug 2018 16:50:09 -0500 Subject: [PATCH 01/12] Added basic work submission --- package.json | 4 +++ src/example.js | 20 +++++++++++++ src/index.js | 31 ++++++++++++------- src/index.js.save | 68 ++++++++++++++++++++++++++++++++++++++++++ src/messageContants.js | 3 ++ src/processData.js | 15 ++++++++-- src/submitWork.js | 17 +++++++++++ 7 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 src/example.js create mode 100644 src/index.js.save create mode 100644 src/submitWork.js diff --git a/package.json b/package.json index a4fc5e5..a484d12 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,10 @@ "mining" ], "author": "Arnab Karmakar", + "contributors": { + "name" : "KorkyMonster" + , "url" : "http://github.com/KorkyMonster" + }, "license": "MIT", "bugs": { "url": "https://github.com/arnabk/stratum-client/issues" diff --git a/src/example.js b/src/example.js new file mode 100644 index 0000000..4e69d88 --- /dev/null +++ b/src/example.js @@ -0,0 +1,20 @@ + +const client = require('./index.js'); +const Client = client({ + server: "grlcgang.com", + port: 3333, + worker: "KorkyMonster.testing", + password: "x", + autoReconnectOnError: true, + onConnect: () => console.log('Connected to server'), + onClose: () => console.log('Connection closed'), + onError: (error) => console.log('Error', error.message), + onAuthorizeSuccess: () => Client.submit(), + onAuthorizeFail: () => console.log('WORKER FAILED TO AUTHORIZE OH NOOOOOO'), + onNewDifficulty: (newDiff) => console.log('New difficulty', newDiff), + onSubscribe: (subscribeData) => console.log('[Subscribe]', subscribeData), + onNewMiningWork: (newWork) => console.log('[New Work]', newWork), + onSubmitWorkSuccess: (error, result) => console.log("Yay! I submitted work successfully!"), + onSubmitWorkFail: (error, result) => console.log("Aww! Error from pool was: " + error), +}); + diff --git a/src/index.js b/src/index.js index cebca19..2f5611c 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ const net = require('net'); const extend = require('lodash/extend'); const connect = require('./connect'); +const submitWork = require('./submitWork'); const onData = require('./onData'); const onError = require('./onError'); const validateConfig = require('./validateConfig'); @@ -12,10 +13,13 @@ const defaultConfig = { }; class Client { - + submit(text) { + submitWork(this.client, { worker_name: "WORKERNAME", job_id: "JOBID", extranonce2: "00000000", ntime: "NTIME", nonce: "NONCE"}); + } start(options) { - const client = new net.Socket(); - client.setEncoding('utf8'); + this.client = new net.Socket(); + + this.client.setEncoding('utf8'); const updatedOptions = extend({}, defaultConfig, options); @@ -23,13 +27,13 @@ class Client { const workObject = new WorkObject(); - connect(client, updatedOptions); + connect(this.client, updatedOptions); - client.on('data', data => onData(client, updatedOptions, data, workObject)); + this.client.on('data', data => onData(this.client, updatedOptions, data, workObject)); - client.on('error', error => onError(client, updatedOptions, error)); + this.client.on('error', error => onError(this.client, updatedOptions, error)); - client.on('close', () => { + this.client.on('close', () => { if (updatedOptions.onClose) updatedOptions.onClose(); /* For some reason, corrupted data keeps streaming. This is a hack. @@ -42,17 +46,22 @@ class Client { onError: null, onAuthorize: null, onAuthorizeSuccess: null, - onAuthorizeFail: null, + onAuthorizeFail: null, onNewDifficulty: null, onSubscribe: null, onNewMiningWork: null, + onSubmitWorkSuccess: null, + onSubmitWorkFail: null, }); }); - + return { + submit: (text) => { + this.submit(text); + }, shutdown: () => { - client.end(); - client.destroy(); + this.client.end(); + this.client.destroy(); }, }; } diff --git a/src/index.js.save b/src/index.js.save new file mode 100644 index 0000000..9823e3f --- /dev/null +++ b/src/index.js.save @@ -0,0 +1,68 @@ +// stratum+tcp://stratum.antpool.com +const net = require('net'); +const extend = require('lodash/extend'); +const connect = require('./connect'); +var submit +const onData = require('./onData'); +const onError = require('./onError'); +const validateConfig = require('./validateConfig'); +const WorkObject = require('./workObject'); + +const defaultConfig = { + "autoReconnectOnError": true +}; + +class Client { + submit(text) { + submit + } + start(options) { + const client = new net.Socket(); + client.setEncoding('utf8'); + + const updatedOptions = extend({}, defaultConfig, options); + + validateConfig(updatedOptions); + + const workObject = new WorkObject(); + + connect(client, updatedOptions); + + client.on('data', data => onData(client, updatedOptions, data, workObject)); + + client.on('error', error => onError(client, updatedOptions, error)); + + client.on('close', () => { + if (updatedOptions.onClose) updatedOptions.onClose(); + /* + For some reason, corrupted data keeps streaming. This is a hack. + With this hack, I am ensuring that no more callbacks are called + after closing the connection (closing from our end) + */ + extend(updatedOptions, { + onConnect: null, + onClose: null, + onError: null, + onAuthorize: null, + onAuthorizeSuccess: null, + onAuthorizeFail: null, + onNewDifficulty: null, + onSubscribe: null, + onNewMiningWork: null, + }); + }); + + return { + submit: (text) => { + this.submit(text); + }, + shutdown: () => { + client.end(); + client.destroy(); + }, + }; + } + +}; + +module.exports = (options) => new Client().start(options); diff --git a/src/messageContants.js b/src/messageContants.js index f4a6a54..147335b 100644 --- a/src/messageContants.js +++ b/src/messageContants.js @@ -10,4 +10,7 @@ module.exports = { miningNotify: "mining.notify", + submitMethod: "mining.submit", + submitWork: '{"params": ["", "", "", "", ""], "id": "mining.submit", "method": "mining.submit"}\n', + }; diff --git a/src/processData.js b/src/processData.js index 2f0a35a..b854d8f 100644 --- a/src/processData.js +++ b/src/processData.js @@ -7,6 +7,7 @@ const { subscribeMethod, miningDifficulty, miningNotify, + submitMethod, } = require('./messageContants'); module.exports = (client, updatedOptions, jsonData, workObject) => { @@ -14,7 +15,7 @@ module.exports = (client, updatedOptions, jsonData, workObject) => { const { error, result, - params + params, } = jsonData; const { onAuthorize, @@ -24,8 +25,11 @@ module.exports = (client, updatedOptions, jsonData, workObject) => { onNewDifficulty, worker, password, - onNewMiningWork + onNewMiningWork, + onSubmitWorkSuccess, + onSubmitWorkFail, } = updatedOptions; +console.log(jsonData); switch (key) { case authorizeMethod: { @@ -70,6 +74,13 @@ module.exports = (client, updatedOptions, jsonData, workObject) => { if (onNewMiningWork) onNewMiningWork(cloneDeep(workObject)); } break; + case submitMethod: + { + const fnSuccess = onSubmitWorkSuccess; + const fnFailure = onSubmitWorkFail; + if (result) fnSuccess(error, result); + else fnFailure(error, result); + } default: break; } diff --git a/src/submitWork.js b/src/submitWork.js new file mode 100644 index 0000000..4eb24b3 --- /dev/null +++ b/src/submitWork.js @@ -0,0 +1,17 @@ +const { submitWork } = require('./messageContants'); + +module.exports = (client, data) => { + console.log(submitWork.replace("", data.worker_name).replace("", data.job_id).replace("", data.extranonce2).replace("", data.ntime ).replace("", data.nonce)); + client.write(submitWork.replace("", data.worker_name).replace("", data.job_id).replace("", data.extranonce2).replace("", data.ntime ).replace("", data.nonce)); +}; + + + + + + + + + + + From 389fd8149933dd21191da39804f5110d73794c32 Mon Sep 17 00:00:00 2001 From: KorkyMonster Date: Fri, 24 Aug 2018 18:18:52 -0500 Subject: [PATCH 02/12] Cleaned up files --- src/example.js | 20 -------------- src/index.js.save | 68 ----------------------------------------------- 2 files changed, 88 deletions(-) delete mode 100644 src/example.js delete mode 100644 src/index.js.save diff --git a/src/example.js b/src/example.js deleted file mode 100644 index 4e69d88..0000000 --- a/src/example.js +++ /dev/null @@ -1,20 +0,0 @@ - -const client = require('./index.js'); -const Client = client({ - server: "grlcgang.com", - port: 3333, - worker: "KorkyMonster.testing", - password: "x", - autoReconnectOnError: true, - onConnect: () => console.log('Connected to server'), - onClose: () => console.log('Connection closed'), - onError: (error) => console.log('Error', error.message), - onAuthorizeSuccess: () => Client.submit(), - onAuthorizeFail: () => console.log('WORKER FAILED TO AUTHORIZE OH NOOOOOO'), - onNewDifficulty: (newDiff) => console.log('New difficulty', newDiff), - onSubscribe: (subscribeData) => console.log('[Subscribe]', subscribeData), - onNewMiningWork: (newWork) => console.log('[New Work]', newWork), - onSubmitWorkSuccess: (error, result) => console.log("Yay! I submitted work successfully!"), - onSubmitWorkFail: (error, result) => console.log("Aww! Error from pool was: " + error), -}); - diff --git a/src/index.js.save b/src/index.js.save deleted file mode 100644 index 9823e3f..0000000 --- a/src/index.js.save +++ /dev/null @@ -1,68 +0,0 @@ -// stratum+tcp://stratum.antpool.com -const net = require('net'); -const extend = require('lodash/extend'); -const connect = require('./connect'); -var submit -const onData = require('./onData'); -const onError = require('./onError'); -const validateConfig = require('./validateConfig'); -const WorkObject = require('./workObject'); - -const defaultConfig = { - "autoReconnectOnError": true -}; - -class Client { - submit(text) { - submit - } - start(options) { - const client = new net.Socket(); - client.setEncoding('utf8'); - - const updatedOptions = extend({}, defaultConfig, options); - - validateConfig(updatedOptions); - - const workObject = new WorkObject(); - - connect(client, updatedOptions); - - client.on('data', data => onData(client, updatedOptions, data, workObject)); - - client.on('error', error => onError(client, updatedOptions, error)); - - client.on('close', () => { - if (updatedOptions.onClose) updatedOptions.onClose(); - /* - For some reason, corrupted data keeps streaming. This is a hack. - With this hack, I am ensuring that no more callbacks are called - after closing the connection (closing from our end) - */ - extend(updatedOptions, { - onConnect: null, - onClose: null, - onError: null, - onAuthorize: null, - onAuthorizeSuccess: null, - onAuthorizeFail: null, - onNewDifficulty: null, - onSubscribe: null, - onNewMiningWork: null, - }); - }); - - return { - submit: (text) => { - this.submit(text); - }, - shutdown: () => { - client.end(); - client.destroy(); - }, - }; - } - -}; - -module.exports = (options) => new Client().start(options); From 7a7e4a0906f698db8018fe62c0dee8837ec9bcc2 Mon Sep 17 00:00:00 2001 From: KorkyMonster Date: Sun, 2 Sep 2018 20:27:32 -0500 Subject: [PATCH 03/12] Improved work submission --- example.js | 20 ++++++++++++ src/index.js | 31 ++++++++++--------- src/index.js.save | 76 ++++++++++++++++++++++++++++++++++++++++++++++ src/processData.js | 4 +-- src/submitWork.js | 6 ++-- 5 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 example.js create mode 100644 src/index.js.save diff --git a/example.js b/example.js new file mode 100644 index 0000000..9b206fe --- /dev/null +++ b/example.js @@ -0,0 +1,20 @@ +const client = require('./index.js'); +const Client = client({ + server: "grlcgang.com", + port: 3333, + worker: "KorkyMonster.testing", + password: "x", + autoReconnectOnError: true, + onConnect: () => console.log('Connected to server'), + onClose: () => console.log('Connection closed'), + onError: (error) => console.log('Error', error.message), + onAuthorizeSuccess: () => Client.submit("KorkyMonster.testing", "cb06", "00000000", "gfgdfgd", "fdsfdfdsf"), + onAuthorizeFail: () => console.log('WORKER FAILED TO AUTHORIZE OH NOOOOOO'), + onNewDifficulty: (newDiff) => console.log('New difficulty', newDiff), + onSubscribe: (subscribeData) => console.log('[Subscribe]', subscribeData), + onNewMiningWork: (newWork) => console.log('[New Work]', newWork), + onSubmitWorkSuccess: (error, result) => console.log("Yay! Our work was accepted!"), + onSubmitWorkFail: (error, result) => console.log("Oh no! Our work was refused because: " + error), +}); + + diff --git a/src/index.js b/src/index.js index 2f5611c..1ed8cb5 100644 --- a/src/index.js +++ b/src/index.js @@ -13,13 +13,15 @@ const defaultConfig = { }; class Client { - submit(text) { - submitWork(this.client, { worker_name: "WORKERNAME", job_id: "JOBID", extranonce2: "00000000", ntime: "NTIME", nonce: "NONCE"}); - } +submit() { +var args = Array.prototype.slice.call(arguments); +args.unshift(this.client); // Make real array from arguments +submitWork.apply(this, args); +} start(options) { - this.client = new net.Socket(); - - this.client.setEncoding('utf8'); + const client = new net.Socket(); + + client.setEncoding('utf8'); const updatedOptions = extend({}, defaultConfig, options); @@ -27,13 +29,13 @@ class Client { const workObject = new WorkObject(); - connect(this.client, updatedOptions); + connect(client, updatedOptions); - this.client.on('data', data => onData(this.client, updatedOptions, data, workObject)); + client.on('data', data => onData(client, updatedOptions, data, workObject)); - this.client.on('error', error => onError(this.client, updatedOptions, error)); + client.on('error', error => onError(client, updatedOptions, error)); - this.client.on('close', () => { + client.on('close', () => { if (updatedOptions.onClose) updatedOptions.onClose(); /* For some reason, corrupted data keeps streaming. This is a hack. @@ -56,12 +58,11 @@ class Client { }); return { - submit: (text) => { - this.submit(text); - }, + client: client, + submit: this.submit, shutdown: () => { - this.client.end(); - this.client.destroy(); + client.end(); + client.destroy(); }, }; } diff --git a/src/index.js.save b/src/index.js.save new file mode 100644 index 0000000..a13a30d --- /dev/null +++ b/src/index.js.save @@ -0,0 +1,76 @@ +// stratum+tcp://stratum.antpool.com +const net = require('net'); +const extend = require('lodash/extend'); +const connect = require('./connect'); +const submitWork = require('./submitWork'); +const onData = require('./onData'); +const onError = require('./onError'); +const validateConfig = require('./validateConfig'); +const WorkObject = require('./workObject'); + +const defaultConfig = { + "autoReconnectOnError": true +}; + +class Client { +/* +submit() { +var args = Array.prototype.slice.call(arguments); // Make real array from arguments +args.unshift(this.client); +console.log(this); +console.log("AAAA"); +submitWork.apply(this, args); +} +*/ + start(options) { + const client = new net.Socket(); + + client.setEncoding('utf8'); + + const updatedOptions = extend({}, defaultConfig, options); + + validateConfig(updatedOptions); + + const workObject = new WorkObject(); + + connect(client, updatedOptions); + + client.on('data', data => onData(client, updatedOptions, data, workObject)); + + client.on('error', error => onError(client, updatedOptions, error)); + + client.on('close', () => { + if (updatedOptions.onClose) updatedOptions.onClose(); + /* + For some reason, corrupted data keeps streaming. This is a hack. + With this hack, I am ensuring that no more callbacks are called + after closing the connection (closing from our end) + */ + extend(updatedOptions, { + onConnect: null, + onClose: null, + onError: null, + onAuthorize: null, + onAuthorizeSuccess: null, + onAuthorizeFail: null, + onNewDifficulty: null, + onSubscribe: null, + onNewMiningWork: null, + onSubmitWorkSuccess: null, + onSubmitWorkFail: null, + }); + }); + + return { + submit: () , + shutdown: () => { + client.end(); + client.destroy(); + }, + }; + } + +}; + +module.exports = (options) => new Client().start(options); +=> diff --git a/src/processData.js b/src/processData.js index b854d8f..e6fedc7 100644 --- a/src/processData.js +++ b/src/processData.js @@ -76,8 +76,8 @@ console.log(jsonData); break; case submitMethod: { - const fnSuccess = onSubmitWorkSuccess; - const fnFailure = onSubmitWorkFail; + const fnSuccess = onSubmitWorkSuccess || (() => {}); + const fnFailure = onSubmitWorkFail || (() => {}); if (result) fnSuccess(error, result); else fnFailure(error, result); } diff --git a/src/submitWork.js b/src/submitWork.js index 4eb24b3..4c70754 100644 --- a/src/submitWork.js +++ b/src/submitWork.js @@ -1,8 +1,8 @@ const { submitWork } = require('./messageContants'); -module.exports = (client, data) => { - console.log(submitWork.replace("", data.worker_name).replace("", data.job_id).replace("", data.extranonce2).replace("", data.ntime ).replace("", data.nonce)); - client.write(submitWork.replace("", data.worker_name).replace("", data.job_id).replace("", data.extranonce2).replace("", data.ntime ).replace("", data.nonce)); +module.exports = (client, worker_name, job_id, extranonce2, ntime, nonce) => { + console.log(submitWork.replace("", worker_name).replace("", job_id).replace("", extranonce2).replace("", ntime ).replace("", nonce)); + client.write(submitWork.replace("", worker_name).replace("", job_id).replace("", extranonce2).replace("", ntime ).replace("", nonce)); }; From 3a881ddd4a4116af3968ec981780cecc3a4a3ae7 Mon Sep 17 00:00:00 2001 From: KorkyMonster Date: Thu, 6 Sep 2018 11:23:52 -0500 Subject: [PATCH 04/12] Removed unneeded index.js --- example.js | 20 ------------- src/index.js.save | 76 ----------------------------------------------- 2 files changed, 96 deletions(-) delete mode 100644 example.js delete mode 100644 src/index.js.save diff --git a/example.js b/example.js deleted file mode 100644 index 9b206fe..0000000 --- a/example.js +++ /dev/null @@ -1,20 +0,0 @@ -const client = require('./index.js'); -const Client = client({ - server: "grlcgang.com", - port: 3333, - worker: "KorkyMonster.testing", - password: "x", - autoReconnectOnError: true, - onConnect: () => console.log('Connected to server'), - onClose: () => console.log('Connection closed'), - onError: (error) => console.log('Error', error.message), - onAuthorizeSuccess: () => Client.submit("KorkyMonster.testing", "cb06", "00000000", "gfgdfgd", "fdsfdfdsf"), - onAuthorizeFail: () => console.log('WORKER FAILED TO AUTHORIZE OH NOOOOOO'), - onNewDifficulty: (newDiff) => console.log('New difficulty', newDiff), - onSubscribe: (subscribeData) => console.log('[Subscribe]', subscribeData), - onNewMiningWork: (newWork) => console.log('[New Work]', newWork), - onSubmitWorkSuccess: (error, result) => console.log("Yay! Our work was accepted!"), - onSubmitWorkFail: (error, result) => console.log("Oh no! Our work was refused because: " + error), -}); - - diff --git a/src/index.js.save b/src/index.js.save deleted file mode 100644 index a13a30d..0000000 --- a/src/index.js.save +++ /dev/null @@ -1,76 +0,0 @@ -// stratum+tcp://stratum.antpool.com -const net = require('net'); -const extend = require('lodash/extend'); -const connect = require('./connect'); -const submitWork = require('./submitWork'); -const onData = require('./onData'); -const onError = require('./onError'); -const validateConfig = require('./validateConfig'); -const WorkObject = require('./workObject'); - -const defaultConfig = { - "autoReconnectOnError": true -}; - -class Client { -/* -submit() { -var args = Array.prototype.slice.call(arguments); // Make real array from arguments -args.unshift(this.client); -console.log(this); -console.log("AAAA"); -submitWork.apply(this, args); -} -*/ - start(options) { - const client = new net.Socket(); - - client.setEncoding('utf8'); - - const updatedOptions = extend({}, defaultConfig, options); - - validateConfig(updatedOptions); - - const workObject = new WorkObject(); - - connect(client, updatedOptions); - - client.on('data', data => onData(client, updatedOptions, data, workObject)); - - client.on('error', error => onError(client, updatedOptions, error)); - - client.on('close', () => { - if (updatedOptions.onClose) updatedOptions.onClose(); - /* - For some reason, corrupted data keeps streaming. This is a hack. - With this hack, I am ensuring that no more callbacks are called - after closing the connection (closing from our end) - */ - extend(updatedOptions, { - onConnect: null, - onClose: null, - onError: null, - onAuthorize: null, - onAuthorizeSuccess: null, - onAuthorizeFail: null, - onNewDifficulty: null, - onSubscribe: null, - onNewMiningWork: null, - onSubmitWorkSuccess: null, - onSubmitWorkFail: null, - }); - }); - - return { - submit: () , - shutdown: () => { - client.end(); - client.destroy(); - }, - }; - } - -}; - -module.exports = (options) => new Client().start(options); -=> From d4e6772c0717d400a77a09c911422b960fa2dcf3 Mon Sep 17 00:00:00 2001 From: KorkyMonster <40674932+KorkyMonster@users.noreply.github.com> Date: Sat, 27 Oct 2018 17:48:26 -0500 Subject: [PATCH 05/12] Updated README.md for Work Submission Changed example to include options such as onSubmitWorkSuccess and onSubmitWorkFail --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f40322d..71cc881 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A NodeJS based stratum client for communication with stratum-capable pool. $ npm i stratum-client --save const client = require('stratum-client'); - client({ + const Client = client({ server: "grlcgang.com", port: 3333, worker: "KorkyMonster.testing", @@ -20,8 +20,12 @@ A NodeJS based stratum client for communication with stratum-capable pool. onNewDifficulty: (newDiff) => console.log('New difficulty', newDiff), onSubscribe: (subscribeData) => console.log('[Subscribe]', subscribeData), onNewMiningWork: (newWork) => console.log('[New Work]', newWork), + onSubmitWorkSuccess: (error, result) => console.log("Yay! Our work was accepted!"), + onSubmitWorkFail: (error, result) => console.log("Oh no! Our work was refused because: " + error), }); +Mining work can then be submitted through `Client.submit("", "", "extranonce2", "ntime", "nonce")`; Worker must have been authorized correctly for work to register with a pool. + `worker` is required in order for `onNewMiningWork()` to receive new work continuously. If `password` if not specified, the client will attempt to authenticate with 'x', which is good enough in most cases. From d58dbf5265e969268e2eae143433c1edc81b1037 Mon Sep 17 00:00:00 2001 From: KorkyMonster Date: Sun, 4 Nov 2018 11:49:39 -0600 Subject: [PATCH 06/12] Fixed formatting --- src/index.js | 96 ++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/src/index.js b/src/index.js index 1ed8cb5..d37db2a 100644 --- a/src/index.js +++ b/src/index.js @@ -9,64 +9,64 @@ const validateConfig = require('./validateConfig'); const WorkObject = require('./workObject'); const defaultConfig = { - "autoReconnectOnError": true + "autoReconnectOnError": true }; class Client { -submit() { -var args = Array.prototype.slice.call(arguments); -args.unshift(this.client); // Make real array from arguments -submitWork.apply(this, args); -} - start(options) { - const client = new net.Socket(); - - client.setEncoding('utf8'); + submit() { + var args = Array.prototype.slice.call(arguments); + args.unshift(this.client); // Make real array from arguments + submitWork.apply(this, args); + } + start(options) { + const client = new net.Socket(); - const updatedOptions = extend({}, defaultConfig, options); + client.setEncoding('utf8'); - validateConfig(updatedOptions); + const updatedOptions = extend({}, defaultConfig, options); - const workObject = new WorkObject(); + validateConfig(updatedOptions); - connect(client, updatedOptions); + const workObject = new WorkObject(); - client.on('data', data => onData(client, updatedOptions, data, workObject)); + connect(client, updatedOptions); - client.on('error', error => onError(client, updatedOptions, error)); + client.on('data', data => onData(client, updatedOptions, data, workObject)); - client.on('close', () => { - if (updatedOptions.onClose) updatedOptions.onClose(); - /* - For some reason, corrupted data keeps streaming. This is a hack. - With this hack, I am ensuring that no more callbacks are called - after closing the connection (closing from our end) - */ - extend(updatedOptions, { - onConnect: null, - onClose: null, - onError: null, - onAuthorize: null, - onAuthorizeSuccess: null, - onAuthorizeFail: null, - onNewDifficulty: null, - onSubscribe: null, - onNewMiningWork: null, - onSubmitWorkSuccess: null, - onSubmitWorkFail: null, - }); - }); - - return { - client: client, - submit: this.submit, - shutdown: () => { - client.end(); - client.destroy(); - }, - }; - } + client.on('error', error => onError(client, updatedOptions, error)); + + client.on('close', () => { + if (updatedOptions.onClose) updatedOptions.onClose(); + /* + For some reason, corrupted data keeps streaming. This is a hack. + With this hack, I am ensuring that no more callbacks are called + after closing the connection (closing from our end) + */ + extend(updatedOptions, { + onConnect: null, + onClose: null, + onError: null, + onAuthorize: null, + onAuthorizeSuccess: null, + onAuthorizeFail: null, + onNewDifficulty: null, + onSubscribe: null, + onNewMiningWork: null, + onSubmitWorkSuccess: null, + onSubmitWorkFail: null, + }); + }); + + return { + client: client, + submit: this.submit, + shutdown: () => { + client.end(); + client.destroy(); + }, + }; + } }; -module.exports = (options) => new Client().start(options); +module.exports = (options) => new Client().start(options); \ No newline at end of file From fa4a317bae7a16e4c9b8d1a5e042cadee729a4cd Mon Sep 17 00:00:00 2001 From: KorkyMonster Date: Thu, 15 Nov 2018 17:12:37 -0600 Subject: [PATCH 07/12] Fixed indention from 4 to 2 spaces --- src/connect.js | 12 +++- src/index.js | 90 +++++++++++------------ src/messageContants.js | 2 +- src/onData.js | 4 +- src/onError.js | 7 +- src/processData.js | 160 ++++++++++++++++++++--------------------- src/submitWork.js | 21 ++---- src/validateConfig.js | 8 +-- src/workObject.js | 26 +++---- 9 files changed, 164 insertions(+), 166 deletions(-) diff --git a/src/connect.js b/src/connect.js index 8e9218a..e11659c 100644 --- a/src/connect.js +++ b/src/connect.js @@ -1,10 +1,16 @@ -const { subscribe } = require('./messageContants'); +const { + subscribe +} = require('./messageContants'); -module.exports = (client, { port, server, onConnect }) => { +module.exports = (client, { + port, + server, + onConnect +}) => { client.connect(port, server, () => { client.write(subscribe); if (onConnect) { onConnect(); } }); -}; +}; \ No newline at end of file diff --git a/src/index.js b/src/index.js index d37db2a..3b34c89 100644 --- a/src/index.js +++ b/src/index.js @@ -9,63 +9,63 @@ const validateConfig = require('./validateConfig'); const WorkObject = require('./workObject'); const defaultConfig = { - "autoReconnectOnError": true + "autoReconnectOnError": true }; class Client { - submit() { - var args = Array.prototype.slice.call(arguments); - args.unshift(this.client); // Make real array from arguments - submitWork.apply(this, args); - } - start(options) { - const client = new net.Socket(); + submit() { + var args = Array.prototype.slice.call(arguments); + args.unshift(this.client); // Make real array from arguments + submitWork.apply(this, args); + } + start(options) { + const client = new net.Socket(); - client.setEncoding('utf8'); + client.setEncoding('utf8'); - const updatedOptions = extend({}, defaultConfig, options); + const updatedOptions = extend({}, defaultConfig, options); - validateConfig(updatedOptions); + validateConfig(updatedOptions); - const workObject = new WorkObject(); + const workObject = new WorkObject(); - connect(client, updatedOptions); + connect(client, updatedOptions); - client.on('data', data => onData(client, updatedOptions, data, workObject)); + client.on('data', data => onData(client, updatedOptions, data, workObject)); - client.on('error', error => onError(client, updatedOptions, error)); + client.on('error', error => onError(client, updatedOptions, error)); - client.on('close', () => { - if (updatedOptions.onClose) updatedOptions.onClose(); - /* - For some reason, corrupted data keeps streaming. This is a hack. - With this hack, I am ensuring that no more callbacks are called - after closing the connection (closing from our end) - */ - extend(updatedOptions, { - onConnect: null, - onClose: null, - onError: null, - onAuthorize: null, - onAuthorizeSuccess: null, - onAuthorizeFail: null, - onNewDifficulty: null, - onSubscribe: null, - onNewMiningWork: null, - onSubmitWorkSuccess: null, - onSubmitWorkFail: null, - }); - }); + client.on('close', () => { + if (updatedOptions.onClose) updatedOptions.onClose(); + /* + For some reason, corrupted data keeps streaming. This is a hack. + With this hack, I am ensuring that no more callbacks are called + after closing the connection (closing from our end) + */ + extend(updatedOptions, { + onConnect: null, + onClose: null, + onError: null, + onAuthorize: null, + onAuthorizeSuccess: null, + onAuthorizeFail: null, + onNewDifficulty: null, + onSubscribe: null, + onNewMiningWork: null, + onSubmitWorkSuccess: null, + onSubmitWorkFail: null, + }); + }); - return { - client: client, - submit: this.submit, - shutdown: () => { - client.end(); - client.destroy(); - }, - }; - } + return { + client: client, + submit: this.submit, + shutdown: () => { + client.end(); + client.destroy(); + }, + }; + } }; diff --git a/src/messageContants.js b/src/messageContants.js index 147335b..9523ad8 100644 --- a/src/messageContants.js +++ b/src/messageContants.js @@ -13,4 +13,4 @@ module.exports = { submitMethod: "mining.submit", submitWork: '{"params": ["", "", "", "", ""], "id": "mining.submit", "method": "mining.submit"}\n', -}; +}; \ No newline at end of file diff --git a/src/onData.js b/src/onData.js index 467d4cf..d180774 100644 --- a/src/onData.js +++ b/src/onData.js @@ -6,9 +6,9 @@ module.exports = (client, updatedOptions, data, workObject) => { if (trim(jsonDataStr).length) { try { processData(client, updatedOptions, JSON.parse(trim(jsonDataStr)), workObject); - } catch(e) { + } catch (e) { console.error(e.message); } } }); -}; +}; \ No newline at end of file diff --git a/src/onError.js b/src/onError.js index 892a08c..bf4387c 100644 --- a/src/onError.js +++ b/src/onError.js @@ -1,7 +1,10 @@ const connect = require('./connect'); module.exports = (client, options, error) => { - const { autoReconnectOnError, onError } = options; + const { + autoReconnectOnError, + onError + } = options; if (onError) onError(error); if (autoReconnectOnError) { @@ -9,4 +12,4 @@ module.exports = (client, options, error) => { } else { client.destroy(); // kill client after server's response } -}; +}; \ No newline at end of file diff --git a/src/processData.js b/src/processData.js index e6fedc7..b2dec99 100644 --- a/src/processData.js +++ b/src/processData.js @@ -2,86 +2,86 @@ const extend = require('lodash/extend'); const cloneDeep = require('lodash/cloneDeep'); const defaultTo = require('lodash/defaultTo'); const { - authorizeMethod, - authorize, - subscribeMethod, - miningDifficulty, - miningNotify, - submitMethod, + authorizeMethod, + authorize, + subscribeMethod, + miningDifficulty, + miningNotify, + submitMethod, } = require('./messageContants'); module.exports = (client, updatedOptions, jsonData, workObject) => { - const key = jsonData.method || jsonData.id; - const { - error, - result, - params, - } = jsonData; - const { - onAuthorize, - onAuthorizeSuccess, - onAuthorizeFail, - onSubscribe, - onNewDifficulty, - worker, - password, - onNewMiningWork, - onSubmitWorkSuccess, - onSubmitWorkFail, - } = updatedOptions; -console.log(jsonData); - switch (key) { - case authorizeMethod: - { - const fnSuccess = onAuthorizeSuccess || onAuthorize || (() => {}); - const fnFailure = onAuthorizeFail || onAuthorize || (() => {}); - if (result) fnSuccess(error, result); - else fnFailure(error, result); - } - case miningDifficulty: - if (params && params.length > 0) { - workObject.miningDiff = params[0]; - if (onNewDifficulty) onNewDifficulty(params[0]); - } - break; - case subscribeMethod: - workObject.extraNonce1 = result[1]; - workObject.extraNonce2Size = result[2]; - if (onSubscribe) { - onSubscribe({ - extraNonce1: workObject.extraNonce1, - extraNonce2Size: workObject.extraNonce2Size, - }); - } - if (worker) { - client.write(authorize.replace("", worker).replace("", defaultTo(password, 'x'))); - } - break; - case miningNotify: - { - let index = -1; - extend(workObject, { - jobId: jsonData.params[++index], - prevhash: jsonData.params[++index], - coinb1: jsonData.params[++index], - coinb2: jsonData.params[++index], - merkle_branch: jsonData.params[++index], - version: jsonData.params[++index], - nbits: jsonData.params[++index], - ntime: jsonData.params[++index], - clean_jobs: jsonData.params[++index], - }); - if (onNewMiningWork) onNewMiningWork(cloneDeep(workObject)); - } - break; - case submitMethod: - { - const fnSuccess = onSubmitWorkSuccess || (() => {}); - const fnFailure = onSubmitWorkFail || (() => {}); - if (result) fnSuccess(error, result); - else fnFailure(error, result); - } - default: - break; - } -}; + const key = jsonData.method || jsonData.id; + const { + error, + result, + params, + } = jsonData; + const { + onAuthorize, + onAuthorizeSuccess, + onAuthorizeFail, + onSubscribe, + onNewDifficulty, + worker, + password, + onNewMiningWork, + onSubmitWorkSuccess, + onSubmitWorkFail, + } = updatedOptions; + console.log(jsonData); + switch (key) { + case authorizeMethod: + { + const fnSuccess = onAuthorizeSuccess || onAuthorize || (() => {}); + const fnFailure = onAuthorizeFail || onAuthorize || (() => {}); + if (result) fnSuccess(error, result); + else fnFailure(error, result); + } + case miningDifficulty: + if (params && params.length > 0) { + workObject.miningDiff = params[0]; + if (onNewDifficulty) onNewDifficulty(params[0]); + } + break; + case subscribeMethod: + workObject.extraNonce1 = result[1]; + workObject.extraNonce2Size = result[2]; + if (onSubscribe) { + onSubscribe({ + extraNonce1: workObject.extraNonce1, + extraNonce2Size: workObject.extraNonce2Size, + }); + } + if (worker) { + client.write(authorize.replace("", worker).replace("", defaultTo(password, 'x'))); + } + break; + case miningNotify: + { + let index = -1; + extend(workObject, { + jobId: jsonData.params[++index], + prevhash: jsonData.params[++index], + coinb1: jsonData.params[++index], + coinb2: jsonData.params[++index], + merkle_branch: jsonData.params[++index], + version: jsonData.params[++index], + nbits: jsonData.params[++index], + ntime: jsonData.params[++index], + clean_jobs: jsonData.params[++index], + }); + if (onNewMiningWork) onNewMiningWork(cloneDeep(workObject)); + } + break; + case submitMethod: + { + const fnSuccess = onSubmitWorkSuccess || (() => {}); + const fnFailure = onSubmitWorkFail || (() => {}); + if (result) fnSuccess(error, result); + else fnFailure(error, result); + } + default: + break; + } +}; \ No newline at end of file diff --git a/src/submitWork.js b/src/submitWork.js index 4c70754..7584e8e 100644 --- a/src/submitWork.js +++ b/src/submitWork.js @@ -1,17 +1,8 @@ -const { submitWork } = require('./messageContants'); +const { + submitWork +} = require('./messageContants'); module.exports = (client, worker_name, job_id, extranonce2, ntime, nonce) => { - console.log(submitWork.replace("", worker_name).replace("", job_id).replace("", extranonce2).replace("", ntime ).replace("", nonce)); - client.write(submitWork.replace("", worker_name).replace("", job_id).replace("", extranonce2).replace("", ntime ).replace("", nonce)); -}; - - - - - - - - - - - + console.log(submitWork.replace("", worker_name).replace("", job_id).replace("", extranonce2).replace("", ntime).replace("", nonce)); + client.write(submitWork.replace("", worker_name).replace("", job_id).replace("", extranonce2).replace("", ntime).replace("", nonce)); +}; \ No newline at end of file diff --git a/src/validateConfig.js b/src/validateConfig.js index 0c6e673..8a737aa 100644 --- a/src/validateConfig.js +++ b/src/validateConfig.js @@ -1,11 +1,9 @@ module.exports = (config) => { if (!config.server) { throw new Error('[server] required'); - } - else if (!config.port) { + } else if (!config.port) { throw new Error('[port] required'); - } - else if (!config.worker) { + } else if (!config.worker) { throw new Error('[worker] required'); } -}; +}; \ No newline at end of file diff --git a/src/workObject.js b/src/workObject.js index 2e52769..ca8b25c 100644 --- a/src/workObject.js +++ b/src/workObject.js @@ -1,27 +1,27 @@ class WorkObject { constructor() { - this.extraNonce1= ''; - this.extraNonce2Size= 0; - this.miningDiff= 0; + this.extraNonce1 = ''; + this.extraNonce2Size = 0; + this.miningDiff = 0; // ID of the job. Use this ID while submitting share generated from this job. - this.jobId= 0; - this.prevhash= ''; // - Hash of previous block. - this.coinb1= ''; // - Initial part of coinbase transaction. - this.coinb2= ''; // - Final part of coinbase transaction. + this.jobId = 0; + this.prevhash = ''; // - Hash of previous block. + this.coinb1 = ''; // - Initial part of coinbase transaction. + this.coinb2 = ''; // - Final part of coinbase transaction. // List of hashes, will be used for calculation of merkle root. // This is not a list of all transactions, it only contains prepared hashes of // steps of merkle tree algorithm. Please read some materials for understanding // how merkle trees calculation works. Unfortunately this example don't have any // step hashes included, my bad! - this.merkle_branch= ''; - this.version= ''; // - Bitcoin block version. - this.nbits= ''; // - Encoded current network difficulty - this.ntime= ''; // - Current ntime/ + this.merkle_branch = ''; + this.version = ''; // - Bitcoin block version. + this.nbits = ''; // - Encoded current network difficulty + this.ntime = ''; // - Current ntime/ // When true, server indicates that submitting shares from previous jobs don't // have a sense and such shares will be rejected. When this flag is set, miner // should also drop all previous jobs, so job_ids can be eventually rotated. - this.clean_jobs= ''; + this.clean_jobs = ''; } }; -module.exports = WorkObject; +module.exports = WorkObject; \ No newline at end of file From aa9bedac02b0da6d558eb09db8025f5072b67937 Mon Sep 17 00:00:00 2001 From: KorkyMonster Date: Thu, 15 Nov 2018 17:30:20 -0600 Subject: [PATCH 08/12] Changed work submission parameters --- README.md | 3 ++- src/index.js | 8 +++----- src/submitWork.js | 7 +++---- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 71cc881..85aa016 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ A NodeJS based stratum client for communication with stratum-capable pool. onSubmitWorkFail: (error, result) => console.log("Oh no! Our work was refused because: " + error), }); -Mining work can then be submitted through `Client.submit("", "", "extranonce2", "ntime", "nonce")`; Worker must have been authorized correctly for work to register with a pool. +Mining work can then be submitted through `Client.submit({"worker_name" : WORKER_NAME_HERE_AS_STRING, "job_id" : JOB_ID, "extranonce2" : EXTRANONCE2, "ntime" : NTIME, "nonce": NONCE})`; Worker must have been authorized correctly for work to register with a pool. `worker` is required in order for `onNewMiningWork()` to receive new work continuously. @@ -45,3 +45,4 @@ DEPRECATED: `onAuthorize(error, result)`. If `onAuthorizeSuccess` or `onAuthoriz ## Other information The project is open for suggestion or feedback. If you notice any issues while developing or using this library, feel free to report it [here](https://github.com/arnabk/stratum-client/issues) + diff --git a/src/index.js b/src/index.js index 3b34c89..bd30638 100644 --- a/src/index.js +++ b/src/index.js @@ -13,10 +13,8 @@ const defaultConfig = { }; class Client { - submit() { - var args = Array.prototype.slice.call(arguments); - args.unshift(this.client); // Make real array from arguments - submitWork.apply(this, args); + submit(options) { + submitWork.apply(this, [options, client]); } start(options) { const client = new net.Socket(); @@ -69,4 +67,4 @@ class Client { }; -module.exports = (options) => new Client().start(options); \ No newline at end of file +module.exports = (options) => new Client().start(options); diff --git a/src/submitWork.js b/src/submitWork.js index 7584e8e..fcc4299 100644 --- a/src/submitWork.js +++ b/src/submitWork.js @@ -2,7 +2,6 @@ const { submitWork } = require('./messageContants'); -module.exports = (client, worker_name, job_id, extranonce2, ntime, nonce) => { - console.log(submitWork.replace("", worker_name).replace("", job_id).replace("", extranonce2).replace("", ntime).replace("", nonce)); - client.write(submitWork.replace("", worker_name).replace("", job_id).replace("", extranonce2).replace("", ntime).replace("", nonce)); -}; \ No newline at end of file +module.exports = (options, client) => { + client.write(submitWork.replace("", options.worker_name).replace("", options.job_id).replace("", options.extranonce2).replace("", options.ntime).replace("", options.nonce)); +}; From 9ef19b5e128acffa192b8191a2676cddb78eb1c1 Mon Sep 17 00:00:00 2001 From: KorkyMonster Date: Tue, 20 Nov 2018 11:57:45 -0600 Subject: [PATCH 09/12] Destructured work submission --- README.md | 2 +- package-lock.json | 30 +++++++++++++++--------------- package.json | 8 ++++---- src/index.js | 5 ++++- src/processData.js | 3 +-- src/submitWork.js | 4 ++-- 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 85aa016..6d44bac 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ A NodeJS based stratum client for communication with stratum-capable pool. onSubmitWorkFail: (error, result) => console.log("Oh no! Our work was refused because: " + error), }); -Mining work can then be submitted through `Client.submit({"worker_name" : WORKER_NAME_HERE_AS_STRING, "job_id" : JOB_ID, "extranonce2" : EXTRANONCE2, "ntime" : NTIME, "nonce": NONCE})`; Worker must have been authorized correctly for work to register with a pool. +Mining work can then be submitted through `Client.submit(["","","","",""]);` Worker must have been authorized correctly for work to register with a pool. `worker` is required in order for `onNewMiningWork()` to receive new work continuously. diff --git a/package-lock.json b/package-lock.json index bcacbab..504235d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,7 @@ "integrity": "sha1-wHshHHyVLsH479Uad+8NHTmQopI=", "dev": true, "requires": { - "balanced-match": "1.0.0", + "balanced-match": "^1.0.0", "concat-map": "0.0.1" } }, @@ -71,12 +71,12 @@ "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", "dev": true, "requires": { - "fs.realpath": "1.0.0", - "inflight": "1.0.6", - "inherits": "2.0.3", - "minimatch": "3.0.4", - "once": "1.4.0", - "path-is-absolute": "1.0.1" + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" } }, "growl": { @@ -103,8 +103,8 @@ "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", "dev": true, "requires": { - "once": "1.4.0", - "wrappy": "1.0.2" + "once": "^1.3.0", + "wrappy": "1" } }, "inherits": { @@ -114,9 +114,9 @@ "dev": true }, "lodash": { - "version": "4.17.10", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz", - "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==" + "version": "4.17.11", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", + "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" }, "minimatch": { "version": "3.0.4", @@ -124,7 +124,7 @@ "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", "dev": true, "requires": { - "brace-expansion": "1.1.8" + "brace-expansion": "^1.1.7" } }, "minimist": { @@ -172,7 +172,7 @@ "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", "dev": true, "requires": { - "wrappy": "1.0.2" + "wrappy": "1" } }, "path-is-absolute": { @@ -187,7 +187,7 @@ "integrity": "sha512-rKC3+DyXWgK0ZLKwmRsrkyHVZAjNkfzeehuFWdGGcqGDTZFH73+RH6S/RDAAxl9GusSjZSUWYLmT9N5pzXFOXQ==", "dev": true, "requires": { - "has-flag": "2.0.0" + "has-flag": "^2.0.0" } }, "wrappy": { diff --git a/package.json b/package.json index a484d12..d1e0d36 100644 --- a/package.json +++ b/package.json @@ -17,9 +17,9 @@ "mining" ], "author": "Arnab Karmakar", - "contributors": { - "name" : "KorkyMonster" - , "url" : "http://github.com/KorkyMonster" + "contributors": { + "name": "KorkyMonster", + "url": "http://github.com/KorkyMonster" }, "license": "MIT", "bugs": { @@ -30,7 +30,7 @@ "mocha": "^4.0.1" }, "dependencies": { - "lodash": "^4.17.10" + "lodash": "^4.17.11" }, "engines": { "node": "~v9.0.0" diff --git a/src/index.js b/src/index.js index bd30638..2be2ae8 100644 --- a/src/index.js +++ b/src/index.js @@ -14,7 +14,10 @@ const defaultConfig = { class Client { submit(options) { - submitWork.apply(this, [options, client]); + submitWork([ + ...options, + this.client, + ]); } start(options) { const client = new net.Socket(); diff --git a/src/processData.js b/src/processData.js index b2dec99..3a7bc6a 100644 --- a/src/processData.js +++ b/src/processData.js @@ -29,7 +29,6 @@ module.exports = (client, updatedOptions, jsonData, workObject) => { onSubmitWorkSuccess, onSubmitWorkFail, } = updatedOptions; - console.log(jsonData); switch (key) { case authorizeMethod: { @@ -84,4 +83,4 @@ module.exports = (client, updatedOptions, jsonData, workObject) => { default: break; } -}; \ No newline at end of file +}; diff --git a/src/submitWork.js b/src/submitWork.js index fcc4299..c2bb697 100644 --- a/src/submitWork.js +++ b/src/submitWork.js @@ -2,6 +2,6 @@ const { submitWork } = require('./messageContants'); -module.exports = (options, client) => { - client.write(submitWork.replace("", options.worker_name).replace("", options.job_id).replace("", options.extranonce2).replace("", options.ntime).replace("", options.nonce)); +module.exports = (options) => { + options[5].write(submitWork.replace("", options[0]).replace("", options[1]).replace("", options[2]).replace("", options[3]).replace("", options[4])); }; From ca7e60c39b572621f4e5748674083850ebf2f0fe Mon Sep 17 00:00:00 2001 From: KorkyMonster Date: Wed, 21 Nov 2018 18:29:22 -0600 Subject: [PATCH 10/12] Ok, I think I finally figured it out --- src/index.js | 13 ++++++------- src/submitWork.js | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 2be2ae8..23e006d 100644 --- a/src/index.js +++ b/src/index.js @@ -12,18 +12,17 @@ const defaultConfig = { "autoReconnectOnError": true }; +const client = new net.Socket(); +client.setEncoding('utf8'); + class Client { submit(options) { - submitWork([ + submitWork({ ...options, - this.client, - ]); + client, + }); } start(options) { - const client = new net.Socket(); - - client.setEncoding('utf8'); - const updatedOptions = extend({}, defaultConfig, options); validateConfig(updatedOptions); diff --git a/src/submitWork.js b/src/submitWork.js index c2bb697..a7e4e43 100644 --- a/src/submitWork.js +++ b/src/submitWork.js @@ -3,5 +3,5 @@ const { } = require('./messageContants'); module.exports = (options) => { - options[5].write(submitWork.replace("", options[0]).replace("", options[1]).replace("", options[2]).replace("", options[3]).replace("", options[4])); + options.client.write(submitWork.replace("", options.worker_name).replace("", options.job_id).replace("", options.extranonce2).replace("", options.ntime).replace("", options.nonce)); }; From ff6508c309625f24c220a67f0a08476dd5b680c6 Mon Sep 17 00:00:00 2001 From: KorkyMonster Date: Fri, 28 Jun 2019 13:54:42 +0530 Subject: [PATCH 11/12] Cleaned up unnecessary formatting --- src/connect.js | 12 +++--------- src/onData.js | 4 ++-- src/onError.js | 7 ++----- src/processData.js | 1 + src/validateConfig.js | 8 +++++--- src/workObject.js | 26 +++++++++++++------------- 6 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/connect.js b/src/connect.js index e11659c..8e9218a 100644 --- a/src/connect.js +++ b/src/connect.js @@ -1,16 +1,10 @@ -const { - subscribe -} = require('./messageContants'); +const { subscribe } = require('./messageContants'); -module.exports = (client, { - port, - server, - onConnect -}) => { +module.exports = (client, { port, server, onConnect }) => { client.connect(port, server, () => { client.write(subscribe); if (onConnect) { onConnect(); } }); -}; \ No newline at end of file +}; diff --git a/src/onData.js b/src/onData.js index d180774..467d4cf 100644 --- a/src/onData.js +++ b/src/onData.js @@ -6,9 +6,9 @@ module.exports = (client, updatedOptions, data, workObject) => { if (trim(jsonDataStr).length) { try { processData(client, updatedOptions, JSON.parse(trim(jsonDataStr)), workObject); - } catch (e) { + } catch(e) { console.error(e.message); } } }); -}; \ No newline at end of file +}; diff --git a/src/onError.js b/src/onError.js index bf4387c..892a08c 100644 --- a/src/onError.js +++ b/src/onError.js @@ -1,10 +1,7 @@ const connect = require('./connect'); module.exports = (client, options, error) => { - const { - autoReconnectOnError, - onError - } = options; + const { autoReconnectOnError, onError } = options; if (onError) onError(error); if (autoReconnectOnError) { @@ -12,4 +9,4 @@ module.exports = (client, options, error) => { } else { client.destroy(); // kill client after server's response } -}; \ No newline at end of file +}; diff --git a/src/processData.js b/src/processData.js index 3a7bc6a..19d3b13 100644 --- a/src/processData.js +++ b/src/processData.js @@ -80,6 +80,7 @@ module.exports = (client, updatedOptions, jsonData, workObject) => { if (result) fnSuccess(error, result); else fnFailure(error, result); } + break; default: break; } diff --git a/src/validateConfig.js b/src/validateConfig.js index 8a737aa..0c6e673 100644 --- a/src/validateConfig.js +++ b/src/validateConfig.js @@ -1,9 +1,11 @@ module.exports = (config) => { if (!config.server) { throw new Error('[server] required'); - } else if (!config.port) { + } + else if (!config.port) { throw new Error('[port] required'); - } else if (!config.worker) { + } + else if (!config.worker) { throw new Error('[worker] required'); } -}; \ No newline at end of file +}; diff --git a/src/workObject.js b/src/workObject.js index ca8b25c..2e52769 100644 --- a/src/workObject.js +++ b/src/workObject.js @@ -1,27 +1,27 @@ class WorkObject { constructor() { - this.extraNonce1 = ''; - this.extraNonce2Size = 0; - this.miningDiff = 0; + this.extraNonce1= ''; + this.extraNonce2Size= 0; + this.miningDiff= 0; // ID of the job. Use this ID while submitting share generated from this job. - this.jobId = 0; - this.prevhash = ''; // - Hash of previous block. - this.coinb1 = ''; // - Initial part of coinbase transaction. - this.coinb2 = ''; // - Final part of coinbase transaction. + this.jobId= 0; + this.prevhash= ''; // - Hash of previous block. + this.coinb1= ''; // - Initial part of coinbase transaction. + this.coinb2= ''; // - Final part of coinbase transaction. // List of hashes, will be used for calculation of merkle root. // This is not a list of all transactions, it only contains prepared hashes of // steps of merkle tree algorithm. Please read some materials for understanding // how merkle trees calculation works. Unfortunately this example don't have any // step hashes included, my bad! - this.merkle_branch = ''; - this.version = ''; // - Bitcoin block version. - this.nbits = ''; // - Encoded current network difficulty - this.ntime = ''; // - Current ntime/ + this.merkle_branch= ''; + this.version= ''; // - Bitcoin block version. + this.nbits= ''; // - Encoded current network difficulty + this.ntime= ''; // - Current ntime/ // When true, server indicates that submitting shares from previous jobs don't // have a sense and such shares will be rejected. When this flag is set, miner // should also drop all previous jobs, so job_ids can be eventually rotated. - this.clean_jobs = ''; + this.clean_jobs= ''; } }; -module.exports = WorkObject; \ No newline at end of file +module.exports = WorkObject; From 83cbcd80eb1840ca40ac440e45b2791a17d1fbc7 Mon Sep 17 00:00:00 2001 From: KorkyMonster Date: Wed, 3 Jul 2019 23:00:42 +0530 Subject: [PATCH 12/12] 1.1.0 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 504235d..3ffff26 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "stratum-client", - "version": "1.0.4", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index d1e0d36..3fd004f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "stratum-client", - "version": "1.0.4", + "version": "1.1.0", "description": "A NodeJS based stratum client for communication with stratum pool", "main": "index.js", "scripts": {