-
Notifications
You must be signed in to change notification settings - Fork 9
/
class.ClientWriter.js
98 lines (75 loc) · 2.51 KB
/
class.ClientWriter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
const
precon = require('@mintpond/mint-precon'),
buffers = require('@mintpond/mint-utils').buffers,
Job = require('./class.Job'),
StratumError = require('./class.StratumError'),
algorithm = require('./service.algorithm');
class ClientWriter {
/**
* Constructor.
*
* @param args
* @param args.client {Client}
*/
constructor(args) {
precon.notNull(args.client, 'client');
const _ = this;
_._client = args.client;
_._port = _._client.port;
_._socket = _._client.socket;
}
reply(args) {
precon.integer(args.replyId, 'replyId');
precon.opt_boolean(args.result, 'result');
precon.opt_instanceOf(args.error, StratumError, 'error');
const _ = this;
const replyId = args.replyId;
const result = args.result;
const error = args.error;
_._socket.send({
id: replyId,
result: error ? false: result,
error: error ? error.responseArr: null
});
}
replySubscribe(args) {
precon.integer(args.replyId, 'replyId');
const _ = this;
const replyId = args.replyId;
const subscriptionIdHex = _._client.subscriptionIdHex;
const extraNonce1Hex = _._client.extraNonce1Hex;
_._socket.send({
id: replyId,
result: [subscriptionIdHex, extraNonce1Hex],
error: null
});
}
miningNotify(args) {
precon.instanceOf(args.job, Job, 'job');
precon.boolean(args.cleanJobs, 'cleanJobs');
precon.positiveNumber(args.diff, 'diff');
const _ = this;
const job = args.job;
const cleanJobs = args.cleanJobs;
const diff = args.diff;
const nDiff = diff / algorithm.multiplier;
const targetBuf = buffers.packUInt256LE(algorithm.diff1 / nDiff);
const m = {
id: null,
method: 'mining.notify',
params: [
/* 0 Job Id */ job.idHex,
/* 1 progpow hash */ job.getProgPowHashBuf(_._client).toString('hex'),
/* 2 seed hash */ job.seedHashBuf.toString('hex'),
/* 3 share target */ buffers.leToHex(targetBuf),
/* 4 clean_jobs */ cleanJobs,
/* 5 block height */ job.height,
/* 6 nbits (diff) */ buffers.leToHex(job.bitsBuf)
]
};
console.log(m);
_._socket.send(m);
}
}
module.exports = ClientWriter;