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

[Fix #354] Introduce decode_ledger_data utility method #785

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

Conversation

ckeshava
Copy link
Collaborator

High Level Overview of Change

This PR introduces a utility method for parsing serialized hexa-decimal ledger-data.

Context of Change

This PR is similar to this method in xrpl.js.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking change that only restructures code)
  • Tests (You added tests for code that already exists, or your new feature included in this PR)
  • Documentation Updates
  • Release

Did you update CHANGELOG.md?

  • Yes
  • No, this change does not impact library users

Test Plan

Three new unit tests are used to validate the correct execution of this new method.

Copy link
Contributor

coderabbitai bot commented Dec 20, 2024

Walkthrough

This pull request introduces updates to the XRPL Python library, including the addition of the decode_ledger_data utility method for parsing serialized ledger headers. The CHANGELOG.md has been updated to reflect these changes, including the introduction of the include_deleted parameter in the ledger_entry request and the removal of Python 3.7 support in favor of Python 3.8. Additionally, new test fixtures for binary codec functionality have been added, along with a new function to decode ledger headers.

Changes

File Change Summary
CHANGELOG.md - Added decode_ledger_data utility method
- Introduced include_deleted parameter in ledger_entry request
- Removed Python 3.7 support
tests/unit/core/binarycodec/fixtures/data/codec-fixtures.json - Added new account state entries
- Added new transaction entries (payments, AMM-related)
- Updated ledger data
tests/unit/core/binarycodec/fixtures/data_driven_fixtures.py - Added get_ledger_data_codec_test() function to fetch ledger data from JSON
tests/unit/core/binarycodec/test_field_id_codec.py - Added TestDecodeLedgerHeader test class
- Imported decode_ledger_header function
xrpl/core/binarycodec/field_id_codec.py - Added decode_ledger_header() function to decode serialized ledger headers

Possibly Related PRs

  • Update CHANGELOG.md #748: This PR updates the CHANGELOG.md to include the addition of the include_deleted parameter in the ledger_entry request, which is also mentioned in the main PR.
  • 4.0.0 release #783: This PR includes the addition of the include_deleted parameter in the ledger_entry request and the removal of Python 3.7 support, both of which are highlighted in the main PR's summary of changes.

Suggested Reviewers

  • anissa-ripple
  • justinr1234

Poem

🐰 In bytes and bits, a ledger's tale unfolds,
Decoding headers, secrets yet untold.
Python evolves, with 3.8's might,
Tokens and headers dancing in binary light!
A rabbit's code, precise and bright 🔍


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 29a708e and 6a4df65.

