hpl_toolkit/compression/
traits.rsuse 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)
}
}