Skip to content

Commit

Permalink
wip - performance including current round
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbarnsley committed Dec 5, 2023
1 parent bb44965 commit c27b98e
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 58 deletions.
5 changes: 1 addition & 4 deletions app/Console/Commands/CacheDelegatePerformance.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function handle(): void
])
->join('blocks', 'blocks.generator_public_key', '=', 'rounds.public_key');

collect(range($round - 5, $round - 1))
collect(range($round - 2, $round - 1))
->each(function ($round, int $index) use ($query) : void {
[$start, $end] = Monitor::heightRangeByRound($round);

Expand All @@ -62,9 +62,6 @@ public function handle(): void
(new WalletCache())->setPerformance($row['public_key'], [
$row['round_0'],
$row['round_1'],
$row['round_2'],
$row['round_3'],
$row['round_4'],
]);
});
}
Expand Down
3 changes: 3 additions & 0 deletions app/Contracts/RoundRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\Round;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection as SupportCollection;

interface RoundRepository
{
Expand All @@ -16,4 +17,6 @@ interface RoundRepository
public function allByRound(int $round): Collection;

public function current(): int;

public function delegates(bool $withBlock = true): SupportCollection;
}
2 changes: 2 additions & 0 deletions app/Facades/Rounds.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use App\Contracts\RoundRepository;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Collection as SupportCollection;

/**
* @method static Collection allByRound(int $round)
* @method static int current()
* @method static SupportCollection delegates()
*/
final class Rounds extends Facade
{
Expand Down
25 changes: 25 additions & 0 deletions app/Repositories/RoundRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@

use App\Contracts\RoundRepository as Contract;
use App\Facades\Network;
use App\Facades\Rounds;
use App\Models\Block;
use App\Models\Round;
use App\Services\Monitor\DelegateTracker;
use App\Services\Monitor\Monitor;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection as SupportCollection;

final class RoundRepository implements Contract
{
Expand All @@ -25,4 +30,24 @@ public function current(): int
{
return Round::max('round');
}

public function delegates(bool $withBlock = true): SupportCollection
{
$roundNumber = Rounds::current();
$delegates = Rounds::allByRound($roundNumber);
$heightRange = Monitor::heightRangeByRound($roundNumber);
$delegates = new SupportCollection(DelegateTracker::execute($delegates, $heightRange[0]));

if ($withBlock) {
$blocks = Block::whereBetween('height', $heightRange)->get()->keyBy('generator_public_key');

$delegates = $delegates->map(fn ($delegate) => [
...$delegate,

'block' => $blocks->get($delegate['publicKey']),
]);
}

return $delegates;
}
}
10 changes: 10 additions & 0 deletions app/Repositories/RoundRepositoryWithCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
namespace App\Repositories;

use App\Contracts\RoundRepository;
use App\Facades\Rounds;
use App\Models\Block;
use App\Repositories\Concerns\ManagesCache;
use App\Services\Monitor\DelegateTracker;
use App\Services\Monitor\Monitor;
use Illuminate\Cache\TaggedCache;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection as SupportCollection;
use Illuminate\Support\Facades\Cache;

final class RoundRepositoryWithCache implements RoundRepository
Expand All @@ -28,6 +33,11 @@ public function current(): int
return $this->rounds->current();
}

public function delegates(bool $withBlock = true): SupportCollection
{
return $this->rounds->delegates($withBlock);
}

private function getCache(): TaggedCache
{
return Cache::tags('rounds');
Expand Down
2 changes: 1 addition & 1 deletion app/Services/Cache/WalletCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function setLastBlock(string $publicKey, array $blocks): void

public function getPerformance(string $publicKey): array
{
return $this->get(sprintf('performance/%s', $publicKey), []);
return $this->get(sprintf('performance/%s', $publicKey), [true, true]);
}

public function setPerformance(string $publicKey, array $value): void
Expand Down
26 changes: 24 additions & 2 deletions app/ViewModels/Concerns/Wallet/CanForge.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace App\ViewModels\Concerns\Wallet;

use App\Facades\Rounds;
use App\Services\Monitor\Monitor;
use App\Services\Cache\DelegateCache;
use App\Services\Cache\WalletCache;
use App\Services\Monitor\DelegateTracker;
use Illuminate\Support\Arr;

trait CanForge
Expand Down Expand Up @@ -57,7 +60,20 @@ public function performance(): array
return [];
}

return (new WalletCache())->getPerformance($publicKey);
$performance = (new WalletCache())->getPerformance($publicKey);

