Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
58f3931
Add proof root sync (`pallet-bridge-proof-root-sync`) as an ring buff…
bkontur Apr 23, 2025
b200fbf
Add proof root sync to the AssetHubs with test
bkontur Apr 24, 2025
a1e70b6
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur Apr 24, 2025
17bb809
Update from github-actions[bot] running command 'fmt'
github-actions[bot] Apr 24, 2025
87c4001
Merge remote-tracking branch 'origin/bko-permlanes-on-ahs-2-on-new-he…
bkontur Apr 25, 2025
2e44551
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur Apr 29, 2025
9e9d52a
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur Apr 29, 2025
9d8432d
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur May 28, 2025
0beafe3
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur May 28, 2025
a246572
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur May 28, 2025
cf24b3d
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur May 28, 2025
d75af27
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur Jul 10, 2025
6305680
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur Jul 16, 2025
e09f6f0
Merge remote-tracking branch 'origin/bko-permlanes-on-ahs-2-on-new-he…
bkontur Jul 18, 2025
54f5513
Compilation nits after rebase
bkontur Jul 18, 2025
f401bc9
cargo.lock
bkontur Jul 18, 2025
8d6f747
PR comments (dedup + simplify)
bkontur Jul 21, 2025
3fcad57
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur Jul 21, 2025
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
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ members = [
"bridges/modules/messages",
"bridges/modules/parachains",
"bridges/modules/proof-root-store",
"bridges/modules/proof-root-sync",
"bridges/modules/relayers",
"bridges/modules/xcm-bridge",
"bridges/modules/xcm-bridge-hub",
Expand Down Expand Up @@ -967,6 +968,7 @@ pallet-bridge-grandpa = { path = "bridges/modules/grandpa", default-features = f
pallet-bridge-messages = { path = "bridges/modules/messages", default-features = false }
pallet-bridge-parachains = { path = "bridges/modules/parachains", default-features = false }
pallet-bridge-proof-root-store = { path = "bridges/modules/proof-root-store", default-features = false }
pallet-bridge-proof-root-sync = { path = "bridges/modules/proof-root-sync", default-features = false }
pallet-bridge-relayers = { path = "bridges/modules/relayers", default-features = false }
pallet-broker = { path = "substrate/frame/broker", default-features = false }
pallet-child-bounties = { path = "substrate/frame/child-bounties", default-features = false }
Expand Down
44 changes: 44 additions & 0 deletions bridges/modules/proof-root-sync/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[package]
name = "pallet-bridge-proof-root-sync"
version = "0.1.0"
description = "Module that allows bridged relay chains to exchange information on their parachains' heads."
authors.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
repository.workspace = true

[lints]
workspace = true

[dependencies]
codec = { features = ["derive"], workspace = true }
frame-benchmarking = { optional = true, workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
impl-trait-for-tuples = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
tracing = { workspace = true }

[dev-dependencies]
sp-io = { workspace = true }

[features]
default = ["std"]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"tracing/std",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
]
60 changes: 60 additions & 0 deletions bridges/modules/proof-root-sync/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (C) 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 <http://www.gnu.org/licenses/>.

//! The pallet's benchmarks.

#![cfg(feature = "runtime-benchmarks")]

use crate::{Config, Pallet, RootsToSend};
use frame_benchmarking::v2::*;
use frame_support::{
traits::{Get, Hooks},
weights::Weight,
};

#[cfg(feature = "runtime-benchmarks")]
pub trait BenchmarkHelper<Key, Value> {
fn create_key_value_for(id: u32) -> (Key, Value);
}

#[instance_benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn on_idle() -> Result<(), BenchmarkError> {
// create data
for id in 0..T::MaxRootsToSend::get() {
let (key, value) = T::BenchmarkHelper::create_key_value_for(id);
Pallet::<T, I>::schedule_for_sync(key, value);
}
let number_to_send = RootsToSend::<T, I>::get().iter().count();

#[block]
{
Pallet::<T, I>::on_idle(0u32.into(), Weight::MAX);
}

// check drained
assert_eq!(
RootsToSend::<T, I>::get().iter().count(),
number_to_send.saturating_sub(T::MaxRootsToSend::get() as _)
);
Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime);
}
161 changes: 161 additions & 0 deletions bridges/modules/proof-root-sync/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A pallet for scheduling and syncing key-value pairs (roots) with arbitrary destinations.
//!
//! The pallet provides functionality to:
//! - Schedule roots for syncing using `schedule_for_sync`
//! - Automatically process scheduled roots during `on_idle` hooks
//!
//! The actual sending/syncing of roots is implemented by the `OnSend` trait, which can be
//! customized for specific use cases like cross-chain communication.
//!
//! Basically, this is a simple `on_idle` hook that can schedule data with a ring buffer and send
//! data.

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

