1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use std::sync::atomic::{
AtomicBool,
AtomicU64,
Ordering,
};
use std::sync::Arc;
use std::time::Duration;
use arc_swap::ArcSwapOption;
pub(crate) use operator::Operator;
use rand::thread_rng;
use self::mirror_network::MirrorNetwork;
use crate::client::network::Network;
use crate::ping_query::PingQuery;
use crate::{
AccountId,
Error,
LedgerId,
PrivateKey,
TransactionId,
};
mod mirror_network;
mod network;
mod operator;
struct ClientInner {
network: Network,
mirror_network: MirrorNetwork,
operator: ArcSwapOption<Operator>,
max_transaction_fee_tinybar: AtomicU64,
ledger_id: ArcSwapOption<LedgerId>,
auto_validate_checksums: AtomicBool,
}
#[derive(Clone)]
pub struct Client(Arc<ClientInner>);
impl Client {
fn with_network(
network: Network,
mirror_network: MirrorNetwork,
ledger_id: impl Into<Option<LedgerId>>,
) -> Self {
Self(Arc::new(ClientInner {
network,
mirror_network,
operator: ArcSwapOption::new(None),
max_transaction_fee_tinybar: AtomicU64::new(0),
ledger_id: ArcSwapOption::new(ledger_id.into().map(Arc::new)),
auto_validate_checksums: AtomicBool::new(false),
}))
}
#[must_use]
pub fn for_mainnet() -> Self {
Self::with_network(Network::mainnet(), MirrorNetwork::mainnet(), LedgerId::mainnet())
}
#[must_use]
pub fn for_testnet() -> Self {
Self::with_network(Network::testnet(), MirrorNetwork::testnet(), LedgerId::testnet())
}
#[must_use]
pub fn for_previewnet() -> Self {
Self::with_network(
Network::previewnet(),
MirrorNetwork::previewnet(),
LedgerId::previewnet(),
)
}
pub fn for_name(name: &str) -> crate::Result<Self> {
match name {
"mainnet" => Ok(Self::for_mainnet()),
"testnet" => Ok(Self::for_testnet()),
"previewnet" => Ok(Self::for_previewnet()),
_ => Err(Error::basic_parse(format!("Unknown network name {name}"))),
}
}
pub(crate) fn ledger_id_internal(&self) -> arc_swap::Guard<Option<Arc<LedgerId>>> {
self.0.ledger_id.load()
}
pub fn set_ledger_id(&self, ledger_id: Option<LedgerId>) {
self.0.ledger_id.store(ledger_id.map(Arc::new));
}
pub(crate) fn random_node_ids(&self) -> Vec<AccountId> {
let node_ids: Vec<_> = self.network().healthy_node_ids().collect();
let node_sample_amount = (node_ids.len() + 2) / 3;
let node_id_indecies =
rand::seq::index::sample(&mut thread_rng(), node_ids.len(), node_sample_amount);
node_id_indecies.into_iter().map(|index| node_ids[index]).collect()
}
pub(crate) fn auto_validate_checksums(&self) -> bool {
self.0.auto_validate_checksums.load(Ordering::Relaxed)
}
pub fn set_auto_validate_checksums(&self, value: bool) {
self.0.auto_validate_checksums.store(value, Ordering::Relaxed);
}
pub fn set_operator(&self, id: AccountId, key: PrivateKey) {
self.0.operator.store(Some(Arc::new(Operator { account_id: id, signer: key })));
}
pub(crate) fn generate_transaction_id(&self) -> Option<TransactionId> {
self.0.operator.load().as_deref().map(Operator::generate_transaction_id)
}
pub(crate) fn network(&self) -> &Network {
&self.0.network
}
pub(crate) fn mirror_network(&self) -> &MirrorNetwork {
&self.0.mirror_network
}
pub(crate) fn max_transaction_fee(&self) -> &AtomicU64 {
&self.0.max_transaction_fee_tinybar
}
#[allow(clippy::unused_self)]
pub(crate) fn request_timeout(&self) -> Option<Duration> {
None
}
pub(crate) fn operator_internal(&self) -> arc_swap::Guard<Option<Arc<Operator>>> {
self.0.operator.load()
}
pub async fn ping(&self, node_account_id: AccountId) -> crate::Result<()> {
PingQuery::new(node_account_id).execute(self, None).await
}
pub async fn ping_with_timeout(
&self,
node_account_id: AccountId,
timeout: Duration,
) -> crate::Result<()> {
PingQuery::new(node_account_id).execute(self, Some(timeout)).await
}
pub async fn ping_all(&self) -> crate::Result<()> {
futures_util::future::try_join_all(
self.network().node_ids().iter().map(|it| self.ping(*it)),
)
.await?;
Ok(())
}
pub async fn ping_all_with_timeout(&self, timeout: Duration) -> crate::Result<()> {
futures_util::future::try_join_all(
self.network().node_ids().iter().map(|it| self.ping_with_timeout(*it, timeout)),
)
.await?;
Ok(())
}
}