-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcscoin-miner.vala
226 lines (188 loc) · 5.77 KB
/
cscoin-miner.vala
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
using GLib;
[CCode (lower_case_cprefix = "cscoin_")]
namespace CSCoin
{
string generate_command (string command_name, ...)
{
var builder = new Json.Builder ();
builder.begin_object ();
builder.set_member_name ("command");
builder.add_string_value (command_name);
builder.set_member_name ("args");
builder.begin_object ();
var list = va_list ();
unowned string? key, val;
for (key = list.arg<string?> (), val = list.arg<string?> ();
key != null && val != null;
key = list.arg<string?> (), val = list.arg<string?> ())
{
builder.set_member_name (key.replace ("-", "_"));
builder.add_string_value (val);
}
// TODO: add command arguments
builder.end_object ();
builder.end_object ();
return Json.to_string (builder.get_root (), false);
}
/**
* Path to the RSA wallet containing both public and private keys.
*/
string wallet_path;
const OptionEntry[] options =
{
{"wallet", 'w', 0, OptionArg.FILENAME, ref wallet_path, "Path to the wallet.", "FILE"},
{null}
};
int main (string[] args)
{
// default options
wallet_path = "default.pem";
try
{
var parser = new OptionContext ();
parser.add_main_entries (options, null);
parser.parse (ref args);
}
catch (OptionError err)
{
return 1;
}
if (args.length < 2)
{
stderr.printf ("Usage: %s [--wallet=<wallet_file>] <ws_url>\n", args[0]);
return 1;
}
Wallet wallet;
try
{
message ("Loading an existing wallet from '%s'...", wallet_path);
wallet = new Wallet.from_path (wallet_path);
}
catch (Error err)
{
stderr.printf ("Could not read the wallet at '%s'.", wallet_path);
return 1;
}
message ("The 'wallet_id' is '%s'.", wallet.get_wallet_id ());
loop.begin (args[1], wallet);
new MainLoop ().run ();
return 0;
}
async void loop (string ws_url, Wallet wallet)
{
var ws_session = new Soup.Session ();
Soup.WebsocketConnection ws = null;
var challenge_executor = new ThreadPool<Challenge>.with_owned_data ((challenge) => {
message ("Received challenge #%lld: challenge-name: %s, last-solution-hash: %s, hash-prefix: %s.",
challenge.challenge_id,
challenge.challenge_type.to_string (),
challenge.last_solution_hash,
challenge.hash_prefix);
var started = get_monotonic_time ();
string? nonce;
try
{
nonce = solve_challenge (challenge.challenge_id,
challenge.challenge_type,
challenge.last_solution_hash,
challenge.hash_prefix,
challenge.parameters,
challenge.cancellable);
if (nonce == null)
{
message ("Could not solve challenge #%d, waiting until the next one...", challenge.challenge_id);
}
else
{
message ("Solved challenge #%lld in %lldms (%lds was given) with nonce '%s'.",
challenge.challenge_id,
(get_monotonic_time () - started) / 1000,
challenge.time_left,
nonce);
message ("Submitting nonce '%s' for challenge #%lld to authority...", nonce, challenge.challenge_id);
ws.send_text (generate_command ("submission", wallet_id: wallet.get_wallet_id (),
nonce: nonce.to_string ()));
}
}
catch (IOError.CANCELLED err)
{
message ("Challenge #%lld have been cancelled, waiting until the next one...", challenge.challenge_id);
}
catch (Error err)
{
critical ("%s (%s, %d)", err.message, err.domain.to_string (), err.code);
}
}, 1, true);
Challenge? current_challenge = null;
do
{
message ("Establishing a connection with the authority...");
try
{
ws = yield ws_session.websocket_connect_async (new Soup.Message ("GET", ws_url), null, null, null);
}
catch (Error err)
{
critical (err.message);
Idle.add (loop.callback);
yield;
continue;
}
message ("Established a connection with the authority!");
ws.message.connect ((type, payload) => {
var response = Json.from_string ((string) payload.get_data ()).get_object ();
if (response.has_member ("challenge_id"))
{
var challenge = new Challenge.from_json_object (response, new Cancellable ());
if (current_challenge != null)
{
if (challenge.challenge_id == current_challenge.challenge_id)
{
message ("Challenge #%lld is already executing.", current_challenge.challenge_id);
return;
}
else
{
/* cancel any running challenge */
current_challenge.cancellable.cancel ();
}
}
try
{
challenge_executor.add (challenge);
}
catch (ThreadError err)
{
critical ("Could not enqueue the challenge #%lld: %s", challenge.challenge_id, err.message);
return;
}
Timeout.add_seconds (challenge.time_left, () => {
challenge.cancellable.cancel ();
return false;
});
current_challenge = challenge;
}
else if (response.has_member ("error"))
{
critical ("Received an error from CA: %s.", response.get_string_member ("error"));
}
});
ws.closing.connect (() => {
warning ("The connection is closing, cancelling any running challenges...");
});
ws.closed.connect (() => {
message ("The connection is closed and will be reestablished in a few...");
Idle.add (loop.callback);
});
ws.error.connect ((err) => {
critical (err.message);
});
ws.send_text (generate_command ("register_wallet", name: "AEDIROUM",
key: wallet.get_key (),
signature: wallet.get_signature ()));
ws.send_text (generate_command ("get_current_challenge"));
yield;
}
while (true);
}
}