hpl_toolkit/compression/
traits.rs

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
use super::{CompressedDataEvent, CompressedDataEventStream};
use crate::schema::ToSchema;
use anchor_lang::prelude::{AccountInfo, AnchorDeserialize, AnchorSerialize, Key, Program, Result};
use hpl_toolkit_macro_merkle_tree_apply_fn_deep::merkle_tree_apply_fn_deep;

pub trait ToNode {
    fn to_node(&self) -> [u8; 32];
}

pub trait CompressedData {
    fn event_stream(&self) -> CompressedDataEventStream
    where
        Self: AnchorSerialize + ToSchema,
    {
        CompressedDataEventStream::Full {
            data: self.schema_value(),
        }
    }

    fn emit<'info>(
        &self,
        slot: u64,
        leaf_idx: u32,
        merkle_tree: &AccountInfo<'info>,
        log_wrapper: &Program<'info, spl_account_compression::Noop>,
        seq_offset: u64,
    ) -> Result<()>
    where
        Self: AnchorSerialize + ToSchema,
    {
        let merkle_tree_bytes = merkle_tree.try_borrow_data()?;
        CompressedDataEvent::Leaf {
            slot,
            tree_id: merkle_tree.key().to_bytes(),
            leaf_idx,
            seq: super::merkle_tree_apply_fn!(merkle_tree_bytes get_seq) + 1 + seq_offset,
            stream_type: self.event_stream(),
        }
        .wrap(log_wrapper)
    }
}

pub trait CompressedDataChunk {
    const KEY: &'static str;

    fn event_stream(&self) -> CompressedDataEventStream
    where
        Self: AnchorSerialize + ToSchema,
    {
        CompressedDataEventStream::PatchChunk {
            key: Self::KEY.to_string(),
            data: self.schema_value(),
        }
    }

    fn emit<'info>(
        &self,
        slot: u64,
        leaf_idx: u32,
        merkle_tree: &AccountInfo<'info>,
        log_wrapper: &Program<'info, spl_account_compression::Noop>,
        seq_offset: u64,
    ) -> Result<()>
    where
        Self: AnchorSerialize + ToSchema,
    {
        let merkle_tree_bytes = merkle_tree.try_borrow_data()?;
        CompressedDataEvent::Leaf {
            slot,
            tree_id: merkle_tree.key().to_bytes(),
            leaf_idx,
            seq: super::merkle_tree_apply_fn!(merkle_tree_bytes get_seq) + 1 + seq_offset,
            stream_type: self.event_stream(),
        }
        .wrap(log_wrapper)
    }
}