fix: for TUI status try to acquire read lock on pmmr_header but if no… (#3119)

fix: for TUI status try to acquire read lock on pmmr_header but if not available just leave the header stats alone
This commit is contained in:
Joseph Goulden 2019-11-15 07:21:42 +00:00 committed by hashmap
parent 8d2c43d7e8
commit 29b871841a
4 changed files with 38 additions and 20 deletions

View file

@ -1216,6 +1216,18 @@ impl Chain {
.map_err(|e| ErrorKind::StoreErr(e, "chain tail".to_owned()).into()) .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<Option<Tip>, 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. /// Tip (head) of the header chain.
pub fn header_head(&self) -> Result<Tip, Error> { pub fn header_head(&self) -> Result<Tip, Error> {
let hash = self.header_pmmr.read().head_hash()?; let hash = self.header_pmmr.read().head_hash()?;

View file

@ -53,7 +53,7 @@ pub struct ServerStats {
/// Chain head /// Chain head
pub chain_stats: ChainStats, pub chain_stats: ChainStats,
/// sync header head /// sync header head
pub header_stats: ChainStats, pub header_stats: Option<ChainStats>,
/// Whether we're currently syncing /// Whether we're currently syncing
pub sync_status: SyncStatus, pub sync_status: SyncStatus,
/// Handle to current stratum server stats /// Handle to current stratum server stats

View file

@ -506,13 +506,17 @@ impl Server {
total_difficulty: head.total_difficulty(), total_difficulty: head.total_difficulty(),
}; };
let header_tip = self.chain.header_head()?; let header_stats = match self.chain.try_header_head()? {
let header = self.chain.get_block_header(&header_tip.hash())?; Some(tip) => {
let header_stats = ChainStats { let header = self.chain.get_block_header(&tip.hash())?;
latest_timestamp: header.timestamp, Some(ChainStats {
height: header.height, latest_timestamp: header.timestamp,
last_block_h: header.prev_hash, height: header.height,
total_difficulty: header.total_difficulty(), last_block_h: header.prev_hash,
total_difficulty: header.total_difficulty(),
})
}
_ => None,
}; };
let disk_usage_bytes = WalkDir::new(&self.config.db_root) let disk_usage_bytes = WalkDir::new(&self.config.db_root)

View file

@ -295,18 +295,20 @@ impl TUIStatusListener for TUIStatusView {
c.call_on_id("chain_timestamp", |t: &mut TextView| { c.call_on_id("chain_timestamp", |t: &mut TextView| {
t.set_content(stats.chain_stats.latest_timestamp.to_string()); t.set_content(stats.chain_stats.latest_timestamp.to_string());
}); });
c.call_on_id("basic_header_tip_hash", |t: &mut TextView| { if let Some(header_stats) = &stats.header_stats {
t.set_content(stats.header_stats.last_block_h.to_string() + "..."); 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(stats.header_stats.height.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(stats.header_stats.total_difficulty.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(stats.header_stats.latest_timestamp.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| { c.call_on_id("tx_pool_size", |t: &mut TextView| {
t.set_content(stats.tx_stats.tx_pool_size.to_string()); t.set_content(stats.tx_stats.tx_pool_size.to_string());
}); });