Skip to content

Commit 29a708e

Browse files
committed
[Fix #354] Introduce decode_ledger_data utility method
1 parent 864f7b9 commit 29a708e

File tree

5 files changed

+87
-2
lines changed

5 files changed

+87
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111
- Support for the Multi-Purpose Tokens (MPT) amendment (XLS-33)
1212
- Add `include_deleted` to ledger_entry request
13+
- decode_ledger_data utility method is useful for parsing serialized ledger-headers
1314

1415
### BREAKING CHANGE:
1516
- Remove Python 3.7 support to fix dependency installation and use 3.8 as new default.
@@ -92,7 +93,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9293
## [2.0.0] - 2023-07-05
9394
### BREAKING CHANGE
9495
- The default signing algorithm in the `Wallet` was changed from secp256k1 to ed25519
95-
-
96+
-
9697
### Added:
9798
- Wallet support for regular key compatibility
9899
- Added new ways of wallet generation: `from_seed`, `from_secret`, `from_entropy`, `from_secret_numbers`

tests/unit/core/binarycodec/fixtures/data/codec-fixtures.json

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4842,7 +4842,7 @@
48424842
}
48434843
],
48444844
"ledgerData": [{
4845-
"binary": "01E91435016340767BF1C4A3EACEB081770D8ADE216C85445DD6FB002C6B5A2930F2DECE006DA18150CB18F6DD33F6F0990754C962A7CCE62F332FF9C13939B03B864117F0BDA86B6E9B4F873B5C3E520634D343EF5D9D9A4246643D64DAD278BA95DC0EAC6EB5350CF970D521276CDE21276CE60A00",
4845+
"serialized_data": "01E91435016340767BF1C4A3EACEB081770D8ADE216C85445DD6FB002C6B5A2930F2DECE006DA18150CB18F6DD33F6F0990754C962A7CCE62F332FF9C13939B03B864117F0BDA86B6E9B4F873B5C3E520634D343EF5D9D9A4246643D64DAD278BA95DC0EAC6EB5350CF970D521276CDE21276CE60A00",
48464846
"json": {
48474847
"account_hash": "3B5C3E520634D343EF5D9D9A4246643D64DAD278BA95DC0EAC6EB5350CF970D5",
48484848
"close_flags": 0,
@@ -4854,5 +4854,31 @@
48544854
"total_coins": "99994494362043555",
48554855
"transaction_hash": "DD33F6F0990754C962A7CCE62F332FF9C13939B03B864117F0BDA86B6E9B4F87"
48564856
}
4857+
}, {
4858+
"serialized_data": "058973980163396C087559B1361DA99E35E60A7A769473C536F4D2218C75872CD23FB76701C0C4A7D5DDD34B52F011B54E84BEC1E84C509ADEB64111AE2D7B6082B2065A70A1538678DF1ED14E8D02783F8E4F47A636BB73264EEADAAF93FA1DC10E8BFA9FEDE1E436313EE22EF7E1202EF7E1210A00",
4859+
"json": {
4860+
"account_hash": "4E8D02783F8E4F47A636BB73264EEADAAF93FA1DC10E8BFA9FEDE1E436313EE2",
4861+
"close_flags": 0,
4862+
"close_time": 787996961,
4863+
"close_time_resolution": 10,
4864+
"parent_close_time": 787996960,
4865+
"parent_hash": "361DA99E35E60A7A769473C536F4D2218C75872CD23FB76701C0C4A7D5DDD34B",
4866+
"total_coins": "99986752893442481",
4867+
"transaction_hash": "52F011B54E84BEC1E84C509ADEB64111AE2D7B6082B2065A70A1538678DF1ED1",
4868+
"ledger_index": 92894104
4869+
}
4870+
}, {
4871+
"serialized_data": "058974040163396C0704FF8BBB7676027C39A0BF54ACA8B472CD2D39CBB83AE9612FF6E129E1FE6237A7D75AE1D62183ED82F39825A20199251427D1DEBFD46C1AABAA473DDFDB93ABBAF9CDD7C48AD76715EE257DBF64BE4248827A147E59F90D1905174C47D6878C278AB12EF7E2C42EF7E2C50A00",
4872+
"json": {
4873+
"account_hash": "D7C48AD76715EE257DBF64BE4248827A147E59F90D1905174C47D6878C278AB1",
4874+
"close_flags": 0,
4875+
"close_time": 787997381,
4876+
"close_time_resolution": 10,
4877+
"parent_close_time": 787997380,
4878+
"parent_hash": "BB7676027C39A0BF54ACA8B472CD2D39CBB83AE9612FF6E129E1FE6237A7D75A",
4879+
"total_coins": "99986752869302155",
4880+
"transaction_hash": "E1D62183ED82F39825A20199251427D1DEBFD46C1AABAA473DDFDB93ABBAF9CD",
4881+
"ledger_index": 92894212
4882+
}
48574883
}]
48584884
}

