Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Commit

Permalink
Bug: Fix missing main_scope field (#1523)
Browse files Browse the repository at this point in the history
Authored-by: fishseabowl <[email protected]>
  • Loading branch information
fishseabowl authored May 13, 2024
1 parent 674c97b commit d910a44
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions starknet-rpc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
8 changes: 5 additions & 3 deletions starknet-rpc-test/get_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ 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;
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]
Expand Down Expand Up @@ -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;

Expand All @@ -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(),
Expand Down
8 changes: 5 additions & 3 deletions starknet-rpc-test/get_class_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ 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;
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]
Expand Down Expand Up @@ -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;

Expand All @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions starknet-rpc-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<D>(deserializer: D) -> Result<Self, D::Error>
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 })
}
}

0 comments on commit d910a44

Please sign in to comment.