relay_substrate_client/sync_header.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
17use bp_header_chain::ConsensusLogReader;
18use finality_relay::SourceHeader as FinalitySourceHeader;
19use sp_runtime::traits::Header as HeaderT;
20
21/// Generic wrapper for `sp_runtime::traits::Header` based headers, that
22/// implements `finality_relay::SourceHeader` and may be used in headers sync directly.
23#[derive(Clone, Debug, PartialEq)]
24pub struct SyncHeader<Header>(Header);
25
26impl<Header> SyncHeader<Header> {
27 /// Extracts wrapped header from self.
28 pub fn into_inner(self) -> Header {
29 self.0
30 }
31}
32
33impl<Header> std::ops::Deref for SyncHeader<Header> {
34 type Target = Header;
35
36 fn deref(&self) -> &Self::Target {
37 &self.0
38 }
39}
40
41impl<Header> From<Header> for SyncHeader<Header> {
42 fn from(header: Header) -> Self {
43 Self(header)
44 }
45}
46
47impl<Header: HeaderT, R: ConsensusLogReader> FinalitySourceHeader<Header::Hash, Header::Number, R>
48 for SyncHeader<Header>
49{
50 fn hash(&self) -> Header::Hash {
51 self.0.hash()
52 }
53
54 fn number(&self) -> Header::Number {
55 *self.0.number()
56 }
57
58 fn is_mandatory(&self) -> bool {
59 R::schedules_authorities_change(self.digest())
60 }
61}