Generic SyncHeader type (#529)

* generic SyncHeader type

* add panic condition to method description

* extract -> into_inner

* checked_sub + expect
This commit is contained in:
Svyatoslav Nikolsky
2020-11-25 20:41:45 +03:00
committed by Bastian Köcher
parent dd7242bc79
commit 441f63a34f
7 changed files with 71 additions and 76 deletions
@@ -84,6 +84,8 @@ pub trait SourceHeader<Hash, Number>: Clone + std::fmt::Debug + PartialEq + Send
/// Returns ID of header. /// Returns ID of header.
fn id(&self) -> HeaderId<Hash, Number>; fn id(&self) -> HeaderId<Hash, Number>;
/// Returns ID of parent header. /// Returns ID of parent header.
///
/// Panics if called for genesis header.
fn parent_id(&self) -> HeaderId<Hash, Number>; fn parent_id(&self) -> HeaderId<Hash, Number>;
} }
+2 -37
View File
@@ -17,13 +17,9 @@
//! Types used to connect to the Millau-Substrate chain. //! Types used to connect to the Millau-Substrate chain.
use codec::Encode; use codec::Encode;
use headers_relay::sync_types::SourceHeader;
use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, Client, TransactionSignScheme}; use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, Client, TransactionSignScheme};
use sp_core::{storage::StorageKey, Pair}; use sp_core::{storage::StorageKey, Pair};
use sp_runtime::{ use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
generic::SignedPayload,
traits::{Header as HeaderT, IdentifyAccount},
};
use std::time::Duration; use std::time::Duration;
pub use millau_runtime::BridgeRialtoCall; pub use millau_runtime::BridgeRialtoCall;
@@ -126,35 +122,4 @@ impl std::fmt::Debug for SigningParams {
} }
/// Millau header type used in headers sync. /// Millau header type used in headers sync.
#[derive(Clone, Debug, PartialEq)] pub type SyncHeader = relay_substrate_client::SyncHeader<millau_runtime::Header>;
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<millau_runtime::Header> for SyncHeader {
fn from(header: millau_runtime::Header) -> Self {
Self(header)
}
}
impl From<SyncHeader> for millau_runtime::Header {
fn from(header: SyncHeader) -> Self {
header.0
}
}
impl SourceHeader<millau_runtime::Hash, millau_runtime::BlockNumber> 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())
}
}
+2 -37
View File
@@ -17,13 +17,9 @@
//! Types used to connect to the Rialto-Substrate chain. //! Types used to connect to the Rialto-Substrate chain.
use codec::Encode; use codec::Encode;
use headers_relay::sync_types::SourceHeader;
use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, Client, TransactionSignScheme}; use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, Client, TransactionSignScheme};
use sp_core::{storage::StorageKey, Pair}; use sp_core::{storage::StorageKey, Pair};
use sp_runtime::{ use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
generic::SignedPayload,
traits::{Header as HeaderT, IdentifyAccount},
};
use std::time::Duration; use std::time::Duration;
pub use rialto_runtime::BridgeMillauCall; pub use rialto_runtime::BridgeMillauCall;
@@ -134,35 +130,4 @@ impl Default for SigningParams {
} }
/// Rialto header type used in headers sync. /// Rialto header type used in headers sync.
#[derive(Clone, Debug, PartialEq)] pub type SyncHeader = relay_substrate_client::SyncHeader<rialto_runtime::Header>;
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<rialto_runtime::Header> for SyncHeader {
fn from(header: rialto_runtime::Header) -> Self {
Self(header)
}
}
impl From<SyncHeader> for rialto_runtime::Header {
fn from(header: SyncHeader) -> Self {
header.0
}
}
impl SourceHeader<rialto_runtime::Hash, rialto_runtime::BlockNumber> 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())
}
}
@@ -22,6 +22,7 @@ mod chain;
mod client; mod client;
mod error; mod error;
mod rpc; mod rpc;
mod sync_header;
pub mod guard; pub mod guard;
pub mod headers_source; pub mod headers_source;
@@ -29,6 +30,7 @@ pub mod headers_source;
pub use crate::chain::{BlockWithJustification, Chain, ChainWithBalances, TransactionSignScheme}; pub use crate::chain::{BlockWithJustification, Chain, ChainWithBalances, TransactionSignScheme};
pub use crate::client::{Client, JustificationsSubscription, OpaqueGrandpaAuthoritiesSet}; pub use crate::client::{Client, JustificationsSubscription, OpaqueGrandpaAuthoritiesSet};
pub use crate::error::{Error, Result}; pub use crate::error::{Error, Result};
pub use crate::sync_header::SyncHeader;
pub use bp_runtime::{BlockNumberOf, Chain as ChainBase, HashOf, HeaderOf}; pub use bp_runtime::{BlockNumberOf, Chain as ChainBase, HashOf, HeaderOf};
/// Header id used by the chain. /// Header id used by the chain.
@@ -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 <http://www.gnu.org/licenses/>.
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>(Header);
impl<Header> SyncHeader<Header> {
/// Extracts wrapped header from self.
pub fn into_inner(self) -> Header {
self.0
}
}
impl<Header> std::ops::Deref for SyncHeader<Header> {
type Target = Header;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<Header> From<Header> for SyncHeader<Header> {
fn from(header: Header) -> Self {
Self(header)
}
}
impl<Header: HeaderT> SourceHeader<Header::Hash, Header::Number> for SyncHeader<Header> {
fn id(&self) -> HeaderId<Header::Hash, Header::Number> {
relay_utils::HeaderId(*self.number(), self.hash())
}
fn parent_id(&self) -> HeaderId<Header::Hash, Header::Number> {
relay_utils::HeaderId(
self.number()
.checked_sub(&One::one())
.expect("should never be called for genesis header"),
*self.parent_hash(),
)
}
}
@@ -54,7 +54,7 @@ impl SubstrateHeadersSyncPipeline for MillauHeadersToRialto {
) -> Result<Self::SignedTransaction, SubstrateError> { ) -> Result<Self::SignedTransaction, SubstrateError> {
let account_id = self.target_sign.signer.public().as_array_ref().clone().into(); 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 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); let transaction = Rialto::sign_transaction(&self.target_client, &self.target_sign.signer, nonce, call);
Ok(transaction) Ok(transaction)
} }
@@ -53,7 +53,7 @@ impl SubstrateHeadersSyncPipeline for RialtoHeadersToMillau {
) -> Result<Self::SignedTransaction, SubstrateError> { ) -> Result<Self::SignedTransaction, SubstrateError> {
let account_id = self.target_sign.signer.public().as_array_ref().clone().into(); 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 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); let transaction = Millau::sign_transaction(&self.target_client, &self.target_sign.signer, nonce, call);
Ok(transaction) Ok(transaction)
} }