Skip to content

Commit

Permalink
Handle ingest errors (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio authored Jul 26, 2023
1 parent c9c40d1 commit b7d5563
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/Services/Processor/Substrate/BlockProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Enjin\Platform\Services\Blockchain\Implementations\Substrate;
use Enjin\Platform\Services\Processor\Substrate\Codec\Codec;
use Enjin\Platform\Support\JSON;
use Exception;
use Facades\Enjin\Platform\Services\Processor\Substrate\State;
use Illuminate\Console\BufferedConsoleOutput;
use Illuminate\Console\Concerns\InteractsWithIO;
Expand Down Expand Up @@ -181,15 +182,19 @@ public function process(Block $block): Block|null

$this->info("Processing block #{$blockNumber} ({$block->hash})");

(new EventProcessor($block, $this->codec))->run();
(new ExtrinsicProcessor($block, $this->codec))->run();
$hasEventErrors = (new EventProcessor($block, $this->codec))->run();
$hasExtrinsicErrors = (new ExtrinsicProcessor($block, $this->codec))->run();
if ($hasEventErrors || $hasExtrinsicErrors) {
$errors = implode(';', [...$hasEventErrors, ...$hasExtrinsicErrors]);

throw new Exception($errors);
}

$block->fill(['synced' => true, 'failed' => false, 'exception' => null])->save();
$this->info(sprintf("Process completed for block #{$blockNumber} in %s seconds", now()->diffInMilliseconds($syncTime) / 1000));
} catch (Throwable $exception) {
$this->error("Failed processing block #{$blockNumber}");
$exception = sprintf('%s: %s (Line %s in %s)', get_class($exception), $exception->getMessage(), $exception->getLine(), $exception->getFile());
$block->fill(['synced' => true, 'failed' => true, 'exception' => $exception])->save();
$block->fill(['synced' => true, 'failed' => true, 'exception' => $exception->getMessage()])->save();
}

return $block;
Expand Down
12 changes: 10 additions & 2 deletions src/Services/Processor/Substrate/EventProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Enjin\Platform\Services\Processor\Substrate\Codec\Codec;
use Enjin\Platform\Services\Processor\Substrate\Codec\Polkadart\PolkadartEvent;
use Illuminate\Support\Facades\Log;
use Throwable;

class EventProcessor
{
Expand All @@ -18,14 +19,21 @@ public function __construct(Block $block, Codec $codec)
$this->codec = $codec;
}

public function run(): void
public function run(): array
{
Log::info("Processing Events from block #{$this->block->number}");
$events = $this->block->events ?? [];
$errors = [];

foreach ($events as $event) {
$this->processEvent($event);
try {
$this->processEvent($event);
} catch (Throwable $exception) {
$errors[] = sprintf('%s: %s (Line %s in %s)', get_class($exception), $exception->getMessage(), $exception->getLine(), $exception->getFile());
}
}

return $errors;
}

protected function processEvent(PolkadartEvent $event): void
Expand Down
12 changes: 10 additions & 2 deletions src/Services/Processor/Substrate/ExtrinsicProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Facades\Enjin\Platform\Services\Database\TransactionService;
use Facades\Enjin\Platform\Services\Processor\Substrate\State;
use Illuminate\Support\Facades\Log;
use Throwable;

class ExtrinsicProcessor
{
Expand All @@ -29,14 +30,21 @@ public function __construct(Block $block, Codec $codec)
$this->codec = $codec;
}

public function run()
public function run(): array
{
Log::info("Processing Extrinsics from block #{$this->block->number}");
$extrinsics = $this->block->extrinsics ?? [];
$errors = [];

foreach ($extrinsics as $index => $extrinsic) {
$this->processExtrinsic($extrinsic, $index);
try {
$this->processExtrinsic($extrinsic, $index);
} catch (Throwable $exception) {
$errors[] = sprintf('%s: %s (Line %s in %s)', get_class($exception), $exception->getMessage(), $exception->getLine(), $exception->getFile());
}
}

return $errors;
}

protected function processExtrinsic(PolkadartExtrinsic $extrinsic, int $index)
Expand Down

0 comments on commit b7d5563

Please sign in to comment.