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.

stratum-client.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var RPCConn = require("./rpcconn");
  2. class StratumClient {
  3. constructor(ip, port, miner) {
  4. this.name = ip+":"+port;
  5. this.queue = [];
  6. this.working = false;
  7. this.nounceReady = false;
  8. this.difficultyReady = false;
  9. this.miner = miner;
  10. this.rpc = new RPCConn(ip, port);
  11. }
  12. log(...msgs) {
  13. process.stdout.write(this.name+": ");
  14. console.log.apply(console, msgs);
  15. }
  16. async startWork() {
  17. if (this.working)
  18. return;
  19. if (this.queue.length === 0)
  20. return;
  21. var work = this.queue.shift();
  22. this.log("Starting work", work.id);
  23. this.working = true;
  24. var res;
  25. try {
  26. res = await this.miner.startWork(work);
  27. } catch (err) {
  28. console.trace(err);
  29. return;
  30. } finally {
  31. this.working = false;
  32. this.startWork();
  33. }
  34. this.log("Work done,", res.toString("hex"));
  35. }
  36. async connect() {
  37. await this.rpc.wait();
  38. await this.miner.wait();
  39. this.rpc.on("mining.notify", async params => {
  40. var work = {
  41. id: params[0], prevHash: params[1],
  42. coinb1: params[2], coinb2: params[3],
  43. merkleBranch: params[4], version: params[5],
  44. nBits: params[6], nTime: params[7], cleanJobs: params[8],
  45. };
  46. this.log("Got work", work.id);
  47. this.queue.push(work);
  48. if (this.difficultyReady && this.nounceReady)
  49. this.startWork();
  50. });
  51. this.rpc.on("mining.set_difficulty", params => {
  52. this.log("Got difficulty", params[0]);
  53. this.miner.setDifficulty(params[0]);
  54. if (this.difficultyReady)
  55. return;
  56. this.difficultyReady = true;
  57. if (this.nounceReady)
  58. this.startWork();
  59. });
  60. var sub = await this.rpc.call("mining.subscribe");
  61. this.log("Got nounce", sub[1]);
  62. this.miner.setNounce(sub[1], sub[2]);
  63. this.nounceReady = true;
  64. if (this.difficultyReady)
  65. this.startWork();
  66. }
  67. async auth(user, pass) {
  68. var success = await this.rpc.call("mining.authorize", user, pass);
  69. if (!success)
  70. throw new Error("Incorrect username/password.");
  71. this.log("Authenticated "+user+".");
  72. }
  73. writePayload() {
  74. var rdlen = 95 - 63;
  75. var rdata = Buffer.alloc(rdlen);
  76. for (var i = 0; i < rdlen; ++i)
  77. rdata[i] = this._block[94 - i];
  78. var rmlen = 64;
  79. var rmid = Buffer.alloc(rmlen);
  80. for (var i = 0; i < rmlen; ++i)
  81. rmid[i] = this._midstate[rmlen - i - 1];
  82. var payload = Buffer.concat([rmid, rdata]);
  83. this.log("Writing '"+payload.toString("hex")+"'");
  84. this._port.write(payload);
  85. }
  86. }
  87. module.exports = StratumClient;