Skip to content

Commit

Permalink
[CI] fail if fmt is wrong and check polkadart_example (#382)
Browse files Browse the repository at this point in the history
* [CI] set exit code if files changed after format

* [CI] check example in CI

* fmt

* [CI] generate the files for the example

* [CI] fix polkadart generate

* [CI] not necessary to format generated files as it seems.

* [polkadart_example] add mock test as melos expects it

* fmt
  • Loading branch information
clangenb authored Dec 19, 2023
1 parent bb78de6 commit 5a946fb
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
package:
- polkadart
- polkadart_cli
- polkadart_example
- polkadart_keyring
- polkadart_scale_codec
- ss58
Expand Down Expand Up @@ -50,6 +51,10 @@ jobs:
env:
MELOS_SDK_PATH: ${{ env.FLUTTER_ROOT }}

- name: Generate Files for polkadart_example
if: ${{ matrix.package == 'polkadart_example' }}
run: cd examples && dart run polkadart_cli:generate -v

- name: Check format
run: SCOPE="${{ matrix.package }}" dart run melos format

Expand Down
15 changes: 10 additions & 5 deletions examples/bin/get_account_infos_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ Future<void> main(List<String> arguments) async {
final polkadot = Polkadot(provider);

final accountMapPrefix = polkadot.query.system.accountMapPrefix();
final keys = await polkadot.rpc.state.getKeysPaged(key: accountMapPrefix, count: 10);
final keys =
await polkadot.rpc.state.getKeysPaged(key: accountMapPrefix, count: 10);

print("First 10 account storage keys: ${keys.map((key) => '0x${hex.encode(key)}')}");
print(
"First 10 account storage keys: ${keys.map((key) => '0x${hex.encode(key)}')}");

// Decoding of the keys has to be done manually for now, see how substrate storage keys are defined:
// https://www.shawntabrizi.com/blog/substrate/transparent-keys-in-substrate/
final accountIds = keys.map((key) => const AccountId32Codec().decode(ByteInput(key.sublist(32))));
print("First 10 account pubKeys: ${accountIds.map((account) => '0x${hex.encode(account)}')}");
final accountIds = keys.map(
(key) => const AccountId32Codec().decode(ByteInput(key.sublist(32))));
print(
"First 10 account pubKeys: ${accountIds.map((account) => '0x${hex.encode(account)}')}");

// Get `AccountInfo`s of those keys
final accountInfos = await Future.wait(accountIds.map((account) => polkadot.query.system.account(account)));
final accountInfos = await Future.wait(
accountIds.map((account) => polkadot.query.system.account(account)));

for (final accountInfo in accountInfos) {
print('AccountInfo: ${accountInfo.toJson()}');
Expand Down
9 changes: 9 additions & 0 deletions examples/test/mock_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:test/test.dart';

void main() {
test(
'Mock test, as melos expects at least a test. Otherwise it returns an error.',
() {
expect(true, true);
});
}
2 changes: 1 addition & 1 deletion melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ scripts:
run: dart run melos exec --scope="${SCOPE:-*}" -c 1 --fail-fast -- 'dart run coverage:format_coverage --lcov --in=coverage --out=coverage/coverage.lcov --packages=.dart_tool/package_config.json --report-on=lib'

format:
run: dart run melos exec --scope="${SCOPE:-*}" -c 1 --fail-fast -- 'dart format .'
run: dart run melos exec --scope="${SCOPE:-*}" -c 1 --fail-fast -- 'dart format . --set-exit-if-changed'

analyze:
run: dart run melos exec --scope="${SCOPE:-*}" -c 1 --fail-fast -- 'dart analyze --fatal-infos .'
3 changes: 1 addition & 2 deletions packages/polkadart/lib/apis/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ class StateApi<P extends Provider> {

/// Subscribes to storage changes for the provided keys
Future<StreamSubscription<StorageChangeSet>> subscribeStorage(
List<Uint8List> storageKeys,
Function(StorageChangeSet) onData) async {
List<Uint8List> storageKeys, Function(StorageChangeSet) onData) async {
final hexKeys = storageKeys.map((key) => '0x${hex.encode(key)}').toList();
final subscription = await _provider.subscribe(
'state_subscribeStorage', [hexKeys], onCancel: (subscription) async {
Expand Down
3 changes: 2 additions & 1 deletion packages/polkadart/lib/substrate/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ class StorageDoubleMap<K1, K2, V> {
});

Uint8List hashedKeyFor(K1 key1, K2 key2) {
final Uint8List hash = Uint8List(32 + hasher1.size(key1) + hasher2.size(key2));
final Uint8List hash =
Uint8List(32 + hasher1.size(key1) + hasher2.size(key2));
_hashPrefixTo(key1, output: hash);

final cursor = hash.offsetInBytes + 32 + hasher1.size(key1);
Expand Down
95 changes: 45 additions & 50 deletions packages/polkadart_cli/lib/src/generator/pallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ Class createPalletQueries(
..name = 'key${storage.hashers.indexOf(hasher) + 1}')))
..body = Block((b) => b
// final hashedKey = _storageName.hashedKeyFor(key1);
..statements
.add(declareFinal('hashedKey')
..statements.add(declareFinal('hashedKey')
.assignHashedKey(storageName, storage)
.statement)
// final bytes = await api.queryStorage([hashedKey]);
Expand All @@ -475,44 +474,47 @@ Class createPalletQueries(
storage.isNullable ? Code('') : Code('/* Default */')));
})))
..methods.addAll(generator.storages.map((storage) => Method((builder) {
final storageName = ReCase(storage.name).camelCase;
builder
..name = sanitize(storage.keyMethodName(), recase: false)
..docs.addAll(sanitizeDocs(['Returns the storage key for `$storageName`.']))
..returns = refs.uint8List
..requiredParameters
.addAll(storage.hashers.map((hasher) => Parameter((b) => b
..type = hasher.codec.primitive(dirname)
..name = 'key${storage.hashers.indexOf(hasher) + 1}')))
..body = Block((b) => b
..statements
.add(declareFinal('hashedKey')
.assignHashedKey(storageName, storage)
.statement)
..statements
.add(Code(' return hashedKey;')));
})))
final storageName = ReCase(storage.name).camelCase;
builder
..name = sanitize(storage.keyMethodName(), recase: false)
..docs.addAll(sanitizeDocs(
['Returns the storage key for `$storageName`.']))
..returns = refs.uint8List
..requiredParameters
.addAll(storage.hashers.map((hasher) => Parameter((b) => b
..type = hasher.codec.primitive(dirname)
..name = 'key${storage.hashers.indexOf(hasher) + 1}')))
..body = Block((b) => b
..statements.add(declareFinal('hashedKey')
.assignHashedKey(storageName, storage)
.statement)
..statements.add(Code(' return hashedKey;')));
})))
..methods.addAll(generator.storages
// We don't support maps with depth > 2 yet.
.where((storage) => storage.hashers.isNotEmpty && storage.hashers.length < 3)
.where((storage) =>
storage.hashers.isNotEmpty && storage.hashers.length < 3)
.map((storage) => Method((builder) {
final storageName = ReCase(storage.name).camelCase;
builder
..name = sanitize(storage.mapPrefixMethodName(), recase: false)
..docs.addAll(sanitizeDocs(['Returns the storage map key prefix for `$storageName`.']))
..returns = refs.uint8List
..requiredParameters
.addAll(storage.hashers.getRange(0, storage.hashers.length - 1).map((hasher) => Parameter((b) => b
..type = hasher.codec.primitive(dirname)
..name = 'key${storage.hashers.indexOf(hasher) + 1}')))
..body = Block((b) => b
..statements
.add(declareFinal('hashedKey')
.assignMapPrefix(storageName, storage)
.statement)
..statements
.add(Code(' return hashedKey;')));
})));
final storageName = ReCase(storage.name).camelCase;
builder
..name =
sanitize(storage.mapPrefixMethodName(), recase: false)
..docs.addAll(sanitizeDocs([
'Returns the storage map key prefix for `$storageName`.'
]))
..returns = refs.uint8List
..requiredParameters.addAll(storage.hashers
.getRange(0, storage.hashers.length - 1)
.map((hasher) => Parameter((b) => b
..type = hasher.codec.primitive(dirname)
..name =
'key${storage.hashers.indexOf(hasher) + 1}')))
..body = Block((b) => b
..statements.add(declareFinal('hashedKey')
.assignMapPrefix(storageName, storage)
.statement)
..statements.add(Code(' return hashedKey;')));
})));
});

Class createPalletTxs(
Expand Down Expand Up @@ -597,7 +599,6 @@ Class createPalletConstants(
});

extension MethodNameExtension on Storage {

/// Name of the generated method returning the key of a storage.
String keyMethodName() {
return '${name}Key';
Expand All @@ -612,11 +613,9 @@ extension MethodNameExtension on Storage {
extension AssignHashedKeyExtension on Expression {
Expression assignHashedKey(String storageName, Storage storage) {
return assign(refer('_$storageName')
.property(storage.hashers.isEmpty
? 'hashedKey'
: 'hashedKeyFor')
.call(storage.hashers.map((hasher) => refer(
'key${storage.hashers.indexOf(hasher) + 1}'))));
.property(storage.hashers.isEmpty ? 'hashedKey' : 'hashedKeyFor')
.call(storage.hashers.map(
(hasher) => refer('key${storage.hashers.indexOf(hasher) + 1}'))));
}

Expression assignMapPrefix(String storageName, Storage storage) {
Expand All @@ -632,14 +631,10 @@ extension AssignHashedKeyExtension on Expression {
);
}

return assign(refer('_$storageName')
.property('mapPrefix')
.call(storage.hashers
return assign(refer('_$storageName').property('mapPrefix').call(storage
.hashers
// Checked above that hasher is not empty.
.getRange(0, storage.hashers.length - 1)
.map((hasher) => refer(
'key${storage.hashers.indexOf(hasher) + 1}'))));
.map((hasher) => refer('key${storage.hashers.indexOf(hasher) + 1}'))));
}
}


0 comments on commit 5a946fb

Please sign in to comment.