Skip to content

Commit

Permalink
Add inbound queue (#750)
Browse files Browse the repository at this point in the history
* add inbound queue
* add symbiotic_message_processor
* add doc comments
  • Loading branch information
ParthDesai authored Nov 27, 2024
1 parent bb1193d commit 62053be
Show file tree
Hide file tree
Showing 22 changed files with 1,915 additions and 529 deletions.
403 changes: 380 additions & 23 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,17 @@ xcm-runtime-apis = { git = "https://github.com/moondance-labs/polkadot-sdk", bra


# Bridges (wasm)
alloy-sol-types = { version = "0.4.2", default-features = false }
milagro-bls = { package = "snowbridge-milagro-bls", version = "1.5.4", default-features = false }
snowbridge-beacon-primitives = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
snowbridge-core = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
snowbridge-pallet-ethereum-client = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
snowbridge-pallet-inbound-queue = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
snowbridge-pallet-inbound-queue-fixtures = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
snowbridge-pallet-outbound-queue = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
snowbridge-pallet-system = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }
snowbridge-router-primitives = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409", default-features = false }


# Polkadot (client)
polkadot-cli = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409" }
Expand Down
6 changes: 6 additions & 0 deletions primitives/bridge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ frame-support = { workspace = true }
frame-system = { workspace = true }
impl-trait-for-tuples = { workspace = true }
macro_rules_attribute = { workspace = true }
pallet-external-validators = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
serde = { workspace = true }
snowbridge-beacon-primitives = { workspace = true }
snowbridge-core = { workspace = true }
snowbridge-pallet-outbound-queue = { workspace = true }
snowbridge-router-primitives = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
Expand All @@ -39,12 +41,14 @@ std = [
"ethabi/std",
"frame-support/std",
"frame-system/std",
"pallet-external-validators/std",
"parity-scale-codec/std",
"scale-info/std",
"serde/std",
"snowbridge-beacon-primitives/std",
"snowbridge-core/std",
"snowbridge-pallet-outbound-queue/std",
"snowbridge-router-primitives/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
Expand All @@ -53,7 +57,9 @@ runtime-benchmarks = [
"cumulus-primitives-core/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-external-validators/runtime-benchmarks",
"snowbridge-core/runtime-benchmarks",
"snowbridge-pallet-outbound-queue/runtime-benchmarks",
"snowbridge-router-primitives/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
42 changes: 42 additions & 0 deletions primitives/bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@

#![cfg_attr(not(feature = "std"), no_std)]

pub mod symbiotic_message_processor;

use {
core::marker::PhantomData,
cumulus_primitives_core::{
relay_chain::{AccountId, Balance},
Assets, Location, SendResult, SendXcm, Xcm, XcmHash,
},
ethabi::Token,
frame_support::{
ensure,
Expand All @@ -34,6 +40,10 @@ use {
ChannelId,
},
snowbridge_pallet_outbound_queue::send_message_impl::Ticket,
snowbridge_router_primitives::inbound::{
ConvertMessage, ConvertMessageError, VersionedXcmMessage,
},
sp_core::hashing,
sp_core::H256,
sp_runtime::{app_crypto::sp_core, traits::Convert, RuntimeDebug},
sp_std::vec::Vec,
Expand Down Expand Up @@ -210,3 +220,35 @@ pub trait DeliverMessage {

fn deliver(ticket: Self::Ticket) -> Result<H256, SendError>;
}

/// Dummy router for xcm messages coming from ethereum
pub struct DoNothingRouter;
impl SendXcm for DoNothingRouter {
type Ticket = Xcm<()>;

fn validate(
_dest: &mut Option<Location>,
xcm: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
Ok((xcm.clone().unwrap(), Assets::new()))
}
fn deliver(xcm: Xcm<()>) -> Result<XcmHash, cumulus_primitives_core::SendError> {
let hash = xcm.using_encoded(hashing::blake2_256);
Ok(hash)
}
}

/// Dummy message converter to convert message to Xcm
pub struct DoNothingConvertMessage;

impl ConvertMessage for DoNothingConvertMessage {
type Balance = Balance;
type AccountId = AccountId;

fn convert(
_: H256,
_message: VersionedXcmMessage,
) -> Result<(Xcm<()>, Self::Balance), ConvertMessageError> {
Err(ConvertMessageError::UnsupportedVersion)
}
}
92 changes: 92 additions & 0 deletions primitives/bridge/src/symbiotic_message_processor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (C) Moondance Labs Ltd.
// This file is part of Tanssi.

// Tanssi 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.

// Tanssi 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 Tanssi. If not, see <http://www.gnu.org/licenses/>

use frame_support::pallet_prelude::*;
use parity_scale_codec::DecodeAll;
use snowbridge_core::Channel;
use snowbridge_router_primitives::inbound::envelope::Envelope;
use snowbridge_router_primitives::inbound::MessageProcessor;
use sp_runtime::DispatchError;
use sp_std::vec::Vec;

/// Magic bytes are added in every payload intended for this processor to make sure
/// that we are the intended recipient of the message. Reason being scale encoding is not type aware.
/// So a same set of bytes can be decoded for two different data structures if their
/// total size is same. Magic bytes can be checked after decoding to make sure that the sender
/// indeed send a message intended for this processor.
pub const MAGIC_BYTES: [u8; 4] = [112, 21, 0, 56];

/// Payload is the whole data we expect to receive from the relayer
#[derive(Encode, Decode)]
pub struct Payload<T>
where
T: pallet_external_validators::Config,
{
pub magic_bytes: [u8; 4],
pub message: Message<T>,
}

/// Actual message inside the payload
#[derive(Encode, Decode)]
pub enum Message<T>
where
T: pallet_external_validators::Config,
{
V1(InboundCommand<T>),
}

/// Command to be executed by this message processor
#[derive(Encode, Decode)]
pub enum InboundCommand<T>
where
T: pallet_external_validators::Config,
{
ReceiveValidators {
validators: Vec<<T as pallet_external_validators::Config>::ValidatorId>,
},
}

pub struct SymbioticMessageProcessor<T>(PhantomData<T>);

impl<T> MessageProcessor for SymbioticMessageProcessor<T>
where
T: pallet_external_validators::Config,
{
fn can_process_message(_channel: &Channel, envelope: &Envelope) -> bool {
let decode_result = Payload::<T>::decode_all(&mut envelope.payload.as_slice());
if let Ok(payload) = decode_result {
payload.magic_bytes == MAGIC_BYTES
} else {
false
}
}

fn process_message(_channel: Channel, envelope: Envelope) -> Result<(), DispatchError> {
let decode_result = Payload::<T>::decode_all(&mut envelope.payload.as_slice());
let message = if let Ok(payload) = decode_result {
payload.message
} else {
return Err(DispatchError::Other("unable to parse the envelope payload"));
};

match message {
Message::V1(InboundCommand::ReceiveValidators { validators }) => {
pallet_external_validators::Pallet::<T>::set_external_validators(validators)?;
Ok(())
}
}
}
}
12 changes: 12 additions & 0 deletions solo-chains/runtime/dancelight/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,15 @@ pallet-migrations = { workspace = true }
snowbridge-beacon-primitives = { workspace = true }
snowbridge-core = { workspace = true }
snowbridge-pallet-ethereum-client = { workspace = true }
snowbridge-pallet-inbound-queue = { workspace = true }
snowbridge-pallet-inbound-queue-fixtures = { workspace = true, optional = true }
snowbridge-pallet-outbound-queue = { workspace = true }
snowbridge-pallet-system = { workspace = true }
snowbridge-router-primitives = { workspace = true }
tp-bridge = { workspace = true }

[dev-dependencies]
alloy-sol-types = { workspace = true, default-features = true }
finality-grandpa = { workspace = true, default-features = true, features = [ "derive-codec" ] }
keyring = { workspace = true }
milagro-bls = { workspace = true, features = [ "std" ] }
Expand Down Expand Up @@ -267,8 +271,11 @@ std = [
"snowbridge-core/std",
"snowbridge-pallet-ethereum-client/fuzzing",
"snowbridge-pallet-ethereum-client/std",
"snowbridge-pallet-inbound-queue-fixtures/std",
"snowbridge-pallet-inbound-queue/std",
"snowbridge-pallet-outbound-queue/std",
"snowbridge-pallet-system/std",
"snowbridge-router-primitives/std",
"sp-api/std",
"sp-application-crypto/std",
"sp-arithmetic/std",
Expand Down Expand Up @@ -360,8 +367,12 @@ runtime-benchmarks = [
"runtime-parachains/runtime-benchmarks",
"snowbridge-core/runtime-benchmarks",
"snowbridge-pallet-ethereum-client/runtime-benchmarks",
"snowbridge-pallet-inbound-queue-fixtures",
"snowbridge-pallet-inbound-queue-fixtures/runtime-benchmarks",
"snowbridge-pallet-inbound-queue/runtime-benchmarks",
"snowbridge-pallet-outbound-queue/runtime-benchmarks",
"snowbridge-pallet-system/runtime-benchmarks",
"snowbridge-router-primitives/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"sp-staking/runtime-benchmarks",
"tanssi-runtime-common/runtime-benchmarks",
Expand Down Expand Up @@ -433,6 +444,7 @@ try-runtime = [
"runtime-common/try-runtime",
"runtime-parachains/try-runtime",
"snowbridge-pallet-ethereum-client/try-runtime",
"snowbridge-pallet-inbound-queue/try-runtime",
"snowbridge-pallet-outbound-queue/try-runtime",
"snowbridge-pallet-system/try-runtime",
"sp-runtime/try-runtime",
Expand Down
Loading

0 comments on commit 62053be

Please sign in to comment.