Repository: paritytech/substrate
File: client/service/src/client/client.rs (Rust)

Code before:
		}

		// Find tree route from last finalized to given block.
		let route_from_finalized =
			sp_blockchain::tree_route(self.backend.blockchain(), info.finalized_hash, hash)?;

		if let Some(retracted) = route_from_finalized.retracted().get(0) {
			warn!(
				"Safety violation: attempted to revert finalized block {:?} which is not in the \
				same chain as last finalized {:?}",
				retracted, info.finalized_hash
			);

			return Err(sp_blockchain::Error::NotInFinalizedChain)
		}

		// We may need to cohercively update the best block if there is more than one
		// leaf or if the finalized block number is greater than last best number recorded
		// by the backend. This last condition may apply in case of consensus implementations
		// not always checking this condition.
		let block_number = self
			.backend
			.blockchain()
			.number(hash)?
			.ok_or(Error::MissingHeader(format!("{hash:?}")))?;
		if self.backend.blockchain().leaves()?.len() > 1 || info.best_number < block_number {
			let route_from_best =
				sp_blockchain::tree_route(self.backend.blockchain(), info.best_hash, hash)?;

			// 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 != hash {
				// 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(hash)?;
			}
		}

		let enacted = route_from_finalized.enacted();
		assert!(enacted.len() > 0);
		for finalize_new in &enacted[..enacted.len() - 1] {
			operation.op.mark_finalized(finalize_new.hash, None)?;
		}

		assert_eq!(enacted.last().map(|e| e.hash), Some(hash));
		operation.op.mark_finalized(hash, justification)?;


Review comment (performance):
Hmm this new check only triggers when there is only one leaf. In that case, we could directly mark_head since there is no other ancestry possible, no need to check the tree route? Even though impact is probably super low since these headers are probably cached.

Code after:
		}

		// Find tree route from last finalized to given block.
		let route_from_finalized =
			sp_blockchain::tree_route(self.backend.blockchain(), info.finalized_hash, hash)?;

		if let Some(retracted) = route_from_finalized.retracted().get(0) {
			warn!(
				"Safety violation: attempted to revert finalized block {:?} which is not in the \
				same chain as last finalized {:?}",
				retracted, info.finalized_hash
			);

			return Err(sp_blockchain::Error::NotInFinalizedChain)
		}

		// We may need to coercively update the best block if there is more than one
		// leaf or if the finalized block number is greater than last best number recorded
		// by the backend. This last condition may apply in case of consensus implementations
		// not always checking this condition.
		let block_number = self
			.backend
			.blockchain()
			.number(hash)?
			.ok_or(Error::MissingHeader(format!("{hash:?}")))?;
		if self.backend.blockchain().leaves()?.len() > 1 || info.best_number < block_number {
			let route_from_best =
				sp_blockchain::tree_route(self.backend.blockchain(), info.best_hash, hash)?;

			// 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 != hash {
				// 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(hash)?;
			}
		}

		let enacted = route_from_finalized.enacted();
		assert!(enacted.len() > 0);
		for finalize_new in &enacted[..enacted.len() - 1] {
			operation.op.mark_finalized(finalize_new.hash, None)?;
		}

		assert_eq!(enacted.last().map(|e| e.hash), Some(hash));
		operation.op.mark_finalized(hash, justification)?;