Skip to main content

zombienet_orchestrator/network/
relaychain.rs

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