-
Notifications
You must be signed in to change notification settings - Fork 12
/
US_Stratum.cs
480 lines (427 loc) · 17.1 KB
/
US_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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.IO;
using BCNet;
namespace CentralMine.NET
{
class US_Stratum : Upstream
{
enum Status
{
Disconnected,
Connecting,
Connected,
Subscribing,
Authorizing,
Ready
};
Thread mThread;
Socket mSocket;
IAsyncResult mSocketAsync;
Status mStatus;
int mRPCID;
Dictionary<int, string> mPendingRPCs;
byte[] mPendingData;
int mPendingDataSize;
string mExtraNOnce;
int mExtraNOnceSize;
bool mAuthorized;
string mSubmitString;
bool mAwaitingSubmitResult = false;
bool mSubmitResult = false;
bool mNewBlockReady = false;
ulong mServerDiff = 0;
List<JobInfo> mJobs;
FileStream mLogFile;
StreamWriter mLog;
public US_Stratum(ClientManager cm) : base(cm)
{
mPendingRPCs = new Dictionary<int, string>();
mJobs = new List<JobInfo>();
mPendingData = new byte[1024 * 16];
mPendingDataSize = 0;
mPort = 0;
mStatus = Status.Disconnected;
mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mThread = new Thread(new ThreadStart(ThreadUpdate));
mThread.Start();
mLogFile = File.Open("stratum.log", FileMode.Create);
mLog = new StreamWriter(mLogFile);
}
public override void Destroy()
{
mThread.Abort();
Disconnect();
mLog.Close();
}
public override void SetHost(string url, ushort port)
{
base.SetHost(url, port);
Disconnect();
}
public override void SetCredentials(string user, string pass)
{
base.SetCredentials(user, pass);
Disconnect();
}
public override WorkBlock GetWorkBlock()
{
if (mStatus != Status.Ready)
return null;
if (mJobs.Count <= 0)
return null;
JobInfo ji = mJobs[mJobs.Count - 1];
ji.GenerateWork();
mNewBlockReady = false;
Console.WriteLine("GetWork job: " + ji.mJobID);
return ji;
}
public override bool SubmitWork(WorkBlock work, uint solution)
{
JobInfo ji = (JobInfo)work;
// {"params": ["slush.miner1", "bf", "00000001", "504e86ed", "b2957c02"], "id": 4, "method": "mining.submit"}
// Values in particular order: worker_name (previously authorized!), job_id, extranonce2, ntime, nonce.
string[] parms = new string[5];
parms[0] = mUser;
parms[1] = ji.mJobID;
parms[2] = ji.mExtraNonce2;
parms[3] = ji.mTimeStr;
parms[4] = Utils.UIntToHexString(solution);
mSubmitString = String.Format("Submit Job({0}), Time({1}), Solution({2})", parms[1], parms[3], parms[4]) + "\n" + ji.strData + "\n" + ji.strTarget;
Console.WriteLine(mSubmitString);
mLog.WriteLine(mSubmitString);
mAwaitingSubmitResult = true;
SendRPC("mining.submit", parms);
while (mAwaitingSubmitResult)
{
Thread.Sleep(50);
}
return mSubmitResult;
}
public override bool NewBlockReady()
{
return mNewBlockReady;
}
void Disconnect()
{
if( mSocket.Connected )
mSocket.Close();
mStatus = Status.Disconnected;
mPendingDataSize = 0;
mAuthorized = false;
}
int SendRPC(string methodName, string[] parameters)
{
int rpcID = mRPCID++;
string rpc = "{\"id\": " + rpcID + ", \"method\": \"" + methodName + "\", \"params\": [";
if (parameters != null)
{
for (int i = 0; i < parameters.Length; i++)
{
rpc += "\"" + parameters[i] + "\"";
if (i < (parameters.Length - 1))
rpc += ", ";
}
}
rpc += "]}\n";
Console.WriteLine("SendRPC: " + rpc);
mLog.WriteLine("SendRPC: " + rpc);
byte[] data = System.Text.Encoding.ASCII.GetBytes(rpc);
int bytesSent = mSocket.Send(data, data.Length, SocketFlags.None);
mPendingRPCs[rpcID] = methodName;
return rpcID;
}
void MiningSubmit(JObject obj, string json)
{
bool? result = obj["result"].Value<bool?>();
if (result == null)
{
mSubmitResult = false;
}
else
{
mSubmitResult = result.Value;
}
mAwaitingSubmitResult = false;
}
void MiningSubscribe(JObject obj)
{
// {"id": 1, "result": [[["mining.set_difficulty", "b4b6693b72a50c7116db18d6497cac52"], ["mining.notify", "ae6812eb4cd7735a302a8a9dd95cf71f"]], "08000002", 4], "error": null}
//obj = JsonConvert.DeserializeObject<JObject>("{\"error\": null, \"id\": 1, \"result\": [[\"mining.notify\", \"ae6812eb4cd7735a302a8a9dd95cf71f\"], \"f8002c90\", 4]}");
JArray result = (JArray)obj["result"];
JArray parms = (JArray)result[0];
mExtraNOnce = (string)result[1];
mExtraNOnceSize = (int)result[2];
/*
string diff = null;
string notify = null;
foreach (JToken t in parms)
{
JArray param = (JArray)t;
string key = (string)param[0];
string val = (string)param[1];
if (key == "mining.set_difficulty")
diff = val;
else if (key == "mining.notify")
notify = val;
else
{
Console.WriteLine("MiningSubscribe - Unknown param: " + key);
}
}
*/
}
void MiningAuthorize(JObject obj)
{
mAuthorized = (bool)obj["result"];
if (!mAuthorized)
Disconnect();
}
void MiningNotify(JObject obj)
{
//obj = JsonConvert.DeserializeObject<JObject>("{\"params\": [\"b3ba\", \"7dcf1304b04e79024066cd9481aa464e2fe17966e19edf6f33970e1fe0b60277\", \"01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff270362f401062f503253482f049b8f175308\", \"0d2f7374726174756d506f6f6c2f000000000100868591052100001976a91431482118f1d7504daf1c001cbfaf91ad580d176d88ac00000000\", [\"57351e8569cb9d036187a79fd1844fd930c1309efcd16c46af9bb9713b6ee734\", \"936ab9c33420f187acae660fcdb07ffdffa081273674f0f41e6ecc1347451d23\"], \"00000002\", \"1b44dfdb\", \"53178f9b\", true], \"id\": null, \"method\": \"mining.notify\"}");
/*
{
"params": [
"bf",
"4d16b6f85af6e2198f44ae2a6de67f78487ae5611b77c6c0440b921e00000000",
"01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff20020862062f503253482f04b8864e5008",
"072f736c7573682f000000000100f2052a010000001976a914d23fcdf86f7e756a64a7a9688ef9903327048ed988ac00000000",
[],
"00000002",
"1c2ac4af",
"504e86b9",
false
],
"id": null,
"method": "mining.notify"
}
job_id - ID of the job. Use this ID while submitting share generated from this job.
prevhash - Hash of previous block.
coinb1 - Initial part of coinbase transaction.
coinb2 - Final part of coinbase transaction.
merkle_branch - List of hashes, will be used for calculation of merkle root. This is not a list of all transactions, it only contains prepared hashes of steps of merkle tree algorithm. Please read some materials for understanding how merkle trees calculation works. Unfortunately this example don't have any step hashes included, my bad!
version - Bitcoin block version.
nbits - Encoded current network difficulty
ntime - Current ntime/
clean_jobs - When true, server indicates that submitting shares from previous jobs don't have a sense and such shares will be rejected. When this flag is set, miner should also drop all previous jobs, so job_ids can be eventually rotated.
*/
JArray parms = (JArray)obj["params"];
if (parms.Count == 9)
{
JobInfo ji = new JobInfo(mExtraNOnce, mExtraNOnceSize);
ji.mJobID = (string)parms[0];
ji.mPrevHash = (string)parms[1];
ji.mCoinBaseA = (string)parms[2];
ji.mCoinBaseB = (string)parms[3];
JArray merkleBranch = (JArray)parms[4];
foreach (JToken t in merkleBranch)
ji.mMerkleBranch.Add((string)t);
ji.mVersion = (string)parms[5];
ji.mDifficulty = (string)parms[6];
ji.mTime = (string)parms[7];
ji.Initialize(mServerDiff);
bool cleanJobs = (bool)parms[8];
if (cleanJobs)
mJobs.Clear();
mJobs.Add(ji);
mNewBlockReady = true;
}
else
{
Disconnect();
Console.WriteLine("MiningNotify - Bad Data");
}
}
void MiningSetDifficulty(JObject obj)
{
//{"params": [32], "id": null, "method": "mining.set_difficulty"}
double difficulty = (double)obj["params"][0];
//difficulty = 32;
mLog.WriteLine("Set Difficulty: " + difficulty);
ulong baseDiff = 0x00000000FFFF0000;
double diff = (double)baseDiff;
// if scrypt
if( mClientManager.mMiningTarget.mPOWAlgorithm == HashAlgorithm.Scrypt )
diff *= 65536;
mServerDiff = (ulong)(diff / difficulty);
}
void ProcessNetworkLine(string line)
{
Console.WriteLine(line);
mLog.WriteLine(line);
JObject obj = JsonConvert.DeserializeObject<JObject>(line);
string pendingMethod = null;
int? nid = (obj["id"]).Value<int?>();
if( nid == null )
{
pendingMethod = (string)obj["method"];
}
else
{
int id = (int)obj["id"];
if (mPendingRPCs.ContainsKey(id))
{
//JObject error = (JObject)obj["error"];
//if( !error.HasValues )
{
pendingMethod = mPendingRPCs[id];
}
//else
//{
// Console.WriteLine("RPC error for method: " + pendingMethod);
// Disconnect();
//}
mPendingRPCs.Remove(id);
}
else
{
Console.WriteLine("Unexpected RPC id: " + id);
}
}
if( pendingMethod != null )
{
switch (pendingMethod)
{
case "mining.subscribe":
MiningSubscribe(obj);
break;
case "mining.authorize":
MiningAuthorize(obj);
break;
case "mining.notify":
MiningNotify(obj);
break;
case "mining.submit":
MiningSubmit(obj, line);
break;
case "mining.set_difficulty":
MiningSetDifficulty(obj);
break;
default:
Console.WriteLine("Unhandled RPC response: " + pendingMethod);
break;
}
}
else
{
Console.WriteLine("unknown method: " + line);
}
}
void ReadNetwork()
{
if (mSocket.Connected)
{
if (mSocket.Poll(1, SelectMode.SelectRead))
{
// Data to be read
byte[] packetData = new byte[1024 * 32];
int bytesRead = mSocket.Receive(packetData);
if (bytesRead > 0)
{
Array.Copy(packetData, 0, mPendingData, mPendingDataSize, bytesRead);
mPendingDataSize += bytesRead;
while (mPendingDataSize > 0)
{
int i = 0;
for (i = 0; i < mPendingDataSize; i++)
{
if (mPendingData[i] == '\n')
break;
}
if (i <= mPendingDataSize)
{
MemoryStream ms = new MemoryStream(mPendingData);
StreamReader sr = new StreamReader(ms);
string line = sr.ReadLine();
ProcessNetworkLine(line);
int offset = (int)line.Length + 1;
mPendingDataSize -= offset;
Array.Copy(mPendingData, offset, mPendingData, 0, mPendingDataSize);
sr.Close();
}
}
}
}
}
}
void ThreadUpdate()
{
while (true)
{
try
{
ReadNetwork();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Disconnect();
}
switch (mStatus)
{
case Status.Disconnected:
if (mURL != null && mPort != 0)
{
// We have something to try to connect to, do it now
mSocketAsync = mSocket.BeginConnect(mURL, (int)mPort, null, null);
mStatus = Status.Connecting;
}
break;
case Status.Connecting:
{
if (mSocketAsync.IsCompleted)
{
mSocket.EndConnect(mSocketAsync);
if (mSocket.Connected)
{
mStatus = Status.Connected;
mRPCID = 1;
}
else
Disconnect();
mSocketAsync = null;
}
}
break;
case Status.Connected:
{
// Now connected, subscribe for mining
SendRPC("mining.subscribe", null);
mExtraNOnceSize = -1;
mStatus = Status.Subscribing;
}
break;
case Status.Subscribing:
if (mExtraNOnceSize >= 0 && mUser != null && mPass != null)
{
// Done subscribing for mining, now authorize the worker
// {"params": ["slush.miner1", "password"], "id": 2, "method": "mining.authorize"}
string[] parms = new string[2];
parms[0] = mUser;
parms[1] = mPass;
SendRPC("mining.authorize", parms);
mStatus = Status.Authorizing;
}
break;
case Status.Authorizing:
if( mAuthorized )
{
mStatus = Status.Ready;
}
break;
case Status.Ready:
break;
}
Thread.Sleep(50);
}
}
}
}