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

refactor: decode ContentValue only with known ContentKey #1403

Merged
merged 1 commit into from
Aug 28, 2024

Conversation

morph-dev
Copy link
Collaborator

@morph-dev morph-dev commented Aug 26, 2024

What was wrong?

When decoding *ContentValue, we try decoding all possible types until one succeed. This is dangerous as wrong type could succeed first (when two values have identical encoding). The correct approach is to decode content values in the presence of the content key, which should be known.

This happens for some types of content values on the state network.

This problem doesn't exist for the content keys, as they have unique prefix.

How was it fixed?

Changed ContentValue::decode and ContentValue::from_hex functions to require content key.

This broke the deserialization of the content values (which makes sense as they can't be deserialized correctly without knowledge of the content key), which resulted that we can't use existing *ContentValue types in json apis.

To solve this, I introduced RawContentValue type that is basically alloy_primitives::Bytes (that serialize/deserialize to/from "0x..." strings).

Future work

I tried to keep this PR as small as possible (it's already big enough), but I think that we should use alloy_primitives::Bytes everywhere where we currently use Vec<u8> to represent content key/value. Reason is that copy is much cheaper and it (de)serializes into hex string. Also, having concrete type name instead of Vec<u8>, makes code more readable.

Created #1404 to track this.

To-Do

@morph-dev morph-dev added the state network Issue related to portal state network label Aug 26, 2024
@morph-dev morph-dev added this to the Devcon 2024 milestone Aug 26, 2024
@morph-dev morph-dev self-assigned this Aug 26, 2024
@morph-dev morph-dev marked this pull request as ready for review August 26, 2024 18:50
Copy link
Member

@ogenev ogenev left a comment

Choose a reason for hiding this comment

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

Nothing stands out to me 💯 . This PR is a great improvement and paying some of the tech debts :).

Copy link
Member

@KolbyML KolbyML left a comment

Choose a reason for hiding this comment

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

:shipit: looks good

@morph-dev morph-dev merged commit 1aa4c2d into ethereum:master Aug 28, 2024
8 checks passed
@morph-dev morph-dev deleted the content_value_decode branch August 28, 2024 06:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state network Issue related to portal state network
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants