-
Notifications
You must be signed in to change notification settings - Fork 69
Support dynamic encodings #131
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
Open
carver
wants to merge
3
commits into
ApeWorX:main
Choose a base branch
from
carver:test-dynamic-structures
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from abc import ABC, abstractmethod | ||
| from typing import Any, List | ||
|
|
||
| from rlp.exceptions import ( | ||
| ObjectDeserializationError, | ||
| ObjectSerializationError, | ||
| ) | ||
|
|
||
|
|
||
| class DynamicSerializable(ABC): | ||
| @property | ||
| @abstractmethod | ||
| def sedes_options(self) -> List: | ||
| ... | ||
|
|
||
| @classmethod | ||
| def deserialize(cls, obj: Any) -> bytes: | ||
| for sedes in cls.sedes_options: | ||
| try: | ||
| return sedes.deserialize(obj) | ||
| except ObjectDeserializationError: | ||
| pass | ||
|
|
||
| @classmethod | ||
| def serialize(cls, obj: Any) -> bytes: | ||
| for sedes in cls.sedes_options: | ||
| try: | ||
| return sedes.serialize(obj) | ||
| except ObjectSerializationError: | ||
| pass | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # Sometimes elements of a Serializable might be arbitrarily byte-strings or lists. | ||
| # See: https://github.com/ethereum/pyrlp/issues/112#issuecomment-785326962 | ||
|
|
||
| import rlp | ||
| from rlp.sedes import big_endian_int, binary, CountableList | ||
| from rlp.sedes.dynamic import DynamicSerializable | ||
| from rlp.sedes.serializable import Serializable | ||
|
|
||
|
|
||
| class LegacyTransaction(Serializable): | ||
| fields = [ | ||
| ('nonce', big_endian_int), | ||
| ('gasprice', big_endian_int), | ||
| # ...snip | ||
| ] | ||
|
|
||
|
|
||
| class TypedTransaction(DynamicSerializable): | ||
| sedes_options = [ | ||
| LegacyTransaction, | ||
| binary, # New typed transactions are opaque bytes | ||
| ] | ||
|
|
||
|
|
||
| class Block(Serializable): | ||
| fields = [ | ||
| ('transaction_list', CountableList(TypedTransaction)), | ||
| ] | ||
|
|
||
|
|
||
| class LegacyBlock(Serializable): | ||
| fields = [ | ||
| ('transaction_list', CountableList(LegacyTransaction)), | ||
| ] | ||
|
|
||
|
|
||
| def test_dynamic_internal_legacy(): | ||
| # Just establish that the basic test setup is working | ||
| block = LegacyBlock([LegacyTransaction(1, 2)]) | ||
| assert block.transaction_list[0].nonce == 1 | ||
| assert block.transaction_list[0].gasprice == 2 | ||
|
|
||
| serialized = rlp.encode(block) | ||
| assert serialized == b'\xc4\xc3\xc2\x01\x02' | ||
|
|
||
| decoded_block = rlp.decode(serialized, sedes=LegacyBlock) | ||
| assert decoded_block == block | ||
| assert decoded_block.transaction_list[0].nonce == 1 | ||
| assert decoded_block.transaction_list[0].gasprice == 2 | ||
|
|
||
|
|
||
| def test_dynamic_internal_list(): | ||
| block = Block([LegacyTransaction(1, 2)]) | ||
| assert block.transaction_list[0].nonce == 1 | ||
| assert block.transaction_list[0].gasprice == 2 | ||
|
|
||
| serialized = rlp.encode(block) | ||
| assert serialized == b'\xc4\xc3\xc2\x01\x02' | ||
|
|
||
| decoded_block = rlp.decode(serialized, sedes=Block) | ||
| assert decoded_block == block | ||
| assert decoded_block.transaction_list[0].nonce == 1 | ||
| assert decoded_block.transaction_list[0].gasprice == 2 | ||
|
|
||
|
|
||
| def test_dynamic_internal_bytes(): | ||
| block = Block([b'\x01new-type']) | ||
| assert block.transaction_list[0] == b'\x01new-type' | ||
|
|
||
| serialized = rlp.encode(block) | ||
| assert serialized == b'\xcb\xca\x89\x01new-type' | ||
|
|
||
| decoded_block = rlp.decode(serialized, sedes=Block) | ||
| assert decoded_block == block | ||
| assert decoded_block.transaction_list[0] == b'\x01new-type' | ||
|
|
||
|
|
||
| def test_dynamic_internal_combined(): | ||
| block = Block([LegacyTransaction(1, 2), b'\x01new-type']) | ||
| assert block.transaction_list[0].nonce == 1 | ||
| assert block.transaction_list[0].gasprice == 2 | ||
| assert block.transaction_list[1] == b'\x01new-type' | ||
|
|
||
| serialized = rlp.encode(block) | ||
| assert serialized == b'\xce\xcd\xc2\x01\x02\x89\x01new-type' | ||
|
|
||
| decoded_block = rlp.decode(serialized, sedes=Block) | ||
| assert decoded_block == block | ||
| assert decoded_block.transaction_list[0].nonce == 1 | ||
| assert decoded_block.transaction_list[0].gasprice == 2 | ||
| assert decoded_block.transaction_list[1] == b'\x01new-type' |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if all of the sedes options result in
ObjectDeserializationError. Won't that result in this returningNone?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sorry I "marked for review" a bit early. That's one of the TODOs listed above.