diff --git a/Cargo.lock b/Cargo.lock index 8210b20eb5..a7bc64efd5 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -1977,12 +1977,14 @@ dependencies = [ "copy_dir", "multiversx-sc", "multiversx-sc-meta-lib", + "multiversx-sc-snippets", "pathdiff", "reqwest", "ruplacer", "semver", "serde", "serde_json", + "tokio", "toml", "zip", ] @@ -2049,6 +2051,7 @@ dependencies = [ "futures", "hex", "log", + "multiversx-chain-scenario-format", "multiversx-sc-scenario", "multiversx-sdk", "tokio", diff --git a/framework/meta/Cargo.toml b/framework/meta/Cargo.toml index 4f1aa79634..029bbfdb6e 100644 --- a/framework/meta/Cargo.toml +++ b/framework/meta/Cargo.toml @@ -26,6 +26,7 @@ template-test-released = [] [dependencies] clap = { version = "4.4.7", features = ["derive"] } +tokio = { version = "1.24", features = ["full"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" toml = { version = "0.8.6", features = ["preserve_order"] } @@ -39,13 +40,15 @@ copy_dir = "0.1.2" pathdiff = "0.2.1" common-path = "1.0.0" +[dependencies.multiversx-sc-meta-lib] +version = "=0.50.3" +path = "../meta-lib" + [dependencies.multiversx-sc] version = "=0.50.3" path = "../base" features = ["alloc", "num-bigint"] - -[dependencies.multiversx-sc-meta-lib] +[dependencies.multiversx-sc-snippets] version = "=0.50.3" -path = "../meta-lib" - +path = "../snippets" diff --git a/framework/meta/src/cli/cli_args_standalone.rs b/framework/meta/src/cli/cli_args_standalone.rs index de6d46fac4..e66c83deb4 100644 --- a/framework/meta/src/cli/cli_args_standalone.rs +++ b/framework/meta/src/cli/cli_args_standalone.rs @@ -29,6 +29,9 @@ pub struct StandaloneCliArgs { #[derive(Clone, PartialEq, Eq, Debug, Subcommand)] pub enum StandaloneCliAction { + #[command(name = "install", about = "Installs framework dependencies")] + Install(InstallArgs), + #[command( about = "General info about the contract an libraries residing in the targetted directory.." )] @@ -44,12 +47,6 @@ pub enum StandaloneCliAction { )] Upgrade(UpgradeArgs), - #[command( - name = "local-deps", - about = "Generates a report on the local depedencies of contract crates. Will explore indirect depdencies too." - )] - LocalDeps(LocalDepsArgs), - #[command(name = "new", about = "Creates a contract by a pre-existing template")] Template(TemplateArgs), @@ -68,8 +65,16 @@ pub enum StandaloneCliAction { #[command(name = "test-coverage", about = "Run test coverage and output report")] TestCoverage(TestCoverageArgs), - #[command(name = "install", about = "Installs framework dependencies")] - Install(InstallArgs), + #[command( + about = "Generates a scenario test initialized with real data fetched from the blockchain." + )] + Account(AccountArgs), + + #[command( + name = "local-deps", + about = "Generates a report on the local depedencies of contract crates. Will explore indirect depdencies too." + )] + LocalDeps(LocalDepsArgs), } #[derive(Default, Clone, PartialEq, Eq, Debug, Args)] @@ -326,3 +331,15 @@ pub struct InstallWasm32Args {} #[derive(Default, Clone, PartialEq, Eq, Debug, Args)] 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 + #[arg(long = "api")] + #[clap(global = true)] + pub api: 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/cli/cli_standalone_main.rs b/framework/meta/src/cli/cli_standalone_main.rs index f9d8b3d6b8..6c8c6a67b0 100644 --- a/framework/meta/src/cli/cli_standalone_main.rs +++ b/framework/meta/src/cli/cli_standalone_main.rs @@ -1,4 +1,5 @@ use crate::cli::{StandaloneCliAction, StandaloneCliArgs}; +use crate::cmd::retrieve_address::retrieve_address; use clap::Parser; use crate::cmd::all::call_all_meta; @@ -12,17 +13,15 @@ use crate::cmd::test_coverage::test_coverage; use crate::cmd::upgrade::upgrade_sc; /// Entry point in the program when calling it as a standalone tool. -pub fn cli_main_standalone() { +pub async fn cli_main_standalone() { let cli_args = StandaloneCliArgs::parse(); match &cli_args.command { Some(StandaloneCliAction::Info(args)) => call_info(args), + Some(StandaloneCliAction::Install(args)) => install(args), Some(StandaloneCliAction::All(args)) => call_all_meta(args), Some(StandaloneCliAction::Upgrade(args)) => { upgrade_sc(args); }, - Some(StandaloneCliAction::LocalDeps(args)) => { - local_deps(args); - }, Some(StandaloneCliAction::Template(args)) => { create_contract(args); }, @@ -36,7 +35,12 @@ pub fn cli_main_standalone() { Some(StandaloneCliAction::TestCoverage(args)) => { test_coverage(args); }, - Some(StandaloneCliAction::Install(args)) => install(args), + Some(StandaloneCliAction::Account(args)) => { + retrieve_address(args).await; + }, + Some(StandaloneCliAction::LocalDeps(args)) => { + local_deps(args); + }, None => {}, } } diff --git a/framework/meta/src/cmd.rs b/framework/meta/src/cmd.rs index 698f10c924..a588f90162 100644 --- a/framework/meta/src/cmd.rs +++ b/framework/meta/src/cmd.rs @@ -3,6 +3,7 @@ pub mod info; pub mod install; pub mod local_deps; pub mod print_util; +pub mod retrieve_address; pub mod scen_test_gen; pub mod template; pub mod test; diff --git a/framework/meta/src/cmd/retrieve_address.rs b/framework/meta/src/cmd/retrieve_address.rs new file mode 100644 index 0000000000..f20b5b8e05 --- /dev/null +++ b/framework/meta/src/cmd/retrieve_address.rs @@ -0,0 +1,9 @@ +use multiversx_sc_snippets::account_tool; + +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; +} diff --git a/framework/meta/src/main.rs b/framework/meta/src/main.rs index b3ecec7201..bf9813b1d0 100644 --- a/framework/meta/src/main.rs +++ b/framework/meta/src/main.rs @@ -1,3 +1,4 @@ -fn main() { - multiversx_sc_meta::cli::cli_main_standalone(); +#[tokio::main] +async fn main() { + multiversx_sc_meta::cli::cli_main_standalone().await; } diff --git a/framework/scenario/Cargo.toml b/framework/scenario/Cargo.toml index d6671523d0..b41d274621 100644 --- a/framework/scenario/Cargo.toml +++ b/framework/scenario/Cargo.toml @@ -33,10 +33,6 @@ clap = { version = "4.4.7", features = ["derive"] } tokio = { version = "1.24", features = ["full"] } unwrap-infallible = "0.1.5" -[[bin]] -name = "sc-scenario" -path = "src/main.rs" - [features] run-go-tests = [] diff --git a/framework/scenario/src/imports.rs b/framework/scenario/src/imports.rs index 9eac89fc70..0da1a2d072 100644 --- a/framework/scenario/src/imports.rs +++ b/framework/scenario/src/imports.rs @@ -20,8 +20,6 @@ pub use crate::{ ScenarioRunner, }, scenario_format::interpret_trait::{InterpretableFrom, InterpreterContext}, - standalone::retrieve_account_as_scenario_set_state, - test_wallets, whitebox_legacy::*, ScenarioTxRun, }; diff --git a/framework/scenario/src/lib.rs b/framework/scenario/src/lib.rs index e79d159f1e..cfac7eaa50 100644 --- a/framework/scenario/src/lib.rs +++ b/framework/scenario/src/lib.rs @@ -8,8 +8,6 @@ mod facade; pub mod managed_test_util; pub mod scenario; pub mod scenario_macros; -pub mod standalone; -pub mod test_wallets; mod vm_go_tool; pub mod whitebox_legacy; diff --git a/framework/scenario/src/main.rs b/framework/scenario/src/main.rs deleted file mode 100644 index 7de76cd7cf..0000000000 --- a/framework/scenario/src/main.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[tokio::main] -async fn main() { - multiversx_sc_scenario::standalone::cli_main().await; -} diff --git a/framework/scenario/src/standalone/mod.rs b/framework/scenario/src/standalone/mod.rs deleted file mode 100644 index 84ecea3916..0000000000 --- a/framework/scenario/src/standalone/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod account_tool; -mod scenario_cli; - -pub use account_tool::retrieve_account_as_scenario_set_state; -pub use scenario_cli::cli_main; diff --git a/framework/scenario/src/standalone/scenario_cli.rs b/framework/scenario/src/standalone/scenario_cli.rs deleted file mode 100644 index 624b316ef0..0000000000 --- a/framework/scenario/src/standalone/scenario_cli.rs +++ /dev/null @@ -1,51 +0,0 @@ -use clap::{Args, Parser, Subcommand}; -use multiversx_sdk::blockchain::CommunicationProxy; - -use crate::imports::Bech32Address; - -use super::account_tool; - -/// Parsed arguments of the meta crate CLI. -#[derive(Default, PartialEq, Eq, Debug, Parser)] -#[command(version, about)] -#[command(propagate_version = true)] -pub struct ScenarioCliArgs { - /// Provide the target API you want the real data to come from - #[arg(long = "api")] - #[clap(global = true)] - pub api: Option, - - #[command(subcommand)] - pub command: Option, -} - -#[derive(Clone, PartialEq, Eq, Debug, Subcommand)] -pub enum ScenarioCliAction { - #[command( - about = "Generates a scenario test initialized with real data fetched from the blockchain." - )] - Account(AccountArgs), -} - -#[derive(Default, Clone, PartialEq, Eq, Debug, Args)] -pub struct AccountArgs { - /// Provide the address you want to retrieve data from - #[arg(long = "address", verbatim_doc_comment)] - pub address: String, -} - -/// Entry point in the program when calling it as a standalone tool. -pub async fn cli_main() { - let cli_args = ScenarioCliArgs::parse(); - let api = CommunicationProxy::new(cli_args.api.expect("API needs tp be specified")); - match &cli_args.command { - Some(ScenarioCliAction::Account(args)) => { - account_tool::print_account_as_scenario_set_state( - &api, - &Bech32Address::from_bech32_string(args.address.to_string()), - ) - .await; - }, - None => {}, - } -} diff --git a/framework/snippets/Cargo.toml b/framework/snippets/Cargo.toml index 49961c84de..495e98425e 100644 --- a/framework/snippets/Cargo.toml +++ b/framework/snippets/Cargo.toml @@ -25,6 +25,10 @@ futures = "0.3" version = "=0.50.3" path = "../scenario" +[dependencies.multiversx-chain-scenario-format] +version = "0.22.2" +path = "../../sdk/scenario-format" + [dependencies.multiversx-sdk] version = "=0.4.1" path = "../../sdk/core" diff --git a/framework/scenario/src/standalone/account_tool.rs b/framework/snippets/src/account_tool.rs similarity index 82% rename from framework/scenario/src/standalone/account_tool.rs rename to framework/snippets/src/account_tool.rs index b6db384b91..db1f14b4af 100644 --- a/framework/scenario/src/standalone/account_tool.rs +++ b/framework/snippets/src/account_tool.rs @@ -1,20 +1,25 @@ -use crate::{ +use multiversx_chain_scenario_format::interpret_trait::IntoRaw; +use multiversx_sc_scenario::{ imports::Bech32Address, - scenario_model::{Account, BytesKey, BytesValue, Scenario, SetStateStep}, + scenario_model::{Account, BytesKey, BytesValue, Scenario, SetStateStep, Step}, }; - -use multiversx_chain_scenario_format::interpret_trait::IntoRaw; use multiversx_sdk::{ blockchain::CommunicationProxy, data::{address::Address, esdt::EsdtBalance}, }; use std::collections::{BTreeMap, HashMap}; +/// Called directly from CLI, from `sc-meta`. +/// +/// Retrieves an account data via the API, +/// then formats it as a scenario set state step. pub async fn print_account_as_scenario_set_state( - api: &CommunicationProxy, - address: &Bech32Address, + api_string: String, + address_bech32_string: String, ) { - let set_state = retrieve_account_as_scenario_set_state(api, address).await; + let api = CommunicationProxy::new(api_string); + 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); println!("{}", scenario.into_raw().to_json_string()); } @@ -24,7 +29,7 @@ fn build_scenario(set_state: SetStateStep) -> Scenario { name: None, comment: None, check_gas: None, - steps: vec![crate::scenario_model::Step::SetState(set_state)], + steps: vec![Step::SetState(set_state)], } } @@ -61,7 +66,7 @@ pub async fn retrieve_account_as_scenario_set_state( set_state_step.put_account(address, account_state) } -pub fn set_account( +fn set_account( account: multiversx_sdk::data::account::Account, account_storage: HashMap, account_esdt: HashMap, @@ -71,7 +76,7 @@ pub fn set_account( .nonce(account.nonce) .balance(account.balance.as_str()) .code(account.code); - account_state.username = Some(account.username.as_str().into()); + account_state.username = Some(format!("str:{}", account.username.as_str()).into()); account_state.storage = convert_storage(account_storage); for (_, esdt_balance) in account_esdt.iter() { diff --git a/framework/snippets/src/imports.rs b/framework/snippets/src/imports.rs index b031e0769c..f2b0e6a5f9 100644 --- a/framework/snippets/src/imports.rs +++ b/framework/snippets/src/imports.rs @@ -1,6 +1,8 @@ pub use crate::multiversx_sc_scenario::imports::*; -pub use crate::{dns_address_for_name, Interactor, InteractorPrepareAsync, StepBuffer}; +pub use crate::{ + dns_address_for_name, test_wallets, Interactor, InteractorPrepareAsync, StepBuffer, +}; pub use env_logger; pub use tokio; diff --git a/framework/snippets/src/interactor.rs b/framework/snippets/src/interactor.rs index 35d53a4a86..82c59752f6 100644 --- a/framework/snippets/src/interactor.rs +++ b/framework/snippets/src/interactor.rs @@ -1,5 +1,5 @@ use multiversx_sc_scenario::{ - imports::{retrieve_account_as_scenario_set_state, Bech32Address, ScenarioRunner}, + imports::{Bech32Address, ScenarioRunner}, mandos_system::{run_list::ScenarioRunnerList, run_trace::ScenarioTraceFile}, multiversx_sc::types::Address, scenario_model::AddressValue, @@ -15,7 +15,7 @@ use std::{ time::Duration, }; -use crate::Sender; +use crate::{account_tool::retrieve_account_as_scenario_set_state, Sender}; pub const INTERACTOR_SCENARIO_TRACE_PATH: &str = "interactor_trace.scen.json"; diff --git a/framework/snippets/src/lib.rs b/framework/snippets/src/lib.rs index b94a6517d9..07c5789d8c 100644 --- a/framework/snippets/src/lib.rs +++ b/framework/snippets/src/lib.rs @@ -1,3 +1,4 @@ +pub mod account_tool; mod interactor; mod interactor_dns; mod interactor_retrieve; @@ -5,6 +6,7 @@ mod interactor_scenario; mod interactor_sender; mod interactor_tx; mod multi; +pub mod test_wallets; pub use env_logger; pub use hex; diff --git a/framework/scenario/src/test_wallets/mod.rs b/framework/snippets/src/test_wallets.rs similarity index 60% rename from framework/scenario/src/test_wallets/mod.rs rename to framework/snippets/src/test_wallets.rs index 437f5c829f..341f93ef29 100644 --- a/framework/scenario/src/test_wallets/mod.rs +++ b/framework/snippets/src/test_wallets.rs @@ -6,60 +6,60 @@ fn test_wallet(pem_file_contents: &str) -> Wallet { /// Test wallet. Do not use on mainnet. pub fn alice() -> Wallet { - test_wallet(include_str!("alice.pem")) + test_wallet(include_str!("test_wallets/alice.pem")) } /// Test wallet. Do not use on mainnet. pub fn bob() -> Wallet { - test_wallet(include_str!("bob.pem")) + test_wallet(include_str!("test_wallets/bob.pem")) } /// Test wallet. Do not use on mainnet. pub fn carol() -> Wallet { - test_wallet(include_str!("carol.pem")) + test_wallet(include_str!("test_wallets/carol.pem")) } /// Test wallet. Do not use on mainnet. pub fn dan() -> Wallet { - test_wallet(include_str!("dan.pem")) + test_wallet(include_str!("test_wallets/dan.pem")) } /// Test wallet. Do not use on mainnet. pub fn eve() -> Wallet { - test_wallet(include_str!("eve.pem")) + test_wallet(include_str!("test_wallets/eve.pem")) } /// Test wallet. Do not use on mainnet. pub fn frank() -> Wallet { - test_wallet(include_str!("frank.pem")) + test_wallet(include_str!("test_wallets/frank.pem")) } /// Test wallet. Do not use on mainnet. pub fn grace() -> Wallet { - test_wallet(include_str!("grace.pem")) + test_wallet(include_str!("test_wallets/grace.pem")) } /// Test wallet. Do not use on mainnet. pub fn heidi() -> Wallet { - test_wallet(include_str!("heidi.pem")) + test_wallet(include_str!("test_wallets/heidi.pem")) } /// Test wallet. Do not use on mainnet. pub fn ivan() -> Wallet { - test_wallet(include_str!("ivan.pem")) + test_wallet(include_str!("test_wallets/ivan.pem")) } /// Test wallet. Do not use on mainnet. pub fn judy() -> Wallet { - test_wallet(include_str!("judy.pem")) + test_wallet(include_str!("test_wallets/judy.pem")) } /// Test wallet. Do not use on mainnet. pub fn mallory() -> Wallet { - test_wallet(include_str!("mallory.pem")) + test_wallet(include_str!("test_wallets/mallory.pem")) } /// Test wallet. Do not use on mainnet. pub fn mike() -> Wallet { - test_wallet(include_str!("mike.pem")) + test_wallet(include_str!("test_wallets/mike.pem")) } diff --git a/framework/scenario/src/test_wallets/alice.pem b/framework/snippets/src/test_wallets/alice.pem similarity index 100% rename from framework/scenario/src/test_wallets/alice.pem rename to framework/snippets/src/test_wallets/alice.pem diff --git a/framework/scenario/src/test_wallets/bob.pem b/framework/snippets/src/test_wallets/bob.pem similarity index 100% rename from framework/scenario/src/test_wallets/bob.pem rename to framework/snippets/src/test_wallets/bob.pem diff --git a/framework/scenario/src/test_wallets/carol.pem b/framework/snippets/src/test_wallets/carol.pem similarity index 100% rename from framework/scenario/src/test_wallets/carol.pem rename to framework/snippets/src/test_wallets/carol.pem diff --git a/framework/scenario/src/test_wallets/dan.pem b/framework/snippets/src/test_wallets/dan.pem similarity index 100% rename from framework/scenario/src/test_wallets/dan.pem rename to framework/snippets/src/test_wallets/dan.pem diff --git a/framework/scenario/src/test_wallets/eve.pem b/framework/snippets/src/test_wallets/eve.pem similarity index 100% rename from framework/scenario/src/test_wallets/eve.pem rename to framework/snippets/src/test_wallets/eve.pem diff --git a/framework/scenario/src/test_wallets/frank.pem b/framework/snippets/src/test_wallets/frank.pem similarity index 100% rename from framework/scenario/src/test_wallets/frank.pem rename to framework/snippets/src/test_wallets/frank.pem diff --git a/framework/scenario/src/test_wallets/grace.pem b/framework/snippets/src/test_wallets/grace.pem similarity index 100% rename from framework/scenario/src/test_wallets/grace.pem rename to framework/snippets/src/test_wallets/grace.pem diff --git a/framework/scenario/src/test_wallets/heidi.pem b/framework/snippets/src/test_wallets/heidi.pem similarity index 100% rename from framework/scenario/src/test_wallets/heidi.pem rename to framework/snippets/src/test_wallets/heidi.pem diff --git a/framework/scenario/src/test_wallets/ivan.pem b/framework/snippets/src/test_wallets/ivan.pem similarity index 100% rename from framework/scenario/src/test_wallets/ivan.pem rename to framework/snippets/src/test_wallets/ivan.pem diff --git a/framework/scenario/src/test_wallets/judy.pem b/framework/snippets/src/test_wallets/judy.pem similarity index 100% rename from framework/scenario/src/test_wallets/judy.pem rename to framework/snippets/src/test_wallets/judy.pem diff --git a/framework/scenario/src/test_wallets/mallory.pem b/framework/snippets/src/test_wallets/mallory.pem similarity index 100% rename from framework/scenario/src/test_wallets/mallory.pem rename to framework/snippets/src/test_wallets/mallory.pem diff --git a/framework/scenario/src/test_wallets/mike.pem b/framework/snippets/src/test_wallets/mike.pem similarity index 100% rename from framework/scenario/src/test_wallets/mike.pem rename to framework/snippets/src/test_wallets/mike.pem