generated from PaulRBerg/hardhat-template
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(solana): puts BashMerkleTree within shared
- Loading branch information
1 parent
79ac5b8
commit 1e48b5d
Showing
5 changed files
with
81 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "batch-merkle-tree" | ||
version = "0.1.0" | ||
description = "A merkle tree that allows to calculate a root over a batch of leaves" | ||
edition = "2021" | ||
|
||
[features] | ||
default = [] | ||
cpi = ["no-entrypoint"] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
no-log-ix-name = [] | ||
idl-build = ["anchor-lang/idl-build"] | ||
|
||
[dependencies] | ||
alloy-sol-types = "0.7.0" | ||
alloy-primitives = "0.7.0" | ||
anchor-lang = "0.30.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use anchor_lang::prelude::*; | ||
use anchor_lang::solana_program::hash::{hashv, Hash}; | ||
|
||
pub struct BatchMerkleTree { | ||
pub root: Hash, | ||
} | ||
|
||
impl From<Hash> for BatchMerkleTree { | ||
fn from(root: Hash) -> Self { | ||
BatchMerkleTree { root: root } | ||
} | ||
} | ||
|
||
impl BatchMerkleTree { | ||
pub fn new() -> Self { | ||
BatchMerkleTree { | ||
root: Hash::default(), | ||
} | ||
} | ||
|
||
pub fn push_batch(&mut self, leaves: Vec<Hash>) -> Result<()> { | ||
let mut current_layer = leaves; | ||
|
||
while current_layer.len() > 1 { | ||
let mut next_layer = Vec::new(); | ||
let mut i = 0; | ||
while i < current_layer.len() { | ||
if i + 1 < current_layer.len() { | ||
next_layer.push(hashv(&[ | ||
¤t_layer[i].to_bytes(), | ||
¤t_layer[i + 1].to_bytes(), | ||
])); | ||
i += 2; | ||
} else { | ||
next_layer.push(current_layer[i]); | ||
i += 1; | ||
} | ||
} | ||
current_layer = next_layer; | ||
} | ||
|
||
self.root = if self.root == Hash::default() { | ||
current_layer[0] | ||
} else { | ||
hashv(&[&self.root.to_bytes(), ¤t_layer[0].to_bytes()]) | ||
}; | ||
|
||
Ok(()) | ||
} | ||
} |