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 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement consensus hook for aura-ext
- Loading branch information
Showing
2 changed files
with
106 additions
and
3 deletions.
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,63 @@ | ||
// 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 | ||
//! block velocity. | ||
//! | ||
//! The velocity `V` refers to the rate of block processing by the relay chain. | ||
use sp_std::num::NonZeroU32; | ||
|
||
/// Block processing velocity, essentially defining the limit of block production per | ||
/// aura slot. | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct BlockProcessingVelocity(NonZeroU32); | ||
|
||
impl BlockProcessingVelocity { | ||
pub(crate) fn get(&self) -> u32 { | ||
self.0.get() | ||
} | ||
} | ||
|
||
impl From<NonZeroU32> for BlockProcessingVelocity { | ||
fn from(value: NonZeroU32) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
/// The consensus hook for managing number of blocks authored per slot. | ||
pub trait ConsensusHook { | ||
/// This hook is called on each block initialization to determine if it's valid to author | ||
/// another block. | ||
fn block_processing_velocity() -> BlockProcessingVelocity; | ||
} | ||
|
||
/// A consensus hook for a fixed block processing velocity. | ||
/// | ||
/// Since it is illegal to provide zero-velocity, this sets a minimum of 1. | ||
pub struct FixedVelocityConsensusHook<const N: u32>; | ||
|
||
impl<const N: u32> ConsensusHook for FixedVelocityConsensusHook<N> { | ||
fn block_processing_velocity() -> BlockProcessingVelocity { | ||
NonZeroU32::new(sp_std::cmp::max(N, 1)) | ||
.expect("1 is the minimum value and non-zero; qed") | ||
.into() | ||
} | ||
} | ||
|
||
/// A fixed-velocity consensus hook, which assumes there's only single core | ||
/// assigned to parachain. | ||
pub type DefaultBlockProcessingVelocity = FixedVelocityConsensusHook<1>; |
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