cumulus_primitives_spec_messaging/lib.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17//! Primitives for the Speculative Messaging protocol.
18//!
19//! A sender parachain keeps each outgoing stream as an MMR and commits all stream roots as one
20//! `StreamsRoot` (a keyed trie). Messages travel between collators; the relay chain only checks
21//! that each receiver's `Requires` matches a sender's `Provides`.
22//!
23//! The relay-visible types (`StreamsRoot`, `RequiresSet`) are in `polkadot-primitives`. This crate
24//! has the parachain-side types and the off-chain wire format.
25
26#![cfg_attr(not(feature = "std"), no_std)]
27
28extern crate alloc;
29
30pub mod channel;
31pub mod fetch;
32pub mod inherent;
33pub mod lift;
34pub mod mmr;
35pub mod stream;
36pub mod streams_root;
37
38#[cfg(test)]
39mod adversarial_tests;
40#[cfg(test)]
41mod conformance_vectors;
42#[cfg(test)]
43mod test_utils;
44
45pub use channel::{
46 AppliedRegister, BroadcastId, ChannelMessage, InChannelId, InChannelState, OpenSession,
47 OutChannelId, OutChannelState, Register, RegisterStatus, SessionParams,
48};
49pub use fetch::{
50 verify_channel_messages, verify_messages, FetchError, FetchResponse, MessagesRequest,
51 MessagesResponse, VerifyError, MAX_MESSAGES_PER_RESPONSE, MAX_SPECULATIVE_MESSAGE_LEN,
52};
53pub use inherent::{
54 ConsumedStreams, LossyRead, MessagingInherentData, Payload, INHERENT_IDENTIFIER,
55};
56pub use lift::{
57 build_requires, build_requires_entry, stitch, ConsumptionRecord, Interval, LiftError,
58 LiftsBySource, RequiresLift, SourceStreams,
59};
60pub use mmr::{
61 leaf_hash, leaf_hash_with_version, FrontierFull, MessagePosition, MmrExtensionProof,
62 MmrFrontier, MmrRoot, ProofError,
63};
64pub use stream::{PrivateKind, StreamId, STREAM_ID_LEN};
65pub use streams_root::{
66 InnerNode, NodeKey, NodeLookup, NodeStore, StreamProof, StreamsRoot, StreamsTree, TreeChild,
67 TreeError, TreeRootEntry,
68};
69
70/// The hash function for all of speculative messaging: leaves, MMR merges, stream and
71/// commitment-tree roots. A protocol constant. Changing it is a consensus break, so nothing here
72/// is generic over it.
73pub type SpecHasher = sp_runtime::traits::BlakeTwo256;
74
75// Domain tags. Each hash preimage starts with one, so a node in one role can never collide with a
76// node in another.
77
78/// MMR leaf.
79pub const LEAF_TAG: u8 = 0x1;
80
81/// MMR inner node.
82pub const INNER_TAG: u8 = 0x2;
83
84/// MMR peak bagging.
85pub const PEAK_TAG: u8 = 0x3;
86
87/// Stream root ([`mmr::MmrRoot::from_bag`]).
88pub const ROOT_TAG: u8 = 0x4;
89
90/// Commitment-tree leaf. It binds the full `StreamId` key.
91pub const TREE_LEAF_TAG: u8 = 0x5;
92
93/// Commitment-tree inner node. It binds its split bit.
94pub const TREE_INNER_TAG: u8 = 0x6;
95
96/// Leaf preimage version. Versions are hash-disjoint, so the layout can change without old leaves
97/// colliding with new ones.
98pub const LEAF_VERSION: u8 = 0x0;
99
100/// Engine id of the header digest carrying a block's `StreamsRoot`, deposited only by blocks that
101/// send.
102pub const SPMS_ENGINE_ID: sp_runtime::ConsensusEngineId = *b"SPMS";