Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
pepoviola committed Oct 23, 2024
1 parent 1c8a35e commit af5db13
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 79 deletions.
142 changes: 77 additions & 65 deletions crates/examples/examples/para_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

use anyhow::anyhow;
use std::env;
use zombienet_sdk::{tx_helper::{
RuntimeUpgradeOptions, ChainUpgrade},
NetworkConfigBuilder
};

use anyhow::anyhow;
use zombienet_sdk::{
tx_helper::{ChainUpgrade, RuntimeUpgradeOptions},
NetworkConfigBuilder,
};

const BEST_BLOCK_METRIC: &str = "block_height{status=\"best\"}";

Expand All @@ -18,48 +18,57 @@ async fn main() -> Result<(), anyhow::Error> {
// allow to pass the upgrade path through first cli argument
let args: Vec<_> = env::args().collect();

let images = zombienet_sdk::environment::get_images_from_env();
let config = NetworkConfigBuilder::new()
.with_relaychain(|r| {
r.with_chain("rococo-local")
.with_default_command("polkadot")
let images = zombienet_sdk::environment::get_images_from_env();
let config = NetworkConfigBuilder::new()
.with_relaychain(|r| {
r.with_chain("rococo-local")
.with_default_command("polkadot")
.with_default_image(images.polkadot.as_str())
.with_node(|node| {
node.with_name("alice")
})
.with_node(|node| node.with_name("bob"))
})
.with_node(|node| node.with_name("alice"))
.with_node(|node| node.with_name("bob"))
})
.with_parachain(|p| {
p.with_id(100)
.with_default_command("test-parachain")
.with_default_image(images.cumulus.as_str())
.with_collator(|c|{
c.with_name("collator")
})
.with_default_command("test-parachain")
.with_default_image(images.cumulus.as_str())
.with_collator(|c| c.with_name("collator"))
})
.build()
.map_err(|e| {
let errs = e.into_iter().map(|e| e.to_string()).collect::<Vec<_>>().join(" ");
anyhow!("config errs: {errs}")
})?;

let spawn_fn = zombienet_sdk::environment::get_spawn_fn();
let network = spawn_fn(config).await?;

// wait 2 blocks
let alice = network.get_node("alice")?;
assert!(alice.wait_metric(BEST_BLOCK_METRIC, |b| b > 2_f64).await.is_ok());

// get current runtime spec
let client = network.get_node("collator")?.client::<subxt::PolkadotConfig>().await?;
let current_runtime = client.backend().current_runtime_version().await?;
println!("current_runtime spec version: {:?}", current_runtime.spec_version);

// get current best
let best_block = alice.reports(BEST_BLOCK_METRIC).await?;

// upgrade runtime
let wasm = if args.len() > 1 {
.build()
.map_err(|e| {
let errs = e
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join(" ");
anyhow!("config errs: {errs}")
})?;

let spawn_fn = zombienet_sdk::environment::get_spawn_fn();
let network = spawn_fn(config).await?;

// wait 2 blocks
let alice = network.get_node("alice")?;
assert!(alice
.wait_metric(BEST_BLOCK_METRIC, |b| b > 2_f64)
.await
.is_ok());

// get current runtime spec
let client = network
.get_node("collator")?
.client::<subxt::PolkadotConfig>()
.await?;
let current_runtime = client.backend().current_runtime_version().await?;
println!(
"current_runtime spec version: {:?}",
current_runtime.spec_version
);

// get current best
let best_block = alice.reports(BEST_BLOCK_METRIC).await?;

// upgrade runtime
let wasm = if args.len() > 1 {
args[1].clone()
} else if env::var("ZOMBIE_WASM_INCREMENTED_PATH").is_ok() {
env::var("ZOMBIE_WASM_INCREMENTED_PATH").unwrap()
Expand All @@ -69,25 +78,28 @@ async fn main() -> Result<(), anyhow::Error> {

println!("Perfoming upgrade from path {wasm}");


network
.parachain(100).expect("Invalid parachain Id")
.runtime_upgrade(RuntimeUpgradeOptions::new(wasm.as_str().into()))
.await?;

// wait 2 more blocks
alice
.wait_metric(BEST_BLOCK_METRIC, |x| x > best_block + 2_f64)
.await?;

let incremented_runtime = client.backend().current_runtime_version().await?;
println!("incremented_runtime spec version: {}", incremented_runtime.spec_version);

assert_eq!(
incremented_runtime.spec_version,
current_runtime.spec_version + 1000,
"version should be incremented"
);

Ok(())
}
network
.parachain(100)
.expect("Invalid parachain Id")
.runtime_upgrade(RuntimeUpgradeOptions::new(wasm.as_str().into()))
.await?;

// wait 2 more blocks
alice
.wait_metric(BEST_BLOCK_METRIC, |x| x > best_block + 2_f64)
.await?;

let incremented_runtime = client.backend().current_runtime_version().await?;
println!(
"incremented_runtime spec version: {}",
incremented_runtime.spec_version
);

assert_eq!(
incremented_runtime.spec_version,
current_runtime.spec_version + 1000,
"version should be incremented"
);

Ok(())
}
2 changes: 1 addition & 1 deletion crates/orchestrator/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod chain_upgrade;
pub mod node;
pub mod parachain;
pub mod relaychain;
pub mod chain_upgrade;

use std::{collections::HashMap, path::PathBuf};

Expand Down
14 changes: 7 additions & 7 deletions crates/orchestrator/src/network/chain_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use async_trait::async_trait;
use anyhow::anyhow;
use async_trait::async_trait;
use subxt_signer::{sr25519::Keypair, SecretUri};

use super::node::NetworkNode;
Expand All @@ -16,10 +16,7 @@ pub trait ChainUpgrade {
///
/// This call 'System.set_code_without_checks' wrapped in
/// 'Sudo.sudo_unchecked_weight'
async fn runtime_upgrade(
&self,
options: RuntimeUpgradeOptions,
) -> Result<(), anyhow::Error> {
async fn runtime_upgrade(&self, options: RuntimeUpgradeOptions) -> Result<(), anyhow::Error> {
// check if the node is valid first
let node = if let Some(node_name) = options.node_name {
if let Some(node) = self
Expand All @@ -29,7 +26,10 @@ pub trait ChainUpgrade {
{
node
} else {
return Err(anyhow!("Node: {} is not part of the set of nodes", node_name));
return Err(anyhow!(
"Node: {} is not part of the set of nodes",
node_name
));
}
} else {
// take the first node
Expand All @@ -54,4 +54,4 @@ pub trait ChainUpgrade {

Ok(())
}
}
}
4 changes: 1 addition & 3 deletions crates/orchestrator/src/network/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ use std::{
};

use async_trait::async_trait;

use provider::types::TransferedFile;
use serde::Serialize;
use subxt::{dynamic::Value, tx::TxStatus, OnlineClient, SubstrateConfig};
use subxt_signer::{sr25519::Keypair, SecretUri};
use support::{constants::THIS_IS_A_BUG, fs::FileSystem, net::wait_ws_ready};
use tracing::info;

use provider::types::TransferedFile;

use super::{chain_upgrade::ChainUpgrade, node::NetworkNode};
use crate::{
network_spec::parachain::ParachainSpec, shared::types::RegisterParachainOptions,
Expand Down
1 change: 0 additions & 1 deletion crates/orchestrator/src/network/relaychain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ impl Relaychain {
pub fn chain(&self) -> &str {
&self.chain
}

}
5 changes: 3 additions & 2 deletions crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ pub use orchestrator::{

// Helpers used for interact with the network
pub mod tx_helper {
pub use orchestrator::shared::types::RuntimeUpgradeOptions;
pub use orchestrator::network::chain_upgrade::ChainUpgrade;
pub use orchestrator::{
network::chain_upgrade::ChainUpgrade, shared::types::RuntimeUpgradeOptions,
};
}

use provider::{DockerProvider, KubernetesProvider, NativeProvider};
Expand Down

0 comments on commit af5db13

Please sign in to comment.