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

Code before:
		info: &BlockchainInfo<Block>,
		notify: bool,
	) -> sp_blockchain::Result<()> {
		if hash == info.finalized_hash {
			warn!(
				"Possible safety violation: attempted to re-finalize last finalized block {:?} ",
				hash,
			);
			return Ok(())
		}

		// 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)?;
			}
		}


Review comment (suggestion):
```suggestion
		// We may need to coercively update the best block if there is more than one
```

Code after:
		info: &BlockchainInfo<Block>,
		notify: bool,
	) -> sp_blockchain::Result<()> {
		if hash == info.finalized_hash {
			warn!(
				"Possible safety violation: attempted to re-finalize last finalized block {:?} ",
				hash,
			);
			return Ok(())
		}

		// 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)?;
			}
		}