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.

index.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. var spawn = require("child_process").spawn;
  2. function hps(n) {
  3. var suffix = "Hash/s";
  4. if (n > 1000000) {
  5. n = n / 1000000;
  6. suffix ="M"+suffix;
  7. } else if (n > 1000) {
  8. n = n / 1000;
  9. suffix = "k"+suffix;
  10. }
  11. return (n.toFixed(2))+" "+suffix;
  12. }
  13. function mineProc(cb1, cb2, merk, dif, en1, en2, cnt) {
  14. var obj = {
  15. coinb1: cb1, coinb2: cb2, merkleBranch: merk,
  16. difficulty: dif, exnounce1: en1, exnounce2: en2, iters: cnt
  17. };
  18. var child = spawn(
  19. "node", [ __dirname+"/mining-process.js", JSON.stringify(obj) ]);
  20. child.output = "";
  21. child.hps = 0;
  22. child.stderr.on("data", d => process.stderr.write(d));
  23. child.stdout.on("data", d => {
  24. var s = d.toString();
  25. if (s[0] === "o")
  26. child.output = s.substr(1);
  27. else if (s[0] === "h")
  28. child.hps = parseInt(s.substr(1));
  29. else
  30. console.error("Warning: Unexpected child output,", s);
  31. });
  32. return child;
  33. }
  34. function mine(
  35. coinb1, coinb2, merkleBranch,
  36. difficulty, exnounce1, exnounce2_len) {
  37. var cores = 4;
  38. var max = Math.pow(2, exnounce2_len * 8) - 2;
  39. var parts = Math.ceil(max / cores);
  40. return new Promise((resolve, reject) => {
  41. var childs = [];
  42. var childsLeft = cores;
  43. var inter = null;
  44. function rej(hash) {
  45. clearInterval(inter);
  46. resolve(hash);
  47. }
  48. for (var i = 0; i < cores; ++i) {
  49. var num = parts * i;
  50. if (num + parts > max)
  51. max - parts;
  52. var buf = Buffer.alloc(exnounce2_len, '\0');
  53. buf.writeUInt32BE(num);
  54. var exnounce2 = buf.toString("hex");
  55. var child = mineProc(
  56. coinb1, coinb2, merkleBranch,
  57. difficulty, exnounce1, exnounce2, parts);
  58. var obj = {
  59. coinb1, coinb2, merkleBranch,
  60. difficulty, exnounce1, exnounce2_len
  61. };
  62. childs[i] = child;
  63. }
  64. childs.forEach(child => {
  65. child.on("exit", code => {
  66. console.error("Child exited with code", code);
  67. if (code === 0) {
  68. childsLeft -= 1;
  69. childs.forEach(x => x !== child && x.kill());
  70. rej(child.output.trim());
  71. } else {
  72. childsLeft -= 1;
  73. if (childsLeft <= 0)
  74. rej(false);
  75. }
  76. });
  77. });
  78. inter = setInterval(() => {
  79. var sum = 0;
  80. childs.forEach(c => sum += c.hps);
  81. console.log(hps(sum));
  82. }, 2000);
  83. });
  84. }
  85. class CPUMiner {
  86. constructor() {
  87. this.work = null;
  88. this.difficulty = 1;
  89. this.exnounce1 = null;
  90. this.exnounce2_len = null;
  91. }
  92. async startWork(work) {
  93. console.log("Starting work", work.id);
  94. if (this.exnounce1 == null) {
  95. console.log("Ignoring work because extranounce is null.");
  96. return false;
  97. }
  98. this.work = work;
  99. return await mine(
  100. this.work.coinb1,
  101. this.work.coinb2,
  102. this.work.berkleBranch,
  103. this.difficulty,
  104. this.exnounce1,
  105. this.exnounce2_len);
  106. }
  107. setDifficulty(difficulty) {
  108. this.difficulty = difficulty;
  109. }
  110. setNounce(en1, en2_len) {
  111. this.exnounce1 = en1;
  112. this.exnounce2_len = en2_len;
  113. }
  114. async wait() {
  115. }
  116. }
  117. module.exports = CPUMiner;