sc_consensus_subspace/
lib.rs

1//! `sc-consensus-subspace` is the core of Subspace consensus implementation.
2//!
3//! You should familiarize yourself with [Subnomicon](https://subnomicon.subspace.network/) and, ideally, protocol
4//! specifications. Documentation here assumes decent prior knowledge of the protocol on conceptual level and will not
5//! explain how the protocol works, it will instead explain how the protocol is implemented.
6//!
7//! All of the modules here are crucial for consensus, open each module for specific details.
8
9#![feature(let_chains, try_blocks, duration_constructors)]
10#![forbid(unsafe_code)]
11#![warn(missing_docs)]
12
13pub mod archiver;
14pub mod aux_schema;
15pub mod block_import;
16pub mod notification;
17pub mod slot_worker;
18#[cfg(test)]
19mod tests;
20pub mod verifier;
21
22use crate::archiver::{ArchivedSegmentNotification, ObjectMappingNotification};
23use crate::block_import::BlockImportingNotification;
24use crate::notification::{SubspaceNotificationSender, SubspaceNotificationStream};
25use crate::slot_worker::{NewSlotNotification, RewardSigningNotification};
26use sp_consensus_subspace::ChainConstants;
27use sp_runtime::traits::Block as BlockT;
28use subspace_erasure_coding::ErasureCoding;
29use subspace_kzg::Kzg;
30
31/// State that must be shared between various consensus components.
32#[derive(Clone)]
33pub struct SubspaceLink<Block: BlockT> {
34    new_slot_notification_sender: SubspaceNotificationSender<NewSlotNotification>,
35    new_slot_notification_stream: SubspaceNotificationStream<NewSlotNotification>,
36    reward_signing_notification_sender: SubspaceNotificationSender<RewardSigningNotification>,
37    reward_signing_notification_stream: SubspaceNotificationStream<RewardSigningNotification>,
38    object_mapping_notification_sender: SubspaceNotificationSender<ObjectMappingNotification>,
39    object_mapping_notification_stream: SubspaceNotificationStream<ObjectMappingNotification>,
40    archived_segment_notification_sender: SubspaceNotificationSender<ArchivedSegmentNotification>,
41    archived_segment_notification_stream: SubspaceNotificationStream<ArchivedSegmentNotification>,
42    block_importing_notification_sender:
43        SubspaceNotificationSender<BlockImportingNotification<Block>>,
44    block_importing_notification_stream:
45        SubspaceNotificationStream<BlockImportingNotification<Block>>,
46    chain_constants: ChainConstants,
47    kzg: Kzg,
48    erasure_coding: ErasureCoding,
49}
50
51impl<Block: BlockT> SubspaceLink<Block> {
52    /// Create new instance.
53    pub fn new(chain_constants: ChainConstants, kzg: Kzg, erasure_coding: ErasureCoding) -> Self {
54        let (new_slot_notification_sender, new_slot_notification_stream) =
55            notification::channel("subspace_new_slot_notification_stream");
56        let (reward_signing_notification_sender, reward_signing_notification_stream) =
57            notification::channel("subspace_reward_signing_notification_stream");
58        let (object_mapping_notification_sender, object_mapping_notification_stream) =
59            notification::channel("subspace_object_mapping_notification_stream");
60        let (archived_segment_notification_sender, archived_segment_notification_stream) =
61            notification::channel("subspace_archived_segment_notification_stream");
62        let (block_importing_notification_sender, block_importing_notification_stream) =
63            notification::channel("subspace_block_importing_notification_stream");
64
65        Self {
66            new_slot_notification_sender,
67            new_slot_notification_stream,
68            reward_signing_notification_sender,
69            reward_signing_notification_stream,
70            object_mapping_notification_sender,
71            object_mapping_notification_stream,
72            archived_segment_notification_sender,
73            archived_segment_notification_stream,
74            block_importing_notification_sender,
75            block_importing_notification_stream,
76            chain_constants,
77            kzg,
78            erasure_coding,
79        }
80    }
81
82    /// Get stream with notifications about new slot arrival with ability to send solution back.
83    pub fn new_slot_notification_stream(&self) -> SubspaceNotificationStream<NewSlotNotification> {
84        self.new_slot_notification_stream.clone()
85    }
86
87    /// A stream with notifications about headers that need to be signed with ability to send
88    /// signature back.
89    pub fn reward_signing_notification_stream(
90        &self,
91    ) -> SubspaceNotificationStream<RewardSigningNotification> {
92        self.reward_signing_notification_stream.clone()
93    }
94
95    /// Get stream with notifications about object mappings
96    pub fn object_mapping_notification_stream(
97        &self,
98    ) -> SubspaceNotificationStream<ObjectMappingNotification> {
99        self.object_mapping_notification_stream.clone()
100    }
101
102    /// Get stream with notifications about archived segment creation
103    pub fn archived_segment_notification_stream(
104        &self,
105    ) -> SubspaceNotificationStream<ArchivedSegmentNotification> {
106        self.archived_segment_notification_stream.clone()
107    }
108
109    /// Get stream with notifications about each imported block right BEFORE import actually
110    /// happens.
111    ///
112    /// NOTE: all Subspace checks have already happened for this block, but block can still
113    /// potentially fail to import in Substrate's internals.
114    pub fn block_importing_notification_stream(
115        &self,
116    ) -> SubspaceNotificationStream<BlockImportingNotification<Block>> {
117        self.block_importing_notification_stream.clone()
118    }
119
120    /// Subspace chain constants.
121    pub fn chain_constants(&self) -> &ChainConstants {
122        &self.chain_constants
123    }
124
125    /// Access KZG instance
126    pub fn kzg(&self) -> &Kzg {
127        &self.kzg
128    }
129
130    /// Access erasure coding instance
131    pub fn erasure_coding(&self) -> &ErasureCoding {
132        &self.erasure_coding
133    }
134}