From 29244ba76d479491c810cc5eb44e3e8b5ce83541 Mon Sep 17 00:00:00 2001 From: Hernando Castano Date: Tue, 18 Aug 2020 16:17:14 -0400 Subject: [PATCH] Introduce Minimal Header Chain: Proving Interface (#287) * 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 --- bridges/bin/node/runtime/Cargo.toml | 5 +++ bridges/bin/node/runtime/src/kovan.rs | 4 +- bridges/bin/node/runtime/src/rialto.rs | 4 +- bridges/modules/currency-exchange/Cargo.toml | 1 + .../currency-exchange/src/benchmarking.rs | 4 +- bridges/modules/currency-exchange/src/lib.rs | 28 ++++-------- bridges/primitives/header-chain/Cargo.toml | 18 ++++++++ bridges/primitives/header-chain/src/lib.rs | 44 +++++++++++++++++++ 8 files changed, 82 insertions(+), 26 deletions(-) create mode 100644 bridges/primitives/header-chain/Cargo.toml create mode 100644 bridges/primitives/header-chain/src/lib.rs diff --git a/bridges/bin/node/runtime/Cargo.toml b/bridges/bin/node/runtime/Cargo.toml index 09e58047bd..bedb7a9a0c 100644 --- a/bridges/bin/node/runtime/Cargo.toml +++ b/bridges/bin/node/runtime/Cargo.toml @@ -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] diff --git a/bridges/bin/node/runtime/src/kovan.rs b/bridges/bin/node/runtime/src/kovan.rs index 1855aa9dd3..189333a83c 100644 --- a/bridges/bin/node/runtime/src/kovan.rs +++ b/bridges/bin/node/runtime/src/kovan.rs @@ -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, }; @@ -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; diff --git a/bridges/bin/node/runtime/src/rialto.rs b/bridges/bin/node/runtime/src/rialto.rs index 800b0ab1c8..17e84a5e1a 100644 --- a/bridges/bin/node/runtime/src/rialto.rs +++ b/bridges/bin/node/runtime/src/rialto.rs @@ -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, }; @@ -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; diff --git a/bridges/modules/currency-exchange/Cargo.toml b/bridges/modules/currency-exchange/Cargo.toml index 03b3d692a3..4b0f0fd068 100644 --- a/bridges/modules/currency-exchange/Cargo.toml +++ b/bridges/modules/currency-exchange/Cargo.toml @@ -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 } diff --git a/bridges/modules/currency-exchange/src/benchmarking.rs b/bridges/modules/currency-exchange/src/benchmarking.rs index 30fb7e63f0..189dac8407 100644 --- a/bridges/modules/currency-exchange/src/benchmarking.rs +++ b/bridges/modules/currency-exchange/src/benchmarking.rs @@ -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}; @@ -50,7 +50,7 @@ pub trait Trait: CurrencyExchangeTrait { /// Prepare proof for importing exchange transaction. fn make_proof( proof_params: ProofParams, - ) -> <>::PeerBlockchain as PeerBlockchain>::TransactionInclusionProof; + ) -> <>::PeerBlockchain as BaseHeaderChain>::TransactionInclusionProof; } benchmarks_instance! { diff --git a/bridges/modules/currency-exchange/src/lib.rs b/bridges/modules/currency-exchange/src/lib.rs index a7c3c5026a..28a53d287b 100644 --- a/bridges/modules/currency-exchange/src/lib.rs +++ b/bridges/modules/currency-exchange/src/lib.rs @@ -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")] @@ -33,28 +34,15 @@ pub trait OnTransactionSubmitted { 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; -} - /// The module configuration trait pub trait Trait: frame_system::Trait { /// Handler for transaction submission result. type OnTransactionSubmitted: OnTransactionSubmitted; /// Represents the blockchain that we'll be exchanging currency with. - type PeerBlockchain: PeerBlockchain; + type PeerBlockchain: BaseHeaderChain; /// Peer blockchain transaction parser. type PeerMaybeLockFundsTransaction: MaybeLockFundsTransaction< - Transaction = ::Transaction, + Transaction = ::Transaction, >; /// Map between blockchains recipients. type RecipientsMap: RecipientsMap< @@ -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: <>::PeerBlockchain as PeerBlockchain>::TransactionInclusionProof, + proof: <>::PeerBlockchain as BaseHeaderChain>::TransactionInclusionProof, ) -> DispatchResult { let submitter = frame_system::ensure_signed(origin)?; @@ -146,7 +134,7 @@ decl_storage! { impl, I: Instance> Module { /// Returns true if currency exchange module is able to import given transaction proof in /// its current state. - pub fn filter_transaction_proof(proof: &::TransactionInclusionProof) -> bool { + pub fn filter_transaction_proof(proof: &::TransactionInclusionProof) -> bool { if let Err(err) = prepare_deposit_details::(proof) { frame_support::debug::trace!( target: "runtime", @@ -192,7 +180,7 @@ struct DepositDetails, I: Instance> { /// Verify and parse transaction proof, preparing everything required for importing /// this transaction proof. fn prepare_deposit_details, I: Instance>( - proof: &<>::PeerBlockchain as PeerBlockchain>::TransactionInclusionProof, + proof: &<>::PeerBlockchain as BaseHeaderChain>::TransactionInclusionProof, ) -> Result, Error> { // ensure that transaction is included in finalized block that we know of let transaction = >::PeerBlockchain::verify_transaction_inclusion_proof(proof) @@ -251,7 +239,7 @@ mod tests { pub struct DummyBlockchain; - impl PeerBlockchain for DummyBlockchain { + impl BaseHeaderChain for DummyBlockchain { type Transaction = RawTransaction; type TransactionInclusionProof = (bool, RawTransaction); diff --git a/bridges/primitives/header-chain/Cargo.toml b/bridges/primitives/header-chain/Cargo.toml new file mode 100644 index 0000000000..034f801001 --- /dev/null +++ b/bridges/primitives/header-chain/Cargo.toml @@ -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 "] +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", +] diff --git a/bridges/primitives/header-chain/src/lib.rs b/bridges/primitives/header-chain/src/lib.rs new file mode 100644 index 0000000000..7e5fa058cf --- /dev/null +++ b/bridges/primitives/header-chain/src/lib.rs @@ -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 . + +//! 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 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; +}