further tweaks to block_accepted logs for clarity (#3379)

* further tweaks to block_accepted logs for clarity

* fix tests

* depth based off prev_head
This commit is contained in:
Antioch Peverell 2020-08-20 17:28:35 +01:00 committed by GitHub
parent 1cff387f61
commit caa6b8c747
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 21 deletions

View file

@ -306,21 +306,29 @@ impl Chain {
}) })
} }
fn determine_status(&self, head: Option<Tip>, prev_head: Tip, fork_point: Tip) -> BlockStatus { fn determine_status(
&self,
head: Option<Tip>,
prev: Tip,
prev_head: Tip,
fork_point: Tip,
) -> BlockStatus {
// If head is updated then we are either "next" block or we just experienced a "reorg" to new head. // If head is updated then we are either "next" block or we just experienced a "reorg" to new head.
// Otherwise this is a "fork" off the main chain. // Otherwise this is a "fork" off the main chain.
if let Some(head) = head { if let Some(head) = head {
if head.prev_block_h == prev_head.last_block_h { if head.prev_block_h == prev_head.last_block_h {
BlockStatus::Next { prev_head } BlockStatus::Next { prev }
} else { } else {
BlockStatus::Reorg { BlockStatus::Reorg {
prev,
prev_head, prev_head,
fork_point, fork_point,
} }
} }
} else { } else {
BlockStatus::Fork { BlockStatus::Fork {
prev_head, prev,
head: prev_head,
fork_point, fork_point,
} }
} }
@ -416,7 +424,13 @@ impl Chain {
match maybe_new_head { match maybe_new_head {
Ok((head, fork_point)) => { Ok((head, fork_point)) => {
let status = self.determine_status(head, prev_head, Tip::from_header(&fork_point)); let prev = self.get_previous_header(&b.header)?;
let status = self.determine_status(
head,
Tip::from_header(&prev),
prev_head,
Tip::from_header(&fork_point),
);
// notifying other parts of the system of the update // notifying other parts of the system of the update
self.adapter.block_accepted(&b, status, opts); self.adapter.block_accepted(&b, status, opts);

View file

@ -448,19 +448,23 @@ impl ChainAdapter for NoopAdapter {
pub enum BlockStatus { pub enum BlockStatus {
/// Block is the "next" block, updating the chain head. /// Block is the "next" block, updating the chain head.
Next { Next {
/// Previous chain head. /// Previous block (previous chain head).
prev_head: Tip, prev: Tip,
}, },
/// Block does not update the chain head and is a fork. /// Block does not update the chain head and is a fork.
Fork { Fork {
/// Previous chain head. /// Previous block on this fork.
prev_head: Tip, prev: Tip,
/// Current chain head.
head: Tip,
/// Fork point for rewind. /// Fork point for rewind.
fork_point: Tip, fork_point: Tip,
}, },
/// Block updates the chain head via a (potentially disruptive) "reorg". /// Block updates the chain head via a (potentially disruptive) "reorg".
/// Previous block was not our previous chain head. /// Previous block was not our previous chain head.
Reorg { Reorg {
/// Previous block on this fork.
prev: Tip,
/// Previous chain head. /// Previous chain head.
prev_head: Tip, prev_head: Tip,
/// Fork point for rewind. /// Fork point for rewind.

View file

@ -385,6 +385,7 @@ fn mine_reorg() {
assert_eq!( assert_eq!(
*adapter.last_status.read(), *adapter.last_status.read(),
Some(BlockStatus::Reorg { Some(BlockStatus::Reorg {
prev: Tip::from_header(&fork_head),
prev_head: head, prev_head: head,
fork_point: Tip::from_header(&fork_point) fork_point: Tip::from_header(&fork_point)
}) })

View file

@ -119,42 +119,48 @@ impl ChainEvents for EventLogger {
fn on_block_accepted(&self, block: &core::Block, status: BlockStatus) { fn on_block_accepted(&self, block: &core::Block, status: BlockStatus) {
match status { match status {
BlockStatus::Reorg { BlockStatus::Reorg {
prev,
prev_head, prev_head,
fork_point, fork_point,
} => { } => {
warn!( warn!(
"block_accepted (REORG!): {} at {}, (prev_head: {} at {}, fork_point: {} at {}, depth: {})", "block_accepted (REORG!): {} at {}, (prev: {} at {}, prev_head: {} at {}, fork_point: {} at {}, depth: {})",
block.hash(), block.hash(),
block.header.height, block.header.height,
prev.hash(),
prev.height,
prev_head.hash(), prev_head.hash(),
prev_head.height, prev_head.height,
fork_point.hash(), fork_point.hash(),
fork_point.height, fork_point.height,
block.header.height.saturating_sub(fork_point.height + 1), prev_head.height.saturating_sub(fork_point.height),
); );
} }
BlockStatus::Fork { BlockStatus::Fork {
prev_head, prev,
head,
fork_point, fork_point,
} => { } => {
debug!( debug!(
"block_accepted (fork?): {} at {}, (prev_head: {} at {}, fork_point: {} at {}, depth: {})", "block_accepted (fork?): {} at {}, (prev: {} at {}, head: {} at {}, fork_point: {} at {}, depth: {})",
block.hash(), block.hash(),
block.header.height, block.header.height,
prev_head.hash(), prev.hash(),
prev_head.height, prev.height,
head.hash(),
head.height,
fork_point.hash(), fork_point.hash(),
fork_point.height, fork_point.height,
block.header.height.saturating_sub(fork_point.height + 1), head.height.saturating_sub(fork_point.height),
); );
} }
BlockStatus::Next { prev_head } => { BlockStatus::Next { prev } => {
debug!( debug!(
"block_accepted (head+): {} at {} (prev_head: {} at {})", "block_accepted (head+): {} at {} (prev: {} at {})",
block.hash(), block.hash(),
block.header.height, block.header.height,
prev_head.hash(), prev.hash(),
prev_head.height, prev.height,
); );
} }
} }
@ -284,8 +290,13 @@ impl ChainEvents for WebHook {
}; };
// Add additional `depth` field to the JSON in case of reorg // Add additional `depth` field to the JSON in case of reorg
let payload = if let BlockStatus::Reorg { fork_point, .. } = status { let payload = if let BlockStatus::Reorg {
let depth = block.header.height.saturating_sub(fork_point.height + 1); fork_point,
prev_head,
..
} = status
{
let depth = prev_head.height.saturating_sub(fork_point.height);
json!({ json!({
"hash": block.header.hash().to_hex(), "hash": block.header.hash().to_hex(),
"status": status_str, "status": status_str,