Skip to content

Commit

Permalink
refactor: update delegate aggregates caching (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbarnsley authored Mar 25, 2024
1 parent 275adae commit d563b39
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 32 deletions.
8 changes: 4 additions & 4 deletions app/Console/Commands/CacheDelegateAggregates.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public function handle(DelegateCache $cache): void
{
$aggregate = (new DelegateTotalAggregates())->aggregate();

$cache->setTotalAmounts(fn () => $aggregate->pluck('total_amount', 'generator_public_key')->toArray());
$cache->setTotalAmounts($aggregate->pluck('total_amount', 'generator_public_key')->toArray());

$cache->setTotalFees(fn () => $aggregate->pluck('total_fee', 'generator_public_key')->toArray());
$cache->setTotalFees($aggregate->pluck('total_fee', 'generator_public_key')->toArray());

$cache->setTotalRewards(fn () => $aggregate->pluck('reward', 'generator_public_key')->toArray());
$cache->setTotalRewards($aggregate->pluck('reward', 'generator_public_key')->toArray());

$cache->setTotalBlocks(fn () => $aggregate->pluck('count', 'generator_public_key')->toArray());
$cache->setTotalBlocks($aggregate->pluck('count', 'generator_public_key')->toArray());
}
}
17 changes: 8 additions & 9 deletions app/Services/Cache/DelegateCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use App\Contracts\Cache as Contract;
use App\Services\Cache\Concerns\ManagesCache;
use Closure;
use Illuminate\Cache\TaggedCache;
use Illuminate\Support\Facades\Cache;

Expand All @@ -19,39 +18,39 @@ public function getTotalAmounts(): array
return $this->get('total_amounts', []);
}

public function setTotalAmounts(Closure $callback): array
public function setTotalAmounts(array $data): void
{
return $this->remember('total_amounts', now()->addHour(), $callback);
$this->put('total_amounts', $data);
}

public function getTotalBlocks(): array
{
return $this->get('total_blocks', []);
}

public function setTotalBlocks(Closure $callback): array
public function setTotalBlocks(array $data): void
{
return $this->remember('total_blocks', now()->addHour(), $callback);
$this->put('total_blocks', $data);
}

public function getTotalFees(): array
{
return $this->get('total_fees', []);
}

public function setTotalFees(Closure $callback): array
public function setTotalFees(array $data): void
{
return $this->remember('total_fees', now()->addHour(), $callback);
$this->put('total_fees', $data);
}

public function getTotalRewards(): array
{
return $this->get('total_rewards', []);
}

public function setTotalRewards(Closure $callback): array
public function setTotalRewards(array $data): void
{
return $this->remember('total_rewards', now()->addHour(), $callback);
$this->put('total_rewards', $data);
}

public function getTotalWalletsVoted(): int
Expand Down
20 changes: 10 additions & 10 deletions tests/Feature/Http/Controllers/ShowWalletControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

(new NetworkCache())->setSupply(fn () => '10000000000');

