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

[CBOR] Lotus and go-f3 ser/de compatibility. #4840

Open
ruseinov opened this issue Oct 3, 2024 · 0 comments
Open

[CBOR] Lotus and go-f3 ser/de compatibility. #4840

ruseinov opened this issue Oct 3, 2024 · 0 comments
Labels
Type: Task Discrete task to implement

Comments

@ruseinov
Copy link
Contributor

ruseinov commented Oct 3, 2024

While investigating go-f3 and lotus implementations I have stumbled upon the generated code that checks certain fields for length. That's specified as e.g. cborgen:"maxlen=1000000000.

Some examples:

  1. Lotus
  2. Go-F3

Going forward it might be nice to have sort of library that'd wrap cbor ser/de and be able to read attributes.
Another thing is that for structs we currently need to implement custom ser/de as follows:

impl Serialize for PowerEntry {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        if !self.pub_key.is_valid() {
            return Err(serde::ser::Error::custom(
                "Byte array in field pub_key is too long",
            ));
        }


        (&self.id,
         &self.power,
         &self.pub_key).serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for PowerEntry {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let (id, power, pub_key) = Deserialize::deserialize(deserializer)?;
        Ok(Self {
            id,
            power,
            pub_key,
        })
    }
}

What this does:

  1. Implements said length constraints.
  2. Converts a struct into a tuple in order to serialize
  3. Converts a tuple into a struct in order to deserialize.

If those could be automated - that'd save a lot of boilerplate. Field ordering is easy - just serialize in that same order they are defined. If any of the struct fields need to be transient - have an attribute to ignore them for cbor ser/de.

@ruseinov ruseinov added Enhancement Type: Task Discrete task to implement and removed Enhancement labels Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Task Discrete task to implement
Projects
Status: New
Development

No branches or pull requests

1 participant