Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interactor - chain simulator refactor #1798

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

6 changes: 1 addition & 5 deletions contracts/examples/adder/interact/src/basic_interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ impl AdderInteract {
.await;

// generate blocks until ESDTSystemSCAddress is enabled
interactor
.proxy
.generate_blocks_until_epoch(1)
.await
.unwrap();
interactor.generate_blocks_until_epoch(1).await.unwrap();

Self {
interactor,
Expand Down
5 changes: 5 additions & 0 deletions framework/snippets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ base64 = "0.22"
log = "0.4.17"
env_logger = "0.11"
futures = "0.3"
anyhow = "1.0.44"

[dependencies.multiversx-sc-scenario]
version = "=0.53.1"
Expand All @@ -29,6 +30,10 @@ path = "../scenario"
version = "0.23.0"
path = "../../sdk/scenario-format"

[dependencies.multiversx-sdk]
version = "=0.6.1"
path = "../../sdk/core"

[dependencies.multiversx-sdk-http]
version = "=0.6.1"
path = "../../sdk/http"
Expand Down
8 changes: 5 additions & 3 deletions framework/snippets/src/account_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ pub async fn print_account_as_scenario_set_state(
use_chain_simulator: bool,
address_bech32_string: String,
) {
let api = GatewayHttpProxy::new(api_string, use_chain_simulator);
let api = GatewayHttpProxy::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 set_state =
retrieve_account_as_scenario_set_state(&api, use_chain_simulator, &address).await;
let scenario = build_scenario(set_state);
println!("{}", scenario.into_raw().to_json_string());
}
Expand All @@ -34,12 +35,13 @@ fn build_scenario(set_state: SetStateStep) -> Scenario {

pub async fn retrieve_account_as_scenario_set_state(
api: &GatewayHttpProxy,
use_chain_simulator: bool,
address: &Bech32Address,
) -> SetStateStep {
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, account_esdt_roles, account_storage) = if api.chain_simulator {
let (account_esdt, account_esdt_roles, account_storage) = if use_chain_simulator {
(HashMap::new(), HashMap::new(), HashMap::new())
} else {
let account_esdt = api
Expand Down
17 changes: 11 additions & 6 deletions framework/snippets/src/interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub const INTERACTOR_SCENARIO_TRACE_PATH: &str = "interactor_trace.scen.json";

pub struct Interactor {
pub proxy: GatewayHttpProxy,
pub use_chain_simulator: bool,
pub network_config: NetworkConfig,
pub sender_map: HashMap<Address, Sender>,

Expand All @@ -29,10 +30,11 @@ pub struct Interactor {

impl Interactor {
pub async fn new(gateway_uri: &str, use_chain_simulator: bool) -> Self {
let proxy = GatewayHttpProxy::new(gateway_uri.to_string(), use_chain_simulator);
let proxy = GatewayHttpProxy::new(gateway_uri.to_string());
let network_config = proxy.get_network_config().await.unwrap();
Self {
proxy,
use_chain_simulator,
network_config,
sender_map: HashMap::new(),
waiting_time_ms: 0,
Expand All @@ -44,10 +46,8 @@ impl Interactor {

pub async fn register_wallet(&mut self, wallet: Wallet) -> Address {
let wallet_address = wallet.address();
self.proxy
.send_user_funds(&wallet_address.to_bech32_string().unwrap())
.await
.unwrap();

self.send_user_funds(&wallet_address).await.unwrap();

let address: Address = wallet_address.into();
self.sender_map.insert(
Expand All @@ -72,7 +72,12 @@ impl Interactor {
}

pub async fn retrieve_account(&mut self, wallet_address: &Bech32Address) {
let set_state = retrieve_account_as_scenario_set_state(&self.proxy, wallet_address).await;
let set_state = retrieve_account_as_scenario_set_state(
&self.proxy,
self.use_chain_simulator,
wallet_address,
)
.await;
self.pre_runners.run_set_state_step(&set_state);
self.post_runners.run_set_state_step(&set_state);
}
Expand Down
57 changes: 57 additions & 0 deletions framework/snippets/src/interactor_chain_simulator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use anyhow::Error;
use multiversx_sdk::{
data::address::Address,
gateway::{
ChainSimulatorGenerateBlocksRequest, ChainSimulatorSendFundsRequest, GatewayAsyncService,
},
};

use crate::Interactor;

impl Interactor {
pub async fn send_user_funds(&self, receiver: &Address) -> Result<String, Error> {
if !self.use_chain_simulator {
return Ok(String::from("no-simulator"));
}

self.proxy
.request(ChainSimulatorSendFundsRequest::to_address(
receiver.to_bech32_string().unwrap(),
))
.await
}

pub async fn generate_blocks(&self, num_blocks: u64) -> Result<String, Error> {
if !self.use_chain_simulator {
return Ok(String::from("no-simulator"));
}

self.proxy
.request(ChainSimulatorGenerateBlocksRequest::num_blocks(num_blocks))
.await
}

pub async fn generate_blocks_until_epoch(&self, epoch_number: u64) -> Result<String, Error> {
if !self.use_chain_simulator {
return Ok(String::from("no-simulator"));
}

self.proxy
.request(ChainSimulatorGenerateBlocksRequest::until_epoch(
epoch_number,
))
.await
}

pub async fn generate_blocks_until_tx_processed(&self, tx_hash: &str) -> Result<String, Error> {
if !self.use_chain_simulator {
return Ok(String::from("no-simulator"));
}

self.proxy
.request(ChainSimulatorGenerateBlocksRequest::until_tx_processed(
tx_hash,
))
.await
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +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_until_tx_processed(&tx_hash)
self.generate_blocks_until_tx_processed(&tx_hash)
.await
.unwrap();
let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,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_until_tx_processed(&tx_hash)
self.generate_blocks_until_tx_processed(&tx_hash)
.await
.unwrap();
let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ 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)
self.generate_blocks_until_tx_processed(&tx_hash)
.await
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions framework/snippets/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod account_tool;
mod interactor;
mod interactor_chain_simulator;
mod interactor_dns;
mod interactor_scenario;
mod interactor_sender;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Interactor {
futures.push(self.proxy.retrieve_tx_on_network(tx_hash.clone()));
}

self.proxy.generate_blocks(4).await.unwrap();
self.generate_blocks(4).await.unwrap();
join_all(futures).await
}
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/http/examples/account.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use multiversx_sdk::data::address::Address;
use multiversx_sdk_http::{GatewayHttpProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY};
use multiversx_sdk_http::{GatewayHttpProxy, DEVNET_GATEWAY};

#[tokio::main]
async fn main() {
Expand All @@ -8,7 +8,7 @@ async fn main() {
)
.unwrap();

let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR);
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
let account = blockchain.get_account(&addr).await.unwrap();

println!("account: {account:#?}");
Expand Down
4 changes: 2 additions & 2 deletions sdk/http/examples/account_storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use multiversx_sdk::data::address::Address;
use multiversx_sdk_http::{GatewayHttpProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY};
use multiversx_sdk_http::{GatewayHttpProxy, DEVNET_GATEWAY};

#[tokio::main]
async fn main() {
Expand All @@ -8,7 +8,7 @@ async fn main() {
)
.unwrap();

let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR);
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
let account_storage = blockchain.get_account_storage_keys(&addr).await.unwrap();

println!("Account Storage: {account_storage:#?}");
Expand Down
4 changes: 2 additions & 2 deletions sdk/http/examples/get_esdt_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use multiversx_sdk::data::address::Address;
use multiversx_sdk_http::{GatewayHttpProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY};
use multiversx_sdk_http::{GatewayHttpProxy, DEVNET_GATEWAY};

#[tokio::main]
async fn main() {
Expand All @@ -8,7 +8,7 @@ async fn main() {
)
.unwrap();

let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR);
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
let balances = blockchain.get_account_esdt_tokens(&addr).await.unwrap();

println!("{balances:#?}");
Expand Down
4 changes: 2 additions & 2 deletions sdk/http/examples/get_hyper_block_by_hash.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use multiversx_sdk_http::{GatewayHttpProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY};
use multiversx_sdk_http::{GatewayHttpProxy, DEVNET_GATEWAY};

#[tokio::main]
async fn main() {
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR);
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
let result = blockchain
.get_hyper_block_by_hash("d59e0dc7d407b1175655357cb8056ec3bb77961192753cddda2fb700c6ce71c6")
.await;
Expand Down
4 changes: 2 additions & 2 deletions sdk/http/examples/get_hyper_block_by_nonce.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use multiversx_sdk_http::{GatewayHttpProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY};
use multiversx_sdk_http::{GatewayHttpProxy, DEVNET_GATEWAY};

#[tokio::main]
async fn main() {
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR);
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
let result = blockchain.get_hyper_block_by_nonce(7468).await;

println!("block by nonce result: {result:#?}")
Expand Down
4 changes: 2 additions & 2 deletions sdk/http/examples/get_hyper_block_latest.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use multiversx_sdk_http::{GatewayHttpProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY};
use multiversx_sdk_http::{GatewayHttpProxy, DEVNET_GATEWAY};

#[tokio::main]
async fn main() {
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR);
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
let result = blockchain.get_latest_hyper_block_nonce().await;

println!("latest block result: {result:?}")
Expand Down
4 changes: 2 additions & 2 deletions sdk/http/examples/get_network_config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use multiversx_sdk_http::{GatewayHttpProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY};
use multiversx_sdk_http::{GatewayHttpProxy, DEVNET_GATEWAY};

#[tokio::main]
async fn main() {
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR);
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
let network_config = blockchain.get_network_config().await.unwrap();

println!("network_config: {network_config:#?}")
Expand Down
4 changes: 2 additions & 2 deletions sdk/http/examples/get_network_economics.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use multiversx_sdk_http::{GatewayHttpProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY};
use multiversx_sdk_http::{GatewayHttpProxy, DEVNET_GATEWAY};

#[tokio::main]
async fn main() {
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR);
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
let network_economics = blockchain.get_network_economics().await.unwrap();

println!("network_economics: {network_economics:#?}")
Expand Down
4 changes: 2 additions & 2 deletions sdk/http/examples/sign_tx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use multiversx_sdk::{data::transaction::Transaction, wallet::Wallet};
use multiversx_sdk_http::{GatewayHttpProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY};
use multiversx_sdk_http::{GatewayHttpProxy, DEVNET_GATEWAY};

#[tokio::main]
async fn main() {
Expand All @@ -8,7 +8,7 @@ async fn main() {
)
.unwrap();
let addr = wl.address();
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR);
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
let network_config = blockchain.get_network_config().await.unwrap();

let arg = blockchain
Expand Down
4 changes: 2 additions & 2 deletions sdk/http/examples/sign_txs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use multiversx_sdk::{data::transaction::Transaction, wallet::Wallet};
use multiversx_sdk_http::{GatewayHttpProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY};
use multiversx_sdk_http::{GatewayHttpProxy, DEVNET_GATEWAY};

#[tokio::main]
async fn main() {
Expand All @@ -8,7 +8,7 @@ async fn main() {
)
.unwrap();
let addr = wl.address();
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR);
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
let network_config = blockchain.get_network_config().await.unwrap();

let arg = blockchain
Expand Down
4 changes: 2 additions & 2 deletions sdk/http/examples/tx_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use multiversx_sdk::{
data::{address::Address, transaction::Transaction},
utils::base64_encode,
};
use multiversx_sdk_http::{GatewayHttpProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY};
use multiversx_sdk_http::{GatewayHttpProxy, DEVNET_GATEWAY};

#[tokio::main]
async fn main() {
Expand All @@ -26,7 +26,7 @@ async fn main() {
signature: None,
};

let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR);
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
let cost = blockchain.request_transaction_cost(&tx).await.unwrap();

println!("tx cost: {cost:#?}");
Expand Down
4 changes: 2 additions & 2 deletions sdk/http/examples/tx_default_args.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use multiversx_sdk::data::address::Address;
use multiversx_sdk_http::{GatewayHttpProxy, DEFAULT_USE_CHAIN_SIMULATOR, DEVNET_GATEWAY};
use multiversx_sdk_http::{GatewayHttpProxy, DEVNET_GATEWAY};

#[tokio::main]
async fn main() {
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string(), DEFAULT_USE_CHAIN_SIMULATOR);
let blockchain = GatewayHttpProxy::new(DEVNET_GATEWAY.to_string());
let network_config = blockchain.get_network_config().await.unwrap();
let addr = Address::from_bech32_string(
"erd1qqqqqqqqqqqqqpgqfzydqmdw7m2vazsp6u5p95yxz76t2p9rd8ss0zp9ts",
Expand Down
Loading
Loading