Skip to content

Commit 64a21de

Browse files
more fixes and improvements
1 parent 4d1a2fb commit 64a21de

File tree

7 files changed

+169
-95
lines changed

7 files changed

+169
-95
lines changed

miner.php

+7-92
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
use Monolog\Handler\StreamHandler;
55
use Monolog\Logger;
66
use RicardoFiorani\DuinoMiner\Configuration;
7-
use RicardoFiorani\DuinoMiner\DucoS1Miner;
7+
use RicardoFiorani\DuinoMiner\Logic\DuinoMinerLogic;
8+
use RicardoFiorani\DuinoMiner\Miner\DucoS1Miner;
89

910
/**
1011
* Configuration
@@ -20,102 +21,16 @@
2021
*/
2122
$logger = new Logger('name');
2223
$logger->pushHandler(new StreamHandler('php://stdout'));
23-
$logger->alert('STARTING PHP MINER FOR USER '.$configuration->getUsername());
24+
$logger->alert('STARTING PHP MINER', ['user' => $configuration->getUsername()]);
2425

2526
/**
2627
* Miner logic
2728
*/
2829
$loop = React\EventLoop\Factory::create();
29-
$connector = new React\Socket\Connector($loop);
30-
$miner = new DucoS1Miner();
30+
$connector = new React\Socket\Connector($loop, ['timeout' => 200]);
31+
$algorithm = new DucoS1Miner();
32+
$miner = new DuinoMinerLogic();
3133

