sc_consensus_manual_seal/finalize_block.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Block finalization utilities
20
21use crate::rpc;
22use sc_client_api::backend::{Backend as ClientBackend, Finalizer};
23use sp_runtime::{traits::Block as BlockT, Justification};
24use std::{marker::PhantomData, sync::Arc};
25
26/// params for block finalization.
27pub struct FinalizeBlockParams<B: BlockT, F, CB> {
28 /// hash of the block
29 pub hash: <B as BlockT>::Hash,
30 /// sender to report errors/success to the rpc.
31 pub sender: rpc::Sender<()>,
32 /// finalization justification
33 pub justification: Option<Justification>,
34 /// Finalizer trait object.
35 pub finalizer: Arc<F>,
36 /// phantom type to pin the Backend type
37 pub _phantom: PhantomData<CB>,
38}
39
40/// finalizes a block in the backend with the given params.
41pub async fn finalize_block<B, F, CB>(params: FinalizeBlockParams<B, F, CB>)
42where
43 B: BlockT,
44 F: Finalizer<B, CB>,
45 CB: ClientBackend<B>,
46{
47 let FinalizeBlockParams { hash, mut sender, justification, finalizer, .. } = params;
48
49 match finalizer.finalize_block(hash, justification, true) {
50 Err(e) => {
51 log::warn!("Failed to finalize block {}", e);
52 rpc::send_result(&mut sender, Err(e.into()))
53 },
54 Ok(()) => {
55 log::info!("✅ Successfully finalized block: {}", hash);
56 rpc::send_result(&mut sender, Ok(()))
57 },
58 }
59}