From 29b871841a90b5ddf06b56b99cf489dab1926ceb Mon Sep 17 00:00:00 2001 From: Joseph Goulden Date: Fri, 15 Nov 2019 07:21:42 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20for=20TUI=20status=20try=20to=20acquire?= =?UTF-8?q?=20read=20lock=20on=20pmmr=5Fheader=20but=20if=20no=E2=80=A6=20?= =?UTF-8?q?(#3119)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: for TUI status try to acquire read lock on pmmr_header but if not available just leave the header stats alone --- chain/src/chain.rs | 12 ++++++++++++ servers/src/common/stats.rs | 2 +- servers/src/grin/server.rs | 18 +++++++++++------- src/bin/tui/status.rs | 26 ++++++++++++++------------ 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 5d09d564d..d8c7713d8 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -1216,6 +1216,18 @@ impl Chain { .map_err(|e| ErrorKind::StoreErr(e, "chain tail".to_owned()).into()) } + /// Tip (head) of the header chain if read lock can be acquired right now. + pub fn try_header_head(&self) -> Result, Error> { + match self.header_pmmr.try_read() { + Some(lock) => { + let hash = lock.head_hash()?; + let header = self.store.get_block_header(&hash)?; + Ok(Some(Tip::from_header(&header))) + } + None => Ok(None), + } + } + /// Tip (head) of the header chain. pub fn header_head(&self) -> Result { let hash = self.header_pmmr.read().head_hash()?; diff --git a/servers/src/common/stats.rs b/servers/src/common/stats.rs index 8ada81749..ba44e04fb 100644 --- a/servers/src/common/stats.rs +++ b/servers/src/common/stats.rs @@ -53,7 +53,7 @@ pub struct ServerStats { /// Chain head pub chain_stats: ChainStats, /// sync header head - pub header_stats: ChainStats, + pub header_stats: Option, /// Whether we're currently syncing pub sync_status: SyncStatus, /// Handle to current stratum server stats diff --git a/servers/src/grin/server.rs b/servers/src/grin/server.rs index 539ca270b..c623a76fd 100644 --- a/servers/src/grin/server.rs +++ b/servers/src/grin/server.rs @@ -506,13 +506,17 @@ impl Server { total_difficulty: head.total_difficulty(), }; - let header_tip = self.chain.header_head()?; - let header = self.chain.get_block_header(&header_tip.hash())?; - let header_stats = ChainStats { - latest_timestamp: header.timestamp, - height: header.height, - last_block_h: header.prev_hash, - total_difficulty: header.total_difficulty(), + let header_stats = match self.chain.try_header_head()? { + Some(tip) => { + let header = self.chain.get_block_header(&tip.hash())?; + Some(ChainStats { + latest_timestamp: header.timestamp, + height: header.height, + last_block_h: header.prev_hash, + total_difficulty: header.total_difficulty(), + }) + } + _ => None, }; let disk_usage_bytes = WalkDir::new(&self.config.db_root) diff --git a/src/bin/tui/status.rs b/src/bin/tui/status.rs index 2f15cd623..27596fe63 100644 --- a/src/bin/tui/status.rs +++ b/src/bin/tui/status.rs @@ -295,18 +295,20 @@ impl TUIStatusListener for TUIStatusView { c.call_on_id("chain_timestamp", |t: &mut TextView| { t.set_content(stats.chain_stats.latest_timestamp.to_string()); }); - c.call_on_id("basic_header_tip_hash", |t: &mut TextView| { - t.set_content(stats.header_stats.last_block_h.to_string() + "..."); - }); - c.call_on_id("basic_header_chain_height", |t: &mut TextView| { - t.set_content(stats.header_stats.height.to_string()); - }); - c.call_on_id("basic_header_total_difficulty", |t: &mut TextView| { - t.set_content(stats.header_stats.total_difficulty.to_string()); - }); - c.call_on_id("basic_header_timestamp", |t: &mut TextView| { - t.set_content(stats.header_stats.latest_timestamp.to_string()); - }); + if let Some(header_stats) = &stats.header_stats { + c.call_on_id("basic_header_tip_hash", |t: &mut TextView| { + t.set_content(header_stats.last_block_h.to_string() + "..."); + }); + c.call_on_id("basic_header_chain_height", |t: &mut TextView| { + t.set_content(header_stats.height.to_string()); + }); + c.call_on_id("basic_header_total_difficulty", |t: &mut TextView| { + t.set_content(header_stats.total_difficulty.to_string()); + }); + c.call_on_id("basic_header_timestamp", |t: &mut TextView| { + t.set_content(header_stats.latest_timestamp.to_string()); + }); + } c.call_on_id("tx_pool_size", |t: &mut TextView| { t.set_content(stats.tx_stats.tx_pool_size.to_string()); });