Skip to content

refactor: add serialized class defs newtypes#3340

Open
CHr15F0x wants to merge 8 commits intomainfrom
chris/serialized-class-defs-newtypes
Open

refactor: add serialized class defs newtypes#3340
CHr15F0x wants to merge 8 commits intomainfrom
chris/serialized-class-defs-newtypes

Conversation

@CHr15F0x
Copy link
Copy Markdown
Contributor

@CHr15F0x CHr15F0x commented Apr 15, 2026

Fixes: #3326

Rationale: we have 4 distinct types which are all passed around as Vec<u8>. Just until recently this seemed good enough (though absolutely against static typing and a "everything is a type approach" that leverages the compiler), until it didn't. I spent a good couple of days trying to find a regression that I introduced, which was a simple copy-pasta caused by using a Vec<u8> Sierra where Casm was actually expected. That was enough.


The PR introduces 4 types:

  • SerializedSierraDefinition
  • SerializedCasmDefinition
  • SerializedCairoDefinition
  • SerializedOpaqueClassDefinition, where the internal Vec<u8> is either sierra or cairo, but we don't really care because it's all passed around to the end consumer without the need to figure out which variant it really is; this is also how classes are stored and served via the RPC
  • SerializedClassDefinition, which is the opposite of the above - it's an enum, which distinguishes between the two variants

I tried to make the changes transparent and not modify the semantics of the APIs, with one notable exception: compute_class_hash, where it actually made sense to consume the buffer and reinterpret it as either of the variants in a safe manner, so that download_class itself does not have to do the reinterpreting in an unsafe manner.

I left all the fixtures as is to keep the delta reasonably small (which it already isn't).

@CHr15F0x CHr15F0x force-pushed the chris/serialized-class-defs-newtypes branch 5 times, most recently from 5646390 to 2c0a166 Compare April 16, 2026 08:45
@CHr15F0x CHr15F0x marked this pull request as ready for review April 16, 2026 09:01
@CHr15F0x CHr15F0x requested a review from a team as a code owner April 16, 2026 09:01
@CHr15F0x CHr15F0x changed the title WIP refactor: add serialized class defs newtypes refactor: add serialized class defs newtypes Apr 16, 2026
Comment thread crates/storage/src/fake.rs Outdated
@CHr15F0x CHr15F0x force-pushed the chris/serialized-class-defs-newtypes branch from defb65a to 3de8656 Compare April 16, 2026 15:00
Copy link
Copy Markdown
Contributor

@zvolin zvolin left a comment

Choose a reason for hiding this comment

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

nice refactor 💪

Comment on lines +888 to +890
compute_class_hash(SerializedOpaqueClassDefinition::from_slice(
CAIRO_0_8_NEW_ATTRIBUTES,
))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could also use the hash helper

#[tokio::test]
async fn cairo_0_11_with_decimal_entry_point_offset() {
let hash = compute_class_hash(CAIRO_0_11_WITH_DECIMAL_ENTRY_POINT_OFFSET).unwrap();
let (hash, _) = compute_class_hash(SerializedOpaqueClassDefinition::from_slice(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same about helper

fn try_from(value: &str) -> Result<Self, Self::Error> {
serde_json::from_str(value)
impl CasmContractClass {
pub fn try_from_serialized_definition(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit, I don't think it would be semantically wrong if we'd name those from_serialized_def, which is way shorter. Wdyt? also in ContractClass

Comment on lines -415 to -418
// Keeping the "experimental" list for backwards
// compatibility. Also, the names in
// BlockifierLibfuncs would have to be mapped to
// (audited|testnet|experimental)_v0.1.0 .
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

are those removals intentional? they kinda seem valid

}

fn sierra_version_from_class(
fn try_sierra_version_from_class(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd drop the try prefix, but up to you

Comment on lines +527 to +531
include_bytes!(
"../../fixtures/contracts/\
integration_class_0x5ae9d09292a50ed48c5930904c880dab56e85b825022a7d689cfc9e65e01ee7.\
json"
),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

formatting looks bit weird now, also the comma

.context("Fetching class definition")?;
let class_definition: crate::types::class::sierra::SierraContractClass =
serde_json::from_str(&String::from_utf8(class_definition)?)
serde_json::from_str(&String::from_utf8(class_definition.into_bytes())?)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ik this was just translated but couldn't it use from_slice?

pub const CLASS_DEFINITION_MAX_ALLOWED_SIZE: u64 = 4 * 1024 * 1024;

#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Dummy)]
pub struct SerializedSierraDefinition(Vec<u8>);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

tempting to just slap Bytes in all those, but should probably be another PR 😅 worth an issue?

Self(bytes)
}

pub fn from_slice(bytes: &[u8]) -> Self {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I get that being verbose is kinda a purpose of this PR, but sometimes, while reading, I wished you've added impl<S: AsRef<[u8]>> From<S> for SerializedXyzDefinition 😅
not sure tho if that wouldn't introduce surface for similar bugs you experienced tho

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.

refactor: Introduce separate types for serialized casm and sierra definitions

2 participants