zombienet_orchestrator/network/
relaychain.rs

1use std::path::PathBuf;
2
3use anyhow::anyhow;
4use async_trait::async_trait;
5use serde::Serialize;
6
7use super::node::NetworkNode;
8use crate::{network::chain_upgrade::ChainUpgrade, shared::types::RuntimeUpgradeOptions};
9
10#[derive(Debug, Serialize)]
11pub struct Relaychain {
12    pub(crate) chain: String,
13    pub(crate) chain_id: String,
14    pub(crate) chain_spec_path: PathBuf,
15    pub(crate) nodes: Vec<NetworkNode>,
16}
17
18#[async_trait]
19impl ChainUpgrade for Relaychain {
20    async fn runtime_upgrade(&self, options: RuntimeUpgradeOptions) -> Result<(), anyhow::Error> {
21        // check if the node is valid first
22        let node = if let Some(node_name) = &options.node_name {
23            if let Some(node) = self
24                .nodes()
25                .into_iter()
26                .find(|node| node.name() == node_name)
27            {
28                node
29            } else {
30                return Err(anyhow!("Node: {node_name} is not part of the set of nodes"));
31            }
32        } else {
33            // take the first node
34            if let Some(node) = self.nodes().first() {
35                node
36            } else {
37                return Err(anyhow!("chain doesn't have any node!"));
38            }
39        };
40
41        self.perform_runtime_upgrade(node, options).await
42    }
43}
44
45impl Relaychain {
46    pub(crate) fn new(chain: String, chain_id: String, chain_spec_path: PathBuf) -> Self {
47        Self {
48            chain,
49            chain_id,
50            chain_spec_path,
51            nodes: Default::default(),
52        }
53    }
54
55    // Public API
56    pub fn nodes(&self) -> Vec<&NetworkNode> {
57        self.nodes.iter().collect()
58    }
59
60    /// Get chain name
61    pub fn chain(&self) -> &str {
62        &self.chain
63    }
64}