Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMT: Subtree Append #171

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions contracts/programs/gummyroll/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ pub enum GummyrollError {
/// When using Canopy, the stored byte length should a multiple of the node's byte length (32 bytes)
#[msg("Expected a different byte length for the merkle roll canopy")]
CanopyLengthMismatch,

/// Error modify pre-subtree-append data structure.
/// Your tree may have been modified during your initialization process, your proof may be invalid or out of date. Please reset the account.
#[msg("Merkle Roll Pre Append Processing Error")]
MerkleTreePreAppendProcessingError,

/// An issue was detected with loading the provided account data for the PreAppend data structure.
#[msg("Issue zero copying concurrent merkle tree append structure")]
PreAppendZeroCopyError,
}

impl From<&CMTError> for GummyrollError {
Expand Down
340 changes: 251 additions & 89 deletions contracts/programs/gummyroll/src/lib.rs

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions contracts/programs/gummyroll/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};
use concurrent_merkle_tree::state::{ChangeLog, Node};
use concurrent_merkle_tree::state::{ChangeLog, ChangeLogInterface, Node, Path};

#[derive(AnchorDeserialize, AnchorSerialize, Clone, Copy, Debug)]
pub struct PathNode {
Expand Down Expand Up @@ -41,30 +41,30 @@ pub struct ChangeLogEvent {
/// Bitmap of node parity (used when hashing)
pub index: u32,
}
// ChangeLog<MAX_DEPTH>
impl<const MAX_DEPTH: usize> From<(Box<ChangeLog<MAX_DEPTH>>, Pubkey, u64)>

impl From<(Box<dyn ChangeLogInterface>, Pubkey, u64)>
for Box<ChangeLogEvent>
{
fn from(log_info: (Box<ChangeLog<MAX_DEPTH>>, Pubkey, u64)) -> Self {
fn from(log_info: (Box<dyn ChangeLogInterface>, Pubkey, u64)) -> Self {
let (changelog, tree_id, seq) = log_info;
let path_len = changelog.path.len() as u32;
let path_len = changelog.get_path_as_vec().len() as u32;
let mut path: Vec<PathNode> = changelog
.path
.get_path_as_vec()
.iter()
.enumerate()
.map(|(lvl, n)| {
PathNode::new(
*n,
(1 << (path_len - lvl as u32)) + (changelog.index >> lvl),
(1 << (path_len - lvl as u32)) + (changelog.get_index() >> lvl),
)
})
.collect();
path.push(PathNode::new(changelog.root, 1));
path.push(PathNode::new(changelog.get_root(), 1));
Box::new(ChangeLogEvent {
id: tree_id,
path,
seq,
index: changelog.index,
index: changelog.get_index(),
})
}
}
Expand Down
7 changes: 6 additions & 1 deletion contracts/programs/gummyroll/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use anchor_lang::{
solana_program::{msg, program::invoke, program_error::ProgramError},
};
use bytemuck::{Pod, PodCastError};
use concurrent_merkle_tree::merkle_roll::MerkleRoll;
use concurrent_merkle_tree::merkle_roll::{MerkleRoll, MerkleRollPreAppend};
use std::any::type_name;
use std::mem::size_of;

Expand Down Expand Up @@ -37,6 +37,11 @@ impl<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> ZeroCopy
{
}

impl<const NUM_PARTITIONS: usize> ZeroCopy
for MerkleRollPreAppend<NUM_PARTITIONS>
{
}

pub fn error_msg<T>(data_len: usize) -> impl Fn(PodCastError) -> ProgramError {
move |_: PodCastError| -> ProgramError {
msg!(
Expand Down
8 changes: 8 additions & 0 deletions lib/concurrent-merkle-tree/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ pub enum CMTError {
"Valid proof was passed to a leaf, but it's value has changed since the proof was issued"
)]
LeafContentsModified,

/// Attempted to append subtree of invalid size for the current state of the tree
#[error("Cannot append subtree with invalid size")]
SubtreeInvalidSize,

/// Attempted to index into proof beyond rightmost bound
#[error("Invalid index into rightmost_proof")]
InvalidProofAccessOrWrite,
}
Loading