-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Setup permissionless lanes for AssetHubs - heads/state-roots sync #8326
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
Open
bkontur
wants to merge
18
commits into
bko-permlanes-on-ahs-2-on-new-head
Choose a base branch
from
bko-permlanes-on-ahs-3-heads-sync
base: bko-permlanes-on-ahs-2-on-new-head
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 b200fbf
Add proof root sync to the AssetHubs with test
bkontur a1e70b6
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur 17bb809
Update from github-actions[bot] running command 'fmt'
github-actions[bot] 87c4001
Merge remote-tracking branch 'origin/bko-permlanes-on-ahs-2-on-new-he…
bkontur 2e44551
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur 9e9d52a
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur 9d8432d
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur 0beafe3
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur a246572
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur cf24b3d
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur d75af27
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur 6305680
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur e09f6f0
Merge remote-tracking branch 'origin/bko-permlanes-on-ahs-2-on-new-he…
bkontur 54f5513
Compilation nits after rebase
bkontur f401bc9
cargo.lock
bkontur 8d6f747
PR comments (dedup + simplify)
bkontur 3fcad57
Merge branch 'bko-permlanes-on-ahs-2-on-new-head' into bko-permlanes-…
bkontur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.