tests/unit/core/binarycodec/fixtures/data_driven_fixtures.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
# top level keys: ['types', 'fields_tests', 'whole_objects', 'values_tests']
1010

1111

12+
def get_ledger_data_codec_test():
13+
"""
14+
Fetch the (serialized, de-serialized) pairs of ledger_data from the
15+
codec-fixtures.json file
16+
"""
17+
18+
with open(
19+
"tests/unit/core/binarycodec/fixtures/data/codec-fixtures.json"
20+
) as codec_tests:
21+
ledger_data = json.load(codec_tests)["ledgerData"]
22+
23+
return ledger_data
24+
25+
1226
def get_field_tests():
1327
"""
1428
Constructs and returns a list of FieldTest objects after parsing JSON data

tests/unit/core/binarycodec/test_field_id_codec.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import xrpl.core.binarycodec.field_id_codec as field_id_codec
44
from tests.unit.core.binarycodec.fixtures import data_driven_fixtures
5+
from xrpl.core.binarycodec.field_id_codec import decode_ledger_header
56

67

78
class TestFieldIDCodec(TestCase):
@@ -21,3 +22,11 @@ def test_encode(self):
2122
def test_decode(self):
2223
for test in self.field_tests:
2324
self.assertEqual(test.name, field_id_codec.decode(test.expected_hex))
25+
26+
27+
class TestDecodeLedgerHeader(TestCase):
28+
def test_decode_ledger_header_valid(self):
29+
for test in data_driven_fixtures.get_ledger_data_codec_test():
30+
self.assertEqual(
31+
test["json"], decode_ledger_header(test["serialized_data"])
32+
)

xrpl/core/binarycodec/field_id_codec.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,44 @@
33
`Field IDs <https://xrpl.org/serialization.html#field-ids>`_
44
"""
55

6+
from typing import Any, Dict
7+
8+
from xrpl.core.binarycodec.binary_wrappers import BinaryParser
69
from xrpl.core.binarycodec.definitions import definitions
710
from xrpl.core.binarycodec.definitions.field_header import FieldHeader
811
from xrpl.core.binarycodec.exceptions import XRPLBinaryCodecException
12+
from xrpl.core.binarycodec.types.hash256 import Hash256
13+
from xrpl.core.binarycodec.types.uint64 import UInt64
14+
15+
16+
def decode_ledger_header(serialized_str: str) -> Dict[str, Any]:
17+
"""
18+
Decodes a serialized ledger header.
19+
Note: The file located at xrpl/core/binarycodec/definitions/definitions.json file
20+
is used to parse the serialized data. If developers need custom definitions,
21+
please update that file.
22+
23+
Args:
24+
serialized_str: A serialized ledger header, represented as a hexa-decimal string
25+
26+
Returns:
27+
A Dict object describing a ledger header
28+
"""
29+
parser = BinaryParser(serialized_str)
30+
31+
return {
32+
"ledger_index": parser.read_uint32(),
33+
# Uint64 types are represented as hex-strings for preserving precision
34+
# For ease of use, explicitly type-cast this value into base-10
35+
"total_coins": str(int(parser.read_type(UInt64).to_hex(), base=16)),
36+
"parent_hash": parser.read_type(Hash256).to_hex(),
37+
"transaction_hash": parser.read_type(Hash256).to_hex(),
38+
"account_hash": parser.read_type(Hash256).to_hex(),
39+
"parent_close_time": parser.read_uint32(),
40+
"close_time": parser.read_uint32(),
41+
"close_time_resolution": parser.read_uint8(),
42+
"close_flags": parser.read_uint8(),
43+
}
944

1045

1146
def encode(field_name: str) -> bytes:

0 commit comments

Comments
 (0)