-
Notifications
You must be signed in to change notification settings - Fork 17
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
Storage subset merkle tree onchain #21
base: master
Are you sure you want to change the base?
Conversation
pub struct DistributeVault<'info> { | ||
/// The [ParentState]. | ||
#[account(has_one = admin)] | ||
pub parent_account: AccountLoader<'info, ParentAccount>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DisitributorRoot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
|
||
/// Accounts required for distributing tokens from the parent vault to distributor vaults. | ||
#[derive(Accounts)] | ||
pub struct DistributeVault<'info> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FundMerkleDisitributorFromRoot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
#[derive(Accounts)] | ||
pub struct DistributeVault<'info> { | ||
/// The [ParentState]. | ||
#[account(has_one = admin)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
permissionless?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
pub parent_vault: Account<'info, TokenAccount>, | ||
|
||
/// Admin | ||
pub admin: Signer<'info>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need admin here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
// [distributor_vault, distributor, distributor_vault, distributor] | ||
let remaining_accounts: &[AccountInfo] = ctx.remaining_accounts; | ||
// Ensure valid layout. | ||
if remaining_accounts.len() % 2 != 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need remaninings accounts here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
); | ||
|
||
// Check distributor has been funded token | ||
if distributor_vault.amount >= distributor_state.max_total_claim { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
distributor_state.funded_amount
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
pub struct NewDistributorParams { | ||
pub version: u64, | ||
pub root: [u8; 32], | ||
pub total_nodes: u8, | ||
pub depth: u8, | ||
pub nodes: Vec<[u8; 32]>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
push in last account
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
pub struct NewDistributorParams { | ||
pub version: u64, | ||
pub root: [u8; 32], | ||
pub total_nodes: u8, | ||
pub depth: u8, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you test max depth
@@ -127,6 +132,19 @@ pub struct NewDistributor<'info> { | |||
)] | |||
pub distributor: AccountLoader<'info, MerkleDistributor>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use init_if_needed for token_vault
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
payer = admin | ||
)] | ||
pub partial_merkle_tree: Account<'info, PartialMerkleTree>, | ||
|
||
/// Base key of the distributor. | ||
pub base: Signer<'info>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use admin as unchecked_account, and payer to pay for rent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
space = PartialMerkleTree::space(total_node as usize), | ||
payer = admin | ||
)] | ||
pub partial_merkle_tree: Account<'info, PartialMerkleTree>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
save nodes in distributor state
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because AccountLoader does not support storing a dynamic Vec directly. So, create CanopyTree
to storage metadata (root, nodes, depth..) of MerkleDistributor
@@ -30,6 +34,7 @@ pub struct NewDistributorParams { | |||
pub claim_type: u8, | |||
pub operator: Pubkey, | |||
pub locker: Pubkey, | |||
pub parent_account: Pubkey, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parent should be pda of base + mint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
init, | ||
seeds = [ | ||
b"ParentAccount".as_ref(), | ||
mint.key().to_bytes().as_ref(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pda of mint and base
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto^
/// Parent vault | ||
/// Should create previously | ||
#[account( | ||
associated_token::mint = mint, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
init if need
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
pub system_program: Program<'info, System>, | ||
} | ||
|
||
pub fn handle_new_parent_account(ctx: Context<NewParentAccount>) -> Result<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
max amount claim and max node
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
use anchor_spl::token::{Mint, TokenAccount}; | ||
|
||
#[derive(Accounts)] | ||
pub struct NewParentAccount<'info> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a function to transfer fund to parent account
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done with FundDistributorRoot
let mut parent_account = ctx.accounts.parent_account.load_init()?; | ||
|
||
parent_account.bump = *ctx.bumps.get("parent_account").unwrap(); | ||
parent_account.admin = ctx.accounts.admin.key(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we dont need parent account
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
partial_merkle_tree.depth, | ||
initial_index, | ||
proof, | ||
partial_merkle_tree.nodes.clone(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should use &partial_merkle_tree.nodes.clone()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
/// Token Address of parent vault | ||
pub parent_vault: Pubkey, | ||
/// Padding | ||
pub padding: u64, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need padding here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
Flow for storage a part of proof onchain:
Note:
I quickly tested the NewClaimAndStake transaction locally using:
The resulting transaction size was 661 bytes. That means we have about 571 bytes of remaining space, which should accommodate roughly 17 proofs for this transaction in this case.