Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian committed Oct 31, 2020
1 parent 5c3a57a commit 4f614cc
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 48 deletions.
4 changes: 2 additions & 2 deletions app/Console/Commands/CacheDelegateAggregates.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Console\Commands;

use App\Models\Wallet;
use App\Contracts\WalletRepository;
use App\Services\Monitor\Aggregates\TotalAmountsByPublicKeysAggregate;
use App\Services\Monitor\Aggregates\TotalBlocksByPublicKeysAggregate;
use App\Services\Monitor\Aggregates\TotalFeesByPublicKeysAggregate;
Expand Down Expand Up @@ -35,7 +35,7 @@ final class CacheDelegateAggregates extends Command
*/
public function handle()
{
$publicKeys = Wallet::whereNotNull('attributes->delegate->username')->pluck('public_key')->toArray();
$publicKeys = resolve(WalletRepository::class)->allWithUsername()->pluck('public_key')->toArray();

Cache::put('delegates.totalFees', (new TotalFeesByPublicKeysAggregate())->aggregate($publicKeys));

Expand Down
4 changes: 2 additions & 2 deletions app/Console/Commands/CacheDelegates.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Console\Commands;

use App\Models\Wallet;
use App\Contracts\WalletRepository;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

Expand All @@ -31,7 +31,7 @@ final class CacheDelegates extends Command
*/
public function handle()
{
Wallet::whereNotNull('attributes->delegate->username')->orderBy('balance')->chunk(200, function ($wallets): void {
resolve(WalletRepository::class)->allWithUsername()->chunk(200, function ($wallets): void {
foreach ($wallets as $wallet) {
Cache::tags(['delegates'])->put($wallet->public_key, $wallet);
}
Expand Down
8 changes: 3 additions & 5 deletions app/Console/Commands/CacheLastBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Console\Commands;

use App\Contracts\RoundRepository;
use App\Jobs\CacheLastBlockByPublicKey;
use App\Models\Round;
use App\Services\Monitor\Monitor;
Expand Down Expand Up @@ -40,11 +41,8 @@ public function handle()
* look for an exact record which can make use of indices to greatly speed up the process.
*/

Round::query()
->where('round', Monitor::roundNumber())
->orderBy('balance', 'desc')
->orderBy('public_key', 'asc')
->get(['public_key'])
resolve(RoundRepository::class)
->allByRound(Monitor::roundNumber())
->each(fn ($round) => CacheLastBlockByPublicKey::dispatch($round->public_key));
}
}
9 changes: 3 additions & 6 deletions app/Console/Commands/CachePastRoundPerformance.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace App\Console\Commands;

use App\Contracts\RoundRepository;
use App\Jobs\CachePastRoundPerformanceByPublicKey;
use App\Models\Round;
use App\Services\Monitor\Monitor;
use Illuminate\Console\Command;

Expand All @@ -32,11 +32,8 @@ final class CachePastRoundPerformance extends Command
*/
public function handle()
{
Round::query()
->where('round', Monitor::roundNumber())
->orderBy('balance', 'desc')
->orderBy('public_key', 'asc')
->get(['round', 'public_key'])
resolve(RoundRepository::class)
->allByRound(Monitor::roundNumber())
->each(fn ($round) => CachePastRoundPerformanceByPublicKey::dispatch($round->round, $round->public_key));
}
}
9 changes: 3 additions & 6 deletions app/Console/Commands/CacheProductivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace App\Console\Commands;

use App\Contracts\RoundRepository;
use App\Jobs\CacheProductivityByPublicKey;
use App\Models\Round;
use App\Services\Monitor\Monitor;
use Illuminate\Console\Command;

Expand All @@ -32,11 +32,8 @@ final class CacheProductivity extends Command
*/
public function handle()
{
Round::query()
->where('round', Monitor::roundNumber())
->orderBy('balance', 'desc')
->orderBy('public_key', 'asc')
->get(['public_key'])
resolve(RoundRepository::class)
->allByRound(Monitor::roundNumber())
->each(fn ($round) => CacheProductivityByPublicKey::dispatch($round->public_key));
}
}
5 changes: 3 additions & 2 deletions app/Console/Commands/CacheVotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Console\Commands;

use App\Contracts\WalletRepository;
use App\Models\Wallet;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
Expand Down Expand Up @@ -31,9 +32,9 @@ final class CacheVotes extends Command
*/
public function handle()
{
$publicKeys = Wallet::query()
$publicKeys = resolve(WalletRepository::class)
->allWithVote()
->distinct('attributes->vote')
->whereNotNull('attributes->vote')
->pluck('attributes')
->pluck('vote')
->toArray();
Expand Down
10 changes: 10 additions & 0 deletions app/Contracts/WalletRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
namespace App\Contracts;

use App\Models\Wallet;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

interface WalletRepository
{
public function allWithUsername(): Builder;

public function allWithVote(): Builder;

public function allWithPublicKey(): Builder;

public function findByAddress(string $address): Wallet;

public function findByPublicKey(string $publicKey): Wallet;

public function findByPublicKeys(array $publicKey): Collection;

public function findByUsername(string $username): Wallet;
}
7 changes: 5 additions & 2 deletions app/DTO/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\DTO;

use App\Models\Wallet;
use App\Contracts\WalletRepository;
use App\Services\ExchangeRate;
use Illuminate\Support\Arr;

Expand All @@ -23,7 +23,10 @@ public function __construct(int $timestamp, array $payment)
$this->timestamp = $timestamp;
$this->amount = $payment['amount'] / 1e8;
$this->address = $payment['recipientId'];
$this->username = Arr::get(Wallet::where('address', $payment['recipientId'])->firstOrFail(), 'attributes.delegate.username');
$this->username = Arr::get(
resolve(WalletRepository::class)->findByAddress($payment['recipientId']),
'attributes.delegate.username'
);
}

public function amount(): float
Expand Down
12 changes: 10 additions & 2 deletions app/Http/Livewire/MonitorNetwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Http\Livewire;

use App\Contracts\RoundRepository;
use App\DTO\Slot;
use App\Facades\Network;
use App\Jobs\CacheLastBlockByPublicKey;
Expand All @@ -19,13 +20,20 @@

final class MonitorNetwork extends Component
{
private RoundRepository $rounds;

public function mount(): void
{
$this->rounds = resolve(RoundRepository::class);
}

public function render(): View
{
// $tracking = DelegateTracker::execute(Monitor::roundDelegates(112168));

$roundNumber = Monitor::roundNumber();
$roundNumber = $this->rounds->currentRound()->round;
$heightRange = Monitor::heightRangeByRound($roundNumber);
$tracking = DelegateTracker::execute(Monitor::activeDelegates(Monitor::roundNumber()));
$tracking = DelegateTracker::execute($this->rounds->allByRound($roundNumber));

$delegates = [];

Expand Down
4 changes: 2 additions & 2 deletions app/Http/Livewire/WalletTransactionTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace App\Http\Livewire;

use App\Contracts\WalletRepository;
use App\Http\Livewire\Concerns\ManagesTransactionTypeScopes;
use App\Models\Transaction;
use App\Models\Wallet;
use App\ViewModels\ViewModelFactory;
use ARKEcosystem\UserInterface\Http\Livewire\Concerns\HasPagination;
use Illuminate\Database\Eloquent\Builder;
Expand Down Expand Up @@ -58,7 +58,7 @@ public function render(): View
}

return view('livewire.wallet-transaction-table', [
'wallet' => ViewModelFactory::make(Wallet::where('address', $this->state['address'])->firstOrFail()),
'wallet' => ViewModelFactory::make(resolve(WalletRepository::class)->findByAddress($this->state['address'])),
'transactions' => ViewModelFactory::paginate($query->latestByTimestamp()->paginate()),
'countReceived' => $this->getReceivedQuery()->count(),
'countSent' => $this->getSentQuery()->count(),
Expand Down
3 changes: 2 additions & 1 deletion app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Providers;

use App\Contracts\WalletRepository;
use App\Facades\Network;
use App\Models\Wallet;
use ArkEcosystem\Crypto\Identities\Address;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function boot()
abort_unless(Address::validate($value, Network::config()), 404);

try {
return Wallet::where('address', $value)->firstOrFail();
return resolve(WalletRepository::class)->findByAddress($value);
} catch (\Throwable $th) {
UI::useErrorMessage(404, trans('general.wallet_not_found', [$value]));

Expand Down
22 changes: 22 additions & 0 deletions app/Repositories/WalletRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,26 @@

use App\Contracts\WalletRepository as Contract;
use App\Models\Wallet;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

final class WalletRepository implements Contract
{
public function allWithUsername(): Builder
{
return Wallet::whereNotNull('attributes->delegate->username')->orderBy('balance');
}

public function allWithVote(): Builder
{
return Wallet::whereNotNull('attributes->vote')->orderBy('balance');
}

public function allWithPublicKey(): Builder
{
return Wallet::whereNotNull('public_key');
}

public function findByAddress(string $address): Wallet
{
return Wallet::where('address', $address)->firstOrFail();
Expand All @@ -19,6 +36,11 @@ public function findByPublicKey(string $publicKey): Wallet
return Wallet::where('public_key', $publicKey)->firstOrFail();
}

public function findByPublicKeys(array $publicKeys): Collection
{
return Wallet::whereIn('public_key', $publicKeys)->get();
}

public function findByUsername(string $username): Wallet
{
return Wallet::where('attributes->delegate->username', $username)->firstOrFail();
Expand Down
22 changes: 22 additions & 0 deletions app/Repositories/WalletRepositoryWithCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use App\Contracts\WalletRepository;
use App\Models\Wallet;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

final class WalletRepositoryWithCache implements WalletRepository
{
Expand All @@ -18,6 +20,21 @@ public function __construct(WalletRepository $wallets)
$this->wallets = $wallets;
}

public function allWithUsername(): Builder
{
return $this->remember(fn () => $this->wallets->allWithUsername());
}

public function allWithVote(): Builder
{
return $this->remember(fn () => $this->wallets->allWithVote());
}

public function allWithPublicKey(): Builder
{
return $this->remember(fn () => $this->wallets->allWithPublicKey());
}

public function findByAddress(string $address): Wallet
{
return $this->remember(fn () => $this->wallets->findByAddress($address));
Expand All @@ -28,6 +45,11 @@ public function findByPublicKey(string $publicKey): Wallet
return $this->remember(fn () => $this->wallets->findByPublicKey($publicKey));
}

public function findByPublicKeys(array $publicKeys): Collection
{
return $this->remember(fn () => $this->wallets->findByPublicKeys($publicKeys));
}

public function findByUsername(string $username): Wallet
{
return $this->remember(fn () => $this->wallets->findByUsername($username));
Expand Down
14 changes: 2 additions & 12 deletions app/Services/Monitor/Monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,14 @@

namespace App\Services\Monitor;

use App\Contracts\RoundRepository;
use App\Facades\Network;
use App\Models\Round;
use Illuminate\Support\Collection;

final class Monitor
{
public static function activeDelegates(int $round): Collection
{
return Round::query()
->where('round', $round)
->orderBy('balance', 'desc')
->orderBy('public_key', 'asc')
->get();
}

public static function roundNumber(): int
{
return Round::orderBy('round', 'desc')->firstOrFail()->round;
return resolve(RoundRepository::class)->currentRound()->round;
}

public static function heightRangeByRound(int $round): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

namespace App\ViewModels\Concerns\Transaction;

use App\Models\Wallet;
use App\Contracts\WalletRepository;
use App\Services\MultiSignature;
use App\ViewModels\WalletViewModel;
use ArkEcosystem\Crypto\Identities\Address;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;

trait InteractsWithMultiSignature
{
Expand All @@ -21,9 +20,7 @@ public function multiSignatureWallet(): ?WalletViewModel
return null;
}

return new WalletViewModel(
Cache::remember("multiSignatureWallet:$address", 60, fn () => Wallet::where('address', $address)->firstOrFail())
);
return new WalletViewModel(resolve(WalletRepository::class)->findByAddress($address));
}

public function multiSignatureAddress(): ?string
Expand Down Expand Up @@ -54,7 +51,7 @@ public function participants(): array

return collect(Arr::get($this->transaction->asset, 'multiSignature.publicKeys', []))
->map(fn ($publicKey) => Address::fromPublicKey($publicKey))
->map(fn ($address) => Cache::remember("participant:$address", 60, fn () => Wallet::where('address', $address)->firstOrFail()))
->map(fn ($address) => resolve(WalletRepository::class)->findByAddress($address))
->map(fn ($wallet) => new WalletViewModel($wallet))
->toArray();
}
Expand Down

0 comments on commit 4f614cc

Please sign in to comment.