tycho-collator 0.3.9

A collator node.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use std::fmt::{Debug, Display};
use std::sync::Arc;

use anyhow::{Result, anyhow, bail};
use tokio::sync::Notify;
use tokio_util::sync::CancellationToken;
use tycho_block_util::queue::{QueueDiffStuff, QueuePartitionIdx};
use tycho_block_util::state::ShardStateStuff;
use tycho_network::PeerId;
use tycho_types::cell::Lazy;
use tycho_types::models::{
    BlockId, BlockIdShort, BlockInfo, OutMsgDescr, ProcessedUptoInfo, ShardIdent,
};
use tycho_util::{FastHashMap, FastHashSet};

use crate::mempool::MempoolAnchorId;
use crate::types::processed_upto::{
    BlockSeqno, ProcessedUptoInfoExtension, ProcessedUptoInfoStuff,
};
use crate::types::{
    ArcSignature, BlockCandidate, BlockStuffForSync, DebugDisplayOpt, ShardDescriptionShortExt,
    ShardHashesExt, TopBlockId, TopBlockIdUpdated,
};
use crate::utils::block::detect_top_processed_to_anchor;

pub(super) type BlockCacheKey = BlockIdShort;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum CollatorState {
    Active,
    Waiting,
    Cancelled,
    CancelPending,
}

pub(super) struct ActiveCollator<C> {
    pub collator: C,
    pub state: CollatorState,

    /// For graceful collation cancellation
    pub cancel_collation: Arc<Notify>,
}

#[derive(Default)]
pub(super) struct CollationSyncState {
    /// Latest known chain time for master block: last imported or next to be collated
    pub mc_block_latest_chain_time: u64,
    /// Master block collation is forced for all shards anyway
    pub mc_collation_forced_for_all: bool,
    /// Collation states by shards. Stores collation status and
    /// additional info for manager to detect next collation step of every shard.
    pub states: FastHashMap<ShardIdent, CollationState>,
    /// If we have running sync then stores the target mc block seqno and cancellation token
    pub active_sync_to_applied: Option<ActiveSync>,
    /// Seqno of last received master block which may be not saved to cache yet
    pub last_received_mc_block_seqno: Option<BlockSeqno>,
    /// Last received applied master block id we have synced to
    pub last_synced_to_mc_block_id: Option<BlockId>,
    /// Master block collation forced by no pending messages after block
    /// in any shard on chain time
    pub mc_forced_by_no_pending_msgs_on_ct: Option<u64>,
}

#[derive(Debug, Default, Clone)]
pub(super) struct ImportedAnchorEvent {
    pub ct: u64,
    pub mc_forced: bool,
    pub collated_block_info: Option<CollatedBlockInfo>,
}

#[derive(Debug, Clone, Copy)]
pub(super) struct CollatedBlockInfo {
    pub prev_mc_block_seqno: BlockSeqno,
    pub has_processed_externals: bool,
}

impl CollatedBlockInfo {
    pub fn new(prev_mc_block_seqno: BlockSeqno, has_processed_externals: bool) -> Self {
        Self {
            prev_mc_block_seqno,
            has_processed_externals,
        }
    }
}

#[derive(Debug)]
pub(super) struct CollationState {
    pub status: CollationStatus,
    pub last_imported_anchor_events: Vec<ImportedAnchorEvent>,
}

