mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 20:59:23 +00:00
Add basic transaction/block code to Tributary
This commit is contained in:
41
coordinator/tributary/src/merkle.rs
Normal file
41
coordinator/tributary/src/merkle.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use blake2::{Digest, Blake2s256};
|
||||
|
||||
pub(crate) fn merkle(hash_args: &[[u8; 32]]) -> [u8; 32] {
|
||||
let mut hashes = Vec::with_capacity(hash_args.len());
|
||||
for hash in hash_args {
|
||||
hashes.push(Blake2s256::digest([b"leaf_hash".as_ref(), hash].concat()));
|
||||
}
|
||||
|
||||
let zero = [0; 32];
|
||||
let mut interim;
|
||||
while hashes.len() > 1 {
|
||||
interim = Vec::with_capacity(hashes.len() / 2);
|
||||
|
||||
let mut i = 0;
|
||||
while i < hashes.len() {
|
||||
interim.push(Blake2s256::digest(
|
||||
[
|
||||
b"branch_hash".as_ref(),
|
||||
hashes[i].as_ref(),
|
||||
hashes
|
||||
.get(i + i)
|
||||
.map(|hash| {
|
||||
let res: &[u8] = hash.as_ref();
|
||||
res
|
||||
})
|
||||
.unwrap_or(zero.as_ref()),
|
||||
]
|
||||
.concat(),
|
||||
));
|
||||
i += 2;
|
||||
}
|
||||
|
||||
hashes = interim;
|
||||
}
|
||||
|
||||
let mut res = zero;
|
||||
if let Some(hash) = hashes.get(0) {
|
||||
res.copy_from_slice(hash.as_ref());
|
||||
}
|
||||
res
|
||||
}
|
||||
Reference in New Issue
Block a user