massa_consensus_exports/
block_status.rs1use massa_models::{
2 active_block::ActiveBlock,
3 address::Address,
4 block::{Block, SecureShareBlock},
5 block_header::SecuredHeader,
6 block_id::BlockId,
7 prehash::PreHashSet,
8 slot::Slot,
9};
10use massa_storage::Storage;
11use serde::{Deserialize, Serialize};
12
13#[derive(Debug, Clone)]
14#[allow(clippy::large_enum_variant)]
15pub enum HeaderOrBlock {
16 Header(SecuredHeader),
17 Block {
18 id: BlockId,
19 slot: Slot,
20 storage: Storage,
21 },
22}
23
24impl HeaderOrBlock {
25 pub fn get_slot(&self) -> Slot {
27 match self {
28 HeaderOrBlock::Header(header) => header.content.slot,
29 HeaderOrBlock::Block { slot, .. } => *slot,
30 }
31 }
32}
33
34#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
36pub enum DiscardReason {
37 Invalid(String),
39 Stale,
41 Final,
43}
44
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub enum BlockStatusId {
47 Incoming = 0,
48 WaitingForSlot = 1,
49 WaitingForDependencies = 2,
50 Active = 3,
51 Discarded = 4,
52}
53
54impl From<&BlockStatus> for BlockStatusId {
55 fn from(status: &BlockStatus) -> Self {
56 match status {
57 BlockStatus::Incoming(_) => BlockStatusId::Incoming,
58 BlockStatus::WaitingForSlot(_) => BlockStatusId::WaitingForSlot,
59 BlockStatus::WaitingForDependencies { .. } => BlockStatusId::WaitingForDependencies,
60 BlockStatus::Active { .. } => BlockStatusId::Active,
61 BlockStatus::Discarded { .. } => BlockStatusId::Discarded,
62 }
63 }
64}
65
66#[derive(Debug, Clone)]
69pub enum StorageOrBlock {
70 Storage(Storage),
72 Block(Box<SecureShareBlock>),
74}
75
76impl StorageOrBlock {
77 pub fn clone_block(&self, block_id: &BlockId) -> SecureShareBlock {
81 match self {
82 StorageOrBlock::Storage(storage) => storage
83 .read_blocks()
84 .get(block_id)
85 .expect("block absent from its own storage")
86 .clone(),
87 StorageOrBlock::Block(block) => *block.clone(),
88 }
89 }
90
91 pub fn strip_to_block(&mut self, block_id: &BlockId) {
94 let block = if let StorageOrBlock::Storage(storage) = self {
95 storage
96 .read_blocks()
97 .get(block_id)
98 .expect("block absent from its own storage")
99 .clone()
100 } else {
101 return;
102 };
103 *self = StorageOrBlock::Block(Box::new(block));
104 }
105}
106
107#[derive(Debug, Clone)]
109pub enum BlockStatus {
110 Incoming(HeaderOrBlock),
113 WaitingForSlot(HeaderOrBlock),
116 WaitingForDependencies {
118 header_or_block: HeaderOrBlock,
120 unsatisfied_dependencies: PreHashSet<BlockId>,
122 sequence_number: u64,
124 },
125 Active {
127 a_block: Box<ActiveBlock>,
128 storage_or_block: StorageOrBlock,
129 },
130 Discarded {
132 slot: Slot,
134 creator: Address,
136 parents: Vec<BlockId>,
138 reason: DiscardReason,
140 sequence_number: u64,
142 },
143}
144
145#[derive(Debug, Clone, Serialize, Deserialize)]
147pub enum ExportBlockStatus {
148 Incoming,
150 WaitingForSlot,
152 WaitingForDependencies,
154 Active(Block),
156 Final(Block),
158 Discarded(DiscardReason),
160}
161
162#[derive(Debug, Clone, Serialize, Deserialize)]
165pub struct ExportCompiledBlock {
166 pub header: SecuredHeader,
168 pub children: Vec<PreHashSet<BlockId>>,
173 pub is_final: bool,
175}
176
177#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
179pub enum Status {
180 Active,
182 Final,
184}