$currentRound = $this->currentSlot();
dump($currentRound['status']);
if ($currentRound['status'] === 'done') {
$performance = [
$performance[1],
$currentRound['block'] !== null,
];
}

// dump($publicKey, $performance, $currentRound);

return $performance;
}

public function hasForged(): bool
Expand All @@ -80,7 +96,8 @@ public function justMissed(): bool

public function keepsMissing(): bool
{
return array_slice(array_reverse($this->performance()), 0, 2) === [false, false];
dump($this->performance());
return $this->performance() === [false, false];
}

public function forgedBlocks(): int
Expand All @@ -102,4 +119,9 @@ public function missedBlocks(): int

return (new WalletCache())->getMissedBlocks($publicKey);
}

public function currentSlot(): array
{
return Rounds::delegates()->firstWhere('publicKey', $this->publicKey());
}
}
62 changes: 32 additions & 30 deletions tests/Feature/Http/Livewire/DelegateDataBoxesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
declare(strict_types=1);

use App\Enums\DelegateForgingStatus;
use App\Facades\Rounds;
use App\Http\Livewire\DelegateDataBoxes;
use App\Models\Block;
use App\Models\Round;
use App\Models\Wallet;
use App\Services\Cache\NetworkCache;
use App\Services\Cache\WalletCache;
use App\Services\Monitor\Monitor;
use App\ViewModels\WalletViewModel;
use Carbon\Carbon;
use Livewire\Livewire;
Expand All @@ -17,10 +19,10 @@
$this->travelTo(Carbon::parse('2022-08-22 00:00'));
});

