Skip to main content

ab_client_consensus_common/
lib.rs

1pub mod consensus_parameters;
2pub mod state;
3#[cfg(test)]
4mod test_utils;
5
6use ab_core_primitives::block::{BlockNumber, BlockTimestamp};
7use ab_core_primitives::pot::{SlotDuration, SlotNumber};
8use ab_core_primitives::segments::HistorySize;
9use futures::channel::mpsc;
10use std::num::NonZeroU8;
11
12/// Proof-of-time consensus constants
13#[derive(Debug, PartialEq, Eq, Clone, Copy)]
14pub struct PotConsensusConstants {
15    /// Interval, in blocks, between blockchain entropy injection into the proof of time chain
16    pub entropy_injection_interval: BlockNumber,
17    /// Interval, in entropy injection intervals, where to take entropy for injection from
18    pub entropy_injection_lookback_depth: NonZeroU8,
19    /// Delay after block, in slots, when entropy injection takes effect
20    pub entropy_injection_delay: SlotNumber,
21}
22
23/// Consensus constants
24#[derive(Debug, PartialEq, Eq, Clone, Copy)]
25pub struct ConsensusConstants {
26    /// Depth at which a block is confirmed and enters the recorded history.
27    ///
28    /// When block `n` is built, the block at `n - block_confirmation_depth` is confirmed, so the
29    /// largest possible reorg is `block_confirmation_depth - 1` blocks. Importing the next best
30    /// block (block number `best_number + 1`) confirms the block at
31    /// `best_number + 1 - block_confirmation_depth`, so fork blocks must be above it, and blocks
32    /// up to it are the same on every chain that can still be extended. This is why they can
33    /// be referenced by block number alone.
34    ///
35    /// With `block_confirmation_depth = 5` and `shard_confirmation_depth = 2`:
36    ///
37    /// ```text
38    /// block number
39    /// best_number + 1  next best block
40    /// best_number      best block
41    /// best_number - 3  deepest fork blocks
42    /// best_number - 4  confirmed once the next best block is imported  -+ read by the super
43    /// best_number - 5  confirmed                                        | segment derivation of
44    /// best_number - 6  confirmed                                       -+ the next best block
45    /// ```
46    ///
47    /// The block lifecycle in the database is shown on `ClientDatabaseOptions` in
48    /// `ab-client-database`.
49    ///
50    /// This is from the perspective of the individual shard, with additional confirmation on the
51    /// beacon chain required for intermediate and leaf shards (see `shard_confirmation_depth`).
52    pub block_confirmation_depth: BlockNumber,
53    /// Depth on the beacon chain in addition to `block_confirmation_depth` after intermediate or
54    /// leaf shard information on the beacon chain is confirmed and corresponding segment roots
55    /// become a part of the super segment.
56    ///
57    /// Segment roots included in the beacon chain block at
58    /// `n - block_confirmation_depth - shard_confirmation_depth` become a part of the super
59    /// segment of block `n` unless later beacon chain blocks up to `n - block_confirmation_depth`
60    /// show that their shard reorged them (see the diagram on `block_confirmation_depth`).
61    ///
62    /// This is separate from the block confirmation on the corresponding shard itself, which
63    /// happens much sooner (see `block_confirmation_depth`), but can't be immediately trusted by
64    /// the beacon chain.
65    pub shard_confirmation_depth: BlockNumber,
66    /// Number of slots between slot arrival and when the corresponding block can be produced
67    pub block_authoring_delay: SlotNumber,
68    /// Proof-of-time consensus constants
69    pub pot: PotConsensusConstants,
70    // TODO: Non-zero block number would be nice
71    /// Period of time in blocks after which the solution range is adjusted
72    pub retarget_interval: BlockNumber,
73    /// Slot probability
74    pub slot_probability: (u64, u64),
75    /// The slot duration in milliseconds
76    pub slot_duration: SlotDuration,
77    /// Number of latest archived segments that are considered "recent history"
78    pub recent_segments: HistorySize,
79    /// Fraction of pieces from the "recent history" (`recent_segments`) in each sector
80    pub recent_history_fraction: (HistorySize, HistorySize),
81    /// Minimum lifetime of a plotted sector, measured in archived segments
82    pub min_sector_lifetime: HistorySize,
83    /// Max block timestamp drift allowed
84    pub max_block_timestamp_drift: BlockTimestamp,
85    // TODO: Non-zero block number would be nice
86    /// Number of beacon chain blocks between shard rotations.
87    ///
88    /// Every this number of beacon chain blocks PoT entropy is taken to calculate the next shard
89    /// assignment.
90    pub shard_rotation_interval: BlockNumber,
91    /// Delay in beacon chain blocks for the next shard rotation.
92    ///
93    /// Delay after shard assignment is revealed before it actually takes effect (essentially the
94    /// amount of time for a node to sync the corresponding shard).
95    pub shard_rotation_delay: BlockNumber,
96}
97
98/// Notification with information about the block that is about to be imported and acknowledgement
99/// sender that can be used to pause block production if necessary
100#[derive(Debug, Clone)]
101pub struct BlockImportingNotification {
102    /// Block number
103    pub block_number: BlockNumber,
104    /// Sender for pausing the block import for archiving purposes is not fast enough to process
105    /// the consensus block
106    pub acknowledgement_sender: mpsc::Sender<()>,
107}