refactor: decode ContentValue only with known ContentKey #1403
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.
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
andContentValue::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 basicallyalloy_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 useVec<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 ofVec<u8>
, makes code more readable.Created #1404 to track this.
To-Do