This commit is contained in:
Tiago Batista Cardoso
2025-11-30 14:49:16 +01:00
parent 85e78447a7
commit 721d52a028
5 changed files with 111 additions and 45 deletions

View File

@@ -1,5 +1,4 @@
use rand::{Rng, rng};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::hash::{DefaultHasher, Hash, Hasher};
@@ -283,29 +282,24 @@ impl MerkleNode {
pub fn serialize(&self) -> Vec<u8> {
let mut bytes = Vec::new();
// 1. Add the type byte
bytes.push(self.get_type_byte());
// 2. Add the node-specific data
match self {
MerkleNode::Chunk(node) => {
bytes.extend_from_slice(&node.data);
}
MerkleNode::Directory(node) => {
// The data is the sequence of directory entries
for entry in &node.entries {
bytes.extend_from_slice(&entry.filename);
bytes.extend_from_slice(&entry.content_hash);
}
}
MerkleNode::Big(node) => {
// The data is the list of child hashes
for hash in &node.children_hashes {
bytes.extend_from_slice(hash);
}
}
MerkleNode::BigDirectory(node) => {
// The data is the list of child hashes
for hash in &node.children_hashes {
bytes.extend_from_slice(hash);
}