From 0953521cc4fbcd029e2c2ee6c4ae77ed1f466b1b Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Fri, 25 Jan 2019 09:16:37 -0300 Subject: [PATCH] don't panic in best_containing (#1559) * don't panic in best_containing * core: client: fix borrow in best_containing --- substrate/core/client/src/client.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/substrate/core/client/src/client.rs b/substrate/core/client/src/client.rs index 86a83ac7ad..6921ecf312 100644 --- a/substrate/core/client/src/client.rs +++ b/substrate/core/client/src/client.rs @@ -1066,9 +1066,14 @@ impl Client where /// Get the most recent block hash of the best (longest) chains /// that contain block with the given `target_hash`. + /// + /// The search space is always limited to blocks which are in the finalized + /// chain or descendents of it. + /// /// If `maybe_max_block_number` is `Some(max_block_number)` /// the search is limited to block `numbers <= max_block_number`. /// in other words as if there were no blocks greater `max_block_number`. + /// /// TODO [snd] possibly implement this on blockchain::Backend and just redirect here /// Returns `Ok(None)` if `target_hash` is not found in search space. /// TODO [snd] write down time complexity @@ -1110,7 +1115,11 @@ impl Client where } return Ok(Some(info.best_hash)); + } else if info.finalized_number >= *target_header.number() { + // header is on a dead fork. + return Ok(None); } + (self.backend.blockchain().leaves()?, info.best_hash) }; @@ -1164,7 +1173,19 @@ impl Client where } } - unreachable!("this is a bug. `target_hash` is in blockchain but wasn't found following all leaves backwards"); + // header may be on a dead fork -- the only leaves that are considered are + // those which can still be finalized. + // + // TODO: only issue this warning when not on a dead fork + // part of https://github.com/paritytech/substrate/issues/1558 + warn!( + "Block {:?} exists in chain but not found when following all \ + leaves backwards. Number limit = {:?}", + target_hash, + maybe_max_number, + ); + + Ok(None) } fn changes_trie_config(&self) -> Result, Error> {