Skip to content

Commit

Permalink
Pangolin2 <> Pangoro2 bridge (#285)
Browse files Browse the repository at this point in the history
* Copy darwinia bm => pangoro bm
Copy crab bm => pangolin bm

* Add pangolin&pangoro bridge-messages

* Add bridge related pallets for pangolin&pangoro

* Add bridge palles to runtime for pangolin & pangoro

* Fix compile

* Missing changes

* Correct bridge-dispatch

* Format

* Update genesis
  • Loading branch information
Guantong authored Feb 20, 2023
1 parent 19f643c commit 8f2c12a
Show file tree
Hide file tree
Showing 21 changed files with 1,142 additions and 0 deletions.
12 changes: 12 additions & 0 deletions node/src/chain_spec/pangolin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ pub fn genesis_config() -> ChainSpec {
)
},
},

// S2S stuff.
bridge_moonbase_grandpa: Default::default(),
bridge_moonbase_parachain: Default::default(),
bridge_pangoro_messages: Default::default(),
pangoro_fee_market: Default::default(),
}
},
Vec::new(),
Expand Down Expand Up @@ -358,5 +364,11 @@ fn testnet_genesis(
)
},
},

// S2S stuff.
bridge_moonbase_grandpa: Default::default(),
bridge_moonbase_parachain: Default::default(),
bridge_pangoro_messages: Default::default(),
pangoro_fee_market: Default::default(),
}
}
12 changes: 12 additions & 0 deletions node/src/chain_spec/pangoro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ pub fn genesis_config() -> ChainSpec {
)
},
},

// S2S stuff.
bridge_rococo_grandpa: Default::default(),
bridge_rococo_parachain: Default::default(),
bridge_pangolin_messages: Default::default(),
pangolin_fee_market: Default::default(),
}
},
Vec::new(),
Expand Down Expand Up @@ -358,5 +364,11 @@ fn testnet_genesis(
)
},
},

// S2S stuff.
bridge_rococo_grandpa: Default::default(),
bridge_rococo_parachain: Default::default(),
bridge_pangolin_messages: Default::default(),
pangolin_fee_market: Default::default(),
}
}
1 change: 1 addition & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod xcm_configs;
pub use bp_darwinia_core as bp_crab;
pub use bp_darwinia_core as bp_darwinia;
pub use bp_darwinia_core as bp_pangolin;
pub use bp_darwinia_core as bp_pangoro;

#[cfg(feature = "test")]
pub mod test;
Expand Down
20 changes: 20 additions & 0 deletions runtime/pangolin/src/bridges_message/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This file is part of Darwinia.
//
// Copyright (C) 2018-2023 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia 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.
//
// Darwinia 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 Darwinia. If not, see <https://www.gnu.org/licenses/>.

pub mod pangoro;
pub use pangoro as bm_pangoro;
182 changes: 182 additions & 0 deletions runtime/pangolin/src/bridges_message/pangoro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// This file is part of Darwinia.
//
// Copyright (C) 2018-2023 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia 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.
//
// Darwinia 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 Darwinia. If not, see <https://www.gnu.org/licenses/>.

// crates.io
use codec::{Decode, Encode};
use scale_info::TypeInfo;
// paritytech
use frame_support::{weights::Weight, RuntimeDebug};
use sp_runtime::{FixedPointNumber, FixedU128};
// darwinia
use crate::*;
use bp_messages::{source_chain::*, target_chain::*, *};
use bp_polkadot_core::parachains::ParaId;
use bp_runtime::*;
use bridge_runtime_common::{
lanes::*,
messages::{source::*, target::*, *},
};
use darwinia_common_runtime::*;

/// Message delivery proof for Pangolin -> Pangoro messages.
pub type ToPangoroMessagesDeliveryProof = FromBridgedChainMessagesDeliveryProof<bp_pangoro::Hash>;
/// Message proof for Pangoro -> Pangolin messages.
pub type FromPangoroMessagesProof = FromBridgedChainMessagesProof<bp_pangoro::Hash>;

