Skip to content

Commit

Permalink
Introduce Minimal Header Chain: Proving Interface (paritytech#287)
Browse files Browse the repository at this point in the history
* Add header-chain primitive crate

* Make MinimalHeaderChain functionaly the same as PeerBlockchain

* Use a better doc comment for MinimalHeaderChain

* Fix benchmark compilation

* Rust Fmt

* Remove Substrate based dependencies

* Rename MinimalHeaderChain to BaseHeaderChain
  • Loading branch information
HCastano authored and bkchr committed Apr 10, 2024
1 parent a0555d8 commit 29244ba
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 26 deletions.
5 changes: 5 additions & 0 deletions bridges/bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ version = "0.1.0"
default-features = false
path = "../../../primitives/ethereum-poa"

[dependencies.bp-header-chain]
version = "0.1.0"
default-features = false
path = "../../../primitives/header-chain"

# Dev Dependencies

[dev-dependencies.libsecp256k1]
Expand Down
4 changes: 2 additions & 2 deletions bridges/bin/node/runtime/src/kovan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use crate::exchange::EthereumTransactionInclusionProof;

use bp_eth_poa::{Address, Header, RawTransaction, U256};
use bp_header_chain::BaseHeaderChain;
use frame_support::RuntimeDebug;
use hex_literal::hex;
use pallet_bridge_currency_exchange::PeerBlockchain;
use pallet_bridge_eth_poa::{
AuraConfiguration, PruningStrategy as BridgePruningStrategy, ValidatorsConfiguration, ValidatorsSource,
};
Expand Down Expand Up @@ -137,7 +137,7 @@ impl BridgePruningStrategy for PruningStrategy {
/// The Kovan Blockchain as seen by the runtime.
pub struct KovanBlockchain;

impl PeerBlockchain for KovanBlockchain {
impl BaseHeaderChain for KovanBlockchain {
type Transaction = RawTransaction;
type TransactionInclusionProof = EthereumTransactionInclusionProof;

Expand Down
4 changes: 2 additions & 2 deletions bridges/bin/node/runtime/src/rialto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use crate::exchange::EthereumTransactionInclusionProof;

use bp_eth_poa::{Address, Header, RawTransaction, U256};
use bp_header_chain::BaseHeaderChain;
use frame_support::RuntimeDebug;
use hex_literal::hex;
use pallet_bridge_currency_exchange::PeerBlockchain;
use pallet_bridge_eth_poa::{
AuraConfiguration, PruningStrategy as TPruningStrategy, ValidatorsConfiguration, ValidatorsSource,
};
Expand Down Expand Up @@ -110,7 +110,7 @@ impl TPruningStrategy for PruningStrategy {
/// The Rialto Blockchain as seen by the runtime.
pub struct RialtoBlockchain;

impl PeerBlockchain for RialtoBlockchain {
impl BaseHeaderChain for RialtoBlockchain {
type Transaction = RawTransaction;
type TransactionInclusionProof = EthereumTransactionInclusionProof;

Expand Down
1 change: 1 addition & 0 deletions bridges/modules/currency-exchange/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]
bp-currency-exchange = { path = "../../primitives/currency-exchange", default-features = false }
bp-header-chain = { path = "../../primitives/header-chain", default-features = false }
codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false }
serde = { version = "1.0", optional = true }

Expand Down
4 changes: 2 additions & 2 deletions bridges/modules/currency-exchange/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! So we are giving runtime opportunity to prepare environment and construct proof
//! before invoking module calls.
use super::{Call, Instance, Module as CurrencyExchangeModule, PeerBlockchain, Trait as CurrencyExchangeTrait};
use super::{BaseHeaderChain, Call, Instance, Module as CurrencyExchangeModule, Trait as CurrencyExchangeTrait};
use sp_std::prelude::*;

use frame_benchmarking::{account, benchmarks_instance};
Expand Down Expand Up @@ -50,7 +50,7 @@ pub trait Trait<I: Instance>: CurrencyExchangeTrait<I> {
/// Prepare proof for importing exchange transaction.
fn make_proof(
proof_params: ProofParams<Self::AccountId>,
) -> <<Self as CurrencyExchangeTrait<I>>::PeerBlockchain as PeerBlockchain>::TransactionInclusionProof;
) -> <<Self as CurrencyExchangeTrait<I>>::PeerBlockchain as BaseHeaderChain>::TransactionInclusionProof;
}

benchmarks_instance! {
Expand Down
28 changes: 8 additions & 20 deletions bridges/modules/currency-exchange/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
use bp_currency_exchange::{
CurrencyConverter, DepositInto, Error as ExchangeError, MaybeLockFundsTransaction, RecipientsMap,
};
use frame_support::{decl_error, decl_module, decl_storage, ensure, Parameter};
use bp_header_chain::BaseHeaderChain;
use frame_support::{decl_error, decl_module, decl_storage, ensure};
use sp_runtime::DispatchResult;

#[cfg(feature = "runtime-benchmarks")]
Expand All @@ -33,28 +34,15 @@ pub trait OnTransactionSubmitted<AccountId> {
fn on_valid_transaction_submitted(submitter: AccountId);
}

/// Peer blockchain interface.
pub trait PeerBlockchain {
/// Transaction type.
type Transaction: Parameter;
/// Transaction inclusion proof type.
type TransactionInclusionProof: Parameter;

/// Verify that transaction is a part of given block.
///
/// Returns Some(transaction) if proof is valid and None otherwise.
fn verify_transaction_inclusion_proof(proof: &Self::TransactionInclusionProof) -> Option<Self::Transaction>;
}

/// The module configuration trait
pub trait Trait<I = DefaultInstance>: frame_system::Trait {
/// Handler for transaction submission result.
type OnTransactionSubmitted: OnTransactionSubmitted<Self::AccountId>;
/// Represents the blockchain that we'll be exchanging currency with.
type PeerBlockchain: PeerBlockchain;
type PeerBlockchain: BaseHeaderChain;
/// Peer blockchain transaction parser.
type PeerMaybeLockFundsTransaction: MaybeLockFundsTransaction<
Transaction = <Self::PeerBlockchain as PeerBlockchain>::Transaction,
Transaction = <Self::PeerBlockchain as BaseHeaderChain>::Transaction,
>;
/// Map between blockchains recipients.
type RecipientsMap: RecipientsMap<
Expand Down Expand Up @@ -101,7 +89,7 @@ decl_module! {
#[weight = 0] // TODO: update me (https://github.com/paritytech/parity-bridges-common/issues/78)
pub fn import_peer_transaction(
origin,
proof: <<T as Trait<I>>::PeerBlockchain as PeerBlockchain>::TransactionInclusionProof,
proof: <<T as Trait<I>>::PeerBlockchain as BaseHeaderChain>::TransactionInclusionProof,
) -> DispatchResult {
let submitter = frame_system::ensure_signed(origin)?;

Expand Down Expand Up @@ -146,7 +134,7 @@ decl_storage! {
impl<T: Trait<I>, I: Instance> Module<T, I> {
/// Returns true if currency exchange module is able to import given transaction proof in
/// its current state.
pub fn filter_transaction_proof(proof: &<T::PeerBlockchain as PeerBlockchain>::TransactionInclusionProof) -> bool {
pub fn filter_transaction_proof(proof: &<T::PeerBlockchain as BaseHeaderChain>::TransactionInclusionProof) -> bool {
if let Err(err) = prepare_deposit_details::<T, I>(proof) {
frame_support::debug::trace!(
target: "runtime",
Expand Down Expand Up @@ -192,7 +180,7 @@ struct DepositDetails<T: Trait<I>, I: Instance> {
/// Verify and parse transaction proof, preparing everything required for importing
/// this transaction proof.
fn prepare_deposit_details<T: Trait<I>, I: Instance>(
proof: &<<T as Trait<I>>::PeerBlockchain as PeerBlockchain>::TransactionInclusionProof,
proof: &<<T as Trait<I>>::PeerBlockchain as BaseHeaderChain>::TransactionInclusionProof,
) -> Result<DepositDetails<T, I>, Error<T, I>> {
// ensure that transaction is included in finalized block that we know of
let transaction = <T as Trait<I>>::PeerBlockchain::verify_transaction_inclusion_proof(proof)
Expand Down Expand Up @@ -251,7 +239,7 @@ mod tests {

pub struct DummyBlockchain;

impl PeerBlockchain for DummyBlockchain {
impl BaseHeaderChain for DummyBlockchain {
type Transaction = RawTransaction;
type TransactionInclusionProof = (bool, RawTransaction);

Expand Down
18 changes: 18 additions & 0 deletions bridges/primitives/header-chain/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "bp-header-chain"
description = "A common interface for describing what a bridge pallet should be able to do."
version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies.parity-scale-codec]
version = "1.3.1"
default-features = false
features = ["derive"]

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
]
44 changes: 44 additions & 0 deletions bridges/primitives/header-chain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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/>.

//! Defines traits which represent a common interface for Substrate pallets which want to
//! incorporate bridge functionality.
#![cfg_attr(not(feature = "std"), no_std)]

use core::clone::Clone;
use core::cmp::Eq;
use core::fmt::Debug;
use parity_scale_codec::{Codec, EncodeLike};

/// A type that can be used as a parameter in a dispatchable function.
///
/// When using `decl_module` all arguments for call functions must implement this trait.
pub trait Parameter: Codec + EncodeLike + Clone + Eq + Debug {}
impl<T> Parameter for T where T: Codec + EncodeLike + Clone + Eq + Debug {}

/// A base trait for pallets which want to keep track of a full set of headers from a bridged chain.
pub trait BaseHeaderChain {
/// Transaction type.
type Transaction: Parameter;
/// Transaction inclusion proof type.
type TransactionInclusionProof: Parameter;

/// Verify that transaction is a part of given block.
///
/// Returns Some(transaction) if proof is valid and None otherwise.
fn verify_transaction_inclusion_proof(proof: &Self::TransactionInclusionProof) -> Option<Self::Transaction>;
}

0 comments on commit 29244ba

Please sign in to comment.