📒 Files selected for processing (1)
  • CHANGELOG.md (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.md

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

@ckeshava ckeshava requested review from khancode and mvadari December 20, 2024 08:42
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: 5

🧹 Nitpick comments (2)
tests/unit/core/binarycodec/fixtures/data/codec-fixtures.json (2)

Line range hint 2315-2882: Consider adding more edge cases for AMM operations.

While the AMM operation test cases cover the basic functionality, consider adding edge cases like:

  • Maximum/minimum values for trading fees
  • Edge cases for deposit/withdraw amounts
  • Error cases with invalid asset combinations

Line range hint 1-10: Add documentation for test data organization.

Consider adding a header comment explaining:

  • The structure and organization of the test data
  • Purpose of each section (accountState, transactions, ledgerData)
  • How to add new test cases

Example:

+ /*
+  * Binary codec test fixtures containing:
+  * 1. accountState - Ledger entry test data
+  * 2. transactions - Transaction test data  
+  * 3. ledgerData - Ledger header test data
+  *
+  * Each section contains both binary and JSON representations
+  * for testing serialization/deserialization.
+  */
{
  "accountState": [
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 864f7b9 and 29a708e.

📒 Files selected for processing (5)
  • CHANGELOG.md (2 hunks)
  • tests/unit/core/binarycodec/fixtures/data/codec-fixtures.json (2 hunks)
  • tests/unit/core/binarycodec/fixtures/data_driven_fixtures.py (1 hunks)
  • tests/unit/core/binarycodec/test_field_id_codec.py (2 hunks)
  • xrpl/core/binarycodec/field_id_codec.py (1 hunks)
🔇 Additional comments (3)
xrpl/core/binarycodec/field_id_codec.py (1)

6-13: LGTM: Imports are properly organized and typed.

The new imports are correctly organized and include necessary types for the implementation.

CHANGELOG.md (1)

Line range hint 11-24: LGTM! The changelog structure follows best practices.

The changes are well-organized under appropriate categories (Added, BREAKING CHANGE, Fixed) and follow the Keep a Changelog format.

🧰 Tools
🪛 Markdownlint (0.37.0)

15-15: Punctuation: ':'
Trailing punctuation in heading

(MD026, no-trailing-punctuation)

tests/unit/core/binarycodec/fixtures/data/codec-fixtures.json (1)

Line range hint 1-4882: LGTM! Comprehensive test data coverage for binary codec functionality.

The test fixtures provide excellent coverage across different ledger entry types, transaction types, and ledger headers. The data is well structured with both binary and JSON representations.

Comment on lines +16 to +43
def decode_ledger_header(serialized_str: str) -> Dict[str, Any]:
"""
Decodes a serialized ledger header.
Note: The file located at xrpl/core/binarycodec/definitions/definitions.json file
is used to parse the serialized data. If developers need custom definitions,
please update that file.

Args:
serialized_str: A serialized ledger header, represented as a hexa-decimal string

Returns:
A Dict object describing a ledger header
"""
parser = BinaryParser(serialized_str)

return {
"ledger_index": parser.read_uint32(),
# Uint64 types are represented as hex-strings for preserving precision
# For ease of use, explicitly type-cast this value into base-10
"total_coins": str(int(parser.read_type(UInt64).to_hex(), base=16)),
"parent_hash": parser.read_type(Hash256).to_hex(),
"transaction_hash": parser.read_type(Hash256).to_hex(),
"account_hash": parser.read_type(Hash256).to_hex(),
"parent_close_time": parser.read_uint32(),
"close_time": parser.read_uint32(),
"close_time_resolution": parser.read_uint8(),
"close_flags": parser.read_uint8(),
}
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

Consider adding error handling for malformed input.

The implementation looks solid and handles uint64 precision correctly. However, it should handle potential errors from malformed input.

Consider wrapping the parsing in a try-except block:

 def decode_ledger_header(serialized_str: str) -> Dict[str, Any]:
     """
     Decodes a serialized ledger header.
     Note: The file located at xrpl/core/binarycodec/definitions/definitions.json file
     is used to parse the serialized data. If developers need custom definitions,
     please update that file.

     Args:
         serialized_str: A serialized ledger header, represented as a hexa-decimal string

     Returns:
         A Dict object describing a ledger header
+
+    Raises:
+        XRPLBinaryCodecException: If the input string is malformed or incomplete
     """
+    if not isinstance(serialized_str, str):
+        raise XRPLBinaryCodecException("serialized_str must be a string")
+
     parser = BinaryParser(serialized_str)
+    try:
         return {
             "ledger_index": parser.read_uint32(),
             # Uint64 types are represented as hex-strings for preserving precision
             # For ease of use, explicitly type-cast this value into base-10
             "total_coins": str(int(parser.read_type(UInt64).to_hex(), base=16)),
             "parent_hash": parser.read_type(Hash256).to_hex(),
             "transaction_hash": parser.read_type(Hash256).to_hex(),
             "account_hash": parser.read_type(Hash256).to_hex(),
             "parent_close_time": parser.read_uint32(),
             "close_time": parser.read_uint32(),
             "close_time_resolution": parser.read_uint8(),
             "close_flags": parser.read_uint8(),
         }
+    except Exception as e:
+        raise XRPLBinaryCodecException(f"Failed to decode ledger header: {str(e)}")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def decode_ledger_header(serialized_str: str) -> Dict[str, Any]:
"""
Decodes a serialized ledger header.
Note: The file located at xrpl/core/binarycodec/definitions/definitions.json file
is used to parse the serialized data. If developers need custom definitions,
please update that file.
Args:
serialized_str: A serialized ledger header, represented as a hexa-decimal string
Returns:
A Dict object describing a ledger header
"""
parser = BinaryParser(serialized_str)
return {
"ledger_index": parser.read_uint32(),
# Uint64 types are represented as hex-strings for preserving precision
# For ease of use, explicitly type-cast this value into base-10
"total_coins": str(int(parser.read_type(UInt64).to_hex(), base=16)),
"parent_hash": parser.read_type(Hash256).to_hex(),
"transaction_hash": parser.read_type(Hash256).to_hex(),
"account_hash": parser.read_type(Hash256).to_hex(),
"parent_close_time": parser.read_uint32(),
"close_time": parser.read_uint32(),
"close_time_resolution": parser.read_uint8(),
"close_flags": parser.read_uint8(),
}
def decode_ledger_header(serialized_str: str) -> Dict[str, Any]:
"""
Decodes a serialized ledger header.
Note: The file located at xrpl/core/binarycodec/definitions/definitions.json file
is used to parse the serialized data. If developers need custom definitions,
please update that file.
Args:
serialized_str: A serialized ledger header, represented as a hexa-decimal string
Returns:
A Dict object describing a ledger header
Raises:
XRPLBinaryCodecException: If the input string is malformed or incomplete
"""
if not isinstance(serialized_str, str):
raise XRPLBinaryCodecException("serialized_str must be a string")
parser = BinaryParser(serialized_str)
try:
return {
"ledger_index": parser.read_uint32(),
# Uint64 types are represented as hex-strings for preserving precision
# For ease of use, explicitly type-cast this value into base-10
"total_coins": str(int(parser.read_type(UInt64).to_hex(), base=16)),
"parent_hash": parser.read_type(Hash256).to_hex(),
"transaction_hash": parser.read_type(Hash256).to_hex(),
"account_hash": parser.read_type(Hash256).to_hex(),
"parent_close_time": parser.read_uint32(),
"close_time": parser.read_uint32(),
"close_time_resolution": parser.read_uint8(),
"close_flags": parser.read_uint8(),
}
except Exception as e:
raise XRPLBinaryCodecException(f"Failed to decode ledger header: {str(e)}")

Comment on lines +12 to +23
def get_ledger_data_codec_test():
"""
Fetch the (serialized, de-serialized) pairs of ledger_data from the
codec-fixtures.json file
"""

with open(
"tests/unit/core/binarycodec/fixtures/data/codec-fixtures.json"
) as codec_tests:
ledger_data = json.load(codec_tests)["ledgerData"]

return ledger_data
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

Align implementation with existing fixture loading pattern.

The current implementation has several issues that should be addressed:

  1. Hardcoded path could break in different environments
  2. Missing error handling
  3. Inconsistent with other fixture loading patterns in this file

Consider refactoring to match the existing pattern:

+_CODEC_FILENAME = "./data/codec-fixtures.json"
+_codec_absolute_path = os.path.join(dirname, _CODEC_FILENAME)
+with open(_codec_absolute_path) as codec_tests:
+    _CODEC_FIXTURES_JSON = json.load(codec_tests)

 def get_ledger_data_codec_test():
     """
     Fetch the (serialized, de-serialized) pairs of ledger_data from the
     codec-fixtures.json file
+
+    Returns:
+        List of ledger data test fixtures
+
+    Raises:
+        KeyError: If ledgerData key is missing from fixtures
     """
-    with open(
-        "tests/unit/core/binarycodec/fixtures/data/codec-fixtures.json"
-    ) as codec_tests:
-        ledger_data = json.load(codec_tests)["ledgerData"]
-
-        return ledger_data
+    try:
+        return _CODEC_FIXTURES_JSON["ledgerData"]
+    except KeyError as e:
+        raise KeyError("Missing ledgerData in codec-fixtures.json") from e

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

@@ -2,6 +2,7 @@

import xrpl.core.binarycodec.field_id_codec as field_id_codec
from tests.unit.core.binarycodec.fixtures import data_driven_fixtures
from xrpl.core.binarycodec.field_id_codec import decode_ledger_header
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 missing import for exception handling.

For the new test cases, we need to import XRPLBinaryCodecException.

Add this import:

 from unittest import TestCase
 
 import xrpl.core.binarycodec.field_id_codec as field_id_codec
+from xrpl.core.binarycodec.exceptions import XRPLBinaryCodecException
 from tests.unit.core.binarycodec.fixtures import data_driven_fixtures
 from xrpl.core.binarycodec.field_id_codec import decode_ledger_header
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
from xrpl.core.binarycodec.field_id_codec import decode_ledger_header
from xrpl.core.binarycodec.field_id_codec import decode_ledger_header
from xrpl.core.binarycodec.exceptions import XRPLBinaryCodecException

Comment on lines +27 to +32
class TestDecodeLedgerHeader(TestCase):
def test_decode_ledger_header_valid(self):
for test in data_driven_fixtures.get_ledger_data_codec_test():
self.assertEqual(
test["json"], decode_ledger_header(test["serialized_data"])
)
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

Enhance test coverage with additional test cases.

The current test implementation only covers the happy path. Consider adding tests for:

  1. Error cases (malformed input)
  2. Edge cases (minimum/maximum values)
  3. Field-specific validations

Add these test methods to improve coverage:

 class TestDecodeLedgerHeader(TestCase):
     def test_decode_ledger_header_valid(self):
         for test in data_driven_fixtures.get_ledger_data_codec_test():
             self.assertEqual(
                 test["json"], decode_ledger_header(test["serialized_data"])
             )
+
+    def test_decode_ledger_header_invalid_input(self):
+        """Test that invalid inputs raise appropriate exceptions."""
+        invalid_inputs = [
+            None,
+            123,
+            "",
+            "invalid hex",
+            "0x1234",  # incomplete data
+        ]
+        for invalid_input in invalid_inputs:
+            with self.assertRaises(XRPLBinaryCodecException):
+                decode_ledger_header(invalid_input)
+
+    def test_decode_ledger_header_field_validation(self):
+        """Test specific field values and types."""
+        test_data = data_driven_fixtures.get_ledger_data_codec_test()[0]
+        result = decode_ledger_header(test_data["serialized_data"])
+
+        # Validate field types
+        self.assertIsInstance(result["ledger_index"], int)
+        self.assertIsInstance(result["total_coins"], str)
+        self.assertIsInstance(result["parent_hash"], str)
+        self.assertTrue(result["parent_hash"].isalnum())
+        self.assertEqual(len(result["parent_hash"]), 64)  # 32 bytes in hex
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
class TestDecodeLedgerHeader(TestCase):
def test_decode_ledger_header_valid(self):
for test in data_driven_fixtures.get_ledger_data_codec_test():
self.assertEqual(
test["json"], decode_ledger_header(test["serialized_data"])
)
class TestDecodeLedgerHeader(TestCase):
def test_decode_ledger_header_valid(self):
for test in data_driven_fixtures.get_ledger_data_codec_test():
self.assertEqual(
test["json"], decode_ledger_header(test["serialized_data"])
)
def test_decode_ledger_header_invalid_input(self):
"""Test that invalid inputs raise appropriate exceptions."""
invalid_inputs = [
None,
123,
"",
"invalid hex",
"0x1234", # incomplete data
]
for invalid_input in invalid_inputs:
with self.assertRaises(XRPLBinaryCodecException):
decode_ledger_header(invalid_input)
def test_decode_ledger_header_field_validation(self):
"""Test specific field values and types."""
test_data = data_driven_fixtures.get_ledger_data_codec_test()[0]
result = decode_ledger_header(test_data["serialized_data"])
# Validate field types
self.assertIsInstance(result["ledger_index"], int)
self.assertIsInstance(result["total_coins"], str)
self.assertIsInstance(result["parent_hash"], str)
self.assertTrue(result["parent_hash"].isalnum())
self.assertEqual(len(result["parent_hash"]), 64) # 32 bytes in hex

CHANGELOG.md Outdated
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Support for the Multi-Purpose Tokens (MPT) amendment (XLS-33)
- Add `include_deleted` to ledger_entry request
- decode_ledger_data utility method is useful for parsing serialized ledger-headers
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 the changelog entry for better clarity and context.

The current entry is vague and could be more informative. Consider revising it to better describe the feature's purpose and scope:

-decode_ledger_data utility method is useful for parsing serialized ledger-headers
+Add `decode_ledger_data` utility method for parsing serialized hexadecimal ledger data, including ledger headers. This method provides functionality similar to the one in xrpl.js library.

This revision:

  1. Uses consistent formatting with other entries
  2. Clearly states the method's purpose
  3. Provides context about its relationship to xrpl.js
  4. Uses proper punctuation
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- decode_ledger_data utility method is useful for parsing serialized ledger-headers
- Add `decode_ledger_data` utility method for parsing serialized hexadecimal ledger data, including ledger headers. This method provides functionality similar to the one in xrpl.js library.

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