An old btc miner project.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

miner.js 2.0KB

6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var RPCConn = require("./rpcconn");
  2. var ASICConn = require("./asicconn");
  3. class Miner {
  4. constructor(ip, port, dev, baud) {
  5. this.name = dev
  6. this.work = null;
  7. this.nextDifficulty = 1;
  8. this.exnounce1 = null;
  9. this.exnounce2_len = null;
  10. this.asic = new ASICConn(dev, baud);
  11. this.rpc = new RPCConn(ip, port);
  12. }
  13. log(...msgs) {
  14. process.stdout.write(this.name+": ");
  15. console.log(msgs);
  16. }
  17. async connect() {
  18. await this.asic.wait();
  19. await this.rpc.wait();
  20. if (this.asic.error)
  21. throw this.asic.error;
  22. this.rpc.on("mining.notify", params => {
  23. var work = {
  24. id: params[0], prevHash: params[1],
  25. coinb1: params[2], coinb2: params[3],
  26. merkleBranch: params[4], version: params[5],
  27. nBits: params[6], nTime: params[7], cleanJobs: params[8],
  28. };
  29. this.notify(work);
  30. });
  31. this.rpc.on("mining.set_difficulty", params => {
  32. this.log("difficulty", params);
  33. this.difficulty(params[0]);
  34. });
  35. var sub = await this.rpc.call("mining.subscribe");
  36. this.exnounce1 = sub[1];
  37. this.exnounce2_len = sub[2];
  38. }
  39. async auth(user, pass) {
  40. var success = await this.rpc.call("mining.authorize", user, pass);
  41. if (!success)
  42. throw new Error("Incorrect username/password.");
  43. console.log("Authenticated "+user+".");
  44. }
  45. notify(work) {
  46. if (this.exnounce1 == null)
  47. return console.log("Ignoring work because extranounce is null.");
  48. this.log("Notification");
  49. this.log(work);
  50. this.work = work;
  51. this.log(
  52. "Using extranounce1 '"+this.exnounce1+
  53. "', and exnounce2 length "+this.exnounce2_len);
  54. }
  55. difficulty(diff) {
  56. this.nextDifficulty = diff;
  57. }
  58. writePayload() {
  59. var rdlen = 95 - 63;
  60. var rdata = Buffer.alloc(rdlen);
  61. for (var i = 0; i < rdlen; ++i)
  62. rdata[i] = this._block[94 - i];
  63. var rmlen = 64;
  64. var rmid = Buffer.alloc(rmlen);
  65. for (var i = 0; i < rmlen; ++i)
  66. rmid[i] = this._midstate[rmlen - i - 1];
  67. var payload = Buffer.concat([rmid, rdata]);
  68. this.log("Writing '"+payload.toString("hex")+"'");
  69. this._port.write(payload);
  70. }
  71. }
  72. module.exports = Miner;