Skip to content

Commit

Permalink
fix: implement hash from indexed merkle tree
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasti810 authored and distractedm1nd committed Jul 18, 2024
1 parent d2e571c commit c622d18
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 49 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ clap = { version = "4.3.2", features = ["derive"] }
config = "0.14.0"
fs2 = "0.4.3"
thiserror = "1.0.50"
indexed-merkle-tree = "0.5.0"
indexed-merkle-tree = "0.5.1"
dotenvy = "0.15.7"
ahash = "0.8.7"
celestia-rpc = "0.2.0"
Expand Down
30 changes: 8 additions & 22 deletions src/da/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,14 @@ mod tests {
node::Node,
sha256_mod,
tree::{IndexedMerkleTree, Proof},
Hash,
};
use rand::rngs::OsRng;
use std::{
fs::OpenOptions,
io::{Error, Seek, SeekFrom},
};

const EMPTY_HASH: &str = Node::HEAD;
const TAIL: &str = Node::TAIL;

pub fn clear_file(filename: &str) -> Result<(), Error> {
// Open file for writing
let mut file = OpenOptions::new().write(true).open(filename)?;
Expand All @@ -175,20 +173,8 @@ mod tests {
}

fn build_empty_tree() -> IndexedMerkleTree {
let active_node = Node::new_leaf(
true,
true,
EMPTY_HASH.to_string(),
EMPTY_HASH.to_string(),
TAIL.to_string(),
);
let inactive_node = Node::new_leaf(
false,
true,
EMPTY_HASH.to_string(),
EMPTY_HASH.to_string(),
TAIL.to_string(),
);
let active_node = Node::new_leaf(true, true, Node::HEAD, Node::HEAD, Node::TAIL);
let inactive_node = Node::new_leaf(false, true, Node::HEAD, Node::HEAD, Node::TAIL);

// build a tree with 4 nodes
IndexedMerkleTree::new(vec![
Expand All @@ -201,14 +187,14 @@ mod tests {
}

fn create_node(label: &str, value: &str) -> Node {
let label = sha256_mod(label);
let value = sha256_mod(value);
Node::new_leaf(true, true, label, value, TAIL.to_string())
let label = sha256_mod(label.as_bytes());
let value = sha256_mod(value.as_bytes());
Node::new_leaf(true, true, label, value, Node::TAIL)
}

fn create_proof_and_vk(
prev_commitment: String,
current_commitment: String,
prev_commitment: Hash,
current_commitment: Hash,
proofs: Vec<Proof>,
) -> (Bls12Proof, VerifyingKey) {
let batched_proof =
Expand Down
40 changes: 18 additions & 22 deletions src/node_types/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ impl Sequencer {
let prev_commitment = if epoch > 0 {
let prev_epoch = epoch - 1;
match self.db.get_commitment(&prev_epoch) {
Ok(commitment) => {
Hash::from_hex(commitment.as_str()).map_err(DeimosError::MerkleTree)?
}
Ok(commitment) => Hash::from_hex(commitment.as_str()).unwrap(),
Err(e) => {
return Err(DatabaseError::ReadError(format!(
"commitment for prev epoch {:?}: {:?}",
Expand Down Expand Up @@ -405,16 +403,16 @@ impl Sequencer {
};

let new_chain_entry = ChainEntry {
hash: sha256_mod(
format!(
"{}, {}, {}",
&incoming_entry.operation, &incoming_entry.value, &last.hash
)
.as_str(),
),
hash: {
let mut data = Vec::new();
data.extend_from_slice(incoming_entry.operation.to_string().as_bytes());
data.extend_from_slice(incoming_entry.value.as_ref());
data.extend_from_slice(last.hash.as_ref());
sha256_mod(&data)
},
previous_hash: last.hash.clone(),
operation: incoming_entry.operation.clone(),
value: incoming_entry.value.clone(),
value: sha256_mod(incoming_entry.value.as_bytes()),
};

current_chain.push(new_chain_entry.clone());
Expand Down Expand Up @@ -447,18 +445,16 @@ impl Sequencer {
Err(_) => {
debug!("Hashchain does not exist, creating new one...");
let new_chain = vec![ChainEntry {
hash: sha256_mod(
format!(
"{}, {}, {}",
Operation::Add,
&incoming_entry.value,
Node::HEAD
)
.as_str(),
),
previous_hash: Node::HEAD.to_string(),
hash: {
let mut data = Vec::new();
data.extend_from_slice(Operation::Add.to_string().as_bytes());
data.extend_from_slice(incoming_entry.value.as_ref());
data.extend_from_slice(Node::HEAD.as_ref());
sha256_mod(&data)
},
previous_hash: Node::HEAD,
operation: incoming_entry.operation.clone(),
value: incoming_entry.value.clone(),
value: sha256_mod(incoming_entry.value.as_bytes()),
}];
let last_entry = match new_chain.last() {
Some(entry) => entry,
Expand Down
6 changes: 3 additions & 3 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ fn generate_test_tree(size: usize, node_count: usize) -> Duration {
let mut leaf = Node::new_leaf(
true,
true,
sha256_mod((i + 1).to_string().as_str()).to_string(),
sha256_mod(&i.to_string()),
Node::TAIL.to_string(),
sha256_mod(&[(i + 1) as u8]),
sha256_mod(&[i as u8]),
Node::TAIL,
);

let start = Instant::now();
Expand Down

0 comments on commit c622d18

Please sign in to comment.