finality_relay/lib.rs
1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
16
17//! This crate has single entrypoint to run synchronization loop that is built around finality
18//! proofs, as opposed to headers synchronization loop, which is built around headers. The headers
19//! are still submitted to the target node, but are treated as auxiliary data as we are not trying
20//! to submit all source headers to the target node.
21
22pub use crate::{
23 base::{FinalityPipeline, SourceClientBase},
24 finality_loop::{
25 metrics_prefix, run, FinalitySyncParams, HeadersToRelay, SourceClient, TargetClient,
26 },
27 finality_proofs::{FinalityProofsBuf, FinalityProofsStream},
28 sync_loop_metrics::SyncLoopMetrics,
29};
30
31use bp_header_chain::ConsensusLogReader;
32use relay_utils::{FailedClient, MaybeConnectionError};
33use std::fmt::Debug;
34
35mod base;
36mod finality_loop;
37mod finality_proofs;
38mod headers;
39mod mock;
40mod sync_loop_metrics;
41
42/// Finality proofs synchronization pipeline.
43pub trait FinalitySyncPipeline: FinalityPipeline {
44 /// A reader that can extract the consensus log from the header digest and interpret it.
45 type ConsensusLogReader: ConsensusLogReader;
46 /// Type of header that we're syncing.
47 type Header: SourceHeader<Self::Hash, Self::Number, Self::ConsensusLogReader>;
48}
49
50/// Header that we're receiving from source node.
51pub trait SourceHeader<Hash, Number, Reader>: Clone + Debug + PartialEq + Send + Sync {
52 /// Returns hash of header.
53 fn hash(&self) -> Hash;
54 /// Returns number of header.
55 fn number(&self) -> Number;
56 /// Returns true if this header needs to be submitted to target node.
57 fn is_mandatory(&self) -> bool;
58}
59
60/// Error that may happen inside finality synchronization loop.
61#[derive(Debug)]
62enum Error<P: FinalitySyncPipeline, SourceError, TargetError> {
63 /// Source client request has failed with given error.
64 Source(SourceError),
65 /// Target client request has failed with given error.
66 Target(TargetError),
67 /// Finality proof for mandatory header is missing from the source node.
68 MissingMandatoryFinalityProof(P::Number),
69 /// `submit_finality_proof` transaction failed
70 ProofSubmissionTxFailed {
71 #[allow(dead_code)]
72 submitted_number: P::Number,
73 #[allow(dead_code)]
74 best_number_at_target: P::Number,
75 },
76 /// `submit_finality_proof` transaction lost
77 ProofSubmissionTxLost,
78}
79
80impl<P, SourceError, TargetError> Error<P, SourceError, TargetError>
81where
82 P: FinalitySyncPipeline,
83 SourceError: MaybeConnectionError,
84 TargetError: MaybeConnectionError,
85{
86 fn fail_if_connection_error(&self) -> Result<(), FailedClient> {
87 match *self {
88 Error::Source(ref error) if error.is_connection_error() => Err(FailedClient::Source),
89 Error::Target(ref error) if error.is_connection_error() => Err(FailedClient::Target),
90 _ => Ok(()),
91 }
92 }
93}