Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLA-2045] Improve query performance #273

Merged
merged 7 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions database/factories/TransactionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function definition()
'method' => fake()->randomElement((new TransactionMethodEnum())->getAttributes()['values']),
'idempotency_key' => fake()->uuid(),
'signed_at_block' => fake()->numberBetween(),
'managed' => true,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Enjin\Platform\Support\Account;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('transactions', function (Blueprint $table) {
$table->boolean('managed')->default(false)->index()->after('state');
});

DB::statement('
UPDATE transactions SET managed = 1
WHERE wallet_public_key IS NULL
or wallet_public_key IN(?)
', ["'" . implode("','", Account::managedPublicKeys()) . "'"]);
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('transactions', function (Blueprint $table) {
$table->dropColumn('managed');
});
}
};
1 change: 1 addition & 0 deletions src/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function configurePackage(Package $package): void
->hasMigration('upgrade_tokens_table')
->hasMigration('upgrade_collections_table')
->hasMigration('add_index_to_syncables_table')
->hasMigration('add_managed_to_transactions_table')
->hasRoute('enjin-platform')
->hasCommand(Sync::class)
->hasCommand(Ingest::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Enjin\Platform\Models\Transaction;
use Enjin\Platform\Rules\ValidSubstrateAccount;
use Enjin\Platform\Services\Database\TransactionService;
use Enjin\Platform\Support\Account;
use Enjin\Platform\Support\SS58Address;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
Expand Down Expand Up @@ -79,24 +78,16 @@ public function resolve($root, array $args, $context, ResolveInfo $resolveInfo,
$args['network'] ?? false,
fn (Builder $query) => $query->where(
'network',
'=',
$args['network'] === 'relay' ? currentRelay()->name : currentMatrix()->name
),
)
->when(
$args['accounts'] ?? false,
function (Builder $query) use ($args) {
$publicKeys = array_map(fn ($wallet) => SS58Address::getPublicKey($wallet), $args['accounts']);

return $query->whereIn('wallet_public_key', $publicKeys);
},
function (Builder $query): void {
$query->where(
fn ($subquery) => $subquery
->whereIn('wallet_public_key', Account::managedPublicKeys())
->orWhereNull('wallet_public_key')
);
}
fn (Builder $query) => $query->whereIn(
'wallet_public_key',
array_map(fn ($wallet) => SS58Address::getPublicKey($wallet), $args['accounts'])
),
fn (Builder $query) => $query->where('managed', true)
)->cursorPaginateWithTotal('id', $args['first'], false);

if ($args['markAsProcessing'] === true || $args['markAsProcessing'] === null) {
Expand Down
14 changes: 14 additions & 0 deletions src/Models/Laravel/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Enjin\Platform\Models\BaseModel;
use Enjin\Platform\Models\Laravel\Traits\EagerLoadSelectFields;
use Enjin\Platform\Models\Laravel\Traits\Transaction as TransactionMethods;
use Enjin\Platform\Support\Account;
use Enjin\Platform\Support\SS58Address;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -64,6 +65,19 @@ public function __construct(array $attributes = [])
parent::__construct($attributes);
}

public static function boot(): void
{
parent::boot();

static::creating(
fn ($model) => $model->managed = (int) (empty($model->wallet_public_key) || Account::isManaged($model->wallet_public_key)),
);

static::updating(
fn ($model) => $model->managed = (int) (empty($model->wallet_public_key) || Account::isManaged($model->wallet_public_key)),
);
}

/**
* The wallet address attribute accessor.
*/
Expand Down
7 changes: 6 additions & 1 deletion src/Support/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function managedPublicKeys(): array
return Cache::remember(
PlatformCache::MANAGED_ACCOUNTS->key(),
now()->addHour(),
fn () => collect(Wallet::where('managed', '=', true)->get()->pluck('public_key'))
fn () => collect(Wallet::where('managed', true)->pluck('public_key'))
->filter()
->add(static::daemonPublicKey())
->unique()
Expand All @@ -89,4 +89,9 @@ public static function parseAccount(array|string|null $account): ?string

return SS58Address::getPublicKey($account);
}

public static function isManaged(string $publicKey): bool
{
return in_array($publicKey, static::managedPublicKeys());
}
}
Loading