impl Default for CollationState {
    fn default() -> Self {
        Self {
            status: CollationStatus::AttemptsInProgress,
            last_imported_anchor_events: vec![],
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum CollationStatus {
    /// Collator is trying to collate current shard,
    /// may import next anchor
    AttemptsInProgress,
    /// Shard collator is waiting for master collator status,
    /// and not trying to collate current shard,
    /// will not import next anchor
    WaitForMasterStatus,
    /// Master collator is waiting for shard status,
    /// and not trying to collate master,
    /// will not import next anchor
    WaitForShardStatus,
    /// Current shard is ready for master collation,
    /// collator is not trying to collate current shard,
    /// will not import next anchor
    ReadyToCollateMaster,
}

#[derive(Debug)]
pub(super) enum NextCollationStep {
    WaitForMasterStatus,
    WaitForShardStatus,
    ResumeAttemptsIn(Vec<ShardIdent>),
    CollateMaster(u64),
}

pub(super) struct ActiveSync {
    pub target_mc_block_seqno: BlockSeqno,
    pub cancelled: CancellationToken,
}

pub(super) struct BlockCacheStoreResult {
    pub received_and_collated: bool,
    pub block_mismatch: bool,
    /// We need this to manage queue recovery on sync
    pub last_collated_mc_block_id: Option<BlockId>,
    pub applied_mc_queue_range: Option<(BlockSeqno, BlockSeqno)>,
}

impl Debug for BlockCacheStoreResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BlockCacheStoreResult")
            .field("received_and_collated", &self.received_and_collated)
            .field("block_mismatch", &self.block_mismatch)
            .field(
                "last_collated_mc_block_id",
                &DebugDisplayOpt(self.last_collated_mc_block_id),
            )
            .field("applied_mc_queue_range", &self.applied_mc_queue_range)
            .finish()
    }
}

#[derive(Clone)]
pub(super) struct BlockCandidateStuff {
    pub candidate: BlockCandidate,
    pub signatures: FastHashMap<PeerId, ArcSignature>,
    pub total_signature_weight: u64,
}

impl BlockCandidateStuff {
    pub fn into_block_for_sync(self) -> Arc<BlockStuffForSync> {
        let BlockCandidateStuff {
            candidate,
            signatures,
            total_signature_weight,
        } = self;

        let BlockCandidate {
            ref_by_mc_seqno,
            block: block_stuff_aug,
            prev_blocks_ids,
            top_shard_blocks_ids,
            queue_diff_aug,
            consensus_info,
            ..
        } = candidate;

        Arc::new(BlockStuffForSync {
            ref_by_mc_seqno,
            block_stuff_aug,
            queue_diff_aug,
            signatures,
            total_signature_weight,
            prev_blocks_ids,
            top_shard_blocks_ids,
            consensus_info,
        })
    }
}

#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
pub(super) enum CandidateStatus {
    Collated,
    Validated,
    Synced,
}

#[expect(clippy::large_enum_variant)]
pub(super) enum BlockCacheEntryData {
    Collated {
        /// Collated block candidate with signatures
        candidate_stuff: BlockCandidateStuff,

        /// Candidate lifecycle status
        status: CandidateStatus,

        /// Whether the block was received after collation
        received_after_collation: bool,
    },
    Received {
        /// Cached state of the applied master block
        cached_state: Option<ShardStateStuff>,
        /// Applied block queue diff
        queue_diff: QueueDiffStuff,
        /// Applied block out messages
        out_msgs: Lazy<OutMsgDescr>,

        /// Whether the block was collated after receiving
        collated_after_receive: bool,

        /// Additional shard block cache info
        additional_shard_block_cache_info: Option<AdditionalShardBlockCacheInfo>,

        /// Processed to info for every partition
        processed_upto: ProcessedUptoInfoStuff,
    },
}

impl BlockCacheEntryData {
    pub fn get_additional_shard_block_cache_info(
        &self,
    ) -> Result<Option<AdditionalShardBlockCacheInfo>> {
        Ok(match self {
            Self::Collated {
                candidate_stuff, ..
            } => Some(AdditionalShardBlockCacheInfo {
                processed_to_anchor_id: candidate_stuff.candidate.processed_to_anchor_id,
                block_info: candidate_stuff.candidate.block.load_info()?.clone(),
            }),
            Self::Received {
                additional_shard_block_cache_info,
                ..
            } => additional_shard_block_cache_info.clone(),
        })
    }

    pub fn processed_upto(&self) -> &ProcessedUptoInfoStuff {
        match self {
            Self::Received { processed_upto, .. }
            | Self::Collated {
                candidate_stuff:
                    BlockCandidateStuff {
                        candidate: BlockCandidate { processed_upto, .. },
                        ..
                    },
                ..
            } => processed_upto,
        }
    }
}

#[derive(Debug, Clone)]
pub struct AdditionalShardBlockCacheInfo {
    pub block_info: BlockInfo,
    pub processed_to_anchor_id: u32,
}

impl Display for BlockCacheEntryData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Collated {
                received_after_collation,
                ..
            } => f
                .debug_struct("Collated")
                .field("received_after_collation", received_after_collation)
                .finish(),
            Self::Received {
                collated_after_receive,
                ..
            } => f
                .debug_struct("Received")
                .field("collated_after_receive", collated_after_receive)
                .finish(),
        }
    }
}

pub(super) struct BlockCacheEntry {
    pub block_id: BlockId,

    /// Block cache entry data
    pub data: BlockCacheEntryData,

    /// Ids of 1 (or 2 in case of merge) previous blocks in shard or master chain
    pub prev_blocks_ids: Vec<BlockId>,
    /// List of shard block ids included in current block.
    /// `updated` indicates if `block_id` was updated since previous block.
    /// It must be filled for master block.
    /// It could be filled for shard blocks if shards can exchange shard blocks with each other without master.
    pub top_shard_blocks_info: Vec<TopBlockIdUpdated>,
    /// For master block will contain top processed to anchor among master and all shards.
    pub top_processed_to_anchor: Option<MempoolAnchorId>,

    /// Seqno of master block that includes current shard block in his subgraph
    pub ref_by_mc_seqno: u32,
}

