Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio committed Nov 28, 2023
2 parents d273018 + 5a9fd34 commit 637ac44
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 65 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/code_analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -25,7 +25,7 @@ jobs:
coverage: none

- name: Checkout friendsofphp php-cs-fixer code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: "friendsofphp/php-cs-fixer"
ref: "v3.13.1"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/security_checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ssh-key: ${{ secrets.SSH_PHP_COMMONS }}

Expand All @@ -27,7 +27,7 @@ jobs:
coverage: none

- name: Checkout sensiolabs security-checker code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: 'sensiolabs/security-checker'
path: 'security-checker'
Expand Down
2 changes: 0 additions & 2 deletions src/GraphQL/Mutations/InsertRuleSetMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Enjin\Platform\FuelTanks\GraphQL\Traits\HasFuelTankValidationRules;
use Enjin\Platform\FuelTanks\Models\Substrate\DispatchRulesParams;
use Enjin\Platform\FuelTanks\Rules\IsFuelTankOwner;
use Enjin\Platform\FuelTanks\Rules\RuleSetNotExists;
use Enjin\Platform\FuelTanks\Services\Blockchain\Implemetations\Substrate;
use Enjin\Platform\GraphQL\Schemas\Primary\Substrate\Traits\StoresTransactions;
use Enjin\Platform\GraphQL\Schemas\Primary\Traits\HasTransactionDeposit;
Expand Down Expand Up @@ -148,7 +147,6 @@ protected function rules(array $args = []): array
'bail',
new MinBigInt(),
new MaxBigInt(Hex::MAX_UINT32),
new RuleSetNotExists(),
],
...$this->dispatchRules($args, '', false),
];
Expand Down
6 changes: 3 additions & 3 deletions src/GraphQL/Traits/HasFuelTankValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ protected function commonRules(string $attribute, array $args = []): array
"{$attribute}.amount" => [
'bail',
new MinBigInt(),
new MaxBigInt(),
new MaxBigInt(Hex::MAX_UINT128),
],
"{$attribute}.resetPeriod" => [
'bail',
new MinBigInt(),
new MaxBigInt(),
new MaxBigInt(Hex::MAX_UINT32),
],
],
default => [
Expand All @@ -44,7 +44,7 @@ protected function commonRules(string $attribute, array $args = []): array
'bail',
'required_with:' . str_replace('collectionId', 'tokenId', $key),
new MinBigInt(),
new MaxBigInt(),
new MaxBigInt(Hex::MAX_UINT128),
Rule::exists('collections', 'collection_chain_id'),
];
})
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL/Types/Input/FuelBudgetInputType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function fields(): array
'description' => __('enjin-platform-fuel-tanks::input_type.fuel_budget.field.amount'),
],
'resetPeriod' => [
'type' => GraphQL::type('Int!'),
'type' => GraphQL::type('BigInt!'),
'description' => __('enjin-platform-fuel-tanks::input_type.fuel_budget.field.resetPeriod'),
],
];
Expand Down
20 changes: 17 additions & 3 deletions src/Models/Substrate/PermittedExtrinsicsParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Enjin\Platform\Facades\TransactionSerializer;
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;

Expand All @@ -25,7 +26,20 @@ public function __construct(
public static function fromEncodable(array $params): self
{
return new self(
extrinsics: Arr::get($params, 'PermittedExtrinsics')
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', [])
),
);
}

Expand All @@ -34,7 +48,7 @@ public static function fromEncodable(array $params): self
*/
public function toEncodable(): array
{
$encodedData = '07';
$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(), '');

Expand All @@ -43,7 +57,7 @@ public function toEncodable(): array
];
}

