zombienet_configuration/
hrmp_channel.rs1use std::marker::PhantomData;
2
3use serde::{Deserialize, Serialize};
4
5use crate::shared::{macros::states, types::ParaId};
6
7#[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 pub fn sender(&self) -> ParaId {
19 self.sender
20 }
21
22 pub fn recipient(&self) -> ParaId {
24 self.recipient
25 }
26
27 pub fn max_capacity(&self) -> u32 {
29 self.max_capacity
30 }
31
32 pub fn max_message_size(&self) -> u32 {
34 self.max_message_size
35 }
36}
37
38states! {
39 Initial,
40 WithSender,
41 WithRecipient
42}
43
44pub 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 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 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 pub fn with_max_capacity(self, max_capacity: u32) -> Self {
100 self.transition(HrmpChannelConfig {
101 max_capacity,
102 ..self.config
103 })
104 }
105
106 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}