Skip to content

Commit

Permalink
Generic SyncHeader type (paritytech#529)
Browse files Browse the repository at this point in the history
* generic SyncHeader type

* add panic condition to method description

* extract -> into_inner

* checked_sub + expect
  • Loading branch information
svyatonik authored and serban300 committed Apr 8, 2024
1 parent eff38a4 commit 9c9ddfb
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 76 deletions.
2 changes: 2 additions & 0 deletions bridges/relays/headers-relay/src/sync_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ pub trait SourceHeader<Hash, Number>: Clone + std::fmt::Debug + PartialEq + Send
/// Returns ID of header.
fn id(&self) -> HeaderId<Hash, Number>;
/// Returns ID of parent header.
///
/// Panics if called for genesis header.
fn parent_id(&self) -> HeaderId<Hash, Number>;
}

Expand Down
39 changes: 2 additions & 37 deletions bridges/relays/millau-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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())
}
}
pub type SyncHeader = relay_substrate_client::SyncHeader<millau_runtime::Header>;
39 changes: 2 additions & 37 deletions bridges/relays/rialto-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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())
}
}
pub type SyncHeader = relay_substrate_client::SyncHeader<rialto_runtime::Header>;
2 changes: 2 additions & 0 deletions bridges/relays/substrate-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ mod chain;
mod client;
mod error;
mod rpc;
mod sync_header;

pub mod guard;
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.
Expand Down
61 changes: 61 additions & 0 deletions bridges/relays/substrate-client/src/sync_header.rs
Original file line number Diff line number Diff line change
@@ -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(),
)
}
}
2 changes: 1 addition & 1 deletion bridges/relays/substrate/src/millau_headers_to_rialto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl SubstrateHeadersSyncPipeline for MillauHeadersToRialto {
) -> Result<Self::SignedTransaction, SubstrateError> {
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)
}
Expand Down
2 changes: 1 addition & 1 deletion bridges/relays/substrate/src/rialto_headers_to_millau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl SubstrateHeadersSyncPipeline for RialtoHeadersToMillau {
) -> Result<Self::SignedTransaction, SubstrateError> {
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)
}
Expand Down

0 comments on commit 9c9ddfb

Please sign in to comment.