Skip to content

Commit

Permalink
Merge pull request #5 from KorkyMonster/master
Browse files Browse the repository at this point in the history
Work Submission
  • Loading branch information
arnabk authored Jul 17, 2019
2 parents e0a2ed6 + 83cbcd8 commit ff33d96
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 93 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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(["<worker>","<job_id>","<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.
Expand All @@ -41,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)


32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
Expand All @@ -26,7 +30,7 @@
"mocha": "^4.0.1"
},
"dependencies": {
"lodash": "^4.17.10"
"lodash": "^4.17.11"
},
"engines": {
"node": "~v9.0.0"
Expand Down
20 changes: 15 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -11,12 +12,17 @@ const defaultConfig = {
"autoReconnectOnError": true
};

class Client {
const client = new net.Socket();
client.setEncoding('utf8');

class Client {
submit(options) {
submitWork({
...options,
client,
});
}
start(options) {
const client = new net.Socket();
client.setEncoding('utf8');

const updatedOptions = extend({}, defaultConfig, options);

validateConfig(updatedOptions);
Expand All @@ -42,14 +48,18 @@ class Client {
onError: null,
onAuthorize: null,
onAuthorizeSuccess: null,
onAuthorizeFail: null,
onAuthorizeFail: null,
onNewDifficulty: null,
onSubscribe: null,
onNewMiningWork: null,
onSubmitWorkSuccess: null,
onSubmitWorkFail: null,
});
});

return {
client: client,
submit: this.submit,
shutdown: () => {
client.end();
client.destroy();
Expand Down
5 changes: 4 additions & 1 deletion src/messageContants.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ module.exports = {

miningNotify: "mining.notify",

};
submitMethod: "mining.submit",
submitWork: '{"params": ["<worker.name>", "<jobID>", "<ExtraNonce2>", "<ntime>", "<nonce>"], "id": "mining.submit", "method": "mining.submit"}\n',

};
147 changes: 79 additions & 68 deletions src/processData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,86 @@ const extend = require('lodash/extend');
const cloneDeep = require('lodash/cloneDeep');
const defaultTo = require('lodash/defaultTo');
const {
authorizeMethod,
authorize,
subscribeMethod,
miningDifficulty,
miningNotify,
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
} = updatedOptions;
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.name>", worker).replace("<worker.pass>", 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;
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;
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.name>", worker).replace("<worker.pass>", 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);
}
break;
default:
break;
}
};
7 changes: 7 additions & 0 deletions src/submitWork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const {
submitWork
} = require('./messageContants');

module.exports = (options) => {
options.client.write(submitWork.replace("<worker.name>", options.worker_name).replace("<jobID>", options.job_id).replace("<ExtraNonce2>", options.extranonce2).replace("<ntime>", options.ntime).replace("<nonce>", options.nonce));
};

0 comments on commit ff33d96

Please sign in to comment.