protected function getEncodedData(string $mutationName)
protected function getEncodedData(string $mutationName): string
{
$transactionMutation = Package::getClassesThatImplementInterface(PlatformBlockchainTransaction::class)
->filter(fn ($class) => Str::contains(class_basename($class), $mutationName))->first();
Expand Down
9 changes: 7 additions & 2 deletions src/Rules/IsFuelTankOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

use Closure;
use Enjin\Platform\FuelTanks\Models\FuelTank;
use Enjin\Platform\Rules\Traits\HasDataAwareRule;
use Enjin\Platform\Support\Account;
use Enjin\Platform\Support\SS58Address;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Arr;

class IsFuelTankOwner implements ValidationRule
class IsFuelTankOwner implements DataAwareRule, ValidationRule
{
use HasDataAwareRule;

/**
* Run the validation rule.
*
Expand All @@ -24,7 +29,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
return;
}

if (!SS58Address::isSameAddress($fuelTank->owner->public_key, Account::daemonPublicKey())) {
if (!SS58Address::isSameAddress($fuelTank->owner->public_key, Arr::get($this->data, 'signingAccount') ?? Account::daemonPublicKey())) {
$fail('enjin-platform-fuel-tanks::validation.is_fuel_tank_owner')->translate();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,29 @@ public function run(PolkadartEvent $event, Block $block, Codec $codec): void
$extrinsic = $block->extrinsics[$event->extrinsicIndex];
$daemonTransaction = Transaction::firstWhere(['transaction_chain_hash' => $extrinsic->hash]);

Log::info(
sprintf(
'FuelTankAccount %s (id: %s) of FuelTank %s (id: %s) was created from transaction %s (id: %s).',
$account->public_key,
$fuelTankAccount->id,
$fuelTank->public_key,
$fuelTank->id,
$daemonTransaction->transaction_chain_hash,
$daemonTransaction->id
)
);
if ($daemonTransaction) {
Log::info(
sprintf(
'FuelTankAccount %s (id: %s) of FuelTank %s (id: %s) was created from transaction %s (id: %s).',
$account->public_key,
$fuelTankAccount->id,
$fuelTank->public_key,
$fuelTank->id,
$daemonTransaction->transaction_chain_hash,
$daemonTransaction->id
)
);
} else {
Log::info(
sprintf(
'FuelTankAccount %s (id: %s) of FuelTank %s (id: %s) was created from unknown transaction.',
$account->public_key,
$fuelTankAccount->id,
$fuelTank->public_key,
$fuelTank->id,
)
);
}

AccountAddedEvent::safeBroadcast(
$fuelTankAccount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function run(PolkadartEvent $event, Block $block, Codec $codec): void
$params = $extrinsic->params;

// TODO: Removes tracking data associated to a dispatch rule (doesn't remove the rule)
// Not sure how to do that yet, we would have to knew the rule structure on chain.
// Not sure how to do that yet, we would have to know the rule structure on chain.
// Maybe we should query the storage for this one? That would make it slower though
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,25 @@ public function run(PolkadartEvent $event, Block $block, Codec $codec): void

$daemonTransaction = Transaction::firstWhere(['transaction_chain_hash' => $extrinsic->hash]);

Log::info(
sprintf(
'FuelTank %s (id: %s) was created from transaction %s (id: %s)',
$fuelTank->public_key,
$fuelTank->id,
$daemonTransaction->transaction_chain_hash,
$daemonTransaction->id
)
);
if ($daemonTransaction) {
Log::info(
sprintf(
'FuelTank %s (id: %s) was created from transaction %s (id: %s)',
$fuelTank->public_key,
$fuelTank->id,
$daemonTransaction->transaction_chain_hash,
$daemonTransaction->id
)
);
} else {
Log::info(
sprintf(
'FuelTank %s (id: %s) was created from unknown transaction',
$fuelTank->public_key,
$fuelTank->id,
)
);
}

FuelTankCreatedEvent::safeBroadcast(
$fuelTank,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Enjin\Platform\Services\Processor\Substrate\Codec\Polkadart\PolkadartEvent;
use Enjin\Platform\Services\Processor\Substrate\Events\SubstrateEvent;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;

class RuleSetInserted implements SubstrateEvent
{
Expand Down Expand Up @@ -46,10 +47,30 @@ public function run(PolkadartEvent $event, Block $block, Codec $codec): void
'updated_at' => $now,
];
}
DispatchRule::insert($insertRules);

$daemonTransaction = Transaction::firstWhere(['transaction_chain_hash' => $extrinsic->hash]);

DispatchRule::insert($insertRules);
if ($daemonTransaction) {
Log::info(
sprintf(
'RuleSetInserted at FuelTank %s (id: %s) from transaction %s (id: %s).',
$fuelTank->public_key,
$fuelTank->id,
$daemonTransaction->transaction_chain_hash,
$daemonTransaction->id
)
);
} else {
Log::info(
sprintf(
'RuleSetInserted at FuelTank %s (id: %s) from unknown transaction.',
$fuelTank->public_key,
$fuelTank->id,
)
);
}

RuleSetInsertedEvent::safeBroadcast(
$fuelTank,
$insertRules,
Expand Down
8 changes: 4 additions & 4 deletions src/Services/Processor/Substrate/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function tanksStorages(array $data): void
$insertData = [];
$insertRules = [];
foreach ($data as [$key, $tank]) {
$tankKey = $this->codec->decode()->tankStorageKey($key);
$tankData = $this->codec->decode()->tankStorageData($tank);
$tankKey = $this->codec->decoder()->tankStorageKey($key);
$tankData = $this->codec->decoder()->tankStorageData($tank);

$ownerWallet = $this->getCachedWallet(
$owner = $tankData['owner'],
Expand Down Expand Up @@ -85,8 +85,8 @@ public function accountsStorages(array $data): void
{
$insertData = [];
foreach ($data as [$key, $fuelTankAccount]) {
$accountKey = $this->codec->decode()->fuelTankAccountStorageKey($key);
$accountData = $this->codec->decode()->fuelTankAccountStorageData($fuelTankAccount);
$accountKey = $this->codec->decoder()->fuelTankAccountStorageKey($key);
$accountData = $this->codec->decoder()->fuelTankAccountStorageData($fuelTankAccount);

$userWallet = $this->getCachedWallet(
$user = $accountKey['userAccount'],
Expand Down
14 changes: 5 additions & 9 deletions tests/Feature/GraphQL/Mutations/CreateFuelTankTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public function test_it_will_fail_with_invalid_parameter_dispatch_rules_user_fue
'message' => 'Variable "$dispatchRules" got invalid value "Invalid" at "dispatchRules[0].userFuelBudget.amount"; Cannot represent following value as uint256: "Invalid"',
],
1 => [
'message' => 'Variable "$dispatchRules" got invalid value "Invalid" at "dispatchRules[0].userFuelBudget.resetPeriod"; Int cannot represent non-integer value: "Invalid"',
'message' => 'Variable "$dispatchRules" got invalid value "Invalid" at "dispatchRules[0].userFuelBudget.resetPeriod"; Cannot represent following value as uint256: "Invalid"',
],
],
$response['errors']
Expand All @@ -464,7 +464,7 @@ public function test_it_will_fail_with_invalid_parameter_dispatch_rules_user_fue
'message' => 'Variable "$dispatchRules" got invalid value 1.1579208923732E+77 at "dispatchRules[0].userFuelBudget.amount"; Cannot represent following value as uint256: 1.1579208923732E+77',
],
1 => [
'message' => 'Variable "$dispatchRules" got invalid value 1.1579208923732E+77 at "dispatchRules[0].userFuelBudget.resetPeriod"; Int cannot represent non 32-bit signed integer value: 1.1579208923732E+77',
'message' => 'Variable "$dispatchRules" got invalid value 1.1579208923732E+77 at "dispatchRules[0].userFuelBudget.resetPeriod"; Cannot represent following value as uint256: 1.1579208923732E+77',
],
],
$response['errors']
Expand All @@ -481,12 +481,8 @@ public function test_it_will_fail_with_invalid_parameter_dispatch_rules_tank_fue
);
$this->assertArraySubset(
[
0 => [
'message' => 'Variable "$dispatchRules" got invalid value "Invalid" at "dispatchRules[0].tankFuelBudget.amount"; Cannot represent following value as uint256: "Invalid"',
],
1 => [
'message' => 'Variable "$dispatchRules" got invalid value "Invalid" at "dispatchRules[0].tankFuelBudget.resetPeriod"; Int cannot represent non-integer value: "Invalid"',
],
['message' => 'Variable "$dispatchRules" got invalid value "Invalid" at "dispatchRules[0].tankFuelBudget.amount"; Cannot represent following value as uint256: "Invalid"'],
['message' => 'Variable "$dispatchRules" got invalid value "Invalid" at "dispatchRules[0].tankFuelBudget.resetPeriod"; Cannot represent following value as uint256: "Invalid"'],
],
$response['errors']
);
Expand All @@ -502,7 +498,7 @@ public function test_it_will_fail_with_invalid_parameter_dispatch_rules_tank_fue
'message' => 'Variable "$dispatchRules" got invalid value 1.1579208923732E+77 at "dispatchRules[0].tankFuelBudget.amount"; Cannot represent following value as uint256: 1.1579208923732E+77',
],
1 => [
'message' => 'Variable "$dispatchRules" got invalid value 1.1579208923732E+77 at "dispatchRules[0].tankFuelBudget.resetPeriod"; Int cannot represent non 32-bit signed integer value: 1.1579208923732E+77',
'message' => 'Variable "$dispatchRules" got invalid value 1.1579208923732E+77 at "dispatchRules[0].tankFuelBudget.resetPeriod"; Cannot represent following value as uint256: 1.1579208923732E+77',
],
],
$response['errors']
Expand Down
Loading

0 comments on commit 637ac44

Please sign in to comment.