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

beefy: put not only lease parachain heads into mmr #4751

Merged
merged 23 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
19 changes: 19 additions & 0 deletions polkadot/runtime/parachains/src/paras/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,15 @@ const INVALID_TX_BAD_VALIDATOR_IDX: u8 = 1;
const INVALID_TX_BAD_SUBJECT: u8 = 2;
const INVALID_TX_DOUBLE_VOTE: u8 = 3;

/// This is intermediate "fix" for this issue:
/// https://github.com/orgs/paritytech/projects/119/views/20?pane=issue&itemId=66568838
///
/// It does not actually fix it, but makes the worst case better. Without that limit someone
/// could completely DoS the relay chain by registering a ridiculously high amount of paras.
/// With this limit the same attack could lead to some parachains ceasing to being able to
eskimor marked this conversation as resolved.
Show resolved Hide resolved
/// communicate via snowbridge.
pub const MAX_PARA_HEADS: usize = 1024;

impl<T: Config> Pallet<T> {
/// This is a call to schedule code upgrades for parachains which is safe to be called
/// outside of this module. That means this function does all checks necessary to ensure
Expand Down Expand Up @@ -1290,6 +1299,16 @@ impl<T: Config> Pallet<T> {
})
}

/// Get a list of the first [`MAX_PARA_HEADS`] para heads sorted by para_id.
/// This method is likely to be removed in the future.
pub fn sorted_para_heads() -> Vec<(u32, Vec<u8>)> {
let mut heads: Vec<(u32, Vec<u8>)> =
Heads::<T>::iter().map(|(id, head)| (id.into(), head.0)).collect();
heads.sort_by_key(|(id, _)| *id);
heads.truncate(MAX_PARA_HEADS);
Copy link
Contributor

Choose a reason for hiding this comment

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

Works for Snowbridge, since BridgeHub with ID 1002 will be sorted before all the other non-system parachains 👍

Copy link
Member

Choose a reason for hiding this comment

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

Ideally we would truncate before sorting. Although it sounds unlikely that someone registers so many parachains that sorting becomes a real problem. Nice thing about sorting first is that system chains will be preferred.

heads
}

// Apply all para actions queued for the given session index.
//
// The actions to take are based on the lifecycle of of the paras.
Expand Down
9 changes: 2 additions & 7 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1301,13 +1301,8 @@ parameter_types! {
pub struct ParaHeadsRootProvider;
impl BeefyDataProvider<H256> for ParaHeadsRootProvider {
fn extra_data() -> H256 {
let mut para_heads: Vec<(u32, Vec<u8>)> = parachains_paras::Parachains::<Runtime>::get()
.into_iter()
.filter_map(|id| {
parachains_paras::Heads::<Runtime>::get(&id).map(|head| (id.into(), head.0))
})
.collect();
para_heads.sort();
let para_heads: Vec<(u32, Vec<u8>)> =
parachains_paras::Pallet::<Runtime>::sorted_para_heads();
binary_merkle_tree::merkle_root::<mmr::Hashing, _>(
para_heads.into_iter().map(|pair| pair.encode()),
)
Expand Down
9 changes: 2 additions & 7 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,8 @@ parameter_types! {
pub struct ParaHeadsRootProvider;
impl BeefyDataProvider<H256> for ParaHeadsRootProvider {
fn extra_data() -> H256 {
let mut para_heads: Vec<(u32, Vec<u8>)> = parachains_paras::Parachains::<Runtime>::get()
.into_iter()
.filter_map(|id| {
parachains_paras::Heads::<Runtime>::get(&id).map(|head| (id.into(), head.0))
})
.collect();
para_heads.sort_by_key(|k| k.0);
let para_heads: Vec<(u32, Vec<u8>)> =
parachains_paras::Pallet::<Runtime>::sorted_para_heads();
binary_merkle_tree::merkle_root::<mmr::Hashing, _>(
para_heads.into_iter().map(|pair| pair.encode()),
)
Expand Down
12 changes: 12 additions & 0 deletions prdoc/pr_4751.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
title: "Use all parachain heads for BEEFY MMR extra data"

doc:
- audience: Runtime Dev
description: |
Previously, the extra data in an MMR leaf nodes was only computed based on lease-based parachain heads.
This PR extends the extra data to include others, including on-demand parachain heads.
Currently, the number of heads is limited to the first 1024 heads sorted by para id.

crates:
- name: polkadot-runtime-parachains
bump: minor
Loading