function createRoundWithDelegatesAndPerformances(array $performances = null, bool $addBlockForNextRound = true, int $wallets = 51): void
function createRoundWithDelegatesAndPerformances(array $performances = null, bool $addBlockForNextRound = true, int $wallets = 51, int $baseIndex = 0): void
{
Wallet::factory($wallets)->create()->each(function ($wallet, $index) use ($performances, $addBlockForNextRound) {
$timestamp = Carbon::now()->add($index * 8, 'seconds')->timestamp;
Wallet::factory($wallets)->create()->each(function ($wallet, $index) use ($performances, $addBlockForNextRound, $baseIndex) {
$timestamp = Carbon::now()->add(($baseIndex + $index) * 8, 'seconds')->timestamp;

$block = Block::factory()->create([
'height' => 5720529,
Expand All @@ -45,7 +47,7 @@ function createRoundWithDelegatesAndPerformances(array $performances = null, boo
(new WalletCache())->setDelegate($wallet->public_key, $wallet);

if (is_null($performances)) {
for ($i = 0; $i < 5; $i++) {
for ($i = 0; $i < 2; $i++) {
$performances[] = (bool) mt_rand(0, 1);
}
}
Expand Down Expand Up @@ -97,7 +99,7 @@ function createRoundWithDelegatesAndPerformances(array $performances = null, boo
});

it('should determine if delegates are forging based on their round history', function () {
createRoundWithDelegatesAndPerformances([true, true, true, true, true]);
createRoundWithDelegatesAndPerformances([true, true]);

$component = Livewire::test(DelegateDataBoxes::class)
->call('setIsReady');
Expand All @@ -112,7 +114,7 @@ function createRoundWithDelegatesAndPerformances(array $performances = null, boo
});

it('should determine if delegates are not forging based on their round history', function () {
createRoundWithDelegatesAndPerformances([false, false, false, false, false]);
createRoundWithDelegatesAndPerformances([false, false]);

$component = Livewire::test(DelegateDataBoxes::class)
->call('setIsReady');
Expand All @@ -127,7 +129,7 @@ function createRoundWithDelegatesAndPerformances(array $performances = null, boo
});

it('should determine if delegates just missed based on their round history', function () {
createRoundWithDelegatesAndPerformances([true, true, true, true, false]);
createRoundWithDelegatesAndPerformances([true, false]);

$component = Livewire::test(DelegateDataBoxes::class)
->call('setIsReady');
Expand All @@ -142,7 +144,7 @@ function createRoundWithDelegatesAndPerformances(array $performances = null, boo
});

it('should determine if delegates are forging after missing 4 slots based on their round history', function () {
createRoundWithDelegatesAndPerformances([false, false, false, false, true]);
createRoundWithDelegatesAndPerformances([false, true]);

$component = Livewire::test(DelegateDataBoxes::class)
->call('setIsReady');
Expand Down Expand Up @@ -224,7 +226,7 @@ function createRoundWithDelegatesAndPerformances(array $performances = null, boo
});

it('should calculate forging correctly', function ($performances, $addBlockForNextRound) {
createRoundWithDelegatesAndPerformances([true, true, true, true, true], true, 50);
createRoundWithDelegatesAndPerformances([true, true], true, 50);
createRoundWithDelegatesAndPerformances($performances, $addBlockForNextRound, 1);

(new NetworkCache())->setHeight(fn (): int => 4234212);
Expand All @@ -242,22 +244,22 @@ function createRoundWithDelegatesAndPerformances(array $performances = null, boo
'Current Height',
]);
})->with([
'missed, missed, missed, forged, forged' => [
[false, false, false, true, true],
'forged, forged, forged' => [
[true, true],
true,
],
'forged, forged, forged, forged, forged' => [
[true, true, true, true, true],
'missed, forged, forged' => [
[false, true],
true,
],
'missed, forged, missed, forged, forged' => [
[false, true, false, true, true],
'missed, missed, forged' => [
[false, false],
true,
],
]);

it('should calculate missed correctly', function ($performances, $addBlockForNextRound) {
createRoundWithDelegatesAndPerformances([true, true, true, true, true], true, 50);
createRoundWithDelegatesAndPerformances([true, true], true, 50);
createRoundWithDelegatesAndPerformances($performances, $addBlockForNextRound, 1);

(new NetworkCache())->setHeight(fn (): int => 4234212);
Expand All @@ -275,23 +277,23 @@ function createRoundWithDelegatesAndPerformances(array $performances = null, boo
'Current Height',
]);
})->with([
'missed, missed, missed, forged, missed' => [
[false, false, false, true, false],
'forged, forged, missed' => [
[true, true],
false,
],
'forged, forged, forged, forged, missed' => [
[true, true, true, true, false],
false,
],
'missed, forged, missed, forged, missed' => [
[false, true, false, true, false],
'missed, forged, missed' => [
[false, true],
false,
],
]);

it('should calculate not forging correctly', function ($performances, $addBlockForNextRound) {
createRoundWithDelegatesAndPerformances([true, true, true, true, true], true, 50);
createRoundWithDelegatesAndPerformances($performances, $addBlockForNextRound, 1);
createRoundWithDelegatesAndPerformances([true, true], true, 50);
createRoundWithDelegatesAndPerformances($performances, $addBlockForNextRound, 1, 49);

$this->travel(1, 'minute');

dump(Rounds::delegates()->pluck('status', 'publicKey'));

(new NetworkCache())->setHeight(fn (): int => 4234212);

Expand All @@ -308,12 +310,12 @@ function createRoundWithDelegatesAndPerformances(array $performances = null, boo
'Current Height',
]);
})->with([
'missed, missed, missed, missed, missed' => [
[false, false, false, false, false],
'missed, missed, missed' => [
[false, false],
false,
],
'forged, missed, forged, missed, missed' => [
[true, false, true, false, false],
'forged, missed, missed' => [
[true, false],
false,
],
]);
15 changes: 0 additions & 15 deletions tests/Unit/Console/Commands/CacheDelegatePerformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
expect((new WalletCache())->getPerformance($publicKey))->toBe([
true,
true,
true,
true,
true,
]);
});

Expand Down Expand Up @@ -65,9 +62,6 @@

expect(Cache::tags('wallet')->has(md5("performance/$publicKey")))->toBeTrue();
expect((new WalletCache())->getPerformance($publicKey))->toBe([
true,
true,
true,
true,
false,
]);
Expand Down Expand Up @@ -95,9 +89,6 @@

expect(Cache::tags('wallet')->has(md5("performance/$publicKey")))->toBeTrue();
expect((new WalletCache())->getPerformance($publicKey))->toBe([
true,
false,
false,
false,
false,
]);
Expand Down Expand Up @@ -125,9 +116,6 @@

expect(Cache::tags('wallet')->has(md5("performance/$publicKey")))->toBeTrue();
expect((new WalletCache())->getPerformance($publicKey))->toBe([
true,
false,
false,
false,
false,
]);
Expand Down Expand Up @@ -155,9 +143,6 @@

expect(Cache::tags('wallet')->has(md5("performance/$publicKey")))->toBeTrue();
expect((new WalletCache())->getPerformance($publicKey))->toBe([
false,
true,
false,
false,
false,
]);
Expand Down
Loading

0 comments on commit c27b98e

Please sign in to comment.