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-1706] Uses decoder new version #40

Merged
merged 10 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"ext-openssl": "*",
"ext-gmp": "*",
"enjin/platform-core": "*",
"rebing/graphql-laravel": "^9.0.0-rc1",
"rebing/graphql-laravel": "^9.0",
"spatie/laravel-package-tools": "^1.0",
"spatie/laravel-ray": "^1.0",
"phrity/websocket": "^1.0"
Expand Down
3 changes: 3 additions & 0 deletions src/Enums/DispatchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Enjin\Platform\FuelTanks\Models\Substrate\FuelTankRules;
use Enjin\Platform\FuelTanks\Models\Substrate\MaxFuelBurnPerTransactionParams;
use Enjin\Platform\FuelTanks\Models\Substrate\PermittedCallsParams;
use Enjin\Platform\FuelTanks\Models\Substrate\PermittedExtrinsicsParams;
use Enjin\Platform\FuelTanks\Models\Substrate\RequireTokenParams;
use Enjin\Platform\FuelTanks\Models\Substrate\TankFuelBudgetParams;
Expand All @@ -24,6 +25,7 @@ enum DispatchRule: string
case TANK_FUEL_BUDGET = 'TankFuelBudget';
case REQUIRE_TOKEN = 'RequireToken';
case PERMITTED_EXTRINSICS = 'PermittedExtrinsics';
case PERMITTED_CALLS = 'PermittedCalls';
case WHITELISTED_PALLETS = 'WhitelistedPallets';

