Skip to content

Commit

Permalink
Merge pull request iotaledger#398 from iotaledger/feat/unsynced-nodes…
Browse files Browse the repository at this point in the history
…-getter

feat: add method to get unsynced nodes
  • Loading branch information
lucasfernog authored Mar 9, 2021
2 parents bdef524 + e899485 commit 15dbf1f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion iota-client/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ impl ClientBuilder {
let sync_ = sync.clone();
let network_info_ = network_info.clone();
let (sync_kill_sender, sync_kill_receiver) = channel(1);
let nodes = nodes.clone();
let runtime = std::thread::spawn(move || {
let runtime = Runtime::new().unwrap();
runtime.block_on(Client::sync_nodes(&sync_, &nodes, &network_info_));
Expand All @@ -222,7 +223,7 @@ impl ClientBuilder {
.expect("failed to init node syncing process");
(Some(runtime), sync, Some(sync_kill_sender), network_info)
} else {
(None, Arc::new(RwLock::new(nodes)), None, network_info)
(None, Arc::new(RwLock::new(nodes.clone())), None, network_info)
};

let mut api_timeout = HashMap::new();
Expand Down Expand Up @@ -271,6 +272,7 @@ impl ClientBuilder {

let client = Client {
runtime,
nodes,
sync,
sync_kill_sender: sync_kill_sender.map(Arc::new),
#[cfg(feature = "mqtt")]
Expand Down
8 changes: 8 additions & 0 deletions iota-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ impl HttpClient {
pub struct Client {
#[allow(dead_code)]
pub(crate) runtime: Option<Runtime>,
/// Node pool.
pub(crate) nodes: HashSet<Url>,
/// Node pool of synced IOTA nodes
pub(crate) sync: Arc<RwLock<HashSet<Url>>>,
/// Flag to stop the node syncing
Expand Down Expand Up @@ -548,6 +550,12 @@ impl Client {
self.network_info.read().await.local_pow
}

/// returns the unsynced nodes.
pub async fn unsynced_nodes(&self) -> HashSet<&Url> {
let synced = self.sync.read().await;
self.nodes.iter().filter(|node| !synced.contains(node)).collect()
}

///////////////////////////////////////////////////////////////////////
// MQTT API
//////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 15dbf1f

Please sign in to comment.