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

Code before:

		if block == last_finalized {
			warn!(
				"Possible safety violation: attempted to re-finalize last finalized block {:?} ",
				last_finalized
			);
			return Ok(())
		}

		let route_from_finalized =
			sp_blockchain::tree_route(self.backend.blockchain(), last_finalized, block)?;

		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, last_finalized
			);

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

		// 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();
		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(block));
		operation.op.mark_finalized(block, justification)?;


Review comment (suggestion):
```suggestion
		if self.backend.blockchain().leaves()?.len() > 1 {
```

Code after:

		if block == last_finalized {
			warn!(
				"Possible safety violation: attempted to re-finalize last finalized block {:?} ",
				last_finalized
			);
			return Ok(())
		}

		let route_from_finalized =
			sp_blockchain::tree_route(self.backend.blockchain(), last_finalized, block)?;

		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, last_finalized
			);

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

		// 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();
		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(block));
		operation.op.mark_finalized(block, justification)?;