((new DelegateCache())->setTotalAmounts(fn () => [$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalFees(fn () => [$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalRewards(fn () => [$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalBlocks(fn () => [$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalAmounts([$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalFees([$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalRewards([$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalBlocks([$wallet->public_key => '1000000000']));

$this
->get(route('wallet', $wallet))
Expand All @@ -37,10 +37,10 @@

(new NetworkCache())->setSupply(fn () => '10000000000');

((new DelegateCache())->setTotalAmounts(fn () => [$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalFees(fn () => [$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalRewards(fn () => [$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalBlocks(fn () => [$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalAmounts([$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalFees([$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalRewards([$wallet->public_key => '1000000000']));
((new DelegateCache())->setTotalBlocks([$wallet->public_key => '1000000000']));

expect($username)->not->toBeEmpty();

Expand Down Expand Up @@ -196,8 +196,8 @@
],
]);

(new DelegateCache())->setTotalFees(fn () => [$wallet->public_key => 234037456741]);
(new DelegateCache())->setTotalRewards(fn () => [$wallet->public_key => 1000000000001]);
(new DelegateCache())->setTotalFees([$wallet->public_key => 234037456741]);
(new DelegateCache())->setTotalRewards([$wallet->public_key => 1000000000001]);

$this
->get(route('wallet', $wallet))
Expand Down
64 changes: 60 additions & 4 deletions tests/Unit/Console/Commands/CacheDelegateAggregatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,69 @@
declare(strict_types=1);

use App\Console\Commands\CacheDelegateAggregates;
use App\Models\Block;
use App\Models\Wallet;
use App\Services\Cache\DelegateCache;
use Carbon\Carbon;

it('should execute the command', function () {
(new CacheDelegateAggregates())->handle($cache = new DelegateCache());

expect($cache->getTotalAmounts())->toBeArray();
expect($cache->getTotalFees())->toBeArray();
expect($cache->getTotalRewards())->toBeArray();
expect($cache->getTotalBlocks())->toBeArray();
expect($cache->getTotalAmounts())->toBe([]);
expect($cache->getTotalFees())->toBe([]);
expect($cache->getTotalRewards())->toBe([]);
expect($cache->getTotalBlocks())->toBe([]);
});

it('should update cache on each run', function () {
$this->travelTo(Carbon::parse('2024-04-08 13:24:03'));

$cache = new DelegateCache();

expect($cache->getCache()->has(md5('total_amounts')))->toBeFalse();
expect($cache->getCache()->has(md5('total_blocks')))->toBeFalse();
expect($cache->getCache()->has(md5('total_fees')))->toBeFalse();
expect($cache->getCache()->has(md5('total_rewards')))->toBeFalse();

(new CacheDelegateAggregates())->handle($cache);

expect($cache->getTotalAmounts())->toBe([]);
expect($cache->getTotalFees())->toBe([]);
expect($cache->getTotalRewards())->toBe([]);
expect($cache->getTotalBlocks())->toBe([]);

$wallet = Wallet::factory()->activeDelegate()->create([
'attributes' => [
'delegate' => [
'username' => 'delegate_1',
'voteBalance' => 1234037456742,
'producedBlocks' => 12340,
],
],
]);

Block::factory()->create([
'generator_public_key' => $wallet->public_key,
'total_amount' => 123 * 1e8,
'total_fee' => 3 * 1e8,
'reward' => 8 * 1e8,
]);

expect($cache->getCache()->has(md5('total_amounts')))->toBeTrue();
expect($cache->getTotalAmounts())->toBe([]);
expect($cache->getTotalFees())->toBe([]);
expect($cache->getTotalRewards())->toBe([]);
expect($cache->getTotalBlocks())->toBe([]);

(new CacheDelegateAggregates())->handle($cache = new DelegateCache());

expect($cache->getTotalAmounts())->toBe([$wallet->public_key => (string) intval(123 * 1e8)]);
expect($cache->getTotalFees())->toBe([$wallet->public_key => (string) intval(3 * 1e8)]);
expect($cache->getTotalRewards())->toBe([$wallet->public_key => (string) intval(8 * 1e8)]);
expect($cache->getTotalBlocks())->toBe([$wallet->public_key => 1]);

expect($cache->getCache()->has(md5('total_amounts')))->toBeTrue();
expect($cache->getCache()->has(md5('total_blocks')))->toBeTrue();
expect($cache->getCache()->has(md5('total_fees')))->toBeTrue();
expect($cache->getCache()->has(md5('total_rewards')))->toBeTrue();
});
10 changes: 5 additions & 5 deletions tests/Unit/ViewModels/WalletViewModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,32 @@
});

it('should sum up the total forged', function () {
(new DelegateCache())->setTotalFees(fn () => [$this->subject->publicKey() => '1000000000']);
(new DelegateCache())->setTotalRewards(fn () => [$this->subject->publicKey() => '1000000000']);
(new DelegateCache())->setTotalFees([$this->subject->publicKey() => '1000000000']);
(new DelegateCache())->setTotalRewards([$this->subject->publicKey() => '1000000000']);

expect($this->subject->totalForged())->toBeFloat();

assertMatchesSnapshot($this->subject->totalForged());
});

it('should sum up the amount forged', function () {
(new DelegateCache())->setTotalAmounts(fn () => [$this->subject->publicKey() => '1000000000']);
(new DelegateCache())->setTotalAmounts([$this->subject->publicKey() => '1000000000']);

expect($this->subject->amountForged())->toBeInt();

assertMatchesSnapshot($this->subject->amountForged());
});

it('should sum up the fees forged', function () {
(new DelegateCache())->setTotalFees(fn () => [$this->subject->publicKey() => '800000000']);
(new DelegateCache())->setTotalFees([$this->subject->publicKey() => '800000000']);

expect($this->subject->feesForged())->toBeInt();

assertMatchesSnapshot($this->subject->feesForged());
});

it('should sum up the rewards forged', function () {
(new DelegateCache())->setTotalRewards(fn () => [$this->subject->publicKey() => '200000000']);
(new DelegateCache())->setTotalRewards([$this->subject->publicKey() => '200000000']);

expect($this->subject->rewardsForged())->toBeInt();

Expand Down

0 comments on commit d563b39

Please sign in to comment.