-
Notifications
You must be signed in to change notification settings - Fork 12
/
US_Wallet.cs
108 lines (97 loc) · 3.03 KB
/
US_Wallet.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Bitnet.Client;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace CentralMine.NET
{
class US_Wallet : Upstream
{
Thread mThread;
int mBlockCount = 0;
bool mNewBlockReady = false;
public US_Wallet(ClientManager cm)
: base(cm)
{
mThread = new Thread(new ThreadStart(ThreadFunc));
mThread.Start();
}
public override void Destroy()
{
mThread.Abort();
base.Destroy();
}
public override WorkBlock GetWorkBlock()
{
WorkBlock work = null;
JObject obj = null;
try
{
// Get block from bitcoin
BitnetClient bc = new BitnetClient("http://" + mURL + ":" + mPort);
bc.Credentials = new NetworkCredential(mUser, mPass);
mBlockCount = bc.GetBlockCount();
obj = bc.GetWork();
mNewBlockReady = false;
}
catch (Exception e)
{
Console.WriteLine("Failed to get work!");
Console.WriteLine(e.Message);
}
if (obj != null)
{
work = new WorkBlock(obj);
}
return work;
}
public override bool SubmitWork(WorkBlock work, uint solution)
{
// Submit this solution to bitcoin
string data = work.GetSolutionString(solution);
Console.WriteLine("Trying solution: " + data);
BitnetClient bc = new BitnetClient("http://" + mURL + ":" + mPort);
bc.Credentials = new NetworkCredential(mUser, mPass);
bool success = bc.GetWork(data);
if (!success)
{
data = work.GetSolutionString((uint)IPAddress.HostToNetworkOrder((int)solution));
success = bc.GetWork(data);
}
return success;
}
public override bool NewBlockReady()
{
return mNewBlockReady;
}
void ThreadFunc()
{
while (true)
{
try
{
if (mURL != null && mPort != 0 && mUser != null && mPass != null)
{
BitnetClient bc = new BitnetClient("http://" + mURL + ":" + mPort);
bc.Credentials = new NetworkCredential(mUser, mPass);
int blockCount = bc.GetBlockCount();
if (blockCount > mBlockCount)
{
mBlockCount = blockCount;
mNewBlockReady = true;
}
}
}
catch (Exception)
{
}
Thread.Sleep(1000 * 10);
}
}
}
}