From f31e426a65225b9830bbf3c148f7ea05bf9dc257 Mon Sep 17 00:00:00 2001 From: Jonathan LEI Date: Tue, 30 Jul 2024 00:45:01 +0800 Subject: [PATCH] test: contract artifact deserliazation integrity (#644) 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. --- starknet-core/src/types/contract/legacy.rs | 27 +++++++++++++++++++++ starknet-core/src/types/contract/mod.rs | 28 +++++++++++++++++----- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/starknet-core/src/types/contract/legacy.rs b/starknet-core/src/types/contract/legacy.rs index 4608bd7a..a6edda7f 100644 --- a/starknet-core/src/types/contract/legacy.rs +++ b/starknet-core/src/types/contract/legacy.rs @@ -984,6 +984,7 @@ fn should_skip_attributes_for_hinted_hash(value: &Option>) #[cfg(test)] mod tests { + use super::super::ContractArtifact; use super::*; #[derive(serde::Deserialize)] @@ -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::(raw_artifact).unwrap(); + let deser_via_contract_artifact = + match serde_json::from_str::(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() { diff --git a/starknet-core/src/types/contract/mod.rs b/starknet-core/src/types/contract/mod.rs index 8d50753f..1d626b42 100644 --- a/starknet-core/src/types/contract/mod.rs +++ b/starknet-core/src/types/contract/mod.rs @@ -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::(raw_artifact) { - Ok(ContractArtifact::SierraClass(_)) => {} + let direct_deser = serde_json::from_str::(raw_artifact).unwrap(); + let via_contract_artifact = match serde_json::from_str::(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() + ); } } @@ -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::(raw_artifact) { - Ok(ContractArtifact::CompiledClass(_)) => {} + let direct_deser = serde_json::from_str::(raw_artifact).unwrap(); + let via_contract_artifact = match serde_json::from_str::(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() + ); } }