Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hansieodendaal committed Aug 12, 2022
1 parent e942f8e commit 4427dab
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ use tari_crypto::{
commitment::HomomorphicCommitment,
tari_utilities::{hex::Hex, Hashable},
};
use tari_mmr::{
BaseLayerKernelMmrHasherBlake256,
BaseLayerOutputMmrHasherBlake256,
BaseLayerWitnessMmrHasherBlake256,
MerkleMountainRange,
MutableMmr,
};
use tokio::task;

use super::error::HorizonSyncError;
Expand Down Expand Up @@ -80,6 +73,10 @@ use crate::{
TransactionOutput,
},
validation::{helpers, FinalHorizonStateValidation},
MutablePrunedOutputMmr,
PrunedKernelMmr,
PrunedOutputMmr,
PrunedWitnessMmr,
};

const LOG_TARGET: &str = "c::bn::state_machine_service::states::horizon_state_sync";
Expand Down Expand Up @@ -331,7 +328,7 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
.fetch_block_accumulated_data(current_header.header().prev_hash.clone())
.await?;
let kernel_pruned_set = block_data.dissolve().0;
let mut kernel_mmr = MerkleMountainRange::<BaseLayerKernelMmrHasherBlake256, _>::new(kernel_pruned_set);
let mut kernel_mmr = PrunedKernelMmr::new(kernel_pruned_set);

