tycho_block_util/archive/
proto.rs

1use tl_proto::{TlRead, TlWrite};
2use tycho_types::models::BlockId;
3
4use crate::tl;
5
6const ARCHIVE_PREFIX_ID: u32 = tl_proto::id!("archive.prefix", scheme = "proto.tl");
7pub const ARCHIVE_PREFIX: [u8; 4] = u32::to_le_bytes(ARCHIVE_PREFIX_ID);
8
9#[derive(Debug, Clone, TlRead, TlWrite)]
10#[tl(boxed, id = "archive.entryHeader", scheme = "proto.tl")]
11pub struct ArchiveEntryHeader {
12    #[tl(with = "tl::block_id")]
13    pub block_id: BlockId,
14    pub ty: ArchiveEntryType,
15    pub data_len: u32,
16}
17
18pub const ARCHIVE_ENTRY_HEADER_LEN: usize = 4 + 4 + 8 + 4 + 32 + 32 + 4 + 4;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TlRead, TlWrite)]
21#[tl(boxed, scheme = "proto.tl")]
22#[repr(u8)]
23pub enum ArchiveEntryType {
24    #[tl(id = "archive.entryType.block")]
25    Block = 0,
26    #[tl(id = "archive.entryType.proof")]
27    Proof = 1,
28    #[tl(id = "archive.entryType.queueDiff")]
29    QueueDiff = 2,
30}
31
32impl ArchiveEntryType {
33    pub const fn from_byte(byte: u8) -> Option<Self> {
34        match byte {
35            0 => Some(Self::Block),
36            1 => Some(Self::Proof),
37            2 => Some(Self::QueueDiff),
38            _ => None,
39        }
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn entry_header_len_consistent() {
49        let header = ArchiveEntryHeader {
50            block_id: BlockId::default(),
51            ty: ArchiveEntryType::Block,
52            data_len: 123123,
53        };
54
55        let serialized = tl_proto::serialize(header);
56        assert_eq!(serialized.len(), ARCHIVE_ENTRY_HEADER_LEN);
57    }
58}