openzeppelin_monitor/utils/tests/builders/
network.rs1use crate::models::{BlockChainType, Network, RpcUrl, SecretString, SecretValue};
6
7pub struct NetworkBuilder {
9 name: String,
10 slug: String,
11 network_type: BlockChainType,
12 chain_id: Option<u64>,
13 network_passphrase: Option<String>,
14 store_blocks: Option<bool>,
15 rpc_urls: Vec<RpcUrl>,
16 block_time_ms: u64,
17 confirmation_blocks: u64,
18 cron_schedule: String,
19 max_past_blocks: Option<u64>,
20}
21
22impl Default for NetworkBuilder {
23 fn default() -> Self {
24 Self {
25 name: "Test Network".to_string(),
26 slug: "test_network".to_string(),
27 network_type: BlockChainType::EVM,
28 chain_id: Some(1),
29 network_passphrase: None,
30 store_blocks: Some(true),
31 rpc_urls: vec![RpcUrl {
32 type_: "rpc".to_string(),
33 url: SecretValue::Plain(SecretString::new("https://test.network".to_string())),
34 weight: 100,
35 }],
36 block_time_ms: 1000,
37 confirmation_blocks: 1,
38 cron_schedule: "0 */5 * * * *".to_string(),
39 max_past_blocks: Some(10),
40 }
41 }
42}
43
44impl NetworkBuilder {
45 pub fn new() -> Self {
46 Self::default()
47 }
48
49 pub fn name(mut self, name: &str) -> Self {
50 self.name = name.to_string();
51 self
52 }
53
54 pub fn slug(mut self, slug: &str) -> Self {
55 self.slug = slug.to_string();
56 self
57 }
58
59 pub fn network_type(mut self, network_type: BlockChainType) -> Self {
60 self.network_type = network_type;
61 self
62 }
63
64 pub fn chain_id(mut self, chain_id: u64) -> Self {
65 self.chain_id = Some(chain_id);
66 self
67 }
68
69 pub fn network_passphrase(mut self, passphrase: &str) -> Self {
70 self.network_passphrase = Some(passphrase.to_string());
71 self
72 }
73
74 pub fn store_blocks(mut self, store: bool) -> Self {
75 self.store_blocks = Some(store);
76 self
77 }
78
79 pub fn rpc_url(mut self, url: &str) -> Self {
80 self.rpc_urls = vec![RpcUrl {
81 type_: "rpc".to_string(),
82 url: SecretValue::Plain(SecretString::new(url.to_string())),
83 weight: 100,
84 }];
85 self
86 }
87
88 pub fn rpc_urls(mut self, urls: Vec<&str>) -> Self {
89 self.rpc_urls = urls
90 .into_iter()
91 .map(|url| RpcUrl {
92 type_: "rpc".to_string(),
93 url: SecretValue::Plain(SecretString::new(url.to_string())),
94 weight: 100,
95 })
96 .collect();
97 self
98 }
99
100 pub fn add_rpc_url(mut self, url: &str, type_: &str, weight: u32) -> Self {
101 self.rpc_urls.push(RpcUrl {
102 type_: type_.to_string(),
103 url: SecretValue::Plain(SecretString::new(url.to_string())),
104 weight,
105 });
106 self
107 }
108
109 pub fn add_secret_rpc_url(mut self, url: SecretValue, type_: &str, weight: u32) -> Self {
110 self.rpc_urls.push(RpcUrl {
111 type_: type_.to_string(),
112 url,
113 weight,
114 });
115 self
116 }
117
118 pub fn clear_rpc_urls(mut self) -> Self {
119 self.rpc_urls.clear();
120 self
121 }
122
123 pub fn block_time_ms(mut self, block_time: u64) -> Self {
124 self.block_time_ms = block_time;
125 self
126 }
127
128 pub fn confirmation_blocks(mut self, blocks: u64) -> Self {
129 self.confirmation_blocks = blocks;
130 self
131 }
132
133 pub fn cron_schedule(mut self, schedule: &str) -> Self {
134 self.cron_schedule = schedule.to_string();
135 self
136 }
137
138 pub fn max_past_blocks(mut self, blocks: u64) -> Self {
139 self.max_past_blocks = Some(blocks);
140 self
141 }
142
143 pub fn build(self) -> Network {
144 Network {
145 name: self.name,
146 slug: self.slug,
147 network_type: self.network_type,
148 chain_id: self.chain_id,
149 network_passphrase: self.network_passphrase,
150 store_blocks: self.store_blocks,
151 rpc_urls: self.rpc_urls,
152 block_time_ms: self.block_time_ms,
153 confirmation_blocks: self.confirmation_blocks,
154 cron_schedule: self.cron_schedule,
155 max_past_blocks: self.max_past_blocks,
156 }
157 }
158}
159
160#[cfg(test)]
161mod tests {
162 use super::*;
163
164 #[test]
165 fn test_default_network() {
166 let network = NetworkBuilder::new().build();
167
168 assert_eq!(network.name, "Test Network");
169 assert_eq!(network.slug, "test_network");
170 assert_eq!(network.network_type, BlockChainType::EVM);
171 assert_eq!(network.chain_id, Some(1));
172 assert_eq!(network.network_passphrase, None);
173 assert_eq!(network.store_blocks, Some(true));
174 assert_eq!(network.block_time_ms, 1000);
175 assert_eq!(network.confirmation_blocks, 1);
176 assert_eq!(network.cron_schedule, "0 */5 * * * *");
177 assert_eq!(network.max_past_blocks, Some(10));
178
179 assert_eq!(network.rpc_urls.len(), 1);
181 assert_eq!(
182 network.rpc_urls[0].url.as_ref().to_string(),
183 "https://test.network".to_string()
184 );
185 assert_eq!(network.rpc_urls[0].type_, "rpc");
186 assert_eq!(network.rpc_urls[0].weight, 100);
187 }
188
189 #[test]
190 fn test_basic_builder_methods() {
191 let network = NetworkBuilder::new()
192 .name("Ethereum")
193 .slug("eth")
194 .network_type(BlockChainType::EVM)
195 .chain_id(1)
196 .store_blocks(true)
197 .block_time_ms(15000)
198 .confirmation_blocks(12)
199 .build();
200
201 assert_eq!(network.name, "Ethereum");
202 assert_eq!(network.slug, "eth");
203 assert_eq!(network.network_type, BlockChainType::EVM);
204 assert_eq!(network.chain_id, Some(1));
205 assert_eq!(network.store_blocks, Some(true));
206 assert_eq!(network.block_time_ms, 15000);
207 assert_eq!(network.confirmation_blocks, 12);
208 }
209
210 #[test]
211 fn test_rpc_url_methods() {
212 let network = NetworkBuilder::new()
213 .clear_rpc_urls()
214 .add_rpc_url("https://rpc1.example.com", "http", 50)
215 .add_rpc_url("https://rpc2.example.com", "ws", 50)
216 .build();
217
218 assert_eq!(network.rpc_urls.len(), 2);
219 assert_eq!(
220 network.rpc_urls[0].url.as_ref().to_string(),
221 "https://rpc1.example.com".to_string()
222 );
223 assert_eq!(network.rpc_urls[0].type_, "http");
224 assert_eq!(network.rpc_urls[0].weight, 50);
225 assert_eq!(
226 network.rpc_urls[1].url.as_ref().to_string(),
227 "https://rpc2.example.com".to_string()
228 );
229 assert_eq!(network.rpc_urls[1].type_, "ws");
230 assert_eq!(network.rpc_urls[1].weight, 50);
231 }
232
233 #[test]
234 fn test_secret_rpc_url() {
235 let network = NetworkBuilder::new()
236 .add_secret_rpc_url(
237 SecretValue::Plain(SecretString::new("https://rpc1.example.com".to_string())),
238 "rpc",
239 50,
240 )
241 .build();
242
243 assert_eq!(network.rpc_urls.len(), 2);
244 assert_eq!(
245 network.rpc_urls[1].url.as_ref().to_string(),
246 "https://rpc1.example.com".to_string()
247 );
248 assert_eq!(network.rpc_urls[1].type_, "rpc");
249 }
250
251 #[test]
252 fn test_rpc_urls_bulk_set() {
253 let network = NetworkBuilder::new()
254 .rpc_urls(vec!["https://rpc1.com", "https://rpc2.com"])
255 .build();
256
257 assert_eq!(network.rpc_urls.len(), 2);
258 assert_eq!(
259 network.rpc_urls[0].url.as_ref().to_string(),
260 "https://rpc1.com".to_string()
261 );
262 assert_eq!(
263 network.rpc_urls[1].url.as_ref().to_string(),
264 "https://rpc2.com".to_string()
265 );
266 assert!(network.rpc_urls.iter().all(|url| url.type_ == "rpc"));
268 assert!(network.rpc_urls.iter().all(|url| url.weight == 100));
269 }
270
271 #[test]
272 fn test_stellar_network() {
273 let network = NetworkBuilder::new()
274 .name("Stellar")
275 .slug("xlm")
276 .network_type(BlockChainType::Stellar)
277 .network_passphrase("Test SDF Network")
278 .build();
279
280 assert_eq!(network.network_type, BlockChainType::Stellar);
281 assert_eq!(
282 network.network_passphrase,
283 Some("Test SDF Network".to_string())
284 );
285 assert_eq!(network.chain_id, Some(1)); }
287}