12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- var RPCConn = require("./rpcconn");
- var ASICConn = require("./asicconn");
-
- class Miner {
- constructor(ip, port, dev, baud) {
- this.name = dev
- this.work = null;
- this.nextDifficulty = 1;
- this.exnounce1 = null;
- this.exnounce2_len = null;
-
- this.asic = new ASICConn(dev, baud);
- this.rpc = new RPCConn(ip, port);
- }
-
- log(...msgs) {
- process.stdout.write(this.name+": ");
- console.log(msgs);
- }
-
- async connect() {
- await this.asic.wait();
- await this.rpc.wait();
-
- if (this.asic.error)
- throw this.asic.error;
-
- this.rpc.on("mining.notify", params => {
- var work = {
- id: params[0], prevHash: params[1],
- coinb1: params[2], coinb2: params[3],
- merkleBranch: params[4], version: params[5],
- nBits: params[6], nTime: params[7], cleanJobs: params[8],
- };
-
- this.notify(work);
- });
- this.rpc.on("mining.set_difficulty", params => {
- this.log("difficulty", params);
- this.difficulty(params[0]);
- });
-
- var sub = await this.rpc.call("mining.subscribe");
- this.exnounce1 = sub[1];
- this.exnounce2_len = sub[2];
- }
-
- async auth(user, pass) {
- var success = await this.rpc.call("mining.authorize", user, pass);
- if (!success)
- throw new Error("Incorrect username/password.");
- console.log("Authenticated "+user+".");
- }
-
- notify(work) {
- if (this.exnounce1 == null)
- return console.log("Ignoring work because extranounce is null.");
-
- this.log("Notification");
- this.log(work);
- this.work = work;
- this.log(
- "Using extranounce1 '"+this.exnounce1+
- "', and exnounce2 length "+this.exnounce2_len);
- }
-
- difficulty(diff) {
- this.nextDifficulty = diff;
- }
-
- writePayload() {
- var rdlen = 95 - 63;
- var rdata = Buffer.alloc(rdlen);
- for (var i = 0; i < rdlen; ++i)
- rdata[i] = this._block[94 - i];
-
- var rmlen = 64;
- var rmid = Buffer.alloc(rmlen);
- for (var i = 0; i < rmlen; ++i)
- rmid[i] = this._midstate[rmlen - i - 1];
-
- var payload = Buffer.concat([rmid, rdata]);
- this.log("Writing '"+payload.toString("hex")+"'");
- this._port.write(payload);
- }
- }
-
- module.exports = Miner;
|