tycho_block_util/block/
block_id_ext.rs1use tycho_types::models::{BlockId, BlockIdShort};
2
3pub trait BlockIdExt {
4 fn relative_to(self, mc_block_id: BlockId) -> BlockIdRelation;
5
6 fn relative_to_self(self) -> BlockIdRelation;
7}
8
9#[derive(Default, Clone, Copy)]
10pub struct BlockIdRelation {
11 pub mc_block_id: BlockId,
12 pub block_id: BlockId,
13}
14
15impl std::fmt::Debug for BlockIdRelation {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 struct DebugBlockId<'a>(&'a BlockId);
18
19 impl std::fmt::Debug for DebugBlockId<'_> {
20 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21 std::fmt::Display::fmt(self.0, f)
22 }
23 }
24
25 f.debug_struct("BlockIdRelation")
26 .field("mc_block_id", &DebugBlockId(&self.mc_block_id))
27 .field("block_id", &DebugBlockId(&self.block_id))
28 .finish()
29 }
30}
31
32impl BlockIdExt for BlockId {
33 fn relative_to(self, mc_block_id: BlockId) -> BlockIdRelation {
34 BlockIdRelation {
35 block_id: self,
36 mc_block_id,
37 }
38 }
39
40 fn relative_to_self(self) -> BlockIdRelation {
41 BlockIdRelation {
42 mc_block_id: self,
43 block_id: self,
44 }
45 }
46}
47
48impl BlockIdExt for &BlockId {
49 fn relative_to(self, mc_block_id: BlockId) -> BlockIdRelation {
50 BlockIdRelation {
51 block_id: *self,
52 mc_block_id,
53 }
54 }
55
56 fn relative_to_self(self) -> BlockIdRelation {
57 BlockIdRelation {
58 mc_block_id: *self,
59 block_id: *self,
60 }
61 }
62}
63
64pub fn calc_next_block_id_short(prev_blocks_ids: &[BlockId]) -> BlockIdShort {
65 debug_assert!(!prev_blocks_ids.is_empty());
66
67 let shard = prev_blocks_ids[0].shard;
68 let max_prev_seqno = prev_blocks_ids.iter().map(|id| id.seqno).max().unwrap();
69 BlockIdShort {
70 shard,
71 seqno: max_prev_seqno + 1,
72 }
73}