From 2bbca773c676f5271f18b02c9fe64c74d9b44d0b Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Mon, 12 Aug 2024 15:33:25 +0300 Subject: [PATCH 01/30] chain-sim - prototype of integration with adder interactor --- contracts/examples/adder/interact/config.toml | 2 +- .../adder/interact/src/basic_interact.rs | 36 ++++++++++--------- .../interactor_scenario/interactor_sc_call.rs | 1 + .../interactor_sc_deploy.rs | 1 + sdk/core/src/gateway.rs | 2 ++ .../src/gateway/gateway_chain_simulator.rs | 32 +++++++++++++++++ 6 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 sdk/core/src/gateway/gateway_chain_simulator.rs diff --git a/contracts/examples/adder/interact/config.toml b/contracts/examples/adder/interact/config.toml index 61ac8dbf87..11eb18de60 100644 --- a/contracts/examples/adder/interact/config.toml +++ b/contracts/examples/adder/interact/config.toml @@ -1 +1 @@ -gateway = 'https://devnet-gateway.multiversx.com' +gateway = 'http://localhost:8085' diff --git a/contracts/examples/adder/interact/src/basic_interact.rs b/contracts/examples/adder/interact/src/basic_interact.rs index 262eb13631..fff69aea1c 100644 --- a/contracts/examples/adder/interact/src/basic_interact.rs +++ b/contracts/examples/adder/interact/src/basic_interact.rs @@ -2,6 +2,8 @@ mod basic_interact_cli; mod basic_interact_config; mod basic_interact_state; +use core::str; + use adder::adder_proxy; use basic_interact_config::Config; use basic_interact_state::State; @@ -63,10 +65,22 @@ impl AdderInteract { .with_tracer(INTERACTOR_SCENARIO_TRACE_PATH) .await; - let adder_owner_address = - interactor.register_wallet(Wallet::from_pem_file("adder-owner.pem").unwrap()); + let adder_owner_address = interactor.register_wallet(test_wallets::alice()); + let wallet_address = interactor.register_wallet(test_wallets::mike()); + interactor + .proxy + .send_user_funds(&Bech32Address::from(&adder_owner_address).to_bech32_string()) + .await; + + interactor + .proxy + .send_user_funds(&Bech32Address::from(&wallet_address).to_bech32_string()) + .await; + + interactor.proxy.generate_blocks(20).await; + Self { interactor, adder_owner_address: adder_owner_address.into(), @@ -75,20 +89,10 @@ impl AdderInteract { } } - async fn set_state(&mut self) { - println!("wallet address: {}", self.wallet_address); - self.interactor - .retrieve_account(&self.adder_owner_address) - .await; - self.interactor.retrieve_account(&self.wallet_address).await; - } - async fn deploy(&mut self) { // warning: multi deploy not yet fully supported // only works with last deployed address - self.set_state().await; - let new_address = self .interactor .tx() @@ -104,7 +108,7 @@ impl AdderInteract { .await; println!("new address: {new_address}"); - self.state.set_adder_address(new_address); + self.state.set_adder_address(new_address.clone()); } async fn multi_deploy(&mut self, count: usize) { @@ -113,7 +117,6 @@ impl AdderInteract { return; } - self.set_state().await; println!("deploying {count} contracts..."); let mut buffer = self.interactor.homogenous_call_buffer(); @@ -141,7 +144,6 @@ impl AdderInteract { } async fn multi_add(&mut self, value: u32, count: usize) { - self.set_state().await; println!("calling contract {count} times..."); let mut buffer = self.interactor.homogenous_call_buffer(); @@ -205,7 +207,7 @@ impl AdderInteract { let response = self .interactor .tx() - .from(&self.wallet_address) + .from(&self.adder_owner_address) .to(self.state.current_adder_address()) .gas(3_000_000) .typed(adder_proxy::AdderProxy) @@ -241,5 +243,5 @@ async fn test() { basic_interact.deploy().await; basic_interact.add(1u32).await; - basic_interact.upgrade(7u32).await; + basic_interact.upgrade(5u32).await; } diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs index 6802304ae9..6c46301808 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs @@ -14,6 +14,7 @@ impl Interactor { { let sc_call_step = sc_call_step.as_mut(); let tx_hash = self.launch_sc_call(sc_call_step).await; + // self.proxy.generate_blocks(20).await; let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await; sc_call_step.save_response(network_response::parse_tx_response(tx)); diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs index 59de249e83..3ff2748f92 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs @@ -53,6 +53,7 @@ impl Interactor { { let sc_deploy_step = sc_deploy_step.as_mut(); let tx_hash = self.launch_sc_deploy(sc_deploy_step).await; + // self.proxy.generate_blocks(1).await; let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await; let addr = sc_deploy_step.tx.from.clone(); diff --git a/sdk/core/src/gateway.rs b/sdk/core/src/gateway.rs index 35e2668b50..33a263607e 100644 --- a/sdk/core/src/gateway.rs +++ b/sdk/core/src/gateway.rs @@ -4,12 +4,14 @@ mod gateway_network; mod gateway_proxy; mod gateway_tx; mod gateway_tx_retrieve; +mod gateway_chain_simulator; pub use gateway_proxy::GatewayProxy; pub const MAINNET_GATEWAY: &str = "https://gateway.multiversx.com"; pub const TESTNET_GATEWAY: &str = "https://testnet-gateway.multiversx.com"; pub const DEVNET_GATEWAY: &str = "https://devnet-gateway.multiversx.com"; +pub const SIMULATOR_GATEWAY: &str = "http://localhost:8085"; // MetachainShardId will be used to identify a shard ID as metachain pub const METACHAIN_SHARD_ID: u32 = 0xFFFFFFFF; diff --git a/sdk/core/src/gateway/gateway_chain_simulator.rs b/sdk/core/src/gateway/gateway_chain_simulator.rs new file mode 100644 index 0000000000..47c14c99c4 --- /dev/null +++ b/sdk/core/src/gateway/gateway_chain_simulator.rs @@ -0,0 +1,32 @@ +use std::collections::HashMap; + +use super::GatewayProxy; + +const SEND_USER_FUNDS_ENDPOINT: &str = "transaction/send-user-funds"; +const GENERATE_BLOCKS_FUNDS_ENDPOINT: &str = "simulator/generate-blocks"; +const ACCOUNT_DATA: &str = "address/"; + +impl GatewayProxy { + pub async fn send_user_funds(&self, receiver: &String) { + let mut r = HashMap::new(); + r.insert("receiver", receiver); + let endpoint_funds = self.get_endpoint(SEND_USER_FUNDS_ENDPOINT); + let _ = self.client.post(endpoint_funds).json(&r).send().await; + } + + pub async fn generate_blocks(&self, number_blocks: u64) { + let url_gen_blocks: String = + format!("{}/{}", GENERATE_BLOCKS_FUNDS_ENDPOINT, number_blocks); + let endpoint_blocks = self.get_endpoint(&url_gen_blocks); + let _ = self.client.post(endpoint_blocks).send().await; + } + + pub async fn set_state_chain_sim(&self, address: String) { + let endpoint_funds = self.get_endpoint(&format!("{}{}", ACCOUNT_DATA, address)); + let data = self.client.get(endpoint_funds).send().await; + match data { + Ok(d) => println!("{:?}", d.text().await), + Err(_) => todo!(), + } + } +} From c060158e3293d385a765eb78acf13f7c4b2d84ce Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Wed, 21 Aug 2024 16:07:35 +0300 Subject: [PATCH 02/30] chain sim - impl in framework --- contracts/examples/adder/interact/config.toml | 2 +- .../adder/interact/src/basic_interact.rs | 31 ++++---- .../interact/src/basic_interact_config.rs | 9 ++- .../interact/src/multisig_interact.rs | 8 +- .../interact/src/bf_interact.rs | 2 +- .../interact/src/comp_interact_controller.rs | 2 +- framework/snippets/src/account_tool.rs | 38 ++++++---- framework/snippets/src/interactor.rs | 7 +- .../interactor_scenario/interactor_sc_call.rs | 5 +- .../interactor_sc_deploy.rs | 5 +- .../interactor_transfer.rs | 5 ++ .../src/multi/interactor_multi_sc_process.rs | 1 + .../src/gateway/gateway_chain_simulator.rs | 76 ++++++++++++++++--- sdk/core/src/gateway/gateway_proxy.rs | 6 ++ 14 files changed, 145 insertions(+), 52 deletions(-) diff --git a/contracts/examples/adder/interact/config.toml b/contracts/examples/adder/interact/config.toml index 11eb18de60..e474de08f5 100644 --- a/contracts/examples/adder/interact/config.toml +++ b/contracts/examples/adder/interact/config.toml @@ -1 +1 @@ -gateway = 'http://localhost:8085' +chain_type = "simulator" diff --git a/contracts/examples/adder/interact/src/basic_interact.rs b/contracts/examples/adder/interact/src/basic_interact.rs index fff69aea1c..937d12641f 100644 --- a/contracts/examples/adder/interact/src/basic_interact.rs +++ b/contracts/examples/adder/interact/src/basic_interact.rs @@ -65,21 +65,10 @@ impl AdderInteract { .with_tracer(INTERACTOR_SCENARIO_TRACE_PATH) .await; - let adder_owner_address = interactor.register_wallet(test_wallets::alice()); + let adder_owner_address = interactor.register_wallet(test_wallets::alice()).await; + let wallet_address = interactor.register_wallet(test_wallets::mike()).await; - let wallet_address = interactor.register_wallet(test_wallets::mike()); - - interactor - .proxy - .send_user_funds(&Bech32Address::from(&adder_owner_address).to_bech32_string()) - .await; - - interactor - .proxy - .send_user_funds(&Bech32Address::from(&wallet_address).to_bech32_string()) - .await; - - interactor.proxy.generate_blocks(20).await; + interactor.proxy.generate_blocks(20).await.unwrap(); Self { interactor, @@ -89,10 +78,20 @@ impl AdderInteract { } } + async fn set_state(&mut self) { + println!("wallet address: {}", self.wallet_address); + self.interactor + .retrieve_account(&self.adder_owner_address) + .await; + self.interactor.retrieve_account(&self.wallet_address).await; + } + async fn deploy(&mut self) { // warning: multi deploy not yet fully supported // only works with last deployed address + self.set_state().await; + let new_address = self .interactor .tx() @@ -108,7 +107,7 @@ impl AdderInteract { .await; println!("new address: {new_address}"); - self.state.set_adder_address(new_address.clone()); + self.state.set_adder_address(new_address); } async fn multi_deploy(&mut self, count: usize) { @@ -117,6 +116,7 @@ impl AdderInteract { return; } + self.set_state().await; println!("deploying {count} contracts..."); let mut buffer = self.interactor.homogenous_call_buffer(); @@ -144,6 +144,7 @@ impl AdderInteract { } async fn multi_add(&mut self, value: u32, count: usize) { + self.set_state().await; println!("calling contract {count} times..."); let mut buffer = self.interactor.homogenous_call_buffer(); diff --git a/contracts/examples/adder/interact/src/basic_interact_config.rs b/contracts/examples/adder/interact/src/basic_interact_config.rs index e17d0cbe65..02773bc1e8 100644 --- a/contracts/examples/adder/interact/src/basic_interact_config.rs +++ b/contracts/examples/adder/interact/src/basic_interact_config.rs @@ -7,7 +7,8 @@ const CONFIG_FILE: &str = "config.toml"; /// Adder Interact configuration #[derive(Debug, Deserialize)] pub struct Config { - gateway: String, + gateway: Option, + chain_type: String, } impl Config { @@ -21,6 +22,10 @@ impl Config { // Returns the gateway pub fn gateway(&self) -> &str { - &self.gateway + match self.chain_type.as_str() { + "real" => self.gateway.as_deref().expect("Please provide gateway!"), + "simulator" => "http://localhost:8085", + _ => "", + } } } diff --git a/contracts/examples/multisig/interact/src/multisig_interact.rs b/contracts/examples/multisig/interact/src/multisig_interact.rs index 497b6636dd..7ab094e04c 100644 --- a/contracts/examples/multisig/interact/src/multisig_interact.rs +++ b/contracts/examples/multisig/interact/src/multisig_interact.rs @@ -19,7 +19,7 @@ async fn main() { env_logger::init(); let mut multisig_interact = MultisigInteract::init().await; - multisig_interact.register_wallets(); + multisig_interact.register_wallets().await; let cli = multisig_interact_cli::InteractCli::parse(); match &cli.command { @@ -90,7 +90,7 @@ impl MultisigInteract { .await .with_tracer(INTERACTOR_SCENARIO_TRACE_PATH) .await; - let wallet_address = interactor.register_wallet(test_wallets::mike()); + let wallet_address = interactor.register_wallet(test_wallets::mike()).await; let multisig_code = BytesValue::interpret_from( "mxsc:../output/multisig.mxsc.json", &InterpreterContext::default(), @@ -106,13 +106,13 @@ impl MultisigInteract { } } - fn register_wallets(&mut self) { + async fn register_wallets(&mut self) { let carol = test_wallets::carol(); let dan = test_wallets::dan(); let eve = test_wallets::eve(); for wallet in &[carol, dan, eve] { - self.interactor.register_wallet(*wallet); + self.interactor.register_wallet(*wallet).await; } } diff --git a/contracts/feature-tests/basic-features/interact/src/bf_interact.rs b/contracts/feature-tests/basic-features/interact/src/bf_interact.rs index e8f269cb71..d14de2d71c 100644 --- a/contracts/feature-tests/basic-features/interact/src/bf_interact.rs +++ b/contracts/feature-tests/basic-features/interact/src/bf_interact.rs @@ -45,7 +45,7 @@ impl BasicFeaturesInteract { .await .with_tracer(INTERACTOR_SCENARIO_TRACE_PATH) .await; - let wallet_address = interactor.register_wallet(test_wallets::mike()); + let wallet_address = interactor.register_wallet(test_wallets::mike()).await; let code_expr = BytesValue::interpret_from( "mxsc:../output/basic-features-storage-bytes.mxsc.json", &InterpreterContext::default(), diff --git a/contracts/feature-tests/composability/interact/src/comp_interact_controller.rs b/contracts/feature-tests/composability/interact/src/comp_interact_controller.rs index 1c2457494b..1ae99af017 100644 --- a/contracts/feature-tests/composability/interact/src/comp_interact_controller.rs +++ b/contracts/feature-tests/composability/interact/src/comp_interact_controller.rs @@ -20,7 +20,7 @@ impl ComposabilityInteract { .await .with_tracer(INTERACTOR_SCENARIO_TRACE_PATH) .await; - let wallet_address = interactor.register_wallet(test_wallets::judy()); + let wallet_address = interactor.register_wallet(test_wallets::judy()).await; let forw_queue_code = BytesValue::interpret_from( "mxsc:../forwarder-queue/output/forwarder-queue.mxsc.json", &InterpreterContext::default(), diff --git a/framework/snippets/src/account_tool.rs b/framework/snippets/src/account_tool.rs index b10baf4a80..c40407e360 100644 --- a/framework/snippets/src/account_tool.rs +++ b/framework/snippets/src/account_tool.rs @@ -40,20 +40,30 @@ pub async fn retrieve_account_as_scenario_set_state( let sdk_address = Address::from_bech32_string(address.to_bech32_str()).unwrap(); let sdk_account = api.get_account(&sdk_address).await.unwrap(); - let account_esdt = api - .get_account_esdt_tokens(&sdk_address) - .await - .unwrap_or_else(|err| { - panic!("failed to retrieve ESDT tokens for address {address}: {err}") - }); - let account_esdt_roles = api - .get_account_esdt_roles(&sdk_address) - .await - .unwrap_or_else(|err| panic!("failed to retrieve ESDT roles for address {address}: {err}")); - let account_storage = api - .get_account_storage_keys(&sdk_address) - .await - .unwrap_or_else(|err| panic!("failed to retrieve storage for address {address}: {err}")); + let (account_esdt, account_esdt_roles, account_storage) = if api.chain_simulator { + (HashMap::new(), HashMap::new(), HashMap::new()) + } else { + let account_esdt = api + .get_account_esdt_tokens(&sdk_address) + .await + .unwrap_or_else(|err| { + panic!("failed to retrieve ESDT tokens for address {address}: {err}") + }); + let account_esdt_roles = api + .get_account_esdt_roles(&sdk_address) + .await + .unwrap_or_else(|err| { + panic!("failed to retrieve ESDT roles for address {address}: {err}") + }); + let account_storage = api + .get_account_storage_keys(&sdk_address) + .await + .unwrap_or_else(|err| { + panic!("failed to retrieve storage for address {address}: {err}") + }); + + (account_esdt, account_esdt_roles, account_storage) + }; let account_state = set_account( sdk_account, diff --git a/framework/snippets/src/interactor.rs b/framework/snippets/src/interactor.rs index bb38873afe..702121c373 100644 --- a/framework/snippets/src/interactor.rs +++ b/framework/snippets/src/interactor.rs @@ -46,8 +46,13 @@ impl Interactor { } } - pub fn register_wallet(&mut self, wallet: Wallet) -> Address { + pub async fn register_wallet(&mut self, wallet: Wallet) -> Address { let address = erdrs_address_to_h256(wallet.address()); + self.proxy + .send_user_funds(&Bech32Address::from(&address).to_bech32_string()) + .await + .unwrap(); + self.sender_map.insert( address.clone(), Sender { diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs index 6c46301808..a9d3930eb6 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs @@ -14,7 +14,10 @@ impl Interactor { { let sc_call_step = sc_call_step.as_mut(); let tx_hash = self.launch_sc_call(sc_call_step).await; - // self.proxy.generate_blocks(20).await; + self.proxy + .generate_blocks_until_tx_processed(&tx_hash) + .await + .unwrap(); let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await; sc_call_step.save_response(network_response::parse_tx_response(tx)); diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs index 3ff2748f92..20d45649d8 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs @@ -53,7 +53,10 @@ impl Interactor { { let sc_deploy_step = sc_deploy_step.as_mut(); let tx_hash = self.launch_sc_deploy(sc_deploy_step).await; - // self.proxy.generate_blocks(1).await; + self.proxy + .generate_blocks_until_tx_processed(&tx_hash) + .await + .unwrap(); let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await; let addr = sc_deploy_step.tx.from.clone(); diff --git a/framework/snippets/src/interactor_scenario/interactor_transfer.rs b/framework/snippets/src/interactor_scenario/interactor_transfer.rs index 6557e4119f..53da3a64b9 100644 --- a/framework/snippets/src/interactor_scenario/interactor_transfer.rs +++ b/framework/snippets/src/interactor_scenario/interactor_transfer.rs @@ -11,6 +11,11 @@ impl Interactor { self.set_nonce_and_sign_tx(sender_address, &mut transaction) .await; let tx_hash = self.proxy.send_transaction(&transaction).await.unwrap(); + self.proxy + .generate_blocks_until_tx_processed(&tx_hash) + .await + .unwrap(); + println!("transfer tx hash: {tx_hash}"); info!("transfer tx hash: {}", tx_hash); diff --git a/framework/snippets/src/multi/interactor_multi_sc_process.rs b/framework/snippets/src/multi/interactor_multi_sc_process.rs index 3364a4d5ee..076c005d84 100644 --- a/framework/snippets/src/multi/interactor_multi_sc_process.rs +++ b/framework/snippets/src/multi/interactor_multi_sc_process.rs @@ -34,6 +34,7 @@ impl Interactor { futures.push(self.proxy.retrieve_tx_on_network(tx_hash.clone())); } + self.proxy.generate_blocks(4).await.unwrap(); join_all(futures).await } } diff --git a/sdk/core/src/gateway/gateway_chain_simulator.rs b/sdk/core/src/gateway/gateway_chain_simulator.rs index 47c14c99c4..9c9e75b819 100644 --- a/sdk/core/src/gateway/gateway_chain_simulator.rs +++ b/sdk/core/src/gateway/gateway_chain_simulator.rs @@ -1,32 +1,86 @@ use std::collections::HashMap; use super::GatewayProxy; +use anyhow::{anyhow, Error}; +use serde::{Deserialize, Serialize}; const SEND_USER_FUNDS_ENDPOINT: &str = "transaction/send-user-funds"; const GENERATE_BLOCKS_FUNDS_ENDPOINT: &str = "simulator/generate-blocks"; -const ACCOUNT_DATA: &str = "address/"; +const GENERATE_BLOCKS_UNTIL_TX_PROCESSED_ENDPOINT: &str = + "simulator/generate-blocks-until-transaction-processed"; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct GenerateBlocksResponse { + pub data: serde_json::Value, + pub error: String, + pub code: String, +} impl GatewayProxy { - pub async fn send_user_funds(&self, receiver: &String) { + pub async fn send_user_funds(&self, receiver: &String) -> Result { + if !self.chain_simulator { + return Ok(String::from("no-simulator")); + } + let mut r = HashMap::new(); r.insert("receiver", receiver); let endpoint_funds = self.get_endpoint(SEND_USER_FUNDS_ENDPOINT); - let _ = self.client.post(endpoint_funds).json(&r).send().await; + let resp = self + .client + .post(endpoint_funds) + .json(&r) + .send() + .await? + .json::() + .await?; + + match resp.code.as_str() { + "successful" => Ok(resp.code), + _ => Err(anyhow!("{}", resp.error)), + } } - pub async fn generate_blocks(&self, number_blocks: u64) { + pub async fn generate_blocks(&self, number_blocks: u64) -> Result { + if !self.chain_simulator { + return Ok(String::from("no-simulator")); + } + let url_gen_blocks: String = format!("{}/{}", GENERATE_BLOCKS_FUNDS_ENDPOINT, number_blocks); let endpoint_blocks = self.get_endpoint(&url_gen_blocks); - let _ = self.client.post(endpoint_blocks).send().await; + let resp = self + .client + .post(endpoint_blocks) + .send() + .await? + .json::() + .await?; + + match resp.code.as_str() { + "successful" => Ok(resp.code), + _ => Err(anyhow!("{}", resp.error)), + } } - pub async fn set_state_chain_sim(&self, address: String) { - let endpoint_funds = self.get_endpoint(&format!("{}{}", ACCOUNT_DATA, address)); - let data = self.client.get(endpoint_funds).send().await; - match data { - Ok(d) => println!("{:?}", d.text().await), - Err(_) => todo!(), + pub async fn generate_blocks_until_tx_processed(&self, tx: &String) -> Result { + if !self.chain_simulator { + return Ok(String::from("no-simulator")); + } + + let url_gen_blocks_until_tx_processed: String = + format!("{}/{}", GENERATE_BLOCKS_UNTIL_TX_PROCESSED_ENDPOINT, tx); + let endpoint_blocks = self.get_endpoint(&url_gen_blocks_until_tx_processed); + let resp = self + .client + .post(endpoint_blocks) + .send() + .await? + .json::() + .await?; + + match resp.code.as_str() { + "successful" => Ok(resp.code), + _ => Err(anyhow!("{}", resp.error)), } } } diff --git a/sdk/core/src/gateway/gateway_proxy.rs b/sdk/core/src/gateway/gateway_proxy.rs index 47925cd79f..f3693c6e12 100644 --- a/sdk/core/src/gateway/gateway_proxy.rs +++ b/sdk/core/src/gateway/gateway_proxy.rs @@ -1,17 +1,23 @@ use reqwest::Client; +pub const CHAIN_SIMULATOR_GATEWAY: &str = "http://localhost:8085"; + /// Allows communication with the MultiversX gateway API. #[derive(Clone, Debug)] pub struct GatewayProxy { pub(crate) proxy_url: String, pub(crate) client: Client, + pub chain_simulator: bool, } impl GatewayProxy { pub fn new(proxy_url: String) -> Self { + let chain_simulator = proxy_url == CHAIN_SIMULATOR_GATEWAY; + Self { proxy_url, client: Client::new(), + chain_simulator, } } From bd9be2e26c158d0ef8d1dcaefbd11d9b366bfc27 Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Fri, 13 Sep 2024 18:52:19 +0200 Subject: [PATCH 03/30] impl for interactor --- .../scenario/src/facade/result_handlers.rs | 2 ++ .../facade/result_handlers/returns_tx_hash.rs | 24 +++++++++++++++++++ .../scenario/model/transaction/tx_response.rs | 2 ++ framework/snippets/src/network_response.rs | 6 +++++ 4 files changed, 34 insertions(+) create mode 100644 framework/scenario/src/facade/result_handlers/returns_tx_hash.rs diff --git a/framework/scenario/src/facade/result_handlers.rs b/framework/scenario/src/facade/result_handlers.rs index 33b5b1a495..88e90d0cd5 100644 --- a/framework/scenario/src/facade/result_handlers.rs +++ b/framework/scenario/src/facade/result_handlers.rs @@ -7,6 +7,7 @@ mod returns_message; mod returns_new_bech32_address; mod returns_new_token_identifier; mod returns_status; +mod returns_tx_hash; mod with_tx_raw_response; pub use expect_error::ExpectError; @@ -18,4 +19,5 @@ pub use returns_message::ReturnsMessage; pub use returns_new_bech32_address::ReturnsNewBech32Address; pub use returns_new_token_identifier::ReturnsNewTokenIdentifier; pub use returns_status::ReturnsStatus; +pub use returns_tx_hash::ReturnsTxHash; pub use with_tx_raw_response::WithRawTxResponse; diff --git a/framework/scenario/src/facade/result_handlers/returns_tx_hash.rs b/framework/scenario/src/facade/result_handlers/returns_tx_hash.rs new file mode 100644 index 0000000000..7494b41edc --- /dev/null +++ b/framework/scenario/src/facade/result_handlers/returns_tx_hash.rs @@ -0,0 +1,24 @@ +use multiversx_sc::types::RHListItemExec; + +use crate::{ + multiversx_sc::types::{RHListItem, TxEnv}, + scenario_model::TxResponse, +}; + +pub struct ReturnsTxHash; + +impl RHListItem for ReturnsTxHash +where + Env: TxEnv, +{ + type Returns = String; +} + +impl RHListItemExec for ReturnsTxHash +where + Env: TxEnv, +{ + fn item_process_result(self, raw_result: &TxResponse) -> Self::Returns { + raw_result.tx_hash.clone() + } +} diff --git a/framework/scenario/src/scenario/model/transaction/tx_response.rs b/framework/scenario/src/scenario/model/transaction/tx_response.rs index 8a7a5c4bf9..b230fd1878 100644 --- a/framework/scenario/src/scenario/model/transaction/tx_response.rs +++ b/framework/scenario/src/scenario/model/transaction/tx_response.rs @@ -20,6 +20,8 @@ pub struct TxResponse { pub gas: u64, /// The refund of the transaction. pub refund: u64, + /// The transaction hash + pub tx_hash: String, } impl TxResponse { diff --git a/framework/snippets/src/network_response.rs b/framework/snippets/src/network_response.rs index 0140dd839e..4684fdce3c 100644 --- a/framework/snippets/src/network_response.rs +++ b/framework/snippets/src/network_response.rs @@ -17,6 +17,7 @@ pub fn parse_tx_response(tx: TransactionOnNetwork) -> TxResponse { if !tx_error.is_success() { return TxResponse { tx_error, + tx_hash: process_tx_hash(&tx).to_string(), ..Default::default() }; } @@ -45,10 +46,15 @@ fn process_success(tx: &TransactionOnNetwork) -> TxResponse { new_deployed_address: process_new_deployed_address(tx), new_issued_token_identifier: process_new_issued_token_identifier(tx), logs: process_logs(tx), + tx_hash: process_tx_hash(tx).to_string(), ..Default::default() } } +fn process_tx_hash(tx: &TransactionOnNetwork) -> &str { + tx.hash.as_deref().unwrap_or("") +} + fn process_out(tx: &TransactionOnNetwork) -> Vec> { let out_scr = tx.smart_contract_results.iter().find(is_out_scr); From 3905c2c88763c0899a783102eb7ee3aa0570fbf7 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Tue, 17 Sep 2024 18:54:07 +0300 Subject: [PATCH 04/30] chain sim - fix test --- tools/interactor-system-func-calls/src/system_sc_interact.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/interactor-system-func-calls/src/system_sc_interact.rs b/tools/interactor-system-func-calls/src/system_sc_interact.rs index df47733d47..4ec3c6647e 100644 --- a/tools/interactor-system-func-calls/src/system_sc_interact.rs +++ b/tools/interactor-system-func-calls/src/system_sc_interact.rs @@ -229,7 +229,7 @@ impl SysFuncCallsInteract { let config = Config::load_config(); let mut interactor = Interactor::new(config.gateway()).await; - let wallet_address = interactor.register_wallet(test_wallets::alice()); + let wallet_address = interactor.register_wallet(test_wallets::alice()).await; Self { interactor, From b72c23db19ba2c6985827b838ea5ad7fddaf8dce Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Thu, 19 Sep 2024 09:58:15 +0200 Subject: [PATCH 05/30] tx hash in scenario --- .../facade/world_tx/scenario_tx_whitebox.rs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/framework/scenario/src/facade/world_tx/scenario_tx_whitebox.rs b/framework/scenario/src/facade/world_tx/scenario_tx_whitebox.rs index 62842a6e25..e20538ce0e 100644 --- a/framework/scenario/src/facade/world_tx/scenario_tx_whitebox.rs +++ b/framework/scenario/src/facade/world_tx/scenario_tx_whitebox.rs @@ -1,3 +1,5 @@ +use core::str; + use crate::debug_executor::contract_instance_wrapped_execution; use crate::scenario::tx_to_step::TxToQueryStep; use crate::{ @@ -68,6 +70,15 @@ where }); let mut response = TxResponse::from_tx_result(tx_result); + if let Some(tx_hash) = &step_wrapper.env.data.tx_hash { + let tx_hash_bytes = tx_hash.as_bytes(); + response.tx_hash = str::from_utf8(tx_hash_bytes) + .expect("tx hash not utf8 valid") + .to_string(); + } else { + response.tx_hash = String::from(""); + } + response.new_deployed_address = Some(new_address); step_wrapper.step.save_response(response); step_wrapper.process_result() @@ -140,7 +151,16 @@ where }); }); - let response = TxResponse::from_tx_result(tx_result); + let mut response = TxResponse::from_tx_result(tx_result); + if let Some(tx_hash) = &step_wrapper.env.data.tx_hash { + let tx_hash_bytes = tx_hash.as_bytes(); + response.tx_hash = str::from_utf8(tx_hash_bytes) + .expect("tx hash not utf8 valid") + .to_string(); + } else { + response.tx_hash = String::from(""); + } + step_wrapper.step.save_response(response); step_wrapper.process_result() } From d305c74d291032432ee93abed3afd3272081f461 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Thu, 19 Sep 2024 19:37:18 +0300 Subject: [PATCH 06/30] chain sim - pass URI from the interactor --- .../adder/interact/src/basic_interact.rs | 21 +++++++++-------- .../interact/src/basic_interact_config.rs | 20 +++++++++++----- .../examples/multisig/interact/config.toml | 3 ++- .../interact/src/multisig_interact.rs | 2 +- .../interact/src/multisig_interact_config.rs | 20 +++++++++++++++- .../basic-features/interact/config.toml | 3 ++- .../interact/src/bf_interact.rs | 2 +- .../interact/src/bf_interact_config.rs | 21 +++++++++++++---- .../composability/interact/config.toml | 9 ++++---- .../interact/src/comp_interact_config.rs | 21 +++++++++++++---- .../interact/src/comp_interact_controller.rs | 2 +- framework/meta/src/cli/cli_args_standalone.rs | 10 +++++++- framework/meta/src/cmd/retrieve_address.rs | 8 ++++++- framework/snippets/src/account_tool.rs | 3 ++- framework/snippets/src/interactor.rs | 4 ++-- sdk/core/examples/account.rs | 4 ++-- sdk/core/examples/account_storage.rs | 4 ++-- sdk/core/examples/get_esdt_tokens.rs | 4 ++-- sdk/core/examples/get_hyper_block_by_hash.rs | 4 ++-- sdk/core/examples/get_hyper_block_by_nonce.rs | 4 ++-- sdk/core/examples/get_hyper_block_latest.rs | 4 ++-- sdk/core/examples/get_network_config.rs | 4 ++-- sdk/core/examples/get_network_economics.rs | 4 ++-- sdk/core/examples/sign_tx.rs | 4 ++-- sdk/core/examples/sign_txs.rs | 4 ++-- sdk/core/examples/tx_cost.rs | 4 ++-- sdk/core/examples/tx_default_args.rs | 4 ++-- sdk/core/examples/tx_info.rs | 4 ++-- sdk/core/examples/vm_query.rs | 4 ++-- sdk/core/src/gateway.rs | 4 +++- sdk/core/src/gateway/gateway_proxy.rs | 12 ++++------ .../interactor-system-func-calls/config.toml | 3 ++- .../src/system_sc_interact.rs | 3 ++- .../src/system_sc_interact_config.rs | 23 +++++++++++++++---- 34 files changed, 168 insertions(+), 82 deletions(-) diff --git a/contracts/examples/adder/interact/src/basic_interact.rs b/contracts/examples/adder/interact/src/basic_interact.rs index ee88d2e9d5..9f3f82a43b 100644 --- a/contracts/examples/adder/interact/src/basic_interact.rs +++ b/contracts/examples/adder/interact/src/basic_interact.rs @@ -63,22 +63,25 @@ struct AdderInteract { impl AdderInteract { async fn init() -> Self { let config = Config::load_config(); - let mut interactor = Interactor::new(config.gateway()) + let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator()) .await .with_tracer(INTERACTOR_SCENARIO_TRACE_PATH) .await; - let adder_owner_address = - interactor.register_wallet(Wallet::from_pem_file("adder-owner.pem").unwrap()).await; + let adder_owner_address = interactor + .register_wallet(Wallet::from_pem_file("adder-owner.pem").unwrap()) + .await; // PASSWORD: "alice" // InsertPassword::Plaintext("alice".to_string()) || InsertPassword::StandardInput - let wallet_address = interactor.register_wallet( - Wallet::from_keystore_secret( - "alice.json", - InsertPassword::Plaintext("alice".to_string()), + let wallet_address = interactor + .register_wallet( + Wallet::from_keystore_secret( + "alice.json", + InsertPassword::Plaintext("alice".to_string()), + ) + .unwrap(), ) - .unwrap(), - ).await; + .await; Self { interactor, diff --git a/contracts/examples/adder/interact/src/basic_interact_config.rs b/contracts/examples/adder/interact/src/basic_interact_config.rs index 02773bc1e8..2ab2577ceb 100644 --- a/contracts/examples/adder/interact/src/basic_interact_config.rs +++ b/contracts/examples/adder/interact/src/basic_interact_config.rs @@ -7,7 +7,7 @@ const CONFIG_FILE: &str = "config.toml"; /// Adder Interact configuration #[derive(Debug, Deserialize)] pub struct Config { - gateway: Option, + gateway_uri: String, chain_type: String, } @@ -20,12 +20,20 @@ impl Config { toml::from_str(&content).unwrap() } - // Returns the gateway - pub fn gateway(&self) -> &str { + // Returns the gateway URI + pub fn gateway_uri(&self) -> &str { + &self.gateway_uri + } + + // Returns if chain type is chain simulator + pub fn use_chain_simulator(&self) -> bool { match self.chain_type.as_str() { - "real" => self.gateway.as_deref().expect("Please provide gateway!"), - "simulator" => "http://localhost:8085", - _ => "", + "simulator" => return true, + "real" => return false, + _ => panic!( + "Invalid chain type: {}. Expected 'simulator' or 'real'.", + self.chain_type + ), } } } diff --git a/contracts/examples/multisig/interact/config.toml b/contracts/examples/multisig/interact/config.toml index 029fab5ac7..148ebb5803 100644 --- a/contracts/examples/multisig/interact/config.toml +++ b/contracts/examples/multisig/interact/config.toml @@ -1,3 +1,4 @@ -gateway = 'https://devnet-gateway.multiversx.com' +chain_type = 'real' +gateway_uri = 'https://devnet-gateway.multiversx.com' quorum = 2 wegld_address = "erd1qqqqqqqqqqqqqpgqqkwzsxkjc83vlfex9dmznwm7tjvxlqqkpauqx0n782" diff --git a/contracts/examples/multisig/interact/src/multisig_interact.rs b/contracts/examples/multisig/interact/src/multisig_interact.rs index 7ab094e04c..0701841b6a 100644 --- a/contracts/examples/multisig/interact/src/multisig_interact.rs +++ b/contracts/examples/multisig/interact/src/multisig_interact.rs @@ -86,7 +86,7 @@ struct MultisigInteract { impl MultisigInteract { async fn init() -> Self { let config = Config::load_config(); - let mut interactor = Interactor::new(&config.gateway) + let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator()) .await .with_tracer(INTERACTOR_SCENARIO_TRACE_PATH) .await; diff --git a/contracts/examples/multisig/interact/src/multisig_interact_config.rs b/contracts/examples/multisig/interact/src/multisig_interact_config.rs index 4aa9903cc0..ec280f4807 100644 --- a/contracts/examples/multisig/interact/src/multisig_interact_config.rs +++ b/contracts/examples/multisig/interact/src/multisig_interact_config.rs @@ -8,7 +8,8 @@ const CONFIG_FILE: &str = "config.toml"; /// Multisig Interact configuration #[derive(Debug, Deserialize)] pub struct Config { - pub gateway: String, + pub gateway_uri: String, + pub chain_type: String, pub quorum: usize, pub wegld_address: Bech32Address, } @@ -21,4 +22,21 @@ impl Config { file.read_to_string(&mut content).unwrap(); toml::from_str(&content).unwrap() } + + // Returns the gateway URI + pub fn gateway_uri(&self) -> &str { + &self.gateway_uri + } + + // Returns if chain type is chain simulator + pub fn use_chain_simulator(&self) -> bool { + match self.chain_type.as_str() { + "simulator" => return true, + "real" => return false, + _ => panic!( + "Invalid chain type: {}. Expected 'simulator' or 'real'.", + self.chain_type + ), + } + } } diff --git a/contracts/feature-tests/basic-features/interact/config.toml b/contracts/feature-tests/basic-features/interact/config.toml index 61ac8dbf87..dfc1ba62a6 100644 --- a/contracts/feature-tests/basic-features/interact/config.toml +++ b/contracts/feature-tests/basic-features/interact/config.toml @@ -1 +1,2 @@ -gateway = 'https://devnet-gateway.multiversx.com' +chain_type = 'real' +gateway_uri = 'https://devnet-gateway.multiversx.com' diff --git a/contracts/feature-tests/basic-features/interact/src/bf_interact.rs b/contracts/feature-tests/basic-features/interact/src/bf_interact.rs index d14de2d71c..b971818f2b 100644 --- a/contracts/feature-tests/basic-features/interact/src/bf_interact.rs +++ b/contracts/feature-tests/basic-features/interact/src/bf_interact.rs @@ -41,7 +41,7 @@ struct BasicFeaturesInteract { impl BasicFeaturesInteract { async fn init() -> Self { let config = Config::load_config(); - let mut interactor = Interactor::new(config.gateway()) + let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator()) .await .with_tracer(INTERACTOR_SCENARIO_TRACE_PATH) .await; diff --git a/contracts/feature-tests/basic-features/interact/src/bf_interact_config.rs b/contracts/feature-tests/basic-features/interact/src/bf_interact_config.rs index e17d0cbe65..9c6cb6ef0f 100644 --- a/contracts/feature-tests/basic-features/interact/src/bf_interact_config.rs +++ b/contracts/feature-tests/basic-features/interact/src/bf_interact_config.rs @@ -7,7 +7,8 @@ const CONFIG_FILE: &str = "config.toml"; /// Adder Interact configuration #[derive(Debug, Deserialize)] pub struct Config { - gateway: String, + gateway_uri: String, + chain_type: String, } impl Config { @@ -19,8 +20,20 @@ impl Config { toml::from_str(&content).unwrap() } - // Returns the gateway - pub fn gateway(&self) -> &str { - &self.gateway + // Returns the gateway URI + pub fn gateway_uri(&self) -> &str { + &self.gateway_uri + } + + // Returns if true if chain type is chain simulator + pub fn use_chain_simulator(&self) -> bool { + match self.chain_type.as_str() { + "simulator" => return true, + "real" => return false, + _ => panic!( + "Invalid chain type: {}. Expected 'simulator' or 'real'.", + self.chain_type + ), + } } } diff --git a/contracts/feature-tests/composability/interact/config.toml b/contracts/feature-tests/composability/interact/config.toml index accfdf2fa5..992112c225 100644 --- a/contracts/feature-tests/composability/interact/config.toml +++ b/contracts/feature-tests/composability/interact/config.toml @@ -1,6 +1,7 @@ -gateway = 'https://testnet-gateway.multiversx.com' -call_type = "LegacyAsync" # Sync / LegacyAsync / TransferExecute +chain_type = 'real' +gateway_uri = 'https://testnet-gateway.multiversx.com' +call_type = "LegacyAsync" # Sync / LegacyAsync / TransferExecute # token_id = "CMPT-4e9332" token_id = "EGLD" -token_nonce = 0 # is 0 if fungible -amount = '50000000000000000' \ No newline at end of file +token_nonce = 0 # is 0 if fungible +amount = '50000000000000000' diff --git a/contracts/feature-tests/composability/interact/src/comp_interact_config.rs b/contracts/feature-tests/composability/interact/src/comp_interact_config.rs index d09c2323f7..96524d1bde 100644 --- a/contracts/feature-tests/composability/interact/src/comp_interact_config.rs +++ b/contracts/feature-tests/composability/interact/src/comp_interact_config.rs @@ -11,7 +11,8 @@ const CONFIG_FILE: &str = "config.toml"; /// Multisig Interact configuration #[derive(Debug, Deserialize)] pub struct Config { - gateway: String, + gateway_uri: String, + chain_type: String, call_type: String, token_id: String, token_nonce: u64, @@ -27,9 +28,21 @@ impl Config { toml::from_str(&content).unwrap() } - // Returns the gateway - pub fn gateway(&self) -> &str { - &self.gateway + // Returns the gateway URI + pub fn gateway_uri(&self) -> &str { + &self.gateway_uri + } + + // Returns if chain type is chain simulator + pub fn use_chain_simulator(&self) -> bool { + match self.chain_type.as_str() { + "simulator" => return true, + "real" => return false, + _ => panic!( + "Invalid chain type: {}. Expected 'simulator' or 'real'.", + self.chain_type + ), + } } pub fn call_type(&self) -> QueuedCallType { diff --git a/contracts/feature-tests/composability/interact/src/comp_interact_controller.rs b/contracts/feature-tests/composability/interact/src/comp_interact_controller.rs index 1ae99af017..e486c6d522 100644 --- a/contracts/feature-tests/composability/interact/src/comp_interact_controller.rs +++ b/contracts/feature-tests/composability/interact/src/comp_interact_controller.rs @@ -16,7 +16,7 @@ pub struct ComposabilityInteract { impl ComposabilityInteract { pub async fn init() -> Self { let config = Config::load_config(); - let mut interactor = Interactor::new(config.gateway()) + let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator()) .await .with_tracer(INTERACTOR_SCENARIO_TRACE_PATH) .await; diff --git a/framework/meta/src/cli/cli_args_standalone.rs b/framework/meta/src/cli/cli_args_standalone.rs index ce2c3b66a6..28b47d51e1 100644 --- a/framework/meta/src/cli/cli_args_standalone.rs +++ b/framework/meta/src/cli/cli_args_standalone.rs @@ -408,11 +408,19 @@ pub struct InstallWasmOptArgs {} #[derive(Default, Clone, PartialEq, Eq, Debug, Args)] pub struct AccountArgs { - /// Provide the target API you want the real data to come from + /// Provide the target API you want the data to come from #[arg(long = "api")] #[clap(global = true)] pub api: Option, + /// Provide if the API is a chain simulator or not + #[arg( + long = "chain-simulator", + default_value = "false", + verbatim_doc_comment + )] + pub chain_simulator: Option, + /// Provide the address you want to retrieve data from #[arg(long = "address", verbatim_doc_comment)] pub address: String, diff --git a/framework/meta/src/cmd/retrieve_address.rs b/framework/meta/src/cmd/retrieve_address.rs index f20b5b8e05..15335408ef 100644 --- a/framework/meta/src/cmd/retrieve_address.rs +++ b/framework/meta/src/cmd/retrieve_address.rs @@ -5,5 +5,11 @@ use crate::cli::AccountArgs; /// Interprets arguments and call the account tool from `multiversx_sc_snippets`. pub async fn retrieve_address(args: &AccountArgs) { let api_string = args.api.clone().expect("API needs to be specified"); - account_tool::print_account_as_scenario_set_state(api_string, args.address.to_string()).await; + let use_chain_simulator = args.chain_simulator.unwrap_or_default(); + account_tool::print_account_as_scenario_set_state( + api_string, + use_chain_simulator, + args.address.to_string(), + ) + .await; } diff --git a/framework/snippets/src/account_tool.rs b/framework/snippets/src/account_tool.rs index c40407e360..3370fc738e 100644 --- a/framework/snippets/src/account_tool.rs +++ b/framework/snippets/src/account_tool.rs @@ -15,9 +15,10 @@ use std::collections::{BTreeMap, HashMap}; /// then formats it as a scenario set state step. pub async fn print_account_as_scenario_set_state( api_string: String, + use_chain_simulator: bool, address_bech32_string: String, ) { - let api = GatewayProxy::new(api_string); + let api = GatewayProxy::new(api_string, use_chain_simulator); let address = Bech32Address::from_bech32_string(address_bech32_string); let set_state = retrieve_account_as_scenario_set_state(&api, &address).await; let scenario = build_scenario(set_state); diff --git a/framework/snippets/src/interactor.rs b/framework/snippets/src/interactor.rs index 702121c373..3102d88d64 100644 --- a/framework/snippets/src/interactor.rs +++ b/framework/snippets/src/interactor.rs @@ -32,8 +32,8 @@ pub struct Interactor { } impl Interactor { - pub async fn new(gateway_url: &str) -> Self { - let proxy = GatewayProxy::new(gateway_url.to_string()); + pub async fn new(gateway_uri: &str, use_chain_simulator: bool) -> Self { + let proxy = GatewayProxy::new(gateway_uri.to_string(), use_chain_simulator); let network_config = proxy.get_network_config().await.unwrap(); Self { proxy, diff --git a/sdk/core/examples/account.rs b/sdk/core/examples/account.rs index 5e9456f4b0..92e3914717 100644 --- a/sdk/core/examples/account.rs +++ b/sdk/core/examples/account.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ data::address::Address, - gateway::{GatewayProxy, DEVNET_GATEWAY}, + gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}, }; #[tokio::main] @@ -10,7 +10,7 @@ async fn main() { ) .unwrap(); - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let account = blockchain.get_account(&addr).await.unwrap(); println!("account: {account:#?}"); diff --git a/sdk/core/examples/account_storage.rs b/sdk/core/examples/account_storage.rs index 7693c26e08..c7d5bda76e 100644 --- a/sdk/core/examples/account_storage.rs +++ b/sdk/core/examples/account_storage.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ data::address::Address, - gateway::{GatewayProxy, DEVNET_GATEWAY}, + gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}, }; #[tokio::main] @@ -10,7 +10,7 @@ async fn main() { ) .unwrap(); - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let account_storage = blockchain.get_account_storage_keys(&addr).await.unwrap(); println!("Account Storage: {account_storage:#?}"); diff --git a/sdk/core/examples/get_esdt_tokens.rs b/sdk/core/examples/get_esdt_tokens.rs index fac424da27..6c2f663e09 100644 --- a/sdk/core/examples/get_esdt_tokens.rs +++ b/sdk/core/examples/get_esdt_tokens.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ data::address::Address, - gateway::{GatewayProxy, DEVNET_GATEWAY}, + gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}, }; #[tokio::main] @@ -10,7 +10,7 @@ async fn main() { ) .unwrap(); - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let balances = blockchain.get_account_esdt_tokens(&addr).await.unwrap(); println!("{balances:#?}"); diff --git a/sdk/core/examples/get_hyper_block_by_hash.rs b/sdk/core/examples/get_hyper_block_by_hash.rs index 1d7f969ce1..a67ea311d3 100644 --- a/sdk/core/examples/get_hyper_block_by_hash.rs +++ b/sdk/core/examples/get_hyper_block_by_hash.rs @@ -1,8 +1,8 @@ -use multiversx_sdk::gateway::{GatewayProxy, DEVNET_GATEWAY}; +use multiversx_sdk::gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}; #[tokio::main] async fn main() { - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let result = blockchain .get_hyper_block_by_hash("20b14ba0e68c465810c5ded091f220e51dad41629d7ccd87dab572206185e419") .await; diff --git a/sdk/core/examples/get_hyper_block_by_nonce.rs b/sdk/core/examples/get_hyper_block_by_nonce.rs index 575255d6c9..68ec349081 100644 --- a/sdk/core/examples/get_hyper_block_by_nonce.rs +++ b/sdk/core/examples/get_hyper_block_by_nonce.rs @@ -1,8 +1,8 @@ -use multiversx_sdk::gateway::{GatewayProxy, DEVNET_GATEWAY}; +use multiversx_sdk::gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}; #[tokio::main] async fn main() { - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let result = blockchain.get_hyper_block_by_nonce(7468).await; println!("block by nonce result: {result:?}") diff --git a/sdk/core/examples/get_hyper_block_latest.rs b/sdk/core/examples/get_hyper_block_latest.rs index 934358fdbf..fe49fa7f1e 100644 --- a/sdk/core/examples/get_hyper_block_latest.rs +++ b/sdk/core/examples/get_hyper_block_latest.rs @@ -1,8 +1,8 @@ -use multiversx_sdk::gateway::{GatewayProxy, DEVNET_GATEWAY}; +use multiversx_sdk::gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}; #[tokio::main] async fn main() { - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let result = blockchain.get_latest_hyper_block_nonce(false).await; println!("latest block result: {result:?}") diff --git a/sdk/core/examples/get_network_config.rs b/sdk/core/examples/get_network_config.rs index d53e0e7ce1..481cc31da3 100644 --- a/sdk/core/examples/get_network_config.rs +++ b/sdk/core/examples/get_network_config.rs @@ -1,8 +1,8 @@ -use multiversx_sdk::gateway::{GatewayProxy, DEVNET_GATEWAY}; +use multiversx_sdk::gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}; #[tokio::main] async fn main() { - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let network_config = blockchain.get_network_config().await.unwrap(); println!("network_config: {network_config:#?}") diff --git a/sdk/core/examples/get_network_economics.rs b/sdk/core/examples/get_network_economics.rs index bb0410c8f0..f7cf9e808b 100644 --- a/sdk/core/examples/get_network_economics.rs +++ b/sdk/core/examples/get_network_economics.rs @@ -1,8 +1,8 @@ -use multiversx_sdk::gateway::{GatewayProxy, DEVNET_GATEWAY}; +use multiversx_sdk::gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}; #[tokio::main] async fn main() { - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let network_economics = blockchain.get_network_economics().await.unwrap(); println!("network_economics: {network_economics:#?}") diff --git a/sdk/core/examples/sign_tx.rs b/sdk/core/examples/sign_tx.rs index aa3f977321..50d2a885d6 100644 --- a/sdk/core/examples/sign_tx.rs +++ b/sdk/core/examples/sign_tx.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ data::transaction::Transaction, - gateway::{GatewayProxy, DEVNET_GATEWAY}, + gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}, wallet::Wallet, }; @@ -11,7 +11,7 @@ async fn main() { ) .unwrap(); let addr = wl.address(); - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let network_config = blockchain.get_network_config().await.unwrap(); let arg = blockchain diff --git a/sdk/core/examples/sign_txs.rs b/sdk/core/examples/sign_txs.rs index 2860d528f7..35cc6d2e01 100644 --- a/sdk/core/examples/sign_txs.rs +++ b/sdk/core/examples/sign_txs.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ data::transaction::Transaction, - gateway::{GatewayProxy, DEVNET_GATEWAY}, + gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}, wallet::Wallet, }; @@ -11,7 +11,7 @@ async fn main() { ) .unwrap(); let addr = wl.address(); - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let network_config = blockchain.get_network_config().await.unwrap(); let arg = blockchain diff --git a/sdk/core/examples/tx_cost.rs b/sdk/core/examples/tx_cost.rs index a982b130ec..1d24230c12 100644 --- a/sdk/core/examples/tx_cost.rs +++ b/sdk/core/examples/tx_cost.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ data::{address::Address, transaction::Transaction}, - gateway::{GatewayProxy, DEVNET_GATEWAY}, + gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}, utils::base64_encode, }; @@ -26,7 +26,7 @@ async fn main() { signature: None, }; - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let cost = blockchain.request_transaction_cost(&tx).await.unwrap(); println!("tx cost: {cost:#?}"); diff --git a/sdk/core/examples/tx_default_args.rs b/sdk/core/examples/tx_default_args.rs index 01b91aa5a6..76412b34d4 100644 --- a/sdk/core/examples/tx_default_args.rs +++ b/sdk/core/examples/tx_default_args.rs @@ -1,11 +1,11 @@ use multiversx_sdk::{ data::address::Address, - gateway::{GatewayProxy, DEVNET_GATEWAY}, + gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}, }; #[tokio::main] async fn main() { - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let network_config = blockchain.get_network_config().await.unwrap(); let addr = Address::from_bech32_string( "erd1qqqqqqqqqqqqqpgqfzydqmdw7m2vazsp6u5p95yxz76t2p9rd8ss0zp9ts", diff --git a/sdk/core/examples/tx_info.rs b/sdk/core/examples/tx_info.rs index f7126daca9..11481d2ec1 100644 --- a/sdk/core/examples/tx_info.rs +++ b/sdk/core/examples/tx_info.rs @@ -1,9 +1,9 @@ -use multiversx_sdk::gateway::{GatewayProxy, DEVNET_GATEWAY}; +use multiversx_sdk::gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}; #[tokio::main] async fn main() { let tx_hash = "49edb289892a655a0e988b360c19326c21107f9696c6197b435667c6e8c6e1a3"; - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let status = blockchain.get_transaction_status(tx_hash).await; println!("tx status: {status:?}"); diff --git a/sdk/core/examples/vm_query.rs b/sdk/core/examples/vm_query.rs index d07f49b856..750316fee6 100644 --- a/sdk/core/examples/vm_query.rs +++ b/sdk/core/examples/vm_query.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ data::{address::Address, vm::VmValueRequest}, - gateway::{GatewayProxy, DEVNET_GATEWAY}, + gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY}, wallet::Wallet, }; @@ -11,7 +11,7 @@ async fn main() { ) .unwrap(); let addr = wl.address(); - let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let req = VmValueRequest { sc_address: Address::from_bech32_string( "erd1qqqqqqqqqqqqqpgqhn3ae8dpc957t7jadn7kywtg503dy7pnj9ts3umqxx", diff --git a/sdk/core/src/gateway.rs b/sdk/core/src/gateway.rs index 33a263607e..c0fc0062d2 100644 --- a/sdk/core/src/gateway.rs +++ b/sdk/core/src/gateway.rs @@ -1,10 +1,10 @@ mod gateway_account; mod gateway_block; +mod gateway_chain_simulator; mod gateway_network; mod gateway_proxy; mod gateway_tx; mod gateway_tx_retrieve; -mod gateway_chain_simulator; pub use gateway_proxy::GatewayProxy; @@ -15,3 +15,5 @@ pub const SIMULATOR_GATEWAY: &str = "http://localhost:8085"; // MetachainShardId will be used to identify a shard ID as metachain pub const METACHAIN_SHARD_ID: u32 = 0xFFFFFFFF; + +pub const DEFAULT_USE_CHAIN_SIMULATOR: bool = false; diff --git a/sdk/core/src/gateway/gateway_proxy.rs b/sdk/core/src/gateway/gateway_proxy.rs index f3693c6e12..46e48455e5 100644 --- a/sdk/core/src/gateway/gateway_proxy.rs +++ b/sdk/core/src/gateway/gateway_proxy.rs @@ -1,27 +1,23 @@ use reqwest::Client; -pub const CHAIN_SIMULATOR_GATEWAY: &str = "http://localhost:8085"; - /// Allows communication with the MultiversX gateway API. #[derive(Clone, Debug)] pub struct GatewayProxy { - pub(crate) proxy_url: String, + pub(crate) proxy_uri: String, pub(crate) client: Client, pub chain_simulator: bool, } impl GatewayProxy { - pub fn new(proxy_url: String) -> Self { - let chain_simulator = proxy_url == CHAIN_SIMULATOR_GATEWAY; - + pub fn new(proxy_uri: String, chain_simulator: bool) -> Self { Self { - proxy_url, + proxy_uri, client: Client::new(), chain_simulator, } } pub(crate) fn get_endpoint(&self, endpoint: &str) -> String { - format!("{}/{}", self.proxy_url, endpoint) + format!("{}/{}", self.proxy_uri, endpoint) } } diff --git a/tools/interactor-system-func-calls/config.toml b/tools/interactor-system-func-calls/config.toml index 61ac8dbf87..dfc1ba62a6 100644 --- a/tools/interactor-system-func-calls/config.toml +++ b/tools/interactor-system-func-calls/config.toml @@ -1 +1,2 @@ -gateway = 'https://devnet-gateway.multiversx.com' +chain_type = 'real' +gateway_uri = 'https://devnet-gateway.multiversx.com' diff --git a/tools/interactor-system-func-calls/src/system_sc_interact.rs b/tools/interactor-system-func-calls/src/system_sc_interact.rs index 4ec3c6647e..dbfa11a14e 100644 --- a/tools/interactor-system-func-calls/src/system_sc_interact.rs +++ b/tools/interactor-system-func-calls/src/system_sc_interact.rs @@ -227,7 +227,8 @@ struct SysFuncCallsInteract { impl SysFuncCallsInteract { async fn init() -> Self { let config = Config::load_config(); - let mut interactor = Interactor::new(config.gateway()).await; + let mut interactor = + Interactor::new(config.gateway_uri(), config.use_chain_simulator()).await; let wallet_address = interactor.register_wallet(test_wallets::alice()).await; diff --git a/tools/interactor-system-func-calls/src/system_sc_interact_config.rs b/tools/interactor-system-func-calls/src/system_sc_interact_config.rs index 4495f069b8..5058d06268 100644 --- a/tools/interactor-system-func-calls/src/system_sc_interact_config.rs +++ b/tools/interactor-system-func-calls/src/system_sc_interact_config.rs @@ -2,12 +2,13 @@ use serde::Deserialize; use std::io::Read; /// Config file -const CONFIG_FILE: &str = "../config.toml"; +const CONFIG_FILE: &str = "config.toml"; /// SysFuncCalls Interact configuration #[derive(Debug, Deserialize)] pub struct Config { - gateway: String, + gateway_uri: String, + chain_type: String, } impl Config { @@ -19,8 +20,20 @@ impl Config { toml::from_str(&content).unwrap() } - // Returns the gateway - pub fn gateway(&self) -> &str { - &self.gateway + // Returns the gateway URI + pub fn gateway_uri(&self) -> &str { + &self.gateway_uri + } + + // Returns if chain type is chain simulator + pub fn use_chain_simulator(&self) -> bool { + match self.chain_type.as_str() { + "simulator" => return true, + "real" => return false, + _ => panic!( + "Invalid chain type: {}. Expected 'simulator' or 'real'.", + self.chain_type + ), + } } } From eb7dd570c109be75d9cab8919bf134efe5ba292a Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Fri, 20 Sep 2024 09:32:37 +0300 Subject: [PATCH 07/30] chain sim - add gateway_uri in adder config --- contracts/examples/adder/interact/config.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contracts/examples/adder/interact/config.toml b/contracts/examples/adder/interact/config.toml index e474de08f5..d40bebad7f 100644 --- a/contracts/examples/adder/interact/config.toml +++ b/contracts/examples/adder/interact/config.toml @@ -1 +1,2 @@ -chain_type = "simulator" +chain_type = 'real' +gateway_uri = 'http://localhost:8085' \ No newline at end of file From 69d09a5467bf4cf31bb242bdcf53a8ea3d8f90c0 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 20 Sep 2024 15:38:09 +0300 Subject: [PATCH 08/30] unified syntax - tx hash in blackbox/whitebox tests --- .../scenario-tester/tests/st_blackbox_test.rs | 42 +++++++++++++++++++ .../scenario-tester/tests/st_whitebox_test.rs | 33 +++++++++++++++ framework/base/src/types/interaction/tx.rs | 6 +-- .../facade/result_handlers/returns_tx_hash.rs | 5 ++- .../facade/world_tx/scenario_tx_whitebox.rs | 24 ++--------- .../src/scenario/model/step/sc_call_step.rs | 3 +- .../src/scenario/model/step/sc_deploy_step.rs | 7 ++-- .../scenario/model/transaction/tx_response.rs | 6 +-- framework/snippets/src/network_response.rs | 14 ++++--- 9 files changed, 102 insertions(+), 38 deletions(-) diff --git a/contracts/feature-tests/scenario-tester/tests/st_blackbox_test.rs b/contracts/feature-tests/scenario-tester/tests/st_blackbox_test.rs index cdcf610b25..d3f78692e8 100644 --- a/contracts/feature-tests/scenario-tester/tests/st_blackbox_test.rs +++ b/contracts/feature-tests/scenario-tester/tests/st_blackbox_test.rs @@ -244,3 +244,45 @@ fn set_state_test() { .balance(600) .esdt_balance(TOKEN_ID, 60); } + +#[test] +fn st_blackbox_tx_hash() { + let mut world = world(); + + world + .account(OWNER_ADDRESS) + .nonce(1) + .balance(100) + .account(OTHER_ADDRESS) + .nonce(2) + .balance(300) + .esdt_balance(TOKEN_ID, 500) + .commit(); + + let (new_address, tx_hash) = world + .tx() + .from(OWNER_ADDRESS) + .typed(scenario_tester_proxy::ScenarioTesterProxy) + .init(5u32) + .code(CODE_PATH) + .new_address(ST_ADDRESS) + .tx_hash([11u8; 32]) + .returns(ReturnsNewAddress) + .returns(ReturnsTxHash) + .run(); + + assert_eq!(new_address, ST_ADDRESS.to_address()); + assert_eq!(tx_hash.as_array(), &[11u8; 32]); + + let tx_hash = world + .tx() + .from(OWNER_ADDRESS) + .to(ST_ADDRESS) + .typed(scenario_tester_proxy::ScenarioTesterProxy) + .add(1u32) + .tx_hash([22u8; 32]) + .returns(ReturnsTxHash) + .run(); + + assert_eq!(tx_hash.as_array(), &[22u8; 32]); +} diff --git a/contracts/feature-tests/scenario-tester/tests/st_whitebox_test.rs b/contracts/feature-tests/scenario-tester/tests/st_whitebox_test.rs index 61dd6cdc2d..99e56d29f1 100644 --- a/contracts/feature-tests/scenario-tester/tests/st_whitebox_test.rs +++ b/contracts/feature-tests/scenario-tester/tests/st_whitebox_test.rs @@ -52,3 +52,36 @@ fn st_whitebox() { .check_account(SCENARIO_TESTER) .check_storage("str:sum", "8"); } + +#[test] +fn st_whitebox_tx_hash() { + let mut world = world(); + + world.account(OWNER).nonce(1); + + let (new_address, tx_hash) = world + .tx() + .from(OWNER) + .raw_deploy() + .code(ST_PATH_EXPR) + .new_address(SCENARIO_TESTER) + .tx_hash([11u8; 32]) + .returns(ReturnsNewBech32Address) + .returns(ReturnsTxHash) + .whitebox(scenario_tester::contract_obj, |sc| { + sc.init(BigUint::from(5u64)); + }); + + assert_eq!(new_address.to_address(), SCENARIO_TESTER.to_address()); + assert_eq!(tx_hash.as_array(), &[11u8; 32]); + + let tx_hash = world + .tx() + .from(OWNER) + .to(SCENARIO_TESTER) + .tx_hash([22u8; 32]) + .returns(ReturnsTxHash) + .whitebox(scenario_tester::contract_obj, |sc| sc.add(3u32.into())); + + assert_eq!(tx_hash.as_array(), &[22u8; 32]); +} diff --git a/framework/base/src/types/interaction/tx.rs b/framework/base/src/types/interaction/tx.rs index 905b9af8fc..b216ce0f28 100644 --- a/framework/base/src/types/interaction/tx.rs +++ b/framework/base/src/types/interaction/tx.rs @@ -908,11 +908,11 @@ where impl Tx where Env: TxEnvWithTxHash, - From: TxFromSpecified, + From: TxFrom, To: TxTo, - Payment: TxPaymentEgldOnly, + Payment: TxPayment, Gas: TxGas, - Data: TxDataFunctionCall, + Data: TxData, RH: TxResultHandler, { /// Sets the mock transaction hash to be used in a test. diff --git a/framework/scenario/src/facade/result_handlers/returns_tx_hash.rs b/framework/scenario/src/facade/result_handlers/returns_tx_hash.rs index 7494b41edc..fb30ea1ff3 100644 --- a/framework/scenario/src/facade/result_handlers/returns_tx_hash.rs +++ b/framework/scenario/src/facade/result_handlers/returns_tx_hash.rs @@ -1,3 +1,4 @@ +use multiversx_chain_vm::types::H256; use multiversx_sc::types::RHListItemExec; use crate::{ @@ -11,7 +12,7 @@ impl RHListItem for ReturnsTxHash where Env: TxEnv, { - type Returns = String; + type Returns = H256; } impl RHListItemExec for ReturnsTxHash @@ -19,6 +20,6 @@ where Env: TxEnv, { fn item_process_result(self, raw_result: &TxResponse) -> Self::Returns { - raw_result.tx_hash.clone() + raw_result.tx_hash.clone().expect("missing tx hash") } } diff --git a/framework/scenario/src/facade/world_tx/scenario_tx_whitebox.rs b/framework/scenario/src/facade/world_tx/scenario_tx_whitebox.rs index e20538ce0e..f8d03dc6da 100644 --- a/framework/scenario/src/facade/world_tx/scenario_tx_whitebox.rs +++ b/framework/scenario/src/facade/world_tx/scenario_tx_whitebox.rs @@ -1,5 +1,3 @@ -use core::str; - use crate::debug_executor::contract_instance_wrapped_execution; use crate::scenario::tx_to_step::TxToQueryStep; use crate::{ @@ -57,6 +55,7 @@ where let contract_obj = contract_obj_builder(); let mut step_wrapper = self.tx_to_step(); + step_wrapper.step.explicit_tx_hash = core::mem::take(&mut step_wrapper.env.data.tx_hash); let (new_address, tx_result) = step_wrapper .env .world @@ -70,15 +69,6 @@ where }); let mut response = TxResponse::from_tx_result(tx_result); - if let Some(tx_hash) = &step_wrapper.env.data.tx_hash { - let tx_hash_bytes = tx_hash.as_bytes(); - response.tx_hash = str::from_utf8(tx_hash_bytes) - .expect("tx hash not utf8 valid") - .to_string(); - } else { - response.tx_hash = String::from(""); - } - response.new_deployed_address = Some(new_address); step_wrapper.step.save_response(response); step_wrapper.process_result() @@ -133,6 +123,7 @@ where let contract_obj = contract_obj_builder(); let mut step_wrapper = self.tx_to_step(); + step_wrapper.step.explicit_tx_hash = core::mem::take(&mut step_wrapper.env.data.tx_hash); // no endpoint is called per se, but if it is empty, the VM thinks it is a simple transfer of value if step_wrapper.step.tx.function.is_empty() { @@ -151,16 +142,7 @@ where }); }); - let mut response = TxResponse::from_tx_result(tx_result); - if let Some(tx_hash) = &step_wrapper.env.data.tx_hash { - let tx_hash_bytes = tx_hash.as_bytes(); - response.tx_hash = str::from_utf8(tx_hash_bytes) - .expect("tx hash not utf8 valid") - .to_string(); - } else { - response.tx_hash = String::from(""); - } - + let response = TxResponse::from_tx_result(tx_result); step_wrapper.step.save_response(response); step_wrapper.process_result() } diff --git a/framework/scenario/src/scenario/model/step/sc_call_step.rs b/framework/scenario/src/scenario/model/step/sc_call_step.rs index 4a68bfa000..2a25fb2c2f 100644 --- a/framework/scenario/src/scenario/model/step/sc_call_step.rs +++ b/framework/scenario/src/scenario/model/step/sc_call_step.rs @@ -205,12 +205,13 @@ impl ScCallStep { .expect("SC call response not yet available") } - pub fn save_response(&mut self, tx_response: TxResponse) { + pub fn save_response(&mut self, mut tx_response: TxResponse) { if let Some(expect) = &mut self.expect { if expect.build_from_response { expect.update_from_response(&tx_response) } } + tx_response.tx_hash = self.explicit_tx_hash.as_ref().map(|vm_hash| vm_hash.as_array().into()); self.response = Some(tx_response); } } diff --git a/framework/scenario/src/scenario/model/step/sc_deploy_step.rs b/framework/scenario/src/scenario/model/step/sc_deploy_step.rs index 7e520e1a17..3b22522f69 100644 --- a/framework/scenario/src/scenario/model/step/sc_deploy_step.rs +++ b/framework/scenario/src/scenario/model/step/sc_deploy_step.rs @@ -135,13 +135,14 @@ impl ScDeployStep { .expect("SC deploy response not yet available") } - pub fn save_response(&mut self, response: TxResponse) { + pub fn save_response(&mut self, mut tx_response: TxResponse) { if let Some(expect) = &mut self.expect { if expect.build_from_response { - expect.update_from_response(&response) + expect.update_from_response(&tx_response) } } - self.response = Some(response); + tx_response.tx_hash = self.explicit_tx_hash.as_ref().map(|vm_hash| vm_hash.as_array().into()); + self.response = Some(tx_response); } } diff --git a/framework/scenario/src/scenario/model/transaction/tx_response.rs b/framework/scenario/src/scenario/model/transaction/tx_response.rs index b230fd1878..547b381338 100644 --- a/framework/scenario/src/scenario/model/transaction/tx_response.rs +++ b/framework/scenario/src/scenario/model/transaction/tx_response.rs @@ -1,4 +1,4 @@ -use multiversx_chain_vm::tx_mock::TxResult; +use multiversx_chain_vm::{tx_mock::TxResult, types::H256}; use multiversx_sc::types::Address; use super::{Log, TxExpect, TxResponseStatus}; @@ -20,8 +20,8 @@ pub struct TxResponse { pub gas: u64, /// The refund of the transaction. pub refund: u64, - /// The transaction hash - pub tx_hash: String, + /// The transaction hash, if available. + pub tx_hash: Option, } impl TxResponse { diff --git a/framework/snippets/src/network_response.rs b/framework/snippets/src/network_response.rs index 4684fdce3c..b5839e2e10 100644 --- a/framework/snippets/src/network_response.rs +++ b/framework/snippets/src/network_response.rs @@ -1,6 +1,6 @@ use multiversx_sc_scenario::{ imports::{Address, ESDTSystemSCAddress}, - multiversx_chain_vm::crypto_functions::keccak256, + multiversx_chain_vm::{crypto_functions::keccak256, types::H256}, scenario_model::{Log, TxResponse, TxResponseStatus}, }; use multiversx_sdk::{ @@ -17,7 +17,7 @@ pub fn parse_tx_response(tx: TransactionOnNetwork) -> TxResponse { if !tx_error.is_success() { return TxResponse { tx_error, - tx_hash: process_tx_hash(&tx).to_string(), + tx_hash: process_tx_hash(&tx), ..Default::default() }; } @@ -46,13 +46,17 @@ fn process_success(tx: &TransactionOnNetwork) -> TxResponse { new_deployed_address: process_new_deployed_address(tx), new_issued_token_identifier: process_new_issued_token_identifier(tx), logs: process_logs(tx), - tx_hash: process_tx_hash(tx).to_string(), + tx_hash: process_tx_hash(tx), ..Default::default() } } -fn process_tx_hash(tx: &TransactionOnNetwork) -> &str { - tx.hash.as_deref().unwrap_or("") +fn process_tx_hash(tx: &TransactionOnNetwork) -> Option { + tx.hash.as_ref().map(|encoded_hash| { + let decoded = hex::decode(encoded_hash).expect("error decoding tx hash from hex"); + assert_eq!(decoded.len(), 32); + H256::from_slice(&decoded) + }) } fn process_out(tx: &TransactionOnNetwork) -> Vec> { From b8853bc338e7e65b9f92eaa22dd9bcb348bb2e41 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 20 Sep 2024 15:55:56 +0300 Subject: [PATCH 09/30] unified syntax - tx hash refactor --- framework/base/src/types/interaction/tx_env.rs | 3 +++ .../src/facade/world_tx/scenario_exec_call.rs | 9 +++++---- .../src/facade/world_tx/scenario_exec_deploy.rs | 1 - .../src/facade/world_tx/scenario_tx_env.rs | 13 ++++++++++++- .../src/facade/world_tx/scenario_tx_whitebox.rs | 2 -- .../src/scenario/model/step/sc_call_step.rs | 5 ++++- .../src/scenario/model/step/sc_deploy_step.rs | 5 ++++- .../src/scenario/tx_to_step/tx_to_step_call.rs | 9 +++++---- .../src/scenario/tx_to_step/tx_to_step_deploy.rs | 8 +++++--- .../src/interactor_tx/interactor_exec_env.rs | 14 +++++++++++++- 10 files changed, 51 insertions(+), 18 deletions(-) diff --git a/framework/base/src/types/interaction/tx_env.rs b/framework/base/src/types/interaction/tx_env.rs index 37e18d7dec..188e304311 100644 --- a/framework/base/src/types/interaction/tx_env.rs +++ b/framework/base/src/types/interaction/tx_env.rs @@ -27,4 +27,7 @@ pub trait TxEnvMockDeployAddress: TxEnv { pub trait TxEnvWithTxHash: TxEnv { fn set_tx_hash(&mut self, tx_hash: H256); + + /// Retrieves current tx hash, while resetting it in self. + fn take_tx_hash(&mut self) -> Option; } diff --git a/framework/scenario/src/facade/world_tx/scenario_exec_call.rs b/framework/scenario/src/facade/world_tx/scenario_exec_call.rs index 8905c7d14d..ca09b9d318 100644 --- a/framework/scenario/src/facade/world_tx/scenario_exec_call.rs +++ b/framework/scenario/src/facade/world_tx/scenario_exec_call.rs @@ -85,7 +85,6 @@ where fn run(self) -> Self::Returns { let mut step_wrapper = self.tx_to_step(); - step_wrapper.step.explicit_tx_hash = core::mem::take(&mut step_wrapper.env.data.tx_hash); step_wrapper.env.world.sc_call(&mut step_wrapper.step); step_wrapper.process_result() } @@ -111,7 +110,6 @@ where fn run(self) -> Self::Returns { let mut step_wrapper = self.tx_to_step(); - step_wrapper.step.explicit_tx_hash = core::mem::take(&mut step_wrapper.env.data.tx_hash); step_wrapper.env.world.sc_call(&mut step_wrapper.step); step_wrapper.process_result() } @@ -119,8 +117,11 @@ where impl<'w> TxEnvWithTxHash for ScenarioEnvExec<'w> { fn set_tx_hash(&mut self, tx_hash: H256) { - assert!(self.data.tx_hash.is_none(), "tx hash set twice"); - self.data.tx_hash = Some(tx_hash); + self.data.set_tx_hash(tx_hash); + } + + fn take_tx_hash(&mut self) -> Option { + self.data.take_tx_hash() } } diff --git a/framework/scenario/src/facade/world_tx/scenario_exec_deploy.rs b/framework/scenario/src/facade/world_tx/scenario_exec_deploy.rs index 40fbbaa878..866d4f562f 100644 --- a/framework/scenario/src/facade/world_tx/scenario_exec_deploy.rs +++ b/framework/scenario/src/facade/world_tx/scenario_exec_deploy.rs @@ -35,7 +35,6 @@ where fn run(self) -> Self::Returns { let mut step_wrapper = self.tx_to_step(); - step_wrapper.step.explicit_tx_hash = core::mem::take(&mut step_wrapper.env.data.tx_hash); step_wrapper.env.world.sc_deploy(&mut step_wrapper.step); step_wrapper.process_result() } diff --git a/framework/scenario/src/facade/world_tx/scenario_tx_env.rs b/framework/scenario/src/facade/world_tx/scenario_tx_env.rs index 339ed9b38b..c3a2a1c204 100644 --- a/framework/scenario/src/facade/world_tx/scenario_tx_env.rs +++ b/framework/scenario/src/facade/world_tx/scenario_tx_env.rs @@ -1,5 +1,5 @@ use multiversx_chain_scenario_format::interpret_trait::InterpreterContext; -use multiversx_sc::types::{ManagedAddress, ManagedBuffer, TxEnv, H256}; +use multiversx_sc::types::{ManagedAddress, ManagedBuffer, TxEnv, TxEnvWithTxHash, H256}; use crate::{api::StaticApi, scenario_model::TxExpect, ScenarioWorld}; @@ -33,6 +33,17 @@ impl TxEnv for ScenarioTxEnvData { } } +impl TxEnvWithTxHash for ScenarioTxEnvData { + fn set_tx_hash(&mut self, tx_hash: H256) { + assert!(self.tx_hash.is_none(), "tx hash set twice"); + self.tx_hash = Some(tx_hash); + } + + fn take_tx_hash(&mut self) -> Option { + core::mem::take(&mut self.tx_hash) + } +} + impl ScenarioTxEnvData { pub fn interpreter_context(&self) -> InterpreterContext { self.interpreter_context.clone() diff --git a/framework/scenario/src/facade/world_tx/scenario_tx_whitebox.rs b/framework/scenario/src/facade/world_tx/scenario_tx_whitebox.rs index f8d03dc6da..62842a6e25 100644 --- a/framework/scenario/src/facade/world_tx/scenario_tx_whitebox.rs +++ b/framework/scenario/src/facade/world_tx/scenario_tx_whitebox.rs @@ -55,7 +55,6 @@ where let contract_obj = contract_obj_builder(); let mut step_wrapper = self.tx_to_step(); - step_wrapper.step.explicit_tx_hash = core::mem::take(&mut step_wrapper.env.data.tx_hash); let (new_address, tx_result) = step_wrapper .env .world @@ -123,7 +122,6 @@ where let contract_obj = contract_obj_builder(); let mut step_wrapper = self.tx_to_step(); - step_wrapper.step.explicit_tx_hash = core::mem::take(&mut step_wrapper.env.data.tx_hash); // no endpoint is called per se, but if it is empty, the VM thinks it is a simple transfer of value if step_wrapper.step.tx.function.is_empty() { diff --git a/framework/scenario/src/scenario/model/step/sc_call_step.rs b/framework/scenario/src/scenario/model/step/sc_call_step.rs index 2a25fb2c2f..8d8d71b0e8 100644 --- a/framework/scenario/src/scenario/model/step/sc_call_step.rs +++ b/framework/scenario/src/scenario/model/step/sc_call_step.rs @@ -211,7 +211,10 @@ impl ScCallStep { expect.update_from_response(&tx_response) } } - tx_response.tx_hash = self.explicit_tx_hash.as_ref().map(|vm_hash| vm_hash.as_array().into()); + tx_response.tx_hash = self + .explicit_tx_hash + .as_ref() + .map(|vm_hash| vm_hash.as_array().into()); self.response = Some(tx_response); } } diff --git a/framework/scenario/src/scenario/model/step/sc_deploy_step.rs b/framework/scenario/src/scenario/model/step/sc_deploy_step.rs index 3b22522f69..6090cfd492 100644 --- a/framework/scenario/src/scenario/model/step/sc_deploy_step.rs +++ b/framework/scenario/src/scenario/model/step/sc_deploy_step.rs @@ -141,7 +141,10 @@ impl ScDeployStep { expect.update_from_response(&tx_response) } } - tx_response.tx_hash = self.explicit_tx_hash.as_ref().map(|vm_hash| vm_hash.as_array().into()); + tx_response.tx_hash = self + .explicit_tx_hash + .as_ref() + .map(|vm_hash| vm_hash.as_array().into()); self.response = Some(tx_response); } } diff --git a/framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs b/framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs index 475326eac7..1d1eb2c9a2 100644 --- a/framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs +++ b/framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs @@ -1,6 +1,6 @@ use multiversx_sc::types::{ - Code, FunctionCall, NotPayable, RHListExec, Tx, TxEnv, TxFromSpecified, TxGas, TxPayment, - TxToSpecified, UpgradeCall, + Code, FunctionCall, NotPayable, RHListExec, Tx, TxEnv, TxEnvWithTxHash, TxFromSpecified, TxGas, + TxPayment, TxToSpecified, UpgradeCall, }; use crate::{ @@ -14,7 +14,7 @@ use super::{address_annotated, gas_annotated, StepWrapper, TxToStep}; impl TxToStep for Tx, RH> where - Env: TxEnv, + Env: TxEnvWithTxHash, From: TxFromSpecified, To: TxToSpecified, Payment: TxPayment, @@ -23,7 +23,7 @@ where { type Step = ScCallStep; - fn tx_to_step(self) -> StepWrapper { + fn tx_to_step(mut self) -> StepWrapper { let mut step = tx_to_sc_call_step( &self.env, self.from, @@ -32,6 +32,7 @@ where self.gas, self.data, ); + step.explicit_tx_hash = self.env.take_tx_hash(); step.expect = Some(self.result_handler.list_tx_expect()); StepWrapper { diff --git a/framework/scenario/src/scenario/tx_to_step/tx_to_step_deploy.rs b/framework/scenario/src/scenario/tx_to_step/tx_to_step_deploy.rs index 4e49be51b4..1c7532e27a 100644 --- a/framework/scenario/src/scenario/tx_to_step/tx_to_step_deploy.rs +++ b/framework/scenario/src/scenario/tx_to_step/tx_to_step_deploy.rs @@ -1,5 +1,6 @@ use multiversx_sc::types::{ - Code, DeployCall, RHListExec, Tx, TxCodeValue, TxEnv, TxFromSpecified, TxGas, TxPayment, + Code, DeployCall, RHListExec, Tx, TxCodeValue, TxEnv, TxEnvWithTxHash, TxFromSpecified, TxGas, + TxPayment, }; use crate::scenario_model::{ScDeployStep, TxExpect, TxResponse}; @@ -9,7 +10,7 @@ use super::{address_annotated, code_annotated, gas_annotated, StepWrapper, TxToS impl TxToStep for Tx>, RH> where - Env: TxEnv, + Env: TxEnvWithTxHash, From: TxFromSpecified, Payment: TxPayment, Gas: TxGas, @@ -18,9 +19,10 @@ where { type Step = ScDeployStep; - fn tx_to_step(self) -> StepWrapper { + fn tx_to_step(mut self) -> StepWrapper { let mut step = tx_to_sc_deploy_step(&self.env, self.from, self.payment, self.gas, self.data); + step.explicit_tx_hash = self.env.take_tx_hash(); step.expect = Some(self.result_handler.list_tx_expect()); StepWrapper { diff --git a/framework/snippets/src/interactor_tx/interactor_exec_env.rs b/framework/snippets/src/interactor_tx/interactor_exec_env.rs index 6473c50a91..436e65ac0c 100644 --- a/framework/snippets/src/interactor_tx/interactor_exec_env.rs +++ b/framework/snippets/src/interactor_tx/interactor_exec_env.rs @@ -1,6 +1,8 @@ use multiversx_sc_scenario::{ api::StaticApi, - multiversx_sc::types::{ManagedAddress, ManagedBuffer, Tx, TxBaseWithEnv, TxEnv}, + multiversx_sc::types::{ + ManagedAddress, ManagedBuffer, Tx, TxBaseWithEnv, TxEnv, TxEnvWithTxHash, H256, + }, scenario_model::TxExpect, ScenarioTxEnv, ScenarioTxEnvData, }; @@ -44,3 +46,13 @@ impl<'w> ScenarioTxEnv for InteractorEnvExec<'w> { &self.data } } + +impl<'w> TxEnvWithTxHash for InteractorEnvExec<'w> { + fn set_tx_hash(&mut self, tx_hash: H256) { + self.data.set_tx_hash(tx_hash); + } + + fn take_tx_hash(&mut self) -> Option { + self.data.take_tx_hash() + } +} From 09c8e3491f9a6e7fbcf0f99af77640096aa4b396 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Fri, 20 Sep 2024 17:28:16 +0300 Subject: [PATCH 10/30] fix interactor tests --- sdk/core/examples/account_storage.rs | 2 +- sdk/core/examples/get_hyper_block_by_hash.rs | 2 +- sdk/core/examples/get_hyper_block_latest.rs | 2 +- sdk/core/examples/tx_info.rs | 2 +- sdk/core/examples/vm_query.rs | 9 +++------ sdk/core/src/gateway/gateway_block.rs | 8 ++------ 6 files changed, 9 insertions(+), 16 deletions(-) diff --git a/sdk/core/examples/account_storage.rs b/sdk/core/examples/account_storage.rs index c7d5bda76e..148cc644da 100644 --- a/sdk/core/examples/account_storage.rs +++ b/sdk/core/examples/account_storage.rs @@ -6,7 +6,7 @@ use multiversx_sdk::{ #[tokio::main] async fn main() { let addr = Address::from_bech32_string( - "erd1932eft30w753xyvme8d49qejgkjc09n5e49w4mwdjtm0neld797su0dlxp", + "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th", ) .unwrap(); diff --git a/sdk/core/examples/get_hyper_block_by_hash.rs b/sdk/core/examples/get_hyper_block_by_hash.rs index a67ea311d3..d6f36e2aba 100644 --- a/sdk/core/examples/get_hyper_block_by_hash.rs +++ b/sdk/core/examples/get_hyper_block_by_hash.rs @@ -4,7 +4,7 @@ use multiversx_sdk::gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_ async fn main() { let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let result = blockchain - .get_hyper_block_by_hash("20b14ba0e68c465810c5ded091f220e51dad41629d7ccd87dab572206185e419") + .get_hyper_block_by_hash("d59e0dc7d407b1175655357cb8056ec3bb77961192753cddda2fb700c6ce71c6") .await; println!("block by hash result: {result:?}"); diff --git a/sdk/core/examples/get_hyper_block_latest.rs b/sdk/core/examples/get_hyper_block_latest.rs index fe49fa7f1e..91ddd9e9c7 100644 --- a/sdk/core/examples/get_hyper_block_latest.rs +++ b/sdk/core/examples/get_hyper_block_latest.rs @@ -3,7 +3,7 @@ use multiversx_sdk::gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_ #[tokio::main] async fn main() { let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); - let result = blockchain.get_latest_hyper_block_nonce(false).await; + let result = blockchain.get_latest_hyper_block_nonce().await; println!("latest block result: {result:?}") } diff --git a/sdk/core/examples/tx_info.rs b/sdk/core/examples/tx_info.rs index 11481d2ec1..d2c4cc93ef 100644 --- a/sdk/core/examples/tx_info.rs +++ b/sdk/core/examples/tx_info.rs @@ -2,7 +2,7 @@ use multiversx_sdk::gateway::{GatewayProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_ #[tokio::main] async fn main() { - let tx_hash = "49edb289892a655a0e988b360c19326c21107f9696c6197b435667c6e8c6e1a3"; + let tx_hash = "fd21782ddb9e2217a3239e849e39d1d2c8fa74142a73f2dda3adb3028c0514e9"; let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let status = blockchain.get_transaction_status(tx_hash).await; diff --git a/sdk/core/examples/vm_query.rs b/sdk/core/examples/vm_query.rs index 750316fee6..51e37f6f68 100644 --- a/sdk/core/examples/vm_query.rs +++ b/sdk/core/examples/vm_query.rs @@ -6,18 +6,15 @@ use multiversx_sdk::{ #[tokio::main] async fn main() { - let wl = Wallet::from_private_key( - "1648ad209d6b157a289884933e3bb30f161ec7113221ec16f87c3578b05830b0", - ) - .unwrap(); + let wl = Wallet::from_pem_file("sdk/core/tests/alice.pem").unwrap(); let addr = wl.address(); let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR); let req = VmValueRequest { sc_address: Address::from_bech32_string( - "erd1qqqqqqqqqqqqqpgqhn3ae8dpc957t7jadn7kywtg503dy7pnj9ts3umqxx", + "erd1qqqqqqqqqqqqqpgq5dvvkmka7sujfsx7cfmygnx0n7luv8k0d8sskpqcec", ) .unwrap(), - func_name: "get".to_string(), + func_name: "empty".to_string(), args: vec![], caller: addr.clone(), value: "0".to_string(), diff --git a/sdk/core/src/gateway/gateway_block.rs b/sdk/core/src/gateway/gateway_block.rs index b6daf57abb..5e1af681ee 100644 --- a/sdk/core/src/gateway/gateway_block.rs +++ b/sdk/core/src/gateway/gateway_block.rs @@ -41,12 +41,8 @@ impl GatewayProxy { } // get_latest_hyper_block_nonce retrieves the latest hyper block (metachain) nonce from the network - pub async fn get_latest_hyper_block_nonce(&self, with_metachain: bool) -> Result { - let mut endpoint = GET_NETWORK_STATUS_ENDPOINT.to_string(); - - if with_metachain { - endpoint = format!("{GET_NETWORK_STATUS_ENDPOINT}/{METACHAIN_SHARD_ID}"); - } + pub async fn get_latest_hyper_block_nonce(&self) -> Result { + let endpoint = format!("{GET_NETWORK_STATUS_ENDPOINT}/{METACHAIN_SHARD_ID}"); let endpoint = self.get_endpoint(endpoint.as_str()); From c69e89adcb200946475b5ed51a7527cdd551f358 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Fri, 20 Sep 2024 17:35:45 +0300 Subject: [PATCH 11/30] chain sim - fix clippy --- .../examples/adder/interact/src/basic_interact_config.rs | 4 ++-- .../multisig/interact/src/multisig_interact_config.rs | 4 ++-- .../basic-features/interact/src/bf_interact_config.rs | 4 ++-- .../composability/interact/src/comp_interact_config.rs | 4 ++-- .../src/system_sc_interact_config.rs | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/contracts/examples/adder/interact/src/basic_interact_config.rs b/contracts/examples/adder/interact/src/basic_interact_config.rs index 2ab2577ceb..a49cc6ff7a 100644 --- a/contracts/examples/adder/interact/src/basic_interact_config.rs +++ b/contracts/examples/adder/interact/src/basic_interact_config.rs @@ -28,8 +28,8 @@ impl Config { // Returns if chain type is chain simulator pub fn use_chain_simulator(&self) -> bool { match self.chain_type.as_str() { - "simulator" => return true, - "real" => return false, + "simulator" => true, + "real" => false, _ => panic!( "Invalid chain type: {}. Expected 'simulator' or 'real'.", self.chain_type diff --git a/contracts/examples/multisig/interact/src/multisig_interact_config.rs b/contracts/examples/multisig/interact/src/multisig_interact_config.rs index ec280f4807..dd73c46092 100644 --- a/contracts/examples/multisig/interact/src/multisig_interact_config.rs +++ b/contracts/examples/multisig/interact/src/multisig_interact_config.rs @@ -31,8 +31,8 @@ impl Config { // Returns if chain type is chain simulator pub fn use_chain_simulator(&self) -> bool { match self.chain_type.as_str() { - "simulator" => return true, - "real" => return false, + "simulator" => true, + "real" => false, _ => panic!( "Invalid chain type: {}. Expected 'simulator' or 'real'.", self.chain_type diff --git a/contracts/feature-tests/basic-features/interact/src/bf_interact_config.rs b/contracts/feature-tests/basic-features/interact/src/bf_interact_config.rs index 9c6cb6ef0f..0257511464 100644 --- a/contracts/feature-tests/basic-features/interact/src/bf_interact_config.rs +++ b/contracts/feature-tests/basic-features/interact/src/bf_interact_config.rs @@ -28,8 +28,8 @@ impl Config { // Returns if true if chain type is chain simulator pub fn use_chain_simulator(&self) -> bool { match self.chain_type.as_str() { - "simulator" => return true, - "real" => return false, + "simulator" => true, + "real" => false, _ => panic!( "Invalid chain type: {}. Expected 'simulator' or 'real'.", self.chain_type diff --git a/contracts/feature-tests/composability/interact/src/comp_interact_config.rs b/contracts/feature-tests/composability/interact/src/comp_interact_config.rs index 96524d1bde..3a261b3f55 100644 --- a/contracts/feature-tests/composability/interact/src/comp_interact_config.rs +++ b/contracts/feature-tests/composability/interact/src/comp_interact_config.rs @@ -36,8 +36,8 @@ impl Config { // Returns if chain type is chain simulator pub fn use_chain_simulator(&self) -> bool { match self.chain_type.as_str() { - "simulator" => return true, - "real" => return false, + "simulator" => true, + "real" => false, _ => panic!( "Invalid chain type: {}. Expected 'simulator' or 'real'.", self.chain_type diff --git a/tools/interactor-system-func-calls/src/system_sc_interact_config.rs b/tools/interactor-system-func-calls/src/system_sc_interact_config.rs index 5058d06268..4fff392c11 100644 --- a/tools/interactor-system-func-calls/src/system_sc_interact_config.rs +++ b/tools/interactor-system-func-calls/src/system_sc_interact_config.rs @@ -28,8 +28,8 @@ impl Config { // Returns if chain type is chain simulator pub fn use_chain_simulator(&self) -> bool { match self.chain_type.as_str() { - "simulator" => return true, - "real" => return false, + "simulator" => true, + "real" => false, _ => panic!( "Invalid chain type: {}. Expected 'simulator' or 'real'.", self.chain_type From 265a9fa6c92805b5190ae56f7c89c212e3283463 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Fri, 20 Sep 2024 23:14:11 +0300 Subject: [PATCH 12/30] wasm-extractor - fix report parameters --- contracts/examples/empty/src/empty.rs | 4 +- framework/meta-lib/src/code_report_json.rs | 2 +- .../meta-lib/src/tools/report_creator.rs | 24 +++++++++-- .../meta-lib/src/tools/wasm_extractor.rs | 40 ++++++++++++------- 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/contracts/examples/empty/src/empty.rs b/contracts/examples/empty/src/empty.rs index 81353a08d9..e7495881d3 100644 --- a/contracts/examples/empty/src/empty.rs +++ b/contracts/examples/empty/src/empty.rs @@ -7,7 +7,9 @@ use multiversx_sc::imports::*; #[multiversx_sc::contract] pub trait EmptyContract { #[init] - fn init(&self) {} + fn init(&self) -> u32 { + 64 + } #[upgrade] fn upgrade(&self) {} diff --git a/framework/meta-lib/src/code_report_json.rs b/framework/meta-lib/src/code_report_json.rs index a0b997bb6e..b1ec390436 100644 --- a/framework/meta-lib/src/code_report_json.rs +++ b/framework/meta-lib/src/code_report_json.rs @@ -24,7 +24,7 @@ impl CodeReportJson { path: report.path.clone(), size, has_allocator: report.has_allocator, - has_panic: report.has_panic.clone(), + has_panic: report.has_panic.to_string(), } } } diff --git a/framework/meta-lib/src/tools/report_creator.rs b/framework/meta-lib/src/tools/report_creator.rs index 37af10fa4e..93cc44e14c 100644 --- a/framework/meta-lib/src/tools/report_creator.rs +++ b/framework/meta-lib/src/tools/report_creator.rs @@ -1,10 +1,28 @@ -pub const WITH_MESSAGE: &str = "with message"; -pub const WITHOUT_MESSAGE: &str = "without message"; +use std::fmt::Display; + +#[derive(Default)] +pub enum PanicMessage { + #[default] + None, + WithoutMessage, + WithMessage, +} + +impl Display for PanicMessage { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let panic_status = match self { + PanicMessage::None => "None", + PanicMessage::WithoutMessage => "without message", + PanicMessage::WithMessage => "with message", + }; + write!(f, "{}", panic_status) + } +} pub struct ReportCreator { pub path: String, pub has_allocator: bool, - pub has_panic: String, + pub has_panic: PanicMessage, } impl ReportCreator {} diff --git a/framework/meta-lib/src/tools/wasm_extractor.rs b/framework/meta-lib/src/tools/wasm_extractor.rs index 50d5b78aff..e2e7d8ddd6 100644 --- a/framework/meta-lib/src/tools/wasm_extractor.rs +++ b/framework/meta-lib/src/tools/wasm_extractor.rs @@ -1,17 +1,17 @@ use colored::Colorize; use std::fs; use wasmparser::{ - BinaryReaderError, DataSectionReader, FunctionBody, ImportSectionReader, Parser, Payload, + BinaryReaderError, DataSectionReader, FunctionBody, ImportSectionReader, Operator, Parser, + Payload, }; use crate::ei::EIVersion; -use super::report_creator::{ReportCreator, WITHOUT_MESSAGE, WITH_MESSAGE}; +use super::report_creator::{PanicMessage, ReportCreator}; const PANIC_WITH_MESSAGE: &[u8; 16] = b"panic occurred: "; const PANIC_WITHOUT_MESSAGE: &[u8; 14] = b"panic occurred"; const ERROR_FAIL_ALLOCATOR: &[u8; 27] = b"memory allocation forbidden"; -const MEMORY_GROW_OPCODE: u8 = 0x40; pub struct WasmInfo { pub imports: Vec, @@ -49,25 +49,35 @@ fn populate_wasm_info( let mut allocator_trigger = false; let mut ei_check = false; let mut memory_grow_flag = false; - let mut has_panic = "none"; + let mut has_panic: PanicMessage = PanicMessage::default(); let parser = Parser::new(0); for payload in parser.parse_all(&wasm_data) { match payload? { Payload::ImportSection(import_section) => { imports = extract_imports(import_section, extract_imports_enabled); - ei_check = is_ei_valid(imports.clone(), check_ei); + ei_check |= is_ei_valid(imports.clone(), check_ei); }, Payload::DataSection(data_section) => { - allocator_trigger = is_fail_allocator_triggered(data_section.clone()); - if is_panic_with_message_triggered(data_section.clone()) { - has_panic = WITH_MESSAGE; - } else if is_panic_without_message_triggered(data_section) { - has_panic = WITHOUT_MESSAGE; + allocator_trigger |= is_fail_allocator_triggered(data_section.clone()); + match has_panic { + PanicMessage::None => { + if is_panic_with_message_triggered(data_section.clone()) { + has_panic = PanicMessage::WithMessage; + } else if is_panic_without_message_triggered(data_section) { + has_panic = PanicMessage::WithoutMessage; + } + }, + PanicMessage::WithoutMessage => { + if is_panic_with_message_triggered(data_section.clone()) { + has_panic = PanicMessage::WithMessage; + } + }, + PanicMessage::WithMessage => continue, } }, Payload::CodeSectionEntry(code_section) => { - memory_grow_flag = is_mem_grow(code_section); + memory_grow_flag |= is_mem_grow(code_section); }, _ => (), } @@ -76,7 +86,7 @@ fn populate_wasm_info( let report = ReportCreator { path, has_allocator: allocator_trigger, - has_panic: has_panic.to_string(), + has_panic, }; Ok(WasmInfo { @@ -173,9 +183,9 @@ fn is_ei_valid(imports: Vec, check_ei: &Option) -> bool { } fn is_mem_grow(code_section: FunctionBody) -> bool { - let mut code = code_section.get_binary_reader(); - while code.bytes_remaining() > 0 { - if code.read_u8().unwrap() == MEMORY_GROW_OPCODE { + for op_reader in code_section.get_operators_reader().iter_mut() { + let op = op_reader.read().unwrap(); + if let Operator::MemoryGrow { mem: _ } = op { return true; } } From c096aae07b8bae2e4477bf5ca592df3b64acf4bc Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Sat, 21 Sep 2024 23:54:44 +0300 Subject: [PATCH 13/30] wasm-extractor - fix mem_grow, create unitests --- Cargo.lock | 44 +++ framework/meta-lib/Cargo.toml | 1 + framework/meta-lib/src/tools.rs | 1 + .../meta-lib/src/tools/wasm_extractor.rs | 10 +- .../meta-lib/src/tools/wasm_extractor_test.rs | 297 ++++++++++++++++++ 5 files changed, 350 insertions(+), 3 deletions(-) create mode 100644 framework/meta-lib/src/tools/wasm_extractor_test.rs diff --git a/Cargo.lock b/Cargo.lock index b865897548..21b721ed78 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -1718,6 +1718,12 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +[[package]] +name = "leb128" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" + [[package]] name = "libc" version = "0.2.158" @@ -2090,6 +2096,7 @@ dependencies = [ "toml", "wasmparser", "wasmprinter", + "wat", ] [[package]] @@ -3680,6 +3687,12 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + [[package]] name = "untrusted" version = "0.9.0" @@ -3873,6 +3886,15 @@ version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" +[[package]] +name = "wasm-encoder" +version = "0.217.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b88b0814c9a2b323a9b46c687e726996c255ac8b64aa237dd11c81ed4854760" +dependencies = [ + "leb128", +] + [[package]] name = "wasmparser" version = "0.216.0" @@ -3898,6 +3920,28 @@ dependencies = [ "wasmparser", ] +[[package]] +name = "wast" +version = "217.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79004ecebded92d3c710d4841383368c7f04b63d0992ddd6b0c7d5029b7629b7" +dependencies = [ + "bumpalo", + "leb128", + "memchr", + "unicode-width", + "wasm-encoder", +] + +[[package]] +name = "wat" +version = "1.217.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c126271c3d92ca0f7c63e4e462e40c69cca52fd4245fcda730d1cf558fb55088" +dependencies = [ + "wast", +] + [[package]] name = "web-sys" version = "0.3.70" diff --git a/framework/meta-lib/Cargo.toml b/framework/meta-lib/Cargo.toml index 8e9209fa9a..37d726fc0b 100644 --- a/framework/meta-lib/Cargo.toml +++ b/framework/meta-lib/Cargo.toml @@ -29,6 +29,7 @@ hex = "0.4" wasmparser = "0.216" wasmprinter = "0.216" semver = "1.0.20" +wat = "1.217.0" [dependencies.multiversx-sc] version = "=0.53.0" diff --git a/framework/meta-lib/src/tools.rs b/framework/meta-lib/src/tools.rs index e0a8962d9b..26fe22aaed 100644 --- a/framework/meta-lib/src/tools.rs +++ b/framework/meta-lib/src/tools.rs @@ -3,6 +3,7 @@ mod git_describe; pub(crate) mod report_creator; pub mod twiggy; mod wasm_extractor; +mod wasm_extractor_test; mod wasm_opt; mod wasm_to_wat; diff --git a/framework/meta-lib/src/tools/wasm_extractor.rs b/framework/meta-lib/src/tools/wasm_extractor.rs index e2e7d8ddd6..20173c88c3 100644 --- a/framework/meta-lib/src/tools/wasm_extractor.rs +++ b/framework/meta-lib/src/tools/wasm_extractor.rs @@ -39,7 +39,7 @@ impl WasmInfo { } } -fn populate_wasm_info( +pub(crate) fn populate_wasm_info( path: String, wasm_data: Vec, extract_imports_enabled: bool, @@ -183,11 +183,15 @@ fn is_ei_valid(imports: Vec, check_ei: &Option) -> bool { } fn is_mem_grow(code_section: FunctionBody) -> bool { - for op_reader in code_section.get_operators_reader().iter_mut() { - let op = op_reader.read().unwrap(); + let mut instructions_reader = code_section + .get_operators_reader() + .expect("Failed to get operators reader"); + + while let Ok(op) = instructions_reader.read() { if let Operator::MemoryGrow { mem: _ } = op { return true; } } + false } diff --git a/framework/meta-lib/src/tools/wasm_extractor_test.rs b/framework/meta-lib/src/tools/wasm_extractor_test.rs new file mode 100644 index 0000000000..e99e94f179 --- /dev/null +++ b/framework/meta-lib/src/tools/wasm_extractor_test.rs @@ -0,0 +1,297 @@ +#[cfg(test)] +pub mod tests { + use wat::Parser; + + use crate::tools::{report_creator::PanicMessage, wasm_extractor::populate_wasm_info}; + + const EMPTY_WITH_FAIL_ALLOCATOR: &str = r#" +(module $empty_wasm.wasm + (type (;0;) (func (result i32))) + (type (;1;) (func (param i32 i32))) + (type (;2;) (func)) + (import "env" "getNumArguments" (func $getNumArguments (;0;) (type 0))) + (import "env" "signalError" (func $signalError (;1;) (type 1))) + (import "env" "checkNoPayment" (func $checkNoPayment (;2;) (type 2))) + (func $_ZN13multiversx_sc2io16arg_nested_tuple22check_num_arguments_eq17hd731b4a37a0ab09aE (;3;) (type 2) + block ;; label = @1 + call $getNumArguments + br_if 0 (;@1;) + return + end + i32.const 131072 + i32.const 25 + call $signalError + unreachable + ) + (func $__rust_alloc (;4;) (type 2) + call $_ZN122_$LT$multiversx_sc_wasm_adapter..wasm_alloc..fail_allocator..FailAllocator$u20$as$u20$core..alloc..global..GlobalAlloc$GT$5alloc17hf044a79dd04bdcb1E + unreachable + ) + (func $_ZN122_$LT$multiversx_sc_wasm_adapter..wasm_alloc..fail_allocator..FailAllocator$u20$as$u20$core..alloc..global..GlobalAlloc$GT$5alloc17hf044a79dd04bdcb1E (;5;) (type 2) + call $_ZN26multiversx_sc_wasm_adapter10wasm_alloc14fail_allocator29signal_allocation_not_allowed17hb15b243b5f851b48E + unreachable + ) + (func $init (;6;) (type 2) + call $checkNoPayment + call $_ZN13multiversx_sc2io16arg_nested_tuple22check_num_arguments_eq17hd731b4a37a0ab09aE + i32.const 0 + i32.load8_u offset=131124 + drop + call $__rust_alloc + unreachable + ) + (func $upgrade (;7;) (type 2) + call $checkNoPayment + call $_ZN13multiversx_sc2io16arg_nested_tuple22check_num_arguments_eq17hd731b4a37a0ab09aE + ) + (func $callBack (;8;) (type 2)) + (func $_ZN26multiversx_sc_wasm_adapter10wasm_alloc14fail_allocator29signal_allocation_not_allowed17hb15b243b5f851b48E (;9;) (type 2) + i32.const 131097 + i32.const 27 + call $signalError + unreachable + ) + (memory (;0;) 3) + (global $__stack_pointer (;0;) (mut i32) i32.const 131072) + (global (;1;) i32 i32.const 131125) + (global (;2;) i32 i32.const 131136) + (export "memory" (memory 0)) + (export "init" (func $init)) + (export "upgrade" (func $upgrade)) + (export "callBack" (func $callBack)) + (export "__data_end" (global 1)) + (export "__heap_base" (global 2)) + (data $.rodata (;0;) (i32.const 131072) "wrong number of argumentsmemory allocation forbidden") +) +"#; + + const EMPTY_WITH_MEM_GROW: &str = r#" +(module $empty_wasm.wasm + (type (;0;) (func (result i32))) + (type (;1;) (func (param i32 i32))) + (type (;2;) (func)) + (type (;3;) (func (param i64))) + (import "env" "getNumArguments" (func $getNumArguments (;0;) (type 0))) + (import "env" "signalError" (func $signalError (;1;) (type 1))) + (import "env" "checkNoPayment" (func $checkNoPayment (;2;) (type 2))) + (import "env" "smallIntFinishUnsigned" (func $smallIntFinishUnsigned (;3;) (type 3))) + (func $_ZN13multiversx_sc2io16arg_nested_tuple22check_num_arguments_eq17hd731b4a37a0ab09aE (;4;) (type 2) + block ;; label = @1 + call $getNumArguments + br_if 0 (;@1;) + return + end + i32.const 131072 + i32.const 25 + call $signalError + unreachable + ) + (func $rust_begin_unwind (;5;) (type 2) + call $_ZN26multiversx_sc_wasm_adapter5panic9panic_fmt17he68b14ffa9b6b21eE + unreachable + ) + (func $_ZN26multiversx_sc_wasm_adapter5panic9panic_fmt17he68b14ffa9b6b21eE (;6;) (type 2) + i32.const 131097 + i32.const 14 + call $signalError + unreachable + ) + (func $init (;7;) (type 2) + (local i32 i32) + call $checkNoPayment + call $_ZN13multiversx_sc2io16arg_nested_tuple22check_num_arguments_eq17hd731b4a37a0ab09aE + i32.const 0 + i32.load8_u offset=131120 + drop + block ;; label = @1 + i32.const 0 + i32.load offset=131112 + local.tee 0 + i32.const 3 + i32.and + i32.eqz + br_if 0 (;@1;) + i32.const 0 + local.get 0 + i32.const -4 + i32.and + i32.const 4 + i32.add + local.tee 0 + i32.store offset=131112 + end + block ;; label = @1 + local.get 0 + i32.const 4 + i32.add + local.tee 1 + i32.const 0 + i32.load offset=131116 + i32.le_u + br_if 0 (;@1;) + i32.const 1 + memory.grow + local.set 0 + i32.const 0 + i32.load offset=131116 + local.set 1 + i32.const 0 + local.get 0 + i32.const 16 + i32.shl + local.tee 0 + i32.const 65536 + i32.add + i32.store offset=131116 + i32.const 0 + i32.load offset=131112 + local.get 0 + local.get 0 + local.get 1 + i32.eq + select + local.tee 0 + i32.const 4 + i32.add + local.set 1 + end + i32.const 0 + local.get 1 + i32.store offset=131112 + block ;; label = @1 + local.get 0 + i32.eqz + br_if 0 (;@1;) + local.get 0 + i32.const 42 + i32.store + i64.const 1 + call $smallIntFinishUnsigned + return + end + call $_ZN5alloc5alloc18handle_alloc_error17he71533634a7a5ac5E + unreachable + ) + (func $_ZN5alloc5alloc18handle_alloc_error17he71533634a7a5ac5E (;8;) (type 2) + call $__rust_alloc_error_handler + unreachable + ) + (func $upgrade (;9;) (type 2) + call $checkNoPayment + call $_ZN13multiversx_sc2io16arg_nested_tuple22check_num_arguments_eq17hd731b4a37a0ab09aE + ) + (func $callBack (;10;) (type 2)) + (func $__rust_alloc_error_handler (;11;) (type 2) + call $__rdl_oom + unreachable + ) + (func $__rdl_oom (;12;) (type 2) + call $_ZN4core9panicking18panic_nounwind_fmt17h21a92179d680342aE + unreachable + ) + (func $_ZN4core9panicking18panic_nounwind_fmt17h21a92179d680342aE (;13;) (type 2) + call $rust_begin_unwind + unreachable + ) + (memory (;0;) 3) + (global $__stack_pointer (;0;) (mut i32) i32.const 131072) + (global (;1;) i32 i32.const 131121) + (global (;2;) i32 i32.const 131136) + (export "memory" (memory 0)) + (export "init" (func $init)) + (export "upgrade" (func $upgrade)) + (export "callBack" (func $callBack)) + (export "__data_end" (global 1)) + (export "__heap_base" (global 2)) + (data $.rodata (;0;) (i32.const 131072) "wrong number of argumentspanic occurred") +) +"#; + + const EMPTY_DBG_WAT: &str = r#" +(module $empty_wasm.wasm + (type (;0;) (func (result i32))) + (type (;1;) (func (param i32 i32))) + (type (;2;) (func)) + (type (;3;) (func (param i64))) + (import "env" "getNumArguments" (func $getNumArguments (;0;) (type 0))) + (import "env" "signalError" (func $signalError (;1;) (type 1))) + (import "env" "checkNoPayment" (func $checkNoPayment (;2;) (type 2))) + (import "env" "smallIntFinishUnsigned" (func $smallIntFinishUnsigned (;3;) (type 3))) + (func $_ZN13multiversx_sc2io16arg_nested_tuple22check_num_arguments_eq17hd731b4a37a0ab09aE (;4;) (type 2) + block ;; label = @1 + call $getNumArguments + br_if 0 (;@1;) + return + end + i32.const 131072 + i32.const 25 + call $signalError + unreachable + ) + (func $init (;5;) (type 2) + call $checkNoPayment + call $_ZN13multiversx_sc2io16arg_nested_tuple22check_num_arguments_eq17hd731b4a37a0ab09aE + i64.const 64 + call $smallIntFinishUnsigned + ) + (func $upgrade (;6;) (type 2) + call $checkNoPayment + call $_ZN13multiversx_sc2io16arg_nested_tuple22check_num_arguments_eq17hd731b4a37a0ab09aE + ) + (func $callBack (;7;) (type 2)) + (memory (;0;) 3) + (global $__stack_pointer (;0;) (mut i32) i32.const 131072) + (global (;1;) i32 i32.const 131097) + (global (;2;) i32 i32.const 131104) + (export "memory" (memory 0)) + (export "init" (func $init)) + (export "upgrade" (func $upgrade)) + (export "callBack" (func $callBack)) + (export "__data_end" (global 1)) + (export "__heap_base" (global 2)) + (data $.rodata (;0;) (i32.const 131072) "wrong number of arguments") +) +"#; + + #[test] + fn test_empty() { + if let Ok(content) = Parser::new().parse_bytes(None, EMPTY_DBG_WAT.as_bytes()) { + let wasm_info = populate_wasm_info(String::new(), content.to_vec(), false, &None) + .expect("Unable to parse WASM content."); + assert!(!wasm_info.memory_grow_flag); + assert!(!wasm_info.report.has_allocator); + assert_eq!( + PanicMessage::None.to_string(), + wasm_info.report.has_panic.to_string() + ); + } + } + + #[test] + fn test_empty_with_mem_grow() { + if let Ok(content) = Parser::new().parse_bytes(None, EMPTY_WITH_MEM_GROW.as_bytes()) { + let wasm_info = populate_wasm_info(String::new(), content.to_vec(), false, &None) + .expect("Unable to parse WASM content."); + assert!(wasm_info.memory_grow_flag); + assert!(!wasm_info.report.has_allocator); + assert_eq!( + PanicMessage::WithoutMessage.to_string(), + wasm_info.report.has_panic.to_string() + ); + } + } + + #[test] + fn test_empty_with_fail_allocator() { + if let Ok(content) = Parser::new().parse_bytes(None, EMPTY_WITH_FAIL_ALLOCATOR.as_bytes()) { + let wasm_info = populate_wasm_info(String::new(), content.to_vec(), false, &None) + .expect("Unable to parse WASM content."); + assert!(!wasm_info.memory_grow_flag); + assert!(wasm_info.report.has_allocator); + assert_eq!( + PanicMessage::None.to_string(), + wasm_info.report.has_panic.to_string() + ); + } + } +} From 0dea313dcfabd8b1883c008da68d4c3c610a36b2 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Sun, 22 Sep 2024 00:00:51 +0300 Subject: [PATCH 14/30] wasm-extractor - clean empty contract --- contracts/examples/empty/src/empty.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/contracts/examples/empty/src/empty.rs b/contracts/examples/empty/src/empty.rs index e7495881d3..81353a08d9 100644 --- a/contracts/examples/empty/src/empty.rs +++ b/contracts/examples/empty/src/empty.rs @@ -7,9 +7,7 @@ use multiversx_sc::imports::*; #[multiversx_sc::contract] pub trait EmptyContract { #[init] - fn init(&self) -> u32 { - 64 - } + fn init(&self) {} #[upgrade] fn upgrade(&self) {} From 4006993bacb1da1927ea7e7c6dffe5c8c7d90aee Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 25 Sep 2024 00:44:59 +0300 Subject: [PATCH 15/30] vm-core crate: Address, H256, BoxedBytes, flags --- Cargo.lock | 208 ++++++-------- Cargo.toml | 1 + framework/base/Cargo.toml | 4 + framework/base/src/abi.rs | 1 + .../base/src/abi/type_abi_impl_vm_core.rs | 204 ++++++++++++++ framework/base/src/formatter.rs | 1 + .../src/formatter/formatter_impl_vm_core.rs | 25 ++ framework/base/src/lib.rs | 3 + framework/base/src/types.rs | 7 +- .../base/src/types/flags/esdt_local_role.rs | 159 ----------- .../base/src/types/{heap/mod.rs => heap.rs} | 8 +- .../types/managed/wrapped/managed_vec_item.rs | 44 +++ publish.sh | 18 +- vm-core/Cargo.toml | 28 ++ vm-core/README.md | 16 ++ vm-core/src/lib.rs | 5 + vm-core/src/types.rs | 13 + .../src/types/address.rs | 66 ++--- .../heap => vm-core/src/types}/boxed_bytes.rs | 22 +- .../mod.rs => vm-core/src/types/flags.rs | 2 + .../src/types/flags/code_metadata.rs | 134 ++++----- .../src/types/flags/esdt_local_role.rs | 15 +- .../src/types/flags/esdt_local_role_flags.rs | 0 .../src/types/flags/esdt_token_type.rs | 9 +- .../src/types/flags/token_type.rs | 12 +- vm-core/src/types/h256.rs | 223 +++++++++++++++ vm-core/src/types/heap_address.rs | 257 ++++++++++++++++++ .../h256.rs => vm-core/src/types/heap_h256.rs | 94 +++---- vm/Cargo.toml | 4 + vm/src/lib.rs | 3 + vm/src/types.rs | 22 +- vm/src/types/vm_address.rs | 153 ----------- vm/src/types/vm_code_metadata.rs | 130 --------- vm/src/types/vm_esdt_local_role_flags.rs | 73 ----- vm/src/types/vm_h256.rs | 108 -------- 35 files changed, 1099 insertions(+), 973 deletions(-) create mode 100644 framework/base/src/abi/type_abi_impl_vm_core.rs create mode 100644 framework/base/src/formatter/formatter_impl_vm_core.rs delete mode 100644 framework/base/src/types/flags/esdt_local_role.rs rename framework/base/src/types/{heap/mod.rs => heap.rs} (63%) create mode 100644 vm-core/Cargo.toml create mode 100644 vm-core/README.md create mode 100644 vm-core/src/lib.rs create mode 100644 vm-core/src/types.rs rename framework/base/src/types/heap/h256_address.rs => vm-core/src/types/address.rs (80%) rename {framework/base/src/types/heap => vm-core/src/types}/boxed_bytes.rs (96%) rename framework/base/src/types/flags/mod.rs => vm-core/src/types/flags.rs (84%) rename {framework/base => vm-core}/src/types/flags/code_metadata.rs (69%) rename vm/src/types/vm_esdt_local_role.rs => vm-core/src/types/flags/esdt_local_role.rs (92%) rename {framework/base => vm-core}/src/types/flags/esdt_local_role_flags.rs (100%) rename {framework/base => vm-core}/src/types/flags/esdt_token_type.rs (89%) rename vm/src/types/vm_token_type.rs => vm-core/src/types/flags/token_type.rs (51%) create mode 100644 vm-core/src/types/h256.rs create mode 100644 vm-core/src/types/heap_address.rs rename framework/base/src/types/heap/h256.rs => vm-core/src/types/heap_h256.rs (78%) delete mode 100644 vm/src/types/vm_address.rs delete mode 100644 vm/src/types/vm_code_metadata.rs delete mode 100644 vm/src/types/vm_esdt_local_role_flags.rs delete mode 100644 vm/src/types/vm_h256.rs diff --git a/Cargo.lock b/Cargo.lock index b865897548..83da1dcf65 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,19 +47,13 @@ dependencies = [ [[package]] name = "addr2line" -version = "0.22.0" +version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" +checksum = "f5fb1d8e4442bd405fdfd1dacb42792696b0cf9cb15882e5d097b742a676d375" dependencies = [ "gimli", ] -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - [[package]] name = "adler2" version = "2.0.0" @@ -166,9 +160,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.86" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" +checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" [[package]] name = "arbitrary" @@ -199,17 +193,17 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "backtrace" -version = "0.3.73" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" dependencies = [ "addr2line", - "cc", "cfg-if", "libc", - "miniz_oxide 0.7.4", + "miniz_oxide", "object", "rustc-demangle", + "windows-targets 0.52.6", ] [[package]] @@ -386,15 +380,15 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.7.1" +version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" +checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" [[package]] name = "cc" -version = "1.1.15" +version = "1.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6" +checksum = "07b1695e2c7e8fc85310cde85aeaab7e3097f593c91d209d3f9df76c928100f0" dependencies = [ "shlex", ] @@ -452,9 +446,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.16" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed6719fffa43d0d87e5fd8caeab59be1554fb028cd30edc88fc4369b17971019" +checksum = "b0956a43b323ac1afaffc053ed5c4b7c1f1800bacd1683c353aabbb752515dd3" dependencies = [ "clap_builder", "clap_derive", @@ -462,9 +456,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.15" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "216aec2b177652e3846684cbfe25c9964d18ec45234f0f5da5157b207ed1aab6" +checksum = "4d72166dd41634086d5803a47eb71ae740e61d84709c36f3c34110173db3961b" dependencies = [ "anstream", "anstyle", @@ -474,9 +468,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.13" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" dependencies = [ "heck", "proc-macro2", @@ -572,9 +566,9 @@ checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "cpufeatures" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51e852e6dc9a5bed1fae92dd2375037bf2b768725bf3be87811edee3249d09ad" +checksum = "608697df725056feaccfa42cffdaeeec3fccc4ffc38358ecd19b243e716a78e0" dependencies = [ "libc", ] @@ -1087,7 +1081,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "324a1be68054ef05ad64b861cc9eaf1d623d2d8cb25b4bf2cb9cdd902b4bf253" dependencies = [ "crc32fast", - "miniz_oxide 0.8.0", + "miniz_oxide", ] [[package]] @@ -1333,15 +1327,15 @@ dependencies = [ [[package]] name = "gimli" -version = "0.29.0" +version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" +checksum = "32085ea23f3234fc7846555e85283ba4de91e21016dc0455a16286d87a292d64" [[package]] name = "globset" -version = "0.4.14" +version = "0.4.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57da3b9b5b85bd66f31093f8c408b90a74431672542466497dcbdfdc02034be1" +checksum = "15f1ce686646e7f1e19bf7d5533fe443a45dbfb990e00629110797578b42fb19" dependencies = [ "aho-corasick", "bstr", @@ -1480,9 +1474,9 @@ dependencies = [ [[package]] name = "hyper-rustls" -version = "0.27.2" +version = "0.27.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155" +checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333" dependencies = [ "futures-util", "http", @@ -1513,9 +1507,9 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.7" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cde7055719c54e36e95e8719f95883f22072a48ede39db7fc17a4e1d5281e9b9" +checksum = "41296eb09f183ac68eec06e03cdbea2e759633d4067b2f6552fc2e009bcad08b" dependencies = [ "bytes", "futures-channel", @@ -1526,7 +1520,6 @@ dependencies = [ "pin-project-lite", "socket2", "tokio", - "tower", "tower-service", "tracing", ] @@ -1543,9 +1536,9 @@ dependencies = [ [[package]] name = "ignore" -version = "0.4.22" +version = "0.4.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b46810df39e66e925525d6e38ce1e7f6e1d208f72dc39757880fcb66e2c58af1" +checksum = "6d89fd380afde86567dfba715db065673989d6253f42b88179abd3eae47bda4b" dependencies = [ "crossbeam-deque", "globset", @@ -1590,9 +1583,9 @@ dependencies = [ [[package]] name = "ipnet" -version = "2.9.0" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" +checksum = "187674a687eed5fe42285b40c6291f9a01517d415fad1c3cbc6a9f778af7fcd4" [[package]] name = "is_terminal_polyfill" @@ -1720,9 +1713,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.158" +version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" +checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" [[package]] name = "linked-list-repeat" @@ -1864,15 +1857,6 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" -[[package]] -name = "miniz_oxide" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" -dependencies = [ - "adler", -] - [[package]] name = "miniz_oxide" version = "0.8.0" @@ -1970,6 +1954,7 @@ dependencies = [ "hex", "hex-literal", "itertools", + "multiversx-chain-vm-core", "multiversx-chain-vm-executor", "num-bigint", "num-traits", @@ -1979,6 +1964,15 @@ dependencies = [ "sha3", ] +[[package]] +name = "multiversx-chain-vm-core" +version = "0.10.0" +dependencies = [ + "bitflags", + "multiversx-chain-vm-executor", + "multiversx-sc-codec", +] + [[package]] name = "multiversx-chain-vm-executor" version = "0.2.0" @@ -2012,6 +2006,7 @@ version = "0.53.0" dependencies = [ "bitflags", "hex-literal", + "multiversx-chain-vm-core", "multiversx-sc-codec", "multiversx-sc-derive", "num-traits", @@ -2509,26 +2504,6 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" -[[package]] -name = "pin-project" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" -dependencies = [ - "pin-project-internal", -] - -[[package]] -name = "pin-project-internal" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "pin-project-lite" version = "0.2.14" @@ -2569,9 +2544,9 @@ dependencies = [ [[package]] name = "pkg-config" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" [[package]] name = "ppv-lite86" @@ -2757,9 +2732,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.3" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +checksum = "62871f2d65009c0256aed1b9cfeeb8ac272833c404e13d53d400cd0dad7a2ac0" dependencies = [ "bitflags", ] @@ -2939,9 +2914,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.35" +version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a85d50532239da68e9addb745ba38ff4612a242c1c7ceea689c4bc7c2f43c36f" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ "bitflags", "errno", @@ -2952,9 +2927,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.12" +version = "0.23.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c58f8c84392efc0a126acce10fa59ff7b3d2ac06ab451a33f2741989b806b044" +checksum = "f2dabaac7466917e566adb06783a81ca48944c6898a1b08b9374106dd671f4c8" dependencies = [ "once_cell", "rustls-pki-types", @@ -2981,9 +2956,9 @@ checksum = "fc0a2ce646f8655401bb81e7927b812614bd5d91dbc968696be50603510fcaf0" [[package]] name = "rustls-webpki" -version = "0.102.7" +version = "0.102.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84678086bd54edf2b415183ed7a94d0efb049f1b646a33e22a36f3794be6ae56" +checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9" dependencies = [ "ring", "rustls-pki-types", @@ -3032,11 +3007,11 @@ dependencies = [ [[package]] name = "schannel" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" +checksum = "e9aaafd5a2b6e3d657ff009d82fbd630b6bd54dd4eb06f21693925cdf80f9b8b" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -3089,9 +3064,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.11.1" +version = "2.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75da29fe9b9b08fe9d6b22b5b4bcbc75d8db3aa31e639aa56bb62e9d46bfceaf" +checksum = "ea4a292869320c0272d7bc55a5a6aafaff59b4f63404a003887b679a2e05b4b6" dependencies = [ "core-foundation-sys", "libc", @@ -3138,18 +3113,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.209" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.209" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", @@ -3158,9 +3133,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.127" +version = "1.0.128" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" dependencies = [ "indexmap", "itoa", @@ -3430,18 +3405,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.63" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.63" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" dependencies = [ "proc-macro2", "quote", @@ -3531,9 +3506,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.11" +version = "0.7.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" +checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" dependencies = [ "bytes", "futures-core", @@ -3566,9 +3541,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.20" +version = "0.22.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" +checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" dependencies = [ "indexmap", "serde", @@ -3577,27 +3552,6 @@ dependencies = [ "winnow", ] -[[package]] -name = "tower" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" -dependencies = [ - "futures-core", - "futures-util", - "pin-project", - "pin-project-lite", - "tokio", - "tower-layer", - "tower-service", -] - -[[package]] -name = "tower-layer" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" - [[package]] name = "tower-service" version = "0.3.3" @@ -3661,9 +3615,9 @@ checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" [[package]] name = "unicode-ident" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "unicode-normalization" @@ -3676,9 +3630,9 @@ dependencies = [ [[package]] name = "unicode-segmentation" -version = "1.11.0" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" [[package]] name = "untrusted" @@ -4097,9 +4051,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.6.18" +version = "0.6.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f" +checksum = "c52ac009d615e79296318c1bcce2d422aaca15ad08515e344feeda07df67a587" dependencies = [ "memchr", ] diff --git a/Cargo.toml b/Cargo.toml index 651456eebd..ed42646cba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ members = [ # "tools/plotter", "tools/interactor-system-func-calls/", + "vm-core", "vm", "contracts/modules", diff --git a/framework/base/Cargo.toml b/framework/base/Cargo.toml index 0402d16f81..8d34cbcf2b 100644 --- a/framework/base/Cargo.toml +++ b/framework/base/Cargo.toml @@ -37,3 +37,7 @@ path = "../derive" version = "=0.21.0" path = "../../data/codec" features = ["derive"] + +[dependencies.multiversx-chain-vm-core] +version = "=0.10.0" +path = "../../vm-core" diff --git a/framework/base/src/abi.rs b/framework/base/src/abi.rs index c031a62166..dd128161bc 100644 --- a/framework/base/src/abi.rs +++ b/framework/base/src/abi.rs @@ -7,6 +7,7 @@ mod type_abi; mod type_abi_from; mod type_abi_impl_basic; mod type_abi_impl_codec_multi; +mod type_abi_impl_vm_core; mod type_description; mod type_description_container; diff --git a/framework/base/src/abi/type_abi_impl_vm_core.rs b/framework/base/src/abi/type_abi_impl_vm_core.rs new file mode 100644 index 0000000000..a1024a919b --- /dev/null +++ b/framework/base/src/abi/type_abi_impl_vm_core.rs @@ -0,0 +1,204 @@ +use super::*; + +use alloc::vec::Vec; +use multiversx_chain_vm_core::types::{ + Address, BoxedBytes, CodeMetadata, EsdtLocalRole, EsdtTokenType, H256, +}; + +impl TypeAbiFrom for H256 {} + +impl TypeAbi for H256 { + type Unmanaged = Self; + + fn type_name() -> TypeName { + "H256".into() + } + + fn type_name_rust() -> TypeName { + "H256".into() + } +} + +impl TypeAbiFrom for Address {} + +impl TypeAbi for Address { + type Unmanaged = Self; + + fn type_name() -> TypeName { + "Address".into() + } + + fn type_name_rust() -> TypeName { + "Address".into() + } +} + +impl TypeAbiFrom for BoxedBytes {} + +impl TypeAbi for BoxedBytes { + type Unmanaged = Self; + + fn type_name() -> TypeName { + "bytes".into() + } + + fn type_name_rust() -> TypeName { + "BoxedBytes".into() + } +} + +impl TypeAbiFrom for CodeMetadata {} +impl TypeAbi for CodeMetadata { + type Unmanaged = Self; + + fn type_name() -> TypeName { + "CodeMetadata".into() + } + + fn type_name_rust() -> TypeName { + "CodeMetadata".into() + } +} + +impl TypeAbiFrom for EsdtTokenType {} +impl TypeAbiFrom<&Self> for EsdtTokenType {} + +// implementation originally geneated via #[type_abi] attribute +impl TypeAbi for EsdtTokenType { + type Unmanaged = Self; + fn type_name() -> TypeName { + "EsdtTokenType".into() + } + + #[allow(clippy::vec_init_then_push)] + fn provide_type_descriptions(accumulator: &mut TDC) { + let type_names = Self::type_names(); + if !accumulator.contains_type(&type_names.abi) { + accumulator.reserve_type_name(type_names.clone()); + let mut variant_descriptions = Vec::new(); + variant_descriptions.push(EnumVariantDescription::new( + &[], + "Fungible", + 0usize, + Vec::new(), + )); + variant_descriptions.push(EnumVariantDescription::new( + &[], + "NonFungible", + 1usize, + Vec::new(), + )); + variant_descriptions.push(EnumVariantDescription::new( + &[], + "SemiFungible", + 2usize, + Vec::new(), + )); + variant_descriptions.push(EnumVariantDescription::new(&[], "Meta", 3usize, Vec::new())); + variant_descriptions.push(EnumVariantDescription::new( + &[], + "Invalid", + 4usize, + Vec::new(), + )); + accumulator.insert( + type_names.clone(), + TypeDescription::new( + &[], + type_names, + TypeContents::Enum(variant_descriptions), + &[ + "TopDecode", + "TopEncode", + "NestedDecode", + "NestedEncode", + "Clone", + "PartialEq", + "Eq", + "Debug", + "ManagedVecItem", + ], + ), + ); + } + } +} + +impl TypeAbiFrom for EsdtLocalRole {} +impl TypeAbiFrom<&Self> for EsdtLocalRole {} + +// implementation originally geneated via #[type_abi] attribute +impl TypeAbi for EsdtLocalRole { + type Unmanaged = Self; + + fn type_name() -> TypeName { + "EsdtLocalRole".into() + } + + #[allow(clippy::vec_init_then_push)] + fn provide_type_descriptions(accumulator: &mut TDC) { + let type_names = Self::type_names(); + if !accumulator.contains_type(&type_names.abi) { + accumulator.reserve_type_name(type_names.clone()); + let mut variant_descriptions = Vec::new(); + variant_descriptions.push(EnumVariantDescription::new(&[], "None", 0usize, Vec::new())); + variant_descriptions.push(EnumVariantDescription::new(&[], "Mint", 1usize, Vec::new())); + variant_descriptions.push(EnumVariantDescription::new(&[], "Burn", 2usize, Vec::new())); + variant_descriptions.push(EnumVariantDescription::new( + &[], + "NftCreate", + 3usize, + Vec::new(), + )); + variant_descriptions.push(EnumVariantDescription::new( + &[], + "NftAddQuantity", + 4usize, + Vec::new(), + )); + variant_descriptions.push(EnumVariantDescription::new( + &[], + "NftBurn", + 5usize, + Vec::new(), + )); + variant_descriptions.push(EnumVariantDescription::new( + &[], + "NftAddUri", + 6usize, + Vec::new(), + )); + variant_descriptions.push(EnumVariantDescription::new( + &[], + "NftUpdateAttributes", + 7usize, + Vec::new(), + )); + variant_descriptions.push(EnumVariantDescription::new( + &[], + "Transfer", + 8usize, + Vec::new(), + )); + accumulator.insert( + type_names.clone(), + TypeDescription::new( + &[], + type_names, + TypeContents::Enum(variant_descriptions), + &[ + "TopDecode", + "TopEncode", + "NestedDecode", + "NestedEncode", + "Clone", + "PartialEq", + "Eq", + "Debug", + "Copy", + ], + ), + ); + } + } +} diff --git a/framework/base/src/formatter.rs b/framework/base/src/formatter.rs index e850ec2111..117d332382 100644 --- a/framework/base/src/formatter.rs +++ b/framework/base/src/formatter.rs @@ -1,6 +1,7 @@ mod formatter_impl_bool; mod formatter_impl_bytes; mod formatter_impl_num; +mod formatter_impl_vm_core; mod formatter_traits; pub mod hex_util; diff --git a/framework/base/src/formatter/formatter_impl_vm_core.rs b/framework/base/src/formatter/formatter_impl_vm_core.rs new file mode 100644 index 0000000000..ca92d9232d --- /dev/null +++ b/framework/base/src/formatter/formatter_impl_vm_core.rs @@ -0,0 +1,25 @@ +use multiversx_chain_vm_core::types::CodeMetadata; + +use super::{hex_util, FormatByteReceiver, SCBinary, SCDisplay, SCLowerHex}; + +impl SCDisplay for CodeMetadata { + fn fmt(&self, f: &mut F) { + self.for_each_string_token(|token| f.append_bytes(token.as_bytes())) + } +} + +impl SCLowerHex for CodeMetadata { + fn fmt(&self, f: &mut F) { + let num = self.bits().to_be_bytes(); + f.append_bytes(&hex_util::byte_to_hex_digits(num[0])[..]); + f.append_bytes(&hex_util::byte_to_hex_digits(num[1])[..]); + } +} + +impl SCBinary for CodeMetadata { + fn fmt(&self, f: &mut F) { + let num = self.bits().to_be_bytes(); + f.append_bytes(&hex_util::byte_to_binary_digits(num[0])[..]); + f.append_bytes(&hex_util::byte_to_binary_digits(num[1])[..]); + } +} diff --git a/framework/base/src/lib.rs b/framework/base/src/lib.rs index 3b8fc6c0f5..1b1ee1f70f 100644 --- a/framework/base/src/lib.rs +++ b/framework/base/src/lib.rs @@ -9,6 +9,9 @@ extern crate alloc; /// The current version of `multiversx_sc_codec`, re-exported. pub use multiversx_sc_codec as codec; +// Re-exporting the VM-core, for convenience. +pub use multiversx_chain_vm_core as vm_core; + /// Reexported for convenience. pub use crate::codec::arrayvec; diff --git a/framework/base/src/types.rs b/framework/base/src/types.rs index 23d94214ba..5f2b4eddd8 100644 --- a/framework/base/src/types.rs +++ b/framework/base/src/types.rs @@ -1,5 +1,4 @@ mod crypto; -mod flags; pub mod heap; mod interaction; mod io; @@ -8,7 +7,6 @@ pub(crate) mod math_util; mod static_buffer; pub use crypto::*; -pub use flags::*; pub use interaction::*; pub use io::*; pub use managed::*; @@ -17,3 +15,8 @@ pub use static_buffer::*; /// Only import the heap types in contracts when the "alloc" feature is on. #[cfg(feature = "alloc")] pub use heap::*; + +pub use crate::vm_core::types::CodeMetadata; +pub use crate::vm_core::types::EsdtLocalRole; +pub use crate::vm_core::types::EsdtLocalRoleFlags; +pub use crate::vm_core::types::EsdtTokenType; diff --git a/framework/base/src/types/flags/esdt_local_role.rs b/framework/base/src/types/flags/esdt_local_role.rs deleted file mode 100644 index b66ac612f5..0000000000 --- a/framework/base/src/types/flags/esdt_local_role.rs +++ /dev/null @@ -1,159 +0,0 @@ -use super::EsdtLocalRoleFlags; -use crate as multiversx_sc; -use crate::{ - codec::{ - self, - derive::{NestedDecode, NestedEncode, TopDecode, TopEncode}, - }, - derive::type_abi, - types::{ManagedVecItem, ManagedVecItemPayloadBuffer}, -}; - -static ESDT_ROLE_NONE: &[u8] = &[]; -static ESDT_ROLE_LOCAL_MINT: &[u8] = b"ESDTRoleLocalMint"; -static ESDT_ROLE_LOCAL_BURN: &[u8] = b"ESDTRoleLocalBurn"; -static ESDT_ROLE_NFT_CREATE: &[u8] = b"ESDTRoleNFTCreate"; -static ESDT_ROLE_NFT_ADD_QUANTITY: &[u8] = b"ESDTRoleNFTAddQuantity"; -static ESDT_ROLE_NFT_BURN: &[u8] = b"ESDTRoleNFTBurn"; -static ESDT_ROLE_NFT_ADD_URI: &[u8] = b"ESDTRoleNFTAddURI"; -static ESDT_ROLE_NFT_UPDATE_ATTRIBUTES: &[u8] = b"ESDTRoleNFTUpdateAttributes"; -static ESDT_ROLE_TRANSFER: &[u8] = b"ESDTTransferRole"; - -#[type_abi] -#[derive(TopDecode, TopEncode, NestedDecode, NestedEncode, Clone, PartialEq, Eq, Debug, Copy)] -pub enum EsdtLocalRole { - None, - Mint, - Burn, - NftCreate, - NftAddQuantity, - NftBurn, - NftAddUri, - NftUpdateAttributes, - Transfer, -} - -impl EsdtLocalRole { - pub fn as_u16(&self) -> u16 { - match self { - Self::None => 0, - Self::Mint => 1, - Self::Burn => 2, - Self::NftCreate => 3, - Self::NftAddQuantity => 4, - Self::NftBurn => 5, - Self::NftAddUri => 6, - Self::NftUpdateAttributes => 7, - Self::Transfer => 8, - } - } - - pub fn as_role_name(&self) -> &'static [u8] { - match self { - Self::None => ESDT_ROLE_NONE, - Self::Mint => ESDT_ROLE_LOCAL_MINT, - Self::Burn => ESDT_ROLE_LOCAL_BURN, - Self::NftCreate => ESDT_ROLE_NFT_CREATE, - Self::NftAddQuantity => ESDT_ROLE_NFT_ADD_QUANTITY, - Self::NftBurn => ESDT_ROLE_NFT_BURN, - Self::NftAddUri => ESDT_ROLE_NFT_ADD_URI, - Self::NftUpdateAttributes => ESDT_ROLE_NFT_UPDATE_ATTRIBUTES, - Self::Transfer => ESDT_ROLE_TRANSFER, - } - } - - pub fn to_flag(&self) -> EsdtLocalRoleFlags { - match self { - Self::None => EsdtLocalRoleFlags::NONE, - Self::Mint => EsdtLocalRoleFlags::MINT, - Self::Burn => EsdtLocalRoleFlags::BURN, - Self::NftCreate => EsdtLocalRoleFlags::NFT_CREATE, - Self::NftAddQuantity => EsdtLocalRoleFlags::NFT_ADD_QUANTITY, - Self::NftBurn => EsdtLocalRoleFlags::NFT_BURN, - Self::NftAddUri => EsdtLocalRoleFlags::NFT_ADD_URI, - Self::NftUpdateAttributes => EsdtLocalRoleFlags::NFT_UPDATE_ATTRIBUTES, - Self::Transfer => EsdtLocalRoleFlags::TRANSFER, - } - } -} - -// TODO: can be done with macros, but I didn't find a public library that does it and is no_std -// we can implement it, it's easy -const ALL_ROLES: [EsdtLocalRole; 8] = [ - EsdtLocalRole::Mint, - EsdtLocalRole::Burn, - EsdtLocalRole::NftCreate, - EsdtLocalRole::NftAddQuantity, - EsdtLocalRole::NftBurn, - EsdtLocalRole::NftAddUri, - EsdtLocalRole::NftUpdateAttributes, - EsdtLocalRole::Transfer, -]; - -impl EsdtLocalRole { - pub fn iter_all() -> core::slice::Iter<'static, EsdtLocalRole> { - ALL_ROLES.iter() - } -} - -impl From for EsdtLocalRole { - #[inline] - fn from(value: u16) -> Self { - match value { - 1 => Self::Mint, - 2 => Self::Burn, - 3 => Self::NftCreate, - 4 => Self::NftAddQuantity, - 5 => Self::NftBurn, - 6 => Self::NftAddUri, - 7 => Self::NftUpdateAttributes, - 8 => Self::Transfer, - _ => Self::None, - } - } -} - -impl<'a> From<&'a [u8]> for EsdtLocalRole { - #[inline] - fn from(byte_slice: &'a [u8]) -> Self { - if byte_slice == ESDT_ROLE_LOCAL_MINT { - Self::Mint - } else if byte_slice == ESDT_ROLE_LOCAL_BURN { - Self::Burn - } else if byte_slice == ESDT_ROLE_NFT_CREATE { - Self::NftCreate - } else if byte_slice == ESDT_ROLE_NFT_ADD_QUANTITY { - Self::NftAddQuantity - } else if byte_slice == ESDT_ROLE_NFT_BURN { - Self::NftBurn - } else if byte_slice == ESDT_ROLE_NFT_ADD_URI { - Self::NftAddUri - } else if byte_slice == ESDT_ROLE_NFT_UPDATE_ATTRIBUTES { - Self::NftUpdateAttributes - } else if byte_slice == ESDT_ROLE_TRANSFER { - Self::Transfer - } else { - Self::None - } - } -} - -impl ManagedVecItem for EsdtLocalRole { - type PAYLOAD = ManagedVecItemPayloadBuffer<1>; - const SKIPS_RESERIALIZATION: bool = false; // TODO: might be ok to be true, but needs testing - type Ref<'a> = Self; - - fn from_byte_reader(reader: Reader) -> Self { - u16::from_byte_reader(reader).into() - } - - unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( - reader: Reader, - ) -> Self::Ref<'a> { - Self::from_byte_reader(reader) - } - - fn to_byte_writer R>(&self, writer: Writer) -> R { - ::to_byte_writer(&self.as_u16(), writer) - } -} diff --git a/framework/base/src/types/heap/mod.rs b/framework/base/src/types/heap.rs similarity index 63% rename from framework/base/src/types/heap/mod.rs rename to framework/base/src/types/heap.rs index eea5a01b90..c87ef2e2c2 100644 --- a/framework/base/src/types/heap/mod.rs +++ b/framework/base/src/types/heap.rs @@ -1,15 +1,11 @@ mod arg_buffer; mod async_call_result; -mod boxed_bytes; -mod h256; -mod h256_address; mod queue; pub use arg_buffer::ArgBuffer; pub use async_call_result::{AsyncCallError, AsyncCallResult}; -pub use boxed_bytes::BoxedBytes; -pub use h256::H256; -pub use h256_address::Address; pub use queue::Queue; pub use alloc::{boxed::Box, string::String, vec::Vec}; + +pub use crate::vm_core::types::{Address, BoxedBytes, H256}; diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index 9f21a13de4..744b310b58 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -1,5 +1,7 @@ use core::borrow::Borrow; +use multiversx_chain_vm_core::types::{EsdtLocalRole, EsdtTokenType}; + use crate::{ api::ManagedTypeApi, types::{ @@ -258,3 +260,45 @@ where ::to_byte_writer(&self.get_handle(), writer) } } + +impl ManagedVecItem for EsdtTokenType { + type PAYLOAD = ManagedVecItemPayloadBuffer<1>; + const SKIPS_RESERIALIZATION: bool = true; + type Ref<'a> = Self; + + fn from_byte_reader(mut reader: Reader) -> Self { + let mut arr: [u8; 1] = [0u8; 1]; + reader(&mut arr[..]); + arr[0].into() + } + + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( + reader: Reader, + ) -> Self::Ref<'a> { + Self::from_byte_reader(reader) + } + + fn to_byte_writer R>(&self, mut writer: Writer) -> R { + writer(&[self.as_u8()]) + } +} + +impl ManagedVecItem for EsdtLocalRole { + type PAYLOAD = ManagedVecItemPayloadBuffer<1>; + const SKIPS_RESERIALIZATION: bool = false; // TODO: might be ok to be true, but needs testing + type Ref<'a> = Self; + + fn from_byte_reader(reader: Reader) -> Self { + u16::from_byte_reader(reader).into() + } + + unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>( + reader: Reader, + ) -> Self::Ref<'a> { + Self::from_byte_reader(reader) + } + + fn to_byte_writer R>(&self, writer: Writer) -> R { + ::to_byte_writer(&self.as_u16(), writer) + } +} diff --git a/publish.sh b/publish.sh index 53a5e922af..60b32b298c 100755 --- a/publish.sh +++ b/publish.sh @@ -60,23 +60,27 @@ # 15. Write a release announcement in Confluence. # -cd vm +cd data/codec-derive cargo publish || return 1 -cd .. +cd ../.. -cd sdk/core +cd data/codec cargo publish || return 1 cd ../.. -cd sdk/scenario-format/ +cd vm-core cargo publish || return 1 -cd ../.. +cd .. -cd data/codec-derive +cd vm +cargo publish || return 1 +cd .. + +cd sdk/core cargo publish || return 1 cd ../.. -cd data/codec +cd sdk/scenario-format/ cargo publish || return 1 cd ../.. diff --git a/vm-core/Cargo.toml b/vm-core/Cargo.toml new file mode 100644 index 0000000000..6034ba4590 --- /dev/null +++ b/vm-core/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "multiversx-chain-vm-core" +version = "0.10.0" +edition = "2021" + +authors = [ + "Andrei Marinica ", + "MultiversX ", +] +license = "GPL-3.0-only" +readme = "README.md" +repository = "https://github.com/multiversx/mx-sdk-rs" +homepage = "https://multiversx.com/" +documentation = "https://docs.multiversx.com/" +description = "MultiversX VM interfaces and base types" +keywords = ["multiversx", "blockchain", "vm", "tools"] +categories = ["cryptography::cryptocurrencies", "development-tools::debugging"] + +[dependencies] +bitflags = "=2.6.0" + +[dependencies.multiversx-sc-codec] +version = "=0.21.0" +path = "../data/codec" +features = ["derive"] + +[dependencies.multiversx-chain-vm-executor] +version = "=0.2.0" diff --git a/vm-core/README.md b/vm-core/README.md new file mode 100644 index 0000000000..fc9282a390 --- /dev/null +++ b/vm-core/README.md @@ -0,0 +1,16 @@ +# MultiversX VM base types, interfaces and builtin function names + +It provides various types and contants referring to the MultiversX blockchain base implementation. + +This functionality is designed to be minimal and to be used from both smart contract code and VM implementations. + +It can be viewed as a collection of system specs, which hold for any MultiversX-related implementation. For example: +- `Address` - MultiversX adresses are 32 bytes long. This is the old SC address type, it holds the bytes on the heap. It is also used in the MultiversX Rust VM. +- `H256` - same as address, currently used for transaction hashes. +- Flags: + - Code metadata - a bitflag encoding the SC code metadta, as it is stored on the blockchain, and encoded in smart contracts; + - ESDT local roles + - as enum + - as bitflags + - ESDT token types + diff --git a/vm-core/src/lib.rs b/vm-core/src/lib.rs new file mode 100644 index 0000000000..937a07d6d8 --- /dev/null +++ b/vm-core/src/lib.rs @@ -0,0 +1,5 @@ +pub mod types; + +extern crate alloc; + +pub use multiversx_sc_codec as codec; diff --git a/vm-core/src/types.rs b/vm-core/src/types.rs new file mode 100644 index 0000000000..6aa0159afd --- /dev/null +++ b/vm-core/src/types.rs @@ -0,0 +1,13 @@ +mod address; +mod boxed_bytes; +mod flags; +mod h256; +mod heap_address; +mod heap_h256; + +pub use address::Address; +pub use boxed_bytes::BoxedBytes; +pub use flags::*; +pub use h256::H256; +pub use heap_address::HeapAddress; +pub use heap_h256::HeapH256; diff --git a/framework/base/src/types/heap/h256_address.rs b/vm-core/src/types/address.rs similarity index 80% rename from framework/base/src/types/heap/h256_address.rs rename to vm-core/src/types/address.rs index a9fa69c2e7..e97ee5934a 100644 --- a/framework/base/src/types/heap/h256_address.rs +++ b/vm-core/src/types/address.rs @@ -1,12 +1,11 @@ -use super::h256::H256; -use crate::{ - abi::{TypeAbi, TypeAbiFrom, TypeName}, - types::heap::BoxedBytes, -}; +use super::H256; use alloc::{boxed::Box, vec::Vec}; use core::fmt::Debug; const SC_ADDRESS_NUM_LEADING_ZEROS: u8 = 8; +pub const NUM_INT_CHARACTERS_FOR_ADDRESS: usize = 10; +pub const VM_TYPE_LEN: usize = 2; +pub const DEFAULT_VM_TYPE: &[u8] = &[5, 0]; /// An Address is just a H256 with a different name. /// Has a different ABI name than H256. @@ -16,6 +15,30 @@ const SC_ADDRESS_NUM_LEADING_ZEROS: u8 = 8; #[derive(Hash, PartialEq, Eq, Clone, Debug)] pub struct Address(H256); +impl Address { + pub const fn new(bytes: [u8; 32]) -> Self { + Address(H256::new(bytes)) + } + + pub fn generate_mock_address(creator_address: &[u8], creator_nonce: u64) -> Self { + let mut result = [0x00; 32]; + + result[10] = 0x11; + result[11] = 0x11; + result[12] = 0x11; + result[13] = 0x11; + + result[14..29].copy_from_slice(&creator_address[..15]); + result[29] = creator_nonce as u8; + result[30..].copy_from_slice(&creator_address[30..]); + + let start_index = NUM_INT_CHARACTERS_FOR_ADDRESS - VM_TYPE_LEN; + result[start_index..(start_index + DEFAULT_VM_TYPE.len())].copy_from_slice(DEFAULT_VM_TYPE); + + Address::from(result) + } +} + impl From for Address { #[inline] fn from(hash: H256) -> Self { @@ -145,12 +168,12 @@ impl Address { self.0.is_zero() } - /// Transmutes self to an (in principle) variable length boxed bytes object. - /// Both BoxedBytes and H256 keep the data on the heap, so only the pointer to that data needs to be transmuted. - /// Does not reallocate or copy data, the data on the heap remains untouched. - pub fn into_boxed_bytes(self) -> BoxedBytes { - self.0.into_boxed_bytes() - } + // /// Transmutes self to an (in principle) variable length boxed bytes object. + // /// Both BoxedBytes and H256 keep the data on the heap, so only the pointer to that data needs to be transmuted. + // /// Does not reallocate or copy data, the data on the heap remains untouched. + // pub fn into_boxed_bytes(self) -> BoxedBytes { + // self.0.into_boxed_bytes() + // } pub fn is_smart_contract_address(&self) -> bool { self.as_bytes() @@ -202,20 +225,6 @@ impl TopDecode for Address { } } -impl TypeAbiFrom for Address {} - -impl TypeAbi for Address { - type Unmanaged = Self; - - fn type_name() -> TypeName { - "Address".into() - } - - fn type_name_rust() -> TypeName { - "Address".into() - } -} - #[cfg(test)] mod address_tests { use super::*; @@ -251,11 +260,4 @@ mod address_tests { fn test_is_zero() { assert!(Address::zero().is_zero()); } - - #[test] - fn test_size_of() { - use core::mem::size_of; - assert_eq!(size_of::
(), size_of::()); - assert_eq!(size_of::>(), size_of::()); - } } diff --git a/framework/base/src/types/heap/boxed_bytes.rs b/vm-core/src/types/boxed_bytes.rs similarity index 96% rename from framework/base/src/types/heap/boxed_bytes.rs rename to vm-core/src/types/boxed_bytes.rs index 336b0874a8..b6fbb86297 100644 --- a/framework/base/src/types/heap/boxed_bytes.rs +++ b/vm-core/src/types/boxed_bytes.rs @@ -5,14 +5,14 @@ use alloc::{ vec::Vec, }; -use crate::{ - abi::{TypeAbi, TypeAbiFrom, TypeName}, - codec::*, -}; +use crate::codec::*; /// Simple wrapper around a boxed byte slice, /// but with a lot of optimized methods for manipulating it. /// The focus is on reducing code size rather improving speed. +/// +/// Used to be used extensively in smart contracts, before the introduction of ManagedBuffer, +/// but was superseded by it. #[derive(Clone, PartialEq, Eq, Debug)] pub struct BoxedBytes(Box<[u8]>); @@ -224,20 +224,6 @@ impl TopDecode for BoxedBytes { } } -impl TypeAbiFrom for BoxedBytes {} - -impl TypeAbi for BoxedBytes { - type Unmanaged = Self; - - fn type_name() -> TypeName { - "bytes".into() - } - - fn type_name_rust() -> TypeName { - "BoxedBytes".into() - } -} - //////////////////////////////////////////////////////////////////////////////// #[cfg(test)] diff --git a/framework/base/src/types/flags/mod.rs b/vm-core/src/types/flags.rs similarity index 84% rename from framework/base/src/types/flags/mod.rs rename to vm-core/src/types/flags.rs index b24a0d4fba..cfe9fc28ef 100644 --- a/framework/base/src/types/flags/mod.rs +++ b/vm-core/src/types/flags.rs @@ -2,8 +2,10 @@ mod code_metadata; mod esdt_local_role; mod esdt_local_role_flags; mod esdt_token_type; +mod token_type; pub use code_metadata::CodeMetadata; pub use esdt_local_role::EsdtLocalRole; pub use esdt_local_role_flags::EsdtLocalRoleFlags; pub use esdt_token_type::EsdtTokenType; +pub use token_type::TokenType; diff --git a/framework/base/src/types/flags/code_metadata.rs b/vm-core/src/types/flags/code_metadata.rs similarity index 69% rename from framework/base/src/types/flags/code_metadata.rs rename to vm-core/src/types/flags/code_metadata.rs index 9275e976cf..596bf9744c 100644 --- a/framework/base/src/types/flags/code_metadata.rs +++ b/vm-core/src/types/flags/code_metadata.rs @@ -1,17 +1,13 @@ #![allow(clippy::bad_bit_mask)] -use crate::{ - abi::{TypeAbi, TypeAbiFrom, TypeName}, - codec::*, - formatter::{hex_util, FormatByteReceiver, SCBinary, SCDisplay, SCLowerHex}, -}; +use crate::codec::*; use bitflags::bitflags; -const UPGRADEABLE_STRING: &[u8] = b"Upgradeable"; -const READABLE_STRING: &[u8] = b"Readable"; -const PAYABLE_STRING: &[u8] = b"Payable"; -const PAYABLE_BY_SC_STRING: &[u8] = b"PayableBySC"; -const DEFAULT_STRING: &[u8] = b"Default"; +const UPGRADEABLE_STRING: &str = "Upgradeable"; +const READABLE_STRING: &str = "Readable"; +const PAYABLE_STRING: &str = "Payable"; +const PAYABLE_BY_SC_STRING: &str = "PayableBySC"; +const DEFAULT_STRING: &str = "Default"; bitflags! { #[derive(Default, PartialEq, Debug, Clone, Copy)] @@ -45,6 +41,43 @@ impl CodeMetadata { pub fn to_byte_array(&self) -> [u8; 2] { self.bits().to_be_bytes() } + + pub fn to_vec(&self) -> Vec { + self.to_byte_array().to_vec() + } + + pub fn for_each_string_token(&self, mut f: F) { + let mut nothing_printed: bool = true; + if self.is_upgradeable() { + f(UPGRADEABLE_STRING); + nothing_printed = false; + } + if self.is_readable() { + if !nothing_printed { + f("|"); + } + f(READABLE_STRING); + nothing_printed = false; + } + if self.is_payable() { + if !nothing_printed { + f("|"); + } + f(PAYABLE_STRING); + nothing_printed = false; + } + if self.is_payable_by_sc() { + if !nothing_printed { + f("|"); + } + f(PAYABLE_BY_SC_STRING); + nothing_printed = false; + } + + if nothing_printed { + f(DEFAULT_STRING); + } + } } impl From<[u8; 2]> for CodeMetadata { @@ -61,6 +94,19 @@ impl From for CodeMetadata { } } +impl From<&[u8]> for CodeMetadata { + fn from(slice: &[u8]) -> Self { + let arr: [u8; 2] = slice.try_into().unwrap_or_default(); + CodeMetadata::from(arr) + } +} + +impl From<&Vec> for CodeMetadata { + fn from(v: &Vec) -> Self { + CodeMetadata::from(v.as_slice()) + } +} + impl NestedEncode for CodeMetadata { fn dep_encode_or_handle_err(&self, dest: &mut O, h: H) -> Result<(), H::HandledErr> where @@ -103,74 +149,6 @@ impl TopDecode for CodeMetadata { } } -impl TypeAbiFrom for CodeMetadata {} - -impl TypeAbi for CodeMetadata { - type Unmanaged = Self; - - fn type_name() -> TypeName { - "CodeMetadata".into() - } - - fn type_name_rust() -> TypeName { - "CodeMetadata".into() - } -} - -impl SCDisplay for CodeMetadata { - fn fmt(&self, f: &mut F) { - let mut nothing_printed: bool = true; - verify_metadata_and_append( - self.is_upgradeable(), - f, - UPGRADEABLE_STRING, - &mut nothing_printed, - ); - verify_metadata_and_append(self.is_readable(), f, READABLE_STRING, &mut nothing_printed); - verify_metadata_and_append(self.is_payable(), f, PAYABLE_STRING, &mut nothing_printed); - verify_metadata_and_append( - self.is_payable_by_sc(), - f, - PAYABLE_BY_SC_STRING, - &mut nothing_printed, - ); - if nothing_printed { - f.append_bytes(DEFAULT_STRING); - } - } -} - -impl SCLowerHex for CodeMetadata { - fn fmt(&self, f: &mut F) { - let num = self.bits().to_be_bytes(); - f.append_bytes(&hex_util::byte_to_hex_digits(num[0])[..]); - f.append_bytes(&hex_util::byte_to_hex_digits(num[1])[..]); - } -} - -impl SCBinary for CodeMetadata { - fn fmt(&self, f: &mut F) { - let num = self.bits().to_be_bytes(); - f.append_bytes(&hex_util::byte_to_binary_digits(num[0])[..]); - f.append_bytes(&hex_util::byte_to_binary_digits(num[1])[..]); - } -} - -fn verify_metadata_and_append( - constraint: bool, - f: &mut F, - bytes_to_append: &[u8], - nothing_printed: &mut bool, -) { - if constraint { - if !*nothing_printed { - f.append_bytes(b"|"); - } - f.append_bytes(bytes_to_append); - *nothing_printed = false; - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/vm/src/types/vm_esdt_local_role.rs b/vm-core/src/types/flags/esdt_local_role.rs similarity index 92% rename from vm/src/types/vm_esdt_local_role.rs rename to vm-core/src/types/flags/esdt_local_role.rs index 40c383b869..048d3876ce 100644 --- a/vm/src/types/vm_esdt_local_role.rs +++ b/vm-core/src/types/flags/esdt_local_role.rs @@ -1,4 +1,8 @@ use super::EsdtLocalRoleFlags; +use crate::codec::{ + self, + derive::{NestedDecode, NestedEncode, TopDecode, TopEncode}, +}; const ESDT_ROLE_NONE: &str = ""; const ESDT_ROLE_LOCAL_MINT: &str = "ESDTRoleLocalMint"; @@ -10,12 +14,7 @@ const ESDT_ROLE_NFT_ADD_URI: &str = "ESDTRoleNFTAddURI"; const ESDT_ROLE_NFT_UPDATE_ATTRIBUTES: &str = "ESDTRoleNFTUpdateAttributes"; const ESDT_ROLE_TRANSFER: &str = "ESDTTransferRole"; -/// The VM implementation for EsdtLocalRole, used internally in builtin functions. -/// -/// There is another near-identical implementation in the framework, used for communicating with the VM. -/// -/// It might be a good idea to move it to some "common ground" crate, between the framework and the VM. -#[derive(Clone, PartialEq, Eq, Debug, Copy)] +#[derive(TopDecode, TopEncode, NestedDecode, NestedEncode, Clone, PartialEq, Eq, Debug, Copy)] pub enum EsdtLocalRole { None, Mint, @@ -43,6 +42,10 @@ impl EsdtLocalRole { } } + pub fn as_role_name(&self) -> &'static [u8] { + self.name().as_bytes() + } + pub fn name(&self) -> &'static str { match self { Self::None => ESDT_ROLE_NONE, diff --git a/framework/base/src/types/flags/esdt_local_role_flags.rs b/vm-core/src/types/flags/esdt_local_role_flags.rs similarity index 100% rename from framework/base/src/types/flags/esdt_local_role_flags.rs rename to vm-core/src/types/flags/esdt_local_role_flags.rs diff --git a/framework/base/src/types/flags/esdt_token_type.rs b/vm-core/src/types/flags/esdt_token_type.rs similarity index 89% rename from framework/base/src/types/flags/esdt_token_type.rs rename to vm-core/src/types/flags/esdt_token_type.rs index e80b91590d..74db07c9a6 100644 --- a/framework/base/src/types/flags/esdt_token_type.rs +++ b/vm-core/src/types/flags/esdt_token_type.rs @@ -1,4 +1,4 @@ -use multiversx_sc_derive::{type_abi, ManagedVecItem}; +// use multiversx_sc_derive::{type_abi, ManagedVecItem}; use crate::codec::{ self, @@ -11,14 +11,9 @@ const ESDT_TYPE_SEMI_FUNGIBLE: &[u8] = b"SemiFungibleESDT"; const ESDT_TYPE_META: &[u8] = b"MetaESDT"; const ESDT_TYPE_INVALID: &[u8] = &[]; -use crate as multiversx_sc; // needed by the TypeAbi generated code - // Note: In the current implementation, SemiFungible is never returned -#[type_abi] -#[derive( - TopDecode, TopEncode, NestedDecode, NestedEncode, Clone, PartialEq, Eq, Debug, ManagedVecItem, -)] +#[derive(TopDecode, TopEncode, NestedDecode, NestedEncode, Clone, PartialEq, Eq, Debug)] pub enum EsdtTokenType { Fungible, NonFungible, diff --git a/vm/src/types/vm_token_type.rs b/vm-core/src/types/flags/token_type.rs similarity index 51% rename from vm/src/types/vm_token_type.rs rename to vm-core/src/types/flags/token_type.rs index 18670a90dc..77ff975a45 100644 --- a/vm/src/types/vm_token_type.rs +++ b/vm-core/src/types/flags/token_type.rs @@ -1,18 +1,18 @@ #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum VMTokenType { +pub enum TokenType { Fungible, SemiFungible, Meta, NonFungible, } -impl VMTokenType { +impl TokenType { pub fn from_system_sc_arg(raw: &[u8]) -> Self { match raw { - b"FNG" => VMTokenType::Fungible, - b"SFT" => VMTokenType::SemiFungible, - b"META" => VMTokenType::Meta, - b"NFT" => VMTokenType::NonFungible, + b"FNG" => TokenType::Fungible, + b"SFT" => TokenType::SemiFungible, + b"META" => TokenType::Meta, + b"NFT" => TokenType::NonFungible, _ => panic!("invalid token type"), } } diff --git a/vm-core/src/types/h256.rs b/vm-core/src/types/h256.rs new file mode 100644 index 0000000000..8a7b63d1a1 --- /dev/null +++ b/vm-core/src/types/h256.rs @@ -0,0 +1,223 @@ +use alloc::{boxed::Box, vec::Vec}; +use core::fmt::Debug; + +const ZERO_32: &[u8] = &[0u8; 32]; + +/// Type that holds 32 bytes of data. +/// Data is kept on the heap to keep wasm size low and avoid copies. +#[derive(Hash, PartialEq, Eq, Clone, Debug)] +pub struct H256([u8; 32]); + +impl H256 { + pub const fn new(bytes: [u8; 32]) -> Self { + H256(bytes) + } +} + +impl From<[u8; 32]> for H256 { + /// Constructs a hash type from the given bytes array of fixed length. + /// + /// # Note + /// + /// The given bytes are interpreted in big endian order. + fn from(arr: [u8; 32]) -> Self { + H256::new(arr) + } +} + +impl<'a> From<&'a [u8; 32]> for H256 { + /// Constructs a hash type from the given reference + /// to the bytes array of fixed length. + /// + /// # Note + /// + /// The given bytes are interpreted in big endian order. + #[inline] + fn from(bytes: &'a [u8; 32]) -> Self { + H256::new(*bytes) + } +} + +impl<'a> From<&'a mut [u8; 32]> for H256 { + /// Constructs a hash type from the given reference + /// to the mutable bytes array of fixed length. + /// + /// # Note + /// + /// The given bytes are interpreted in big endian order. + #[inline] + fn from(bytes: &'a mut [u8; 32]) -> Self { + H256::new(*bytes) + } +} + +impl From> for H256 { + #[inline] + fn from(bytes: Box<[u8; 32]>) -> Self { + H256::new(*bytes) + } +} + +impl H256 { + pub fn from_slice(slice: &[u8]) -> Self { + let mut arr = [0u8; 32]; + let len = core::cmp::min(slice.len(), 32); + arr[..len].copy_from_slice(&slice[..len]); + H256::new(arr) + } +} + +impl From for [u8; 32] { + fn from(s: H256) -> Self { + s.0 + } +} + +impl AsRef<[u8]> for H256 { + #[inline] + fn as_ref(&self) -> &[u8] { + self.as_bytes() + } +} + +impl AsMut<[u8]> for H256 { + #[inline] + fn as_mut(&mut self) -> &mut [u8] { + self.0.as_mut() + } +} + +impl H256 { + /// Returns a new zero-initialized fixed hash. + pub fn zero() -> Self { + H256([0u8; 32]) + } + + /// Returns the size of this hash in bytes. + #[inline] + pub fn len_bytes() -> usize { + 32 + } + + /// Extracts a byte slice containing the entire fixed hash. + #[inline] + pub fn as_bytes(&self) -> &[u8] { + self.0.as_ref() + } + + #[inline] + pub fn as_array(&self) -> &[u8; 32] { + &self.0 + } + + #[inline] + pub fn copy_to_array(&self, target: &mut [u8; 32]) { + target.copy_from_slice(&self.0[..]); + } + + #[inline] + pub fn to_vec(&self) -> Vec { + self.0[..].to_vec() + } + + /// Pointer to the data on the heap. + #[inline] + pub fn as_ptr(&self) -> *const u8 { + self.0.as_ptr() + } + + /// Returns an unsafe mutable pointer to the data on the heap. + /// Used by the API to populate data. + #[inline] + pub fn as_mut_ptr(&mut self) -> *mut u8 { + self.0.as_mut_ptr() + } + + /// True if all 32 bytes of the hash are zero. + pub fn is_zero(&self) -> bool { + self.as_bytes() == ZERO_32 + } +} + +use crate::codec::*; + +impl NestedEncode for H256 { + fn dep_encode_or_handle_err(&self, dest: &mut O, _h: H) -> Result<(), H::HandledErr> + where + O: NestedEncodeOutput, + H: EncodeErrorHandler, + { + dest.write(&self.0[..]); + Ok(()) + } +} + +impl TopEncode for H256 { + fn top_encode_or_handle_err(&self, output: O, _h: H) -> Result<(), H::HandledErr> + where + O: TopEncodeOutput, + H: EncodeErrorHandler, + { + output.set_slice_u8(&self.0[..]); + Ok(()) + } +} + +impl NestedDecode for H256 { + fn dep_decode_or_handle_err(input: &mut I, h: H) -> Result + where + I: NestedDecodeInput, + H: DecodeErrorHandler, + { + let mut res = H256::zero(); + input.read_into(res.as_mut(), h)?; + Ok(res) + } +} + +impl TopDecode for H256 { + fn top_decode_or_handle_err(input: I, h: H) -> Result + where + I: TopDecodeInput, + H: DecodeErrorHandler, + { + Ok(Self::new(<[u8; 32]>::top_decode_or_handle_err(input, h)?)) + } +} + +#[cfg(test)] +mod h256_tests { + use super::*; + use crate::codec::test_util::{check_top_encode, check_top_encode_decode}; + use alloc::vec::Vec; + + #[test] + fn test_h256_from_array() { + let addr = H256::from([4u8; 32]); + check_top_encode_decode(addr, &[4u8; 32]); + } + + #[test] + fn test_opt_h256() { + let addr = H256::from([4u8; 32]); + let mut expected: Vec = Vec::new(); + expected.push(1u8); + expected.extend_from_slice(&[4u8; 32]); + check_top_encode_decode(Some(addr), expected.as_slice()); + } + + #[test] + fn test_ser_h256_ref() { + let addr = H256::from([4u8; 32]); + let expected_bytes: &[u8] = &[4u8; 32 * 3]; + + let tuple = (&addr, &&&addr, addr.clone()); + let serialized_bytes = check_top_encode(&tuple); + assert_eq!(serialized_bytes.as_slice(), expected_bytes); + } + + #[test] + fn test_is_zero() { + assert!(H256::zero().is_zero()); + } +} diff --git a/vm-core/src/types/heap_address.rs b/vm-core/src/types/heap_address.rs new file mode 100644 index 0000000000..467f3c469e --- /dev/null +++ b/vm-core/src/types/heap_address.rs @@ -0,0 +1,257 @@ +use super::{heap_h256::HeapH256, BoxedBytes}; + +use alloc::{boxed::Box, vec::Vec}; +use core::fmt::Debug; + +const SC_ADDRESS_NUM_LEADING_ZEROS: u8 = 8; + +/// Old smart contracts were using this Address implementation, +/// which was explicitly relying on the heap, to avoid large data copies on the stack. +/// +/// It is no longer used, kept for reference. +#[derive(Hash, PartialEq, Eq, Clone, Debug)] +pub struct HeapAddress(HeapH256); + +impl From for HeapAddress { + #[inline] + fn from(hash: HeapH256) -> Self { + HeapAddress(hash) + } +} + +impl From for HeapH256 { + #[inline] + fn from(address: HeapAddress) -> Self { + address.0 + } +} + +impl<'a> From<&'a HeapAddress> for &'a HeapH256 { + #[inline] + fn from(address: &'a HeapAddress) -> Self { + &address.0 + } +} + +impl From<[u8; 32]> for HeapAddress { + #[inline] + fn from(arr: [u8; 32]) -> Self { + HeapAddress(HeapH256::from(arr)) + } +} + +impl<'a> From<&'a [u8; 32]> for HeapAddress { + #[inline] + fn from(bytes: &'a [u8; 32]) -> Self { + HeapAddress(HeapH256::from(bytes)) + } +} + +impl<'a> From<&'a mut [u8; 32]> for HeapAddress { + #[inline] + fn from(bytes: &'a mut [u8; 32]) -> Self { + HeapAddress(HeapH256::from(bytes)) + } +} + +impl From> for HeapAddress { + #[inline] + fn from(bytes: Box<[u8; 32]>) -> Self { + HeapAddress(HeapH256::from(bytes)) + } +} + +impl HeapAddress { + pub fn from_slice(slice: &[u8]) -> Self { + HeapAddress(HeapH256::from_slice(slice)) + } +} + +impl From for [u8; 32] { + #[inline] + fn from(addr: HeapAddress) -> Self { + addr.0.into() + } +} + +impl AsRef<[u8]> for HeapAddress { + #[inline] + fn as_ref(&self) -> &[u8] { + self.0.as_ref() + } +} + +impl AsMut<[u8]> for HeapAddress { + #[inline] + fn as_mut(&mut self) -> &mut [u8] { + self.0.as_mut() + } +} + +impl HeapAddress { + /// Returns a new address of 32 zeros. + /// Allocates directly in heap. + /// Minimal resulting wasm code (14 bytes if not inlined). + pub fn zero() -> Self { + HeapAddress(HeapH256::zero()) + } + + /// Returns the size of an address in bytes. + #[inline] + pub fn len_bytes() -> usize { + HeapH256::len_bytes() + } + + /// Extracts a byte slice containing the entire fixed hash. + #[inline] + pub fn as_bytes(&self) -> &[u8] { + self.0.as_bytes() + } + + #[inline] + pub fn as_array(&self) -> &[u8; 32] { + self.0.as_array() + } + + #[inline] + pub fn copy_to_array(&self, target: &mut [u8; 32]) { + self.0.copy_to_array(target) + } + + #[inline] + pub fn to_vec(&self) -> Vec { + self.0.to_vec() + } + + /// Pointer to the data on the heap. + #[inline] + pub fn as_ptr(&self) -> *const u8 { + self.0.as_ptr() + } + + /// Returns an unsafe mutable pointer to the data on the heap. + /// Used by the API to populate data. + #[inline] + pub fn as_mut_ptr(&mut self) -> *mut u8 { + self.0.as_mut_ptr() + } + + /// True if all 32 bytes of the hash are zero. + pub fn is_zero(&self) -> bool { + self.0.is_zero() + } + + /// Transmutes self to an (in principle) variable length boxed bytes object. + /// Both BoxedBytes and H256 keep the data on the heap, so only the pointer to that data needs to be transmuted. + /// Does not reallocate or copy data, the data on the heap remains untouched. + pub fn into_boxed_bytes(self) -> BoxedBytes { + self.0.into_boxed_bytes() + } + + pub fn is_smart_contract_address(&self) -> bool { + self.as_bytes() + .iter() + .take(SC_ADDRESS_NUM_LEADING_ZEROS.into()) + .all(|item| item == &0u8) + } +} + +use crate::codec::*; + +impl NestedEncode for HeapAddress { + fn dep_encode_or_handle_err(&self, dest: &mut O, h: H) -> Result<(), H::HandledErr> + where + O: NestedEncodeOutput, + H: EncodeErrorHandler, + { + self.0.dep_encode_or_handle_err(dest, h) + } +} + +impl TopEncode for HeapAddress { + fn top_encode_or_handle_err(&self, output: O, h: H) -> Result<(), H::HandledErr> + where + O: TopEncodeOutput, + H: EncodeErrorHandler, + { + self.0.top_encode_or_handle_err(output, h) + } +} + +impl NestedDecode for HeapAddress { + fn dep_decode_or_handle_err(input: &mut I, h: H) -> Result + where + I: NestedDecodeInput, + H: DecodeErrorHandler, + { + Ok(HeapAddress(HeapH256::dep_decode_or_handle_err(input, h)?)) + } +} + +impl TopDecode for HeapAddress { + fn top_decode_or_handle_err(input: I, h: H) -> Result + where + I: TopDecodeInput, + H: DecodeErrorHandler, + { + Ok(HeapAddress(HeapH256::top_decode_or_handle_err(input, h)?)) + } +} + +// impl TypeAbiFrom for HeapAddress {} + +// impl TypeAbi for HeapAddress { +// type Unmanaged = Self; + +// fn type_name() -> TypeName { +// "Address".into() +// } + +// fn type_name_rust() -> TypeName { +// "Address".into() +// } +// } + +#[cfg(test)] +mod address_tests { + use super::*; + use crate::codec::test_util::{check_top_encode, check_top_encode_decode}; + use alloc::vec::Vec; + + #[test] + fn test_address() { + let addr = HeapAddress::from([4u8; 32]); + check_top_encode_decode(addr, &[4u8; 32]); + } + + #[test] + fn test_opt_address() { + let addr = HeapAddress::from([4u8; 32]); + let mut expected: Vec = Vec::new(); + expected.push(1u8); + expected.extend_from_slice(&[4u8; 32]); + check_top_encode_decode(Some(addr), expected.as_slice()); + } + + #[test] + fn test_ser_address_ref() { + let addr = HeapAddress::from([4u8; 32]); + let expected_bytes: &[u8] = &[4u8; 32 * 3]; + + let tuple = (&addr, &&&addr, addr.clone()); + let serialized_bytes = check_top_encode(&tuple); + assert_eq!(serialized_bytes.as_slice(), expected_bytes); + } + + #[test] + fn test_is_zero() { + assert!(HeapAddress::zero().is_zero()); + } + + #[test] + fn test_size_of() { + use core::mem::size_of; + assert_eq!(size_of::(), size_of::()); + assert_eq!(size_of::>(), size_of::()); + } +} diff --git a/framework/base/src/types/heap/h256.rs b/vm-core/src/types/heap_h256.rs similarity index 78% rename from framework/base/src/types/heap/h256.rs rename to vm-core/src/types/heap_h256.rs index f440765d00..728b81f5a1 100644 --- a/framework/base/src/types/heap/h256.rs +++ b/vm-core/src/types/heap_h256.rs @@ -1,19 +1,17 @@ -use crate::{ - abi::{TypeAbi, TypeAbiFrom, TypeName}, - types::heap::BoxedBytes, -}; use alloc::{boxed::Box, vec::Vec}; use core::fmt::Debug; const ERR_BAD_H256_LENGTH: &str = "bad H256 length"; const ZERO_32: &[u8] = &[0u8; 32]; -/// Type that holds 32 bytes of data. -/// Data is kept on the heap to keep wasm size low and avoid copies. +/// Old smart contracts were using this H256 implementation, +/// which was explicitly relying on the heap, to avoid large data copies on the stack. +/// +/// It is no longer used, kept for reference. #[derive(Hash, PartialEq, Eq, Clone, Debug)] -pub struct H256(Box<[u8; 32]>); +pub struct HeapH256(Box<[u8; 32]>); -impl From<[u8; 32]> for H256 { +impl From<[u8; 32]> for HeapH256 { /// Constructs a hash type from the given bytes array of fixed length. /// /// # Note @@ -21,11 +19,11 @@ impl From<[u8; 32]> for H256 { /// The given bytes are interpreted in big endian order. #[inline] fn from(arr: [u8; 32]) -> Self { - H256(Box::new(arr)) + HeapH256(Box::new(arr)) } } -impl<'a> From<&'a [u8; 32]> for H256 { +impl<'a> From<&'a [u8; 32]> for HeapH256 { /// Constructs a hash type from the given reference /// to the bytes array of fixed length. /// @@ -34,11 +32,11 @@ impl<'a> From<&'a [u8; 32]> for H256 { /// The given bytes are interpreted in big endian order. #[inline] fn from(bytes: &'a [u8; 32]) -> Self { - H256(Box::new(*bytes)) + HeapH256(Box::new(*bytes)) } } -impl<'a> From<&'a mut [u8; 32]> for H256 { +impl<'a> From<&'a mut [u8; 32]> for HeapH256 { /// Constructs a hash type from the given reference /// to the mutable bytes array of fixed length. /// @@ -47,48 +45,48 @@ impl<'a> From<&'a mut [u8; 32]> for H256 { /// The given bytes are interpreted in big endian order. #[inline] fn from(bytes: &'a mut [u8; 32]) -> Self { - H256(Box::new(*bytes)) + HeapH256(Box::new(*bytes)) } } -impl From> for H256 { +impl From> for HeapH256 { #[inline] fn from(bytes: Box<[u8; 32]>) -> Self { - H256(bytes) + HeapH256(bytes) } } -impl H256 { +impl HeapH256 { pub fn from_slice(slice: &[u8]) -> Self { let mut arr = [0u8; 32]; let len = core::cmp::min(slice.len(), 32); arr[..len].copy_from_slice(&slice[..len]); - H256(Box::new(arr)) + HeapH256(Box::new(arr)) } } -impl From for [u8; 32] { +impl From for [u8; 32] { #[inline] - fn from(s: H256) -> Self { + fn from(s: HeapH256) -> Self { *(s.0) } } -impl AsRef<[u8]> for H256 { +impl AsRef<[u8]> for HeapH256 { #[inline] fn as_ref(&self) -> &[u8] { self.as_bytes() } } -impl AsMut<[u8]> for H256 { +impl AsMut<[u8]> for HeapH256 { #[inline] fn as_mut(&mut self) -> &mut [u8] { self.0.as_mut() } } -impl H256 { +impl HeapH256 { /// Returns a new zero-initialized fixed hash. /// Allocates directly in heap. /// Minimal resulting wasm code (14 bytes if not inlined). @@ -96,7 +94,7 @@ impl H256 { use alloc::alloc::{alloc_zeroed, Layout}; unsafe { let ptr = alloc_zeroed(Layout::new::<[u8; 32]>()) as *mut [u8; 32]; - H256(Box::from_raw(ptr)) + HeapH256(Box::from_raw(ptr)) } } @@ -159,7 +157,9 @@ impl H256 { use crate::codec::*; -impl NestedEncode for H256 { +use super::BoxedBytes; + +impl NestedEncode for HeapH256 { fn dep_encode_or_handle_err(&self, dest: &mut O, _h: H) -> Result<(), H::HandledErr> where O: NestedEncodeOutput, @@ -170,7 +170,7 @@ impl NestedEncode for H256 { } } -impl TopEncode for H256 { +impl TopEncode for HeapH256 { fn top_encode_or_handle_err(&self, output: O, _h: H) -> Result<(), H::HandledErr> where O: TopEncodeOutput, @@ -181,19 +181,19 @@ impl TopEncode for H256 { } } -impl NestedDecode for H256 { +impl NestedDecode for HeapH256 { fn dep_decode_or_handle_err(input: &mut I, h: H) -> Result where I: NestedDecodeInput, H: DecodeErrorHandler, { - let mut res = H256::zero(); + let mut res = HeapH256::zero(); input.read_into(res.as_mut(), h)?; Ok(res) } } -impl H256 { +impl HeapH256 { // Transmutes directly from a (variable-sized) boxed byte slice. // Will exit early if the input length is not 32. // Designed to be used especially in deserializer implementations. @@ -207,14 +207,14 @@ impl H256 { if input.len() == 32 { let raw = Box::into_raw(input); let array_box = unsafe { Box::<[u8; 32]>::from_raw(raw as *mut [u8; 32]) }; - Ok(H256(array_box)) + Ok(HeapH256(array_box)) } else { Err(h.handle_error(DecodeError::from(ERR_BAD_H256_LENGTH))) } } } -impl TopDecode for H256 { +impl TopDecode for HeapH256 { fn top_decode_or_handle_err(input: I, h: H) -> Result where I: TopDecodeInput, @@ -224,19 +224,19 @@ impl TopDecode for H256 { } } -impl TypeAbiFrom for H256 {} +// impl TypeAbiFrom for HeapH256 {} -impl TypeAbi for H256 { - type Unmanaged = Self; +// impl TypeAbi for HeapH256 { +// type Unmanaged = Self; - fn type_name() -> TypeName { - "H256".into() - } +// fn type_name() -> TypeName { +// "H256".into() +// } - fn type_name_rust() -> TypeName { - "H256".into() - } -} +// fn type_name_rust() -> TypeName { +// "H256".into() +// } +// } #[cfg(test)] mod h256_tests { @@ -246,13 +246,13 @@ mod h256_tests { #[test] fn test_h256_from_array() { - let addr = H256::from([4u8; 32]); + let addr = HeapH256::from([4u8; 32]); check_top_encode_decode(addr, &[4u8; 32]); } #[test] fn test_opt_h256() { - let addr = H256::from([4u8; 32]); + let addr = HeapH256::from([4u8; 32]); let mut expected: Vec = Vec::new(); expected.push(1u8); expected.extend_from_slice(&[4u8; 32]); @@ -261,7 +261,7 @@ mod h256_tests { #[test] fn test_ser_h256_ref() { - let addr = H256::from([4u8; 32]); + let addr = HeapH256::from([4u8; 32]); let expected_bytes: &[u8] = &[4u8; 32 * 3]; let tuple = (&addr, &&&addr, addr.clone()); @@ -271,20 +271,20 @@ mod h256_tests { #[test] fn test_is_zero() { - assert!(H256::zero().is_zero()); + assert!(HeapH256::zero().is_zero()); } #[test] fn test_size_of() { use core::mem::size_of; - assert_eq!(size_of::(), size_of::()); - assert_eq!(size_of::>(), size_of::()); + assert_eq!(size_of::(), size_of::()); + assert_eq!(size_of::>(), size_of::()); } #[test] fn test_into_boxed_bytes() { let array = b"32_bytes________________________"; - let h256 = H256::from(array); + let h256 = HeapH256::from(array); let bb = h256.into_boxed_bytes(); assert_eq!(bb.as_slice(), &array[..]); } diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 78c36567af..95025b7eee 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -34,5 +34,9 @@ rand = { version= "0.8.5", optional = true } rand_seeder = "0.3.0" ed25519-dalek = "2.1.0" +[dependencies.multiversx-chain-vm-core] +version = "=0.10.0" +path = "../vm-core" + [dependencies.multiversx-chain-vm-executor] version = "0.2.0" diff --git a/vm/src/lib.rs b/vm/src/lib.rs index 48f9c288e1..a991128d6e 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -14,5 +14,8 @@ pub use world_mock::BlockchainMock; // Re-exporting the executor, for convenience. pub use multiversx_chain_vm_executor as executor; +// Re-exporting the VM-core, for convenience. +pub use multiversx_chain_vm_core as vm_core; + #[macro_use] extern crate alloc; diff --git a/vm/src/types.rs b/vm/src/types.rs index d1ada0c0f6..931211393c 100644 --- a/vm/src/types.rs +++ b/vm/src/types.rs @@ -1,20 +1,14 @@ -mod vm_address; -mod vm_code_metadata; -mod vm_esdt_local_role; -mod vm_esdt_local_role_flags; -mod vm_h256; -mod vm_token_type; +pub use crate::vm_core::types::Address as VMAddress; +pub use crate::vm_core::types::CodeMetadata as VMCodeMetadata; +pub use crate::vm_core::types::EsdtLocalRole; +pub use crate::vm_core::types::EsdtLocalRoleFlags; +pub use crate::vm_core::types::TokenType as VMTokenType; +pub use crate::vm_core::types::H256; + +pub type RawHandle = i32; use num_bigint::BigUint; use num_traits::Zero; -pub use vm_address::VMAddress; -pub use vm_code_metadata::VMCodeMetadata; -pub use vm_esdt_local_role::EsdtLocalRole; -pub use vm_esdt_local_role_flags::EsdtLocalRoleFlags; -pub use vm_h256::H256; -pub use vm_token_type::VMTokenType; - -pub type RawHandle = i32; pub(crate) fn top_encode_u64(value: u64) -> Vec { top_encode_big_uint(&BigUint::from(value)) diff --git a/vm/src/types/vm_address.rs b/vm/src/types/vm_address.rs deleted file mode 100644 index 75786787cc..0000000000 --- a/vm/src/types/vm_address.rs +++ /dev/null @@ -1,153 +0,0 @@ -use super::H256; - -use core::fmt::Debug; - -const SC_ADDRESS_NUM_LEADING_ZEROS: u8 = 8; - -pub const NUM_INT_CHARACTERS_FOR_ADDRESS: usize = 10; -pub const VM_TYPE_LEN: usize = 2; -pub const DEFAULT_VM_TYPE: &[u8] = &[5, 0]; - -/// Address type being used in the VM only. -/// -/// Its implementation is similar to that of the heap Address in the framework, -/// but we have a separate implementation for the VM, because it is a separate component. -#[derive(Hash, PartialEq, Eq, Clone, Debug)] -pub struct VMAddress(H256); - -impl VMAddress { - pub const fn new(bytes: [u8; 32]) -> Self { - VMAddress(H256::new(bytes)) - } - - pub fn generate_mock_address(creator_address: &[u8], creator_nonce: u64) -> Self { - let mut result = [0x00; 32]; - - result[10] = 0x11; - result[11] = 0x11; - result[12] = 0x11; - result[13] = 0x11; - - result[14..29].copy_from_slice(&creator_address[..15]); - result[29] = creator_nonce as u8; - result[30..].copy_from_slice(&creator_address[30..]); - - let start_index = NUM_INT_CHARACTERS_FOR_ADDRESS - VM_TYPE_LEN; - result[start_index..(start_index + DEFAULT_VM_TYPE.len())].copy_from_slice(DEFAULT_VM_TYPE); - - VMAddress::from(result) - } -} - -impl From for VMAddress { - fn from(hash: H256) -> Self { - VMAddress(hash) - } -} - -impl From for H256 { - fn from(address: VMAddress) -> Self { - address.0 - } -} - -impl<'a> From<&'a VMAddress> for &'a H256 { - fn from(address: &'a VMAddress) -> Self { - &address.0 - } -} - -impl From<[u8; 32]> for VMAddress { - fn from(arr: [u8; 32]) -> Self { - VMAddress(H256::from(arr)) - } -} - -impl From<&[u8; 32]> for VMAddress { - fn from(bytes: &[u8; 32]) -> Self { - VMAddress(H256::from(bytes)) - } -} - -impl From<&mut [u8; 32]> for VMAddress { - fn from(bytes: &mut [u8; 32]) -> Self { - VMAddress(H256::from(bytes)) - } -} - -impl From> for VMAddress { - fn from(bytes: Box<[u8; 32]>) -> Self { - VMAddress(H256::from(bytes)) - } -} - -impl VMAddress { - pub fn from_slice(slice: &[u8]) -> Self { - VMAddress(H256::from_slice(slice)) - } -} - -impl From for [u8; 32] { - fn from(addr: VMAddress) -> Self { - addr.0.into() - } -} - -impl AsRef<[u8]> for VMAddress { - fn as_ref(&self) -> &[u8] { - self.0.as_ref() - } -} - -impl AsMut<[u8]> for VMAddress { - fn as_mut(&mut self) -> &mut [u8] { - self.0.as_mut() - } -} - -impl VMAddress { - /// Returns a new address of 32 zeros. - /// Allocates directly in heap. - /// Minimal resulting wasm code (14 bytes if not inlined). - pub fn zero() -> Self { - VMAddress(H256::zero()) - } - - /// Extracts a byte slice containing the entire fixed hash. - pub fn as_bytes(&self) -> &[u8] { - self.0.as_bytes() - } - - pub fn as_array(&self) -> &[u8; 32] { - self.0.as_array() - } - - pub fn to_vec(&self) -> Vec { - self.0.to_vec() - } - - pub fn is_smart_contract_address(&self) -> bool { - self.as_bytes() - .iter() - .take(SC_ADDRESS_NUM_LEADING_ZEROS.into()) - .all(|item| item == &0u8) - } -} - -#[cfg(test)] -mod tests { - use crate::{display_util::address_hex, types::VMAddress}; - - #[test] - fn generate_mock_address_test() { - let creator_address = VMAddress::new([ - 111, 119, 110, 101, 114, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, - 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, - ]); - let mock_address = VMAddress::generate_mock_address(&creator_address.to_vec(), 1u64); - assert_eq!( - address_hex(&mock_address), - "0x00000000000000000500111111116f776e65725f5f5f5f5f5f5f5f5f5f015f5f" - ); - } -} diff --git a/vm/src/types/vm_code_metadata.rs b/vm/src/types/vm_code_metadata.rs deleted file mode 100644 index 5073d9ffe1..0000000000 --- a/vm/src/types/vm_code_metadata.rs +++ /dev/null @@ -1,130 +0,0 @@ -#![allow(clippy::bad_bit_mask)] - -use bitflags::bitflags; - -bitflags! { - #[derive(Default, PartialEq, Debug, Clone, Copy)] - pub struct VMCodeMetadata: u16 { - const DEFAULT = 0; - const UPGRADEABLE = 0b0000_0001_0000_0000; // LSB of first byte - const READABLE = 0b0000_0100_0000_0000; // 3rd LSB of first byte - const PAYABLE = 0b0000_0000_0000_0010; // 2nd LSB of second byte - const PAYABLE_BY_SC = 0b0000_0000_0000_0100; // 3rd LSB of second byte - } -} - -impl VMCodeMetadata { - pub fn is_upgradeable(&self) -> bool { - *self & VMCodeMetadata::UPGRADEABLE != VMCodeMetadata::DEFAULT - } - - pub fn is_payable(&self) -> bool { - *self & VMCodeMetadata::PAYABLE != VMCodeMetadata::DEFAULT - } - - pub fn is_payable_by_sc(&self) -> bool { - *self & VMCodeMetadata::PAYABLE_BY_SC != VMCodeMetadata::DEFAULT - } - - pub fn is_readable(&self) -> bool { - *self & VMCodeMetadata::READABLE != VMCodeMetadata::DEFAULT - } - - pub fn to_byte_array(&self) -> [u8; 2] { - self.bits().to_be_bytes() - } - - pub fn to_vec(&self) -> Vec { - self.to_byte_array().to_vec() - } -} - -impl From<[u8; 2]> for VMCodeMetadata { - fn from(arr: [u8; 2]) -> Self { - VMCodeMetadata::from(u16::from_be_bytes(arr)) - } -} - -impl From<&[u8]> for VMCodeMetadata { - fn from(slice: &[u8]) -> Self { - let arr: [u8; 2] = slice.try_into().unwrap_or_default(); - VMCodeMetadata::from(arr) - } -} - -impl From<&Vec> for VMCodeMetadata { - fn from(v: &Vec) -> Self { - VMCodeMetadata::from(v.as_slice()) - } -} - -impl From for VMCodeMetadata { - #[inline] - fn from(value: u16) -> Self { - VMCodeMetadata::from_bits_truncate(value) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_default() { - assert!(!VMCodeMetadata::DEFAULT.is_upgradeable()); - assert!(!VMCodeMetadata::DEFAULT.is_payable()); - assert!(!VMCodeMetadata::DEFAULT.is_readable()); - } - - #[test] - fn test_all() { - let all = VMCodeMetadata::UPGRADEABLE - | VMCodeMetadata::PAYABLE - | VMCodeMetadata::PAYABLE_BY_SC - | VMCodeMetadata::READABLE; - assert!(all.is_upgradeable()); - assert!(all.is_payable()); - assert!(all.is_payable_by_sc()); - assert!(all.is_readable()); - - assert_eq!(all.bits(), 0x0506); - - assert_eq!(VMCodeMetadata::from_bits_truncate(0xffff), all); - } - - #[test] - fn test_each() { - assert!(VMCodeMetadata::UPGRADEABLE.is_upgradeable()); - assert!(!VMCodeMetadata::PAYABLE.is_upgradeable()); - assert!(!VMCodeMetadata::PAYABLE_BY_SC.is_upgradeable()); - assert!(!VMCodeMetadata::READABLE.is_upgradeable()); - - assert!(!VMCodeMetadata::UPGRADEABLE.is_payable()); - assert!(VMCodeMetadata::PAYABLE.is_payable()); - assert!(!VMCodeMetadata::PAYABLE_BY_SC.is_payable()); - assert!(!VMCodeMetadata::READABLE.is_payable()); - - assert!(!VMCodeMetadata::UPGRADEABLE.is_payable_by_sc()); - assert!(!VMCodeMetadata::PAYABLE.is_payable_by_sc()); - assert!(VMCodeMetadata::PAYABLE_BY_SC.is_payable_by_sc()); - assert!(!VMCodeMetadata::READABLE.is_payable_by_sc()); - - assert!(!VMCodeMetadata::UPGRADEABLE.is_readable()); - assert!(!VMCodeMetadata::PAYABLE.is_readable()); - assert!(!VMCodeMetadata::PAYABLE_BY_SC.is_readable()); - assert!(VMCodeMetadata::READABLE.is_readable()); - } - - /// Translated from vm-wasm. - #[test] - fn test_from_array() { - assert!(VMCodeMetadata::from([1, 0]).is_upgradeable()); - assert!(!VMCodeMetadata::from([1, 0]).is_readable()); - assert!(VMCodeMetadata::from([0, 2]).is_payable()); - assert!(VMCodeMetadata::from([4, 0]).is_readable()); - assert!(!VMCodeMetadata::from([4, 0]).is_upgradeable()); - assert!(!VMCodeMetadata::from([0, 0]).is_upgradeable()); - assert!(!VMCodeMetadata::from([0, 0]).is_payable()); - assert!(!VMCodeMetadata::from([0, 0]).is_readable()); - } -} diff --git a/vm/src/types/vm_esdt_local_role_flags.rs b/vm/src/types/vm_esdt_local_role_flags.rs deleted file mode 100644 index 2b47879ec8..0000000000 --- a/vm/src/types/vm_esdt_local_role_flags.rs +++ /dev/null @@ -1,73 +0,0 @@ -#![allow(clippy::bad_bit_mask)] - -use super::EsdtLocalRole; -use bitflags::bitflags; - -bitflags! { - /// The VM implementation for EsdtLocalRoleFlags, used internally in builtin functions. - /// - /// There is another near-identical implementation in the framework, used for communicating with the VM. - /// - /// It might be a good idea to move it to some "common ground" crate, between the framework and the VM. - #[derive(PartialEq, Clone, Copy)] - pub struct EsdtLocalRoleFlags: u64 { - const NONE = 0b00000000; - const MINT = 0b00000001; - const BURN = 0b00000010; - const NFT_CREATE = 0b00000100; - const NFT_ADD_QUANTITY = 0b00001000; - const NFT_BURN = 0b00010000; - const NFT_ADD_URI = 0b00100000; - const NFT_UPDATE_ATTRIBUTES = 0b01000000; - const TRANSFER = 0b10000000; - } -} - -impl EsdtLocalRoleFlags { - pub fn has_role(&self, role: &EsdtLocalRole) -> bool { - *self & role.to_flag() != EsdtLocalRoleFlags::NONE - } - - pub fn iter_roles(&self) -> impl Iterator { - EsdtLocalRole::iter_all().filter(move |role| self.has_role(role)) - } -} - -#[cfg(test)] -pub mod tests { - use super::*; - use alloc::vec::Vec; - - #[test] - fn test_flags_has_role() { - let flags = EsdtLocalRoleFlags::MINT; - assert!(flags.has_role(&EsdtLocalRole::Mint)); - let flags = EsdtLocalRoleFlags::MINT | EsdtLocalRoleFlags::BURN; - assert!(flags.has_role(&EsdtLocalRole::Mint)); - let flags = EsdtLocalRoleFlags::NONE; - assert!(!flags.has_role(&EsdtLocalRole::Mint)); - let flags = EsdtLocalRoleFlags::BURN; - assert!(!flags.has_role(&EsdtLocalRole::Mint)); - } - - #[test] - fn test_flags_iter_role() { - let flags = EsdtLocalRoleFlags::MINT; - assert_eq!( - flags.iter_roles().collect::>(), - alloc::vec![&EsdtLocalRole::Mint], - ); - - let flags = EsdtLocalRoleFlags::MINT | EsdtLocalRoleFlags::BURN; - assert_eq!( - flags.iter_roles().collect::>(), - alloc::vec![&EsdtLocalRole::Mint, &EsdtLocalRole::Burn], - ); - - let flags = EsdtLocalRoleFlags::NONE; - assert_eq!( - flags.iter_roles().collect::>(), - Vec::<&EsdtLocalRole>::new(), - ); - } -} diff --git a/vm/src/types/vm_h256.rs b/vm/src/types/vm_h256.rs deleted file mode 100644 index 58e8dd7eda..0000000000 --- a/vm/src/types/vm_h256.rs +++ /dev/null @@ -1,108 +0,0 @@ -use core::fmt::Debug; - -// const ERR_BAD_H256_LENGTH: &str = "bad H256 length"; -const ZERO_32: &[u8] = &[0u8; 32]; - -/// Type that holds 32 bytes of data. -/// Data is kept on the heap to keep wasm size low and avoid copies. -#[derive(Hash, PartialEq, Eq, Clone, Debug)] -pub struct H256([u8; 32]); - -impl H256 { - pub const fn new(bytes: [u8; 32]) -> Self { - H256(bytes) - } -} - -impl From<[u8; 32]> for H256 { - /// Constructs a hash type from the given bytes array of fixed length. - /// - /// # Note - /// - /// The given bytes are interpreted in big endian order. - fn from(arr: [u8; 32]) -> Self { - H256(arr) - } -} - -impl<'a> From<&'a [u8; 32]> for H256 { - /// Constructs a hash type from the given reference - /// to the bytes array of fixed length. - /// - /// # Note - /// - /// The given bytes are interpreted in big endian order. - fn from(bytes: &'a [u8; 32]) -> Self { - H256(*bytes) - } -} - -impl<'a> From<&'a mut [u8; 32]> for H256 { - /// Constructs a hash type from the given reference - /// to the mutable bytes array of fixed length. - /// - /// # Note - /// - /// The given bytes are interpreted in big endian order. - fn from(bytes: &'a mut [u8; 32]) -> Self { - H256(*bytes) - } -} - -impl From> for H256 { - fn from(bytes: Box<[u8; 32]>) -> Self { - H256(*bytes) - } -} - -impl H256 { - pub fn from_slice(slice: &[u8]) -> Self { - let mut arr = [0u8; 32]; - let len = core::cmp::min(slice.len(), 32); - arr[..len].copy_from_slice(&slice[..len]); - H256(arr) - } -} - -impl From for [u8; 32] { - fn from(s: H256) -> Self { - s.0 - } -} - -impl AsRef<[u8]> for H256 { - fn as_ref(&self) -> &[u8] { - self.as_bytes() - } -} - -impl AsMut<[u8]> for H256 { - fn as_mut(&mut self) -> &mut [u8] { - self.0.as_mut() - } -} - -impl H256 { - /// Returns a new zero-initialized fixed hash. - pub fn zero() -> Self { - H256([0u8; 32]) - } - - /// Extracts a byte slice containing the entire fixed hash. - pub fn as_bytes(&self) -> &[u8] { - self.0.as_ref() - } - - pub fn as_array(&self) -> &[u8; 32] { - &self.0 - } - - pub fn to_vec(&self) -> Vec { - self.0[..].to_vec() - } - - /// True if all 32 bytes of the hash are zero. - pub fn is_zero(&self) -> bool { - self.as_bytes() == ZERO_32 - } -} From 167d9b9689998a83d1359fce7ba1f4fb40a9bc54 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 25 Sep 2024 00:49:49 +0300 Subject: [PATCH 16/30] cleanup --- vm-core/src/types/address.rs | 7 ------- vm-core/src/types/heap_address.rs | 14 -------------- vm-core/src/types/heap_h256.rs | 14 -------------- 3 files changed, 35 deletions(-) diff --git a/vm-core/src/types/address.rs b/vm-core/src/types/address.rs index e97ee5934a..0f5099fddb 100644 --- a/vm-core/src/types/address.rs +++ b/vm-core/src/types/address.rs @@ -168,13 +168,6 @@ impl Address { self.0.is_zero() } - // /// Transmutes self to an (in principle) variable length boxed bytes object. - // /// Both BoxedBytes and H256 keep the data on the heap, so only the pointer to that data needs to be transmuted. - // /// Does not reallocate or copy data, the data on the heap remains untouched. - // pub fn into_boxed_bytes(self) -> BoxedBytes { - // self.0.into_boxed_bytes() - // } - pub fn is_smart_contract_address(&self) -> bool { self.as_bytes() .iter() diff --git a/vm-core/src/types/heap_address.rs b/vm-core/src/types/heap_address.rs index 467f3c469e..57e053e815 100644 --- a/vm-core/src/types/heap_address.rs +++ b/vm-core/src/types/heap_address.rs @@ -198,20 +198,6 @@ impl TopDecode for HeapAddress { } } -// impl TypeAbiFrom for HeapAddress {} - -// impl TypeAbi for HeapAddress { -// type Unmanaged = Self; - -// fn type_name() -> TypeName { -// "Address".into() -// } - -// fn type_name_rust() -> TypeName { -// "Address".into() -// } -// } - #[cfg(test)] mod address_tests { use super::*; diff --git a/vm-core/src/types/heap_h256.rs b/vm-core/src/types/heap_h256.rs index 728b81f5a1..80f5a54f67 100644 --- a/vm-core/src/types/heap_h256.rs +++ b/vm-core/src/types/heap_h256.rs @@ -224,20 +224,6 @@ impl TopDecode for HeapH256 { } } -// impl TypeAbiFrom for HeapH256 {} - -// impl TypeAbi for HeapH256 { -// type Unmanaged = Self; - -// fn type_name() -> TypeName { -// "H256".into() -// } - -// fn type_name_rust() -> TypeName { -// "H256".into() -// } -// } - #[cfg(test)] mod h256_tests { use super::*; From a93e4c6ba9366e26580782568fde44b63a487f1b Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 25 Sep 2024 01:06:46 +0300 Subject: [PATCH 17/30] lldb formatter fix --- .../pretty-printers/multiversx_sc_lldb_pretty_printers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/rust-debugger/pretty-printers/multiversx_sc_lldb_pretty_printers.py b/tools/rust-debugger/pretty-printers/multiversx_sc_lldb_pretty_printers.py index 1232201e28..bb67d11d43 100644 --- a/tools/rust-debugger/pretty-printers/multiversx_sc_lldb_pretty_printers.py +++ b/tools/rust-debugger/pretty-printers/multiversx_sc_lldb_pretty_printers.py @@ -44,10 +44,10 @@ # 4. SC wasm - Managed multi value types -# 5. SC wasm - heap -MOD_PATH = "multiversx_sc::types::heap" +# 5. VM core types +MOD_PATH = "multiversx_chain_vm_core::types" -HEAP_ADDRESS_TYPE = f"{MOD_PATH}::h256_address::Address" +HEAP_ADDRESS_TYPE = f"{MOD_PATH}::address::Address" BOXED_BYTES_TYPE = f"{MOD_PATH}::boxed_bytes::BoxedBytes" # 6. MultiversX codec - Multi-types From d3cedb0c877160c908d85915005b267ca2937dac Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 25 Sep 2024 01:09:35 +0300 Subject: [PATCH 18/30] no_std fix --- vm-core/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vm-core/src/lib.rs b/vm-core/src/lib.rs index 937a07d6d8..6a3f022106 100644 --- a/vm-core/src/lib.rs +++ b/vm-core/src/lib.rs @@ -1,3 +1,5 @@ +#![no_std] + pub mod types; extern crate alloc; From 24b644144f4669c6b9e56df4b1e52f937d27c756 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 25 Sep 2024 10:29:44 +0300 Subject: [PATCH 19/30] vm-core builtin func names --- framework/base/src/api.rs | 2 +- .../base/src/types/interaction/system_proxy.rs | 1 - .../system_proxy/builtin_func_proxy.rs | 2 +- .../src}/builtin_func_names.rs | 0 vm-core/src/lib.rs | 6 ++++-- vm/src/tx_execution/builtin_function_mocks.rs | 2 -- .../builtin_func_container.rs | 4 +++- .../esdt_nft/esdt_local_burn.rs | 3 ++- .../esdt_nft/esdt_local_mint.rs | 4 ++-- .../esdt_nft/esdt_nft_add_quantity_mock.rs | 7 ++----- .../esdt_nft/esdt_nft_add_uri_mock.rs | 3 ++- .../esdt_nft/esdt_nft_burn_mock.rs | 3 ++- .../esdt_nft/esdt_nft_create_mock.rs | 3 ++- .../esdt_nft/esdt_nft_update_attriutes_mock.rs | 3 ++- .../general/change_owner_mock.rs | 6 ++---- .../general/claim_developer_rewards_mock.rs | 9 +++++---- .../general/delete_username_mock.rs | 8 +++++--- .../general/migrate_username_mock.rs | 4 ++-- .../general/set_username_mock.rs | 4 ++-- .../general/upgrade_contract.rs | 6 ++---- .../transfer/esdt_multi_transfer_mock.rs | 3 ++- .../transfer/esdt_nft_transfer_mock.rs | 6 ++---- .../transfer/esdt_transfer_mock.rs | 6 ++---- .../vm_builtin_function_names.rs | 16 ---------------- vm/src/vm_hooks/vh_handler/vh_blockchain.rs | 2 +- vm/src/vm_hooks/vh_handler/vh_send.rs | 6 +++--- 26 files changed, 51 insertions(+), 68 deletions(-) rename {framework/base/src/types/interaction/system_proxy => vm-core/src}/builtin_func_names.rs (100%) delete mode 100644 vm/src/tx_execution/builtin_function_mocks/vm_builtin_function_names.rs diff --git a/framework/base/src/api.rs b/framework/base/src/api.rs index 094522d1ac..827929972b 100644 --- a/framework/base/src/api.rs +++ b/framework/base/src/api.rs @@ -30,4 +30,4 @@ pub use storage_api::*; pub use vm_api::VMApi; // Backwards compatibility. -pub use crate::types::system_proxy::builtin_func_names::*; +pub use crate::vm_core::builtin_func_names::*; diff --git a/framework/base/src/types/interaction/system_proxy.rs b/framework/base/src/types/interaction/system_proxy.rs index 3b1fb836d6..e86c9f32ff 100644 --- a/framework/base/src/types/interaction/system_proxy.rs +++ b/framework/base/src/types/interaction/system_proxy.rs @@ -1,4 +1,3 @@ -pub mod builtin_func_names; mod builtin_func_proxy; mod esdt_system_sc_proxy; mod legacy_system_sc_proxy; diff --git a/framework/base/src/types/interaction/system_proxy/builtin_func_proxy.rs b/framework/base/src/types/interaction/system_proxy/builtin_func_proxy.rs index 6755dbbd03..cdb03b369b 100644 --- a/framework/base/src/types/interaction/system_proxy/builtin_func_proxy.rs +++ b/framework/base/src/types/interaction/system_proxy/builtin_func_proxy.rs @@ -5,7 +5,7 @@ use crate::types::{ TxEnv, TxFrom, TxGas, TxProxyTrait, TxTo, TxTypedCall, }; -use super::builtin_func_names::{ +use crate::vm_core::builtin_func_names::{ CHANGE_OWNER_BUILTIN_FUNC_NAME, CLAIM_DEVELOPER_REWARDS_FUNC_NAME, DELETE_USERNAME_FUNC_NAME, ESDT_LOCAL_BURN_FUNC_NAME, ESDT_LOCAL_MINT_FUNC_NAME, ESDT_NFT_ADD_QUANTITY_FUNC_NAME, ESDT_NFT_ADD_URI_FUNC_NAME, ESDT_NFT_BURN_FUNC_NAME, ESDT_NFT_CREATE_FUNC_NAME, diff --git a/framework/base/src/types/interaction/system_proxy/builtin_func_names.rs b/vm-core/src/builtin_func_names.rs similarity index 100% rename from framework/base/src/types/interaction/system_proxy/builtin_func_names.rs rename to vm-core/src/builtin_func_names.rs diff --git a/vm-core/src/lib.rs b/vm-core/src/lib.rs index 6a3f022106..503f690d82 100644 --- a/vm-core/src/lib.rs +++ b/vm-core/src/lib.rs @@ -1,7 +1,9 @@ #![no_std] -pub mod types; - extern crate alloc; +pub mod builtin_func_names; +pub mod types; + +/// Re-exported for convenience. pub use multiversx_sc_codec as codec; diff --git a/vm/src/tx_execution/builtin_function_mocks.rs b/vm/src/tx_execution/builtin_function_mocks.rs index dff42101cc..d50044d0c1 100644 --- a/vm/src/tx_execution/builtin_function_mocks.rs +++ b/vm/src/tx_execution/builtin_function_mocks.rs @@ -3,8 +3,6 @@ mod builtin_func_trait; mod esdt_nft; mod general; mod transfer; -pub mod vm_builtin_function_names; pub use builtin_func_container::BuiltinFunctionContainer; pub use builtin_func_trait::{BuiltinFunction, BuiltinFunctionEsdtTransferInfo}; -pub use vm_builtin_function_names as builtin_function_names; diff --git a/vm/src/tx_execution/builtin_function_mocks/builtin_func_container.rs b/vm/src/tx_execution/builtin_function_mocks/builtin_func_container.rs index 8b5a302377..369a1e50df 100644 --- a/vm/src/tx_execution/builtin_function_mocks/builtin_func_container.rs +++ b/vm/src/tx_execution/builtin_function_mocks/builtin_func_container.rs @@ -1,6 +1,5 @@ use super::{ builtin_func_trait::BuiltinFunction, - builtin_function_names::*, esdt_nft::{ ESDTLocalBurn, ESDTLocalMint, ESDTNftAddQuantity, ESDTNftAddUri, ESDTNftBurn, ESDTNftCreate, ESDTNftUpdateAttributes, @@ -9,12 +8,15 @@ use super::{ transfer::{ESDTMultiTransfer, ESDTNftTransfer, ESDTTransfer}, BuiltinFunctionEsdtTransferInfo, }; + use crate::{ tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}, types::EsdtLocalRole, }; +use crate::vm_core::builtin_func_names::*; + /// Container for builtin function logic. /// /// Currently has no data, but could conceivably be configurable in the future. diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_burn.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_burn.rs index 88d84f5681..12053b7506 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_burn.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_burn.rs @@ -1,8 +1,9 @@ use num_bigint::BigUint; use crate::{ - tx_execution::{builtin_function_names::ESDT_LOCAL_BURN_FUNC_NAME, BlockchainVMRef}, + tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, + vm_core::builtin_func_names::ESDT_LOCAL_BURN_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_mint.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_mint.rs index 73f35aef69..e1026dc19f 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_mint.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_mint.rs @@ -1,9 +1,9 @@ use num_bigint::BigUint; -use crate::tx_execution::{builtin_function_names::ESDT_LOCAL_MINT_FUNC_NAME, BlockchainVMRef}; - use crate::{ + tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, + vm_core::builtin_func_names::ESDT_LOCAL_MINT_FUNC_NAME, world_mock::EsdtInstanceMetadata, }; diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_quantity_mock.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_quantity_mock.rs index f462d64ef3..d32f929bd7 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_quantity_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_quantity_mock.rs @@ -2,12 +2,9 @@ use num_bigint::BigUint; use crate::{ tx_execution::BlockchainVMRef, - types::{top_decode_u64, top_encode_u64}, -}; - -use crate::{ - tx_execution::builtin_function_names::ESDT_NFT_ADD_QUANTITY_FUNC_NAME, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, + types::{top_decode_u64, top_encode_u64}, + vm_core::builtin_func_names::ESDT_NFT_ADD_QUANTITY_FUNC_NAME, world_mock::EsdtInstanceMetadata, }; diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_uri_mock.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_uri_mock.rs index 19966902b4..030dae16db 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_uri_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_uri_mock.rs @@ -1,7 +1,8 @@ use crate::{ - tx_execution::{builtin_function_names::ESDT_NFT_ADD_URI_FUNC_NAME, BlockchainVMRef}, + tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, types::{top_decode_u64, top_encode_u64}, + vm_core::builtin_func_names::ESDT_NFT_ADD_URI_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_burn_mock.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_burn_mock.rs index 26ffe468a6..8ad98cbc87 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_burn_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_burn_mock.rs @@ -1,9 +1,10 @@ use num_bigint::BigUint; use crate::{ - tx_execution::{builtin_function_names::ESDT_NFT_BURN_FUNC_NAME, BlockchainVMRef}, + tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, types::{top_decode_u64, top_encode_u64}, + vm_core::builtin_func_names::ESDT_NFT_BURN_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_create_mock.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_create_mock.rs index 2f538c9e67..f94c39a4b4 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_create_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_create_mock.rs @@ -1,9 +1,10 @@ use num_bigint::BigUint; use crate::{ - tx_execution::{builtin_function_names::ESDT_NFT_CREATE_FUNC_NAME, BlockchainVMRef}, + tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, types::{top_decode_u64, top_encode_u64}, + vm_core::builtin_func_names::ESDT_NFT_CREATE_FUNC_NAME, world_mock::{EsdtInstance, EsdtInstanceMetadata}, }; diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_update_attriutes_mock.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_update_attriutes_mock.rs index 49c2629cfb..defc3b8f4f 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_update_attriutes_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_update_attriutes_mock.rs @@ -1,7 +1,8 @@ use crate::{ - tx_execution::{builtin_function_names::ESDT_NFT_UPDATE_ATTRIBUTES_FUNC_NAME, BlockchainVMRef}, + tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, types::{top_decode_u64, top_encode_u64}, + vm_core::builtin_func_names::ESDT_NFT_UPDATE_ATTRIBUTES_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/general/change_owner_mock.rs b/vm/src/tx_execution/builtin_function_mocks/general/change_owner_mock.rs index 8352b606cd..c7128b3b4f 100644 --- a/vm/src/tx_execution/builtin_function_mocks/general/change_owner_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/general/change_owner_mock.rs @@ -1,10 +1,8 @@ -use crate::tx_execution::{ - builtin_function_names::CHANGE_OWNER_BUILTIN_FUNC_NAME, BlockchainVMRef, -}; - use crate::{ + tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}, types::VMAddress, + vm_core::builtin_func_names::CHANGE_OWNER_BUILTIN_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/general/claim_developer_rewards_mock.rs b/vm/src/tx_execution/builtin_function_mocks/general/claim_developer_rewards_mock.rs index 0b7e9c694f..08adbd0306 100644 --- a/vm/src/tx_execution/builtin_function_mocks/general/claim_developer_rewards_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/general/claim_developer_rewards_mock.rs @@ -1,10 +1,11 @@ -use crate::tx_execution::{ - builtin_function_names::CLAIM_DEVELOPER_REWARDS_FUNC_NAME, BlockchainVMRef, -}; use num_bigint::BigUint; use num_traits::Zero; -use crate::tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}; +use crate::{ + tx_execution::BlockchainVMRef, + tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}, + vm_core::builtin_func_names::CLAIM_DEVELOPER_REWARDS_FUNC_NAME, +}; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/general/delete_username_mock.rs b/vm/src/tx_execution/builtin_function_mocks/general/delete_username_mock.rs index c697f76859..5661e7c339 100644 --- a/vm/src/tx_execution/builtin_function_mocks/general/delete_username_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/general/delete_username_mock.rs @@ -1,6 +1,8 @@ -use crate::tx_execution::{builtin_function_names::DELETE_USERNAME_FUNC_NAME, BlockchainVMRef}; - -use crate::tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}; +use crate::{ + tx_execution::BlockchainVMRef, + tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}, + vm_core::builtin_func_names::DELETE_USERNAME_FUNC_NAME, +}; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/general/migrate_username_mock.rs b/vm/src/tx_execution/builtin_function_mocks/general/migrate_username_mock.rs index ea7c5ec57a..5ca3ef0ad5 100644 --- a/vm/src/tx_execution/builtin_function_mocks/general/migrate_username_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/general/migrate_username_mock.rs @@ -1,6 +1,6 @@ -use crate::tx_execution::{builtin_function_names::MIGRATE_USERNAME_FUNC_NAME, BlockchainVMRef}; - +use crate::tx_execution::BlockchainVMRef; use crate::tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}; +use crate::vm_core::builtin_func_names::MIGRATE_USERNAME_FUNC_NAME; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/general/set_username_mock.rs b/vm/src/tx_execution/builtin_function_mocks/general/set_username_mock.rs index 8df9d6316d..925d5a1ef4 100644 --- a/vm/src/tx_execution/builtin_function_mocks/general/set_username_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/general/set_username_mock.rs @@ -1,6 +1,6 @@ -use crate::tx_execution::{builtin_function_names::SET_USERNAME_FUNC_NAME, BlockchainVMRef}; - +use crate::tx_execution::BlockchainVMRef; use crate::tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}; +use crate::vm_core::builtin_func_names::SET_USERNAME_FUNC_NAME; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/general/upgrade_contract.rs b/vm/src/tx_execution/builtin_function_mocks/general/upgrade_contract.rs index 7de2032273..eb106e810a 100644 --- a/vm/src/tx_execution/builtin_function_mocks/general/upgrade_contract.rs +++ b/vm/src/tx_execution/builtin_function_mocks/general/upgrade_contract.rs @@ -1,10 +1,8 @@ -use crate::tx_execution::{ - builtin_function_names::UPGRADE_CONTRACT_FUNC_NAME, create_transfer_value_log, BlockchainVMRef, -}; - use crate::{ + tx_execution::{create_transfer_value_log, BlockchainVMRef}, tx_mock::{BlockchainUpdate, CallType, TxCache, TxFunctionName, TxInput, TxResult}, types::VMCodeMetadata, + vm_core::builtin_func_names::UPGRADE_CONTRACT_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_multi_transfer_mock.rs b/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_multi_transfer_mock.rs index 183819f372..40eef80053 100644 --- a/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_multi_transfer_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_multi_transfer_mock.rs @@ -1,7 +1,8 @@ use crate::{ - tx_execution::{builtin_function_names::ESDT_MULTI_TRANSFER_FUNC_NAME, BlockchainVMRef}, + tx_execution::BlockchainVMRef, tx_mock::TxLog, types::{top_decode_u64, top_encode_u64}, + vm_core::builtin_func_names::ESDT_MULTI_TRANSFER_FUNC_NAME, }; use crate::{ diff --git a/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_nft_transfer_mock.rs b/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_nft_transfer_mock.rs index 24d5925662..8f5be1a372 100644 --- a/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_nft_transfer_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_nft_transfer_mock.rs @@ -1,10 +1,8 @@ use crate::{ - tx_execution::{ - builtin_function_names::ESDT_NFT_TRANSFER_FUNC_NAME, BlockchainVMRef, - BuiltinFunctionEsdtTransferInfo, - }, + tx_execution::{BlockchainVMRef, BuiltinFunctionEsdtTransferInfo}, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, types::VMAddress, + vm_core::builtin_func_names::ESDT_NFT_TRANSFER_FUNC_NAME, }; use super::{ diff --git a/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_transfer_mock.rs b/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_transfer_mock.rs index 8f5a912a2a..8a5efdb8b0 100644 --- a/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_transfer_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_transfer_mock.rs @@ -1,9 +1,7 @@ use crate::{ - tx_execution::{ - builtin_function_names::ESDT_TRANSFER_FUNC_NAME, BlockchainVMRef, - BuiltinFunctionEsdtTransferInfo, - }, + tx_execution::{BlockchainVMRef, BuiltinFunctionEsdtTransferInfo}, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, + vm_core::builtin_func_names::ESDT_TRANSFER_FUNC_NAME, }; use super::{ diff --git a/vm/src/tx_execution/builtin_function_mocks/vm_builtin_function_names.rs b/vm/src/tx_execution/builtin_function_mocks/vm_builtin_function_names.rs deleted file mode 100644 index 108ed29016..0000000000 --- a/vm/src/tx_execution/builtin_function_mocks/vm_builtin_function_names.rs +++ /dev/null @@ -1,16 +0,0 @@ -pub const ESDT_LOCAL_MINT_FUNC_NAME: &str = "ESDTLocalMint"; -pub const ESDT_LOCAL_BURN_FUNC_NAME: &str = "ESDTLocalBurn"; -pub const ESDT_MULTI_TRANSFER_FUNC_NAME: &str = "MultiESDTNFTTransfer"; -pub const ESDT_NFT_TRANSFER_FUNC_NAME: &str = "ESDTNFTTransfer"; -pub const ESDT_NFT_CREATE_FUNC_NAME: &str = "ESDTNFTCreate"; -pub const ESDT_NFT_ADD_QUANTITY_FUNC_NAME: &str = "ESDTNFTAddQuantity"; -pub const ESDT_NFT_ADD_URI_FUNC_NAME: &str = "ESDTNFTAddURI"; -pub const ESDT_NFT_UPDATE_ATTRIBUTES_FUNC_NAME: &str = "ESDTNFTUpdateAttributes"; -pub const ESDT_NFT_BURN_FUNC_NAME: &str = "ESDTNFTBurn"; -pub const ESDT_TRANSFER_FUNC_NAME: &str = "ESDTTransfer"; -pub const CHANGE_OWNER_BUILTIN_FUNC_NAME: &str = "ChangeOwnerAddress"; -pub const CLAIM_DEVELOPER_REWARDS_FUNC_NAME: &str = "ClaimDeveloperRewards"; -pub const SET_USERNAME_FUNC_NAME: &str = "SetUserName"; -pub const MIGRATE_USERNAME_FUNC_NAME: &str = "migrateUserName"; -pub const DELETE_USERNAME_FUNC_NAME: &str = "DeleteUserName"; -pub const UPGRADE_CONTRACT_FUNC_NAME: &str = "upgradeContract"; diff --git a/vm/src/vm_hooks/vh_handler/vh_blockchain.rs b/vm/src/vm_hooks/vh_handler/vh_blockchain.rs index 43940d4a57..a59854cab3 100644 --- a/vm/src/vm_hooks/vh_handler/vh_blockchain.rs +++ b/vm/src/vm_hooks/vh_handler/vh_blockchain.rs @@ -1,6 +1,6 @@ use crate::{ - tx_execution::vm_builtin_function_names::*, types::{EsdtLocalRole, EsdtLocalRoleFlags, RawHandle, VMAddress}, + vm_core::builtin_func_names::*, vm_hooks::VMHooksHandlerSource, world_mock::{EsdtData, EsdtInstance}, }; diff --git a/vm/src/vm_hooks/vh_handler/vh_send.rs b/vm/src/vm_hooks/vh_handler/vh_send.rs index 4d0584586b..57c0315332 100644 --- a/vm/src/vm_hooks/vh_handler/vh_send.rs +++ b/vm/src/vm_hooks/vh_handler/vh_send.rs @@ -1,10 +1,10 @@ use crate::{ - tx_execution::builtin_function_names::{ + tx_mock::{AsyncCallTxData, Promise, TxFunctionName, TxTokenTransfer}, + types::{top_encode_big_uint, top_encode_u64, RawHandle, VMAddress, VMCodeMetadata}, + vm_core::builtin_func_names::{ ESDT_MULTI_TRANSFER_FUNC_NAME, ESDT_NFT_TRANSFER_FUNC_NAME, ESDT_TRANSFER_FUNC_NAME, UPGRADE_CONTRACT_FUNC_NAME, }, - tx_mock::{AsyncCallTxData, Promise, TxFunctionName, TxTokenTransfer}, - types::{top_encode_big_uint, top_encode_u64, RawHandle, VMAddress, VMCodeMetadata}, vm_hooks::VMHooksHandlerSource, }; use num_traits::Zero; From c88c59dc38dcca6fe9b416badc92eaf3ea4fd68e Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Fri, 27 Sep 2024 11:57:20 +0300 Subject: [PATCH 20/30] wasm-extractor - rename enum PanicReport + max_severity --- framework/meta-lib/src/tools.rs | 1 + framework/meta-lib/src/tools/panic_report.rs | 80 +++++++++++++++++++ .../meta-lib/src/tools/report_creator.rs | 23 +----- .../meta-lib/src/tools/wasm_extractor.rs | 50 +----------- .../meta-lib/src/tools/wasm_extractor_test.rs | 8 +- 5 files changed, 90 insertions(+), 72 deletions(-) create mode 100644 framework/meta-lib/src/tools/panic_report.rs diff --git a/framework/meta-lib/src/tools.rs b/framework/meta-lib/src/tools.rs index 26fe22aaed..9437133224 100644 --- a/framework/meta-lib/src/tools.rs +++ b/framework/meta-lib/src/tools.rs @@ -1,5 +1,6 @@ mod find_workspace; mod git_describe; +pub(crate) mod panic_report; pub(crate) mod report_creator; pub mod twiggy; mod wasm_extractor; diff --git a/framework/meta-lib/src/tools/panic_report.rs b/framework/meta-lib/src/tools/panic_report.rs new file mode 100644 index 0000000000..1be0f7cc56 --- /dev/null +++ b/framework/meta-lib/src/tools/panic_report.rs @@ -0,0 +1,80 @@ +use std::fmt::Display; + +use wasmparser::DataSectionReader; +const PANIC_WITH_MESSAGE: &[u8; 16] = b"panic occurred: "; +const PANIC_WITHOUT_MESSAGE: &[u8; 14] = b"panic occurred"; + +#[derive(Default, PartialEq, Clone)] +pub enum PanicReport { + #[default] + None, + WithoutMessage, + WithMessage, +} + +impl Display for PanicReport { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let panic_status = match self { + PanicReport::None => "None", + PanicReport::WithoutMessage => "without message", + PanicReport::WithMessage => "with message", + }; + write!(f, "{}", panic_status) + } +} + +impl PanicReport { + pub fn data_section_severity(&self, data_section: DataSectionReader) -> Self { + if is_panic_with_message_triggered(data_section.clone()) { + return Self::WithMessage; + } + + if is_panic_without_message_triggered(data_section) { + println!("here"); + return Self::WithoutMessage; + } + + Self::None + } + + pub fn max_severity(&mut self, data_section: DataSectionReader) { + if *self == PanicReport::WithMessage { + return; + } + + let panic_report = self.data_section_severity(data_section); + if panic_report == PanicReport::None { + return; + } + + *self = panic_report; + } +} + +fn is_panic_with_message_triggered(data_section: DataSectionReader) -> bool { + for data_fragment in data_section.into_iter().flatten() { + if data_fragment + .data + .windows(PANIC_WITH_MESSAGE.len()) + .any(|data| data == PANIC_WITH_MESSAGE) + { + return true; + } + } + + false +} + +fn is_panic_without_message_triggered(data_section: DataSectionReader) -> bool { + for data_fragment in data_section.into_iter().flatten() { + if data_fragment + .data + .windows(PANIC_WITHOUT_MESSAGE.len()) + .any(|data| data == PANIC_WITHOUT_MESSAGE) + { + return true; + } + } + + false +} diff --git a/framework/meta-lib/src/tools/report_creator.rs b/framework/meta-lib/src/tools/report_creator.rs index 93cc44e14c..9555814075 100644 --- a/framework/meta-lib/src/tools/report_creator.rs +++ b/framework/meta-lib/src/tools/report_creator.rs @@ -1,28 +1,9 @@ -use std::fmt::Display; - -#[derive(Default)] -pub enum PanicMessage { - #[default] - None, - WithoutMessage, - WithMessage, -} - -impl Display for PanicMessage { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let panic_status = match self { - PanicMessage::None => "None", - PanicMessage::WithoutMessage => "without message", - PanicMessage::WithMessage => "with message", - }; - write!(f, "{}", panic_status) - } -} +use super::panic_report::PanicReport; pub struct ReportCreator { pub path: String, pub has_allocator: bool, - pub has_panic: PanicMessage, + pub has_panic: PanicReport, } impl ReportCreator {} diff --git a/framework/meta-lib/src/tools/wasm_extractor.rs b/framework/meta-lib/src/tools/wasm_extractor.rs index 20173c88c3..733ca3fad0 100644 --- a/framework/meta-lib/src/tools/wasm_extractor.rs +++ b/framework/meta-lib/src/tools/wasm_extractor.rs @@ -7,10 +7,8 @@ use wasmparser::{ use crate::ei::EIVersion; -use super::report_creator::{PanicMessage, ReportCreator}; +use super::{panic_report::PanicReport, report_creator::ReportCreator}; -const PANIC_WITH_MESSAGE: &[u8; 16] = b"panic occurred: "; -const PANIC_WITHOUT_MESSAGE: &[u8; 14] = b"panic occurred"; const ERROR_FAIL_ALLOCATOR: &[u8; 27] = b"memory allocation forbidden"; pub struct WasmInfo { @@ -49,7 +47,7 @@ pub(crate) fn populate_wasm_info( let mut allocator_trigger = false; let mut ei_check = false; let mut memory_grow_flag = false; - let mut has_panic: PanicMessage = PanicMessage::default(); + let mut has_panic: PanicReport = PanicReport::default(); let parser = Parser::new(0); for payload in parser.parse_all(&wasm_data) { @@ -60,21 +58,7 @@ pub(crate) fn populate_wasm_info( }, Payload::DataSection(data_section) => { allocator_trigger |= is_fail_allocator_triggered(data_section.clone()); - match has_panic { - PanicMessage::None => { - if is_panic_with_message_triggered(data_section.clone()) { - has_panic = PanicMessage::WithMessage; - } else if is_panic_without_message_triggered(data_section) { - has_panic = PanicMessage::WithoutMessage; - } - }, - PanicMessage::WithoutMessage => { - if is_panic_with_message_triggered(data_section.clone()) { - has_panic = PanicMessage::WithMessage; - } - }, - PanicMessage::WithMessage => continue, - } + has_panic.max_severity(data_section); }, Payload::CodeSectionEntry(code_section) => { memory_grow_flag |= is_mem_grow(code_section); @@ -119,34 +103,6 @@ fn is_fail_allocator_triggered(data_section: DataSectionReader) -> bool { false } -fn is_panic_with_message_triggered(data_section: DataSectionReader) -> bool { - for data_fragment in data_section.into_iter().flatten() { - if data_fragment - .data - .windows(PANIC_WITH_MESSAGE.len()) - .any(|data| data == PANIC_WITH_MESSAGE) - { - return true; - } - } - - false -} - -fn is_panic_without_message_triggered(data_section: DataSectionReader) -> bool { - for data_fragment in data_section.into_iter().flatten() { - if data_fragment - .data - .windows(PANIC_WITHOUT_MESSAGE.len()) - .any(|data| data == PANIC_WITHOUT_MESSAGE) - { - return true; - } - } - - false -} - pub fn extract_imports( import_section: ImportSectionReader, import_extraction_enabled: bool, diff --git a/framework/meta-lib/src/tools/wasm_extractor_test.rs b/framework/meta-lib/src/tools/wasm_extractor_test.rs index e99e94f179..030cddc503 100644 --- a/framework/meta-lib/src/tools/wasm_extractor_test.rs +++ b/framework/meta-lib/src/tools/wasm_extractor_test.rs @@ -2,7 +2,7 @@ pub mod tests { use wat::Parser; - use crate::tools::{report_creator::PanicMessage, wasm_extractor::populate_wasm_info}; + use crate::tools::{panic_report::PanicReport, wasm_extractor::populate_wasm_info}; const EMPTY_WITH_FAIL_ALLOCATOR: &str = r#" (module $empty_wasm.wasm @@ -261,7 +261,7 @@ pub mod tests { assert!(!wasm_info.memory_grow_flag); assert!(!wasm_info.report.has_allocator); assert_eq!( - PanicMessage::None.to_string(), + PanicReport::None.to_string(), wasm_info.report.has_panic.to_string() ); } @@ -275,7 +275,7 @@ pub mod tests { assert!(wasm_info.memory_grow_flag); assert!(!wasm_info.report.has_allocator); assert_eq!( - PanicMessage::WithoutMessage.to_string(), + PanicReport::WithoutMessage.to_string(), wasm_info.report.has_panic.to_string() ); } @@ -289,7 +289,7 @@ pub mod tests { assert!(!wasm_info.memory_grow_flag); assert!(wasm_info.report.has_allocator); assert_eq!( - PanicMessage::None.to_string(), + PanicReport::None.to_string(), wasm_info.report.has_panic.to_string() ); } From 7c0fc8e1f615d9bb6d588fde2bbd2a5daad0a0d5 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Fri, 27 Sep 2024 14:40:15 +0300 Subject: [PATCH 21/30] chain sim - generate one block after register wallets --- contracts/examples/adder/interact/src/basic_interact.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contracts/examples/adder/interact/src/basic_interact.rs b/contracts/examples/adder/interact/src/basic_interact.rs index 9f3f82a43b..7042df9781 100644 --- a/contracts/examples/adder/interact/src/basic_interact.rs +++ b/contracts/examples/adder/interact/src/basic_interact.rs @@ -83,6 +83,8 @@ impl AdderInteract { ) .await; + interactor.proxy.generate_blocks(1).await.unwrap(); + Self { interactor, adder_owner_address: adder_owner_address.into(), From a39c586f0c573b2832e7b977bc73bcab73f899d3 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Fri, 27 Sep 2024 14:48:08 +0300 Subject: [PATCH 22/30] chain sim - add enum ChainType in config --- contracts/examples/adder/interact/config.toml | 4 ++-- .../interact/src/basic_interact_config.rs | 19 ++++++++++------- .../interact/src/multisig_interact_config.rs | 19 ++++++++++------- .../interact/src/bf_interact_config.rs | 21 +++++++++++-------- .../interact/src/comp_interact_config.rs | 19 ++++++++++------- .../src/system_sc_interact_config.rs | 19 ++++++++++------- 6 files changed, 58 insertions(+), 43 deletions(-) diff --git a/contracts/examples/adder/interact/config.toml b/contracts/examples/adder/interact/config.toml index d40bebad7f..64846243cc 100644 --- a/contracts/examples/adder/interact/config.toml +++ b/contracts/examples/adder/interact/config.toml @@ -1,2 +1,2 @@ -chain_type = 'real' -gateway_uri = 'http://localhost:8085' \ No newline at end of file +chain_type = 'simulator' +gateway_uri = 'http://localhost:8085' diff --git a/contracts/examples/adder/interact/src/basic_interact_config.rs b/contracts/examples/adder/interact/src/basic_interact_config.rs index a49cc6ff7a..edd4ba6b1e 100644 --- a/contracts/examples/adder/interact/src/basic_interact_config.rs +++ b/contracts/examples/adder/interact/src/basic_interact_config.rs @@ -4,11 +4,18 @@ use std::io::Read; /// Config file const CONFIG_FILE: &str = "config.toml"; +#[derive(Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ChainType { + Real, + Simulator, +} + /// Adder Interact configuration #[derive(Debug, Deserialize)] pub struct Config { gateway_uri: String, - chain_type: String, + chain_type: ChainType, } impl Config { @@ -27,13 +34,9 @@ impl Config { // Returns if chain type is chain simulator pub fn use_chain_simulator(&self) -> bool { - match self.chain_type.as_str() { - "simulator" => true, - "real" => false, - _ => panic!( - "Invalid chain type: {}. Expected 'simulator' or 'real'.", - self.chain_type - ), + match self.chain_type { + ChainType::Real => false, + ChainType::Simulator => true, } } } diff --git a/contracts/examples/multisig/interact/src/multisig_interact_config.rs b/contracts/examples/multisig/interact/src/multisig_interact_config.rs index dd73c46092..c11c2c447f 100644 --- a/contracts/examples/multisig/interact/src/multisig_interact_config.rs +++ b/contracts/examples/multisig/interact/src/multisig_interact_config.rs @@ -5,11 +5,18 @@ use std::io::Read; /// Config file const CONFIG_FILE: &str = "config.toml"; +#[derive(Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ChainType { + Real, + Simulator, +} + /// Multisig Interact configuration #[derive(Debug, Deserialize)] pub struct Config { pub gateway_uri: String, - pub chain_type: String, + pub chain_type: ChainType, pub quorum: usize, pub wegld_address: Bech32Address, } @@ -30,13 +37,9 @@ impl Config { // Returns if chain type is chain simulator pub fn use_chain_simulator(&self) -> bool { - match self.chain_type.as_str() { - "simulator" => true, - "real" => false, - _ => panic!( - "Invalid chain type: {}. Expected 'simulator' or 'real'.", - self.chain_type - ), + match self.chain_type { + ChainType::Real => false, + ChainType::Simulator => true, } } } diff --git a/contracts/feature-tests/basic-features/interact/src/bf_interact_config.rs b/contracts/feature-tests/basic-features/interact/src/bf_interact_config.rs index 0257511464..edd4ba6b1e 100644 --- a/contracts/feature-tests/basic-features/interact/src/bf_interact_config.rs +++ b/contracts/feature-tests/basic-features/interact/src/bf_interact_config.rs @@ -4,11 +4,18 @@ use std::io::Read; /// Config file const CONFIG_FILE: &str = "config.toml"; +#[derive(Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ChainType { + Real, + Simulator, +} + /// Adder Interact configuration #[derive(Debug, Deserialize)] pub struct Config { gateway_uri: String, - chain_type: String, + chain_type: ChainType, } impl Config { @@ -25,15 +32,11 @@ impl Config { &self.gateway_uri } - // Returns if true if chain type is chain simulator + // Returns if chain type is chain simulator pub fn use_chain_simulator(&self) -> bool { - match self.chain_type.as_str() { - "simulator" => true, - "real" => false, - _ => panic!( - "Invalid chain type: {}. Expected 'simulator' or 'real'.", - self.chain_type - ), + match self.chain_type { + ChainType::Real => false, + ChainType::Simulator => true, } } } diff --git a/contracts/feature-tests/composability/interact/src/comp_interact_config.rs b/contracts/feature-tests/composability/interact/src/comp_interact_config.rs index 3a261b3f55..cffff8e915 100644 --- a/contracts/feature-tests/composability/interact/src/comp_interact_config.rs +++ b/contracts/feature-tests/composability/interact/src/comp_interact_config.rs @@ -8,11 +8,18 @@ use crate::forwarder_queue_proxy::QueuedCallType; /// Config file const CONFIG_FILE: &str = "config.toml"; +#[derive(Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ChainType { + Real, + Simulator, +} + /// Multisig Interact configuration #[derive(Debug, Deserialize)] pub struct Config { gateway_uri: String, - chain_type: String, + chain_type: ChainType, call_type: String, token_id: String, token_nonce: u64, @@ -35,13 +42,9 @@ impl Config { // Returns if chain type is chain simulator pub fn use_chain_simulator(&self) -> bool { - match self.chain_type.as_str() { - "simulator" => true, - "real" => false, - _ => panic!( - "Invalid chain type: {}. Expected 'simulator' or 'real'.", - self.chain_type - ), + match self.chain_type { + ChainType::Real => false, + ChainType::Simulator => true, } } diff --git a/tools/interactor-system-func-calls/src/system_sc_interact_config.rs b/tools/interactor-system-func-calls/src/system_sc_interact_config.rs index 4fff392c11..c61059d57e 100644 --- a/tools/interactor-system-func-calls/src/system_sc_interact_config.rs +++ b/tools/interactor-system-func-calls/src/system_sc_interact_config.rs @@ -4,11 +4,18 @@ use std::io::Read; /// Config file const CONFIG_FILE: &str = "config.toml"; +#[derive(Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ChainType { + Real, + Simulator, +} + /// SysFuncCalls Interact configuration #[derive(Debug, Deserialize)] pub struct Config { gateway_uri: String, - chain_type: String, + chain_type: ChainType, } impl Config { @@ -27,13 +34,9 @@ impl Config { // Returns if chain type is chain simulator pub fn use_chain_simulator(&self) -> bool { - match self.chain_type.as_str() { - "simulator" => true, - "real" => false, - _ => panic!( - "Invalid chain type: {}. Expected 'simulator' or 'real'.", - self.chain_type - ), + match self.chain_type { + ChainType::Real => false, + ChainType::Simulator => true, } } } From f601d26f7bfab069615055ab16e1437381ba4133 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Fri, 27 Sep 2024 17:44:35 +0300 Subject: [PATCH 23/30] remove dbg files --- framework/meta/src/cmd/code_report/generate_report.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/framework/meta/src/cmd/code_report/generate_report.rs b/framework/meta/src/cmd/code_report/generate_report.rs index 5c06b65295..0cd0c76993 100644 --- a/framework/meta/src/cmd/code_report/generate_report.rs +++ b/framework/meta/src/cmd/code_report/generate_report.rs @@ -108,6 +108,11 @@ fn find_mxsc_files(path: &PathBuf) -> Vec { let mut mxsc_files = Vec::new(); for entry in read_dir(path).unwrap() { let file_path = entry.unwrap().path(); + + if file_path.to_str().unwrap().ends_with("-dbg.mxsc.json") { + continue; + } + if file_path.to_str().unwrap().ends_with(".mxsc.json") { mxsc_files.push(file_path); } From 40fc99ee6fd543fec85c847aad3d66ba3f998559 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 27 Sep 2024 18:30:34 +0300 Subject: [PATCH 24/30] vm-core renamed to chain-core --- Cargo.lock | 46 +++++++++---------- framework/base/Cargo.toml | 2 +- .../base/src/abi/type_abi_impl_vm_core.rs | 2 +- framework/base/src/api.rs | 2 +- .../src/formatter/formatter_impl_vm_core.rs | 2 +- framework/base/src/lib.rs | 2 +- framework/base/src/types.rs | 8 ++-- framework/base/src/types/heap.rs | 2 +- .../system_proxy/builtin_func_proxy.rs | 2 +- .../types/managed/wrapped/managed_vec_item.rs | 2 +- .../multiversx_sc_lldb_pretty_printers.py | 2 +- vm-core/Cargo.toml | 2 +- vm/Cargo.toml | 2 +- vm/src/lib.rs | 2 +- .../builtin_func_container.rs | 2 +- .../esdt_nft/esdt_local_burn.rs | 2 +- .../esdt_nft/esdt_local_mint.rs | 2 +- .../esdt_nft/esdt_nft_add_quantity_mock.rs | 2 +- .../esdt_nft/esdt_nft_add_uri_mock.rs | 2 +- .../esdt_nft/esdt_nft_burn_mock.rs | 2 +- .../esdt_nft/esdt_nft_create_mock.rs | 2 +- .../esdt_nft_update_attriutes_mock.rs | 2 +- .../general/change_owner_mock.rs | 2 +- .../general/claim_developer_rewards_mock.rs | 2 +- .../general/delete_username_mock.rs | 2 +- .../general/migrate_username_mock.rs | 2 +- .../general/set_username_mock.rs | 2 +- .../general/upgrade_contract.rs | 2 +- .../transfer/esdt_multi_transfer_mock.rs | 2 +- .../transfer/esdt_nft_transfer_mock.rs | 2 +- .../transfer/esdt_transfer_mock.rs | 2 +- vm/src/types.rs | 12 ++--- vm/src/vm_hooks/vh_handler/vh_blockchain.rs | 2 +- vm/src/vm_hooks/vh_handler/vh_send.rs | 6 +-- 34 files changed, 66 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 83da1dcf65..e7a2087e0d 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -187,9 +187,9 @@ checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" [[package]] name = "autocfg" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "backtrace" @@ -386,9 +386,9 @@ checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" [[package]] name = "cc" -version = "1.1.21" +version = "1.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07b1695e2c7e8fc85310cde85aeaab7e3097f593c91d209d3f9df76c928100f0" +checksum = "9540e661f81799159abee814118cc139a2004b3a3aa3ea37724a1b66530b90e0" dependencies = [ "shlex", ] @@ -1076,9 +1076,9 @@ dependencies = [ [[package]] name = "flate2" -version = "1.0.33" +version = "1.0.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "324a1be68054ef05ad64b861cc9eaf1d623d2d8cb25b4bf2cb9cdd902b4bf253" +checksum = "a1b589b4dc103969ad3cf85c950899926ec64300a1a46d76c03a6072957036f0" dependencies = [ "crc32fast", "miniz_oxide", @@ -1931,6 +1931,15 @@ dependencies = [ "multiversx-sc-meta-lib", ] +[[package]] +name = "multiversx-chain-core" +version = "0.10.0" +dependencies = [ + "bitflags", + "multiversx-chain-vm-executor", + "multiversx-sc-codec", +] + [[package]] name = "multiversx-chain-scenario-format" version = "0.23.0" @@ -1954,7 +1963,7 @@ dependencies = [ "hex", "hex-literal", "itertools", - "multiversx-chain-vm-core", + "multiversx-chain-core", "multiversx-chain-vm-executor", "num-bigint", "num-traits", @@ -1964,15 +1973,6 @@ dependencies = [ "sha3", ] -[[package]] -name = "multiversx-chain-vm-core" -version = "0.10.0" -dependencies = [ - "bitflags", - "multiversx-chain-vm-executor", - "multiversx-sc-codec", -] - [[package]] name = "multiversx-chain-vm-executor" version = "0.2.0" @@ -2006,7 +2006,7 @@ version = "0.53.0" dependencies = [ "bitflags", "hex-literal", - "multiversx-chain-vm-core", + "multiversx-chain-core", "multiversx-sc-codec", "multiversx-sc-derive", "num-traits", @@ -2732,9 +2732,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.5" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62871f2d65009c0256aed1b9cfeeb8ac272833c404e13d53d400cd0dad7a2ac0" +checksum = "355ae415ccd3a04315d3f8246e86d67689ea74d88d915576e1589a351062a13b" dependencies = [ "bitflags", ] @@ -3157,9 +3157,9 @@ dependencies = [ [[package]] name = "serde_spanned" -version = "0.6.7" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" dependencies = [ "serde", ] @@ -4051,9 +4051,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.6.19" +version = "0.6.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c52ac009d615e79296318c1bcce2d422aaca15ad08515e344feeda07df67a587" +checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" dependencies = [ "memchr", ] diff --git a/framework/base/Cargo.toml b/framework/base/Cargo.toml index 8d34cbcf2b..324c87d955 100644 --- a/framework/base/Cargo.toml +++ b/framework/base/Cargo.toml @@ -38,6 +38,6 @@ version = "=0.21.0" path = "../../data/codec" features = ["derive"] -[dependencies.multiversx-chain-vm-core] +[dependencies.multiversx-chain-core] version = "=0.10.0" path = "../../vm-core" diff --git a/framework/base/src/abi/type_abi_impl_vm_core.rs b/framework/base/src/abi/type_abi_impl_vm_core.rs index a1024a919b..13b8988a59 100644 --- a/framework/base/src/abi/type_abi_impl_vm_core.rs +++ b/framework/base/src/abi/type_abi_impl_vm_core.rs @@ -1,7 +1,7 @@ use super::*; use alloc::vec::Vec; -use multiversx_chain_vm_core::types::{ +use multiversx_chain_core::types::{ Address, BoxedBytes, CodeMetadata, EsdtLocalRole, EsdtTokenType, H256, }; diff --git a/framework/base/src/api.rs b/framework/base/src/api.rs index 827929972b..8123eded28 100644 --- a/framework/base/src/api.rs +++ b/framework/base/src/api.rs @@ -30,4 +30,4 @@ pub use storage_api::*; pub use vm_api::VMApi; // Backwards compatibility. -pub use crate::vm_core::builtin_func_names::*; +pub use crate::chain_core::builtin_func_names::*; diff --git a/framework/base/src/formatter/formatter_impl_vm_core.rs b/framework/base/src/formatter/formatter_impl_vm_core.rs index ca92d9232d..609c090e36 100644 --- a/framework/base/src/formatter/formatter_impl_vm_core.rs +++ b/framework/base/src/formatter/formatter_impl_vm_core.rs @@ -1,4 +1,4 @@ -use multiversx_chain_vm_core::types::CodeMetadata; +use multiversx_chain_core::types::CodeMetadata; use super::{hex_util, FormatByteReceiver, SCBinary, SCDisplay, SCLowerHex}; diff --git a/framework/base/src/lib.rs b/framework/base/src/lib.rs index 1b1ee1f70f..9f6680af7c 100644 --- a/framework/base/src/lib.rs +++ b/framework/base/src/lib.rs @@ -10,7 +10,7 @@ extern crate alloc; pub use multiversx_sc_codec as codec; // Re-exporting the VM-core, for convenience. -pub use multiversx_chain_vm_core as vm_core; +pub use multiversx_chain_core as chain_core; /// Reexported for convenience. pub use crate::codec::arrayvec; diff --git a/framework/base/src/types.rs b/framework/base/src/types.rs index 5f2b4eddd8..41d303e91b 100644 --- a/framework/base/src/types.rs +++ b/framework/base/src/types.rs @@ -16,7 +16,7 @@ pub use static_buffer::*; #[cfg(feature = "alloc")] pub use heap::*; -pub use crate::vm_core::types::CodeMetadata; -pub use crate::vm_core::types::EsdtLocalRole; -pub use crate::vm_core::types::EsdtLocalRoleFlags; -pub use crate::vm_core::types::EsdtTokenType; +pub use crate::chain_core::types::CodeMetadata; +pub use crate::chain_core::types::EsdtLocalRole; +pub use crate::chain_core::types::EsdtLocalRoleFlags; +pub use crate::chain_core::types::EsdtTokenType; diff --git a/framework/base/src/types/heap.rs b/framework/base/src/types/heap.rs index c87ef2e2c2..18a7a5f9fc 100644 --- a/framework/base/src/types/heap.rs +++ b/framework/base/src/types/heap.rs @@ -8,4 +8,4 @@ pub use queue::Queue; pub use alloc::{boxed::Box, string::String, vec::Vec}; -pub use crate::vm_core::types::{Address, BoxedBytes, H256}; +pub use crate::chain_core::types::{Address, BoxedBytes, H256}; diff --git a/framework/base/src/types/interaction/system_proxy/builtin_func_proxy.rs b/framework/base/src/types/interaction/system_proxy/builtin_func_proxy.rs index cdb03b369b..1bf7c3a1fc 100644 --- a/framework/base/src/types/interaction/system_proxy/builtin_func_proxy.rs +++ b/framework/base/src/types/interaction/system_proxy/builtin_func_proxy.rs @@ -5,7 +5,7 @@ use crate::types::{ TxEnv, TxFrom, TxGas, TxProxyTrait, TxTo, TxTypedCall, }; -use crate::vm_core::builtin_func_names::{ +use crate::chain_core::builtin_func_names::{ CHANGE_OWNER_BUILTIN_FUNC_NAME, CLAIM_DEVELOPER_REWARDS_FUNC_NAME, DELETE_USERNAME_FUNC_NAME, ESDT_LOCAL_BURN_FUNC_NAME, ESDT_LOCAL_MINT_FUNC_NAME, ESDT_NFT_ADD_QUANTITY_FUNC_NAME, ESDT_NFT_ADD_URI_FUNC_NAME, ESDT_NFT_BURN_FUNC_NAME, ESDT_NFT_CREATE_FUNC_NAME, diff --git a/framework/base/src/types/managed/wrapped/managed_vec_item.rs b/framework/base/src/types/managed/wrapped/managed_vec_item.rs index 744b310b58..00cb212c18 100644 --- a/framework/base/src/types/managed/wrapped/managed_vec_item.rs +++ b/framework/base/src/types/managed/wrapped/managed_vec_item.rs @@ -1,6 +1,6 @@ use core::borrow::Borrow; -use multiversx_chain_vm_core::types::{EsdtLocalRole, EsdtTokenType}; +use multiversx_chain_core::types::{EsdtLocalRole, EsdtTokenType}; use crate::{ api::ManagedTypeApi, diff --git a/tools/rust-debugger/pretty-printers/multiversx_sc_lldb_pretty_printers.py b/tools/rust-debugger/pretty-printers/multiversx_sc_lldb_pretty_printers.py index bb67d11d43..237511a541 100644 --- a/tools/rust-debugger/pretty-printers/multiversx_sc_lldb_pretty_printers.py +++ b/tools/rust-debugger/pretty-printers/multiversx_sc_lldb_pretty_printers.py @@ -45,7 +45,7 @@ # 4. SC wasm - Managed multi value types # 5. VM core types -MOD_PATH = "multiversx_chain_vm_core::types" +MOD_PATH = "multiversx_chain_core::types" HEAP_ADDRESS_TYPE = f"{MOD_PATH}::address::Address" BOXED_BYTES_TYPE = f"{MOD_PATH}::boxed_bytes::BoxedBytes" diff --git a/vm-core/Cargo.toml b/vm-core/Cargo.toml index 6034ba4590..62adc9e454 100644 --- a/vm-core/Cargo.toml +++ b/vm-core/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "multiversx-chain-vm-core" +name = "multiversx-chain-core" version = "0.10.0" edition = "2021" diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 95025b7eee..0810f82eba 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -34,7 +34,7 @@ rand = { version= "0.8.5", optional = true } rand_seeder = "0.3.0" ed25519-dalek = "2.1.0" -[dependencies.multiversx-chain-vm-core] +[dependencies.multiversx-chain-core] version = "=0.10.0" path = "../vm-core" diff --git a/vm/src/lib.rs b/vm/src/lib.rs index a991128d6e..51e8931642 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -15,7 +15,7 @@ pub use world_mock::BlockchainMock; pub use multiversx_chain_vm_executor as executor; // Re-exporting the VM-core, for convenience. -pub use multiversx_chain_vm_core as vm_core; +pub use multiversx_chain_core as chain_core; #[macro_use] extern crate alloc; diff --git a/vm/src/tx_execution/builtin_function_mocks/builtin_func_container.rs b/vm/src/tx_execution/builtin_function_mocks/builtin_func_container.rs index 369a1e50df..39220d9722 100644 --- a/vm/src/tx_execution/builtin_function_mocks/builtin_func_container.rs +++ b/vm/src/tx_execution/builtin_function_mocks/builtin_func_container.rs @@ -15,7 +15,7 @@ use crate::{ types::EsdtLocalRole, }; -use crate::vm_core::builtin_func_names::*; +use crate::chain_core::builtin_func_names::*; /// Container for builtin function logic. /// diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_burn.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_burn.rs index 12053b7506..b3f42b3844 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_burn.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_burn.rs @@ -1,9 +1,9 @@ use num_bigint::BigUint; use crate::{ + chain_core::builtin_func_names::ESDT_LOCAL_BURN_FUNC_NAME, tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, - vm_core::builtin_func_names::ESDT_LOCAL_BURN_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_mint.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_mint.rs index e1026dc19f..cb59eee3d5 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_mint.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_local_mint.rs @@ -1,9 +1,9 @@ use num_bigint::BigUint; use crate::{ + chain_core::builtin_func_names::ESDT_LOCAL_MINT_FUNC_NAME, tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, - vm_core::builtin_func_names::ESDT_LOCAL_MINT_FUNC_NAME, world_mock::EsdtInstanceMetadata, }; diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_quantity_mock.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_quantity_mock.rs index d32f929bd7..4ef229d686 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_quantity_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_quantity_mock.rs @@ -1,10 +1,10 @@ use num_bigint::BigUint; use crate::{ + chain_core::builtin_func_names::ESDT_NFT_ADD_QUANTITY_FUNC_NAME, tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, types::{top_decode_u64, top_encode_u64}, - vm_core::builtin_func_names::ESDT_NFT_ADD_QUANTITY_FUNC_NAME, world_mock::EsdtInstanceMetadata, }; diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_uri_mock.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_uri_mock.rs index 030dae16db..3c4d2b986b 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_uri_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_add_uri_mock.rs @@ -1,8 +1,8 @@ use crate::{ + chain_core::builtin_func_names::ESDT_NFT_ADD_URI_FUNC_NAME, tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, types::{top_decode_u64, top_encode_u64}, - vm_core::builtin_func_names::ESDT_NFT_ADD_URI_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_burn_mock.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_burn_mock.rs index 8ad98cbc87..ef6362f073 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_burn_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_burn_mock.rs @@ -1,10 +1,10 @@ use num_bigint::BigUint; use crate::{ + chain_core::builtin_func_names::ESDT_NFT_BURN_FUNC_NAME, tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, types::{top_decode_u64, top_encode_u64}, - vm_core::builtin_func_names::ESDT_NFT_BURN_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_create_mock.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_create_mock.rs index f94c39a4b4..0007ec3c91 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_create_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_create_mock.rs @@ -1,10 +1,10 @@ use num_bigint::BigUint; use crate::{ + chain_core::builtin_func_names::ESDT_NFT_CREATE_FUNC_NAME, tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, types::{top_decode_u64, top_encode_u64}, - vm_core::builtin_func_names::ESDT_NFT_CREATE_FUNC_NAME, world_mock::{EsdtInstance, EsdtInstanceMetadata}, }; diff --git a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_update_attriutes_mock.rs b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_update_attriutes_mock.rs index defc3b8f4f..d6f088f4f6 100644 --- a/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_update_attriutes_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/esdt_nft/esdt_nft_update_attriutes_mock.rs @@ -1,8 +1,8 @@ use crate::{ + chain_core::builtin_func_names::ESDT_NFT_UPDATE_ATTRIBUTES_FUNC_NAME, tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, types::{top_decode_u64, top_encode_u64}, - vm_core::builtin_func_names::ESDT_NFT_UPDATE_ATTRIBUTES_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/general/change_owner_mock.rs b/vm/src/tx_execution/builtin_function_mocks/general/change_owner_mock.rs index c7128b3b4f..17d168a4cd 100644 --- a/vm/src/tx_execution/builtin_function_mocks/general/change_owner_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/general/change_owner_mock.rs @@ -1,8 +1,8 @@ use crate::{ + chain_core::builtin_func_names::CHANGE_OWNER_BUILTIN_FUNC_NAME, tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}, types::VMAddress, - vm_core::builtin_func_names::CHANGE_OWNER_BUILTIN_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/general/claim_developer_rewards_mock.rs b/vm/src/tx_execution/builtin_function_mocks/general/claim_developer_rewards_mock.rs index 08adbd0306..3d705959f1 100644 --- a/vm/src/tx_execution/builtin_function_mocks/general/claim_developer_rewards_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/general/claim_developer_rewards_mock.rs @@ -2,9 +2,9 @@ use num_bigint::BigUint; use num_traits::Zero; use crate::{ + chain_core::builtin_func_names::CLAIM_DEVELOPER_REWARDS_FUNC_NAME, tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}, - vm_core::builtin_func_names::CLAIM_DEVELOPER_REWARDS_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/general/delete_username_mock.rs b/vm/src/tx_execution/builtin_function_mocks/general/delete_username_mock.rs index 5661e7c339..719454c20a 100644 --- a/vm/src/tx_execution/builtin_function_mocks/general/delete_username_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/general/delete_username_mock.rs @@ -1,7 +1,7 @@ use crate::{ + chain_core::builtin_func_names::DELETE_USERNAME_FUNC_NAME, tx_execution::BlockchainVMRef, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}, - vm_core::builtin_func_names::DELETE_USERNAME_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/general/migrate_username_mock.rs b/vm/src/tx_execution/builtin_function_mocks/general/migrate_username_mock.rs index 5ca3ef0ad5..8674aed324 100644 --- a/vm/src/tx_execution/builtin_function_mocks/general/migrate_username_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/general/migrate_username_mock.rs @@ -1,6 +1,6 @@ +use crate::chain_core::builtin_func_names::MIGRATE_USERNAME_FUNC_NAME; use crate::tx_execution::BlockchainVMRef; use crate::tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}; -use crate::vm_core::builtin_func_names::MIGRATE_USERNAME_FUNC_NAME; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/general/set_username_mock.rs b/vm/src/tx_execution/builtin_function_mocks/general/set_username_mock.rs index 925d5a1ef4..65e1c95f1e 100644 --- a/vm/src/tx_execution/builtin_function_mocks/general/set_username_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/general/set_username_mock.rs @@ -1,6 +1,6 @@ +use crate::chain_core::builtin_func_names::SET_USERNAME_FUNC_NAME; use crate::tx_execution::BlockchainVMRef; use crate::tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}; -use crate::vm_core::builtin_func_names::SET_USERNAME_FUNC_NAME; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/general/upgrade_contract.rs b/vm/src/tx_execution/builtin_function_mocks/general/upgrade_contract.rs index eb106e810a..18ed1783ec 100644 --- a/vm/src/tx_execution/builtin_function_mocks/general/upgrade_contract.rs +++ b/vm/src/tx_execution/builtin_function_mocks/general/upgrade_contract.rs @@ -1,8 +1,8 @@ use crate::{ + chain_core::builtin_func_names::UPGRADE_CONTRACT_FUNC_NAME, tx_execution::{create_transfer_value_log, BlockchainVMRef}, tx_mock::{BlockchainUpdate, CallType, TxCache, TxFunctionName, TxInput, TxResult}, types::VMCodeMetadata, - vm_core::builtin_func_names::UPGRADE_CONTRACT_FUNC_NAME, }; use super::super::builtin_func_trait::BuiltinFunction; diff --git a/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_multi_transfer_mock.rs b/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_multi_transfer_mock.rs index 40eef80053..0235d68f9e 100644 --- a/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_multi_transfer_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_multi_transfer_mock.rs @@ -1,8 +1,8 @@ use crate::{ + chain_core::builtin_func_names::ESDT_MULTI_TRANSFER_FUNC_NAME, tx_execution::BlockchainVMRef, tx_mock::TxLog, types::{top_decode_u64, top_encode_u64}, - vm_core::builtin_func_names::ESDT_MULTI_TRANSFER_FUNC_NAME, }; use crate::{ diff --git a/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_nft_transfer_mock.rs b/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_nft_transfer_mock.rs index 8f5be1a372..39bbe7f1cb 100644 --- a/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_nft_transfer_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_nft_transfer_mock.rs @@ -1,8 +1,8 @@ use crate::{ + chain_core::builtin_func_names::ESDT_NFT_TRANSFER_FUNC_NAME, tx_execution::{BlockchainVMRef, BuiltinFunctionEsdtTransferInfo}, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, types::VMAddress, - vm_core::builtin_func_names::ESDT_NFT_TRANSFER_FUNC_NAME, }; use super::{ diff --git a/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_transfer_mock.rs b/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_transfer_mock.rs index 8a5efdb8b0..4d8bd08b2e 100644 --- a/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_transfer_mock.rs +++ b/vm/src/tx_execution/builtin_function_mocks/transfer/esdt_transfer_mock.rs @@ -1,7 +1,7 @@ use crate::{ + chain_core::builtin_func_names::ESDT_TRANSFER_FUNC_NAME, tx_execution::{BlockchainVMRef, BuiltinFunctionEsdtTransferInfo}, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult}, - vm_core::builtin_func_names::ESDT_TRANSFER_FUNC_NAME, }; use super::{ diff --git a/vm/src/types.rs b/vm/src/types.rs index 931211393c..9ab80284a5 100644 --- a/vm/src/types.rs +++ b/vm/src/types.rs @@ -1,9 +1,9 @@ -pub use crate::vm_core::types::Address as VMAddress; -pub use crate::vm_core::types::CodeMetadata as VMCodeMetadata; -pub use crate::vm_core::types::EsdtLocalRole; -pub use crate::vm_core::types::EsdtLocalRoleFlags; -pub use crate::vm_core::types::TokenType as VMTokenType; -pub use crate::vm_core::types::H256; +pub use crate::chain_core::types::Address as VMAddress; +pub use crate::chain_core::types::CodeMetadata as VMCodeMetadata; +pub use crate::chain_core::types::EsdtLocalRole; +pub use crate::chain_core::types::EsdtLocalRoleFlags; +pub use crate::chain_core::types::TokenType as VMTokenType; +pub use crate::chain_core::types::H256; pub type RawHandle = i32; diff --git a/vm/src/vm_hooks/vh_handler/vh_blockchain.rs b/vm/src/vm_hooks/vh_handler/vh_blockchain.rs index a59854cab3..5dcd5e7c0d 100644 --- a/vm/src/vm_hooks/vh_handler/vh_blockchain.rs +++ b/vm/src/vm_hooks/vh_handler/vh_blockchain.rs @@ -1,6 +1,6 @@ use crate::{ + chain_core::builtin_func_names::*, types::{EsdtLocalRole, EsdtLocalRoleFlags, RawHandle, VMAddress}, - vm_core::builtin_func_names::*, vm_hooks::VMHooksHandlerSource, world_mock::{EsdtData, EsdtInstance}, }; diff --git a/vm/src/vm_hooks/vh_handler/vh_send.rs b/vm/src/vm_hooks/vh_handler/vh_send.rs index 57c0315332..67d590f772 100644 --- a/vm/src/vm_hooks/vh_handler/vh_send.rs +++ b/vm/src/vm_hooks/vh_handler/vh_send.rs @@ -1,10 +1,10 @@ use crate::{ - tx_mock::{AsyncCallTxData, Promise, TxFunctionName, TxTokenTransfer}, - types::{top_encode_big_uint, top_encode_u64, RawHandle, VMAddress, VMCodeMetadata}, - vm_core::builtin_func_names::{ + chain_core::builtin_func_names::{ ESDT_MULTI_TRANSFER_FUNC_NAME, ESDT_NFT_TRANSFER_FUNC_NAME, ESDT_TRANSFER_FUNC_NAME, UPGRADE_CONTRACT_FUNC_NAME, }, + tx_mock::{AsyncCallTxData, Promise, TxFunctionName, TxTokenTransfer}, + types::{top_encode_big_uint, top_encode_u64, RawHandle, VMAddress, VMCodeMetadata}, vm_hooks::VMHooksHandlerSource, }; use num_traits::Zero; From 5b6fee0fff2aaf854f9ba856f79df0740df625fe Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 27 Sep 2024 18:38:33 +0300 Subject: [PATCH 25/30] chain-core cleanup --- Cargo.lock | 1 - vm-core/Cargo.toml | 3 --- vm-core/README.md | 2 +- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e7a2087e0d..8bda75ef7b 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -1936,7 +1936,6 @@ name = "multiversx-chain-core" version = "0.10.0" dependencies = [ "bitflags", - "multiversx-chain-vm-executor", "multiversx-sc-codec", ] diff --git a/vm-core/Cargo.toml b/vm-core/Cargo.toml index 62adc9e454..4c170181f7 100644 --- a/vm-core/Cargo.toml +++ b/vm-core/Cargo.toml @@ -23,6 +23,3 @@ bitflags = "=2.6.0" version = "=0.21.0" path = "../data/codec" features = ["derive"] - -[dependencies.multiversx-chain-vm-executor] -version = "=0.2.0" diff --git a/vm-core/README.md b/vm-core/README.md index fc9282a390..76827c0b17 100644 --- a/vm-core/README.md +++ b/vm-core/README.md @@ -1,4 +1,4 @@ -# MultiversX VM base types, interfaces and builtin function names +# MultiversX blockchain base types, interfaces and builtin function names It provides various types and contants referring to the MultiversX blockchain base implementation. From e31136e3e26c3f215bb454cc90d39d71bd28ebd9 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 30 Sep 2024 10:10:54 +0300 Subject: [PATCH 26/30] Address conversions removed --- .../scenario/src/api/impl_vh/static_api.rs | 2 +- .../src/facade/world_tx/scenario_exec_call.rs | 2 +- .../src/facade/world_tx/scenario_set_state.rs | 22 ++++----- .../scenario/model/transaction/tx_response.rs | 2 +- .../src/scenario/model/value/address_key.rs | 4 -- .../src/scenario/model/value/address_value.rs | 4 -- .../src/scenario/run_vm/check_state.rs | 2 +- .../scenario/src/scenario/run_vm/sc_call.rs | 4 +- .../scenario/src/scenario/run_vm/sc_deploy.rs | 4 +- .../scenario/src/scenario/run_vm/sc_query.rs | 4 +- .../scenario/src/scenario/run_vm/set_state.rs | 10 ++--- .../scenario/src/scenario/run_vm/transfer.rs | 6 +-- .../whitebox_legacy/contract_obj_wrapper.rs | 45 +++++++------------ 13 files changed, 44 insertions(+), 67 deletions(-) diff --git a/framework/scenario/src/api/impl_vh/static_api.rs b/framework/scenario/src/api/impl_vh/static_api.rs index 7e375a0c31..d4941a60db 100644 --- a/framework/scenario/src/api/impl_vh/static_api.rs +++ b/framework/scenario/src/api/impl_vh/static_api.rs @@ -55,7 +55,7 @@ impl StaticApi { /// /// This placeholder then needs to be converted to something useful. pub fn is_current_address_placeholder(address: &Address) -> bool { - address.as_array() == StaticApiVMHooksHandler::CURRENT_ADDRESS_PLACEHOLDER.as_array() + address == &StaticApiVMHooksHandler::CURRENT_ADDRESS_PLACEHOLDER } pub fn reset() { diff --git a/framework/scenario/src/facade/world_tx/scenario_exec_call.rs b/framework/scenario/src/facade/world_tx/scenario_exec_call.rs index ca09b9d318..9c1551111b 100644 --- a/framework/scenario/src/facade/world_tx/scenario_exec_call.rs +++ b/framework/scenario/src/facade/world_tx/scenario_exec_call.rs @@ -52,7 +52,7 @@ impl<'w> TxEnvMockDeployAddress for ScenarioEnvExec<'w> { .world .get_state() .accounts - .get(&from_value.to_vm_address()) + .get(&from_value.to_address()) .expect("sender does not exist") .nonce; let new_address_value = address_annotated(self, &new_address); diff --git a/framework/scenario/src/facade/world_tx/scenario_set_state.rs b/framework/scenario/src/facade/world_tx/scenario_set_state.rs index d036d647cd..3fd44d70b3 100644 --- a/framework/scenario/src/facade/world_tx/scenario_set_state.rs +++ b/framework/scenario/src/facade/world_tx/scenario_set_state.rs @@ -73,7 +73,7 @@ impl ScenarioWorld { let balance_value = big_uint_annotated(&env, &balance); let accounts = &mut self.get_mut_state().accounts; for (vm_address_key, account) in accounts.iter_mut() { - if vm_address_key == &address_value.to_vm_address() { + if vm_address_key == &address_value.to_address() { account.egld_balance.clone_from(&balance_value.value); } } @@ -88,8 +88,8 @@ impl ScenarioWorld { let address_value = address_annotated(&env, &address); let balance_value = big_uint_annotated(&env, &balance); let accounts = &mut self.get_mut_state().accounts; - for (vm_address, account) in accounts.iter_mut() { - if vm_address == &address_value.to_vm_address() { + for (address, account) in accounts.iter_mut() { + if address == &address_value.to_address() { account.esdt.set_esdt_balance( token_id.to_vec(), 0, @@ -129,8 +129,8 @@ impl ScenarioWorld { let mut esdt_attributes = Vec::new(); let _ = attributes.top_encode(&mut esdt_attributes); let accounts = &mut self.get_mut_state().accounts; - for (vm_address, account) in accounts.iter_mut() { - if vm_address == &address_value.to_vm_address() { + for (account_address, account) in accounts.iter_mut() { + if account_address == &address_value.to_address() { account.esdt.set_esdt_balance( token_id.to_vec(), nonce_value.value, @@ -138,7 +138,7 @@ impl ScenarioWorld { EsdtInstanceMetadata { creator: creator .as_ref() - .map(|c| address_annotated(&env, c).to_vm_address()), + .map(|c| address_annotated(&env, c).to_address()), attributes: esdt_attributes.clone(), royalties: royalties_value.value, name: name.unwrap_or_default().to_vec(), @@ -156,8 +156,8 @@ impl ScenarioWorld { BigUintValue: From, { let accounts = &mut self.get_mut_state().accounts; - for (vm_address, account) in accounts.iter_mut() { - if vm_address == &AddressKey::from(address).to_vm_address() { + for (account_address, account) in accounts.iter_mut() { + if account_address == &AddressKey::from(address).to_address() { account .developer_rewards .clone_from(&BigUintValue::from(developer_rewards).value); @@ -172,8 +172,8 @@ impl ScenarioWorld { let env = self.new_env_data(); let address_value = address_annotated(&env, &address); let accounts = &mut self.get_mut_state().accounts; - for (vm_address, account) in accounts.iter_mut() { - if vm_address == &address_value.to_vm_address() { + for (account_address, account) in accounts.iter_mut() { + if account_address == &address_value.to_address() { account.esdt.set_roles( token_id.to_vec(), roles @@ -231,7 +231,7 @@ impl<'w> SetStateBuilderBase<'w> { .vm_runner .blockchain_mock .state - .account_exists(&address.to_vm_address()), + .account_exists(&address.to_address()), "updating existing accounts currently not supported" ); diff --git a/framework/scenario/src/scenario/model/transaction/tx_response.rs b/framework/scenario/src/scenario/model/transaction/tx_response.rs index 547b381338..8215a60756 100644 --- a/framework/scenario/src/scenario/model/transaction/tx_response.rs +++ b/framework/scenario/src/scenario/model/transaction/tx_response.rs @@ -37,7 +37,7 @@ impl TxResponse { .result_logs .iter() .map(|tx_log| Log { - address: Address::from_slice(tx_log.address.as_bytes()), + address: tx_log.address.clone(), endpoint: tx_log.endpoint.to_string(), topics: tx_log.topics.clone(), data: tx_log.data.clone(), diff --git a/framework/scenario/src/scenario/model/value/address_key.rs b/framework/scenario/src/scenario/model/value/address_key.rs index f54e05b2f3..a5234d6a63 100644 --- a/framework/scenario/src/scenario/model/value/address_key.rs +++ b/framework/scenario/src/scenario/model/value/address_key.rs @@ -29,10 +29,6 @@ impl AddressKey { pub fn to_address(&self) -> Address { self.value.clone() } - - pub fn to_vm_address(&self) -> multiversx_chain_vm::types::VMAddress { - self.value.as_array().into() - } } impl Ord for AddressKey { diff --git a/framework/scenario/src/scenario/model/value/address_value.rs b/framework/scenario/src/scenario/model/value/address_value.rs index fdde631ede..8ef4e78a26 100644 --- a/framework/scenario/src/scenario/model/value/address_value.rs +++ b/framework/scenario/src/scenario/model/value/address_value.rs @@ -36,10 +36,6 @@ impl AddressValue { pub fn to_address(&self) -> Address { self.value.clone() } - - pub fn to_vm_address(&self) -> multiversx_chain_vm::types::VMAddress { - self.value.as_array().into() - } } impl fmt::Display for AddressValue { diff --git a/framework/scenario/src/scenario/run_vm/check_state.rs b/framework/scenario/src/scenario/run_vm/check_state.rs index abed0212cc..c1d8275c2b 100644 --- a/framework/scenario/src/scenario/run_vm/check_state.rs +++ b/framework/scenario/src/scenario/run_vm/check_state.rs @@ -23,7 +23,7 @@ impl ScenarioVMRunner { fn execute(state: &BlockchainState, accounts: &CheckAccounts) { for (expected_address, expected_account) in accounts.accounts.iter() { - if let Some(account) = state.accounts.get(&expected_address.to_vm_address()) { + if let Some(account) = state.accounts.get(&expected_address.to_address()) { assert!( expected_account.nonce.check(account.nonce), "bad account nonce. Address: {}. Want: {}. Have: {}", diff --git a/framework/scenario/src/scenario/run_vm/sc_call.rs b/framework/scenario/src/scenario/run_vm/sc_call.rs index 0a66578eaf..0cfad3aa62 100644 --- a/framework/scenario/src/scenario/run_vm/sc_call.rs +++ b/framework/scenario/src/scenario/run_vm/sc_call.rs @@ -86,8 +86,8 @@ impl ScenarioVMRunner { fn tx_input_from_call(sc_call_step: &ScCallStep) -> TxInput { let tx = &sc_call_step.tx; TxInput { - from: tx.from.to_vm_address(), - to: tx.to.to_vm_address(), + from: tx.from.to_address(), + to: tx.to.to_address(), egld_value: tx.egld_value.value.clone(), esdt_values: tx_esdt_transfers_from_scenario(tx.esdt_value.as_slice()), func_name: tx.function.clone().into(), diff --git a/framework/scenario/src/scenario/run_vm/sc_deploy.rs b/framework/scenario/src/scenario/run_vm/sc_deploy.rs index 11bb387a6b..4386a1ab5d 100644 --- a/framework/scenario/src/scenario/run_vm/sc_deploy.rs +++ b/framework/scenario/src/scenario/run_vm/sc_deploy.rs @@ -43,7 +43,7 @@ impl ScenarioVMRunner { tx_result.pending_calls.no_calls(), "Async calls from constructors are currently not supported" ); - (new_address.as_array().into(), tx_result) + (new_address, tx_result) } pub fn perform_sc_deploy_lambda_and_check( @@ -65,7 +65,7 @@ impl ScenarioVMRunner { fn tx_input_from_deploy(sc_deploy_step: &ScDeployStep) -> TxInput { let tx = &sc_deploy_step.tx; TxInput { - from: tx.from.to_vm_address(), + from: tx.from.to_address(), to: multiversx_chain_vm::types::VMAddress::zero(), egld_value: tx.egld_value.value.clone(), esdt_values: Vec::new(), diff --git a/framework/scenario/src/scenario/run_vm/sc_query.rs b/framework/scenario/src/scenario/run_vm/sc_query.rs index 1d3476ab4e..2e0c1cbdda 100644 --- a/framework/scenario/src/scenario/run_vm/sc_query.rs +++ b/framework/scenario/src/scenario/run_vm/sc_query.rs @@ -48,8 +48,8 @@ impl ScenarioVMRunner { fn tx_input_from_query(sc_query_step: &ScQueryStep) -> TxInput { TxInput { - from: sc_query_step.tx.to.to_vm_address(), - to: sc_query_step.tx.to.to_vm_address(), + from: sc_query_step.tx.to.to_address(), + to: sc_query_step.tx.to.to_address(), egld_value: BigUint::from(0u32), esdt_values: Vec::new(), func_name: sc_query_step.tx.function.clone().into(), diff --git a/framework/scenario/src/scenario/run_vm/set_state.rs b/framework/scenario/src/scenario/run_vm/set_state.rs index 8284d42422..42042ee1cc 100644 --- a/framework/scenario/src/scenario/run_vm/set_state.rs +++ b/framework/scenario/src/scenario/run_vm/set_state.rs @@ -35,7 +35,7 @@ fn execute(state: &mut BlockchainState, set_state_step: &SetStateStep) { ); state.validate_and_add_account(AccountData { - address: address.to_vm_address(), + address: address.to_address(), nonce: account .nonce .as_ref() @@ -65,7 +65,7 @@ fn execute(state: &mut BlockchainState, set_state_step: &SetStateStep) { contract_owner: account .owner .as_ref() - .map(|address_value| address_value.to_vm_address()), + .map(|address_value| address_value.to_address()), developer_rewards: account .developer_rewards .as_ref() @@ -79,9 +79,9 @@ fn execute(state: &mut BlockchainState, set_state_step: &SetStateStep) { "field should have SC format" ); state.put_new_address( - new_address.creator_address.to_vm_address(), + new_address.creator_address.to_address(), new_address.creator_nonce.value, - new_address.new_address.to_vm_address(), + new_address.new_address.to_address(), ) } for new_token_identifier in set_state_step.new_token_identifiers.iter().cloned() { @@ -155,7 +155,7 @@ fn convert_scenario_esdt_instance_to_world_mock( creator: scenario_esdt .creator .as_ref() - .map(|creator| creator.to_vm_address()), + .map(|creator| creator.to_address()), royalties: scenario_esdt .royalties .as_ref() diff --git a/framework/scenario/src/scenario/run_vm/transfer.rs b/framework/scenario/src/scenario/run_vm/transfer.rs index 0ecb89b7b8..a0fcc948dc 100644 --- a/framework/scenario/src/scenario/run_vm/transfer.rs +++ b/framework/scenario/src/scenario/run_vm/transfer.rs @@ -18,7 +18,7 @@ impl ScenarioVMRunner { pub fn perform_validator_reward(&mut self, validator_rewards_step: &ValidatorRewardStep) { self.blockchain_mock.state.increase_validator_reward( - &validator_rewards_step.tx.to.to_vm_address(), + &validator_rewards_step.tx.to.to_address(), &validator_rewards_step.tx.egld_value.value, ); } @@ -26,8 +26,8 @@ impl ScenarioVMRunner { fn tx_input_from_transfer(tx_transfer: &TxTransfer) -> TxInput { TxInput { - from: tx_transfer.from.to_vm_address(), - to: tx_transfer.to.to_vm_address(), + from: tx_transfer.from.to_address(), + to: tx_transfer.to.to_address(), egld_value: tx_transfer.egld_value.value.clone(), esdt_values: tx_esdt_transfers_from_scenario(tx_transfer.esdt_value.as_slice()), func_name: TxFunctionName::EMPTY, diff --git a/framework/scenario/src/whitebox_legacy/contract_obj_wrapper.rs b/framework/scenario/src/whitebox_legacy/contract_obj_wrapper.rs index b88377b98d..3ae56e5a61 100644 --- a/framework/scenario/src/whitebox_legacy/contract_obj_wrapper.rs +++ b/framework/scenario/src/whitebox_legacy/contract_obj_wrapper.rs @@ -13,10 +13,7 @@ use crate::{ ScenarioWorld, }; use multiversx_chain_scenario_format::interpret_trait::InterpretableFrom; -use multiversx_chain_vm::{ - tx_mock::{TxContext, TxContextStack, TxFunctionName, TxResult}, - types::VMAddress, -}; +use multiversx_chain_vm::tx_mock::{TxContext, TxContextStack, TxFunctionName, TxResult}; use multiversx_sc::types::{BigUint, H256}; use num_traits::Zero; @@ -89,7 +86,7 @@ impl BlockchainStateWrapper { } pub fn check_egld_balance(&self, address: &Address, expected_balance: &num_bigint::BigUint) { - let actual_balance = match &self.world.get_state().accounts.get(&to_vm_address(address)) { + let actual_balance = match &self.world.get_state().accounts.get(address) { Some(acc) => acc.egld_balance.clone(), None => num_bigint::BigUint::zero(), }; @@ -109,7 +106,7 @@ impl BlockchainStateWrapper { token_id: &[u8], expected_balance: &num_bigint::BigUint, ) { - let actual_balance = match &self.world.get_state().accounts.get(&to_vm_address(address)) { + let actual_balance = match &self.world.get_state().accounts.get(address) { Some(acc) => acc.esdt.get_esdt_balance(token_id, 0), None => num_bigint::BigUint::zero(), }; @@ -135,7 +132,7 @@ impl BlockchainStateWrapper { T: TopEncode + TopDecode + PartialEq + core::fmt::Debug, { let (actual_balance, actual_attributes_serialized) = - match &self.world.get_state().accounts.get(&to_vm_address(address)) { + match &self.world.get_state().accounts.get(address) { Some(acc) => { let esdt_data = acc.esdt.get_by_identifier_or_default(token_id); let opt_instance = esdt_data.instances.get_by_nonce(nonce); @@ -304,20 +301,19 @@ impl BlockchainStateWrapper { CB: ContractBase + CallableContract + 'static, ContractObjBuilder: 'static + Copy + Fn() -> CB, { - let deployer_vm_address = to_vm_address(deployer); let deployer_acc = self .world .get_state() .accounts - .get(&deployer_vm_address) + .get(deployer) .unwrap() .clone(); let new_sc_address = self.address_factory.new_sc_address(); self.world.get_mut_state().put_new_address( - deployer_vm_address, + deployer.clone(), deployer_acc.nonce, - to_vm_address(&new_sc_address), + new_sc_address.clone(), ); ContractObjWrapper::new(new_sc_address, obj_builder) @@ -493,8 +489,7 @@ impl BlockchainStateWrapper { } pub fn add_mandos_set_account(&mut self, address: &Address) { - let vm_address = to_vm_address(address); - if let Some(acc) = self.world.get_state().accounts.get(&vm_address).cloned() { + if let Some(acc) = self.world.get_state().accounts.get(address).cloned() { let opt_contract_path = self.address_to_code_path.get(address); if let Some(trace) = &mut self.world.get_mut_debugger_backend().trace { MandosGenerator::new(&mut trace.scenario_trace, &mut self.current_tx_id) @@ -504,8 +499,7 @@ impl BlockchainStateWrapper { } pub fn add_mandos_check_account(&mut self, address: &Address) { - let vm_address = to_vm_address(address); - if let Some(acc) = self.world.get_state().accounts.get(&vm_address).cloned() { + if let Some(acc) = self.world.get_state().accounts.get(address).cloned() { if let Some(trace) = &mut self.world.get_mut_debugger_backend().trace { MandosGenerator::new(&mut trace.scenario_trace, &mut self.current_tx_id) .check_account(&acc); @@ -662,7 +656,7 @@ impl BlockchainStateWrapper { impl BlockchainStateWrapper { pub fn get_egld_balance(&self, address: &Address) -> num_bigint::BigUint { - match self.world.get_state().accounts.get(&to_vm_address(address)) { + match self.world.get_state().accounts.get(address) { Some(acc) => acc.egld_balance.clone(), None => panic!( "get_egld_balance: Account {:?} does not exist", @@ -677,7 +671,7 @@ impl BlockchainStateWrapper { token_id: &[u8], token_nonce: u64, ) -> num_bigint::BigUint { - match self.world.get_state().accounts.get(&to_vm_address(address)) { + match self.world.get_state().accounts.get(address) { Some(acc) => acc.esdt.get_esdt_balance(token_id, token_nonce), None => panic!( "get_esdt_balance: Account {:?} does not exist", @@ -692,7 +686,7 @@ impl BlockchainStateWrapper { token_id: &[u8], token_nonce: u64, ) -> Option { - match self.world.get_state().accounts.get(&to_vm_address(address)) { + match self.world.get_state().accounts.get(address) { Some(acc) => match acc.esdt.get_by_identifier(token_id) { Some(esdt_data) => esdt_data .instances @@ -708,8 +702,8 @@ impl BlockchainStateWrapper { } pub fn dump_state(&self) { - for addr in self.world.get_state().accounts.keys() { - self.dump_state_for_account_hex_attributes(&to_framework_address(addr)); + for address in self.world.get_state().accounts.keys() { + self.dump_state_for_account_hex_attributes(address); println!(); } } @@ -725,8 +719,7 @@ impl BlockchainStateWrapper { &self, address: &Address, ) { - let vm_address = to_vm_address(address); - let account = match self.world.get_state().accounts.get(&vm_address) { + let account = match self.world.get_state().accounts.get(address) { Some(acc) => acc, None => panic!( "dump_state_for_account: Account {:?} does not exist", @@ -817,11 +810,3 @@ where let c_base = func(); Box::new(c_base) } - -fn to_vm_address(address: &Address) -> VMAddress { - address.as_array().into() -} - -fn to_framework_address(vm_address: &VMAddress) -> Address { - vm_address.as_array().into() -} From 53613d083022c36f5a045d5dde90bd368e008dfc Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 30 Sep 2024 14:15:58 +0300 Subject: [PATCH 27/30] core Address referenced in SDK --- Cargo.lock | 38 ++++++++++++------- framework/snippets/src/interactor.rs | 23 +---------- .../interactor_scenario/interactor_sc_call.rs | 6 +-- .../interactor_sc_deploy.rs | 15 +++----- .../interactor_vm_query.rs | 10 ++--- framework/snippets/src/interactor_sender.rs | 5 +-- sdk/core/Cargo.toml | 6 ++- sdk/core/src/{data/mod.rs => data.rs} | 0 sdk/core/src/data/address.rs | 12 ++++++ 9 files changed, 58 insertions(+), 57 deletions(-) rename sdk/core/src/{data/mod.rs => data.rs} (100%) diff --git a/Cargo.lock b/Cargo.lock index 699420da34..e7d39bfcc6 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -2161,6 +2161,7 @@ dependencies = [ "hmac", "itertools", "log", + "multiversx-chain-core", "pbkdf2", "pem", "rand", @@ -2319,9 +2320,12 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.19.0" +version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +checksum = "82881c4be219ab5faaf2ad5e5e5ecdff8c66bd7402ca3160975c93b24961afd1" +dependencies = [ + "portable-atomic", +] [[package]] name = "openssl" @@ -2554,6 +2558,12 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" +[[package]] +name = "portable-atomic" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" + [[package]] name = "ppv-lite86" version = "0.2.20" @@ -2738,18 +2748,18 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.6" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "355ae415ccd3a04315d3f8246e86d67689ea74d88d915576e1589a351062a13b" +checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" dependencies = [ "bitflags", ] [[package]] name = "regex" -version = "1.10.6" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" dependencies = [ "aho-corasick", "memchr", @@ -2759,9 +2769,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" dependencies = [ "aho-corasick", "memchr", @@ -2770,9 +2780,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "reqwest" @@ -2956,9 +2966,9 @@ dependencies = [ [[package]] name = "rustls-pki-types" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc0a2ce646f8655401bb81e7927b812614bd5d91dbc968696be50603510fcaf0" +checksum = "0e696e35370c65c9c541198af4543ccd580cf17fc25d8e05c5a242b202488c55" [[package]] name = "rustls-webpki" @@ -3389,9 +3399,9 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.12.0" +version = "3.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64" +checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" dependencies = [ "cfg-if", "fastrand", diff --git a/framework/snippets/src/interactor.rs b/framework/snippets/src/interactor.rs index bb38873afe..a2de650975 100644 --- a/framework/snippets/src/interactor.rs +++ b/framework/snippets/src/interactor.rs @@ -2,13 +2,8 @@ use multiversx_sc_scenario::{ imports::{Bech32Address, ScenarioRunner}, mandos_system::{run_list::ScenarioRunnerList, run_trace::ScenarioTraceFile}, multiversx_sc::types::Address, - scenario_model::AddressValue, -}; -use multiversx_sdk::{ - data::{address::Address as ErdrsAddress, network_config::NetworkConfig}, - gateway::GatewayProxy, - wallet::Wallet, }; +use multiversx_sdk::{data::network_config::NetworkConfig, gateway::GatewayProxy, wallet::Wallet}; use std::{ collections::HashMap, path::{Path, PathBuf}, @@ -47,7 +42,7 @@ impl Interactor { } pub fn register_wallet(&mut self, wallet: Wallet) -> Address { - let address = erdrs_address_to_h256(wallet.address()); + let address: Address = wallet.address().into(); self.sender_map.insert( address.clone(), Sender { @@ -75,17 +70,3 @@ impl Interactor { self.post_runners.run_set_state_step(&set_state); } } - -pub(crate) fn mandos_to_erdrs_address(mandos_address: &AddressValue) -> ErdrsAddress { - let bytes = mandos_address.value.as_array(); - ErdrsAddress::from_bytes(*bytes) -} - -pub(crate) fn address_h256_to_erdrs(address: &Address) -> ErdrsAddress { - let bytes = address.as_array(); - ErdrsAddress::from_bytes(*bytes) -} - -pub(crate) fn erdrs_address_to_h256(erdrs_address: ErdrsAddress) -> Address { - erdrs_address.to_bytes().into() -} diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs index 6802304ae9..590a6c1898 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs @@ -1,4 +1,4 @@ -use crate::{address_h256_to_erdrs, mandos_to_erdrs_address, network_response, Interactor}; +use crate::{network_response, Interactor}; use log::info; use multiversx_sc_scenario::{ api::StaticApi, @@ -57,8 +57,8 @@ impl Interactor { Transaction { nonce: 0, value: contract_call.egld_payment.to_alloc().to_string(), - sender: mandos_to_erdrs_address(&tx_call.from), - receiver: address_h256_to_erdrs(&contract_call.basic.to.to_address()), + sender: tx_call.from.to_address().into(), + receiver: contract_call.basic.to.to_address().into(), gas_price: self.network_config.min_gas_price, gas_limit: tx_call.gas_limit.value, data, diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs index 59de249e83..0d505e569b 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs @@ -1,24 +1,19 @@ -use crate::{mandos_to_erdrs_address, network_response, Interactor}; +use crate::{network_response, Interactor}; use log::info; use multiversx_sc_scenario::{ - imports::Bech32Address, + imports::{Address, Bech32Address}, mandos_system::ScenarioRunner, scenario_model::{ScDeployStep, SetStateStep}, }; -use multiversx_sdk::{ - data::{address::Address as ErdrsAddress, transaction::Transaction}, - utils::base64_encode, -}; - -const DEPLOY_RECEIVER: [u8; 32] = [0u8; 32]; +use multiversx_sdk::{data::transaction::Transaction, utils::base64_encode}; impl Interactor { pub(crate) fn sc_deploy_to_blockchain_tx(&self, sc_deploy_step: &ScDeployStep) -> Transaction { Transaction { nonce: 0, value: sc_deploy_step.tx.egld_value.value.to_string(), - sender: mandos_to_erdrs_address(&sc_deploy_step.tx.from), - receiver: ErdrsAddress::from_bytes(DEPLOY_RECEIVER), + sender: sc_deploy_step.tx.from.to_address().into(), + receiver: Address::zero().into(), gas_price: self.network_config.min_gas_price, gas_limit: sc_deploy_step.tx.gas_limit.value, data: Some(base64_encode(sc_deploy_step.tx.to_tx_data())), diff --git a/framework/snippets/src/interactor_scenario/interactor_vm_query.rs b/framework/snippets/src/interactor_scenario/interactor_vm_query.rs index aa9f87efb7..c28774eed4 100644 --- a/framework/snippets/src/interactor_scenario/interactor_vm_query.rs +++ b/framework/snippets/src/interactor_scenario/interactor_vm_query.rs @@ -1,6 +1,6 @@ #![allow(deprecated)] -use crate::{address_h256_to_erdrs, Interactor}; +use crate::Interactor; use log::info; use multiversx_sc_scenario::{ api::StaticApi, @@ -20,9 +20,9 @@ impl Interactor { } pub async fn perform_sc_query(&mut self, step: &mut ScQueryStep) { - let sc_address = address_h256_to_erdrs(&step.tx.to.to_address()); - let req = VmValueRequest { - sc_address: sc_address.clone(), + let sc_address = step.tx.to.to_address(); + let req = VmValueRequest { + sc_address: sc_address.clone().into(), func_name: step.tx.function.clone(), args: step .tx @@ -30,7 +30,7 @@ impl Interactor { .iter() .map(|arg| hex::encode(&arg.value)) .collect(), - caller: sc_address, + caller: sc_address.into(), value: "0".to_string(), }; let result = self diff --git a/framework/snippets/src/interactor_sender.rs b/framework/snippets/src/interactor_sender.rs index 86d6abcada..75d8e01198 100644 --- a/framework/snippets/src/interactor_sender.rs +++ b/framework/snippets/src/interactor_sender.rs @@ -2,7 +2,7 @@ use log::debug; use multiversx_sc_scenario::multiversx_sc::types::Address; use multiversx_sdk::{data::transaction::Transaction, wallet::Wallet}; -use crate::{address_h256_to_erdrs, Interactor}; +use crate::Interactor; /// A user account that can sign transactions (a pem is present). pub struct Sender { @@ -13,10 +13,9 @@ pub struct Sender { impl Interactor { pub async fn recall_nonce(&self, address: &Address) -> u64 { - let erdrs_address = address_h256_to_erdrs(address); let account = self .proxy - .get_account(&erdrs_address) + .get_account(&address.clone().into()) .await .expect("failed to retrieve account nonce"); account.nonce diff --git a/sdk/core/Cargo.toml b/sdk/core/Cargo.toml index 1dc7b129f5..283f74c2d0 100644 --- a/sdk/core/Cargo.toml +++ b/sdk/core/Cargo.toml @@ -38,4 +38,8 @@ log = "0.4.17" scrypt = "0.11" aes = "0.8" ctr = "0.9.2" -uuid = {version = "1.10.0", features = ["v4"]} \ No newline at end of file +uuid = {version = "1.10.0", features = ["v4"]} + +[dependencies.multiversx-chain-core] +version = "=0.10.0" +path = "../../vm-core" diff --git a/sdk/core/src/data/mod.rs b/sdk/core/src/data.rs similarity index 100% rename from sdk/core/src/data/mod.rs rename to sdk/core/src/data.rs diff --git a/sdk/core/src/data/address.rs b/sdk/core/src/data/address.rs index 49d92ea608..5b0946be6d 100644 --- a/sdk/core/src/data/address.rs +++ b/sdk/core/src/data/address.rs @@ -40,6 +40,18 @@ impl Address { } } +impl From for Address { + fn from(value: multiversx_chain_core::types::Address) -> Self { + Address(value.as_array().clone()) + } +} + +impl From
for multiversx_chain_core::types::Address { + fn from(value: Address) -> Self { + multiversx_chain_core::types::Address::new(value.0) + } +} + impl<'a> From<&'a PublicKey> for Address { fn from(public_key: &PublicKey) -> Address { let bytes = public_key.to_bytes(); From d3d9472832f02118a47e6bf78585e677e2e2356d Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 30 Sep 2024 14:22:35 +0300 Subject: [PATCH 28/30] clippy fix --- sdk/core/src/data/address.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/src/data/address.rs b/sdk/core/src/data/address.rs index 5b0946be6d..f41723219d 100644 --- a/sdk/core/src/data/address.rs +++ b/sdk/core/src/data/address.rs @@ -42,7 +42,7 @@ impl Address { impl From for Address { fn from(value: multiversx_chain_core::types::Address) -> Self { - Address(value.as_array().clone()) + Address(*value.as_array()) } } From 64a217471f0395f9fccb53cb321bd5ce24c59412 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 30 Sep 2024 15:06:31 +0300 Subject: [PATCH 29/30] cleanup --- framework/snippets/src/interactor.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/framework/snippets/src/interactor.rs b/framework/snippets/src/interactor.rs index b19c3e79d0..41a4db046e 100644 --- a/framework/snippets/src/interactor.rs +++ b/framework/snippets/src/interactor.rs @@ -43,7 +43,6 @@ impl Interactor { pub async fn register_wallet(&mut self, wallet: Wallet) -> Address { let wallet_address = wallet.address(); - // let address = erdrs_address_to_h256(wallet.address()); self.proxy .send_user_funds(&wallet_address.to_bech32_string().unwrap()) .await From 7e72cdfde688264b19f524272e8e127711f1c63c Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 30 Sep 2024 15:06:53 +0300 Subject: [PATCH 30/30] adder interactor config default back to devnet --- contracts/examples/adder/interact/config.toml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/contracts/examples/adder/interact/config.toml b/contracts/examples/adder/interact/config.toml index 64846243cc..c6f9a97a1d 100644 --- a/contracts/examples/adder/interact/config.toml +++ b/contracts/examples/adder/interact/config.toml @@ -1,2 +1,5 @@ -chain_type = 'simulator' -gateway_uri = 'http://localhost:8085' +# chain_type = 'simulator' +# gateway_uri = 'http://localhost:8085' + +chain_type = 'real' +gateway_uri = 'https://devnet-gateway.multiversx.com'