From 0fdc521d24a353b8d7b1355e45b287d70cb9b01a Mon Sep 17 00:00:00 2001 From: Fusee Date: Fri, 13 Oct 2023 16:09:37 +0200 Subject: [PATCH 1/6] improved error handling when cloning the blockchain --- mocking/src/errors/mocking.rs | 10 ++++++- mocking/src/world/infos.rs | 56 ++++++++++++++++++++++------------- token/src/error/token.rs | 3 +- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/mocking/src/errors/mocking.rs b/mocking/src/errors/mocking.rs index 6d48b52..f6095f5 100644 --- a/mocking/src/errors/mocking.rs +++ b/mocking/src/errors/mocking.rs @@ -1,16 +1,24 @@ use serde::{Deserialize, Serialize}; use novax::errors::NovaXError; +use novax_token::error::token::TokenError; #[derive(Serialize, Deserialize, PartialEq, Clone, Debug)] pub enum NovaXMockingError { UnableToFetchAddressKeys, UnableToParseAddressKeys, UnableToReadInfosFromFile, - NovaXError(NovaXError) + NovaXError(NovaXError), + NovaXTokenError(TokenError) } impl From for NovaXMockingError { fn from(value: NovaXError) -> Self { NovaXMockingError::NovaXError(value) } +} + +impl From for NovaXMockingError { + fn from(value: TokenError) -> Self { + NovaXMockingError::NovaXTokenError(value) + } } \ No newline at end of file diff --git a/mocking/src/world/infos.rs b/mocking/src/world/infos.rs index 887300f..937807b 100644 --- a/mocking/src/world/infos.rs +++ b/mocking/src/world/infos.rs @@ -82,7 +82,7 @@ impl ScenarioWorldInfos { pub async fn fetch( gateway_url: &str, addresses: &[Address] - ) -> ScenarioWorldInfos { + ) -> Result { let ( accounts, keys, @@ -93,11 +93,13 @@ impl ScenarioWorldInfos { get_addresses_balances(gateway_url, addresses) ).await; - ScenarioWorldInfos { - address_keys: keys, - address_balances: balances, - address_infos: accounts, - } + let infos = ScenarioWorldInfos { + address_keys: keys?, + address_balances: balances?, + address_infos: accounts?, + }; + + Ok(infos) } pub fn into_world(self, register: RegisterFunction) -> ScenarioWorld @@ -171,7 +173,6 @@ impl ScenarioWorldInfos { } } } - } set_state_accounts_step = set_state_accounts_step.put_account(&*encoded_contract_address_expr, account); @@ -194,7 +195,7 @@ impl ScenarioWorldInfos { } } -async fn get_addresses_infos(gateway_url: &str, addresses: &[Address]) -> Vec { +async fn get_addresses_infos(gateway_url: &str, addresses: &[Address]) -> Result, NovaXMockingError> { let mut accounts_futures = vec![]; for address in addresses { accounts_futures.push(async { @@ -203,12 +204,16 @@ async fn get_addresses_infos(gateway_url: &str, addresses: &[Address]) -> Vec> = join_all(accounts_futures).await; - accounts.into_iter() - .filter_map(|e| e.ok()) - .collect() + let mut results = vec![]; + + for account in accounts { + results.push(account?) + } + + Ok(results) } -async fn get_addresses_keys(gateway_url: &str, addresses: &[Address]) -> HashMap<[u8; 32], AddressKeys> { +async fn get_addresses_keys(gateway_url: &str, addresses: &[Address]) -> Result, NovaXMockingError> { let mut keys_futures = vec![]; for address in addresses { keys_futures.push(async { @@ -217,12 +222,17 @@ async fn get_addresses_keys(gateway_url: &str, addresses: &[Address]) -> HashMap }; let keys: Vec> = join_all(keys_futures).await; - keys.into_iter() - .filter_map(|e| e.ok()) - .collect() + let mut results = HashMap::new(); + + for key in keys { + let key = key?; + results.insert(key.0, key.1); + } + + Ok(results) } -async fn get_addresses_balances(gateway_url: &str, addresses: &[Address]) -> HashMap<[u8; 32], Vec> { +async fn get_addresses_balances(gateway_url: &str, addresses: &[Address]) -> Result>, NovaXMockingError> { let mut balances_futures = vec![]; for address in addresses { balances_futures.push(async { @@ -232,9 +242,13 @@ async fn get_addresses_balances(gateway_url: &str, addresses: &[Address]) -> Has }) }; - let balances: BalanceAsyncResults = join_all(balances_futures).await; - balances.into_iter() - .filter_map(|e| e.ok()) + let balances_results: BalanceAsyncResults = join_all(balances_futures).await; + let mut balances = vec![]; + for balance in balances_results { + balances.push(balance?) + } + + let result = balances.into_iter() .map(|e| { let mut amounts: Vec = vec![]; for infos in e.1 { @@ -248,7 +262,9 @@ async fn get_addresses_balances(gateway_url: &str, addresses: &[Address]) -> Has (e.0, amounts) }) - .collect() + .collect(); + + Ok(result) } fn convert_hashmap_address_keys_to_bytes(hashmap: HashMap) -> HashMap<[u8; 32], T> { diff --git a/token/src/error/token.rs b/token/src/error/token.rs index 1c97437..e8acf16 100644 --- a/token/src/error/token.rs +++ b/token/src/error/token.rs @@ -1,6 +1,7 @@ +use serde::{Deserialize, Serialize}; use novax::errors::NovaXError; -#[derive(PartialEq, Debug)] +#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)] pub enum TokenError { TokenNotFound { token_identifier: String }, UnknownErrorForToken { token_identifier: String }, From c7b67cb25b7020d9f5e6c604251f7b8053edb30b Mon Sep 17 00:00:00 2001 From: Fusee Date: Fri, 13 Oct 2023 16:12:04 +0200 Subject: [PATCH 2/6] fixed clippy --- executor/src/mocking/executor.rs | 2 +- executor/src/network/transaction.rs | 2 +- mocking/src/gateway/keys.rs | 6 +++--- mocking/src/world/infos.rs | 2 +- tester/core/tests/network_deploy.rs | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/executor/src/mocking/executor.rs b/executor/src/mocking/executor.rs index 7c84fc1..209b020 100644 --- a/executor/src/mocking/executor.rs +++ b/executor/src/mocking/executor.rs @@ -152,7 +152,7 @@ impl DeployExecutor for MockExecutor let sc_deploy_step = sc_deploy_step.as_mut(); - let owned_sc_deploy_step = mem::replace(sc_deploy_step, ScDeployStep::new().into()); + let owned_sc_deploy_step = mem::replace(sc_deploy_step, ScDeployStep::new()); *sc_deploy_step = owned_sc_deploy_step.from(&caller); { diff --git a/executor/src/network/transaction.rs b/executor/src/network/transaction.rs index df723e8..f344925 100644 --- a/executor/src/network/transaction.rs +++ b/executor/src/network/transaction.rs @@ -107,7 +107,7 @@ impl DeployExecutor for BaseTransactionNetwork OriginalResult: TopEncodeMulti + Send + Sync, { let sc_deploy_step = sc_deploy_step.as_mut(); - let owned_sc_deploy_step = mem::replace(sc_deploy_step, ScDeployStep::new().into()); + let owned_sc_deploy_step = mem::replace(sc_deploy_step, ScDeployStep::new()); let mut interactor = Interactor::new(&self.gateway_url).await; let sender_address = interactor.register_wallet(self.wallet); *sc_deploy_step = owned_sc_deploy_step.from(&multiversx_sc::types::Address::from(sender_address.to_bytes())); diff --git a/mocking/src/gateway/keys.rs b/mocking/src/gateway/keys.rs index fb673fe..bf1d4fe 100644 --- a/mocking/src/gateway/keys.rs +++ b/mocking/src/gateway/keys.rs @@ -23,10 +23,10 @@ impl AddressKeys { let bech32 = address.to_bech32_string().map_err(NovaXError::from)?; let url = format!("{gateway_url}/address/{bech32}/keys"); - let Ok(response) = reqwest::get(url).await else { return Err(NovaXMockingError::UnableToFetchAddressKeys.into()) }; - let Ok(response) = response.text().await else { return Err(NovaXMockingError::UnableToFetchAddressKeys.into()) }; + let Ok(response) = reqwest::get(url).await else { return Err(NovaXMockingError::UnableToFetchAddressKeys) }; + let Ok(response) = response.text().await else { return Err(NovaXMockingError::UnableToFetchAddressKeys) }; - let Ok(result) = serde_json::from_str::(&response) else { return Err(NovaXMockingError::UnableToParseAddressKeys.into()) }; + let Ok(result) = serde_json::from_str::(&response) else { return Err(NovaXMockingError::UnableToParseAddressKeys) }; Ok(result) } diff --git a/mocking/src/world/infos.rs b/mocking/src/world/infos.rs index 937807b..01a9352 100644 --- a/mocking/src/world/infos.rs +++ b/mocking/src/world/infos.rs @@ -53,7 +53,7 @@ impl ScenarioWorldInfos { .open(file_path) .unwrap(); - let Ok(result) = serde_json::from_reader::<_, ScenarioWorldInfosJson>(reader) else { return Err(NovaXMockingError::UnableToReadInfosFromFile.into()) }; + let Ok(result) = serde_json::from_reader::<_, ScenarioWorldInfosJson>(reader) else { return Err(NovaXMockingError::UnableToReadInfosFromFile) }; Ok(result.into()) } diff --git a/tester/core/tests/network_deploy.rs b/tester/core/tests/network_deploy.rs index 9d46066..d5e2b32 100644 --- a/tester/core/tests/network_deploy.rs +++ b/tester/core/tests/network_deploy.rs @@ -49,7 +49,7 @@ impl BlockchainInteractor for MockInteractor { response.new_deployed_address = Some(Address::from_bech32_string(NEW_CONTRACT).unwrap().into()); let step = sc_deploy_step.as_mut(); - step.response = Some(response.clone()); + step.response = Some(response); } } From 2d1421ddbe07d48f2e19d5d437e111b348d215db Mon Sep 17 00:00:00 2001 From: Fusee Date: Fri, 13 Oct 2023 16:13:01 +0200 Subject: [PATCH 3/6] changed novax-token crate version --- token/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/token/Cargo.toml b/token/Cargo.toml index ce01bef..9dacb28 100644 --- a/token/Cargo.toml +++ b/token/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "novax-token" -version = "0.0.1" +version = "0.0.2" edition = "2021" license = "GPL-3.0-only" description = "The `novax-token` crate offers utilities for retrieving token information from the blockchain, such as querying address balances, token metadata, and other related operations. By encapsulating the logic for interacting with token-related blockchain features, `novax-token` simplifies the process of querying and managing tokens within decentralized applications, making it a valuable tool for developers working with blockchain tokens and assets." From 35f5b46abe644c4e37e8ff5781c4290aa6057389 Mon Sep 17 00:00:00 2001 From: Fusee Date: Fri, 13 Oct 2023 16:18:08 +0200 Subject: [PATCH 4/6] changed novax-mocking crate version --- Cargo.lock | 2 +- mocking/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d3d4bc..1d8336e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1597,7 +1597,7 @@ dependencies = [ [[package]] name = "novax-token" -version = "0.0.1" +version = "0.0.2" dependencies = [ "async-trait", "hex", diff --git a/mocking/Cargo.toml b/mocking/Cargo.toml index aa2ec0a..da6554d 100644 --- a/mocking/Cargo.toml +++ b/mocking/Cargo.toml @@ -13,7 +13,7 @@ num-bigint = "0.4.3" async-trait = "0.1.72" tokio = "1.30.0" novax = { path = "../core", version = "0.0.1" } -novax-token = { path = "../token", version = "0.0.1" } +novax-token = { path = "../token", version = "0.0.2" } multiversx-sc = "0.43.2" multiversx-sc-snippets = "0.43.2" multiversx-sdk = "0.2.0" From 1521a0653f7ffef46dad905d417ff7698d3d0fac Mon Sep 17 00:00:00 2001 From: Fusee Date: Fri, 13 Oct 2023 16:21:43 +0200 Subject: [PATCH 5/6] changed novax-mocking crate version --- mocking/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mocking/Cargo.toml b/mocking/Cargo.toml index da6554d..82a236c 100644 --- a/mocking/Cargo.toml +++ b/mocking/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "novax-mocking" -version = "0.0.1" +version = "0.0.2" edition = "2021" license = "GPL-3.0-only" description = "The `novax-mocking` crate is a part of the Nova framework, providing utilities to simulate blockchain environments. It facilitates the creation of a `MockExecutor` and other simulated entities, enabling easy testing and development against smart contract interactions within a controlled, replicated blockchain scenario." From 71f99de625aa949287e66fc1f371258cbb64deaf Mon Sep 17 00:00:00 2001 From: Fusee Date: Fri, 13 Oct 2023 16:26:33 +0200 Subject: [PATCH 6/6] updated novax-mocking version in the cookbook --- Cargo.lock | 2 +- cookbook/deps/Cargo.lock | 8 ++++---- cookbook/deps/Cargo.toml | 2 +- cookbook/src/mocking/basic.md | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d8336e..06837b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1568,7 +1568,7 @@ dependencies = [ [[package]] name = "novax-mocking" -version = "0.0.1" +version = "0.0.2" dependencies = [ "async-trait", "futures", diff --git a/cookbook/deps/Cargo.lock b/cookbook/deps/Cargo.lock index 0634d78..a1b2131 100644 --- a/cookbook/deps/Cargo.lock +++ b/cookbook/deps/Cargo.lock @@ -1549,9 +1549,9 @@ dependencies = [ [[package]] name = "novax-mocking" -version = "0.0.1" +version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ded7f676d8f48f69361fa1ae954add02be66b656cc80051af24f2399d47c3109" +checksum = "089e0309f785673d69582121cf5ada101ae273701aeb2859b8144eaeab35aecc" dependencies = [ "async-trait", "futures", @@ -1582,9 +1582,9 @@ dependencies = [ [[package]] name = "novax-token" -version = "0.0.1" +version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d88f4961372d5c3720d79735d2f5858c875b2b51131495bfc33f38ca829c8118" +checksum = "8aaf163678b17b4ae13787d3983a769eabc6d7da3ad1b0a22b13fc780c06af17" dependencies = [ "async-trait", "hex", diff --git a/cookbook/deps/Cargo.toml b/cookbook/deps/Cargo.toml index da7ca41..9b441e5 100644 --- a/cookbook/deps/Cargo.toml +++ b/cookbook/deps/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] novax = "0.0.1" novax-caching = "0.0.1" -novax-mocking = "0.0.1" +novax-mocking = "0.0.2" tokio = "1.32.0" num-bigint = "0.4.4" async-trait = "0.1.73" diff --git a/cookbook/src/mocking/basic.md b/cookbook/src/mocking/basic.md index c9f5dcd..a4f8110 100644 --- a/cookbook/src/mocking/basic.md +++ b/cookbook/src/mocking/basic.md @@ -4,7 +4,7 @@ NovaX equips developers with the capability to mock their environments seamlessl Before delving into the mocking process with NovaX, ensure you've added the necessary extension crate to your project. Update your `Cargo.toml` dependencies to include: ```toml -novax-mocking = "0.0.1" +novax-mocking = "0.0.2" ``` Now, let's proceed with how to execute mocked queries, calls, and deploys using NovaX.