From d910a4457118cfc131ab3696e8ee8d1a7a83eeb9 Mon Sep 17 00:00:00 2001 From: Xuefei Han Date: Mon, 13 May 2024 04:36:38 -0700 Subject: [PATCH] Bug: Fix missing main_scope field (#1523) Authored-by: fishseabowl --- CHANGELOG.md | 1 + Cargo.lock | 1 + starknet-rpc-test/Cargo.toml | 1 + starknet-rpc-test/get_class.rs | 8 +++++--- starknet-rpc-test/get_class_at.rs | 8 +++++--- starknet-rpc-test/src/lib.rs | 28 ++++++++++++++++++++++++++++ 6 files changed, 41 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6ead47c87..a379975e17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Next release +- bug: fix contract serialisation - fix: starknet_call errs if contract nonexistent - fix: txn hash calculation and refactor - chore: remove all da/settlement related code diff --git a/Cargo.lock b/Cargo.lock index 16a50047cc..0652aefc1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10931,6 +10931,7 @@ dependencies = [ "flate2", "reqwest", "rstest 0.18.2", + "serde", "serde_json", "starknet-accounts", "starknet-contract", diff --git a/starknet-rpc-test/Cargo.toml b/starknet-rpc-test/Cargo.toml index 8eb30d6c60..75bfa4feb1 100644 --- a/starknet-rpc-test/Cargo.toml +++ b/starknet-rpc-test/Cargo.toml @@ -14,6 +14,7 @@ env_logger = "0.9" flate2 = { workspace = true } reqwest = "0.11.18" rstest = "0.18.1" +serde = "1.0.192" serde_json = "1.0.108" starknet-accounts = { workspace = true } starknet-contract = { workspace = true } diff --git a/starknet-rpc-test/get_class.rs b/starknet-rpc-test/get_class.rs index a679f72559..1275927b89 100644 --- a/starknet-rpc-test/get_class.rs +++ b/starknet-rpc-test/get_class.rs @@ -3,7 +3,7 @@ use std::io::Read; use assert_matches::assert_matches; use flate2::read::GzDecoder; use rstest::rstest; -use starknet_core::types::contract::legacy::{LegacyContractClass, LegacyProgram}; +use starknet_core::types::contract::legacy::LegacyContractClass; use starknet_core::types::contract::SierraClass; use starknet_core::types::{BlockId, ContractClass, FlattenedSierraClass, StarknetError}; use starknet_ff::FieldElement; @@ -11,6 +11,7 @@ use starknet_providers::Provider; use starknet_providers::ProviderError::StarknetError as StarknetProviderError; use starknet_rpc_test::constants::{CAIRO_1_ACCOUNT_CONTRACT_CLASS_HASH, TEST_CONTRACT_CLASS_HASH}; use starknet_rpc_test::fixtures::{madara, ThreadSafeMadaraClient}; +use starknet_rpc_test::LegacyProgramWrapper; #[rstest] #[tokio::test] @@ -46,7 +47,7 @@ async fn fail_non_existing_class_hash(madara: &ThreadSafeMadaraClient) -> Result #[rstest] #[tokio::test] -#[ignore = "Waiting for issue #1469 to be solved"] +#[ignore = "Waiting for issue #1585 to be solved"] async fn work_ok_retrieving_class_for_contract_version_0(madara: &ThreadSafeMadaraClient) -> Result<(), anyhow::Error> { let rpc = madara.get_starknet_client().await; @@ -67,7 +68,8 @@ async fn work_ok_retrieving_class_for_contract_version_0(madara: &ThreadSafeMada let mut gz = GzDecoder::new(&c.program[..]); let mut decompressed_bytes = Vec::new(); gz.read_to_end(&mut decompressed_bytes).unwrap(); - let program: LegacyProgram = serde_json::from_slice(decompressed_bytes.as_slice())?; + let legacy_program_wrapper: LegacyProgramWrapper = serde_json::from_slice(decompressed_bytes.as_slice())?; + let program = legacy_program_wrapper.legacy_program; assert_eq!( program.data.len(), test_contract_class.program.data.len(), diff --git a/starknet-rpc-test/get_class_at.rs b/starknet-rpc-test/get_class_at.rs index 20d2972ac8..5c5f998e69 100644 --- a/starknet-rpc-test/get_class_at.rs +++ b/starknet-rpc-test/get_class_at.rs @@ -3,7 +3,7 @@ use std::io::Read; use assert_matches::assert_matches; use flate2::read::GzDecoder; use rstest::rstest; -use starknet_core::types::contract::legacy::{LegacyContractClass, LegacyProgram}; +use starknet_core::types::contract::legacy::LegacyContractClass; use starknet_core::types::contract::SierraClass; use starknet_core::types::{BlockId, ContractClass, FlattenedSierraClass, StarknetError}; use starknet_ff::FieldElement; @@ -11,6 +11,7 @@ use starknet_providers::Provider; use starknet_providers::ProviderError::StarknetError as StarknetProviderError; use starknet_rpc_test::constants::{CAIRO_1_ACCOUNT_CONTRACT_ADDRESS, TEST_CONTRACT_ADDRESS}; use starknet_rpc_test::fixtures::{madara, ThreadSafeMadaraClient}; +use starknet_rpc_test::LegacyProgramWrapper; #[rstest] #[tokio::test] @@ -44,7 +45,7 @@ async fn fail_non_existing_contract(madara: &ThreadSafeMadaraClient) -> Result<( #[rstest] #[tokio::test] -#[ignore = "Waiting for issue #1469 to be solved"] +#[ignore = "Waiting for issue #1585 to be solved"] async fn work_ok_retrieving_class_for_contract_version_0(madara: &ThreadSafeMadaraClient) -> Result<(), anyhow::Error> { let rpc = madara.get_starknet_client().await; @@ -64,7 +65,8 @@ async fn work_ok_retrieving_class_for_contract_version_0(madara: &ThreadSafeMada let mut d = GzDecoder::new(&c.program[..]); let mut data = String::new(); d.read_to_string(&mut data).unwrap(); - let program: LegacyProgram = serde_json::from_str(data.as_str())?; + let legacy_program_wrapper: LegacyProgramWrapper = serde_json::from_str(data.as_str())?; + let program = legacy_program_wrapper.legacy_program; assert_eq!( program.data, test_contract_class.program.data, diff --git a/starknet-rpc-test/src/lib.rs b/starknet-rpc-test/src/lib.rs index 1cdc0a2242..0494678284 100644 --- a/starknet-rpc-test/src/lib.rs +++ b/starknet-rpc-test/src/lib.rs @@ -6,11 +6,13 @@ use std::fmt::Debug; use anyhow::anyhow; use reqwest::header::CONTENT_TYPE; use reqwest::{Client, Response}; +use serde::{de, Deserialize, Deserializer}; use serde_json::json; use starknet_accounts::{ Account, AccountDeployment, AccountError, AccountFactoryError, Declaration, Execution, LegacyDeclaration, OpenZeppelinAccountFactory, SingleOwnerAccount, }; +use starknet_core::types::contract::legacy::LegacyProgram; use starknet_core::types::{DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult}; use starknet_providers::jsonrpc::{HttpTransport, JsonRpcClient}; use starknet_providers::Provider; @@ -214,3 +216,29 @@ impl MadaraClient { Ok(response.status().is_success()) } } + +pub struct LegacyProgramWrapper { + pub legacy_program: LegacyProgram, +} + +impl<'de> Deserialize<'de> for LegacyProgramWrapper { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let value = serde_json::Value::deserialize(deserializer)?; + + // Ensure the required fields are present in the JSON data + let mut json_obj = value.as_object().ok_or_else(|| de::Error::custom("Expected JSON object"))?.clone(); + + // If 'main_scope' field is missing, set it to the default value + if !json_obj.contains_key("main_scope") { + json_obj.insert("main_scope".to_string(), serde_json::Value::String("__main__".to_string())); + } + + // Deserialize the modified JSON data into LegacyProgram + let legacy_program = serde_json::from_value(serde_json::Value::Object(json_obj)).map_err(de::Error::custom)?; + + Ok(LegacyProgramWrapper { legacy_program }) + } +}