extern crate alloc;

use alloc::{collections::VecDeque, vec::Vec};
use frame_support::pallet_prelude::Weight;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(feature = "runtime-benchmarks")]
pub use benchmarking::BenchmarkHelper;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub use weights::WeightInfo;
pub mod weights;

pub use pallet::*;

const LOG_TARGET: &str = "runtime::bridge-proof-root-sync";

/// A trait for sending/syncing roots, for example, to other chains.
pub trait OnSend<Key, Value> {
/// Process a list of roots (key-value pairs) for sending.
///
/// # Arguments
///
/// * `roots` - A vector of roots where each root is a tuple of (key, value). Roots are ordered
/// from the oldest (index 0) to the newest (last index).
fn on_send(roots: &Vec<(Key, Value)>);
}

#[impl_trait_for_tuples::impl_for_tuples(8)]
impl<Key, Value> OnSend<Key, Value> for Tuple {
fn on_send(roots: &Vec<(Key, Value)>) {
for_tuples!( #( Tuple::on_send(roots);) * );
}
}

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{pallet_prelude::*, sp_runtime::SaturatedConversion, weights::WeightMeter};
use frame_system::pallet_prelude::*;

#[pallet::pallet]
pub struct Pallet<T, I = ()>(_);

/// The pallet configuration trait.
#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config {
/// Benchmarks results from runtime we're plugged into.
type WeightInfo: WeightInfo;

/// The key type used to identify stored values of type `T::Value`.
type Key: Parameter;

/// The type of the root value.
type Value: Parameter;

/// Maximum number of roots to retain in `RootsToSend` storage.
/// This setting prevents unbounded growth of the on-chain state.
/// If we hit this number, we start removing the oldest data from `RootsToSend`.
#[pallet::constant]
type RootsToKeep: Get<u32>;

/// Maximum number of roots to drain and send with `T::OnSend`.
#[pallet::constant]
type MaxRootsToSend: Get<u32>;

/// Means for sending/syncing roots.
type OnSend: OnSend<Self::Key, Self::Value>;

/// Helper type for benchmarks.
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper: BenchmarkHelper<Self::Key, Self::Value>;
}

/// A ring-buffer storage of roots (key-value pairs) that need to be sent/synced to other
/// chains. When the buffer reaches its capacity limit defined by `T::RootsToKeep`, the oldest
/// elements are removed. The elements are drained and processed in order by `T::OnSend` during
/// `on_idle` up to `T::MaxRootsToSend` elements at a time.
#[pallet::storage]
#[pallet::unbounded]
pub type RootsToSend<T: Config<I>, I: 'static = ()> =
StorageValue<_, VecDeque<(T::Key, T::Value)>, ValueQuery>;

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
fn on_idle(_n: BlockNumberFor<T>, limit: Weight) -> Weight {
let mut meter = WeightMeter::with_limit(limit);
if meter.try_consume(T::WeightInfo::on_idle()).is_err() {
tracing::debug!(
target: LOG_TARGET,
?limit,
on_idle_weight = ?T::WeightInfo::on_idle(),
"Not enough weight for on_idle.",
);
return meter.consumed();
}

// Send roots.
RootsToSend::<T, I>::mutate(|roots| {
let range_for_send =
0..core::cmp::min(T::MaxRootsToSend::get().saturated_into(), roots.len());
T::OnSend::on_send(&roots.drain(range_for_send).collect::<Vec<_>>())
});

meter.consumed()
}
}

impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Schedule new data to be synced by `T::OnSend` means.
///
/// The roots are stored in a ring buffer with limited capacity as defined by
/// `T::RootsToKeep`. When the buffer reaches its capacity limit, the oldest elements are
/// removed. The elements will be drained and processed in order by `T::OnSend` during
/// `on_idle` up to `T::MaxRootsToSend` elements at a time.
pub fn schedule_for_sync(key: T::Key, value: T::Value) {
RootsToSend::<T, I>::mutate(|roots| {
// Add to schedules.
roots.push_back((key, value));

// Remove from the front up to the `T::RootsToKeep` limit.
let max = T::RootsToKeep::get();
while roots.len() > (max as usize) {
let _ = roots.pop_front();
}
Comment on lines +153 to +157
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw something similar done also in #8324 in do_note_new_roots. Maybe we can deduplicate this.

});
}
}
}
Loading
Loading