Skip to content

Commit

Permalink
Add eth outbound queue and system pallets (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpolaczyk authored Nov 26, 2024
1 parent b7cb12f commit b866a6e
Show file tree
Hide file tree
Showing 29 changed files with 3,611 additions and 1,040 deletions.
734 changes: 417 additions & 317 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ flashbox-runtime = { path = "runtime/flashbox", default-features = false }

dancelight-runtime = { path = "solo-chains/runtime/dancelight", default-features = false }
dancelight-runtime-constants = { path = "solo-chains/runtime/dancelight/constants", default-features = false }
ethabi = { package = "ethabi-decode", version = "1.0.0", default-features = false }
manual-xcm-rpc = { path = "client/manual-xcm" }
node-common = { path = "client/node-common" }
services-payment-rpc = { path = "client/services-payment" }
Expand All @@ -96,9 +97,8 @@ tanssi-runtime-common = { path = "runtime/common", default-features = false }
tc-consensus = { path = "client/consensus" }
tc-orchestrator-chain-rpc-interface = { path = "client/orchestrator-chain-rpc-interface" }
tc-service-container-chain = { path = "client/service-container-chain" }

tp-author-noting-inherent = { path = "primitives/author-noting-inherent", default-features = false }
tp-fungibles-ext = { path = "primitives/fungibles-ext", default-features = false }
tp-bridge = { path = "primitives/bridge", default-features = false }
tp-maths = { path = "primitives/maths", default-features = false }
tp-traits = { path = "primitives/traits", default-features = false }
tp-xcm-core-buyer = { path = "primitives/xcm-core-buyer", default-features = false }
Expand Down Expand Up @@ -261,7 +261,10 @@ xcm-runtime-apis = { git = "https://github.com/moondance-labs/polkadot-sdk", bra
# Bridges (wasm)
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-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 }

# Polkadot (client)
polkadot-cli = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-stable2409" }
Expand Down
7 changes: 7 additions & 0 deletions pallets/external-validator-slashes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ pallet-session = { workspace = true }
pallet-staking = { workspace = true }
parity-scale-codec = { workspace = true, features = [ "derive", "max-encoded-len" ] }
scale-info = { workspace = true }
snowbridge-core = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-staking = { workspace = true }
sp-std = { workspace = true }
tp-bridge = { workspace = true }
tp-traits = { workspace = true }

[dev-dependencies]
Expand All @@ -41,20 +44,24 @@ std = [
"pallet-staking/std",
"parity-scale-codec/std",
"scale-info/std",
"snowbridge-core/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-staking/std",
"sp-std/std",
"tp-bridge/std",
"tp-traits/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
"snowbridge-core/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"sp-staking/runtime-benchmarks",
"tp-bridge/runtime-benchmarks",
"tp-traits/runtime-benchmarks",
]

Expand Down
13 changes: 13 additions & 0 deletions pallets/external-validator-slashes/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ mod benchmarks {
Ok(())
}

#[benchmark]
fn root_test_send_msg_to_eth() -> Result<(), BenchmarkError> {
let nonce = Default::default();
// Max limits depend on runtime, these are for Dancelight
let num_msgs = 32;
let msg_size = 900;

#[extrinsic_call]
_(RawOrigin::Root, nonce, num_msgs, msg_size);

Ok(())
}

impl_benchmark_test_suite!(
ExternalValidatorSlashes,
crate::mock::new_test_ext(),
Expand Down
66 changes: 66 additions & 0 deletions pallets/external-validator-slashes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use {
pallet_staking::SessionInterface,
parity_scale_codec::FullCodec,
parity_scale_codec::{Decode, Encode},
sp_core::H256,
sp_runtime::traits::{Convert, Debug, One, Saturating, Zero},
sp_runtime::DispatchResult,
sp_runtime::Perbill,
Expand All @@ -47,6 +48,9 @@ use {
tp_traits::{EraIndexProvider, InvulnerablesProvider, OnEraStart},
};

use snowbridge_core::ChannelId;
use tp_bridge::{Command, Message, ValidateMessage};

pub use pallet::*;

#[cfg(test)]
Expand All @@ -63,6 +67,7 @@ pub mod weights;
pub mod pallet {
use super::*;
pub use crate::weights::WeightInfo;
use tp_bridge::DeliverMessage;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
Expand Down Expand Up @@ -123,6 +128,14 @@ pub mod pallet {
/// Invulnerable provider, used to get the invulnerables to know when not to slash
type InvulnerablesProvider: InvulnerablesProvider<Self::ValidatorId>;

/// Validate a message that will be sent to Ethereum.
type ValidateMessage: ValidateMessage;

/// Send a message to Ethereum. Needs to be validated first.
type OutboundQueue: DeliverMessage<
Ticket = <<Self as pallet::Config>::ValidateMessage as ValidateMessage>::Ticket,
>;

/// The weight information of this pallet.
type WeightInfo: WeightInfo;
}
Expand All @@ -143,6 +156,10 @@ pub mod pallet {
DeferPeriodIsOver,
/// There was an error computing the slash
ErrorComputingSlash,
/// Failed to validate the message that was going to be sent to Ethereum
EthereumValidateFail,
/// Failed to deliver the message to Ethereum
EthereumDeliverFail,
}

#[pallet::pallet]
Expand Down Expand Up @@ -263,6 +280,55 @@ pub mod pallet {
NextSlashId::<T>::put(next_slash_id.saturating_add(One::one()));
Ok(())
}

#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::root_test_send_msg_to_eth())]
pub fn root_test_send_msg_to_eth(
origin: OriginFor<T>,
nonce: H256,
num_msgs: u32,
msg_size: u32,
) -> DispatchResult {
ensure_root(origin)?;

for i in 0..num_msgs {
// Make sure each message has a different payload
let mut payload = sp_core::blake2_256((nonce, i).encode().as_ref()).to_vec();
// Extend with zeros until msg_size is reached
payload.resize(msg_size as usize, 0);
// Example command, this should be something like "ReportSlashes"
let command = Command::Test(payload);

// Validate
let channel_id: ChannelId = snowbridge_core::PRIMARY_GOVERNANCE_CHANNEL;

let outbound_message = Message {
id: None,
channel_id,
command,
};

// validate the message
// Ignore fee because for now only root can send messages
let (ticket, _fee) =
T::ValidateMessage::validate(&outbound_message).map_err(|err| {
log::error!(
"root_test_send_msg_to_eth: validation of message {i} failed. {err:?}"
);
crate::pallet::Error::<T>::EthereumValidateFail
})?;

// Deliver
T::OutboundQueue::deliver(ticket).map_err(|err| {
log::error!(
"root_test_send_msg_to_eth: delivery of message {i} failed. {err:?}"
);
crate::pallet::Error::<T>::EthereumDeliverFail
})?;
}

Ok(())
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions pallets/external-validator-slashes/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use {
traits::{ConstU16, ConstU64, Get},
},
frame_system as system,
snowbridge_core::outbound::{SendError, SendMessageFeeProvider},
sp_core::H256,
sp_runtime::{
testing::UintAuthorityId,
Expand Down Expand Up @@ -196,6 +197,23 @@ impl DeferPeriodGetter {
}
}

pub struct MockOkOutboundQueue;
impl tp_bridge::DeliverMessage for MockOkOutboundQueue {
type Ticket = ();

fn deliver(_: Self::Ticket) -> Result<H256, SendError> {
Ok(H256::zero())
}
}

impl SendMessageFeeProvider for MockOkOutboundQueue {
type Balance = u128;

fn local_fee() -> Self::Balance {
1
}
}

parameter_types! {
pub const BondingDuration: u32 = 5u32;
}
Expand All @@ -210,6 +228,8 @@ impl external_validator_slashes::Config for Test {
type SessionInterface = ();
type EraIndexProvider = MockEraIndexProvider;
type InvulnerablesProvider = MockInvulnerableProvider;
type ValidateMessage = ();
type OutboundQueue = MockOkOutboundQueue;
type WeightInfo = ();
}

Expand Down
21 changes: 21 additions & 0 deletions pallets/external-validator-slashes/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use sp_std::marker::PhantomData;
pub trait WeightInfo {
fn cancel_deferred_slash(s: u32, ) -> Weight;
fn force_inject_slash() -> Weight;
fn root_test_send_msg_to_eth() -> Weight;
}

/// Weights for pallet_external_validator_slashes using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -91,6 +92,16 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}

fn root_test_send_msg_to_eth() -> Weight {
// Proof Size summary in bytes:
// Measured: `322`
// Estimated: `3601`
// Minimum execution time: 994_654_000 picoseconds.
Weight::from_parts(1_015_195_000, 3601)
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -126,4 +137,14 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}

fn root_test_send_msg_to_eth() -> Weight {
// Proof Size summary in bytes:
// Measured: `322`
// Estimated: `3601`
// Minimum execution time: 994_654_000 picoseconds.
Weight::from_parts(1_015_195_000, 3601)
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(5_u64))
}
}
59 changes: 59 additions & 0 deletions primitives/bridge/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[package]
name = "tp-bridge"
authors = { workspace = true }
description = "Tanssi bridge primitive"
edition = "2021"
license = "GPL-3.0-only"
version = "0.1.0"

[lints]
workspace = true

[dependencies]
dp-chain-state-snapshot = { workspace = true }
dp-container-chain-genesis-data = { workspace = true }
ethabi = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
impl-trait-for-tuples = { workspace = true }
macro_rules_attribute = { 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 }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

# Cumulus
cumulus-primitives-core = { workspace = true }

[features]
default = [ "std" ]
std = [
"cumulus-primitives-core/std",
"dp-chain-state-snapshot/std",
"dp-container-chain-genesis-data/std",
"ethabi/std",
"frame-support/std",
"frame-system/std",
"parity-scale-codec/std",
"scale-info/std",
"serde/std",
"snowbridge-beacon-primitives/std",
"snowbridge-core/std",
"snowbridge-pallet-outbound-queue/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"cumulus-primitives-core/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"snowbridge-core/runtime-benchmarks",
"snowbridge-pallet-outbound-queue/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
Loading

0 comments on commit b866a6e

Please sign in to comment.