Open Lohann opened 1 month ago
Here are some key observations to aid the review process:
โฑ๏ธ Estimated effort to review: 4 ๐ต๐ต๐ต๐ตโช |
๐งช No relevant tests |
๐ No security concerns identified |
โก Recommended focus areas for review Code Complexity The file contains a large amount of code (over 1000 lines), which includes complex classes and methods. This could impact maintainability and readability. Consider refactoring into smaller, more manageable components. Possible Redundancy The file seems to be defining classes and methods that are potentially duplicated or similar to those in other parts of the project. Review for possible consolidation or removal to avoid redundancy. Inconsistent Naming There are inconsistencies in naming conventions and potential typographical errors in variable and class names. Standardizing naming conventions would improve code clarity and consistency. |
Explore these optional code suggestions:
Category | Suggestion | Score |
Possible bug |
Correct the method call to pass the output stream in encoding___ **In theencodeTo method of $TypeDefCompositeCodec , ensure that the encode method of $TypeDefCompositeCodec is called with the output parameter to correctly pass the output stream.** [packages/substrate_metadata/lib/scale_info/type_definition.dart [115]](https://github.com/leonardocustodio/polkadart/pull/487/files#diff-dfd2daaf996cb19132ec190538042c9be7a1c3c08473db576068e53cadc284b7R115-R115) ```diff -$TypeDefCompositeCodec._().encode(value as TypeDefComposite); +$TypeDefCompositeCodec._().encodeTo(value as TypeDefComposite, output); ``` Suggestion importance[1-10]: 9Why: This suggestion corrects a potential bug by ensuring the correct method is called with the necessary parameters, which is crucial for the proper functioning of the encoding process. | 9 |
Improve error handling during hex string decoding___ **Add error handling for potential decoding failures when parsing the hex string toensure the application does not crash and provides a meaningful error message.** [packages/substrate_metadata/lib/metadata/common.dart [18-26]](https://github.com/leonardocustodio/polkadart/pull/487/files#diff-744e37ecda229197b2bb42eae309e7de9faac25e2f15536323a7aa91c2f2c4bcR18-R26) ```diff -if (hexString.startsWith('0x')) { - rawData = hex.decode(hexString.substring(2)); -} else { - rawData = hex.decode(hexString); +try { + if (hexString.startsWith('0x')) { + rawData = hex.decode(hexString.substring(2)); + } else { + rawData = hex.decode(hexString); + } + final input = ByteInput.fromBytes(rawData); + return codec.decode(input); +} catch (e) { + throw Exception('Failed to decode hex string: $e'); } -final input = ByteInput.fromBytes(rawData); -return codec.decode(input); ``` Suggestion importance[1-10]: 8Why: Adding error handling for hex string decoding is crucial for preventing application crashes and providing meaningful error messages. This suggestion significantly enhances the robustness of the code by handling potential exceptions. | 8 | |
Improve error handling for empty or invalid JSON maps in deserialization___ **Consider handling the case where the 'json' map is empty or does not containexpected keys in TypeDef.fromJson . This will prevent runtime errors when keys are missing.** [packages/substrate_metadata/lib/scale_info/type_definition.dart [11]](https://github.com/leonardocustodio/polkadart/pull/487/files#diff-dfd2daaf996cb19132ec190538042c9be7a1c3c08473db576068e53cadc284b7R11-R11) ```diff -final String typeName = json.keys.first; +final String typeName = json.keys.isNotEmpty ? json.keys.first : throw Exception('JSON map is empty or invalid'); ``` Suggestion importance[1-10]: 8Why: This suggestion improves error handling by checking if the JSON map is empty, which prevents runtime errors and provides a clear exception message. It addresses a potential bug effectively. | 8 | |
Ensure safe casting of
___
**Ensure that the | 7 | |
Prevent potential null dereference by adding null checks or assertions___ **Add null checks or assertions for theinput parameter in the decode method to prevent potential runtime errors when input is null.**
[packages/substrate_metadata/lib/metadata/v14.dart [44-54]](https://github.com/leonardocustodio/polkadart/pull/487/files#diff-9d886c70250a98c868358affc2aa9f1c90113c965258dbcfe065bbd693ec37e5R44-R54)
```diff
RuntimeMetadataV14 decode(Input input) {
+ assert(input != null, 'Input cannot be null');
final types = SequenceCodec(PortableType.codec).decode(input);
...
}
```
Suggestion importance[1-10]: 7Why: Adding an assertion to check for null input is a good practice to prevent runtime errors. This change enhances the robustness of the code by ensuring that the input is not null before proceeding with the decode operation. | 7 | |
Ensure non-null JSON input in deserialization methods to prevent errors___ **Add null checks for the 'json' parameter in thefromJson methods to ensure that a non-null map is always processed, preventing potential NullPointerExceptions .**
[packages/substrate_metadata/lib/scale_info/type_definition.dart [10]](https://github.com/leonardocustodio/polkadart/pull/487/files#diff-dfd2daaf996cb19132ec190538042c9be7a1c3c08473db576068e53cadc284b7R10-R10)
```diff
factory TypeDef.fromJson(MapSuggestion importance[1-10]: 7Why: Adding a null check for the JSON parameter is a good practice to prevent null pointer exceptions. It enhances the robustness of the code by ensuring that the input is valid. | 7 | |
Maintainability |
Refactor duplicated metadata fetching logic into a separate method___ **Refactor the repeated logic for fetching and validatingRuntimeMetadataV14 into a separate method to avoid code duplication and enhance maintainability.** [packages/polkadart_cli/bin/get_metadata.dart [19-32]](https://github.com/leonardocustodio/polkadart/pull/487/files#diff-57a3a4cde8ec9ce2a8917067a7e2c5ce10a5f04c64cfd3e3f47046ed1c1da8c6R19-R32) ```diff -final decodedMetadata = await api.getMetadata(); -if (decodedMetadata.metadata is! RuntimeMetadataV14) { - await provider.disconnect(); - throw Exception('Only metadata version 14 is supported'); -} -final version = await api.getRuntimeVersion(); -await provider.disconnect(); -return ChainProperties( - decodedMetadata.metadata as RuntimeMetadataV14, - version, -); +return fetchAndValidateMetadataV14(api); ``` Suggestion importance[1-10]: 6Why: The suggestion promotes code maintainability by refactoring repeated logic into a separate method. This reduces code duplication and makes future updates easier, although it does not address any critical issues. | 6 |
Enhancement |
Enhance error handling in
___
**Consider implementing a more robust type checking and error handling for the | 5 |
Improve error handling by providing more descriptive error messages___ **Consider handling the case whereinput.read() returns a value outside the expected range by providing a more descriptive error message or a different error handling strategy.** [packages/substrate_metadata/lib/metadata/v14.dart [160-178]](https://github.com/leonardocustodio/polkadart/pull/487/files#diff-9d886c70250a98c868358affc2aa9f1c90113c965258dbcfe065bbd693ec37e5R160-R178) ```diff final index = input.read(); switch (index) { case 0: return StorageHasher.blake2_128; ... default: - throw Exception('Unknown StorageHasher variant index $index'); + throw Exception('Unknown StorageHasher variant index $index. Please check the input source or data integrity.'); } ``` Suggestion importance[1-10]: 5Why: The suggestion provides a slightly more descriptive error message, which can help with debugging. However, the improvement is marginal as it only adds a generic suggestion to check input source or data integrity. | 5 |
Also please bump substrate_metadata to v2.0.0 as that will be a breaking change from what we talked about
User description
PR Type
enhancement, other
Description
RuntimeMetadataV14
class to handle runtime metadata version 14.StateApi
to useRuntimeMetadataPrefixed
for metadata operations.substrate_metadata
.Changes walkthrough ๐
20 files
apis.dart
Update imports for metadata handling
packages/polkadart/lib/apis/apis.dart
substrate_metadata
package.polkadart
.state.dart
Update state API to use prefixed metadata
packages/polkadart/lib/apis/state.dart
RuntimeMetadata
toRuntimeMetadataPrefixed
.RuntimeMetadataPrefixed
.get_metadata.dart
Add script for fetching chain metadata
packages/polkadart_cli/bin/get_metadata.dart
ChainProperties
class to handle metadata and version.chain.dart
Update chain generator for metadata v14
packages/polkadart_cli/lib/src/generator/chain.dart
RuntimeMetadataV14
fromsubstrate_metadata
.metadata.types
.field.dart
Add Field class for scale info
packages/polkadart_cli/lib/src/scale_info/field.dart
Field
to represent struct-like data fields.Field
.scale_info.dart
Introduce scale info library
packages/polkadart_cli/lib/src/scale_info/scale_info.dart
type_definition.dart
Add type definition handling for scale info
packages/polkadart_cli/lib/src/scale_info/type_definition.dart
type_parameter.dart
Add TypeParameter class for scale info
packages/polkadart_cli/lib/src/scale_info/type_parameter.dart
TypeParameter
class for handling generic type parameters.TypeParameter
.variant.dart
Add Variant class for scale info
packages/polkadart_cli/lib/src/scale_info/variant.dart
Variant
class to represent enum variants.Variant
.common.dart
Add common metadata handling classes
packages/substrate_metadata/lib/metadata/common.dart
RuntimeMetadata
andRuntimeMetadataPrefixed
classes.metadata.dart
Introduce metadata handling library
packages/substrate_metadata/lib/metadata/metadata.dart
v14.dart
Implement RuntimeMetadataV14 and codecs
packages/substrate_metadata/lib/metadata/v14.dart
RuntimeMetadataV14
class for handling v14 metadata.field.dart
Add Field class with codec for scale info
packages/substrate_metadata/lib/scale_info/field.dart
Field
class for scale info with codec support.Field
.portable.dart
Add PortableType class with codec
packages/substrate_metadata/lib/scale_info/portable.dart
PortableType
class for portable type representation.PortableType
.scale_info.dart
Introduce scale info library with codecs
packages/substrate_metadata/lib/scale_info/scale_info.dart
type_definition.dart
Add type definition classes with codec
packages/substrate_metadata/lib/scale_info/type_definition.dart
type_metadata.dart
Add TypeMetadata class for detailed type info
packages/substrate_metadata/lib/scale_info/type_metadata.dart
TypeMetadata
class for detailed type information.TypeMetadata
.type_parameter.dart
Add TypeParameter class with codec
packages/substrate_metadata/lib/scale_info/type_parameter.dart
TypeParameter
class for generic type handling.TypeParameter
.variant.dart
Add Variant class with codec
packages/substrate_metadata/lib/scale_info/variant.dart
Variant
class for enum variant representation.Variant
.substrate_metadata.dart
Update exports for substrate metadata library
packages/substrate_metadata/lib/substrate_metadata.dart
1 files
pubspec.yaml
Update dependencies to include substrate_metadata
packages/polkadart_cli/pubspec.yaml - Added `substrate_metadata` as a dependency.