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.

mining-process.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var cryptutil = require("../cryptutil");
  2. var {
  3. coinb1, coinb2, merkleBranch,
  4. difficulty, exnounce1, exnounce2, iters
  5. } = JSON.parse(process.argv[2]);
  6. var start = exnounce2.toString("hex");
  7. coinb1 = Buffer.from(coinb1, "hex");
  8. coinb2 = Buffer.from(coinb2, "hex");
  9. exnounce1 = Buffer.from(exnounce1, "hex");
  10. exnounce2 = Buffer.from(exnounce2, "hex");
  11. //stratnum difficulty = nbits "encoded current network difficulty"
  12. difficulty = 5;
  13. var hashes = 0;
  14. var sd = new Date().getTime();
  15. for (var i = 0; i < iters; ++i) {
  16. var coinbase =
  17. cryptutil.createCoinbase(exnounce1, exnounce2, coinb1, coinb2);
  18. var cbHash = cryptutil.createCbHash(coinbase);
  19. //build merkle root
  20. //create block header
  21. //check block header starts with 0000
  22. var success = true;
  23. for (var i = 0; i < difficulty; ++i) {
  24. if (cbHash[i] !== 0) {
  25. success = false;
  26. break;
  27. }
  28. }
  29. hashes += 1;
  30. var d = new Date().getTime();
  31. if (d - 2000 >= sd) {
  32. console.log("h"+Math.floor(hashes / 2));
  33. sd = d;
  34. hashes = 0;
  35. }
  36. if (success) {
  37. console.log("o"+cbHash.toString("hex"));
  38. //send hash back to server
  39. process.exit(0);
  40. }
  41. cryptutil.incbufBE(exnounce2);
  42. if (exnounce2[0] == 255) {
  43. console.error("exnounce2[0] reached 255");
  44. process.exit(1);
  45. }
  46. }
  47. console.error("iterated through "+iters);
  48. process.exit(1);