From f2781eb2e77b888e94c15342c4250bb2f0383330 Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Wed, 25 Nov 2020 20:41:45 +0300 Subject: [PATCH] Generic SyncHeader type (#529) * generic SyncHeader type * add panic condition to method description * extract -> into_inner * checked_sub + expect --- .../relays/headers-relay/src/sync_types.rs | 2 + bridges/relays/millau-client/src/lib.rs | 39 +----------- bridges/relays/rialto-client/src/lib.rs | 39 +----------- bridges/relays/substrate-client/src/lib.rs | 2 + .../substrate-client/src/sync_header.rs | 61 +++++++++++++++++++ .../substrate/src/millau_headers_to_rialto.rs | 2 +- .../substrate/src/rialto_headers_to_millau.rs | 2 +- 7 files changed, 71 insertions(+), 76 deletions(-) create mode 100644 bridges/relays/substrate-client/src/sync_header.rs diff --git a/bridges/relays/headers-relay/src/sync_types.rs b/bridges/relays/headers-relay/src/sync_types.rs index 7f975100f8edf..e1da6232258c9 100644 --- a/bridges/relays/headers-relay/src/sync_types.rs +++ b/bridges/relays/headers-relay/src/sync_types.rs @@ -84,6 +84,8 @@ pub trait SourceHeader: Clone + std::fmt::Debug + PartialEq + Send /// Returns ID of header. fn id(&self) -> HeaderId; /// Returns ID of parent header. + /// + /// Panics if called for genesis header. fn parent_id(&self) -> HeaderId; } diff --git a/bridges/relays/millau-client/src/lib.rs b/bridges/relays/millau-client/src/lib.rs index 9c018a8419a41..c7d04056878fa 100644 --- a/bridges/relays/millau-client/src/lib.rs +++ b/bridges/relays/millau-client/src/lib.rs @@ -17,13 +17,9 @@ //! Types used to connect to the Millau-Substrate chain. use codec::Encode; -use headers_relay::sync_types::SourceHeader; use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, Client, TransactionSignScheme}; use sp_core::{storage::StorageKey, Pair}; -use sp_runtime::{ - generic::SignedPayload, - traits::{Header as HeaderT, IdentifyAccount}, -}; +use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount}; use std::time::Duration; pub use millau_runtime::BridgeRialtoCall; @@ -126,35 +122,4 @@ impl std::fmt::Debug for SigningParams { } /// Millau header type used in headers sync. -#[derive(Clone, Debug, PartialEq)] -pub struct SyncHeader(millau_runtime::Header); - -impl std::ops::Deref for SyncHeader { - type Target = millau_runtime::Header; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl From for SyncHeader { - fn from(header: millau_runtime::Header) -> Self { - Self(header) - } -} - -impl From for millau_runtime::Header { - fn from(header: SyncHeader) -> Self { - header.0 - } -} - -impl SourceHeader for SyncHeader { - fn id(&self) -> HeaderId { - relay_utils::HeaderId(*self.number(), self.hash()) - } - - fn parent_id(&self) -> HeaderId { - relay_utils::HeaderId(*self.number() - 1, *self.parent_hash()) - } -} +pub type SyncHeader = relay_substrate_client::SyncHeader; diff --git a/bridges/relays/rialto-client/src/lib.rs b/bridges/relays/rialto-client/src/lib.rs index f45408d1538a4..9e38831d56c43 100644 --- a/bridges/relays/rialto-client/src/lib.rs +++ b/bridges/relays/rialto-client/src/lib.rs @@ -17,13 +17,9 @@ //! Types used to connect to the Rialto-Substrate chain. use codec::Encode; -use headers_relay::sync_types::SourceHeader; use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, Client, TransactionSignScheme}; use sp_core::{storage::StorageKey, Pair}; -use sp_runtime::{ - generic::SignedPayload, - traits::{Header as HeaderT, IdentifyAccount}, -}; +use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount}; use std::time::Duration; pub use rialto_runtime::BridgeMillauCall; @@ -134,35 +130,4 @@ impl Default for SigningParams { } /// Rialto header type used in headers sync. -#[derive(Clone, Debug, PartialEq)] -pub struct SyncHeader(rialto_runtime::Header); - -impl std::ops::Deref for SyncHeader { - type Target = rialto_runtime::Header; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl From for SyncHeader { - fn from(header: rialto_runtime::Header) -> Self { - Self(header) - } -} - -impl From for rialto_runtime::Header { - fn from(header: SyncHeader) -> Self { - header.0 - } -} - -impl SourceHeader for SyncHeader { - fn id(&self) -> HeaderId { - relay_utils::HeaderId(*self.number(), self.hash()) - } - - fn parent_id(&self) -> HeaderId { - relay_utils::HeaderId(*self.number() - 1, *self.parent_hash()) - } -} +pub type SyncHeader = relay_substrate_client::SyncHeader; diff --git a/bridges/relays/substrate-client/src/lib.rs b/bridges/relays/substrate-client/src/lib.rs index 567fc4a61984e..c6d077b21c562 100644 --- a/bridges/relays/substrate-client/src/lib.rs +++ b/bridges/relays/substrate-client/src/lib.rs @@ -22,6 +22,7 @@ mod chain; mod client; mod error; mod rpc; +mod sync_header; pub mod guard; pub mod headers_source; @@ -29,6 +30,7 @@ pub mod headers_source; pub use crate::chain::{BlockWithJustification, Chain, ChainWithBalances, TransactionSignScheme}; pub use crate::client::{Client, JustificationsSubscription, OpaqueGrandpaAuthoritiesSet}; pub use crate::error::{Error, Result}; +pub use crate::sync_header::SyncHeader; pub use bp_runtime::{BlockNumberOf, Chain as ChainBase, HashOf, HeaderOf}; /// Header id used by the chain. diff --git a/bridges/relays/substrate-client/src/sync_header.rs b/bridges/relays/substrate-client/src/sync_header.rs new file mode 100644 index 0000000000000..fd1c582b9480e --- /dev/null +++ b/bridges/relays/substrate-client/src/sync_header.rs @@ -0,0 +1,61 @@ +// Copyright 2019-2020 Parity Technologies (UK) Ltd. +// This file is part of Parity Bridges Common. + +// Parity Bridges Common is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity Bridges Common is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity Bridges Common. If not, see . + +use headers_relay::sync_types::SourceHeader; +use num_traits::{CheckedSub, One}; +use relay_utils::HeaderId; +use sp_runtime::traits::Header as HeaderT; + +/// Generic wrapper for `sp_runtime::traits::Header` based headers, that +/// implements `headers_relay::sync_types::SourceHeader` and may be used in headers sync directly. +#[derive(Clone, Debug, PartialEq)] +pub struct SyncHeader
(Header); + +impl
SyncHeader
{ + /// Extracts wrapped header from self. + pub fn into_inner(self) -> Header { + self.0 + } +} + +impl
std::ops::Deref for SyncHeader
{ + type Target = Header; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl
From
for SyncHeader
{ + fn from(header: Header) -> Self { + Self(header) + } +} + +impl SourceHeader for SyncHeader
{ + fn id(&self) -> HeaderId { + relay_utils::HeaderId(*self.number(), self.hash()) + } + + fn parent_id(&self) -> HeaderId { + relay_utils::HeaderId( + self.number() + .checked_sub(&One::one()) + .expect("should never be called for genesis header"), + *self.parent_hash(), + ) + } +} diff --git a/bridges/relays/substrate/src/millau_headers_to_rialto.rs b/bridges/relays/substrate/src/millau_headers_to_rialto.rs index 714a6c22f5e61..8b77e71657a09 100644 --- a/bridges/relays/substrate/src/millau_headers_to_rialto.rs +++ b/bridges/relays/substrate/src/millau_headers_to_rialto.rs @@ -54,7 +54,7 @@ impl SubstrateHeadersSyncPipeline for MillauHeadersToRialto { ) -> Result { let account_id = self.target_sign.signer.public().as_array_ref().clone().into(); let nonce = self.target_client.next_account_index(account_id).await?; - let call = BridgeMillauCall::import_signed_header(header.header().clone().into()).into(); + let call = BridgeMillauCall::import_signed_header(header.header().clone().into_inner()).into(); let transaction = Rialto::sign_transaction(&self.target_client, &self.target_sign.signer, nonce, call); Ok(transaction) } diff --git a/bridges/relays/substrate/src/rialto_headers_to_millau.rs b/bridges/relays/substrate/src/rialto_headers_to_millau.rs index 92a141654c503..3a13c6e148c63 100644 --- a/bridges/relays/substrate/src/rialto_headers_to_millau.rs +++ b/bridges/relays/substrate/src/rialto_headers_to_millau.rs @@ -53,7 +53,7 @@ impl SubstrateHeadersSyncPipeline for RialtoHeadersToMillau { ) -> Result { let account_id = self.target_sign.signer.public().as_array_ref().clone().into(); let nonce = self.target_client.next_account_index(account_id).await?; - let call = BridgeRialtoCall::import_signed_header(header.header().clone().into()).into(); + let call = BridgeRialtoCall::import_signed_header(header.header().clone().into_inner()).into(); let transaction = Millau::sign_transaction(&self.target_client, &self.target_sign.signer, nonce, call); Ok(transaction) }