cairo-vm: Why does Program do not implement `serde`?

In vm/src/types/program.rs:

    pub fn serialize(&self) -> Result<Vec<u8>, ProgramError> {
        let program_serializer: ProgramSerializer = ProgramSerializer::from(self);
        let bytes: Vec<u8> = serde_json::to_vec(&program_serializer)?;
        Ok(bytes)
    }

    pub fn deserialize(
        program_serializer_bytes: &[u8],
        entrypoint: Option<&str>,
    ) -> Result<Program, ProgramError> {
        let program_serializer: ProgramSerializer =
            serde_json::from_slice(program_serializer_bytes)?;
        let program_json = ProgramJson::from(program_serializer);
        let program = parse_program_json(program_json, entrypoint)?;
        Ok(program)
    }

Program does not implement the traits Serialize/Deserialize, but instead those two methods who do the same thing, only in a way that is not compatible with the serde crate. Why? I break upstream usage of the traits. If I have a struct that contains a Program (like blockifier::ContractClassV1Inner ), I cannot derive the correct trait onto it.

Why not just implement the trait?

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 18 (17 by maintainers)

Most upvoted comments

I think structs containing a 1 to 1 version of the compiled cairo programs should be standarized (like contract classes in starknet-api) so users don’t have to depend on this crate for it

My input would be to:

  • drop the whole custom deserializer thing you have
  • define a struct for each outside world struct you want to be able to load (DeprecatedProgram, Program) (or v0, v1) (or legacy, idk)
  • implement a two-way serde for those (you should be able to call serialize then deserialize without losing data)
  • impl From<ThoseClassesYouJustCreated> for ExecutableProgram (ExecutableProgram being what is currently named Program)

Then if someone still needs it for some reason, we can easily derive Serde onto the ExecutableProgram while making it clear that this is not the format outputted by the compiler.