/// Message payload for Pangolin -> Pangoro messages.
pub type ToPangoroMessagePayload = FromThisChainMessagePayload<WithPangoroMessageBridge>;
/// Message payload for Pangoro -> Pangolin messages.
pub type FromPangoroMessagePayload = FromBridgedChainMessagePayload<WithPangoroMessageBridge>;

/// Message verifier for Pangolin -> Pangoro messages.
pub type ToPangoroMessageVerifier<R> =
FromThisChainMessageVerifier<WithPangoroMessageBridge, R, WithPangoroFeeMarket>;

/// Encoded Pangoro Call as it comes from Pangoro.
pub type FromPangoroEncodedCall = FromBridgedChainEncodedMessageCall<RuntimeCall>;

/// Call-dispatch based message dispatch for Pangoro -> Pangolin messages.
pub type FromPangoroMessageDispatch = FromBridgedChainMessageDispatch<
WithPangoroMessageBridge,
Runtime,
Balances,
WithPangoroDispatch,
>;

pub const INITIAL_PANGORO_TO_PANGOLIN_CONVERSION_RATE: FixedU128 =
FixedU128::from_inner(FixedU128::DIV);

frame_support::parameter_types! {
/// Pangolin to Pangoro conversion rate. Initially we treate both tokens as equal.
pub storage PangoroToPangolinConversionRate: FixedU128 = INITIAL_PANGORO_TO_PANGOLIN_CONVERSION_RATE;
}

#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub enum PangolinToPangoroParameter {
/// The conversion formula we use is: `PangoroTokens = PangolinTokens *
/// conversion_rate`.
PangoroToPangolinConversionRate(FixedU128),
}
impl Parameter for PangolinToPangoroParameter {
fn save(&self) {
match *self {
PangolinToPangoroParameter::PangoroToPangolinConversionRate(ref conversion_rate) =>
PangoroToPangolinConversionRate::set(conversion_rate),
}
}
}

pub type ToPangoroMaximalOutboundPayloadSize =
bridge_runtime_common::messages::source::FromThisChainMaximalOutboundPayloadSize<
WithPangoroMessageBridge,
>;

/// Pangoro <-> Pangolin message bridge.
#[derive(Clone, Copy, RuntimeDebug)]
pub struct WithPangoroMessageBridge;
impl MessageBridge for WithPangoroMessageBridge {
type BridgedChain = Pangoro;
type ThisChain = Pangolin;

const BRIDGED_CHAIN_ID: bp_runtime::ChainId = PANGORO_CHAIN_ID;
const BRIDGED_MESSAGES_PALLET_NAME: &'static str =
bridge_runtime_common::pallets::WITH_PANGOLIN_MESSAGES_PALLET_NAME;
const RELAYER_FEE_PERCENT: u32 = 10;
const THIS_CHAIN_ID: bp_runtime::ChainId = PANGOLIN_CHAIN_ID;
}

#[derive(Clone, Copy, RuntimeDebug)]
pub struct Pangolin;
impl ChainWithMessages for Pangolin {
type AccountId = bp_pangolin::AccountId;
type Balance = bp_pangolin::Balance;
type Hash = bp_pangolin::Hash;
type Signature = bp_pangolin::Signature;
type Signer = bp_pangolin::AccountPublic;
}
impl ThisChainWithMessages for Pangolin {
type RuntimeCall = RuntimeCall;
type RuntimeOrigin = RuntimeOrigin;

fn is_message_accepted(_send_origin: &Self::RuntimeOrigin, lane: &LaneId) -> bool {
*lane == PANGORO_PANGOLIN_LANE
}

fn maximal_pending_messages_at_outbound_lane() -> MessageNonce {
MessageNonce::MAX
}
}