/**
Expand All @@ -39,6 +41,7 @@ public function toKind(): FuelTankRules
self::TANK_FUEL_BUDGET => new TankFuelBudgetParams('', ''),
self::REQUIRE_TOKEN => new RequireTokenParams('', ''),
self::PERMITTED_EXTRINSICS => new PermittedExtrinsicsParams(),
self::PERMITTED_CALLS => new PermittedCallsParams(),
self::WHITELISTED_PALLETS => new WhitelistedPalletsParams(),
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Events/Substrate/FuelTanks/AccountRemoved.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AccountRemoved extends PlatformBroadcastEvent
/**
* Create a new event instance.
*/
public function __construct(Model $fuelTankAccount)
public function __construct(Model $fuelTankAccount, ?Model $transaction = null)
{
parent::__construct();

Expand Down
2 changes: 1 addition & 1 deletion src/Events/Substrate/FuelTanks/CallDispatched.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CallDispatched extends PlatformBroadcastEvent
/**
* Create a new event instance.
*/
public function __construct(Model $fuelTank, Model $caller)
public function __construct(Model $fuelTank, Model $caller, ?Model $transaction = null)
{
parent::__construct();

Expand Down
2 changes: 1 addition & 1 deletion src/Events/Substrate/FuelTanks/FreezeStateMutated.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FreezeStateMutated extends PlatformBroadcastEvent
/**
* Create a new event instance.
*/
public function __construct(Model $fuelTank)
public function __construct(Model $fuelTank, ?Model $transaction = null)
{
parent::__construct();

Expand Down
2 changes: 1 addition & 1 deletion src/Events/Substrate/FuelTanks/FuelTankDestroyed.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FuelTankDestroyed extends PlatformBroadcastEvent
/**
* Create a new event instance.
*/
public function __construct(Model $fuelTank)
public function __construct(Model $fuelTank, ?Model $transaction = null)
{
parent::__construct();

Expand Down
2 changes: 1 addition & 1 deletion src/Events/Substrate/FuelTanks/FuelTankMutated.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FuelTankMutated extends PlatformBroadcastEvent
/**
* Create a new event instance.
*/
public function __construct(Model $fuelTank)
public function __construct(Model $fuelTank, ?Model $transaction = null)
{
parent::__construct();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MutateFreezeStateScheduled extends PlatformBroadcastEvent
/**
* Create a new event instance.
*/
public function __construct(Model $fuelTank)
public function __construct(Model $fuelTank, ?Model $transaction = null)
{
parent::__construct();

Expand Down
2 changes: 1 addition & 1 deletion src/Events/Substrate/FuelTanks/RuleSetRemoved.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RuleSetRemoved extends PlatformBroadcastEvent
/**
* Create a new event instance.
*/
public function __construct(Model $fuelTank, int $ruleSetId)
public function __construct(Model $fuelTank, int $ruleSetId, ?Model $transaction = null)
{
parent::__construct();

Expand Down
1 change: 0 additions & 1 deletion src/GraphQL/Mutations/InsertRuleSetMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public function resolve(
Substrate $blockchainService
) {
$dispatchRules = $blockchainService->getDispatchRulesParams($args['dispatchRules']);

$encodedData = $serializationService->encode(
$this->getMutationName(),
static::getEncodableParams(
Expand Down
9 changes: 9 additions & 0 deletions src/Models/Substrate/AccountRulesParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(
*/
public function fromEncodable(mixed $params): self
{

if (!empty($whitelist = Arr::get($params, 'WhitelistedCallers'))) {
$this->whitelistedCallers = WhitelistedCallersParams::fromEncodable($whitelist);
}
Expand Down Expand Up @@ -48,4 +49,12 @@ public function toEncodable(): array

return $params;
}

public function toArray(): array
{
return [
'WhitelistedCallers' => $this->whitelistedCallers?->toArray(),
'RequireToken' => $this->requireToken?->toArray(),
];
}
}
49 changes: 45 additions & 4 deletions src/Models/Substrate/DispatchRulesParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(
public function fromEncodable(int $setId, mixed $params): self
{
$this->ruleSetId = $setId;
$this->isFrozen = $params['isFrozen'];
$this->isFrozen = $params['isFrozen'] ?? false;

foreach ($params['rules'] as $rule) {
$ruleParam = '\Enjin\Platform\FuelTanks\Models\Substrate\\' . ($ruleName = array_key_first($rule)) . 'Params';
Expand Down Expand Up @@ -76,13 +76,54 @@ public function toEncodable(): array
$params[] = $this->whitelistedPallets->toEncodable();
}

// We have to set an empty array for the permitted extrinsics here and encode manually later
// due to what appears to be a bug in the Scale Codec library where it cannot encode a Call
// type due to missing metadata when creating the Call ScaleInstance class.
if ($this->permittedExtrinsics) {
$params[] = ['PermittedExtrinsics' => ['extrinsics' => []]];
}


return $params;
}

public function toArray(): array
{
$params = [];

if ($this->whitelistedCallers) {
$params[] = $this->whitelistedCallers->toArray();
}

if ($this->requireToken) {
$params[] = $this->requireToken->toArray();
}

if ($this->whitelistedCollections) {
$params[] = $this->whitelistedCollections->toArray();
}

if ($this->maxFuelBurnPerTransaction) {
$params[] = $this->maxFuelBurnPerTransaction->toArray();
}

if ($this->userFuelBudget) {
$params[] = $this->userFuelBudget->toArray();
}

if ($this->tankFuelBudget) {
$params[] = $this->tankFuelBudget->toArray();
}

if ($this->whitelistedPallets) {
$params[] = $this->whitelistedPallets->toArray();
}

if ($this->permittedCalls) {
$params[] = $this->permittedCalls->toArray();
}

if ($this->permittedExtrinsics) {
$params[] = $this->permittedExtrinsics->toArray();
}

return $params;
}
}
7 changes: 7 additions & 0 deletions src/Models/Substrate/MaxFuelBurnPerTransactionParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ public function toEncodable(): array
'MaxFuelBurnPerTransaction' => $this->max,
];
}

public function toArray(): array
{
return [
'MaxFuelBurnPerTransaction' => $this->max,
];
}
}
21 changes: 17 additions & 4 deletions src/Models/Substrate/PermittedCallsParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

namespace Enjin\Platform\FuelTanks\Models\Substrate;

use Enjin\BlockchainTools\HexConverter;
use Illuminate\Support\Arr;

class PermittedCallsParams extends FuelTankRules
{
protected ?array $calls;

/**
* Creates a new instance.
*/
public function __construct(
public ?array $calls = [],
) {
public function __construct(?array $calls = [])
{
$this->calls = array_map(
fn ($call) => HexConverter::prefix(is_string($call) ? $call : HexConverter::bytesToHex($call)),
$calls
);
}

/**
Expand All @@ -20,7 +26,7 @@ public function __construct(
public static function fromEncodable(array $params): self
{
return new self(
calls: Arr::get($params, 'PermittedCalls.calls')
calls: Arr::get($params, 'PermittedCalls.calls') ?? Arr::get($params, 'PermittedCalls') ?? []
);
}

Expand All @@ -33,4 +39,11 @@ public function toEncodable(): array
'PermittedCalls' => $this->calls,
];
}

public function toArray(): array
{
return [
'PermittedCalls' => $this->calls,
];
}
}
71 changes: 51 additions & 20 deletions src/Models/Substrate/PermittedExtrinsicsParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,56 @@

use Enjin\BlockchainTools\HexConverter;
use Enjin\Platform\Facades\TransactionSerializer;
use Enjin\Platform\Services\Processor\Substrate\Codec\Encoder as BaseEncoder;
use Enjin\Platform\Interfaces\PlatformBlockchainTransaction;
use Enjin\Platform\Package;
use Enjin\Platform\Services\Processor\Substrate\Codec\Encoder as BaseEncoder;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class PermittedExtrinsicsParams extends FuelTankRules
{
protected ?array $extrinsics;

/**
* Creates a new instance.
*/
public function __construct(
public ?array $extrinsics = [],
) {
public function __construct(?array $extrinsics = [])
{
$this->extrinsics = array_map(
function ($extrinsic) {
if (($palletName = Arr::get($extrinsic, 'palletName')) && ($methodName = Arr::get($extrinsic, 'extrinsicName'))) {
return HexConverter::hexToString($palletName) . '.' . HexConverter::hexToString($methodName);
}

$palletName = array_key_first($extrinsic);
$methodName = array_key_first($extrinsic[$palletName]);

return $palletName . '.' . $methodName;
},
$extrinsics
);
}

public function fromMethods(array $methods): self
{
return new self(
extrinsics: array_map(
fn ($method) => [
explode('.', Arr::get(BaseEncoder::getCallIndexKeys(), $method))[0] => [
explode('.', Arr::get(BaseEncoder::getCallIndexKeys(), $method))[1] => null,
],
],
$methods
)
);
}

public function toMethods(): array
{
return array_map(
fn ($extrinsic) => collect(BaseEncoder::getCallIndexKeys())->filter(fn ($item) => $item == $extrinsic)->keys()->first(),
$this->extrinsics
);
}

/**
Expand All @@ -26,31 +62,26 @@ public function __construct(
public static function fromEncodable(array $params): self
{
return new self(
extrinsics: array_map(
fn ($extrinsic) => is_string($extrinsic) ? $extrinsic :
collect(BaseEncoder::getCallIndexKeys())
->filter(
fn ($item) => $item
==
sprintf(
'%s.%s',
HexConverter::hexToString(Arr::get($extrinsic, 'palletName')),
HexConverter::hexToString(Arr::get($extrinsic, 'extrinsicName')),
),
)->keys()->first(),
Arr::get($params, 'PermittedExtrinsics.extrinsics', [])
),
extrinsics: Arr::get($params, 'PermittedExtrinsics.extrinsics') ?? Arr::get($params, 'PermittedExtrinsics')
);
}

public function toArray(): array
{
return [
'PermittedExtrinsics' => $this->extrinsics,
];
}

/**
* Returns the encodable representation of this instance.
*/
public function toEncodable(): array
{
$methods = $this->toMethods();
$encodedData = '07'; // TODO: This should come from the metadata and not hardcode it.
$encodedData .= HexConverter::intToHex(count($this->extrinsics) * 4);
$encodedData .= collect($this->extrinsics)->reduce(fn ($data, $mutation) => Str::of($data)->append($this->getEncodedData($mutation))->toString(), '');
$encodedData .= HexConverter::intToHex(count($methods) * 4);
$encodedData .= collect($methods)->reduce(fn ($data, $mutation) => Str::of($data)->append($this->getEncodedData($mutation))->toString(), '');

return [
'PermittedExtrinsics' => ['extrinsics' => $encodedData],
Expand Down
15 changes: 13 additions & 2 deletions src/Models/Substrate/RequireTokenParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ public function __construct(
*/
public static function fromEncodable(array $params): self
{
$collectionId = Arr::get($params, 'RequireToken.collectionId') ?? Arr::get($params, 'RequireToken.collection_id');
$tokenId = Arr::get($params, 'RequireToken.tokenId') ?? Arr::get($params, 'RequireToken.token_id');

return new self(
collectionId: gmp_strval(Arr::get($params, 'RequireToken.collectionId')),
tokenId: gmp_strval(Arr::get($params, 'RequireToken.tokenId')),
collectionId: gmp_strval($collectionId),
tokenId: gmp_strval($tokenId),
);
}

Expand All @@ -36,4 +39,12 @@ public function toEncodable(): array
'tokenId' => $this->tokenId,
]];
}

public function toArray(): array
{
return ['RequireToken' => [
'collectionId' => $this->collectionId,
'tokenId' => $this->tokenId,
]];
}
}
12 changes: 12 additions & 0 deletions src/Models/Substrate/TankFuelBudgetParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ public function toEncodable(): array
],
];
}

public function toArray(): array
{
return [
'TankFuelBudget' => [
'amount' => $this->amount,
'resetPeriod' => $this->resetPeriod,
'totalConsumed' => $this->totalConsumed,
'lastResetBlock' => $this->lastResetBlock,
],
];
}
}
Loading
Loading