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

Work In Progress branch - Contracts - ABI #489

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

justkawal
Copy link
Collaborator

Work in Progress for Contracts ABI Code.

@justkawal justkawal self-assigned this Oct 20, 2024
@justkawal justkawal marked this pull request as draft October 20, 2024 19:01
@CLAassistant
Copy link

CLAassistant commented Oct 20, 2024

CLA assistant check
All committers have signed the CLA.

Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 5 🔵🔵🔵🔵🔵
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Complexity and Size
The schema definition is extremely large and complex, which could lead to difficulties in maintenance and understanding. Consider modularizing the schema into smaller, manageable components.

Redundancy
There appears to be significant redundancy with previous schema versions. Evaluate if it's possible to inherit or extend previous versions to reduce code duplication.

Deprecated Features
The schema includes definitions that are marked as deprecated. It's crucial to assess whether these are still needed or if they can be removed to clean up the codebase.

Performance Concerns
The hashing implementation might have performance implications due to the complex operations within loops. Performance testing and optimization may be necessary.

Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Score
Possible bug
Add checks for key existence in maps to prevent runtime errors

Consider handling exceptions where the 'types' key might not exist in the 'project'
map to prevent runtime errors.

packages/abi/lib/abi_description.dart [51]

-final List<dynamic> types = project['types'];
+final List<dynamic> types = project.containsKey('types') ? project['types'] : <dynamic>[];
Suggestion importance[1-10]: 9

Why: Adding checks for key existence in maps is important for preventing runtime errors, especially when dealing with dynamic data structures where keys might be missing.

9
Performance
Replace synchronous file reading with asynchronous methods

Use asynchronous file reading methods to avoid blocking the main thread, improving
the application's responsiveness.

packages/abi/example/example.dart [8]

-final jsonMap = jsonDecode(File(jsonFile).readAsStringSync());
+final jsonMap = jsonDecode(await File(jsonFile).readAsString());
Suggestion importance[1-10]: 8

Why: Using asynchronous file reading enhances application performance by preventing the main thread from blocking, which is crucial for maintaining responsiveness in applications.

8
Possible issue
Prevent zero-sized array elements by setting the minimum value of 'cellsPerElem' to 1

Ensure that the minimum value for 'cellsPerElem' in 'ArrayLayout_for_PortableForm'
is set to 1 instead of 0 to prevent potential issues with zero-sized arrays.

packages/abi/lib/schemas/v3/ink_v3_schema.dart [48-54]

 'cellsPerElem': {
     'description':
         'The number of cells each element in the array layout consists of.',
     'type': 'integer',
     'format': 'uint64',
-    'minimum': 0.0
+    'minimum': 1.0
 }
Suggestion importance[1-10]: 8

Why: This suggestion addresses a potential issue with zero-sized arrays by ensuring that 'cellsPerElem' has a minimum value of 1, which is a valid and impactful change to prevent errors related to array size.

8
Ensure type references are valid by setting the minimum index to 1

Update the 'minimum' value for 'type' in 'Field_for_PortableForm' to 1 to avoid
referencing a non-existent type index.

packages/abi/lib/schemas/v3/ink_v3_schema.dart [257-261]

 'type': {
     'description': 'The type of the field.',
     'type': 'integer',
     'format': 'uint32',
-    'minimum': 0.0
+    'minimum': 1.0
 }
Suggestion importance[1-10]: 7

Why: Setting the minimum value for 'type' to 1 helps avoid referencing a non-existent type index, which is a reasonable improvement to ensure data integrity and prevent potential runtime errors.

7
Prevent invalid type references by setting the minimum 'id' value to 1

Modify the 'minimum' value for 'id' in 'PortableType' to 1 to ensure it references a
valid type.

packages/abi/lib/schemas/v3/ink_v3_schema.dart [477]

-'id': {'type': 'integer', 'format': 'uint32', 'minimum': 0.0}
+'id': {'type': 'integer', 'format': 'uint32', 'minimum': 1.0}
Suggestion importance[1-10]: 7

Why: This change ensures that 'id' references a valid type by setting a minimum value of 1, which is a beneficial adjustment to maintain consistency and prevent invalid references.

7
Ensure variant indices start from a valid non-zero value

Adjust the 'minimum' value for 'index' in 'Variant_for_PortableForm' to 1 to ensure
it starts from a valid non-zero index.