#[derive(Clone, Copy, RuntimeDebug)]
pub struct Pangoro;
impl ChainWithMessages for Pangoro {
type AccountId = bp_pangoro::AccountId;
type Balance = bp_pangoro::Balance;
type Hash = bp_pangoro::Hash;
type Signature = bp_pangoro::Signature;
type Signer = bp_pangoro::AccountPublic;
}
impl BridgedChainWithMessages for Pangoro {
fn maximal_extrinsic_size() -> u32 {
bp_pangoro::DarwiniaLike::max_extrinsic_size()
}

fn verify_dispatch_weight(_message_payload: &[u8], payload_weight: &Weight) -> bool {
let upper_limit = target::maximal_incoming_message_dispatch_weight(
bp_pangoro::DarwiniaLike::max_extrinsic_weight(),
);
payload_weight.all_lte(upper_limit)
}
}
impl TargetHeaderChain<ToPangoroMessagePayload, <Self as ChainWithMessages>::AccountId>
for Pangoro
{
type Error = &'static str;
type MessagesDeliveryProof = ToPangoroMessagesDeliveryProof;

fn verify_message(payload: &ToPangoroMessagePayload) -> Result<(), Self::Error> {
source::verify_chain_message::<WithPangoroMessageBridge>(payload)
}

fn verify_messages_delivery_proof(
proof: Self::MessagesDeliveryProof,
) -> Result<(LaneId, InboundLaneData<bp_pangoro::AccountId>), Self::Error> {
source::verify_messages_delivery_proof_from_parachain::<
WithPangoroMessageBridge,
bp_pangoro::Header,
Runtime,
WithMoonbaseParachainsInstance,
>(ParaId(2105), proof)
}
}
impl SourceHeaderChain<<Self as ChainWithMessages>::Balance> for Pangoro {
type Error = &'static str;
type MessagesProof = FromPangoroMessagesProof;

fn verify_messages_proof(
proof: Self::MessagesProof,
messages_count: u32,
) -> Result<ProvedMessages<Message<<Self as ChainWithMessages>::Balance>>, Self::Error> {
target::verify_messages_proof_from_parachain::<
WithPangoroMessageBridge,
bp_pangoro::Header,
Runtime,
WithMoonbaseParachainsInstance,
>(ParaId(2105), proof, messages_count)
}
}
21 changes: 21 additions & 0 deletions runtime/pangolin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
mod pallets;
pub use pallets::*;

mod bridges_message;
pub use bridges_message::*;

mod migration;
mod weights;

Expand All @@ -52,6 +55,7 @@ pub type SignedExtra = (
frame_system::CheckNonce<Runtime>,
frame_system::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
BridgeRejectObsoleteHeadersAndMessages,
);

/// Unchecked extrinsic type as expected by this runtime.
Expand Down Expand Up @@ -154,6 +158,13 @@ frame_support::construct_runtime! {
Ethereum: pallet_ethereum = 36,
Evm: pallet_evm = 37,
MessageTransact: darwinia_message_transact = 38,

// Pangolin <> Pangoro
BridgeMoonbaseGrandpa: pallet_bridge_grandpa::<Instance1> = 39,
BridgeMoonbaseParachain: pallet_bridge_parachains::<Instance1> = 40,
BridgePangoroMessages: pallet_bridge_messages::<Instance1> = 41,
BridgePangoroDispatch: pallet_bridge_dispatch::<Instance1> = 42,
PangoroFeeMarket: pallet_fee_market::<Instance1> = 43
}
}

Expand All @@ -168,6 +179,16 @@ frame_benchmarking::define_benchmarks! {

impl_self_contained_call!();

bridge_runtime_common::generate_bridge_reject_obsolete_headers_and_messages! {
RuntimeCall, AccountId,
// Grandpa
BridgeMoonbaseGrandpa,
// Messages
BridgePangoroMessages,
// Parachain
BridgeMoonbaseParachain
}

sp_api::impl_runtime_apis! {
impl sp_consensus_aura::AuraApi<Block, sp_consensus_aura::sr25519::AuthorityId> for Runtime {
fn slot_duration() -> sp_consensus_aura::SlotDuration {
Expand Down
Loading

0 comments on commit 8f2c12a

Please sign in to comment.