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

dev: add definitions.json generation script #772

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mvadari
Copy link
Collaborator

@mvadari mvadari commented Nov 14, 2024

High Level Overview of Change

This PR adds a script to generate the definitions.json file from rippled source code.

Context of Change

Copied (and modified) from https://github.com/RichardAH/xrpl-codec-gen. It makes more sense to store this script in the library repo now.

Type of Change

  • New feature (non-breaking change which adds functionality)

Did you update HISTORY.md?

  • No, this change does not impact library users

Test Plan

Works locally.

Copy link
Contributor

coderabbitai bot commented Nov 14, 2024

Walkthrough

The changes include the addition of a new script, generate_definitions.py, which generates a definitions.json file from the source code of the rippled project. This script reads protocol-related headers and macro files, processes their contents, and outputs structured JSON data. Additionally, the definitions.json file has undergone significant modifications, with new entries added and several existing entries removed or retained across various sections, including TYPES, LEDGER_ENTRY_TYPES, FIELDS, TRANSACTION_RESULTS, and TRANSACTION_TYPES. Furthermore, updates were made to the generate_tx_models.py script, changing the source files for SFields and TxFormats, and modifying the type mapping.

Changes

File Change Summary
tools/generate_definitions.py New script created to generate definitions.json. It reads protocol-related files, processes data, and outputs structured JSON. Includes functions for reading files, translating formats, and extracting type information.
xrpl/core/binarycodec/definitions/definitions.json Modifications to definitions.json: new entries added in LEDGER_ENTRY_TYPES, several entries removed, and others retained. Changes also include removals in TRANSACTION_RESULTS and TRANSACTION_TYPES.
tools/generate_tx_models.py Significant modifications to _parse_rippled_source function to read SFields and TxFormats from new source files. Updated regex patterns and added new entry "UINT192": "str" to TYPE_MAP.

Poem

In the meadow, scripts do play,
Generating definitions, hip-hip-hooray!
New types hop in, old ones take flight,
JSON dances, oh what a sight!
With every change, we leap and bound,
In the world of code, joy is found! 🐇✨


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 906cb05 and 4474908.

📒 Files selected for processing (1)
  • tools/generate_tx_models.py (2 hunks)
🔇 Additional comments (4)
tools/generate_tx_models.py (4)

29-31: Update to source file path for SFields

The source file for SFields has been updated to include/xrpl/protocol/detail/sfields.macro, which aligns with the new directory structure of the rippled project.


63-63: Added UINT192 to TYPE_MAP

The inclusion of "UINT192": "str" in TYPE_MAP is appropriate, expanding the mapping to support UINT192 types. Ensure that any fields of this type are correctly handled elsewhere in the codebase.


42-47: Verify that the updated regex pattern correctly parses TxFormats

The regex pattern for extracting TxFormats has been updated to match the new structure in transactions.macro. Ensure that the pattern accurately captures all transaction definitions, including any new or complex transaction types.

Run the following script to test the regex pattern against transactions.macro:


33-34: Verify that the updated regex pattern correctly parses SFields

The regex pattern for extracting SFields has been modified to accommodate the new format in sfields.macro. Please verify that the pattern accurately captures all necessary fields, including any edge cases or variations in formatting.

Run the following script to test the regex pattern against sfields.macro:


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (5)
tools/generate_definitions.py (4)

6-15: Add docstring to explain CAPITALIZATION_EXCEPTIONS dictionary

The dictionary's purpose and when/how exceptions are applied should be documented for maintainability.

