1use std::collections::HashMap;
2
3use crate::error::HederaError;
4use crate::ledger_id::LedgerId;
5use crate::managed_network::{ArcNetworkNode, ManagedNetwork, NetworkNode};
6use crate::network_name::NetworkName;
7use crate::node::Node;
8use crate::node_address::NodeAddress;
9use crate::node_address_book::NodeAddressBook;
10use crate::AccountId;
11
12fn mainnet_nodes() -> HashMap<String, AccountId> {
13 let mut nodes = HashMap::new();
14 nodes.insert("35.237.200.180:50212".to_string(), AccountId::simple(3));
15 nodes.insert("35.186.191.247:50212".to_string(), AccountId::simple(4));
16 nodes.insert("35.192.2.25:50212".to_string(), AccountId::simple(5));
17 nodes.insert("35.199.161.108:50212".to_string(), AccountId::simple(6));
18 nodes.insert("35.203.82.240:50212".to_string(), AccountId::simple(7));
19 nodes.insert("35.236.5.219:50212".to_string(), AccountId::simple(8));
20 nodes.insert("35.197.192.225:50212".to_string(), AccountId::simple(9));
21 nodes.insert("35.242.233.154:50212".to_string(), AccountId::simple(10));
22 nodes.insert("35.240.118.96:50212".to_string(), AccountId::simple(11));
23 nodes.insert("35.204.86.32:50212".to_string(), AccountId::simple(12));
24 nodes.insert("35.234.132.107:50212".to_string(), AccountId::simple(13));
25 nodes.insert("35.236.2.27:50212".to_string(), AccountId::simple(14));
26 nodes.insert("35.228.11.53:50212".to_string(), AccountId::simple(15));
27 nodes.insert("34.91.181.183:50212".to_string(), AccountId::simple(16));
28 nodes.insert("34.86.212.247:50212".to_string(), AccountId::simple(17));
29 nodes.insert("172.105.247.67:50212".to_string(), AccountId::simple(18));
30 nodes.insert("34.89.87.138:50212".to_string(), AccountId::simple(19));
31 nodes.insert("34.82.78.255:50212".to_string(), AccountId::simple(20));
32 nodes
33}
34
35fn testnet_nodes() -> HashMap<String, AccountId> {
36 let mut nodes = HashMap::new();
37 nodes.insert(
38 "0.testnet.hedera.com:50211".to_string(),
39 AccountId::simple(3),
40 );
41 nodes.insert(
42 "1.testnet.hedera.com:50211".to_string(),
43 AccountId::simple(4),
44 );
45 nodes.insert(
46 "2.testnet.hedera.com:50211".to_string(),
47 AccountId::simple(5),
48 );
49 nodes.insert(
50 "3.testnet.hedera.com:50211".to_string(),
51 AccountId::simple(6),
52 );
53 nodes.insert(
54 "4.testnet.hedera.com:50211".to_string(),
55 AccountId::simple(7),
56 );
57 nodes
58}
59
60fn previewnet_nodes() -> HashMap<String, AccountId> {
61 let mut nodes = HashMap::new();
62 nodes.insert(
63 "0.previewnet.hedera.com:50211".to_string(),
64 AccountId::simple(3),
65 );
66 nodes.insert(
67 "1.previewnet.hedera.com:50211".to_string(),
68 AccountId::simple(4),
69 );
70 nodes.insert(
71 "2.previewnet.hedera.com:50211".to_string(),
72 AccountId::simple(5),
73 );
74 nodes.insert(
75 "3.previewnet.hedera.com:50211".to_string(),
76 AccountId::simple(6),
77 );
78 nodes.insert(
79 "4.previewnet.hedera.com:50211".to_string(),
80 AccountId::simple(7),
81 );
82 nodes
83}
84
85#[derive(Debug, Clone)]
86pub struct Network {
87 network: ManagedNetwork,
88 address_book: Option<HashMap<AccountId, NodeAddress>>,
89}
90
91impl Network {
92 pub fn new() -> Network {
93 Network {
94 network: ManagedNetwork::new_empty(),
95 address_book: None,
96 }
97 }
98
99 pub fn ledger_id(&self) -> LedgerId {
100 self.network.ledger_id.clone()
101 }
102
103 pub async fn set_ledger_id(&mut self, id: LedgerId) -> Result<(), HederaError> {
104 self.network.ledger_id = id.clone();
105 if self.network.transport_security {
106 let address_book = Self::read_address_book_resource(&format!(
107 "addressbook/{}.services",
108 id.to_string()
109 ))
110 .await?;
111 self.network.set_nodes_address_book(&address_book).await?;
112 self.address_book = Some(address_book);
113 }
114 Ok(())
115 }
116
117 pub async fn set_network_name(&mut self, net: NetworkName) -> Result<(), HederaError> {
118 let id = LedgerId::from_network_name(net);
119 self.set_ledger_id(id).await
120 }
121
122 pub async fn read_address_book_resource(
123 file_name: &str,
124 ) -> Result<HashMap<AccountId, NodeAddress>, HederaError> {
125 let f = tokio::fs::read(file_name)
126 .await
127 .map_err(|_| HederaError::NodeAddressBookDeserialize)?;
128 let node_address_book = NodeAddressBook::from_proto_bytes(f)?;
129 let mut map: HashMap<AccountId, NodeAddress> = HashMap::new();
130 for node_address in node_address_book.node_addresses.into_iter() {
131 if let Some(ref account_id) = node_address.account_id {
132 map.insert(account_id.clone(), node_address.clone());
133 }
134 }
135 Ok(map)
136 }
137
138 pub fn for_network_name(network_name: &NetworkName) -> Result<Self, HederaError> {
139 let network = match network_name {
140 NetworkName::MainNet => mainnet_nodes(),
141 NetworkName::TestNet => testnet_nodes(),
142 NetworkName::PreviewNet => previewnet_nodes(),
143 NetworkName::Other => HashMap::new(),
144 };
145 Self::from_network(network)
146 }
147
148 pub fn from_network(network: HashMap<String, AccountId>) -> Result<Self, HederaError> {
149 let mut cli = Self::new();
150 let mut nodes = Vec::with_capacity(network.len());
151 for (address, account_id) in network.into_iter() {
152 nodes.push(NetworkNode::Node(Node::new(account_id, address, 0)));
153 }
154 let network = ManagedNetwork::from_nodes(nodes)?;
155 cli.network = network;
156 Ok(cli)
157 }
158
159 pub async fn set_network(
160 &self,
161 network: HashMap<String, AccountId>,
162 ) -> Result<(), HederaError> {
163 let mut nodes = Vec::with_capacity(network.len());
164 for (address, account_id) in network.into_iter() {
165 nodes.push(NetworkNode::Node(Node::new(account_id, address, 0)));
166 }
167 self.network.set_network_nodes(nodes).await
168 }
169
170 pub async fn network(&self) -> Result<HashMap<String, AccountId>, HederaError> {
171 self.network.network().await
172 }
173
174 pub async fn set_transport_security(&mut self, security: bool) {
175 self.network.set_transport_security(security).await;
176 }
177
178 pub fn set_max_node_attempts(&mut self, max_node_attempts: u64) {
179 self.network.max_node_attempts = max_node_attempts;
180 }
181
182 pub fn max_node_attempts(&self) -> u64 {
183 self.network.max_node_attempts
184 }
185
186 pub fn set_node_min_backoff(&mut self, node_min_backoff: u64) {
187 self.network.min_backoff = node_min_backoff;
188 }
189
190 pub fn node_min_backoff(&self) -> u64 {
191 self.network.min_backoff
192 }
193
194 pub fn set_node_max_backoff(&mut self, node_max_backoff: u64) {
195 self.network.max_backoff = node_max_backoff;
196 }
197
198 pub fn node_max_backoff(&self) -> u64 {
199 self.network.max_backoff
200 }
201
202 pub fn set_max_nodes_per_transaction(&mut self, max_nodes_per_transaction: usize) {
203 self.network.max_nodes_per_transaction = Some(max_nodes_per_transaction);
204 }
205
206 pub fn max_nodes_per_transaction(&self) -> Option<usize> {
207 self.network.max_nodes_per_transaction
208 }
209
210 pub fn set_verify_certificate(&mut self, verify: bool) {
211 self.network.verify_certificate = verify;
212 }
213
214 pub fn verify_certificate(&self) -> bool {
215 self.network.verify_certificate
216 }
217
218 pub async fn node_account_ids_for_execute(&self) -> Vec<AccountId> {
219 let nodes = self
220 .network
221 .number_of_most_heathy_nodes(self.network.number_of_nodes_for_transaction().await)
222 .await;
223 let mut account_ids = Vec::with_capacity(nodes.len());
224 for node in nodes {
225 let n_r = node.read().await;
226 if let NetworkNode::Node(n) = &*n_r {
227 account_ids.push(n.account_id.clone());
228 }
229 }
230 account_ids
231 }
232
233 pub async fn node_for_account_id(
234 &self,
235 node_account_id: &AccountId,
236 ) -> Result<ArcNetworkNode, HederaError> {
237 self.network.node_for_execute(node_account_id).await
238 }
239}
240
241