Skip to content

Commit

Permalink
Impl Constants (leonardocustodio#306)
Browse files Browse the repository at this point in the history
* constants impl Constants - New Approach leonardocustodio#220

* some variable renaming

* few bug fixes

* docs

* requested changes

* Constant<T>
  • Loading branch information
justkawal authored Mar 28, 2023
1 parent 772d7d4 commit 7ce1215
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 64 deletions.
2 changes: 1 addition & 1 deletion packages/polkadart_scale_codec/lib/io/byte_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ByteOutput with Output {
@override
void write(List<int> bytes) {
if (_buffer.length - cursor < bytes.length) {
_buffer.replaceRange(cursor, cursor + bytes.length, bytes);
_buffer.insertAll(cursor, bytes);
} else {
_buffer.setRange(cursor, cursor + bytes.length, bytes);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/substrate_metadata/lib/models/chain_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ part of models;
class ChainInfo {
final int version;
final ScaleCodec scaleCodec;
final Map<String, Map<String, Constant>> constants;

ChainInfo({
required this.version,
required this.scaleCodec,
required this.constants,
});

static ChainInfo fromMetadata(DecodedMetadata metadata,
Expand All @@ -16,6 +18,6 @@ class ChainInfo {
'Legacy types are required for metadata versions below 14');
return LegacyParser(metadata, legacyTypes!).getChainInfo();
}
return V14Parser.getChainInfo(metadata);
return V14Parser(metadata).getChainInfo();
}
}
27 changes: 27 additions & 0 deletions packages/substrate_metadata/lib/models/constant.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
part of models;

///
/// Metadata represeting one pallet constant.
class Constant<T> {
///
/// The constant type codec suitable for decoding the constant bytes
final Codec<T> type;

///
/// Value stored in the constant (SCALE encoded).
final Uint8List bytes;

///
/// Documentation of the constant.
final List<String> docs;

///
/// Creates a constant object model
const Constant({required this.type, required this.bytes, required this.docs});

///
/// Decoded constant value from the bytes using the type codec
T get value {
return type.decode(Input.fromBytes(bytes));
}
}
1 change: 1 addition & 0 deletions packages/substrate_metadata/lib/models/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ part 'decoded_block/decoded_block_events.model.dart';
part 'decoded_block/decoded_block_extrinsics.model.dart';
part 'raw_block/raw_block.model.dart';
part 'raw_block/raw_block_events.model.dart';
part 'constant.dart';
111 changes: 75 additions & 36 deletions packages/substrate_metadata/lib/parsers/legacy_parser.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
part of parsers;

/// FilterTypedef helps to filter the modules that are not needed
typedef FilterTypedef = dynamic Function(AnyLegacyModule mod);

/// PalletTypedef helps to call the function for each module and passes the module and the index
typedef PalletTypedef = void Function(AnyLegacyModule mod, int index);

class LegacyParser {
final DecodedMetadata decodedMetadata;
final LegacyTypes legacyTypes;

final _resultingRegistry = Registry();

LegacyParser(this.decodedMetadata, this.legacyTypes);
late final TypeNormalizer _typeNormalizer;

LegacyParser(this.decodedMetadata, this.legacyTypes) {
_typeNormalizer = TypeNormalizer(
types: legacyTypes.types,
typesAlias: legacyTypes.typesAlias ?? <String, Map<String, String>>{},
);
}

ChainInfo getChainInfo() {
final rawMetadata = decodedMetadata.metadataJson;
Expand All @@ -21,11 +34,6 @@ class LegacyParser {
final genericCallsCodec = <int, MapEntry<String, Codec>>{};
final genericEventsCodec = <int, MapEntry<String, Codec>>{};

final typeNormalizer = TypeNormalizer(
types: legacyTypes.types,
typesAlias: legacyTypes.typesAlias ?? <String, Map<String, String>>{},
);

for (final module in rawMetadata['modules']) {
// Getting the module name and converting it to camelCase
final String moduleName = module['name']!;
Expand All @@ -49,11 +57,7 @@ class LegacyParser {
// List<String>
final codecs = <Codec>[];
for (final String arg in args) {
final String parsedType =
typeNormalizer.normalize(arg, moduleName).toString();

final Codec codec = _resultingRegistry.parseSpecificCodec(
legacyTypes.types, parsedType);
final codec = _getCodecFromType(arg, moduleName);
codecs.add(codec);
}
argsCodec = TupleCodec(codecs);
Expand All @@ -65,13 +69,7 @@ class LegacyParser {
final String name = arg['name'];
final String argType = arg['type'];

final String parsedType =
typeNormalizer.normalize(argType, moduleName).toString();

final Codec codec = _resultingRegistry.parseSpecificCodec(
legacyTypes.types, parsedType);

codecs[name] = codec;
codecs[name] = _getCodecFromType(argType, moduleName);
}
argsCodec = CompositeCodec(codecs);
} else {
Expand All @@ -84,10 +82,6 @@ class LegacyParser {
MapEntry(module['name'], ComplexEnumCodec.sparse(callEnumCodec));
}

//
// Events Parsing
final eventRegistry = Registry();

if (module['events'] != null) {
eventModuleIndex++;

Expand All @@ -108,11 +102,7 @@ class LegacyParser {
// List<String>
final codecs = <Codec>[];
for (final String arg in args) {
final String parsedType =
typeNormalizer.normalize(arg, moduleName).toString();

final Codec codec = eventRegistry.parseSpecificCodec(
legacyTypes.types, parsedType);
final codec = _getCodecFromType(arg, moduleName);
codecs.add(codec);
}
argsCodec = TupleCodec(codecs);
Expand All @@ -124,13 +114,7 @@ class LegacyParser {
final String name = arg['name'];
final String argType = arg['type'];

final String parsedType =
typeNormalizer.normalize(argType, moduleName).toString();

final Codec codec = eventRegistry.parseSpecificCodec(
legacyTypes.types, parsedType);

codecs[name] = codec;
codecs[name] = _getCodecFromType(argType, moduleName);
}
argsCodec = CompositeCodec(codecs);
} else {
Expand Down Expand Up @@ -170,8 +154,17 @@ class LegacyParser {
_createExtrinsicCodec();

return ChainInfo(
scaleCodec: ScaleCodec(_resultingRegistry),
version: decodedMetadata.version);
scaleCodec: ScaleCodec(_resultingRegistry),
version: decodedMetadata.version,
constants: _constants(),
);
}

Codec _getCodecFromType(String type, String? moduleName) {
final String parsedType =
_typeNormalizer.normalize(type, moduleName).toString();

return _resultingRegistry.parseSpecificCodec(legacyTypes.types, parsedType);
}

void _createExtrinsicCodec() {
Expand Down Expand Up @@ -204,6 +197,21 @@ class LegacyParser {
_resultingRegistry.addCodec('ExtrinsicSignatureCodec', extrinsicCodec);
}

Map<String, Map<String, Constant>> _constants() {
final constants = <String, Map<String, Constant>>{};
_forEachPallet(pallet: (module, _) {
for (final constant in module.constants) {
constants[module.name] ??= <String, Constant>{};

constants[module.name]![constant.name] = Constant(
type: _getCodecFromType(constant.type, module.name),
bytes: constant.value,
docs: constant.docs);
}
});
return constants;
}

void _defineCalls() {
_defineGenericLookUpSource();
}
Expand All @@ -222,4 +230,35 @@ class LegacyParser {
_resultingRegistry.addCodec(
'GenericLookupSource', ComplexEnumCodec.sparse(enums));
}

void _forEachPallet({
required PalletTypedef pallet,
FilterTypedef? filter,
}) {
final metadata = decodedMetadata.metadataObject;
switch (metadata.version) {
case 9:
case 10:
case 11:
var index = 0;
for (final module in metadata.value.modules) {
if (filter != null && filter(module) == null) {
continue;
}
pallet(module, index);
index += 1;
}
return;

case 12:
case 13:
for (final module in metadata.value.modules) {
if (filter != null && filter(module) == null) {
continue;
}
pallet(module, module.index);
}
return;
}
}
}
Loading

0 comments on commit 7ce1215

Please sign in to comment.