From 16db02b0db9244676963a4242e16db36d05c1a58 Mon Sep 17 00:00:00 2001 From: Sebastian Kunert Date: Wed, 3 May 2023 13:41:51 +0200 Subject: [PATCH] Only calculate tree route during finalization when there are multiple leaves (#14067) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Only calculate tree route when there are multiple leaves * Update client/service/src/client/client.rs Co-authored-by: Bastian Köcher --------- Co-authored-by: Bastian Köcher --- client/service/src/client/client.rs | 31 +++++++++++++++++------------ 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index eee7e6b82363c..91c59cdbac844 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -936,19 +936,24 @@ where return Err(sp_blockchain::Error::NotInFinalizedChain) } - let route_from_best = - sp_blockchain::tree_route(self.backend.blockchain(), best_block, block)?; - - // if the block is not a direct ancestor of the current best chain, - // then some other block is the common ancestor. - if route_from_best.common_block().hash != block { - // NOTE: we're setting the finalized block as best block, this might - // be slightly inaccurate since we might have a "better" block - // further along this chain, but since best chain selection logic is - // plugable we cannot make a better choice here. usages that need - // an accurate "best" block need to go through `SelectChain` - // instead. - operation.op.mark_head(block)?; + // If there is only one leaf, best block is guaranteed to be + // a descendant of the new finalized block. If not, + // we need to check. + if self.backend.blockchain().leaves()?.len() > 1 { + let route_from_best = + sp_blockchain::tree_route(self.backend.blockchain(), best_block, block)?; + + // if the block is not a direct ancestor of the current best chain, + // then some other block is the common ancestor. + if route_from_best.common_block().hash != block { + // NOTE: we're setting the finalized block as best block, this might + // be slightly inaccurate since we might have a "better" block + // further along this chain, but since best chain selection logic is + // plugable we cannot make a better choice here. usages that need + // an accurate "best" block need to go through `SelectChain` + // instead. + operation.op.mark_head(block)?; + } } let enacted = route_from_finalized.enacted();