Write a new impl of the merkle algorithm

This one tries to be understandable.
This commit is contained in:
Luke Parker
2023-07-03 12:33:16 -04:00
parent 2b190851a7
commit 733a5c1f8a
5 changed files with 58 additions and 75 deletions

View File

@@ -5,12 +5,11 @@ use std_shims::{
use crate::{
hash,
merkle::merkle_root,
serialize::*,
transaction::{Input, Transaction},
};
mod merkle_root;
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct BlockHeader {
pub major_version: u64,
@@ -71,11 +70,11 @@ impl Block {
Ok(())
}
pub fn tx_merkle_root(&self) -> [u8; 32] {
merkle_root::tree_hash(self.miner_tx.hash(), &self.txs)
fn tx_merkle_root(&self) -> [u8; 32] {
merkle_root(self.miner_tx.hash(), &self.txs)
}
pub fn serialize_hashable(&self) -> Vec<u8> {
fn serialize_hashable(&self) -> Vec<u8> {
let mut blob = self.header.serialize();
blob.extend_from_slice(&self.tx_merkle_root());
write_varint(&(1 + u64::try_from(self.txs.len()).unwrap()), &mut blob).unwrap();
@@ -87,7 +86,7 @@ impl Block {
out
}
pub fn id(&self) -> [u8; 32] {
pub fn hash(&self) -> [u8; 32] {
// TODO: Handle block 202612
// https://monero.stackexchange.com/questions/421/what-happened-at-block-202612
// If this block's header is fully-equivalent to 202612, return the malformed hash instead