Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inbound queue #750

Merged
merged 10 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
ParthDesai marked this conversation as resolved.
Show resolved Hide resolved
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
103 changes: 95 additions & 8 deletions solo-chains/runtime/dancelight/src/bridge_to_ethereum_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,33 @@
//! The bridge to ethereum config

pub const SLOTS_PER_EPOCH: u32 = snowbridge_pallet_ethereum_client::config::SLOTS_PER_EPOCH as u32;

#[cfg(not(test))]
use crate::EthereumBeaconClient;
use frame_support::weights::ConstantMultiplier;

use sp_core::H160;
use sp_core::{ConstU32, ConstU8};
#[cfg(not(feature = "runtime-benchmarks"))]
use tp_bridge::symbiotic_message_processor::SymbioticMessageProcessor;
use {
crate::{
parameter_types, weights, xcm_config::UniversalLocation, AggregateMessageOrigin, Balance,
Balances, EthereumOutboundQueue, EthereumSystem, FixedU128, GetAggregateMessageOrigin,
Keccak256, MessageQueue, Runtime, RuntimeEvent, TreasuryAccount, WeightToFee, UNITS,
parameter_types, weights, xcm_config, xcm_config::UniversalLocation,
AggregateMessageOrigin, Balance, Balances, EthereumInboundQueue, EthereumOutboundQueue,
EthereumSystem, FixedU128, GetAggregateMessageOrigin, Keccak256, MessageQueue, Runtime,
RuntimeEvent, TransactionByteFee, TreasuryAccount, WeightToFee, UNITS,
},
dancelight_runtime_constants::snowbridge::EthereumLocation,
pallet_xcm::EnsureXcm,
snowbridge_beacon_primitives::{Fork, ForkVersions},
snowbridge_core::{gwei, meth, AllowSiblingsOnly, PricingParameters, Rewards},
sp_core::{ConstU32, ConstU8},
tp_bridge::{DoNothingConvertMessage, DoNothingRouter},
};

// Ethereum Bridge
parameter_types! {
pub storage EthereumGatewayAddress: H160 = H160(hex_literal::hex!("EDa338E4dC46038493b885327842fD3E301CaB39"));
ParthDesai marked this conversation as resolved.
Show resolved Hide resolved
}

parameter_types! {
pub Parameters: PricingParameters<u128> = PricingParameters {
exchange_rate: FixedU128::from_rational(1, 400),
Expand Down Expand Up @@ -140,16 +153,23 @@ impl snowbridge_pallet_system::Config for Runtime {
#[cfg(feature = "runtime-benchmarks")]
type Helper = benchmark_helper::EthSystemBenchHelper;
type DefaultPricingParameters = Parameters;
type InboundDeliveryCost = ();
//type InboundDeliveryCost = EthereumInboundQueue;
type InboundDeliveryCost = EthereumInboundQueue;
girazoki marked this conversation as resolved.
Show resolved Hide resolved
type UniversalLocation = UniversalLocation;
type EthereumLocation = EthereumLocation;
type WeightInfo = crate::weights::snowbridge_pallet_system::SubstrateWeight<Runtime>;
}

#[cfg(feature = "runtime-benchmarks")]
mod benchmark_helper {
use {crate::RuntimeOrigin, xcm::latest::Location};
use snowbridge_beacon_primitives::BeaconHeader;
use snowbridge_core::Channel;
use snowbridge_pallet_system::Channels;
use snowbridge_router_primitives::inbound::envelope::Envelope;
use snowbridge_router_primitives::inbound::MessageProcessor;
use sp_core::H256;
use {
crate::EthereumBeaconClient, crate::Runtime, crate::RuntimeOrigin, xcm::latest::Location,
};

pub struct EthSystemBenchHelper;

Expand All @@ -158,4 +178,71 @@ mod benchmark_helper {
RuntimeOrigin::from(pallet_xcm::Origin::Xcm(location))
}
}

impl snowbridge_pallet_inbound_queue::BenchmarkHelper<Runtime> for EthSystemBenchHelper {
fn initialize_storage(beacon_header: BeaconHeader, block_roots_root: H256) {
let submit_message = snowbridge_pallet_inbound_queue_fixtures::register_token::make_register_token_message();
let envelope: Envelope = Envelope::try_from(&submit_message.message.event_log).unwrap();

Channels::<Runtime>::set(
envelope.channel_id,
Some(Channel {
agent_id: Default::default(),
para_id: Default::default(),
}),
);

EthereumBeaconClient::store_finalized_header(beacon_header, block_roots_root).unwrap();
}
}

pub struct DoNothingMessageProcessor;

impl MessageProcessor for DoNothingMessageProcessor {
fn can_process_message(_: &Channel, _: &Envelope) -> bool {
true
}

fn process_message(_: Channel, _: Envelope) -> Result<(), sp_runtime::DispatchError> {
Ok(())
}
}
}

#[cfg(test)]
mod test_helpers {
use snowbridge_core::inbound::{Log, Proof, VerificationError, Verifier};

pub struct MockVerifier;

impl Verifier for MockVerifier {
fn verify(_: &Log, _: &Proof) -> Result<(), VerificationError> {
Ok(())
}
}
}

impl snowbridge_pallet_inbound_queue::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
#[cfg(not(test))]
type Verifier = EthereumBeaconClient;
#[cfg(test)]
type Verifier = test_helpers::MockVerifier;
type Token = Balances;
type XcmSender = DoNothingRouter;
type GatewayAddress = EthereumGatewayAddress;
type MessageConverter = DoNothingConvertMessage;
ParthDesai marked this conversation as resolved.
Show resolved Hide resolved
type ChannelLookup = EthereumSystem;
type PricingParameters = EthereumSystem;
type WeightInfo = weights::snowbridge_pallet_inbound_queue::SubstrateWeight<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type Helper = benchmark_helper::EthSystemBenchHelper;
type WeightToFee = WeightToFee;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type MaxMessageSize = ConstU32<2048>;
ParthDesai marked this conversation as resolved.
Show resolved Hide resolved
type AssetTransactor = <xcm_config::XcmConfig as xcm_executor::Config>::AssetTransactor;
#[cfg(not(feature = "runtime-benchmarks"))]
type MessageProcessor = (SymbioticMessageProcessor<Runtime>,);
#[cfg(feature = "runtime-benchmarks")]
type MessageProcessor = (benchmark_helper::DoNothingMessageProcessor,);
}
Loading
Loading