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

Code before:

	/// Provide BEEFY finality for block based on `finality_proof`:
	/// 1. Prune irrelevant past sessions from the oracle,
	/// 2. Set BEEFY best block,
	/// 3. Send best block hash and `finality_proof` to RPC worker.
	///
	/// Expects `finality proof` to be valid.
	fn finalize(&mut self, finality_proof: BeefyVersionedFinalityProof<B>) -> Result<(), Error> {
		let block_num = match finality_proof {
			VersionedFinalityProof::V1(ref sc) => sc.commitment.block_number,
		};

		// Conclude voting round for this block.
		self.voting_oracle.rounds_mut().ok_or(Error::UninitSession)?.conclude(block_num);
		// Prune any now "finalized" sessions from queue.
		self.voting_oracle.try_prune();

		if Some(block_num) > self.best_beefy_block {
			// Set new best BEEFY block number.
			self.best_beefy_block = Some(block_num);
			metric_set!(self, beefy_best_block, block_num);

			self.on_demand_justifications.cancel_requests_older_than(block_num);

			let maybe_block_hash =
				self.backend.blockchain().expect_block_hash_from_id(&BlockId::Number(block_num));

			match maybe_block_hash {
				Ok(hash) => {
					if let Err(err) = self
						.backend
						.append_justification(&hash, (BEEFY_ENGINE_ID, finality_proof.encode()))
					{
						error!(target: "beefy", "🥩 Error {:?} on appending justification: {:?}", err, finality_proof);
					};

					self.links
						.to_rpc_best_block_sender
						.notify(|| Ok::<_, ()>(hash))
						.expect("forwards closure result; the closure always returns Ok; qed.");
				},
				Err(err) => {
					error!(target: "beefy", "🥩 Error {:?} hash not known for given proof: {:?}", err, finality_proof);
				},
			};

			self.links
				.to_rpc_justif_sender
				.notify(|| Ok::<_, ()>(finality_proof))
				.expect("forwards closure result; the closure always returns Ok; qed.");
		} else {

Review comment (suggestion):
```suggestion
			let block_hash =
				match self.backend.blockchain().expect_block_hash_from_id(&BlockId::Number(block_num)) {
				Ok(),
				Err(),
			}
```

Would make the code a little bit easier.

Code after:

	/// Provide BEEFY finality for block based on `finality_proof`:
	/// 1. Prune irrelevant past sessions from the oracle,
	/// 2. Set BEEFY best block,
	/// 3. Send best block hash and `finality_proof` to RPC worker.
	///
	/// Expects `finality proof` to be valid.
	fn finalize(&mut self, finality_proof: BeefyVersionedFinalityProof<B>) -> Result<(), Error> {
		let block_num = match finality_proof {
			VersionedFinalityProof::V1(ref sc) => sc.commitment.block_number,
		};

		// Conclude voting round for this block.
		self.voting_oracle.rounds_mut().ok_or(Error::UninitSession)?.conclude(block_num);
		// Prune any now "finalized" sessions from queue.
		self.voting_oracle.try_prune();

		if Some(block_num) > self.best_beefy_block {
			// Set new best BEEFY block number.
			self.best_beefy_block = Some(block_num);
			metric_set!(self, beefy_best_block, block_num);

			self.on_demand_justifications.cancel_requests_older_than(block_num);

			if let Err(e) = self
				.backend
				.blockchain()
				.expect_block_hash_from_id(&BlockId::Number(block_num))
				.and_then(|hash| {
					self.links
						.to_rpc_best_block_sender
						.notify(|| Ok::<_, ()>(hash))
						.expect("forwards closure result; the closure always returns Ok; qed.");

					self.backend
						.append_justification(&hash, (BEEFY_ENGINE_ID, finality_proof.encode()))
				}) {
				error!(target: "beefy", "🥩 Error {:?} on appending justification: {:?}", e, finality_proof);
			}

			self.links
				.to_rpc_justif_sender
				.notify(|| Ok::<_, ()>(finality_proof))
				.expect("forwards closure result; the closure always returns Ok; qed.");
		} else {
			debug!(target: "beefy", "🥩 Can't set best beefy to older: {}", block_num);
		}
		Ok(())
	}

	/// Handle previously buffered justifications and votes that now land in the voting interval.