From 8367f394b0cb12bdcf556e896f0efe54dd62230f Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Thu, 19 Sep 2024 14:57:05 +0200 Subject: [PATCH 01/17] Fix test --- crates/forge/src/run_tests/resolve_config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/forge/src/run_tests/resolve_config.rs b/crates/forge/src/run_tests/resolve_config.rs index cf900d276d..0090b38118 100644 --- a/crates/forge/src/run_tests/resolve_config.rs +++ b/crates/forge/src/run_tests/resolve_config.rs @@ -182,8 +182,8 @@ mod tests { &[ForkTarget { name: "SOME_NAME".to_string(), url: "unparsable_url".to_string(), - block_id_type: "Tag".to_string(), - block_id_value: "Latest".to_string(), + block_id_type: "tag".to_string(), + block_id_value: "latest".to_string(), }], &mut BlockNumberMap::default() ) From 620849b1ddb331e7239f8ffb2197d080f799648c Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Mon, 23 Sep 2024 12:14:43 +0200 Subject: [PATCH 02/17] Ymmmm first try --- crates/forge/src/run_tests/resolve_config.rs | 38 +++++--------------- crates/forge/src/scarb/config.rs | 17 +++++---- 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/crates/forge/src/run_tests/resolve_config.rs b/crates/forge/src/run_tests/resolve_config.rs index 0090b38118..79d37fb274 100644 --- a/crates/forge/src/run_tests/resolve_config.rs +++ b/crates/forge/src/run_tests/resolve_config.rs @@ -10,7 +10,6 @@ use forge_runner::package_tests::{ TestTargetWithResolvedConfig, }, }; -use num_bigint::BigInt; use starknet_api::block::BlockNumber; pub async fn resolve_config( @@ -77,27 +76,6 @@ async fn resolve_fork_config( Ok(Some(ResolvedForkConfig { url, block_number })) } -fn parse_block_id(fork_target: &ForkTarget) -> Result { - let block_id = match fork_target.block_id_type.as_str() { - "number" => BlockId::BlockNumber(fork_target.block_id_value.parse()?), - "hash" => { - let block_hash = fork_target.block_id_value.parse::()?; - - BlockId::BlockHash(block_hash.into()) - } - "tag" => { - if fork_target.block_id_value == "latest" { - BlockId::BlockTag - } else { - Err(anyhow!(r#"only "latest" block tag is supported"#))? - } - } - _ => Err(anyhow!("block_id must be one of (number | hash | tag)"))?, - }; - - Ok(block_id) -} - fn replace_id_with_params( raw_fork_config: RawForkConfig, fork_targets: &[ForkTarget], @@ -114,10 +92,10 @@ fn replace_id_with_params( anyhow!("Fork configuration named = {name} not found in the Scarb.toml") })?; - let block_id = parse_block_id(fork_target_from_runner_config)?; + let block_id = fork_target_from_runner_config.block_id.clone(); Ok(InlineForkConfig { - url: fork_target_from_runner_config.url.parse()?, + url: fork_target_from_runner_config.url.clone(), block: block_id, }) } @@ -179,12 +157,12 @@ mod tests { assert!(resolve_config( mocked_tests, - &[ForkTarget { - name: "SOME_NAME".to_string(), - url: "unparsable_url".to_string(), - block_id_type: "tag".to_string(), - block_id_value: "latest".to_string(), - }], + &[ForkTarget::new( + "SOME_NAME".to_string(), + "unparsable_url".to_string(), + "tag".to_string(), + "latest".to_string() + )], &mut BlockNumberMap::default() ) .await diff --git a/crates/forge/src/scarb/config.rs b/crates/forge/src/scarb/config.rs index 0940c3d582..7f85846dfb 100644 --- a/crates/forge/src/scarb/config.rs +++ b/crates/forge/src/scarb/config.rs @@ -5,6 +5,8 @@ use std::{ collections::{HashMap, HashSet}, num::NonZeroU32, }; +use url::Url; +use cheatnet::runtime_extensions::forge_config_extension::config::BlockId; #[allow(clippy::module_name_repetitions)] #[allow(clippy::struct_excessive_bools)] @@ -34,9 +36,8 @@ pub struct ForgeConfigFromScarb { #[derive(Debug, PartialEq, Clone)] pub struct ForkTarget { pub name: String, - pub url: String, - pub block_id_type: String, - pub block_id_value: String, + pub url: Url, + pub block_id: BlockId } impl ForkTarget { @@ -44,9 +45,13 @@ impl ForkTarget { pub fn new(name: String, url: String, block_id_type: String, block_id_value: String) -> Self { Self { name, - url, - block_id_type, - block_id_value, + url: Url::parse(&url).expect("Failed to parse fork url"), + block_id: match block_id_type.as_str() { + "number" => BlockId::BlockNumber(block_id_value.parse().expect("Failed to parse block number")), + "hash" => BlockId::BlockHash(block_id_value.parse().expect("Failed to parse block hash")), + "tag" => BlockId::BlockTag, + _ => panic!("block_id must be one of (number | hash | tag)"), + } } } } From d6f396e58c8749bbfa7cb8b0265b5920fea654d8 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Mon, 23 Sep 2024 12:26:16 +0200 Subject: [PATCH 03/17] Remove 'to_runnable_unparsable_url' test --- crates/forge/src/run_tests/resolve_config.rs | 44 -------------------- 1 file changed, 44 deletions(-) diff --git a/crates/forge/src/run_tests/resolve_config.rs b/crates/forge/src/run_tests/resolve_config.rs index 79d37fb274..74fc4c5f8e 100644 --- a/crates/forge/src/run_tests/resolve_config.rs +++ b/crates/forge/src/run_tests/resolve_config.rs @@ -125,50 +125,6 @@ mod tests { } } - #[tokio::test] - async fn to_runnable_unparsable_url() { - let mocked_tests = TestTargetWithConfig { - sierra_program: program_for_testing(), - casm_program: Arc::new(compile_sierra_to_casm(&program_for_testing().program).unwrap()), - test_cases: vec![TestCaseWithConfig { - name: "crate1::do_thing".to_string(), - config: TestCaseConfig { - available_gas: None, - ignored: false, - expected_result: ExpectedTestResult::Success, - fork_config: Some(RawForkConfig::Named("SOME_NAME".into())), - fuzzer_config: None, - }, - test_details: TestDetails { - sierra_entry_point_statement_idx: 100, - parameter_types: vec![ - (GenericTypeId("RangeCheck".into()), 1), - (GenericTypeId("GasBuiltin".into()), 1), - ], - return_types: vec![ - (GenericTypeId("RangeCheck".into()), 1), - (GenericTypeId("GasBuiltin".into()), 1), - (GenericTypeId("Enum".into()), 3), - ], - }, - }], - tests_location: TestTargetLocation::Lib, - }; - - assert!(resolve_config( - mocked_tests, - &[ForkTarget::new( - "SOME_NAME".to_string(), - "unparsable_url".to_string(), - "tag".to_string(), - "latest".to_string() - )], - &mut BlockNumberMap::default() - ) - .await - .is_err()); - } - #[tokio::test] async fn to_runnable_non_existent_id() { let mocked_tests = TestTargetWithConfig { From edf50eb55d1309ca1b239be82715efb43b34fc9c Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Mon, 23 Sep 2024 13:41:27 +0200 Subject: [PATCH 04/17] Fmt --- crates/forge/src/run_tests/resolve_config.rs | 2 +- crates/forge/src/scarb/config.rs | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/forge/src/run_tests/resolve_config.rs b/crates/forge/src/run_tests/resolve_config.rs index 74fc4c5f8e..201875f4cb 100644 --- a/crates/forge/src/run_tests/resolve_config.rs +++ b/crates/forge/src/run_tests/resolve_config.rs @@ -160,7 +160,7 @@ mod tests { &[ForkTarget::new( "definitely_non_existing".to_string(), "https://not_taken.com".to_string(), - "Number".to_string(), + "number".to_string(), "120".to_string(), )], &mut BlockNumberMap::default() diff --git a/crates/forge/src/scarb/config.rs b/crates/forge/src/scarb/config.rs index 7f85846dfb..16ba1ac3d8 100644 --- a/crates/forge/src/scarb/config.rs +++ b/crates/forge/src/scarb/config.rs @@ -1,4 +1,5 @@ use anyhow::{bail, Result}; +use cheatnet::runtime_extensions::forge_config_extension::config::BlockId; use itertools::Itertools; use serde::Deserialize; use std::{ @@ -6,7 +7,6 @@ use std::{ num::NonZeroU32, }; use url::Url; -use cheatnet::runtime_extensions::forge_config_extension::config::BlockId; #[allow(clippy::module_name_repetitions)] #[allow(clippy::struct_excessive_bools)] @@ -37,7 +37,7 @@ pub struct ForgeConfigFromScarb { pub struct ForkTarget { pub name: String, pub url: Url, - pub block_id: BlockId + pub block_id: BlockId, } impl ForkTarget { @@ -47,11 +47,17 @@ impl ForkTarget { name, url: Url::parse(&url).expect("Failed to parse fork url"), block_id: match block_id_type.as_str() { - "number" => BlockId::BlockNumber(block_id_value.parse().expect("Failed to parse block number")), - "hash" => BlockId::BlockHash(block_id_value.parse().expect("Failed to parse block hash")), + "number" => BlockId::BlockNumber( + block_id_value + .parse() + .expect("Failed to parse block number"), + ), + "hash" => { + BlockId::BlockHash(block_id_value.parse().expect("Failed to parse block hash")) + } "tag" => BlockId::BlockTag, _ => panic!("block_id must be one of (number | hash | tag)"), - } + }, } } } From 24d4e7422172b795636b42df53025e0ccbab7425 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Mon, 23 Sep 2024 14:03:02 +0200 Subject: [PATCH 05/17] Lint --- crates/forge/src/run_tests/resolve_config.rs | 6 ++--- crates/forge/src/scarb.rs | 24 ++++++++++---------- crates/forge/src/scarb/config.rs | 12 +++++----- crates/forge/tests/integration/setup_fork.rs | 6 ++--- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/crates/forge/src/run_tests/resolve_config.rs b/crates/forge/src/run_tests/resolve_config.rs index 201875f4cb..e0b171bd30 100644 --- a/crates/forge/src/run_tests/resolve_config.rs +++ b/crates/forge/src/run_tests/resolve_config.rs @@ -159,9 +159,9 @@ mod tests { mocked_tests, &[ForkTarget::new( "definitely_non_existing".to_string(), - "https://not_taken.com".to_string(), - "number".to_string(), - "120".to_string(), + "https://not_taken.com", + "number", + "120", )], &mut BlockNumberMap::default() ) diff --git a/crates/forge/src/scarb.rs b/crates/forge/src/scarb.rs index 5018494b8e..7c43bc9282 100644 --- a/crates/forge/src/scarb.rs +++ b/crates/forge/src/scarb.rs @@ -204,21 +204,21 @@ mod tests { fork: vec![ ForkTarget::new( "FIRST_FORK_NAME".to_string(), - "http://some.rpc.url".to_string(), - "number".to_string(), - "1".to_string(), + "http://some.rpc.url", + "number", + "1", ), ForkTarget::new( "SECOND_FORK_NAME".to_string(), - "http://some.rpc.url".to_string(), - "hash".to_string(), - "1".to_string(), + "http://some.rpc.url", + "hash", + "1", ), ForkTarget::new( "THIRD_FORK_NAME".to_string(), - "http://some.rpc.url".to_string(), - "tag".to_string(), - "latest".to_string(), + "http://some.rpc.url", + "tag", + "latest", ) ], fuzzer_runs: None, @@ -446,9 +446,9 @@ mod tests { exit_first: false, fork: vec![ForkTarget::new( "ENV_URL_FORK".to_string(), - "http://some.rpc.url_from_env".to_string(), - "number".to_string(), - "1".to_string(), + "http://some.rpc.url_from_env", + "number", + "1", )], fuzzer_runs: None, fuzzer_seed: None, diff --git a/crates/forge/src/scarb/config.rs b/crates/forge/src/scarb/config.rs index 16ba1ac3d8..3d89fead8b 100644 --- a/crates/forge/src/scarb/config.rs +++ b/crates/forge/src/scarb/config.rs @@ -42,11 +42,11 @@ pub struct ForkTarget { impl ForkTarget { #[must_use] - pub fn new(name: String, url: String, block_id_type: String, block_id_value: String) -> Self { + pub fn new(name: String, url: &str, block_id_type: &str, block_id_value: &str) -> Self { Self { name, - url: Url::parse(&url).expect("Failed to parse fork url"), - block_id: match block_id_type.as_str() { + url: Url::parse(url).expect("Failed to parse fork url"), + block_id: match block_id_type { "number" => BlockId::BlockNumber( block_id_value .parse() @@ -141,9 +141,9 @@ impl TryFrom for ForgeConfigFromScarb { fork_targets.push(ForkTarget::new( raw_fork_target.name, - raw_fork_target.url, - block_id_type.to_string(), - block_id_value.clone(), + raw_fork_target.url.as_str(), + block_id_type, + block_id_value, )); } diff --git a/crates/forge/tests/integration/setup_fork.rs b/crates/forge/tests/integration/setup_fork.rs index e0eca83869..19ba47bd7b 100644 --- a/crates/forge/tests/integration/setup_fork.rs +++ b/crates/forge/tests/integration/setup_fork.rs @@ -166,9 +166,9 @@ fn fork_aliased_decorator() { }), fork_targets: vec![ForkTarget::new( "FORK_NAME_FROM_SCARB_TOML".to_string(), - node_rpc_url().to_string(), - "tag".to_string(), - "latest".to_string(), + node_rpc_url().as_str(), + "tag", + "latest", )], }, &mut BlockNumberMap::default(), From bf19de2c07331cae1444cd0619a9ad3bcd5321e7 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Mon, 23 Sep 2024 14:58:19 +0200 Subject: [PATCH 06/17] Refactor ForkTarget::new --- crates/forge/src/run_tests/resolve_config.rs | 3 +- crates/forge/src/scarb.rs | 10 +++-- crates/forge/src/scarb/config.rs | 42 +++++++++++--------- crates/forge/tests/integration/setup_fork.rs | 3 +- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/crates/forge/src/run_tests/resolve_config.rs b/crates/forge/src/run_tests/resolve_config.rs index e0b171bd30..6eedce7285 100644 --- a/crates/forge/src/run_tests/resolve_config.rs +++ b/crates/forge/src/run_tests/resolve_config.rs @@ -162,7 +162,8 @@ mod tests { "https://not_taken.com", "number", "120", - )], + ) + .unwrap()], &mut BlockNumberMap::default() ) .await diff --git a/crates/forge/src/scarb.rs b/crates/forge/src/scarb.rs index 7c43bc9282..ad61904820 100644 --- a/crates/forge/src/scarb.rs +++ b/crates/forge/src/scarb.rs @@ -207,19 +207,22 @@ mod tests { "http://some.rpc.url", "number", "1", - ), + ) + .unwrap(), ForkTarget::new( "SECOND_FORK_NAME".to_string(), "http://some.rpc.url", "hash", "1", - ), + ) + .unwrap(), ForkTarget::new( "THIRD_FORK_NAME".to_string(), "http://some.rpc.url", "tag", "latest", ) + .unwrap() ], fuzzer_runs: None, fuzzer_seed: None, @@ -449,7 +452,8 @@ mod tests { "http://some.rpc.url_from_env", "number", "1", - )], + ) + .unwrap()], fuzzer_runs: None, fuzzer_seed: None, max_n_steps: None, diff --git a/crates/forge/src/scarb/config.rs b/crates/forge/src/scarb/config.rs index 3d89fead8b..fdb69e4793 100644 --- a/crates/forge/src/scarb/config.rs +++ b/crates/forge/src/scarb/config.rs @@ -1,4 +1,4 @@ -use anyhow::{bail, Result}; +use anyhow::{anyhow, bail, Result}; use cheatnet::runtime_extensions::forge_config_extension::config::BlockId; use itertools::Itertools; use serde::Deserialize; @@ -41,24 +41,28 @@ pub struct ForkTarget { } impl ForkTarget { - #[must_use] - pub fn new(name: String, url: &str, block_id_type: &str, block_id_value: &str) -> Self { - Self { + pub fn new(name: String, url: &str, block_id_type: &str, block_id_value: &str) -> Result { + let parsed_url = Url::parse(url).map_err(|_| anyhow!("Failed to parse fork url"))?; + let block_id = match block_id_type { + "number" => BlockId::BlockNumber( + block_id_value + .parse() + .map_err(|_| anyhow!("Failed to parse block number"))?, + ), + "hash" => BlockId::BlockHash( + block_id_value + .parse() + .map_err(|_| anyhow!("Failed to parse block hash"))?, + ), + "tag" => BlockId::BlockTag, + _ => return Err(anyhow!("block_id must be one of (number | hash | tag)")), + }; + + Ok(Self { name, - url: Url::parse(url).expect("Failed to parse fork url"), - block_id: match block_id_type { - "number" => BlockId::BlockNumber( - block_id_value - .parse() - .expect("Failed to parse block number"), - ), - "hash" => { - BlockId::BlockHash(block_id_value.parse().expect("Failed to parse block hash")) - } - "tag" => BlockId::BlockTag, - _ => panic!("block_id must be one of (number | hash | tag)"), - }, - } + url: parsed_url, + block_id, + }) } } @@ -144,7 +148,7 @@ impl TryFrom for ForgeConfigFromScarb { raw_fork_target.url.as_str(), block_id_type, block_id_value, - )); + )?); } Ok(ForgeConfigFromScarb { diff --git a/crates/forge/tests/integration/setup_fork.rs b/crates/forge/tests/integration/setup_fork.rs index 19ba47bd7b..5014221be2 100644 --- a/crates/forge/tests/integration/setup_fork.rs +++ b/crates/forge/tests/integration/setup_fork.rs @@ -169,7 +169,8 @@ fn fork_aliased_decorator() { node_rpc_url().as_str(), "tag", "latest", - )], + ) + .unwrap()], }, &mut BlockNumberMap::default(), )) From e75299a092e1c7162d66c14d28d0cb7efa27c706 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Mon, 23 Sep 2024 16:19:24 +0200 Subject: [PATCH 07/17] Improve value matching --- crates/forge/src/scarb/config.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/forge/src/scarb/config.rs b/crates/forge/src/scarb/config.rs index fdb69e4793..c2e6dec9b1 100644 --- a/crates/forge/src/scarb/config.rs +++ b/crates/forge/src/scarb/config.rs @@ -54,7 +54,10 @@ impl ForkTarget { .parse() .map_err(|_| anyhow!("Failed to parse block hash"))?, ), - "tag" => BlockId::BlockTag, + "tag" => match block_id_value { + "latest" => BlockId::BlockTag, + _ => return Err(anyhow!("block_id.tag can only be equal to 'latest'")), + } _ => return Err(anyhow!("block_id must be one of (number | hash | tag)")), }; From 7e704a5696677145ac368b0dc008e635e9ab59cc Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Mon, 23 Sep 2024 16:36:13 +0200 Subject: [PATCH 08/17] Lint --- crates/forge/src/scarb/config.rs | 2 +- crates/forge/tests/integration/setup_fork.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/forge/src/scarb/config.rs b/crates/forge/src/scarb/config.rs index c2e6dec9b1..e6725cefce 100644 --- a/crates/forge/src/scarb/config.rs +++ b/crates/forge/src/scarb/config.rs @@ -57,7 +57,7 @@ impl ForkTarget { "tag" => match block_id_value { "latest" => BlockId::BlockTag, _ => return Err(anyhow!("block_id.tag can only be equal to 'latest'")), - } + }, _ => return Err(anyhow!("block_id must be one of (number | hash | tag)")), }; diff --git a/crates/forge/tests/integration/setup_fork.rs b/crates/forge/tests/integration/setup_fork.rs index 06464b7dc4..b802a1166c 100644 --- a/crates/forge/tests/integration/setup_fork.rs +++ b/crates/forge/tests/integration/setup_fork.rs @@ -258,10 +258,10 @@ fn fork_aliased_decorator_overrding() { }), fork_targets: vec![ForkTarget::new( "FORK_NAME_FROM_SCARB_TOML".to_string(), - node_rpc_url().to_string(), - "number".to_string(), - "12341234".to_string(), - )], + node_rpc_url().as_str(), + "number", + "12341234", + ).unwrap()], }, &mut BlockNumberMap::default(), )) From c10e812ffed31811f0f001185fbfd50264f0460e Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Tue, 24 Sep 2024 08:47:56 +0200 Subject: [PATCH 09/17] Fmt --- crates/forge/tests/integration/setup_fork.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/forge/tests/integration/setup_fork.rs b/crates/forge/tests/integration/setup_fork.rs index b802a1166c..b41aed5352 100644 --- a/crates/forge/tests/integration/setup_fork.rs +++ b/crates/forge/tests/integration/setup_fork.rs @@ -261,7 +261,8 @@ fn fork_aliased_decorator_overrding() { node_rpc_url().as_str(), "number", "12341234", - ).unwrap()], + ) + .unwrap()], }, &mut BlockNumberMap::default(), )) From 372a5db0b370661838d733b11a6915d064e7e842 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:18:59 +0200 Subject: [PATCH 10/17] Add test to 'Fork Target' --- crates/forge/src/scarb/config.rs | 122 +++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/crates/forge/src/scarb/config.rs b/crates/forge/src/scarb/config.rs index e6725cefce..2be0f557f9 100644 --- a/crates/forge/src/scarb/config.rs +++ b/crates/forge/src/scarb/config.rs @@ -167,3 +167,125 @@ impl TryFrom for ForgeConfigFromScarb { }) } } + + +#[cfg(test)] +mod tests { + use super::*; + use url::Url; + use num_bigint::BigInt; + + #[test] + fn test_fork_target_new_valid_number() { + let name = "TestFork".to_string(); + let url = "http://example.com"; + let block_id_type = "number"; + let block_id_value = "123"; + + let fork_target = ForkTarget::new(name.clone(), url, block_id_type, block_id_value).unwrap(); + + assert_eq!(fork_target.name, name); + assert_eq!(fork_target.url, Url::parse(url).unwrap()); + if let BlockId::BlockNumber(number) = fork_target.block_id { + assert_eq!(number, 123); + } else { + panic!("Expected BlockId::BlockNumber"); + } + } + + #[test] + fn test_fork_target_new_valid_hash() { + let name = "TestFork".to_string(); + let url = "http://example.com"; + let block_id_type = "hash"; + let block_id_value = "0x1"; + + let fork_target = ForkTarget::new(name.clone(), url, block_id_type, block_id_value).unwrap(); + + assert_eq!(fork_target.name, name); + assert_eq!(fork_target.url, Url::parse(url).unwrap()); + if let BlockId::BlockHash(hash) = fork_target.block_id { + assert_eq!(hash.to_bigint(), BigInt::from(1)); + } else { + panic!("Expected BlockId::BlockHash"); + } + } + + #[test] + fn test_fork_target_new_valid_tag() { + let name = "TestFork".to_string(); + let url = "http://example.com"; + let block_id_type = "tag"; + let block_id_value = "latest"; + + let fork_target = ForkTarget::new(name.clone(), url, block_id_type, block_id_value).unwrap(); + + assert_eq!(fork_target.name, name); + assert_eq!(fork_target.url, Url::parse(url).unwrap()); + if let BlockId::BlockTag = fork_target.block_id { + // Expected variant + } else { + panic!("Expected BlockId::BlockTag"); + } + } + + #[test] + fn test_fork_target_new_invalid_url() { + let name = "TestFork".to_string(); + let url = "invalid_url"; + let block_id_type = "number"; + let block_id_value = "123"; + + let result = ForkTarget::new(name, url, block_id_type, block_id_value); + assert!(result.is_err()); + assert_eq!(result.unwrap_err().to_string(), "Failed to parse fork url"); + } + + #[test] + fn test_fork_target_new_invalid_block_id_type() { + let name = "TestFork".to_string(); + let url = "http://example.com"; + let block_id_type = "invalid_type"; + let block_id_value = "123"; + + let result = ForkTarget::new(name, url, block_id_type, block_id_value); + assert!(result.is_err()); + assert_eq!(result.unwrap_err().to_string(), "block_id must be one of (number | hash | tag)"); + } + + #[test] + fn test_fork_target_new_invalid_block_id_value_number() { + let name = "TestFork".to_string(); + let url = "http://example.com"; + let block_id_type = "number"; + let block_id_value = "invalid_number"; + + let result = ForkTarget::new(name, url, block_id_type, block_id_value); + assert!(result.is_err()); + assert_eq!(result.unwrap_err().to_string(), "Failed to parse block number"); + } + + #[test] + fn test_fork_target_new_invalid_block_id_value_hash() { + let name = "TestFork".to_string(); + let url = "http://example.com"; + let block_id_type = "hash"; + let block_id_value = "invalid_hash"; + + let result = ForkTarget::new(name, url, block_id_type, block_id_value); + assert!(result.is_err()); + assert_eq!(result.unwrap_err().to_string(), "Failed to parse block hash"); + } + + #[test] + fn test_fork_target_new_invalid_block_id_value_tag() { + let name = "TestFork".to_string(); + let url = "http://example.com"; + let block_id_type = "tag"; + let block_id_value = "invalid_tag"; + + let result = ForkTarget::new(name, url, block_id_type, block_id_value); + assert!(result.is_err()); + assert_eq!(result.unwrap_err().to_string(), "block_id.tag can only be equal to 'latest'"); + } +} \ No newline at end of file From f02777e6157ed8166f81fe7afd59812a85af8352 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:24:12 +0200 Subject: [PATCH 11/17] Change name type in ForkTarget::new --- crates/forge/src/run_tests/resolve_config.rs | 2 +- crates/forge/src/scarb.rs | 29 +++-------- crates/forge/src/scarb/config.rs | 53 ++++++++++++-------- crates/forge/tests/integration/setup_fork.rs | 4 +- 4 files changed, 42 insertions(+), 46 deletions(-) diff --git a/crates/forge/src/run_tests/resolve_config.rs b/crates/forge/src/run_tests/resolve_config.rs index 320e8f3f49..6b57852023 100644 --- a/crates/forge/src/run_tests/resolve_config.rs +++ b/crates/forge/src/run_tests/resolve_config.rs @@ -174,7 +174,7 @@ mod tests { assert!(resolve_config( mocked_tests, &[ForkTarget::new( - "definitely_non_existing".to_string(), + "definitely_non_existing", "https://not_taken.com", "number", "120", diff --git a/crates/forge/src/scarb.rs b/crates/forge/src/scarb.rs index ad61904820..d083173fe3 100644 --- a/crates/forge/src/scarb.rs +++ b/crates/forge/src/scarb.rs @@ -202,27 +202,12 @@ mod tests { ForgeConfigFromScarb { exit_first: false, fork: vec![ - ForkTarget::new( - "FIRST_FORK_NAME".to_string(), - "http://some.rpc.url", - "number", - "1", - ) - .unwrap(), - ForkTarget::new( - "SECOND_FORK_NAME".to_string(), - "http://some.rpc.url", - "hash", - "1", - ) - .unwrap(), - ForkTarget::new( - "THIRD_FORK_NAME".to_string(), - "http://some.rpc.url", - "tag", - "latest", - ) - .unwrap() + ForkTarget::new("FIRST_FORK_NAME", "http://some.rpc.url", "number", "1",) + .unwrap(), + ForkTarget::new("SECOND_FORK_NAME", "http://some.rpc.url", "hash", "1",) + .unwrap(), + ForkTarget::new("THIRD_FORK_NAME", "http://some.rpc.url", "tag", "latest",) + .unwrap() ], fuzzer_runs: None, fuzzer_seed: None, @@ -448,7 +433,7 @@ mod tests { ForgeConfigFromScarb { exit_first: false, fork: vec![ForkTarget::new( - "ENV_URL_FORK".to_string(), + "ENV_URL_FORK", "http://some.rpc.url_from_env", "number", "1", diff --git a/crates/forge/src/scarb/config.rs b/crates/forge/src/scarb/config.rs index 2be0f557f9..8c6f574f0e 100644 --- a/crates/forge/src/scarb/config.rs +++ b/crates/forge/src/scarb/config.rs @@ -41,7 +41,7 @@ pub struct ForkTarget { } impl ForkTarget { - pub fn new(name: String, url: &str, block_id_type: &str, block_id_value: &str) -> Result { + pub fn new(name: &str, url: &str, block_id_type: &str, block_id_value: &str) -> Result { let parsed_url = Url::parse(url).map_err(|_| anyhow!("Failed to parse fork url"))?; let block_id = match block_id_type { "number" => BlockId::BlockNumber( @@ -62,7 +62,7 @@ impl ForkTarget { }; Ok(Self { - name, + name: name.to_string(), url: parsed_url, block_id, }) @@ -147,7 +147,7 @@ impl TryFrom for ForgeConfigFromScarb { raw_fork_target.block_id.iter().exactly_one().unwrap(); fork_targets.push(ForkTarget::new( - raw_fork_target.name, + raw_fork_target.name.as_str(), raw_fork_target.url.as_str(), block_id_type, block_id_value, @@ -168,21 +168,20 @@ impl TryFrom for ForgeConfigFromScarb { } } - #[cfg(test)] mod tests { use super::*; - use url::Url; use num_bigint::BigInt; + use url::Url; #[test] fn test_fork_target_new_valid_number() { - let name = "TestFork".to_string(); + let name = "TestFork"; let url = "http://example.com"; let block_id_type = "number"; let block_id_value = "123"; - let fork_target = ForkTarget::new(name.clone(), url, block_id_type, block_id_value).unwrap(); + let fork_target = ForkTarget::new(name, url, block_id_type, block_id_value).unwrap(); assert_eq!(fork_target.name, name); assert_eq!(fork_target.url, Url::parse(url).unwrap()); @@ -195,12 +194,12 @@ mod tests { #[test] fn test_fork_target_new_valid_hash() { - let name = "TestFork".to_string(); + let name = "TestFork"; let url = "http://example.com"; let block_id_type = "hash"; let block_id_value = "0x1"; - let fork_target = ForkTarget::new(name.clone(), url, block_id_type, block_id_value).unwrap(); + let fork_target = ForkTarget::new(name, url, block_id_type, block_id_value).unwrap(); assert_eq!(fork_target.name, name); assert_eq!(fork_target.url, Url::parse(url).unwrap()); @@ -213,12 +212,12 @@ mod tests { #[test] fn test_fork_target_new_valid_tag() { - let name = "TestFork".to_string(); + let name = "TestFork"; let url = "http://example.com"; let block_id_type = "tag"; let block_id_value = "latest"; - let fork_target = ForkTarget::new(name.clone(), url, block_id_type, block_id_value).unwrap(); + let fork_target = ForkTarget::new(name, url, block_id_type, block_id_value).unwrap(); assert_eq!(fork_target.name, name); assert_eq!(fork_target.url, Url::parse(url).unwrap()); @@ -231,7 +230,7 @@ mod tests { #[test] fn test_fork_target_new_invalid_url() { - let name = "TestFork".to_string(); + let name = "TestFork"; let url = "invalid_url"; let block_id_type = "number"; let block_id_value = "123"; @@ -243,49 +242,61 @@ mod tests { #[test] fn test_fork_target_new_invalid_block_id_type() { - let name = "TestFork".to_string(); + let name = "TestFork"; let url = "http://example.com"; let block_id_type = "invalid_type"; let block_id_value = "123"; let result = ForkTarget::new(name, url, block_id_type, block_id_value); assert!(result.is_err()); - assert_eq!(result.unwrap_err().to_string(), "block_id must be one of (number | hash | tag)"); + assert_eq!( + result.unwrap_err().to_string(), + "block_id must be one of (number | hash | tag)" + ); } #[test] fn test_fork_target_new_invalid_block_id_value_number() { - let name = "TestFork".to_string(); + let name = "TestFork"; let url = "http://example.com"; let block_id_type = "number"; let block_id_value = "invalid_number"; let result = ForkTarget::new(name, url, block_id_type, block_id_value); assert!(result.is_err()); - assert_eq!(result.unwrap_err().to_string(), "Failed to parse block number"); + assert_eq!( + result.unwrap_err().to_string(), + "Failed to parse block number" + ); } #[test] fn test_fork_target_new_invalid_block_id_value_hash() { - let name = "TestFork".to_string(); + let name = "TestFork"; let url = "http://example.com"; let block_id_type = "hash"; let block_id_value = "invalid_hash"; let result = ForkTarget::new(name, url, block_id_type, block_id_value); assert!(result.is_err()); - assert_eq!(result.unwrap_err().to_string(), "Failed to parse block hash"); + assert_eq!( + result.unwrap_err().to_string(), + "Failed to parse block hash" + ); } #[test] fn test_fork_target_new_invalid_block_id_value_tag() { - let name = "TestFork".to_string(); + let name = "TestFork"; let url = "http://example.com"; let block_id_type = "tag"; let block_id_value = "invalid_tag"; let result = ForkTarget::new(name, url, block_id_type, block_id_value); assert!(result.is_err()); - assert_eq!(result.unwrap_err().to_string(), "block_id.tag can only be equal to 'latest'"); + assert_eq!( + result.unwrap_err().to_string(), + "block_id.tag can only be equal to 'latest'" + ); } -} \ No newline at end of file +} diff --git a/crates/forge/tests/integration/setup_fork.rs b/crates/forge/tests/integration/setup_fork.rs index b41aed5352..6e4688c635 100644 --- a/crates/forge/tests/integration/setup_fork.rs +++ b/crates/forge/tests/integration/setup_fork.rs @@ -165,7 +165,7 @@ fn fork_aliased_decorator() { }), }), fork_targets: vec![ForkTarget::new( - "FORK_NAME_FROM_SCARB_TOML".to_string(), + "FORK_NAME_FROM_SCARB_TOML", node_rpc_url().as_str(), "tag", "latest", @@ -257,7 +257,7 @@ fn fork_aliased_decorator_overrding() { }), }), fork_targets: vec![ForkTarget::new( - "FORK_NAME_FROM_SCARB_TOML".to_string(), + "FORK_NAME_FROM_SCARB_TOML", node_rpc_url().as_str(), "number", "12341234", From 9830fee8a8fc08f3314ef6e7b2868a9d34804dd2 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Tue, 24 Sep 2024 13:12:36 +0200 Subject: [PATCH 12/17] Fix typo in 'setup_package' --- crates/scarb-api/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/scarb-api/src/lib.rs b/crates/scarb-api/src/lib.rs index 6e059cfd38..137d346edc 100644 --- a/crates/scarb-api/src/lib.rs +++ b/crates/scarb-api/src/lib.rs @@ -262,7 +262,7 @@ mod tests { [[tool.snforge.fork]] name = "THIRD_FORK_NAME" url = "http://some.rpc.url" - block_id.tag = "Latest" + block_id.tag = "latest" "#, package_name, snforge_std_path From 1eb3b00b282a58770b2f3f032260a849fa02d291 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Wed, 25 Sep 2024 11:51:15 +0200 Subject: [PATCH 13/17] Update logic --- crates/forge/src/scarb/config.rs | 51 ++++---------------------------- 1 file changed, 6 insertions(+), 45 deletions(-) diff --git a/crates/forge/src/scarb/config.rs b/crates/forge/src/scarb/config.rs index 8c6f574f0e..41a58189c2 100644 --- a/crates/forge/src/scarb/config.rs +++ b/crates/forge/src/scarb/config.rs @@ -56,9 +56,9 @@ impl ForkTarget { ), "tag" => match block_id_value { "latest" => BlockId::BlockTag, - _ => return Err(anyhow!("block_id.tag can only be equal to 'latest'")), + _ => bail!("block_id.tag can only be equal to latest"), }, - _ => return Err(anyhow!("block_id must be one of (number | hash | tag)")), + block_id_key => bail!("block_id = {block_id_key} is not valid. Possible values are = \"number\", \"hash\" and \"tag\""), }; Ok(Self { @@ -116,21 +116,12 @@ fn validate_raw_fork_config(raw_config: RawForgeConfig) -> Result Date: Wed, 25 Sep 2024 12:00:32 +0200 Subject: [PATCH 14/17] Make logic more cleaner --- crates/forge/src/scarb/config.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/forge/src/scarb/config.rs b/crates/forge/src/scarb/config.rs index 41a58189c2..dbf0d3acde 100644 --- a/crates/forge/src/scarb/config.rs +++ b/crates/forge/src/scarb/config.rs @@ -116,12 +116,12 @@ fn validate_raw_fork_config(raw_config: RawForgeConfig) -> Result Ok(()), + _ => bail!("block_id should be set once per fork"), + })?; Ok(raw_config) } From 218d6f9d4fdb7c8323df38fb58b3753ace567ab4 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Wed, 25 Sep 2024 13:22:45 +0200 Subject: [PATCH 15/17] Add happy path test for loading fork conifig with 'block_id.tag = 'latest'' --- crates/forge/src/scarb.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/crates/forge/src/scarb.rs b/crates/forge/src/scarb.rs index d083173fe3..0e963225b4 100644 --- a/crates/forge/src/scarb.rs +++ b/crates/forge/src/scarb.rs @@ -131,6 +131,7 @@ mod tests { use std::env; use std::str::FromStr; use test_utils::tempdir_with_tool_versions; + use cheatnet::runtime_extensions::forge_config_extension::config::BlockId; fn setup_package(package_name: &str) -> TempDir { let temp = tempdir_with_tool_versions().unwrap(); @@ -398,6 +399,36 @@ mod tests { assert!(format!("{err:?}").contains("block_id.tag can only be equal to latest")); } + #[test] + fn get_forge_config_for_package_with_block_tag() { + let temp = setup_package("simple_package"); + let content = indoc!( + r#" + [package] + name = "simple_package" + version = "0.1.0" + + [[tool.snforge.fork]] + name = "SAME_NAME" + url = "http://some.rpc.url" + block_id.tag = "latest" + "# + ); + temp.child("Scarb.toml").write_str(content).unwrap(); + + let scarb_metadata = ScarbCommand::metadata() + .inherit_stderr() + .current_dir(temp.path()) + .run() + .unwrap(); + + let forge_config = load_package_config::( + &scarb_metadata, + &scarb_metadata.workspace.members[0], + ).unwrap(); + assert_eq!(forge_config.fork[0].block_id, BlockId::BlockTag); + } + #[test] fn get_forge_config_resolves_env_variables() { let temp = setup_package("simple_package"); From 5ee6892882926297fce91c06f7f59950c208021a Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Wed, 25 Sep 2024 13:23:33 +0200 Subject: [PATCH 16/17] Fmt --- crates/forge/src/scarb.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/forge/src/scarb.rs b/crates/forge/src/scarb.rs index 0e963225b4..9e8101285b 100644 --- a/crates/forge/src/scarb.rs +++ b/crates/forge/src/scarb.rs @@ -124,6 +124,7 @@ mod tests { use assert_fs::fixture::{FileWriteStr, PathChild, PathCopy}; use assert_fs::TempDir; use camino::Utf8PathBuf; + use cheatnet::runtime_extensions::forge_config_extension::config::BlockId; use configuration::load_package_config; use indoc::{formatdoc, indoc}; use scarb_api::metadata::MetadataCommandExt; @@ -131,7 +132,6 @@ mod tests { use std::env; use std::str::FromStr; use test_utils::tempdir_with_tool_versions; - use cheatnet::runtime_extensions::forge_config_extension::config::BlockId; fn setup_package(package_name: &str) -> TempDir { let temp = tempdir_with_tool_versions().unwrap(); @@ -425,7 +425,8 @@ mod tests { let forge_config = load_package_config::( &scarb_metadata, &scarb_metadata.workspace.members[0], - ).unwrap(); + ) + .unwrap(); assert_eq!(forge_config.fork[0].block_id, BlockId::BlockTag); } From c0063634f0814b55400cac6ef5327a9c9b01ed26 Mon Sep 17 00:00:00 2001 From: kkawula <57270771+kkawula@users.noreply.github.com> Date: Thu, 26 Sep 2024 12:41:20 +0200 Subject: [PATCH 17/17] Add tests that check if it is possible to pass felt as either decimal or hex --- crates/forge/src/scarb.rs | 13 ++++++++++--- crates/forge/tests/integration/setup_fork.rs | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/forge/src/scarb.rs b/crates/forge/src/scarb.rs index 9e8101285b..f66fe5555b 100644 --- a/crates/forge/src/scarb.rs +++ b/crates/forge/src/scarb.rs @@ -168,11 +168,16 @@ mod tests { [[tool.snforge.fork]] name = "SECOND_FORK_NAME" url = "http://some.rpc.url" - block_id.hash = "1" + block_id.hash = "0xa" [[tool.snforge.fork]] name = "THIRD_FORK_NAME" url = "http://some.rpc.url" + block_id.hash = "10" + + [[tool.snforge.fork]] + name = "FOURTH_FORK_NAME" + url = "http://some.rpc.url" block_id.tag = "latest" "#, package_name, @@ -205,9 +210,11 @@ mod tests { fork: vec![ ForkTarget::new("FIRST_FORK_NAME", "http://some.rpc.url", "number", "1",) .unwrap(), - ForkTarget::new("SECOND_FORK_NAME", "http://some.rpc.url", "hash", "1",) + ForkTarget::new("SECOND_FORK_NAME", "http://some.rpc.url", "hash", "10",) + .unwrap(), + ForkTarget::new("THIRD_FORK_NAME", "http://some.rpc.url", "hash", "0xa",) .unwrap(), - ForkTarget::new("THIRD_FORK_NAME", "http://some.rpc.url", "tag", "latest",) + ForkTarget::new("FOURTH_FORK_NAME", "http://some.rpc.url", "tag", "latest",) .unwrap() ], fuzzer_runs: None, diff --git a/crates/forge/tests/integration/setup_fork.rs b/crates/forge/tests/integration/setup_fork.rs index 6e4688c635..06ce5b4426 100644 --- a/crates/forge/tests/integration/setup_fork.rs +++ b/crates/forge/tests/integration/setup_fork.rs @@ -368,6 +368,26 @@ fn get_block_info_in_forked_block() { assert(block_info.block_timestamp > 1711645884, block_info.block_timestamp.into()); assert(block_info.block_number > 54060, block_info.block_number.into()); }} + + #[test] + #[fork(url: "{node_rpc_url}", block_hash: 0x06ae121e46f5375f93b00475fb130348ae38148e121f84b0865e17542e9485de)] + fn test_fork_get_block_info_block_hash() {{ + let block_info = starknet::get_block_info().unbox(); + assert(block_info.block_timestamp == 1711645884, block_info.block_timestamp.into()); + assert(block_info.block_number == 54060, block_info.block_number.into()); + let expected_sequencer_addr = contract_address_const::<0x1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8>(); + assert(block_info.sequencer_address == expected_sequencer_addr, block_info.sequencer_address.into()); + }} + + #[test] + #[fork(url: "{node_rpc_url}", block_hash: 3021433528476416000728121069095289682281028310523383289416465162415092565470)] + fn test_fork_get_block_info_block_hash_with_number() {{ + let block_info = starknet::get_block_info().unbox(); + assert(block_info.block_timestamp == 1711645884, block_info.block_timestamp.into()); + assert(block_info.block_number == 54060, block_info.block_number.into()); + let expected_sequencer_addr = contract_address_const::<0x1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8>(); + assert(block_info.sequencer_address == expected_sequencer_addr, block_info.sequencer_address.into()); + }} "#, node_rpc_url = node_rpc_url() ).as_str(),