This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 378
refactor unincluded segment length into a ConsensusHook #2501
Merged
rphmeier
merged 19 commits into
slumber-async-backing-feature
from
rh-unincluded-consensus-hooks
May 17, 2023
Merged
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8fdc8cf
refactor unincluded segment length into a ConsensusHook
rphmeier cc0ff1c
add docs
rphmeier 3ce29a6
refactor bandwidth_out calculation
rphmeier f7da3a9
test for limits from impl
slumber 8579612
fmt
slumber 83a25ed
make tests compile
slumber 0930d4e
update comment
rphmeier 5d5d165
uncomment test
rphmeier b67b0d6
fix collator test by adding parent to state proof
rphmeier c0c0edc
patch HRMP watermark rules for unincluded segment
rphmeier 9b4b2f7
get consensus-common tests to pass, using unincluded segment
rphmeier fdf948b
fix unincluded segment tests
rphmeier 79c5650
get all tests passing
rphmeier 8c363b5
fmt
rphmeier 352d556
rustdoc CI
rphmeier 1818426
aura-ext: limit the number of authored blocks per slot (#2551)
slumber b9431fe
remove stray println
rphmeier 4115b0c
fix test warning
rphmeier 54646ae
fix doc link
rphmeier 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
This file contains 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,108 @@ | ||
// Copyright 2023 Parity Technologies (UK) Ltd. | ||
// This file is part of Cumulus. | ||
|
||
// Cumulus 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. | ||
|
||
// Cumulus 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 Cumulus. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! The definition of a [`ConsensusHook`] trait for consensus logic to manage the backlog | ||
//! of parachain blocks ready to submit to the relay chain, as well as some basic implementations. | ||
|
||
use super::relay_state_snapshot::RelayChainStateProof; | ||
use sp_std::num::NonZeroU32; | ||
|
||
/// The possible capacity of the unincluded segment. | ||
#[derive(Clone)] | ||
pub struct UnincludedSegmentCapacity(UnincludedSegmentCapacityInner); | ||
|
||
impl UnincludedSegmentCapacity { | ||
pub(crate) fn get(&self) -> u32 { | ||
match self.0 { | ||
UnincludedSegmentCapacityInner::ExpectParentIncluded => 1, | ||
UnincludedSegmentCapacityInner::Value(v) => v.get(), | ||
} | ||
} | ||
|
||
pub(crate) fn is_expecting_included_parent(&self) -> bool { | ||
match self.0 { | ||
UnincludedSegmentCapacityInner::ExpectParentIncluded => true, | ||
UnincludedSegmentCapacityInner::Value(_) => false, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub(crate) enum UnincludedSegmentCapacityInner { | ||
ExpectParentIncluded, | ||
Value(NonZeroU32), | ||
} | ||
|
||
impl From<NonZeroU32> for UnincludedSegmentCapacity { | ||
fn from(value: NonZeroU32) -> Self { | ||
UnincludedSegmentCapacity(UnincludedSegmentCapacityInner::Value(value)) | ||
} | ||
} | ||
|
||
/// The consensus hook for dealing with the unincluded segment. | ||
/// | ||
/// Higher-level and user-configurable consensus logic is more informed about the | ||
/// desired unincluded segment length, as well as any rules for adapting it dynamically | ||
/// according to the relay-chain state. | ||
pub trait ConsensusHook { | ||
/// This hook is called partway through the `set_validation_data` inherent in parachain-system. | ||
/// | ||
/// The hook is allowed to panic if customized consensus rules aren't met and is required | ||
/// to return a maximum capacity for the unincluded segment. | ||
fn on_state_proof( | ||
state_proof: &RelayChainStateProof, | ||
) -> UnincludedSegmentCapacity; | ||
} | ||
|
||
/// A special consensus hook for handling the migration to asynchronous backing gracefully, | ||
/// even if collators haven't been updated to provide the last included parent in the state | ||
/// proof yet. | ||
/// | ||
/// This behaves as though the parent is included, even if the relay chain state proof doesn't contain | ||
/// the included para head. If the para head is present in the state proof, this does ensure the | ||
/// parent is included. | ||
pub struct ExpectParentIncluded; | ||
|
||
impl ConsensusHook for ExpectParentIncluded { | ||
fn on_state_proof( | ||
_state_proof: &RelayChainStateProof, | ||
) -> UnincludedSegmentCapacity { | ||
UnincludedSegmentCapacity(UnincludedSegmentCapacityInner::ExpectParentIncluded) | ||
} | ||
} | ||
|
||
/// A consensus hook for a fixed unincluded segment length. This hook does nothing but | ||
/// set the capacity of the unincluded segment to the constant N. | ||
/// | ||
/// Since it is illegal to provide an unincluded segment length of 0, this sets a minimum of | ||
/// 1. | ||
pub struct FixedCapacityUnincludedSegment<const N: u32>; | ||
|
||
impl<const N: u32> ConsensusHook for FixedCapacityUnincludedSegment<N> { | ||
fn on_state_proof( | ||
_state_proof: &RelayChainStateProof, | ||
) -> UnincludedSegmentCapacity { | ||
NonZeroU32::new(sp_std::cmp::max(N, 1)) | ||
.expect("1 is the minimum value and non-zero; qed") | ||
.into() | ||
} | ||
} | ||
|
||
/// A fixed-capacity unincluded segment hook, which requires that the parent block is | ||
/// included prior to the current block being authored. | ||
/// | ||
/// This is a simple type alias around a fixed-capacity unincluded segment with a size of 1. | ||
pub type RequireParentIncluded = FixedCapacityUnincludedSegment<1>; |
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.
Capacity expecting included parent seems confusing, or perhaps tangential?
Why isn't it
is_set
or something?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.
it's more of a "is demanding" than "is expecting", true.