Skip to content

Commit

Permalink
solana: extract AccountTrie to a new crate
Browse files Browse the repository at this point in the history
To help reuse the AccountTrie type between different programs (most
notably the IBC module and relayer), introduce solana_trie crate and
move it there.
  • Loading branch information
mina86 committed Oct 25, 2023
1 parent f24342e commit 20f5a2a
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 375 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rust-version = "1.71.0"
[workspace]
members = [
"common/*",
"solana/trie",
"solana/trie-example",
"solana/solana-ibc/programs/*",
]
Expand Down Expand Up @@ -42,6 +43,7 @@ strum = { version = "0.25.0", default-features = false, features = ["derive"] }
lib = { path = "common/lib" }
memory = { path = "common/memory" }
sealable-trie = { path = "common/sealable-trie" }
solana-trie = { path = "solana/trie" }
stdx = { path = "common/stdx" }

# dev-dependencies
Expand Down
2 changes: 1 addition & 1 deletion solana/solana-ibc/programs/solana-ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ serde_json.workspace = true

lib.workspace = true
memory.workspace = true
sealable-trie.workspace = true
solana-trie.workspace = true
stdx.workspace = true

[dev-dependencies]
Expand Down
16 changes: 5 additions & 11 deletions solana/solana-ibc/programs/solana-ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,10 @@ mod execution_context;
#[cfg(test)]
mod tests;
mod transfer;
mod trie;
mod trie_key;
mod validation_context;
// mod client_context;

/// Discriminants for the data stored in the accounts.
mod magic {
pub(crate) const UNINITIALISED: u32 = 0;
pub(crate) const TRIE_ROOT: u32 = 1;
}



#[anchor_lang::program]
pub mod solana_ibc {
Expand All @@ -56,8 +48,9 @@ pub mod solana_ibc {
msg!("This is private_store {:?}", private);

let account = &ctx.accounts.trie;
let provable = trie::AccountTrie::new(account.try_borrow_mut_data()?)
.ok_or(ProgramError::InvalidAccountData)?;
let provable =
solana_trie::AccountTrie::new(account.try_borrow_mut_data()?)
.ok_or(ProgramError::InvalidAccountData)?;

let inner = IbcStorageInner { private, provable };
let mut store = IbcStorage(Rc::new(RefCell::new(inner)));
Expand Down Expand Up @@ -238,7 +231,8 @@ pub struct PrivateStorage {
#[derive(Debug)]
pub struct IbcStorageInner<'a, 'b> {
pub private: &'a mut PrivateStorage,
pub provable: trie::AccountTrie<core::cell::RefMut<'a, &'b mut [u8]>>,
pub provable:
solana_trie::AccountTrie<core::cell::RefMut<'a, &'b mut [u8]>>,
}

#[derive(Debug, Clone)]
Expand Down
1 change: 1 addition & 0 deletions solana/trie-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ solana-program.workspace = true
lib.workspace = true
memory.workspace = true
sealable-trie.workspace = true
solana-trie.workspace = true
stdx.workspace = true
15 changes: 4 additions & 11 deletions solana/trie-example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@ use solana_program::program::set_return_data;
use solana_program::program_error::ProgramError;
use solana_program::pubkey::Pubkey;

mod trie;

type Result<T = (), E = ProgramError> = core::result::Result<T, E>;

/// Discriminants for the data stored in the accounts.
mod magic {
pub(crate) const UNINITIALISED: u32 = 0;
pub(crate) const TRIE_ROOT: u32 = 1;
}

solana_program::entrypoint!(process_instruction);

pub fn process_instruction(
Expand All @@ -26,8 +18,9 @@ pub fn process_instruction(
if account.owner != program_id {
return Err(ProgramError::IncorrectProgramId);
}
let mut trie = trie::AccountTrie::new(account.try_borrow_mut_data()?)
.ok_or(ProgramError::InvalidAccountData)?;
let mut trie =
solana_trie::AccountTrie::new(account.try_borrow_mut_data()?)
.ok_or(ProgramError::InvalidAccountData)?;
match Instruction::decode(instruction)? {
Instruction::Get { key, include_proof } => {
handle_get(trie, key, include_proof)?;
Expand All @@ -43,7 +36,7 @@ pub fn process_instruction(
}

fn handle_get(
trie: trie::AccountTrie,
trie: solana_trie::AccountTrie<core::cell::RefMut<'_, &'_ mut [u8]>>,
key: &[u8],
include_proof: bool,
) -> Result {
Expand Down
Loading

0 comments on commit 20f5a2a

Please sign in to comment.