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
mod builder;
mod high_level;
use std::{
sync::{Arc, RwLock},
time::Duration,
};
use bee_block::{output::RentStructure, protocol::ProtocolParameters};
use bee_pow::providers::{NonceProvider, NonceProviderBuilder};
#[cfg(not(target_family = "wasm"))]
use tokio::{runtime::Runtime, sync::broadcast::Sender};
#[cfg(feature = "mqtt")]
use {
crate::node_api::mqtt::{BrokerOptions, MqttEvent, TopicHandlerMap},
rumqttc::AsyncClient as MqttClient,
tokio::sync::watch::{Receiver as WatchReceiver, Sender as WatchSender},
};
pub use self::builder::{ClientBuilder, NetworkInfo, NetworkInfoDto};
use crate::{constants::DEFAULT_TIPS_INTERVAL, error::Result};
#[derive(Clone)]
pub struct Client {
#[allow(dead_code)]
#[cfg(not(target_family = "wasm"))]
pub(crate) runtime: Option<Arc<Runtime>>,
pub(crate) node_manager: crate::node_manager::NodeManager,
#[cfg(not(target_family = "wasm"))]
pub(crate) sync_kill_sender: Option<Arc<Sender<()>>>,
#[cfg(feature = "mqtt")]
pub(crate) mqtt_client: Option<MqttClient>,
#[cfg(feature = "mqtt")]
pub(crate) mqtt_topic_handlers: Arc<tokio::sync::RwLock<TopicHandlerMap>>,
#[cfg(feature = "mqtt")]
pub(crate) broker_options: BrokerOptions,
#[cfg(feature = "mqtt")]
pub(crate) mqtt_event_channel: (Arc<WatchSender<MqttEvent>>, WatchReceiver<MqttEvent>),
pub(crate) network_info: Arc<RwLock<NetworkInfo>>,
pub(crate) api_timeout: Duration,
pub(crate) remote_pow_timeout: Duration,
#[allow(dead_code)] pub(crate) pow_worker_count: Option<usize>,
}
impl std::fmt::Debug for Client {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut d = f.debug_struct("Client");
d.field("node_manager", &self.node_manager);
#[cfg(feature = "mqtt")]
d.field("broker_options", &self.broker_options);
d.field("network_info", &self.network_info).finish()
}
}
impl Drop for Client {
fn drop(&mut self) {
#[cfg(not(target_family = "wasm"))]
if let Some(sender) = self.sync_kill_sender.take() {
sender.send(()).expect("failed to stop syncing process");
}
#[cfg(not(target_family = "wasm"))]
if let Some(runtime) = self.runtime.take() {
if let Ok(runtime) = Arc::try_unwrap(runtime) {
runtime.shutdown_background();
}
}
#[cfg(feature = "mqtt")]
if let Some(mqtt_client) = self.mqtt_client.take() {
std::thread::spawn(move || {
let _ = crate::async_runtime::block_on(mqtt_client.cancel());
})
.join()
.unwrap();
}
}
}
impl Client {
pub fn builder() -> ClientBuilder {
ClientBuilder::new()
}
pub fn get_pow_provider(&self) -> impl NonceProvider {
let local_pow: bool = self.get_local_pow();
#[cfg(target_family = "wasm")]
let miner = crate::api::wasm_miner::SingleThreadedMiner::builder()
.local_pow(local_pow)
.finish();
#[cfg(not(target_family = "wasm"))]
let miner = {
let mut miner = crate::api::miner::ClientMiner::builder().with_local_pow(local_pow);
if let Some(worker_count) = self.pow_worker_count {
miner = miner.with_worker_count(worker_count)
}
miner.finish()
};
miner
}
pub fn get_network_info(&self) -> Result<NetworkInfo> {
#[cfg(target_family = "wasm")]
{
let info = futures::executor::block_on(async move { self.get_info().await })?.node_info;
let mut client_network_info = self.network_info.write().map_err(|_| crate::Error::PoisonError)?;
client_network_info.protocol_parameters = info.protocol.try_into()?;
}
Ok(self.network_info.read().map_err(|_| crate::Error::PoisonError)?.clone())
}
pub fn get_protocol_parameters(&self) -> Result<ProtocolParameters> {
Ok(self.get_network_info()?.protocol_parameters)
}
pub fn get_protocol_version(&self) -> Result<u8> {
Ok(self.get_network_info()?.protocol_parameters.protocol_version())
}
pub fn get_network_name(&self) -> Result<String> {
Ok(self.get_network_info()?.protocol_parameters.network_name().into())
}
pub fn get_network_id(&self) -> Result<u64> {
Ok(self.get_network_info()?.protocol_parameters.network_id())
}
pub fn get_bech32_hrp(&self) -> Result<String> {
Ok(self.get_network_info()?.protocol_parameters.bech32_hrp().into())
}
pub fn get_min_pow_score(&self) -> Result<u32> {
Ok(self.get_network_info()?.protocol_parameters.min_pow_score())
}
pub fn get_below_max_depth(&self) -> Result<u8> {
Ok(self.get_network_info()?.protocol_parameters.below_max_depth())
}
pub fn get_rent_structure(&self) -> Result<RentStructure> {
Ok(self.get_network_info()?.protocol_parameters.rent_structure().clone())
}
pub fn get_token_supply(&self) -> Result<u64> {
Ok(self.get_network_info()?.protocol_parameters.token_supply())
}
pub fn get_tips_interval(&self) -> u64 {
self.network_info
.read()
.map_or(DEFAULT_TIPS_INTERVAL, |info| info.tips_interval)
}
pub fn get_local_pow(&self) -> bool {
self.network_info
.read()
.map_or(NetworkInfo::default().local_pow, |info| info.local_pow)
}
pub(crate) fn get_timeout(&self) -> Duration {
self.api_timeout
}
pub(crate) fn get_remote_pow_timeout(&self) -> Duration {
self.remote_pow_timeout
}
pub fn get_fallback_to_local_pow(&self) -> bool {
self.network_info
.read()
.map_or(NetworkInfo::default().fallback_to_local_pow, |info| {
info.fallback_to_local_pow
})
}
}