packages/abi/lib/schemas/v3/ink_v3_schema.dart [788-793]

 'index': {
     'description':
         'Index of the variant, used in `parity-scale-codec`.\n\nThe value of this will be, in order of precedence: 1. The explicit index defined by a `#[codec(index = N)]` attribute. 2. The implicit index from the position of the variant in the `enum` definition.',
     'type': 'integer',
     'format': 'uint8',
-    'minimum': 0.0
+    'minimum': 1.0
 }
Suggestion importance[1-10]: 6

Why: Adjusting the minimum value for 'index' to 1 ensures that variant indices start from a valid non-zero value, which is a useful change for maintaining proper indexing and avoiding potential issues with zero-based indices. However, the impact is slightly less critical compared to other suggestions.

6
Maintainability
Replace hard-coded file paths with configurable paths

Avoid using hard-coded file paths in your code. Consider using a configuration file
or environment variables to define file paths.

packages/abi/example/example.dart [7]

-final jsonFile = '/Users/kawal/Desktop/git_projects/polkadart/packages/abi/example/v5_metadata.json';
+final jsonFile = config.get('metadataFilePath');
Suggestion importance[1-10]: 7

Why: Replacing hard-coded file paths with configurable paths improves maintainability and flexibility, allowing for easier changes in file locations without modifying the code.

7
Refactor repeated code into a function to improve maintainability

Replace the repeated code for processing types with a function to enhance code
reusability and maintainability.

packages/abi/lib/abi_description.dart [51-80]

-for (int i = 0; i < types.length; i++) {
-  final String label = types[i]['type']['def'].keys.first;
-  types[i]['type']['def'] = <String, dynamic>{
-    label.capitalize(): types[i]['type']['def'][label]
-  };
-}
-for (int i = 0; i < types.length; i++) {
-  if (types[i]['type'].containsKey('path') &&
-      types[i]['type']['path'] is List) {
-    if (!['Option', 'Result'].contains(types[i]['type']['path'][0])) {
-      final List<dynamic> path = types[i]['type']['path'];
-      // TODO: Check if namespace is important or not.
-      //path.insert(0, namespace);
-      types[i]['type']['path'] = path;
-    }
-  } else {
-    types[i]['type']['path'] = <String>[];
-  }
-  if (types[i]['type']['def'].containsKey('Variant')) {
-    if (types[i]['type']['def']['Variant'].containsKey('variants')) {
-      final List<dynamic> variants =
-          types[i]['type']['def']['Variant']['variants'];
-      for (int j = 0; j < variants.length; j++) {
-        variants[j]['fields'] = <dynamic>[];
-      }
-      types[i]['type']['def']['Variant']['variants'] = variants;
-    }
-  }
-}
+processTypes(types);
Suggestion importance[1-10]: 6

Why: Refactoring repeated code into a function enhances maintainability by reducing code duplication, making the codebase easier to manage and update in the future.

6
Best practice
Restrict unexpected properties in 'PortableType' by setting 'additionalProperties' to false

Add explicit 'additionalProperties' constraints to the 'PortableType' definition to
control or prevent unexpected properties.

packages/abi/lib/schemas/v4/ink_v4_schema.dart [500-504]

 'type': 'object',
 'required': ['id', 'type'],
 'properties': {
   'id': {'type': 'integer', 'format': 'uint32', 'minimum': 0.0},
   'type': {'$ref': '#/definitions/Type_for_PortableForm'}
-}
+},
+'additionalProperties': false
Suggestion importance[1-10]: 7

Why: Adding 'additionalProperties': false is a good practice to ensure that only defined properties are allowed, enhancing schema validation and preventing unexpected data.

7
Enhancement
Update the schema 'title' for better clarity and specificity

Consider using a more descriptive 'title' in the schema definition to better reflect
the specific version and purpose of the schema.

packages/abi/lib/schemas/v4/ink_v4_schema.dart [3]

-'title': 'InkProject'
+'title': 'InkProject ABI Schema v4'
Suggestion importance[1-10]: 5

Why: Updating the schema title to include version information can improve clarity and specificity, making it easier to identify the schema's purpose and version at a glance.

5
Add a default value to the 'type' property to ensure it is always initialized

Consider adding a default value for the 'type' property in the 'PortableType'
definition to ensure it always has a valid fallback value.

packages/abi/lib/schemas/v4/ink_v4_schema.dart [504]

-'type': {'$ref': '#/definitions/Type_for_PortableForm'}
+'type': {'$ref': '#/definitions/Type_for_PortableForm', 'default': 'defaultType'}
Suggestion importance[1-10]: 4

Why: Adding a default value to the 'type' property can prevent potential issues if the property is not set. However, the necessity of this change depends on the context and usage of the schema, which is not clear from the diff alone.

4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants