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

removes fallible ErasureMeta.fec_set_index type-casts #1841

Merged
merged 1 commit into from
Jul 19, 2024

Conversation

behzadnouri
Copy link

@behzadnouri behzadnouri commented Jun 24, 2024

Problem

fec_set_index is u32 by the definition in the shred struct. Blockstore however uses u64 for ErasureMeta.fec_set_index for backward compatibility reasons which results in weird typing and fallible conversions from u64 to u32.
Any ErasureMeta with a fec_set_index which does not fit in u32 is invalid and should be discarded early.

Summary of Changes

The commit changes ErasureMeta.fec_set_index type to u32 while using serde attributes to maintain backward compatibility for (de)serialization.

@behzadnouri behzadnouri force-pushed the serde-compat-fec-set-index branch 3 times, most recently from 4b21f3e to 5f35b48 Compare July 18, 2024 14:43
fec_set_index is u32 by the definition in the shred struct. Blockstore
however uses u64 for ErasureMeta.fec_set_index for backward
compatibility reasons which results in weird typing and fallible
conversions from u64 to u32. Any ErasureMeta with a fec_set_index which
does not fit in u32 is invalid and should be discarded early.

The commit changes ErasureMeta.fec_set_index type to u32 while using
serde attributes to maintain backward compatibility for
(de)serialization.
Comment on lines +143 to +168
// Serializes a value of type T by first type-casting to type R.
pub(super) fn serialize<S: Serializer, R, T: Copy>(
&val: &T,
serializer: S,
) -> Result<S::Ok, S::Error>
where
R: TryFrom<T> + Serialize,
<R as TryFrom<T>>::Error: std::fmt::Display,
{
R::try_from(val)
.map_err(serde::ser::Error::custom)?
.serialize(serializer)
}

// Deserializes a value of type R and type-casts it to type T.
pub(super) fn deserialize<'de, D, R, T>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
R: Deserialize<'de>,
T: TryFrom<R>,
<T as TryFrom<R>>::Error: std::fmt::Display,
{
R::deserialize(deserializer)
.map(T::try_from)?
.map_err(serde::de::Error::custom)
}
Copy link
Author

Choose a reason for hiding this comment

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

intentionally generic so that it can be used elsewhere for similar type discrepancies.

Copy link

@AshwinSekar AshwinSekar left a comment

Choose a reason for hiding this comment

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

LGTM maybe the serde module can also be used for SlotMeta fields? and eventually we can do an overhaul to key blockstore by u32 for shreds as well.

@behzadnouri
Copy link
Author

LGTM maybe the serde module can also be used for SlotMeta fields? and eventually we can do an overhaul to key blockstore by u32 for shreds as well.

yeah, looking into other blockstore fields with mismatching types.

@behzadnouri behzadnouri merged commit 09458ef into anza-xyz:master Jul 19, 2024
41 checks passed
@behzadnouri behzadnouri deleted the serde-compat-fec-set-index branch July 19, 2024 14:10
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 this pull request may close these issues.

2 participants