sc_consensus_grandpa_rpc/finality.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
19use serde::{Deserialize, Serialize};
20
21use sc_consensus_grandpa::FinalityProofProvider;
22use sp_runtime::traits::{Block as BlockT, NumberFor};
23
24#[derive(Clone, Serialize, Deserialize)]
25pub struct EncodedFinalityProof(pub sp_core::Bytes);
26
27/// Local trait mainly to allow mocking in tests.
28pub trait RpcFinalityProofProvider<Block: BlockT> {
29 /// Prove finality for the given block number by returning a Justification for the last block of
30 /// the authority set.
31 fn rpc_prove_finality(
32 &self,
33 block: NumberFor<Block>,
34 ) -> Result<Option<EncodedFinalityProof>, sc_consensus_grandpa::FinalityProofError>;
35}
36
37impl<B, Block> RpcFinalityProofProvider<Block> for FinalityProofProvider<B, Block>
38where
39 Block: BlockT,
40 NumberFor<Block>: finality_grandpa::BlockNumberOps,
41 B: sc_client_api::backend::Backend<Block> + Send + Sync + 'static,
42{
43 fn rpc_prove_finality(
44 &self,
45 block: NumberFor<Block>,
46 ) -> Result<Option<EncodedFinalityProof>, sc_consensus_grandpa::FinalityProofError> {
47 self.prove_finality(block).map(|x| x.map(|y| EncodedFinalityProof(y.into())))
48 }
49}