3234
while (true) {
33-
$logger->alert(sprintf('Connecting to %s...', $configuration->getPoolAddress()));
34-
$connector->connect($configuration->getPoolAddress())->then(
35-
function (React\Socket\ConnectionInterface $connection) use ($logger, $miner, $configuration) {
36-
// connection successfully established
37-
$connection->on('data', function ($data) use ($connection, $logger) {
38-
$logger->info('SERVER SENT: ' . $data);
39-
40-
$arguments = explode(',', $data);
41-
$event = array_shift($arguments);
42-
$event = str_replace(array("\r", "\n"), '', $event);
43-
44-
if (in_array($event, ['OK', 'GOOD', 'BAD'])) {
45-
$connection->emit($event, $arguments);
46-
$logger->info('EMITTING: ' . $event);
47-
return;
48-
}
49-
50-
if(floatval($event) < 5){
51-
$logger->info('Connected to server version: ' . floatval($event));
52-
53-
return;
54-
}
55-
56-
//Then we know its a job
57-
$connection->emit('JOB_INCOMING', explode(',', $data));
58-
});
59-
60-
$connection->on('close', function () use ($logger) {
61-
$logger->warning('[SOCKET CLOSED]');
62-
});
63-
64-
//Based on server response
65-
$connection->on('OK', function () use ($connection) {
66-
$connection->emit('TIME_FOR_NEW_JOB');
67-
});
68-
69-
$connection->on('TIME_FOR_NEW_JOB', function () use ($connection, $configuration) {
70-
$connection->write(sprintf("JOB,%s,MEDIUM", $configuration->getUsername()));
71-
});
72-
73-
$connection->on('GOOD', function () use ($connection, $logger) {
74-
$logger->info('Share Accepted!');
75-
$connection->emit('TIME_FOR_NEW_JOB');
76-
});
77-
78-
$connection->on('BAD', function () use ($connection, $logger) {
79-
$logger->warning('Share NOT ACCEPTED!');
80-
$connection->emit('TIME_FOR_NEW_JOB');
81-
});
82-
83-
$connection->on('JOB_INCOMING', function ($baseHash, $digest, $difficulty) use ($connection, $logger, $miner) {
84-
$logger->info("Starting to work on {$baseHash}:{$digest}:{$difficulty}");
85-
86-
$start = microtime(true);
87-
$result = $miner->work((string)$baseHash, (string)$digest, (int)$difficulty);
88-
$timeDifference = microtime(true) - $start;
89-
90-
$logger->debug('Time difference', [$timeDifference]);
91-
92-
if ($result < 0) {
93-
$logger->alert('Hash Not Found :(');
94-
$connection->emit('TIME_FOR_NEW_JOB');
95-
return;
96-
}
97-
98-
$logger->info('FOUND!', ['result' => $result]);
99-
100-
$hashRate = $result / ($timeDifference);
101-
$logger->info('Current HashRate: ' . round($hashRate, 2) . ' H/s');
102-
$connection->write("$result,$hashRate,{$miner->getName()}");
103-
});
104-
105-
//The knot that ties everything together
106-
$connection->emit('TIME_FOR_NEW_JOB');
107-
},
108-
function (Exception $error) use ($logger) {
109-
$logger->error($error->getMessage());
110-
}
111-
);
112-
113-
$loop->run();
114-
115-
$logger->alert('Sleeping 60 seconds before trying again...');
116-
117-
foreach(range(60, 0) as $i ){
118-
sleep(1);
119-
$logger->info(sprintf('%s seconds before trying again.', $i));
120-
}
35+
$miner->run($logger, $loop, $connector, $algorithm, $configuration);
12136
}
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace RicardoFiorani\DuinoMiner\Logic;
4+
5+
use Exception;
6+
use Psr\Log\LoggerInterface;
7+
use React\EventLoop\LoopInterface;
8+
use React\Socket\ConnectionInterface;
9+
use React\Socket\ConnectorInterface;
10+
use RicardoFiorani\DuinoMiner\Configuration;
11+
use RicardoFiorani\DuinoMiner\Miner\MinerInterface;
12+
use RicardoFiorani\DuinoMiner\Printer\HashRatePrinter;
13+
14+
class DuinoMinerLogic
15+
{
16+
public function run(LoggerInterface $logger, LoopInterface $loop, ConnectorInterface $connector, MinerInterface $algorithm, Configuration $configuration)
17+
{
18+
$logger->alert(sprintf('Connecting to %s...', $configuration->getPoolAddress()));
19+
20+
$connector->connect($configuration->getPoolAddress())->then(
21+
function (ConnectionInterface $connection) use ($logger, $algorithm, $configuration) {
22+
// connection successfully established
23+
$connection->on('data', function ($data) use ($connection, $logger) {
24+
$logger->info('SERVER SENT DATA', [$data]);
25+
26+
$arguments = explode(',', $data);
27+
$event = array_shift($arguments);
28+
$event = str_replace(array("\r", "\n"), '', $event);
29+
30+
if (in_array($event, ['OK', 'GOOD', 'BAD'])) {
31+
$connection->emit($event, $arguments);
32+
$logger->info('EMITTING: ' . $event);
33+
return;
34+
}
35+
36+
//We need to explode again because array_shift changes the original $arguments
37+
$miningArguments = explode(',', $data);
38+
39+
if (count($miningArguments) < 2) {
40+
$logger->info('Connected to server version: ' . floatval($event));
41+
42+
return;
43+
}
44+
45+
//Then we know its a job
46+
$connection->emit('JOB_INCOMING', $miningArguments);
47+
});
48+
49+
$connection->on('close', function () use ($logger) {
50+
$logger->warning('[SOCKET CLOSED]');
51+
});
52+
53+
//Based on server response
54+
$connection->on('OK', function () use ($connection) {
55+
$connection->emit('TIME_FOR_NEW_JOB');
56+
});
57+
58+
$connection->on('TIME_FOR_NEW_JOB', function () use ($connection, $configuration, $logger) {
59+
$logger->info('Asking server for a new job...');
60+
$connection->write(sprintf("JOB,%s,MEDIUM", $configuration->getUsername()));
61+
});
62+
63+
$connection->on('GOOD', function () use ($connection, $logger) {
64+
$logger->info('Share Accepted!');
65+
$connection->emit('TIME_FOR_NEW_JOB');
66+
});
67+
68+
$connection->on('BAD', function () use ($connection, $logger) {
69+
$logger->warning('Share NOT ACCEPTED!');
70+
$connection->emit('TIME_FOR_NEW_JOB');
71+
});
72+
73+
$connection->on('JOB_INCOMING', function ($baseHash, $digest, $difficulty) use ($connection, $logger, $algorithm) {
74+
$logger->info("Starting to work", ['baseHash' => $baseHash, 'digest' => $digest, 'difficulty' => $difficulty]);
75+
76+
$start = microtime(true);
77+
$result = $algorithm->work((string)$baseHash, (string)$digest, (int)$difficulty);
78+
$timeDifference = microtime(true) - $start;
79+
80+
$logger->debug('Time difference', [$timeDifference]);
81+
82+
if ($result < 0) {
83+
$logger->alert('Hash Not Found :(');
84+
$connection->emit('TIME_FOR_NEW_JOB');
85+
return;
86+
}
87+
88+
$logger->info('FOUND!', ['result' => $result]);
89+
90+
$hashRate = $result / ($timeDifference);
91+
$logger->info('Current HashRate', ['hashRate' => HashRatePrinter::print($hashRate)]);
92+
$connection->write("$result,$hashRate,{$algorithm->getName()}");
93+
});
94+
95+
//The knot that ties everything together
96+
$connection->emit('TIME_FOR_NEW_JOB');
97+
},
98+
function (Exception $error) use ($logger) {
99+
$logger->error($error->getMessage());
100+
}
101+
);
102+
103+
$loop->run();
104+
105+
$logger->alert('Sleeping 10 seconds before trying again...');
106+
107+
foreach (range(10, 0) as $i) {
108+
sleep(1);
109+
$logger->info(sprintf('%s seconds before trying again.', $i));
110+
}
111+
}
112+
}

