Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. class Program
  2. {
  3. static Pool _pool = null;
  4. static Work _work = null;
  5. static uint _nonce = 0;
  6. static long _maxAgeTicks = 20000 * TimeSpan.TicksPerMillisecond;
  7. static uint _batchSize = 100000;
  8.  
  9. static void Main(string[] args)
  10. {
  11. while (true)
  12. {
  13. try
  14. {
  15. _pool = SelectPool();
  16. _work = GetWork();
  17. while (true)
  18. {
  19. if (_work == null || _work.Age > _maxAgeTicks)
  20. _work = GetWork();
  21.  
  22. if (_work.FindShare(ref _nonce, _batchSize))
  23. {
  24. SendShare(_work.Current);
  25. _work = null;
  26. }
  27. else
  28. PrintCurrentState();
  29. }
  30. }
  31. catch (Exception e)
  32. {
  33. Console.WriteLine();
  34. Console.Write("ERROR: ");
  35. Console.WriteLine(e.Message);
  36. }
  37. Console.WriteLine();
  38. Console.Write("Hit 'Enter' to try again...");
  39. Console.ReadLine();
  40. }
  41. }
  42.  
  43.  
  44. private static void ClearConsole()
  45. {
  46. Console.Clear();
  47. Console.WriteLine("*****************************");
  48. Console.WriteLine("*** Minimal Bitcoin Miner ***");
  49. Console.WriteLine("*****************************");
  50. Console.WriteLine();
  51. }
  52.  
  53. private static Pool SelectPool()
  54. {
  55. ClearConsole();
  56. Print("Chose a Mining Pool 'user:password@url:port' or leave empty to skip.");
  57. Console.Write("Select Pool: ");
  58. string login = ReadLineDefault("lithander_2:foo@btcguild.com:8332");
  59. return new Pool(login);
  60. }
  61.  
  62. private static Work GetWork()
  63. {
  64. ClearConsole();
  65. Print("Requesting Work from Pool...");
  66. Print("Server URL: " + _pool.Url.ToString());
  67. Print("User: " + _pool.User);
  68. Print("Password: " + _pool.Password);
  69. return _pool.GetWork();
  70. }
  71.  
  72. private static void SendShare(byte[] share)
  73. {
  74. ClearConsole();
  75. Print("*** Found Valid Share ***");
  76. Print("Share: " + Utils.ToString(_work.Current));
  77. Print("Nonce: " + Utils.ToString(_nonce));
  78. Print("Hash: " + Utils.ToString(_work.Hash));
  79. Print("Sending Share to Pool...");
  80. if (_pool.SendShare(share))
  81. Print("Server accepted the Share!");
  82. else
  83. Print("Server declined the Share!");
  84.  
  85. Console.Write("Hit 'Enter' to continue...");
  86. Console.ReadLine();
  87. }
  88.  
  89. private static DateTime _lastPrint = DateTime.Now;
  90. private static void PrintCurrentState()
  91. {
  92. ClearConsole();
  93. Print("Data: " + Utils.ToString(_work.Data));
  94. string current = Utils.ToString(_nonce);
  95. string max = Utils.ToString(uint.MaxValue);
  96. double progress = ((double)_nonce / uint.MaxValue) * 100;
  97. Print("Nonce: " + current + "/" + max + " " + progress.ToString("F2") + "%");
  98. Print("Hash: " + Utils.ToString(_work.Hash));
  99. TimeSpan span = DateTime.Now - _lastPrint;
  100. Print("Speed: " + (int)(((_batchSize) / 1000) / span.TotalSeconds) + "Kh/s");
  101. _lastPrint = DateTime.Now;
  102. }
  103.  
  104. private static void Print(string msg)
  105. {
  106. Console.WriteLine(msg);
  107. Console.WriteLine();
  108. }
  109.  
  110. private static string ReadLineDefault(string defaultValue)
  111. {
  112. //Allow Console.ReadLine with a default value
  113. string userInput = Console.ReadLine();
  114. Console.WriteLine();
  115. if (userInput == "")
  116. return defaultValue;
  117. else
  118. return userInput;
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement