Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio committed Oct 7, 2024
1 parent 5fa2fc5 commit 9df7065
Show file tree
Hide file tree
Showing 57 changed files with 256 additions and 222 deletions.
74 changes: 51 additions & 23 deletions src/Commands/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Enjin\Platform\Commands;

use Amp\Future;
use Amp\Serialization\SerializationException;
use Amp\Sync\ChannelException;
use Carbon\Carbon;
Expand Down Expand Up @@ -30,9 +29,6 @@
use Symfony\Component\Console\Helper\ProgressBar;
use Throwable;

use function Amp\async;
use function Amp\Parallel\Context\contextFactory;

class Sync extends Command
{
/**
Expand Down Expand Up @@ -175,30 +171,62 @@ protected function getStorageAt(string $blockHash): array
$storageKeys = $this->getStorageKeys($blockHash);
$progress = $this->createAndStartDebugBar(count($storageKeys));

$storages = Future\await(
array_map(
fn ($keyAndHash) => async(function () use ($progress, $keyAndHash) {
try {
$storageKey = $keyAndHash[0];

$context = contextFactory()->start(__DIR__ . '/contexts/get_storage.php');
$context->send($keyAndHash);
[$storage, $total] = $context->join();
$rpc = new SubstrateSocketClient();

$storages = array_map(
function ($keyAndHash) use ($rpc, $progress) {
try {
$storageKey = $keyAndHash[0];
$blockHash = $keyAndHash[1];

$total = 0;
$storageValues = [];

while (true) {
try {
$keys = $rpc->send(
'state_getKeysPaged',
[
$storageKey->value,
1000,
$startKey ?? null,
$blockHash,
]
);
} catch (Throwable) {
continue;
}

if (empty($keys)) {
break;
}

$storage = $rpc->send(
'state_queryStorageAt',
[
$keys,
$blockHash,
]
);
$storageValues[] = Arr::get($storage, '0.changes');
$total += count($keys);
$startKey = Arr::last($keys);
}

$this->newLine();
$this->info('Finished to fetch: ' . $storageKey->type->name . ' storage');
$this->newLine();
$this->info('Finished to fetch: ' . $storageKey->type->name . ' storage');

$progress->advance();
$progress->advance();

return [$storageKey, $storage, $total];
} catch (SerializationException|ChannelException $e) {
throw new PlatformException("Failed to sync: {$e->getMessage()}");
}
}),
$storageKeys,
)
return [$storageKey, $storageValues, $total];
} catch (SerializationException|ChannelException $e) {
throw new PlatformException("Failed to sync: {$e->getMessage()}");
}
},
$storageKeys,
);

$rpc->close();
$progress->finish();

return $storages;
Expand Down
4 changes: 2 additions & 2 deletions src/Services/Processor/Substrate/BlockProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function subscribeToNewHeads(): void

PlatformBlockIngesting::dispatch($block);

$this->info(sprintf('Ingested header for block #%s in %s seconds', $blockNumber, now()->diffInMilliseconds($syncTime) / 1000));
$this->info(sprintf('Ingested header for block #%s in %s seconds', $blockNumber, $syncTime->diffInMilliseconds(now()) / 1000));

$this->fetchEvents($block);
$this->fetchExtrinsics($block);
Expand Down Expand Up @@ -193,7 +193,7 @@ public function process(Block $block): ?Block
}

$block->fill(['synced' => true, 'failed' => false, 'exception' => null])->save();
$this->info(sprintf("Process completed for block #{$blockNumber} in %s seconds", now()->diffInMilliseconds($syncTime) / 1000));
$this->info(sprintf("Process completed for block #{$blockNumber} in %s seconds", $syncTime->diffInMilliseconds(now()) / 1000));
} catch (Throwable $exception) {
$this->error("Failed processing block #{$blockNumber}");
$block->fill(['synced' => true, 'failed' => true, 'exception' => $exception->getMessage()])->save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->who = Account::parseAccount($self->getValue($data, ['who', 'T::AccountId']));
$self->free = $self->getValue($data, ['free', 'T::Balance']);
$self->who = Account::parseAccount($self->getValue($data, 'T::AccountId'));
$self->free = $self->getValue($data, 'T::Balance');

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->who = Account::parseAccount($self->getValue($data, ['who', 'T::AccountId']));
$self->amount = $self->getValue($data, ['amount', 'T::Balance']);
$self->who = Account::parseAccount($self->getValue($data, 'T::AccountId'));
$self->amount = $self->getValue($data, 'T::Balance');

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->account = Account::parseAccount($self->getValue($data, ['account', 'T::AccountId']));
$self->amount = $self->getValue($data, ['amount', 'T::Balance']);
$self->account = Account::parseAccount($self->getValue($data, 'T::AccountId'));
$self->amount = $self->getValue($data, 'T::Balance');

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->account = Account::parseAccount($self->getValue($data, ['account', 'T::AccountId']));
$self->freeBalance = $self->getValue($data, ['free_balance', 'T::Balance']);
$self->account = Account::parseAccount($self->getValue($data, 'T::AccountId'));
$self->freeBalance = $self->getValue($data, 'T::Balance');

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->from = Account::parseAccount($self->getValue($data, ['from', '0']));
$self->to = Account::parseAccount($self->getValue($data, ['to', '1']));
$self->amount = $self->getValue($data, ['amount', '2']);
$self->destinationStatus = $self->getValue($data, ['destination_status', '3']);
$self->from = Account::parseAccount($self->getValue($data, 0));
$self->to = Account::parseAccount($self->getValue($data, 1));
$self->amount = $self->getValue($data, 2);
$self->destinationStatus = $self->getValue($data, 3);

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->who = Account::parseAccount($self->getValue($data, ['who', 'T::AccountId']));
$self->amount = $self->getValue($data, ['amount', 'T::Balance']);
$self->who = Account::parseAccount($self->getValue($data, 'T::AccountId'));
$self->amount = $self->getValue($data, 'T::Balance');

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->who = Account::parseAccount($self->getValue($data, ['who', 'T::AccountId']));
$self->amount = $self->getValue($data, ['amount', 'T::Balance']);
$self->who = Account::parseAccount($self->getValue($data, 'T::AccountId'));
$self->amount = $self->getValue($data, 'T::Balance');

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->from = Account::parseAccount($self->getValue($data, ['from', '0']));
$self->to = Account::parseAccount($self->getValue($data, ['to', '1']));
$self->amount = $self->getValue($data, ['amount', '2']);
$self->from = Account::parseAccount($self->getValue($data, 0));
$self->to = Account::parseAccount($self->getValue($data, 1));
$self->amount = $self->getValue($data, 2);

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->who = Account::parseAccount($self->getValue($data, ['who', 'T::AccountId']));
$self->amount = $self->getValue($data, ['amount', 'T::Balance']);
$self->who = Account::parseAccount($self->getValue($data, 'T::AccountId'));
$self->amount = $self->getValue($data, 'T::Balance');

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->who = Account::parseAccount($self->getValue($data, ['who', 'T::AccountId']));
$self->amount = $self->getValue($data, ['amount', 'T::Balance']);
$self->who = Account::parseAccount($self->getValue($data, 'T::AccountId'));
$self->amount = $self->getValue($data, 'T::Balance');

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ public function toBroadcast(?array $with = null): array
];
}

public function getValue(array $data, array $keys): mixed
public function getValue(array $data, array|string|int $keys): mixed
{
if (!is_array($keys)) {
$keys = [$keys];
}

foreach ($keys as $key) {
if (Arr::has($data, $k = "event.{$this->getKey($key)}")) {
return Arr::get($data, $k);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->tankId = Account::parseAccount($self->getValue($data, ['tank_id', '0']));
$self->userId = Account::parseAccount($self->getValue($data, ['user_id', '1']));
$self->tankDeposit = $self->getValue($data, ['tank_deposit', '2']);
$self->userDeposit = $self->getValue($data, ['user_deposit', '3']);
$self->totalReceived = $self->getValue($data, ['total_received', '4']);
$self->tankId = Account::parseAccount($self->getValue($data, 0));
$self->userId = Account::parseAccount($self->getValue($data, 1));
$self->tankDeposit = $self->getValue($data, 2);
$self->userDeposit = $self->getValue($data, 3);
$self->totalReceived = $self->getValue($data, 4);

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->tankId = Account::parseAccount($self->getValue($data, ['tank_id', '0']));
$self->userId = Account::parseAccount($self->getValue($data, ['user_id', '1']));
$self->tankId = Account::parseAccount($self->getValue($data, 0));
$self->userId = Account::parseAccount($self->getValue($data, 1));

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->tankId = Account::parseAccount($self->getValue($data, ['tank_id', '0']));
$self->userId = Account::parseAccount($self->getValue($data, ['user_id', '1']));
$self->ruleSetId = $self->getValue($data, ['rule_set_id', '2']);
$self->ruleKind = $self->getValue($data, ['rule_kind', '3']);
$self->tankId = Account::parseAccount($self->getValue($data, 0));
$self->userId = Account::parseAccount($self->getValue($data, 1));
$self->ruleSetId = $self->getValue($data, 2);
$self->ruleKind = $self->getValue($data, 3);

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->caller = Account::parseAccount($self->getValue($data, ['caller', '0']));
$self->tankId = Account::parseAccount($self->getValue($data, ['tank_id', '1']));
$self->caller = Account::parseAccount($self->getValue($data, 0));
$self->tankId = Account::parseAccount($self->getValue($data, 1));

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->tankId = Account::parseAccount($self->getValue($data, ['tank_id', 'T::AccountId']));
$self->userId = Account::parseAccount($self->getValue($data, ['user_id', 'Option<T::AccountId>']));
$self->consumption = $self->getValue($data, ['consumption', 'ConsumptionOf<T>']);
$self->tankId = Account::parseAccount($self->getValue($data, 'T::AccountId'));
$self->userId = Account::parseAccount($self->getValue($data, 'Option<T::AccountId>'));
$self->consumption = $self->getValue($data, 'ConsumptionOf<T>');

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->tankId = Account::parseAccount($self->getValue($data, ['tank_id', 'T::AccountId']));
$self->ruleSetId = $self->getValue($data, ['rule_set_id.Some', 'Option<T::RuleSetId>']);
$self->isFrozen = $self->getValue($data, ['is_frozen', 'bool']) ?? false;
$self->tankId = Account::parseAccount($self->getValue($data, 'T::AccountId'));
$self->ruleSetId = $self->getValue($data, 'Option<T::RuleSetId>');
$self->isFrozen = $self->getValue($data, 'bool') ?? false;

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->owner = Account::parseAccount($self->getValue($data, ['owner', '0']));
$self->tankName = HexConverter::hexToString(is_string($value = $self->getValue($data, ['name', '1'])) ? $value : HexConverter::bytesToHex($value));
$self->tankId = HexConverter::prefix(is_string($value = $self->getValue($data, ['tank_id', '2'])) ? $value : HexConverter::bytesToHex($value));
$self->owner = Account::parseAccount($self->getValue($data, 0));
$self->tankName = HexConverter::hexToString(is_string($value = $self->getValue($data, 1)) ? $value : HexConverter::bytesToHex($value));
$self->tankId = HexConverter::prefix(is_string($value = $self->getValue($data, 2)) ? $value : HexConverter::bytesToHex($value));

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->tankId = Account::parseAccount($self->getValue($data, ['tank_id', 'T::AccountId']));
$self->tankId = Account::parseAccount($self->getValue($data, 'T::AccountId'));

return $self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ public static function fromChain(array $data): self
$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->tankId = Account::parseAccount($self->getValue($data, ['tank_id', 'T::AccountId']));
$self->tankId = Account::parseAccount($self->getValue($data, 'T::AccountId'));

$self->userAccountManagement = is_bool($b = $self->getValue($data, ['mutation.user_account_management.SomeMutation', 'T::TankMutation.user_account_management.SomeMutation']))
$self->userAccountManagement = is_bool($b = $self->getValue($data, 'T::TankMutation.user_account_management.SomeMutation'))
? ['tankReservesAccountCreationDeposit' => $b]
: $b;

$self->providesDeposit = is_bool($b = $self->getValue($data, ['mutation.provides_deposit.Some', 'T::TankMutation.provides_deposit', 'T::TankMutation.coverage_policy']))
? $b
: $b === 'FeesAndDeposit';
$self->accountRules = $self->getValue($data, ['mutation.account_rules.Some', 'T::TankMutation.account_rules']);
$self->providesDeposit = $self->getValue($data, 'T::TankMutation.coverage_policy') === 'FeesAndDeposit';
$self->accountRules = $self->getValue($data, 'T::TankMutation.account_rules');

return $self;
}
Expand Down
Loading

0 comments on commit 9df7065

Please sign in to comment.