Add a docstring explaining the purpose:

 CAPITALIZATION_EXCEPTIONS = {
+    """Mapping of special case strings to their proper capitalization in the XRPL.
+    
+    These exceptions override the default word capitalization rules when processing
+    field names and types from the rippled source code.
+    """
     "NFTOKEN": "NFToken",

17-19: Enhance error handling with descriptive message

The error message could be more informative about what the rippled path should contain.

 if len(sys.argv) != 2:
-    print("Usage: python " + sys.argv[0] + " path/to/rippled")
+    print(f"Usage: {sys.argv[0]} PATH_TO_RIPPLED\n"
+          f"PATH_TO_RIPPLED should point to the root of the rippled source code containing the 'include' directory")
     sys.exit(1)

87-93: Document and simplify complex regex patterns

The regex patterns are complex and would benefit from documentation and named groups.

+# Pattern to match STYPE definitions in two possible formats:
+# 1. STYPE(STI_NAME, NUMBER)
+# 2. STI_NAME = NUMBER
+TYPE_PATTERN = r"""
+    ^[ ]*                   # Start of line with optional spaces
+    (?:STYPE\(STI_         # First format: STYPE(STI_
+        (?P<name1>[^ ]*?)   # Capture name
+        [ ]*,[ ]*           # Comma separator
+        (?P<num1>[0-9-]+)   # Capture number
+        [ ]*\)              # Closing parenthesis
+    |                       # OR
+    STI_                    # Second format: STI_
+        (?P<name2>[^ ]*?)   # Capture name
+        [ ]*=[ ]*           # Equals sign
+        (?P<num2>[0-9-]+)   # Capture number
+    )
+    [ ]*,?[ ]*$            # Optional comma and end of line
+"""
+
 type_hits = re.findall(
-    r"^ *STYPE\(STI_([^ ]*?) *, *([0-9-]+) *\) *\\?$", sfield_h, re.MULTILINE
+    TYPE_PATTERN, sfield_h, re.MULTILINE | re.VERBOSE
 )

1-326: Add unit tests for the script

The script performs critical data processing but lacks tests to verify its correctness.

Would you like me to help create a test suite for this script? The tests would cover:

  • File reading and error handling
  • String translation logic
  • Regex pattern matching
  • Data validation
  • JSON output formatting
xrpl/core/binarycodec/definitions/definitions.json (1)

Line range hint 1-1068: Well-structured definitions file with clear organization

The file maintains a clean and consistent structure with:

  • Clear separation of concerns between different type definitions
  • Consistent formatting and indentation
  • Valid JSON syntax
  • Logical grouping of related entries

This organization makes the file easy to maintain and extend.

Consider adding a schema file to formally validate the structure and prevent accidental malformation during future updates.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f9bae8c and 906cb05.

📒 Files selected for processing (2)
  • tools/generate_definitions.py (1 hunks)
  • xrpl/core/binarycodec/definitions/definitions.json (1 hunks)
🔇 Additional comments (1)
xrpl/core/binarycodec/definitions/definitions.json (1)

33-57: LGTM: LEDGER_ENTRY_TYPES changes are well-structured

The new ledger entry types are properly organized with:

  • Consistent numerical ordering of type IDs
  • No duplicate IDs
  • Clear semantic naming that reflects their purpose

Comment on lines +1 to +326
return "false"


def _is_serialized(t: str) -> str:
if t == "LEDGERENTRY" or t == "TRANSACTION" or t == "VALIDATION" or t == "METADATA":
return "false"
return "true"


def _is_signing_field(t: str, not_signing_field: str) -> str:
if not_signing_field == "notSigning":
return "false"
if t == "LEDGERENTRY" or t == "TRANSACTION" or t == "VALIDATION" or t == "METADATA":
return "false"
return "true"


# Parse SField.cpp for all the SFields and their serialization info
sfield_hits = re.findall(
r"^ *[A-Z]*TYPED_SFIELD *\( *sf([^,\n]*),[ \n]*([^, \n]+)[ \n]*,[ \n]*"
r"([0-9]+)(,.*?(notSigning))?",
sfield_macro_file,
re.MULTILINE,
)
for x in range(len(sfield_hits)):
print(" [")
print(' "' + sfield_hits[x][0] + '",')
print(" {")
print(' "nth": ' + sfield_hits[x][2] + ",")
print(' "isVLEncoded": ' + _is_vl_encoded(sfield_hits[x][1]) + ",")
print(' "isSerialized": ' + _is_serialized(sfield_hits[x][1]) + ",")
print(
' "isSigningField": '
+ _is_signing_field(sfield_hits[x][1], sfield_hits[x][4])
+ ","
)
print(' "type": "' + _translate(sfield_hits[x][1]) + '"')
print(" }")
print(" ]" + ("," if x < len(sfield_hits) - 1 else ""))

print(" ],")

########################################################################
# TER code processing
########################################################################
print(' "TRANSACTION_RESULTS": {')
ter_h = str(ter_h).replace("[[maybe_unused]]", "")

ter_code_hits = re.findall(
r"^ *((tel|tem|tef|ter|tes|tec)[A-Z_]+)( *= *([0-9-]+))? *,? *(\/\/[^\n]*)?$",
ter_h,
re.MULTILINE,
)
upto = -1
last = ""

for x in range(len(ter_code_hits)):
if ter_code_hits[x][3] != "":
upto = int(ter_code_hits[x][3])

current = ter_code_hits[x][1]
if current != last and last != "":
print("")
pass
last = current

print(
' "'
+ ter_code_hits[x][0]
+ '": '
+ str(upto)
+ ("," if x < len(ter_code_hits) - 1 else "")
)

upto += 1

print(" },")

########################################################################
# Transaction type processing
########################################################################
print(' "TRANSACTION_TYPES": {')
print(' "Invalid": -1,')

tx_hits = re.findall(
r"^ *TRANSACTION\(tt[A-Z_]+ *,* ([0-9]+) *, *([A-Za-z]+).*$",
transactions_file,
re.MULTILINE,
)
for x in range(len(tx_hits)):
print(
' "'
+ tx_hits[x][1]
+ '": '
+ tx_hits[x][0]
+ ("," if x < len(tx_hits) - 1 else "")
)

print(" }")
print("}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add logging and validation throughout the script

The script would benefit from proper logging and validation of parsed data.

Add logging and validation:

 import re
 import sys
+import logging
+from typing import List, Tuple, Dict
+
+# Configure logging
+logging.basicConfig(
+    level=logging.INFO,
+    format='%(asctime)s - %(levelname)s - %(message)s'
+)
+
+def validate_parsed_data(section_name: str, data: List[Tuple[str, ...]]) -> None:
+    """Validate parsed data for completeness and correctness.
+    
+    Args:
+        section_name: Name of the section being validated
+        data: List of parsed tuples
+        
+    Raises:
+        SystemExit: If validation fails
+    """
+    if not data:
+        logging.error(f"No data parsed for section: {section_name}")
+        sys.exit(1)
+    logging.info(f"Successfully parsed {len(data)} entries for {section_name}")

Then use throughout the script:

 type_hits = re.findall(...)
+validate_parsed_data("TYPES", type_hits)
+logging.info("Processing type definitions")

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +26 to +29
def _read_file(filename: str) -> str:
with open(filename, "r") as f:
return f.read()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling for file operations

The file reading function should handle potential IOErrors and provide meaningful error messages.

 def _read_file(filename: str) -> str:
+    """Read and return the contents of a file.
+    
+    Args:
+        filename: Path to the file to read
+    
+    Returns:
+        The contents of the file as a string
+        
+    Raises:
+        SystemExit: If the file cannot be read
+    """
     try:
         with open(filename, "r") as f:
             return f.read()
+    except IOError as e:
+        print(f"Error reading {filename}: {e}", file=sys.stderr)
+        sys.exit(1)

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +47 to +78
def _translate(inp: str) -> str:
if re.match(r"^UINT", inp):
if re.search(r"256|160|128", inp):
return inp.replace("UINT", "Hash")
else:
return inp.replace("UINT", "UInt")
if inp == "OBJECT" or inp == "ARRAY":
return "ST" + inp[0:1].upper() + inp[1:].lower()
if inp == "ACCOUNT":
return "AccountID"
if inp == "LEDGERENTRY":
return "LedgerEntry"
if inp == "NOTPRESENT":
return "NotPresent"
if inp == "PATHSET":
return "PathSet"
if inp == "VL":
return "Blob"
if inp == "DIR_NODE":
return "DirectoryNode"
if inp == "PAYCHAN":
return "PayChannel"

parts = inp.split("_")
result = ""
for part in parts:
if part in CAPITALIZATION_EXCEPTIONS:
result += CAPITALIZATION_EXCEPTIONS[part]
else:
result += part[0:1].upper() + part[1:].lower()
return result

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve _translate function with documentation and type hints

The translation function is complex and would benefit from better documentation and structure.

-def _translate(inp: str) -> str:
+def _translate(inp: str) -> str:
+    """Translate rippled type names to binary codec format.
+    
+    Args:
+        inp: Input string in rippled format (e.g., 'UINT256', 'ACCOUNT')
+        
+    Returns:
+        Translated string in binary codec format (e.g., 'Hash256', 'AccountID')
+    """
+    # Define translation mapping for direct conversions
+    DIRECT_TRANSLATIONS = {
+        "OBJECT": "STObject",
+        "ARRAY": "STArray",
+        "ACCOUNT": "AccountID",
+        "LEDGERENTRY": "LedgerEntry",
+        "NOTPRESENT": "NotPresent",
+        "PATHSET": "PathSet",
+        "VL": "Blob",
+        "DIR_NODE": "DirectoryNode",
+        "PAYCHAN": "PayChannel",
+    }
+
+    # Handle direct translations first
+    if inp in DIRECT_TRANSLATIONS:
+        return DIRECT_TRANSLATIONS[inp]
+
     # Handle UINT types
     if re.match(r"^UINT", inp):
         if re.search(r"256|160|128", inp):
             return inp.replace("UINT", "Hash")
         else:
             return inp.replace("UINT", "UInt")
-    if inp == "OBJECT" or inp == "ARRAY":
-        return "ST" + inp[0:1].upper() + inp[1:].lower()
-    if inp == "ACCOUNT":
-        return "AccountID"
-    if inp == "LEDGERENTRY":
-        return "LedgerEntry"
-    # ... rest of the conditions

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +31 to +38
sfield_h_fn = sys.argv[1] + "/include/xrpl/protocol/SField.h"
sfield_macro_fn = sys.argv[1] + "/include/xrpl/protocol/detail/sfields.macro"
ledger_entries_macro_fn = (
sys.argv[1] + "/include/xrpl/protocol/detail/ledger_entries.macro"
)
ter_h_fn = sys.argv[1] + "/include/xrpl/protocol/TER.h"
transactions_macro_fn = sys.argv[1] + "/include/xrpl/protocol/detail/transactions.macro"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use pathlib for robust path handling

Using string concatenation for paths is error-prone. Consider using pathlib for more robust path handling.

+from pathlib import Path
+
+rippled_path = Path(sys.argv[1])
+include_path = rippled_path / "include" / "xrpl" / "protocol"
+
-sfield_h_fn = sys.argv[1] + "/include/xrpl/protocol/SField.h"
-sfield_macro_fn = sys.argv[1] + "/include/xrpl/protocol/detail/sfields.macro"
+sfield_h_fn = include_path / "SField.h"
+sfield_macro_fn = include_path / "detail" / "sfields.macro"

Committable suggestion skipped: line range outside the PR's diff.

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

Successfully merging this pull request may close these issues.

1 participant