zombienet_configuration/
hrmp_channel.rs

1use std::marker::PhantomData;
2
3use serde::{Deserialize, Serialize};
4
5use crate::shared::{macros::states, types::ParaId};
6
7/// HRMP channel configuration, with fine-grained configuration options.
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub struct HrmpChannelConfig {
10    sender: ParaId,
11    recipient: ParaId,
12    max_capacity: u32,
13    max_message_size: u32,
14}
15
16impl HrmpChannelConfig {
17    /// The sending parachain ID.
18    pub fn sender(&self) -> ParaId {
19        self.sender
20    }
21
22    /// The receiving parachain ID.
23    pub fn recipient(&self) -> ParaId {
24        self.recipient
25    }
26
27    /// The maximum capacity of messages in the channel.
28    pub fn max_capacity(&self) -> u32 {
29        self.max_capacity
30    }
31
32    /// The maximum size of a message in the channel.
33    pub fn max_message_size(&self) -> u32 {
34        self.max_message_size
35    }
36}
37
38states! {
39    Initial,
40    WithSender,
41    WithRecipient
42}
43
44/// HRMP channel configuration builder, used to build an [`HrmpChannelConfig`] declaratively with fields validation.
45pub struct HrmpChannelConfigBuilder<State> {
46    config: HrmpChannelConfig,
47    _state: PhantomData<State>,
48}
49
50impl Default for HrmpChannelConfigBuilder<Initial> {
51    fn default() -> Self {
52        Self {
53            config: HrmpChannelConfig {
54                sender: 0,
55                recipient: 0,
56                max_capacity: 8,
57                max_message_size: 512,
58            },
59            _state: PhantomData,
60        }
61    }
62}
63
64impl<A> HrmpChannelConfigBuilder<A> {
65    fn transition<B>(&self, config: HrmpChannelConfig) -> HrmpChannelConfigBuilder<B> {
66        HrmpChannelConfigBuilder {
67            config,
68            _state: PhantomData,
69        }
70    }
71}
72
73impl HrmpChannelConfigBuilder<Initial> {
74    pub fn new() -> Self {
75        Self::default()
76    }
77
78    /// Set the sending parachain ID.
79    pub fn with_sender(self, sender: ParaId) -> HrmpChannelConfigBuilder<WithSender> {
80        self.transition(HrmpChannelConfig {
81            sender,
82            ..self.config
83        })
84    }
85}
86
87impl HrmpChannelConfigBuilder<WithSender> {
88    /// Set the receiving parachain ID.
89    pub fn with_recipient(self, recipient: ParaId) -> HrmpChannelConfigBuilder<WithRecipient> {
90        self.transition(HrmpChannelConfig {
91            recipient,
92            ..self.config
93        })
94    }
95}
96
97impl HrmpChannelConfigBuilder<WithRecipient> {
98    /// Set the max capacity of messages in the channel.
99    pub fn with_max_capacity(self, max_capacity: u32) -> Self {
100        self.transition(HrmpChannelConfig {
101            max_capacity,
102            ..self.config
103        })
104    }
105
106    /// Set the maximum size of a message in the channel.
107    pub fn with_max_message_size(self, max_message_size: u32) -> Self {
108        self.transition(HrmpChannelConfig {
109            max_message_size,
110            ..self.config
111        })
112    }
113
114    pub fn build(self) -> HrmpChannelConfig {
115        self.config
116    }
117}
118
119#[cfg(test)]
120mod tests {
121    use super::*;
122
123    #[test]
124    fn hrmp_channel_config_builder_should_build_a_new_hrmp_channel_config_correctly() {
125        let hrmp_channel_config = HrmpChannelConfigBuilder::new()
126            .with_sender(1000)
127            .with_recipient(2000)
128            .with_max_capacity(50)
129            .with_max_message_size(100)
130            .build();
131
132        assert_eq!(hrmp_channel_config.sender(), 1000);
133        assert_eq!(hrmp_channel_config.recipient(), 2000);
134        assert_eq!(hrmp_channel_config.max_capacity(), 50);
135        assert_eq!(hrmp_channel_config.max_message_size(), 100);
136    }
137}