Skip to content

Commit

Permalink
test: contract artifact deserliazation integrity (#644)
Browse files Browse the repository at this point in the history
Adds test cases that asserts contract artifacts are
not corrupted whether deserialized directly or via the
`ContractArtifact` enum. The test for `LegacyContractClass` -
`test_legacy_artifact_deser_from_contract_artifact` - is ignored due to
a known issue #392.
  • Loading branch information
xJonathanLEI authored Jul 29, 2024
1 parent 2774834 commit f31e426
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
27 changes: 27 additions & 0 deletions starknet-core/src/types/contract/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@ fn should_skip_attributes_for_hinted_hash(value: &Option<Vec<LegacyAttribute>>)

#[cfg(test)]
mod tests {
use super::super::ContractArtifact;
use super::*;

#[derive(serde::Deserialize)]
Expand All @@ -1007,6 +1008,32 @@ mod tests {
}
}

#[test]
#[ignore = "https://github.com/xJonathanLEI/starknet-rs/issues/392"]
fn test_legacy_artifact_deser_from_contract_artifact() {
for raw_artifact in [
include_str!("../../../test-data/contracts/cairo0/artifacts/oz_account.txt"),
include_str!("../../../test-data/contracts/cairo0/artifacts/event_example.txt"),
include_str!("../../../test-data/contracts/cairo0/artifacts/pre-0.11.0/oz_account.txt"),
include_str!(
"../../../test-data/contracts/cairo0/artifacts/pre-0.11.0/event_example.txt"
),
] {
let direct_deser = serde_json::from_str::<LegacyContractClass>(raw_artifact).unwrap();
let deser_via_contract_artifact =
match serde_json::from_str::<ContractArtifact>(raw_artifact).unwrap() {
ContractArtifact::LegacyClass(class) => class,
_ => panic!("unexpected artifact type"),
};

// Class should be identical however it's deserialized
assert_eq!(
direct_deser.class_hash().unwrap(),
deser_via_contract_artifact.class_hash().unwrap()
);
}
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_contract_class_hash() {
Expand Down
28 changes: 22 additions & 6 deletions starknet-core/src/types/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,10 +1023,18 @@ mod tests {
include_str!("../../../test-data/contracts/cairo2/artifacts/erc20_sierra.txt"),
include_str!("../../../test-data/contracts/cairo2.6/artifacts/erc20_sierra.txt"),
] {
match serde_json::from_str::<ContractArtifact>(raw_artifact) {
Ok(ContractArtifact::SierraClass(_)) => {}
let direct_deser = serde_json::from_str::<SierraClass>(raw_artifact).unwrap();
let via_contract_artifact = match serde_json::from_str::<ContractArtifact>(raw_artifact)
{
Ok(ContractArtifact::SierraClass(class)) => class,
_ => panic!("Unexpected result"),
}
};

// Class should be identical however it's deserialized
assert_eq!(
direct_deser.class_hash().unwrap(),
via_contract_artifact.class_hash().unwrap()
);
}
}

Expand All @@ -1040,10 +1048,18 @@ mod tests {
include_str!("../../../test-data/contracts/cairo2/artifacts/erc20_compiled.txt"),
include_str!("../../../test-data/contracts/cairo2.6/artifacts/erc20_compiled.txt"),
] {
match serde_json::from_str::<ContractArtifact>(raw_artifact) {
Ok(ContractArtifact::CompiledClass(_)) => {}
let direct_deser = serde_json::from_str::<CompiledClass>(raw_artifact).unwrap();
let via_contract_artifact = match serde_json::from_str::<ContractArtifact>(raw_artifact)
{
Ok(ContractArtifact::CompiledClass(class)) => class,
_ => panic!("Unexpected result"),
}
};

// Class should be identical however it's deserialized
assert_eq!(
direct_deser.class_hash().unwrap(),
via_contract_artifact.class_hash().unwrap()
);
}
}

Expand Down

0 comments on commit f31e426

Please sign in to comment.