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

fix #274, refactor trait and make 'nodes' public again #277

Merged
merged 2 commits into from
Nov 26, 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
37 changes: 11 additions & 26 deletions crates/orchestrator/src/network/chain_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,22 @@ use crate::{shared::types::RuntimeUpgradeOptions, tx_helper};

#[async_trait]
pub trait ChainUpgrade {
/// Get a vec of [`NetworkNode`]
fn nodes(&self) -> Vec<&NetworkNode>;

/// Perform a runtime upgrade (with sudo)
///
/// This call 'System.set_code_without_checks' wrapped in
/// 'Sudo.sudo_unchecked_weight'
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
.nodes()
.into_iter()
.find(|node| node.name() == node_name)
{
node
} else {
return Err(anyhow!(
"Node: {} is not part of the set of nodes",
node_name
));
}
} else {
// take the first node
if let Some(node) = self.nodes().first() {
node
} else {
return Err(anyhow!("chain doesn't have any node!"));
}
};

async fn runtime_upgrade(&self, options: RuntimeUpgradeOptions) -> Result<(), anyhow::Error>;

/// Perform a runtime upgrade (with sudo), inner call with the node pass as arg.
///
/// This call 'System.set_code_without_checks' wrapped in
/// 'Sudo.sudo_unchecked_weight'
async fn perform_runtime_upgrade(
&self,
node: &NetworkNode,
options: RuntimeUpgradeOptions,
) -> Result<(), anyhow::Error> {
let sudo = if let Some(possible_seed) = options.seed {
Keypair::from_secret_key(possible_seed)
.map_err(|_| anyhow!("seed should return a Keypair"))?
Expand Down
31 changes: 28 additions & 3 deletions crates/orchestrator/src/network/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
str::FromStr,
};

use anyhow::anyhow;
use async_trait::async_trait;
use provider::types::TransferedFile;
use serde::Serialize;
Expand All @@ -13,7 +14,8 @@ use tracing::info;

use super::{chain_upgrade::ChainUpgrade, node::NetworkNode};
use crate::{
network_spec::parachain::ParachainSpec, shared::types::RegisterParachainOptions,
network_spec::parachain::ParachainSpec,
shared::types::{RegisterParachainOptions, RuntimeUpgradeOptions},
ScopedFilesystem,
};

Expand All @@ -29,8 +31,31 @@ pub struct Parachain {

#[async_trait]
impl ChainUpgrade for Parachain {
fn nodes(&self) -> Vec<&NetworkNode> {
self.collators.iter().collect()
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
.collators()
.into_iter()
.find(|node| node.name() == node_name)
{
node
} else {
return Err(anyhow!(
"Node: {} is not part of the set of nodes",
node_name
));
}
} else {
// take the first node
if let Some(node) = self.collators().first() {
node
} else {
return Err(anyhow!("chain doesn't have any node!"));
}
};

self.perform_runtime_upgrade(node, options).await
}
}

Expand Down
33 changes: 30 additions & 3 deletions crates/orchestrator/src/network/relaychain.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::path::PathBuf;

use anyhow::anyhow;
use async_trait::async_trait;
use serde::Serialize;

use super::node::NetworkNode;
use crate::network::chain_upgrade::ChainUpgrade;
use crate::{network::chain_upgrade::ChainUpgrade, shared::types::RuntimeUpgradeOptions};

#[derive(Debug, Serialize)]
pub struct Relaychain {
Expand All @@ -16,8 +17,31 @@ pub struct Relaychain {

#[async_trait]
impl ChainUpgrade for Relaychain {
fn nodes(&self) -> Vec<&NetworkNode> {
self.nodes.iter().collect()
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
.nodes()
.into_iter()
.find(|node| node.name() == node_name)
{
node
} else {
return Err(anyhow!(
"Node: {} is not part of the set of nodes",
node_name
));
}
} else {
// take the first node
if let Some(node) = self.nodes().first() {
node
} else {
return Err(anyhow!("chain doesn't have any node!"));
}
};

self.perform_runtime_upgrade(node, options).await
}
}

Expand All @@ -32,6 +56,9 @@ impl Relaychain {
}

// Public API
pub fn nodes(&self) -> Vec<&NetworkNode> {
self.nodes.iter().collect()
}

/// Get chain name
pub fn chain(&self) -> &str {
Expand Down
Loading