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

Cannot (de)serialize objects with optional fields #112

Open
lithp opened this issue Mar 26, 2019 · 3 comments · May be fixed by #131
Open

Cannot (de)serialize objects with optional fields #112

lithp opened this issue Mar 26, 2019 · 3 comments · May be fixed by #131

Comments

@lithp
Copy link

lithp commented Mar 26, 2019

Parity uses a custom BlockDetails object with a very weird last field.

is_finalized is a boolean field with the following representation:

  • A value of True is represented as a True boolean field: len(BlockDetails) == 5
  • A value of False is represented as nothing: len(BlockDetails) == 4

Presumably they wanted to save a little space in the database by simply not saving this field.

I don't think it's possible to create a subclass of rlp.Serializable which handles this use-case.

@carver
Copy link
Contributor

carver commented Apr 16, 2019

One option is a method dedicated to pattern matching and dispatching, used like:

sedes_options = (BlockDetails, ShortBlockDetails)
try:
  rlp_obj, sedes_idx = rlp.dispatch_decode(encoded, sedes=sedes_options)
except rlp.AmbiguousDispatchError:
  # multiple sedes were capable of decoding the rlp, so no way to know which one is correct
except rlp.MissingDispatchError:
  # None of the sedes could decode the rlp

assert sedes_index in (0, 1)  # identifies which serializer was used

where rlp.MissingDispatchError is a subclass of rlp.DecodingError


I think the logic of: "these two serializers are actually for the same thing and we should merge them" should not be in pyrlp. It's easy enough to wrap the above example to create that effect.

@carver
Copy link
Contributor

carver commented Feb 24, 2021

Oof, looks like we need to support serializing structures that are sometimes lists and sometimes byte-strings, so this becomes relevant again. (see https://ethereum-magicians.org/t/eip-2718-typed-transaction-envelope/4355/59?u=carver )

Looking into options now...

@carver
Copy link
Contributor

carver commented Mar 3, 2021

Actually, this is pretty straightforward to just implement by hand, and I'm not sure the generalized solution would have added much value here. Maybe just a new page in the docs. See a WIP example here: ethereum/py-evm#1980 Note how BerlinTransactionBuilder does all of this pretty quickly in a deserialize() method, which can then be used as a sedes class in rlp.decode().

pacrob pushed a commit to pacrob/pyrlp that referenced this issue Apr 17, 2024
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 a pull request may close this issue.

2 participants