forester_utils/
account_zero_copy.rs

1use std::{fmt, marker::PhantomData, mem, pin::Pin};
2
3use account_compression::processor::initialize_address_merkle_tree::Pubkey;
4use light_client::rpc::Rpc;
5use light_concurrent_merkle_tree::copy::ConcurrentMerkleTreeCopy;
6use light_hash_set::HashSet;
7use light_hasher::Hasher;
8use light_indexed_merkle_tree::copy::IndexedMerkleTreeCopy;
9use num_traits::{CheckedAdd, CheckedSub, ToBytes, Unsigned};
10use solana_sdk::account::Account;
11
12#[derive(Debug, Clone)]
13pub struct AccountZeroCopy<'a, T> {
14    pub account: Pin<Box<Account>>,
15    deserialized: *const T,
16    _phantom_data: PhantomData<&'a T>,
17}
18
19impl<'a, T> AccountZeroCopy<'a, T> {
20    pub async fn new<R: Rpc>(rpc: &mut R, address: Pubkey) -> AccountZeroCopy<'a, T> {
21        let account = Box::pin(rpc.get_account(address).await.unwrap().unwrap());
22        let deserialized = account.data[8..].as_ptr() as *const T;
23
24        Self {
25            account,
26            deserialized,
27            _phantom_data: PhantomData,
28        }
29    }
30
31    // Safe method to access `deserialized` ensuring the lifetime is respected
32    pub fn deserialized(&self) -> &'a T {
33        unsafe { &*self.deserialized }
34    }
35}
36
37/// Fetches the given account, then copies and serializes it as a `HashSet`.
38///
39/// # Safety
40///
41/// This is highly unsafe. Ensuring that:
42///
43/// * The correct account is used.
44/// * The account has enough space to be treated as a HashSet with specified
45///   parameters.
46/// * The account data is aligned.
47///
48/// Is the caller's responsibility.
49pub async unsafe fn get_hash_set<T, R: Rpc>(rpc: &mut R, pubkey: Pubkey) -> HashSet {
50    let mut data = rpc.get_account(pubkey).await.unwrap().unwrap().data.clone();
51
52    HashSet::from_bytes_copy(&mut data[8 + mem::size_of::<T>()..]).unwrap()
53}
54
55/// Fetches the given account, then copies and serializes it as a
56/// `ConcurrentMerkleTree`.
57pub async fn get_concurrent_merkle_tree<T, R, H, const HEIGHT: usize>(
58    rpc: &mut R,
59    pubkey: Pubkey,
60) -> ConcurrentMerkleTreeCopy<H, HEIGHT>
61where
62    R: Rpc,
63    H: Hasher,
64{
65    let account = rpc.get_account(pubkey).await.unwrap().unwrap();
66
67    ConcurrentMerkleTreeCopy::from_bytes_copy(&account.data[8 + mem::size_of::<T>()..]).unwrap()
68}
69// TODO: do discriminator check
70/// Fetches the given account, then copies and serializes it as an
71/// `IndexedMerkleTree`.
72pub async fn get_indexed_merkle_tree<T, R, H, I, const HEIGHT: usize, const NET_HEIGHT: usize>(
73    rpc: &mut R,
74    pubkey: Pubkey,
75) -> IndexedMerkleTreeCopy<H, I, HEIGHT, NET_HEIGHT>
76where
77    R: Rpc,
78    H: Hasher,
79    I: CheckedAdd
80        + CheckedSub
81        + Copy
82        + Clone
83        + fmt::Debug
84        + PartialOrd
85        + ToBytes
86        + TryFrom<usize>
87        + Unsigned,
88    usize: From<I>,
89{
90    let account = rpc.get_account(pubkey).await.unwrap().unwrap();
91
92    IndexedMerkleTreeCopy::from_bytes_copy(&account.data[8 + mem::size_of::<T>()..]).unwrap()
93}