From 5da6d1a4727c7067ad5c6ed952555b4a507d0a4e Mon Sep 17 00:00:00 2001 From: ItsANameToo <35610748+ItsANameToo@users.noreply.github.com> Date: Tue, 1 Dec 2020 10:59:37 +0100 Subject: [PATCH] fix: missed blocks count (#645) --- app/DTO/Slot.php | 4 ++-- tests/Unit/DTO/SlotTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/DTO/Slot.php b/app/DTO/Slot.php index 246d53aa4d..704cf4fb15 100644 --- a/app/DTO/Slot.php +++ b/app/DTO/Slot.php @@ -4,7 +4,6 @@ namespace App\DTO; -use App\Actions\CacheNetworkHeight; use App\Services\Monitor\Monitor; use App\ViewModels\WalletViewModel; use Carbon\Carbon; @@ -106,11 +105,12 @@ public function keepsMissing(): bool public function missedCount(): int { + // TODO: this means it will incorrectly show 0 for delegates that missed every block since they got voted in if ($this->getLastHeight() === 0) { return 0; } - return abs(CacheNetworkHeight::execute() - $this->getLastHeight()); + return $this->roundNumber - Monitor::roundNumberFromHeight($this->getLastHeight()); } public function isDone(): bool diff --git a/tests/Unit/DTO/SlotTest.php b/tests/Unit/DTO/SlotTest.php index ba76b87773..ec18366920 100644 --- a/tests/Unit/DTO/SlotTest.php +++ b/tests/Unit/DTO/SlotTest.php @@ -56,3 +56,20 @@ expect($subject->keepsMissing())->toBeFalse(); expect($subject->missedCount())->toBe(0); }); + +it('should show the correct missed blocks amount when spanning multiple rounds', function () { + configureExplorerDatabase(); + + $wallet = Wallet::factory()->create(); + + $subject = new Slot([ + 'publicKey' => $wallet->public_key, + 'last_block' => [ + 'publicKey' => $wallet->public_key, + 'height' => 1, + ], + 'status' => 'done', + ], Block::whereBetween('height', [1, 5])->get(), 10); + + expect($subject->missedCount())->toBe(9); +});