-
Notifications
You must be signed in to change notification settings - Fork 22
/
Miner.cs
127 lines (105 loc) · 4.29 KB
/
Miner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
namespace DotNetStratumMiner
{
class Miner
{
// General Variables
public volatile bool done = false;
public volatile uint FinalNonce = 0;
Thread[] threads;
public Miner(int? optThreadCount = null)
{
int threadCount = optThreadCount ?? Environment.ProcessorCount;
if (threadCount > Environment.ProcessorCount) {
threadCount = Environment.ProcessorCount;
}
threads = new Thread[threadCount];
}
public void Mine(object sender, DoWorkEventArgs e)
{
Debug.WriteLine("New Miner. ID = " + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Starting {0} threads for new block...", threads.Length);
Job ThisJob = (Job)e.Argument;
// Gets the data to hash and the target from the work
byte[] databyte = Utilities.ReverseByteArrayByFours(Utilities.HexStringToByteArray(ThisJob.Data));
byte[] targetbyte = Utilities.HexStringToByteArray(ThisJob.Target);
done = false;
FinalNonce = 0;
// Spin up background threads to do the hashing
for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread(() => doScrypt(databyte, targetbyte, (uint)i, (uint)threads.Length));
threads[i].IsBackground = false;
threads[i].Priority = ThreadPriority.Normal;//.Lowest; // For debugging
threads[i].Start();
}
// Block until all the threads finish
for (int i = 0; i < threads.Length; i++)
{
threads[i].Join();
}
// Fill in the answer if work done
if (FinalNonce != 0)
{
ThisJob.Answer = FinalNonce;
e.Result = ThisJob;
}
else
e.Result = null;
Debug.WriteLine("Miner ID {0} finished", Thread.CurrentThread.ManagedThreadId);
}
// Reference: https://github.com/replicon/Replicon.Cryptography.SCrypt
public void doScrypt(byte[] Tempdata, byte[] Target, uint Nonce, uint Increment)
{
double Hashcount = 0;
byte[] Databyte = new byte[80];
Array.Copy(Tempdata, 0, Databyte, 0, 76);
Debug.WriteLine("New thread");
DateTime StartTime = DateTime.Now;
try
{
byte[] ScryptResult = new byte[32];
// Loop until done is set or we meet the target
while (!done)
{
Databyte[76] = (byte)(Nonce >> 0);
Databyte[77] = (byte)(Nonce >> 8);
Databyte[78] = (byte)(Nonce >> 16);
Databyte[79] = (byte)(Nonce >> 24);
ScryptResult = Replicon.Cryptography.SCrypt.SCrypt.DeriveKey(Databyte, Databyte, 1024, 1, 1, 32);
Hashcount++;
if (meetsTarget(ScryptResult, Target)) // Did we meet the target?
{
if (!done)
FinalNonce = Nonce;
done = true;
break;
}
else
Nonce += Increment; // If not, increment the nonce and try again
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
FinalNonce = 0;
}
double Elapsedtime = (DateTime.Now - StartTime).TotalMilliseconds;
Console.WriteLine("Thread finished - {0:0} hashes in {1:0.00} ms. Speed: {2:0.00} kHash/s", Hashcount, Elapsedtime, Hashcount / Elapsedtime);
}
public bool meetsTarget(byte[] hash, byte[] target)
{
for (int i = hash.Length - 1; i >= 0; i--)
{
if ((hash[i] & 0xff) > (target[i] & 0xff))
return false;
if ((hash[i] & 0xff) < (target[i] & 0xff))
return true;
}
return false;
}
}
}