| } | } | ||||
| async startWork(work) { | async startWork(work) { | ||||
| console.log("Starting work", work.id); | |||||
| if (this.exnounce1 == null) { | if (this.exnounce1 == null) { | ||||
| console.log("Ignoring work because extranounce is null."); | console.log("Ignoring work because extranounce is null."); | ||||
| return false; | return false; |
| exnounce1 = Buffer.from(exnounce1, "hex"); | exnounce1 = Buffer.from(exnounce1, "hex"); | ||||
| exnounce2 = Buffer.from(exnounce2, "hex"); | exnounce2 = Buffer.from(exnounce2, "hex"); | ||||
| difficulty = 3; | |||||
| difficulty = 5; | |||||
| var hashes = 0; | var hashes = 0; | ||||
| var sd = new Date().getTime(); | var sd = new Date().getTime(); |
| }); | }); | ||||
| var onjson = json => { | var onjson = json => { | ||||
| var obj = JSON.parse(json); | |||||
| var obj; | |||||
| try { | |||||
| obj = JSON.parse(json); | |||||
| } catch (err) { | |||||
| console.log("Failed to parse JSON: '"+json+"'"); | |||||
| throw err; | |||||
| } | |||||
| if (obj.id == null) { | if (obj.id == null) { | ||||
| var listeners = this._listeners[obj.method]; | var listeners = this._listeners[obj.method]; | ||||
| cb(obj.error, obj.result); | cb(obj.error, obj.result); | ||||
| } | } | ||||
| //Digest JSON lines as they come, but only if they're a whole line | |||||
| var currdata = ""; | var currdata = ""; | ||||
| this.sock.on("data", d => { | this.sock.on("data", d => { | ||||
| var s = d.toString(); | var s = d.toString(); | ||||
| currdata += d; | |||||
| if (s.indexOf("\n") !== -1) { | |||||
| currdata.split("\n").filter(l => l !== "").forEach(onjson); | |||||
| currdata = ""; | |||||
| currdata += s; | |||||
| var arr = s.split("\n"); | |||||
| while (arr[1] != null) { | |||||
| onjson(arr.shift()); | |||||
| arr.shift(); | |||||
| } | } | ||||
| currdata = arr[0] || ""; | |||||
| }); | }); | ||||
| } | } | ||||
| } | } | ||||
| async startWork() { | async startWork() { | ||||
| this.log("Starting work"); | |||||
| this.log("Starting work", this.work.id); | |||||
| var res; | var res; | ||||
| try { | try { | ||||
| res = await this.miner.startWork(this.work); | res = await this.miner.startWork(this.work); | ||||
| }); | }); | ||||
| var sub = await this.rpc.call("mining.subscribe"); | var sub = await this.rpc.call("mining.subscribe"); | ||||
| this.log("Got nounce", sub[1]); | |||||
| this.miner.setNounce(sub[1], sub[2]); | this.miner.setNounce(sub[1], sub[2]); | ||||
| if (--this.waitFor == 0) | if (--this.waitFor == 0) | ||||
| this.startWork(); | this.startWork(); | ||||
| if (!success) | if (!success) | ||||
| throw new Error("Incorrect username/password."); | throw new Error("Incorrect username/password."); | ||||
| console.log("Authenticated "+user+"."); | |||||
| this.log("Authenticated "+user+"."); | |||||
| } | } | ||||
| writePayload() { | writePayload() { |
| "name": "stratum-test", | "name": "stratum-test", | ||||
| "version": "1.0.0", | "version": "1.0.0", | ||||
| "lockfileVersion": 1, | "lockfileVersion": 1, | ||||
| "requires": true, | |||||
| "dependencies": { | "dependencies": { | ||||
| "bindings": { | "bindings": { | ||||
| "version": "1.3.0", | "version": "1.3.0", |