forked from arionum/miner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminer
More file actions
executable file
·438 lines (384 loc) · 11.1 KB
/
miner
File metadata and controls
executable file
·438 lines (384 loc) · 11.1 KB
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
#!/usr/bin/php
<?php
namespace Arionum\Miner;
error_reporting(0);
ini_set("memory_limit", "5G");
$type = trim($argv[1]);
$node = trim($argv[2]);
$public_key = trim($argv[3]);
$private_key = trim($argv[4]);
new Miner($type, $node, $public_key, $private_key);
/**
* Class Miner
*/
class Miner
{
/**
* The current version of the miner.
*/
public const VERSION = 'v0.4a';
/**
* The mode name for pool mining.
*/
public const MODE_POOL = 'pool';
/**
* The mode name for solo mining.
*/
public const MODE_SOLO = 'solo';
/**
* The response status for OK.
*/
public const NODE_STATUS_OK = 'ok';
/**
* @var string
*/
private $publicKey;
/**
* @var string
*/
private $privateKey;
/**
* @var float
*/
private $speed;
/**
* @var float
*/
private $avgSpeed;
/**
* @var string
*/
private $node;
/**
* @var string
*/
private $block;
/**
* @var int
*/
private $difficulty;
/**
* @var int
*/
private $counter;
/**
* @var int
*/
private $allTime;
/**
* @var int
*/
private $beginTime;
/**
* @var string
*/
private $type;
/**
* @var int
*/
private $limit;
/**
* @var string
*/
private $worker;
/**
* @var int
*/
private $lastUpdate;
/**
* @var int
*/
private $submit;
/**
* @var int
*/
private $confirm;
/**
* @var int
*/
private $found;
/**
* @var int
*/
private $height;
/**
* @var bool
*/
private $testnet;
/**
* Miner constructor.
*
* @param $type
* @param $node
* @param $public_key
* @param $private_key
*/
public function __construct($type, $node, $public_key, $private_key)
{
$this->outputHeader();
$this->checkDependencies();
if (empty($type) || empty($public_key) || empty($node) || ($type == self::MODE_SOLO && empty($private_key))) {
echo "Usage:
For Solo mining: ./miner solo <node> <public_key> <private_key>
For Pool mining: ./miner pool <pool-address> <your-address>\n\n";
exit;
}
if ($type == self::MODE_POOL) {
$private_key = $public_key;
// Check address validity
$dst_b = $this->base58Decode($public_key);
if (strlen($dst_b) != 64) {
die("ERROR: Invalid Arionum address!");
}
}
$worker = uniqid();
$this->prepare($public_key, $private_key, $node, $type, $worker);
$res = $this->update();
if (!$res) {
die("ERROR: Could not get mining info from the node");
}
$this->run();
}
/**
* @param string $publicKey
* @param string $privateKey
* @param string $node
* @param string $type
* @param string $worker
*/
public function prepare(string $publicKey, string $privateKey, string $node, string $type, string $worker)
{
$this->publicKey = $publicKey;
$this->privateKey = $privateKey;
$this->node = $node;
$this->type = $type;
$this->worker = $worker;
$this->counter = 0;
$this->submit = 0;
$this->confirm = 0;
$this->found = 0;
}
/**
* @return bool
*/
public function update(): bool
{
$this->lastUpdate = time();
echo "--> Updating mining info\n";
$extra = "";
if ($this->type == self::MODE_POOL) {
$extra = "&worker=".$this->worker."&address=".$this->privateKey."&hashrate=".$this->speed;
}
$res = file_get_contents($this->node."/mine.php?q=info".$extra);
$info = json_decode($res, true);
if ($info['status'] != self::NODE_STATUS_OK) {
return false;
}
$data = $info['data'];
$this->block = $data['block'];
$this->difficulty = $data['difficulty'];
$this->limit = 240;
if ($this->type === self::MODE_POOL) {
$this->limit = $data['limit'];
$this->publicKey = $data['public_key'];
}
$this->height = $data['height'];
$this->testnet = $data['testnet'];
if ($this->testnet) {
$this->limit = 100000000000000;
}
echo "Min DL: ".$this->limit."\n";
return true;
}
/**
* @param string $nonce
* @param string $argon
* @return bool
*/
private function submit(string $nonce, string $argon): bool
{
$argon = ($this->height > 10800 && ($this->height < 80000 || $this->height % 2 == 0)) ?
substr($argon, 30) :
$argon = substr($argon, 29);
echo "--> Submitting nonce $nonce / $argon\n";
$postData = http_build_query(
[
'argon' => $argon,
'nonce' => $nonce,
'private_key' => $this->privateKey,
'public_key' => $this->publicKey,
'address' => $this->privateKey,
]
);
$opts = [
'http' =>
[
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postData,
],
];
$context = stream_context_create($opts);
$res = file_get_contents($this->node."/mine.php?q=submitNonce", false, $context);
$data = json_decode($res, true);
if ($data['status'] == self::NODE_STATUS_OK) {
echo "\n--> Nonce confirmed.\n";
return true;
} else {
echo "--> The nonce did not confirm.\n\n";
return false;
}
}
/**
* Run the miner.
*/
public function run()
{
$this->allTime = microtime(true);
$this->beginTime = time();
$it = 0;
$this->counter = 0;
$start = microtime(true);
while (1) {
$this->counter++;
if (time() - $this->lastUpdate > 2) {
echo "--> Last hash rate: ".$this->speed." H/s ".
"Average: ".$this->avgSpeed." H/s ".
"Total hashes: ".$this->counter." ".
"Mining Time: ".(time() - $this->beginTime)." ".
"Shares: ".$this->confirm." ".
"Finds: ".$this->found."\n";
$this->update();
}
$nonce = base64_encode(openssl_random_pseudo_bytes(32));
$nonce = preg_replace("/[^a-zA-Z0-9]/", "", $nonce);
$base = $this->publicKey."-".$nonce."-".$this->block."-".$this->difficulty;
$argon = ($this->height > 10800 && ($this->height < 80000 || $this->height % 2 == 0)) ?
password_hash(
$base,
PASSWORD_ARGON2I,
['memory_cost' => 524288, "time_cost" => 1, "threads" => 1]
) :
$argon = password_hash(
$base,
PASSWORD_ARGON2I,
['memory_cost' => 16384, "time_cost" => 4, "threads" => 4]
);
$hash = $base.$argon;
for ($i = 0; $i < 5; $i++) {
$hash = hash("sha512", $hash, true);
}
$hash = hash("sha512", $hash);
$m = str_split($hash, 2);
$duration = hexdec($m[10]).hexdec($m[15]).hexdec($m[20]).hexdec($m[23]).
hexdec($m[31]).hexdec($m[40]).hexdec($m[45]).hexdec($m[55]);
$duration = ltrim($duration, '0');
$result = gmp_div($duration, $this->difficulty);
if ($result > 0 && $result <= $this->limit) {
$confirmed = $this->submit($nonce, $argon);
if ($this->testnet) {
echo "\nARGON: $argon\n";
echo "\nBase: $base\n";
}
if ($confirmed && $result <= 240) {
$this->found++;
} elseif ($confirmed) {
$this->confirm++;
}
$this->submit++;
if ($this->testnet) {
sleep(240);
}
}
$it++;
if ($it == 10) {
$it = 0;
$end = microtime(true);
$this->speed = 10 / ($end - $start);
$this->avgSpeed = $this->counter / ($end - $this->allTime);
$start = $end;
}
}
}
/**
* @param array $source
* @param mixed $source_base
* @param mixed $target_base
* @return array
*
* @author Mika Tuupola
* @link https://github.com/tuupola/base58
*/
public function baseConvert(array $source, $source_base, $target_base)
{
$result = [];
while ($count = count($source)) {
$quotient = [];
$remainder = 0;
for ($i = 0; $i !== $count; $i++) {
$accumulator = $source[$i] + $remainder * $source_base;
$digit = (integer)($accumulator / $target_base);
$remainder = $accumulator % $target_base;
if (count($quotient) || $digit) {
array_push($quotient, $digit);
};
}
array_unshift($result, $remainder);
$source = $quotient;
}
return $result;
}
/**
* @param mixed $data
* @param bool $integer
* @return int|string
*
* @author Mika Tuupola
* @link https://github.com/tuupola/base58
*/
public function base58Decode($data, $integer = false)
{
$data = str_split($data);
$data = array_map(function ($character) {
$chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
return strpos($chars, $character);
}, $data);
/* Return as integer when requested. */
if ($integer) {
$converted = $this->baseConvert($data, 58, 10);
return (integer)implode("", $converted);
}
$converted = $this->baseConvert($data, 58, 256);
return implode("", array_map(function ($ascii) {
return chr($ascii);
}, $converted));
}
/**
* Check for the required dependencies.
*/
public function checkDependencies()
{
if (!extension_loaded("gmp")) {
die("The GMP PHP extension is missing.");
}
if (!extension_loaded("openssl")) {
die("The OpenSSL PHP extension is missing.");
}
if (floatval(phpversion()) < 7.2) {
die("The minimum PHP version required is 7.2.");
}
if (!defined("PASSWORD_ARGON2I")) {
die("The PHP version is not compiled with argon2i support.");
}
}
/**
* Output the miner header text.
*/
public function outputHeader()
{
echo "######################\n";
echo "# Arionum Miner ".self::VERSION." #\n";
echo "# www.arionum.com #\n";
echo "######################\n\n";
}
}