Skip to content

Commit

Permalink
fix #274, refactor trait and make 'nodes' public again
Browse files Browse the repository at this point in the history
  • Loading branch information
pepoviola committed Nov 26, 2024
1 parent 16fe9a7 commit 013eeaa
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 33 deletions.
33 changes: 7 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,18 @@ 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
30 changes: 27 additions & 3 deletions crates/orchestrator/src/network/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
};

use async_trait::async_trait;
use anyhow::anyhow;
use provider::types::TransferedFile;
use serde::Serialize;
use subxt::{dynamic::Value, tx::TxStatus, OnlineClient, SubstrateConfig};
Expand All @@ -13,7 +14,7 @@ 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 +30,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
34 changes: 30 additions & 4 deletions crates/orchestrator/src/network/relaychain.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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 +16,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 +55,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

0 comments on commit 013eeaa

Please sign in to comment.