impl BlockCacheEntry {
    pub fn from_collated(
        candidate: Box<BlockCandidate>,
        top_shard_blocks_info: Vec<TopBlockIdUpdated>,
        top_processed_to_anchor: Option<MempoolAnchorId>,
    ) -> Result<Self> {
        let block_id = *candidate.block.id();
        let prev_blocks_ids = candidate.prev_blocks_ids.clone();
        let ref_by_mc_seqno = candidate.ref_by_mc_seqno;
        let entry = BlockCandidateStuff {
            candidate: *candidate,
            signatures: Default::default(),
            total_signature_weight: 0,
        };

        Ok(Self {
            block_id,
            data: BlockCacheEntryData::Collated {
                candidate_stuff: entry,
                status: CandidateStatus::Collated,
                received_after_collation: false,
            },
            prev_blocks_ids,
            top_shard_blocks_info,
            top_processed_to_anchor,
            ref_by_mc_seqno,
        })
    }

    pub fn from_received(
        state: ShardStateStuff,
        prev_blocks_ids: Vec<BlockId>,
        queue_diff: QueueDiffStuff,
        out_msgs: Lazy<OutMsgDescr>,
        ref_by_mc_seqno: u32,
        processed_upto: ProcessedUptoInfoStuff,
    ) -> Result<Self> {
        let block_id = *state.block_id();

        let mut top_shard_blocks_info = vec![];
        let mut top_processed_to_anchor = None;
        let cached_state = if block_id.is_masterchain() {
            for item in state.shards()?.iter() {
                let (shard_id, shard_descr) = item?;
                top_shard_blocks_info.push(TopBlockIdUpdated {
                    block: TopBlockId {
                        ref_by_mc_seqno: shard_descr.reg_mc_seqno,
                        block_id: shard_descr.get_block_id(shard_id),
                    },
                    updated: shard_descr.top_sc_block_updated,
                });
            }
            top_processed_to_anchor = Some(detect_top_processed_to_anchor(
                state.shards()?.as_vec()?.iter().map(|(_, d)| *d),
                state
                    .state()
                    .processed_upto
                    .load()?
                    .get_min_externals_processed_to()?
                    .0,
            ));
            Some(state)
        } else {
            None
        };

        Ok(Self {
            block_id,
            data: BlockCacheEntryData::Received {
                cached_state,
                queue_diff,
                out_msgs,
                collated_after_receive: false,
                additional_shard_block_cache_info: None,
                processed_upto,
            },
            prev_blocks_ids,
            top_shard_blocks_info,
            top_processed_to_anchor,
            ref_by_mc_seqno,
        })
    }

    pub fn key(&self) -> BlockCacheKey {
        self.block_id.as_short_id()
    }

    pub fn iter_top_shard_blocks_ids(&self) -> impl Iterator<Item = &BlockId> {
        self.top_shard_blocks_info
            .iter()
            .map(|item| &item.block.block_id)
    }

    pub fn get_top_blocks_keys(&self) -> Result<Vec<BlockCacheKey>> {
        if !self.block_id.is_masterchain() {
            bail!(
                "get_top_blocks_keys can be called only for master block, current block_id is {}",
                self.block_id,
            )
        }

        let mut top_blocks_keys = vec![self.key()];
        top_blocks_keys.extend(self.iter_top_shard_blocks_ids().map(|id| id.as_short_id()));

        Ok(top_blocks_keys)
    }

    pub fn cached_state(&self) -> Result<&ShardStateStuff> {
        if let BlockCacheEntryData::Received { cached_state, .. } = &self.data {
            cached_state.as_ref().ok_or_else(|| {
                anyhow!(
                    "`cached_state` shoul not be None for block ({})",
                    self.block_id
                )
            })
        } else {
            anyhow::bail!(
                "Block should be `Received` to contain `cached_state` ({})",
                self.block_id
            )
        }
    }
}

pub(super) struct McBlockSubgraph {
    pub master_block: BlockCacheEntry,
    pub shard_blocks: Vec<BlockCacheEntry>,
}

impl McBlockSubgraph {
    pub(crate) fn get_partitions(&self) -> FastHashSet<QueuePartitionIdx> {
        let mut partitions = FastHashSet::default();

        for block in self
            .shard_blocks
            .iter()
            .chain(std::iter::once(&self.master_block))
        {
            for partition in block.data.processed_upto().partitions.keys() {
                partitions.insert(*partition);
            }
        }
        partitions
    }
}

#[expect(clippy::large_enum_variant)]
pub(super) enum McBlockSubgraphExtract {
    Extracted(McBlockSubgraph),
    AlreadyExtracted,
}

impl Display for McBlockSubgraphExtract {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Extracted(_) => write!(f, "Extracted"),
            Self::AlreadyExtracted => write!(f, "AlreadyExtracted"),
        }
    }
}

pub struct HandledBlockFromBcCtx {
    pub mc_block_id: BlockId,
    pub state: ShardStateStuff,
    pub processed_upto: ProcessedUptoInfo,
}