for hash in kernel_hashes.drain(..) {
kernel_mmr.push(hash)?;
Expand Down Expand Up @@ -492,8 +489,8 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
.await?;
let (_, output_pruned_set, witness_pruned_set, _) = block_data.dissolve();

let mut output_mmr = MerkleMountainRange::<BaseLayerOutputMmrHasherBlake256, _>::new(output_pruned_set);
let mut witness_mmr = MerkleMountainRange::<BaseLayerWitnessMmrHasherBlake256, _>::new(witness_pruned_set);
let mut output_mmr = PrunedOutputMmr::new(output_pruned_set);
let mut witness_mmr = PrunedWitnessMmr::new(witness_pruned_set);
let mut constants = self.rules.consensus_constants(current_header.height()).clone();
let mut last_sync_timer = Instant::now();
let mut avg_latency = RollingAverageTime::new(20);
Expand Down Expand Up @@ -601,10 +598,7 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
bitmap.run_optimize();

let pruned_output_set = output_mmr.get_pruned_hash_set()?;
let output_mmr = MutableMmr::<BaseLayerOutputMmrHasherBlake256, _>::new(
pruned_output_set.clone(),
bitmap.clone(),
)?;
let output_mmr = MutablePrunedOutputMmr::new(pruned_output_set.clone(), bitmap.clone())?;

let mmr_root = output_mmr.get_merkle_root()?;
if mmr_root != current_header.header().output_mr {
Expand Down
57 changes: 22 additions & 35 deletions base_layer/core/src/blocks/genesis_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,27 @@ pub fn get_igor_genesis_block() -> ChainBlock {
// lets get the block
let block = get_igor_genesis_block_raw();

// user this code to generate the correct header mr fields for igor if the gen block changes.
// Use this code to generate the correct header mr fields for igor if the gen block changes.

// use croaring::Bitmap;
// use tari_mmr::{MerkleMountainRange, MutableMmr};
// use tari_mmr::{BaseLayerOutputMmrHasherBlake256, BaseLayerWitnessMmrHasherBlake256, MerkleMountainRange,
// MutableMmr};
//
// let mut kernel_mmr = MerkleMountainRange::<BaseLayerKernelMmrHasherBlake256, _>::new(Vec::new());
// use crate::{BaseLayerKernelMmr, BaseLayerOutputMutableMmr, BaseLayerWitnessMmr};
//
// let mut kernel_mmr = BaseLayerKernelMmr::new(Vec::new());
// for k in block.body.kernels() {
// println!("k: {}", k);
// kernel_mmr.push(k.hash()).unwrap();
// }
//
// let mut witness_mmr = MerkleMountainRange::<BaseLayerWitnessMmrHasherBlake256, _>::new(Vec::new());
// let mut output_mmr = MutableMmr::<BaseLayerOutputMmrHasherBlake256, _>::new(Vec::new(),
// Bitmap::create()).unwrap();
// let mut witness_mmr = BaseLayerWitnessMmr::new(Vec::new());
// let mut output_mmr = BaseLayerOutputMutableMmr::new(Vec::new(), Bitmap::create()).unwrap();
//
// for o in block.body.outputs() {
// witness_mmr.push(o.witness_hash()).unwrap();
// output_mmr.push(o.hash()).unwrap();
// }
//
// println!("kernel mr: {}",kernel_mmr.get_merkle_root().unwrap().to_hex());
// println!("kernel mr: {}", kernel_mmr.get_merkle_root().unwrap().to_hex());
// println!("witness mr: {}", witness_mmr.get_merkle_root().unwrap().to_hex());
// println!("output mr: {}", output_mmr.get_merkle_root().unwrap().to_hex());

Expand Down Expand Up @@ -220,23 +219,17 @@ pub fn get_esmeralda_genesis_block() -> ChainBlock {
// NB: `esmerlada_genesis_sanity_check` must pass

// use croaring::Bitmap;
// use tari_mmr::{
// BaseLayerKernelMmrHasherBlake256,
// BaseLayerOutputMmrHasherBlake256,
// BaseLayerWitnessMmrHasherBlake256,
// MerkleMountainRange,
// MutableMmr,
// };
//
// let mut kernel_mmr = MerkleMountainRange::<BaseLayerKernelMmrHasherBlake256, _>::new(Vec::new());
// use crate::{BaseLayerKernelMmr, BaseLayerOutputMutableMmr, BaseLayerWitnessMmr};
//
// let mut kernel_mmr = BaseLayerKernelMmr::new(Vec::new());
// for k in block.body.kernels() {
// println!("k: {}", k);
// kernel_mmr.push(k.hash()).unwrap();
// }
//
// let mut witness_mmr = MerkleMountainRange::<BaseLayerWitnessMmrHasherBlake256, _>::new(Vec::new());
// let mut output_mmr = MutableMmr::<BaseLayerOutputMmrHasherBlake256, _>::new(Vec::new(),
// Bitmap::create()).unwrap();
// let mut witness_mmr = BaseLayerWitnessMmr::new(Vec::new());
// let mut output_mmr = BaseLayerOutputMutableMmr::new(Vec::new(), Bitmap::create()).unwrap();
//
// for o in block.body.outputs() {
// witness_mmr.push(o.witness_hash()).unwrap();
Expand Down Expand Up @@ -358,20 +351,16 @@ mod test {

use croaring::Bitmap;
use tari_common_types::types::Commitment;
use tari_mmr::{
BaseLayerKernelMmrHasherBlake256,
BaseLayerOutputMmrHasherBlake256,
BaseLayerWitnessMmrHasherBlake256,
MerkleMountainRange,
MutableMmr,
};

use super::*;
use crate::{
consensus::ConsensusManager,
test_helpers::blockchain::create_new_blockchain_with_network,
transactions::{transaction_components::transaction_output::batch_verify_range_proofs, CryptoFactories},
validation::{ChainBalanceValidator, FinalHorizonStateValidation},
KernelMmr,
MutableOutputMmr,
WitnessMmr,
};

#[test]
Expand Down Expand Up @@ -406,14 +395,13 @@ mod test {
.any(|k| k.features.contains(KernelFeatures::COINBASE_KERNEL)));

// Check MMR
let mut kernel_mmr = MerkleMountainRange::<BaseLayerKernelMmrHasherBlake256, _>::new(Vec::new());
let mut kernel_mmr = KernelMmr::new(Vec::new());
for k in block.block().body.kernels() {
kernel_mmr.push(k.hash()).unwrap();
}

let mut witness_mmr = MerkleMountainRange::<BaseLayerWitnessMmrHasherBlake256, _>::new(Vec::new());
let mut output_mmr =
MutableMmr::<BaseLayerOutputMmrHasherBlake256, _>::new(Vec::new(), Bitmap::create()).unwrap();
let mut witness_mmr = WitnessMmr::new(Vec::new());
let mut output_mmr = MutableOutputMmr::new(Vec::new(), Bitmap::create()).unwrap();

for o in block.block().body.outputs() {
o.verify_metadata_signature().unwrap();
Expand Down Expand Up @@ -473,14 +461,13 @@ mod test {
.any(|k| k.features.contains(KernelFeatures::COINBASE_KERNEL)));

// Check MMR
let mut kernel_mmr = MerkleMountainRange::<BaseLayerKernelMmrHasherBlake256, _>::new(Vec::new());
let mut kernel_mmr = KernelMmr::new(Vec::new());
for k in block.block().body.kernels() {
kernel_mmr.push(k.hash()).unwrap();
}

let mut witness_mmr = MerkleMountainRange::<BaseLayerWitnessMmrHasherBlake256, _>::new(Vec::new());
let mut output_mmr =
MutableMmr::<BaseLayerOutputMmrHasherBlake256, _>::new(Vec::new(), Bitmap::create()).unwrap();
let mut witness_mmr = WitnessMmr::new(Vec::new());
let mut output_mmr = MutableOutputMmr::new(Vec::new(), Bitmap::create()).unwrap();
assert_eq!(block.block().body.kernels().len(), 1);
assert_eq!(block.block().body.outputs().len(), 1);
for o in block.block().body.outputs() {
Expand Down
22 changes: 9 additions & 13 deletions base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,7 @@ use tari_common_types::{
chain_metadata::ChainMetadata,
types::{BlockHash, Commitment, FixedHash, HashOutput, PublicKey, Signature},
};
use tari_mmr::{
pruned_hashset::PrunedHashSet,
BaseLayerInputMmrHasherBlake256,
BaseLayerKernelMmrHasherBlake256,
BaseLayerOutputMmrHasherBlake256,
BaseLayerWitnessMmrHasherBlake256,
MerkleMountainRange,
MutableMmr,
};
use tari_mmr::pruned_hashset::PrunedHashSet;
use tari_utilities::{epoch_time::EpochTime, hex::Hex, ByteArray, Hashable};

use crate::{
Expand Down Expand Up @@ -95,6 +87,10 @@ use crate::{
OrphanValidation,
PostOrphanBodyValidation,
},
MutablePrunedOutputMmr,
PrunedInputMmr,
PrunedKernelMmr,
PrunedWitnessMmr,
};

const LOG_TARGET: &str = "c::cs::database";
Expand Down Expand Up @@ -1259,10 +1255,10 @@ pub fn calculate_mmr_roots<T: BlockchainBackend>(db: &T, block: &Block) -> Resul
value: header.prev_hash.to_hex(),
})?;

let mut kernel_mmr = MerkleMountainRange::<BaseLayerKernelMmrHasherBlake256, _>::new(kernels);
let mut output_mmr = MutableMmr::<BaseLayerOutputMmrHasherBlake256, _>::new(outputs, deleted)?;
let mut witness_mmr = MerkleMountainRange::<BaseLayerWitnessMmrHasherBlake256, _>::new(range_proofs);
let mut input_mmr = MerkleMountainRange::<BaseLayerInputMmrHasherBlake256, _>::new(PrunedHashSet::default());
let mut kernel_mmr = PrunedKernelMmr::new(kernels);
let mut output_mmr = MutablePrunedOutputMmr::new(outputs, deleted)?;
let mut witness_mmr = PrunedWitnessMmr::new(range_proofs);
let mut input_mmr = PrunedInputMmr::new(PrunedHashSet::default());
let mut deleted_outputs = Vec::new();

for kernel in body.kernels().iter() {
Expand Down
19 changes: 7 additions & 12 deletions base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,7 @@ use tari_common_types::{
types::{BlockHash, Commitment, FixedHash, HashOutput, PublicKey, Signature, BLOCK_HASH_LENGTH},
};
use tari_crypto::hash::blake2::Blake256;
use tari_mmr::{
BaseLayerKernelMmrHasherBlake256,
BaseLayerOutputMmrHasherBlake256,
BaseLayerWitnessMmrHasherBlake256,
Hash,
MerkleMountainRange,
MutableMmr,
};
use tari_mmr::Hash;
use tari_storage::lmdb_store::{db, LMDBBuilder, LMDBConfig, LMDBStore};
use tari_utilities::{
hash::Hashable,
Expand Down Expand Up @@ -125,6 +118,9 @@ use crate::{
TransactionOutput,
},
},
MutablePrunedOutputMmr,
PrunedKernelMmr,
PrunedWitnessMmr,
};

type DatabaseRef = Arc<Database<'static>>;
Expand Down Expand Up @@ -1289,7 +1285,7 @@ impl LMDBDatabase {
..
} = data;

let mut kernel_mmr = MerkleMountainRange::<BaseLayerKernelMmrHasherBlake256, _>::new(pruned_kernel_set);
let mut kernel_mmr = PrunedKernelMmr::new(pruned_kernel_set);

for kernel in kernels {
total_kernel_sum = &total_kernel_sum + &kernel.excess;
Expand All @@ -1304,9 +1300,8 @@ impl LMDBDatabase {
})?;
self.insert_kernel(txn, &block_hash, &kernel, pos)?;
}
let mut output_mmr =
MutableMmr::<BaseLayerOutputMmrHasherBlake256, _>::new(pruned_output_set, Bitmap::create())?;
let mut witness_mmr = MerkleMountainRange::<BaseLayerWitnessMmrHasherBlake256, _>::new(pruned_proof_set);
let mut output_mmr = MutablePrunedOutputMmr::new(pruned_output_set, Bitmap::create())?;
let mut witness_mmr = PrunedWitnessMmr::new(pruned_proof_set);

let leaf_count = witness_mmr.get_leaf_count()?;

Expand Down
38 changes: 38 additions & 0 deletions base_layer/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,41 @@ pub mod large_ints {
}

pub use large_ints::{U256, U512};
use tari_crypto::{hash::blake2::Blake256, hash_domain, hashing::DomainSeparatedHasher};
use tari_mmr::{pruned_hashset::PrunedHashSet, Hash, MerkleMountainRange, MutableMmr};

hash_domain!(
KernelMmrHashDomain,
"com.tari.tari_project.base_layer.core.kernel_mmr",
1
);
pub type KernelMmrHasherBlake256 = DomainSeparatedHasher<Blake256, KernelMmrHashDomain>;
pub type KernelMmr = MerkleMountainRange<KernelMmrHasherBlake256, Vec<Hash>>;
pub type PrunedKernelMmr = MerkleMountainRange<KernelMmrHasherBlake256, PrunedHashSet>;

hash_domain!(
WitnessMmrHashDomain,
"com.tari.tari_project.base_layer.core.witness_mmr",
1
);
pub type WitnessMmrHasherBlake256 = DomainSeparatedHasher<Blake256, WitnessMmrHashDomain>;
pub type WitnessMmr = MerkleMountainRange<WitnessMmrHasherBlake256, Vec<Hash>>;
pub type PrunedWitnessMmr = MerkleMountainRange<WitnessMmrHasherBlake256, PrunedHashSet>;

hash_domain!(
OutputMmrHashDomain,
"com.tari.tari_project.base_layer.core.output_mmr",
1
);
pub type OutputMmrHasherBlake256 = DomainSeparatedHasher<Blake256, OutputMmrHashDomain>;
pub type MutableOutputMmr = MutableMmr<OutputMmrHasherBlake256, Vec<Hash>>;
pub type PrunedOutputMmr = MerkleMountainRange<OutputMmrHasherBlake256, PrunedHashSet>;
pub type MutablePrunedOutputMmr = MutableMmr<OutputMmrHasherBlake256, PrunedHashSet>;

hash_domain!(
InputMmrHashDomain,
"com.tari.tari_project.base_layer.core.output_mmr",
1
);
pub type InputMmrHasherBlake256 = DomainSeparatedHasher<Blake256, InputMmrHashDomain>;
pub type PrunedInputMmr = MerkleMountainRange<InputMmrHasherBlake256, PrunedHashSet>;
28 changes: 15 additions & 13 deletions base_layer/core/tests/helpers/block_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,17 @@ use tari_core::{
},
CryptoFactories,
},
KernelMmr,
KernelMmrHasherBlake256,
MutableOutputMmr,
WitnessMmr,
WitnessMmrHasherBlake256,
};
use tari_crypto::{
keys::PublicKey as PublicKeyTrait,
tari_utilities::{hash::Hashable, hex::Hex},
};
use tari_mmr::{
BaseLayerKernelMmrHasherBlake256,
BaseLayerOutputMmrHasherBlake256,
BaseLayerWitnessMmrHasherBlake256,
MerkleMountainRange,
MutableMmr,
};
use tari_mmr::{Hash, MutableMmr};
use tari_script::script;

pub fn create_coinbase(
Expand Down Expand Up @@ -156,12 +155,12 @@ fn print_new_genesis_block(network: Network) {
.build()
.unwrap();

let mut kernel_mmr = MerkleMountainRange::<BaseLayerKernelMmrHasherBlake256, _>::new(Vec::new());
let mut kernel_mmr = KernelMmr::new(Vec::new());
kernel_mmr.push(kernel.hash()).unwrap();

let mut witness_mmr = MerkleMountainRange::<BaseLayerWitnessMmrHasherBlake256, _>::new(Vec::new());
let mut witness_mmr = WitnessMmr::new(Vec::new());
witness_mmr.push(utxo.witness_hash()).unwrap();
let mut output_mmr = MutableMmr::<BaseLayerOutputMmrHasherBlake256, _>::new(Vec::new(), Bitmap::create()).unwrap();
let mut output_mmr = MutableOutputMmr::new(Vec::new(), Bitmap::create()).unwrap();
output_mmr.push(utxo.hash()).unwrap();

header.kernel_mr = kernel_mmr.get_merkle_root().unwrap();
Expand Down Expand Up @@ -240,6 +239,9 @@ pub fn create_genesis_block(

// Calculate the MMR Merkle roots for the genesis block template and update the header.
fn update_genesis_block_mmr_roots(template: NewBlockTemplate) -> Result<Block, ChainStorageError> {
type BaseLayerKernelMutableMmr = MutableMmr<KernelMmrHasherBlake256, Vec<Hash>>;
type BaseLayerWitnessMutableMmr = MutableMmr<WitnessMmrHasherBlake256, Vec<Hash>>;

let NewBlockTemplate { header, mut body, .. } = template;
// Make sure the body components are sorted. If they already are, this is a very cheap call.
body.sort();
Expand All @@ -248,13 +250,13 @@ fn update_genesis_block_mmr_roots(template: NewBlockTemplate) -> Result<Block, C
let rp_hashes: Vec<HashOutput> = body.outputs().iter().map(|out| out.witness_hash()).collect();

let mut header = BlockHeader::from(header);
header.kernel_mr = MutableMmr::<BaseLayerKernelMmrHasherBlake256, _>::new(kernel_hashes, Bitmap::create())
header.kernel_mr = BaseLayerKernelMutableMmr::new(kernel_hashes, Bitmap::create())
.unwrap()
.get_merkle_root()?;
header.output_mr = MutableMmr::<BaseLayerOutputMmrHasherBlake256, _>::new(out_hashes, Bitmap::create())
header.output_mr = MutableOutputMmr::new(out_hashes, Bitmap::create())
.unwrap()
.get_merkle_root()?;
header.witness_mr = MutableMmr::<BaseLayerWitnessMmrHasherBlake256, _>::new(rp_hashes, Bitmap::create())
header.witness_mr = BaseLayerWitnessMutableMmr::new(rp_hashes, Bitmap::create())
.unwrap()
.get_merkle_root()?;
Ok(Block { header, body })
Expand Down
Loading

0 comments on commit 4427dab

Please sign in to comment.