-
Notifications
You must be signed in to change notification settings - Fork 12
/
ClientManager.cs
399 lines (346 loc) · 14 KB
/
ClientManager.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Bitnet.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
namespace CentralMine.NET
{
class ClientManager
{
public enum Currency
{
Bitcoin,
Xencoin,
Gamerscoin
};
EventLog mEventLog;
Dictionary<uint, bool> mBlacklist;
Dictionary<Currency, string> mCurrencyProviders;
Listener mListener;
public List<Client> mClients;
Mutex mClientListMutex;
Thread mUpdateThread;
Email mMailer;
Upstream mUpstream;
public double mHashrate;
int mPrevBlockIndex;
public WorkBlock[] mPrevBlocks;
public WorkBlock mBlock = null;
public bool mDumpClients = false;
public MiningTarget mMiningTarget;
public long mBlocksSubmitted;
public long mBlocksAccepted;
public ClientManager()
{
mEventLog = new EventLog();
mBlacklist = new Dictionary<uint, bool>();
mBlacklist[0xC425E50F] = true;
mBlocksSubmitted = 0;
mBlocksAccepted = 0;
#region Bitcoin
MiningTarget bc = new MiningTarget();
bc.mName = "Bitcoin";
bc.mPOWAlgorithm = HashAlgorithm.DoubleSHA256;
bc.mWallet = new WalletInfo();
bc.mWallet.mRPCAddress = "127.0.0.1";
bc.mWallet.mRPCPort = 8332;
bc.mWallet.mRPCUser = "rpcuser";
bc.mWallet.mRPCPass = "rpcpass";
PoolInfo pi = new PoolInfo();
pi.mName = "Slush";
pi.mAddress = "stratum.bitcoin.cz";
pi.mPort = 3333;
pi.mUser = "f2mserver.worker1";
pi.mPassword = "yezPpJrt";
bc.mPools.Add(pi);
#endregion
#region Bitgem
MiningTarget bg = new MiningTarget();
bg.mName = "Bitgem";
bg.mPOWAlgorithm = HashAlgorithm.Scrypt;
bg.mWallet = new WalletInfo();
bg.mWallet.mRPCAddress = "127.0.0.1";
bg.mWallet.mRPCPort = 8348;
bg.mWallet.mRPCUser = "rpcuser";
bg.mWallet.mRPCPass = "rpcpass";
pi = new PoolInfo();
pi.mName = "JTeam";
pi.mAddress = "us-east.jtcpools.org";
pi.mPort = 3365;
pi.mUser = "f2mserver.worker1";
pi.mPassword = "torque9900";
bg.mPools.Add(pi);
#endregion
#region Gamerscoin
MiningTarget gc = new MiningTarget();
gc.mName = "Gamerscoin";
gc.mPOWAlgorithm = HashAlgorithm.Scrypt;
gc.mWallet = new WalletInfo();
gc.mWallet.mRPCAddress = "127.0.0.1";
gc.mWallet.mRPCPort = 7332;
gc.mWallet.mRPCUser = "rpcuser";
gc.mWallet.mRPCPass = "rpcpass";
pi = new PoolInfo();
pi.mName = "gamerscoin";
pi.mAddress = "gamers-coin.org";
pi.mPort = 3333;
pi.mUser = "rono.f2mserver";
pi.mPassword = "torque9900";
gc.mPools.Add(pi);
#endregion
#region AllMulti
MiningTarget multi = new MiningTarget();
multi.mName = "Mutli";
multi.mPOWAlgorithm = HashAlgorithm.Scrypt;
pi = new PoolInfo();
pi.mName = "WeMineAll";
pi.mAddress = "multi2.wemineall.com";
pi.mPort = 5555;
pi.mUser = "f2mserver.1";
pi.mPassword = "x";
multi.mPools.Add(pi);
#endregion
#region BBQ
MiningTarget bbq = new MiningTarget();
bbq.mName = "BBQ";
bbq.mPOWAlgorithm = HashAlgorithm.Scrypt;
pi = new PoolInfo();
pi.mName = "BBQPool";
pi.mAddress = "www.bbqpool.net";
pi.mPort = 3333;
pi.mUser = "f2mserver.1";
pi.mPassword = "x";
bbq.mPools.Add(pi);
#endregion
//mMiningTarget = gc;
//mMiningTarget = bg;
//mMiningTarget = bc;
//mMiningTarget = multi;
mMiningTarget = bbq;
//mUpstream = new US_Wallet(this);
//mUpstream.SetHost(mMiningTarget.mWallet.mRPCAddress, mMiningTarget.mWallet.mRPCPort);
//mUpstream.SetCredentials(mMiningTarget.mWallet.mRPCUser, mMiningTarget.mWallet.mRPCPass);
mUpstream = new US_Stratum(this);
mUpstream.SetHost(mMiningTarget.mPools[0].mAddress, mMiningTarget.mPools[0].mPort);
mUpstream.SetCredentials(mMiningTarget.mPools[0].mUser, mMiningTarget.mPools[0].mPassword);
//mUpstream = new US_Wallet();
//mUpstream.SetHost("127.0.0.1", 7332);
//mUpstream.SetCredentials("rpcuser", "rpcpass");
//mUpstream = new US_Stratum();
//mUpstream.SetHost("gamers-coin.org", 3333);
//mUpstream.SetCredentials("rono.f2mserver", "torque9900");
mPrevBlocks = new WorkBlock[5];
mPrevBlockIndex = 0;
mMailer = new Email();
mClients = new List<Client>();
mClientListMutex = new Mutex();
mListener = new Listener(80, this);
mUpdateThread = new Thread(new ThreadStart(Update));
mHashrate = 0;
BeginBlock();
mUpdateThread.Start();
}
public void Close()
{
mUpstream.Destroy();
// Kill the listener
mListener.Close();
// Kill the update thread
mUpdateThread.Abort();
// Close all clients
foreach (Client c in mClients)
{
c.Close();
}
// Close the event log
mEventLog.Close();
}
public void SetCurrency(Currency c)
{
//if (mCurrency != c)
//{
// mEventLog.RecordEvent(EventLog.EventType.Server, string.Format("Currency changed from: {0} to {1}", mCurrency, c));
// mCurrency = c;
//}
}
public void AcceptClient(TcpClient client)
{
IPEndPoint ep = client.Client.RemoteEndPoint as IPEndPoint;
byte[] bytes = ep.Address.GetAddressBytes();
uint addr = (uint)(bytes[0] << 24) | (uint)(bytes[1] << 16) | (uint)(bytes[2] << 8) | bytes[3];
if (!mBlacklist.ContainsKey(addr))
{
Client c = new Client(client, this);
mEventLog.RecordEvent(EventLog.EventType.Network, string.Format("New connection from: {0}", ep.Address.ToString()));
mClientListMutex.WaitOne();
mClients.Add(c);
mClientListMutex.ReleaseMutex();
}
}
void BeginBlock()
{
mEventLog.RecordEvent(EventLog.EventType.Upstream, string.Format("Getting {0} block", mMiningTarget.mName));
WorkBlock work = mUpstream.GetWorkBlock();
if (work != null)
{
mEventLog.RecordEvent(EventLog.EventType.Upstream, string.Format("Got {0} block: {1}", mMiningTarget.mName, work.ToString()));
// Put the current block in the previous list
if (mBlock != null)
{
mPrevBlocks[mPrevBlockIndex++] = mBlock;
if (mPrevBlockIndex >= mPrevBlocks.Length)
mPrevBlockIndex = 0;
}
mBlock = work;
mBlock.mAlgorithm = mMiningTarget.mPOWAlgorithm;
}
else
{
mEventLog.RecordEvent(EventLog.EventType.Upstream, string.Format("GetWork failed"));
}
}
void AssignWork(Client c)
{
if (mBlock != null)
{
//mEventLog.RecordEvent(EventLog.EventType.HashWork, string.Format("Allocating {0} hashes for client: {1}", c.mDesiredHashes, c.ToLogString()));
HashManager.HashBlock hashes = mBlock.mHashMan.Allocate(c.mDesiredHashes, c);
if (hashes != null)
{
//mEventLog.RecordEvent(EventLog.EventType.HashWork, string.Format("Sending hash range ({0} - {1}) to client: {2}", hashes.Start, hashes.Start + hashes.Count, c.ToLogString()));
c.SendWork(hashes, mBlock);
}
}
}
public void WorkComplete(Client solver, bool solutionFound, uint solution)
{
mEventLog.RecordClientWork(solver);
WorkBlock block = solver.mCurrentBlock;
block.mHashMan.FinishBlock(solver.mHashBlock);
solver.mHashBlock = null;
if (solutionFound && mBlock == block)
{
mBlocksSubmitted++;
bool success = mUpstream.SubmitWork(block, solution);
//if (!success)
// success = mUpstream.SubmitWork(block, (uint)IPAddress.HostToNetworkOrder((int)solution));
// Start a new block
if (success)
{
// Send email notification about this found solution
/*
TimeSpan span = DateTime.Now - block.mHashMan.mStartTime;
string hashrate = string.Format("{0:N}", block.mHashMan.mHashesDone / span.TotalSeconds);
string body = "Found solution for " + block.mCurrency + " block: \n" + block.ToString() + "\n\n";
body += "Solution string: " + data + "\n";
body += "Block Accepted: " + success.ToString() + "\n";
body += "Hashes Done: " + block.mHashMan.mHashesDone + "\n";
body += "Time Spent: " + span.ToString() + "\n";
body += "Hashrate: " + hashrate + "\n";
body += "Clients: " + mClients.Count + "\n";
body += "\n\n";
//mMailer.SendEmail(body);
*/
//mMailer.SendEmail("Block Accepted");
string data = block.GetSolutionString(solution);
mEventLog.RecordEvent(EventLog.EventType.Upstream, string.Format("Work accepted! solution: {0}, dataString: {1}", solution, data));
BeginBlock();
mBlocksAccepted++;
}
else
{
string data = block.GetSolutionString(solution);
mEventLog.RecordEvent(EventLog.EventType.Upstream, string.Format("Work not accepted. solution: {0}, dataString: {1}", solution, data));
}
}
}
public uint GetHashesDone()
{
uint hashesDone = mBlock.mHashMan.mHashesDone;
foreach (WorkBlock b in mPrevBlocks)
{
if (b != null)
hashesDone += b.mHashMan.mHashesDone;
}
return hashesDone;
}
void Update()
{
while (true)
{
double oldHashrate = mHashrate;
mHashrate = 0;
if (mBlock == null || mUpstream.NewBlockReady() || mBlock.mHashMan.IsComplete() || mBlock.mHashMan.IsExpired() )
{
// Start work on a new block
BeginBlock();
}
Client clientThatWantsInfo = null;
if (mClientListMutex.WaitOne(1))
{
int clientsOnline = mClients.Count;
foreach (Client c in mClients)
{
bool stillAlive = c.Update();
if (!stillAlive)
{
if (c.mCurrentBlock != null)
c.mCurrentBlock.mHashMan.FreeBlock(c.mHashBlock);
mClients.Remove(c);
break;
}
mHashrate += c.mHashrate;
if (c.mState == Client.State.Busy && c.mCurrentBlock != mBlock)
c.StopWork();
if (c.mState == Client.State.Ready)
AssignWork(c);
if (c.mStatusClient)
c.SendStatus(clientsOnline, oldHashrate);
if (c.mClientInfoRequested && clientThatWantsInfo == null)
{
clientThatWantsInfo = c;
c.mClientInfoRequested = false;
}
}
if (clientThatWantsInfo != null)
{
string info = "{\"clients\":[";
foreach (Client c in mClients)
{
info += c.ToJSON() + ",";
}
info = info.Substring(0, info.Length - 1);
info += "]}";
clientThatWantsInfo.SendClientInfo(info);
}
mClientListMutex.ReleaseMutex();
Thread.Sleep(50);
}
else
{
Thread.Sleep(1);
}
if (mDumpClients)
{
FileStream file = new FileStream("clients.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(file);
mClientListMutex.WaitOne();
foreach (Client c in mClients)
{
string str = c.ToString();
sw.WriteLine(str);
}
mClientListMutex.ReleaseMutex();
sw.Close();
mDumpClients = false;
}
}
}
}
}