src/DuinoMiner/DucoS1Miner.php src/DuinoMiner/Miner/DucoS1Miner.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @link https://github.com/revoxhere/duino-coin/blob/gh-pages/assets/whitepaper.pdf
77
*/
88

9-
namespace RicardoFiorani\DuinoMiner;
9+
namespace RicardoFiorani\DuinoMiner\Miner;
1010

1111
class DucoS1Miner implements MinerInterface
1212
{

src/DuinoMiner/MinerInterface.php src/DuinoMiner/Miner/MinerInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22

3-
namespace RicardoFiorani\DuinoMiner;
3+
namespace RicardoFiorani\DuinoMiner\Miner;
44

55
interface MinerInterface
66
{
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace RicardoFiorani\DuinoMiner\Printer;
4+
5+
class HashRatePrinter
6+
{
7+
public static function print(float $hashRate): string
8+
{
9+
return match (true) {
10+
$hashRate > 99999 => round($hashRate / 100000, 2) . ' Gh/s',
11+
$hashRate > 9999 => round($hashRate / 10000, 2) . ' Mh/s',
12+
$hashRate > 999 => round($hashRate / 1000, 2) . ' Kh/s',
13+
default => $hashRate . ' h/s',
14+
};
15+
}
16+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace RicardoFiorani\Tests\DuinoMiner;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use RicardoFiorani\DuinoMiner\Miner\DucoS1Miner;
7+
use RicardoFiorani\DuinoMiner\Printer\HashRatePrinter;
8+
9+
class HashRatePrinterTest extends TestCase
10+
{
11+
/**
12+
* @dataProvider hashRateProvider
13+
*/
14+
public function testPrint($input, $expected): void
15+
{
16+
TestCase::assertEquals($expected, HashRatePrinter::print($input));
17+
}
18+
19+
public function hashRateProvider(): array
20+
{
21+
return [
22+
[999, '999 h/s'],
23+
[1000, '1 Kh/s'],
24+
[10000, '1 Mh/s'],
25+
[100000, '1 Gh/s'],
26+
[1000000, '10 Gh/s'],
27+
[251.3, '251.3 h/s'],
28+
[999.9999, '1 Kh/s'],
29+
];
30+
}
31+
}

tests/DuinoMiner/MinerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace RicardoFiorani\Tests\DuinoMiner;
44

55
use PHPUnit\Framework\TestCase;
6-
use RicardoFiorani\DuinoMiner\DucoS1Miner;
6+
use RicardoFiorani\DuinoMiner\Miner\DucoS1Miner;
77

88
class MinerTest extends TestCase
99
{

0 commit comments

Comments
 (0)