Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use crate::bitcoin_config::ConfigEntry as BitcoinEntry;
use crate::components::bitcoin_client::{BitcoinChainInfo, BitcoinClient};
use crate::components::bitcoin_config_view::BitcoinConfigView;
use crate::components::file_explorer::FileExplorer;
use crate::components::p2pool_client::{ChainInfo, P2PoolClient, PeerInfo, SharesResponse};
Expand Down Expand Up @@ -105,6 +106,8 @@ pub struct App {
pub p2pool_config: Option<P2PoolConfig>,
pub bitcoin_data: Vec<BitcoinEntry>,
pub bitcoin_status_tab: usize,
pub bitcoin_chain_info: Option<BitcoinChainInfo>,
pub bitcoin_chain_info_error: Option<String>,
pub settings: Settings,
pub p2pool_client: P2PoolClient,
pub p2pool_websocket_client: P2PoolWebSocketClient,
Expand All @@ -125,6 +128,8 @@ pub struct App {
pub live_peer_events: Vec<LivePeerEvent>,
pub p2pool_live_error: Option<String>,
pub p2pool_live_stream_started: bool,
pub bitcoin_chain_info_tx: mpsc::UnboundedSender<anyhow::Result<BitcoinChainInfo>>,
pub bitcoin_chain_info_rx: mpsc::UnboundedReceiver<anyhow::Result<BitcoinChainInfo>>,
pub p2pool_live_tx: mpsc::UnboundedSender<anyhow::Result<LiveP2PoolEvent>>,
pub p2pool_live_rx: mpsc::UnboundedReceiver<anyhow::Result<LiveP2PoolEvent>>,
// async channel to receive chain info updates from the background task that
Expand All @@ -141,6 +146,7 @@ impl App {
#[must_use]
pub fn new() -> App {
let (chain_info_tx, chain_info_rx) = mpsc::unbounded_channel();
let (bitcoin_chain_info_tx, bitcoin_chain_info_rx) = mpsc::unbounded_channel();
let (peer_info_tx, peer_info_rx) = mpsc::unbounded_channel();
let (share_info_tx, share_info_rx) = mpsc::unbounded_channel();
let (p2pool_live_tx, p2pool_live_rx) = mpsc::unbounded_channel();
Expand All @@ -157,6 +163,8 @@ impl App {
p2pool_config: None,
bitcoin_data: Vec::new(),
bitcoin_status_tab: 0,
bitcoin_chain_info: None,
bitcoin_chain_info_error: None,
settings: Settings::default(),
p2pool_client: P2PoolClient::new(),
p2pool_websocket_client: P2PoolWebSocketClient::new(),
Expand All @@ -173,6 +181,8 @@ impl App {
live_peer_events: Vec::new(),
p2pool_live_error: None,
p2pool_live_stream_started: false,
bitcoin_chain_info_tx,
bitcoin_chain_info_rx,
p2pool_live_tx,
p2pool_live_rx,
chain_info_tx,
Expand Down Expand Up @@ -208,6 +218,21 @@ impl App {
}
}

pub fn poll_bitcoin_chain_info(&mut self) {
while let Ok(result) = self.bitcoin_chain_info_rx.try_recv() {
match result {
Ok(info) => {
self.bitcoin_chain_info = Some(info);
self.bitcoin_chain_info_error = None;
}
Err(e) => {
self.bitcoin_chain_info = None;
self.bitcoin_chain_info_error = Some(e.to_string());
}
}
}
}

pub fn poll_peer_info(&mut self) {
while let Ok(result) = self.peer_info_rx.try_recv() {
match result {
Expand Down Expand Up @@ -305,6 +330,9 @@ impl App {
}
if let Some(&(_, screen)) = SIDEBAR_ITEMS.get(self.sidebar_index) {
self.current_screen = screen;
if self.current_screen == CurrentScreen::BitcoinStatus {
self.fetch_bitcoin_chain_info();
}
Comment thread
pool2win marked this conversation as resolved.
if self.current_screen == CurrentScreen::P2PoolStatus {
let chain_client = self.p2pool_client.clone();
let chain_tx = self.chain_info_tx.clone();
Expand Down Expand Up @@ -347,9 +375,125 @@ impl App {
}
}
}

fn fetch_bitcoin_chain_info(&mut self) {
self.bitcoin_chain_info = None;
self.bitcoin_chain_info_error = None;

if self.bitcoin_conf_path.is_none() {
return;
}

let client = BitcoinClient::from_config_entries(&self.bitcoin_data);
let tx = self.bitcoin_chain_info_tx.clone();

if let Ok(handle) = tokio::runtime::Handle::try_current() {
handle.spawn(async move {
let res = client.fetch_chain_info().await;
let _ = tx.send(res);
});
}
}
}
impl Default for App {
fn default() -> Self {
Self::new()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn poll_bitcoin_chain_info_updates_state_on_success() {
let mut app = App::new();
app.bitcoin_chain_info_error = Some("stale".to_string());
app.bitcoin_chain_info_tx
.send(Ok(BitcoinChainInfo {
network: "mainnet".to_string(),
block_height: 1,
best_block_hash: "abc".to_string(),
verification_progress: None,
initial_block_download: None,
connection_count: None,
}))
.unwrap();

app.poll_bitcoin_chain_info();

let info = app.bitcoin_chain_info.as_ref().unwrap();

assert_eq!(info.block_height, 1);
assert_eq!(info.best_block_hash, "abc");
assert!(app.bitcoin_chain_info_error.is_none());
}

#[test]
fn poll_bitcoin_chain_info_updates_state_on_error() {
let mut app = App::new();
app.bitcoin_chain_info = Some(BitcoinChainInfo {
network: "mainnet".to_string(),
block_height: 1,
best_block_hash: "abc".to_string(),
verification_progress: None,
initial_block_download: None,
connection_count: None,
});
app.bitcoin_chain_info_tx
.send(Err(anyhow::anyhow!("boom")))
.unwrap();

app.poll_bitcoin_chain_info();

assert!(app.bitcoin_chain_info.is_none());
assert_eq!(app.bitcoin_chain_info_error.as_deref(), Some("boom"));
}

#[test]
fn poll_bitcoin_chain_info_processes_all_queued_results() {
let mut app = App::new();
app.bitcoin_chain_info_tx
.send(Ok(BitcoinChainInfo {
network: "mainnet".to_string(),
block_height: 1,
best_block_hash: "abc".to_string(),
verification_progress: None,
initial_block_download: None,
connection_count: None,
}))
.unwrap();
app.bitcoin_chain_info_tx
.send(Err(anyhow::anyhow!("second failure")))
.unwrap();

app.poll_bitcoin_chain_info();

assert!(app.bitcoin_chain_info.is_none());
assert_eq!(
app.bitcoin_chain_info_error.as_deref(),
Some("second failure")
);
}

#[test]
fn fetch_bitcoin_chain_info_clears_state_without_configured_bitcoin_conf() {
let mut app = App::new();
app.bitcoin_conf_path = None;
app.bitcoin_chain_info = Some(BitcoinChainInfo {
network: "mainnet".to_string(),
block_height: 1,
best_block_hash: "abc".to_string(),
verification_progress: None,
initial_block_download: None,
connection_count: None,
});
app.bitcoin_chain_info_error = Some("stale".to_string());

app.fetch_bitcoin_chain_info();

assert!(app.bitcoin_chain_info.is_none());
assert!(app.bitcoin_chain_info_error.is_none());
assert!(app.bitcoin_chain_info_rx.try_recv().is_err());
}
}
Loading
Loading