Skip to main content

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