-
Notifications
You must be signed in to change notification settings - Fork 22
/
Stratum.cs
279 lines (235 loc) · 9.32 KB
/
Stratum.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.Collections;
using System.Diagnostics;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Text;
namespace DotNetStratumMiner
{
class Stratum
{
public event EventHandler<StratumEventArgs> GotSetDifficulty;
public event EventHandler<StratumEventArgs> GotNotify;
public event EventHandler<StratumEventArgs> GotResponse;
public static Hashtable PendingACKs = new Hashtable();
public TcpClient tcpClient;
private int SharesSubmitted = 0;
private string page = "";
public string ExtraNonce1 = "";
public int ExtraNonce2 = 0;
private string Server;
private int Port;
private string Username;
private string Password;
public int ID;
public void ConnectToServer(string MineServer, int MinePort, string MineUser, string MinePassword)
{
try
{
ID = 1;
Server = MineServer;
Port = MinePort;
Username = MineUser;
Password = MinePassword;
tcpClient = new TcpClient(AddressFamily.InterNetwork);
// Start an asynchronous connection
tcpClient.BeginConnect(Server, Port, new AsyncCallback(ConnectCallback), tcpClient);
}
catch (Exception ex)
{
Console.WriteLine("Socket error:" + ex.Message);
}
}
private void ConnectCallback(IAsyncResult result)
{
if (tcpClient.Connected)
Console.WriteLine("Connected");
else
{
Console.WriteLine("Unable to connect to server {0} on port {1}", Server, Port);
Environment.Exit(-1);
}
// We are connected successfully
try
{
SendSUBSCRIBE();
SendAUTHORIZE();
NetworkStream networkStream = tcpClient.GetStream();
byte[] buffer = new byte[tcpClient.ReceiveBufferSize];
// Now we are connected start async read operation.
networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);
}
catch (Exception ex)
{
Console.WriteLine("Socket error:" + ex.Message);
}
}
public void SendSUBSCRIBE()
{
Byte[] bytesSent;
StratumCommand Command = new StratumCommand();
Command.id = ID++;
Command.method = "mining.subscribe";
Command.parameters = new ArrayList();
string request = Utilities.JsonSerialize(Command) + "\n";
bytesSent = Encoding.ASCII.GetBytes(request);
try
{
tcpClient.GetStream().Write(bytesSent, 0, bytesSent.Length);
PendingACKs.Add(Command.id, Command.method);
}
catch (Exception ex)
{
Console.WriteLine("Socket error:" + ex.Message);
ConnectToServer(Server, Port, Username, Password);
}
Console.WriteLine("Sent mining.subscribe");
}
public void SendAUTHORIZE()
{
Byte[] bytesSent;
StratumCommand Command = new StratumCommand();
Command.id = ID++;
Command.method = "mining.authorize";
Command.parameters = new ArrayList();
Command.parameters.Add(Username);
Command.parameters.Add(Password);
string request = Utilities.JsonSerialize(Command) + "\n";
bytesSent = Encoding.ASCII.GetBytes(request);
try
{
tcpClient.GetStream().Write(bytesSent, 0, bytesSent.Length);
PendingACKs.Add(Command.id, Command.method);
}
catch(Exception ex)
{
Console.WriteLine("Socket error:" + ex.Message);
ConnectToServer(Server, Port, Username, Password);
}
Console.WriteLine("Sent mining.authorize");
}
public void SendSUBMIT(string JobID, string nTime, string Nonce, int Difficulty)
{
StratumCommand Command = new StratumCommand();
Command.id = ID++;
Command.method = "mining.submit";
Command.parameters = new ArrayList();
Command.parameters.Add(Username);
Command.parameters.Add(JobID);
Command.parameters.Add(ExtraNonce2.ToString("x8"));
Command.parameters.Add(nTime);
Command.parameters.Add(Nonce);
string SubmitString = Utilities.JsonSerialize(Command) + "\n";
Byte[] bytesSent = Encoding.ASCII.GetBytes(SubmitString);
try
{
tcpClient.GetStream().Write(bytesSent, 0, bytesSent.Length);
PendingACKs.Add(Command.id, Command.method);
}
catch (Exception ex)
{
Console.WriteLine("Socket error:" + ex.Message);
ConnectToServer(Server, Port, Username, Password);
}
SharesSubmitted++;
Console.WriteLine("{0} - Submit (Difficulty {1})", DateTime.Now, Difficulty);
Debug.WriteLine("[{0}] Submit (Difficulty {1}) : {2}", DateTime.Now, Difficulty, SubmitString);
}
// Callback for Read operation
private void ReadCallback(IAsyncResult result)
{
NetworkStream networkStream;
int bytesread;
byte[] buffer = result.AsyncState as byte[];
try
{
networkStream = tcpClient.GetStream();
bytesread = networkStream.EndRead(result);
}
catch (Exception ex)
{
Console.WriteLine("Socket error:" + ex.Message);
return;
}
if (bytesread == 0)
{
Console.WriteLine(DateTime.Now + " Disconnected. Reconnecting...");
Debug.WriteLine(DateTime.Now + " Disconnected. Reconnecting...");
tcpClient.Close();
tcpClient = null;
PendingACKs.Clear();
ConnectToServer(Server, Port, Username, Password);
return;
}
// Get the data
string data = ASCIIEncoding.ASCII.GetString(buffer, 0, bytesread);
Debug.WriteLine(data);
page = page + data;
int FoundClose = page.IndexOf('}');
while (FoundClose > 0)
{
string CurrentString = page.Substring(0, FoundClose + 1);
// We can get either a command or response from the server. Try to deserialise both
StratumCommand Command = Utilities.JsonDeserialize<StratumCommand>(CurrentString);
StratumResponse Response = Utilities.JsonDeserialize<StratumResponse>(CurrentString);
StratumEventArgs e = new StratumEventArgs();
if (Command.method != null) // We got a command
{
Debug.WriteLine(DateTime.Now + " Got Command: " + CurrentString);
e.MiningEventArg = Command;
switch (Command.method)
{
case "mining.notify":
if (GotNotify != null)
GotNotify(this, e);
break;
case "mining.set_difficulty":
if (GotSetDifficulty != null)
GotSetDifficulty(this, e);
break;
}
}
else if (Response.error != null || Response.result != null) // We got a response
{
Debug.WriteLine(DateTime.Now + " Got Response: " + CurrentString);
e.MiningEventArg = Response;
// Find the command that this is the response to and remove it from the list of commands that we're waiting on a response to
string Cmd = (string)PendingACKs[Response.id];
PendingACKs.Remove(Response.id);
if (Cmd == null)
Console.WriteLine("Unexpected Response");
else if (GotResponse != null)
GotResponse(Cmd, e);
}
page = page.Remove(0, FoundClose + 2);
FoundClose = page.IndexOf('}');
}
// Then start reading from the network again.
networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);
}
}
[DataContract]
public class StratumCommand
{
[DataMember]
public string method;
[DataMember]
public System.Nullable<int> id;
[DataMember(Name = "params")]
public ArrayList parameters;
}
[DataContract]
public class StratumResponse
{
[DataMember]
public ArrayList error;
[DataMember]
public System.Nullable<int> id;
[DataMember]
public object result;
}
public class StratumEventArgs:EventArgs
{
public object MiningEventArg;
}
}