Skip to content

Commit

Permalink
DRY up vn_mmr calculation, add vn reg utxo validations
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Nov 17, 2022
1 parent 1ffab87 commit f21a76b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
23 changes: 12 additions & 11 deletions base_layer/core/src/blocks/genesis_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ pub fn get_esmeralda_genesis_block() -> ChainBlock {
//
// use croaring::Bitmap;
// use std::convert::TryFrom;
// use crate::{KernelMmr, MutableOutputMmr, WitnessMmr};
// use crate::{chain_storage::calculate_validator_node_mr, KernelMmr, MutableOutputMmr, WitnessMmr};

// let mut kernel_mmr = KernelMmr::new(Vec::new());
// for k in block.body.kernels() {
Expand All @@ -244,7 +244,7 @@ pub fn get_esmeralda_genesis_block() -> ChainBlock {
// }
// }

// let vn_mmr = ValidatorNodeMmr::new(Vec::new());
// let vn_mmr = calculate_validator_node_mr(&[]).unwrap();

// block.header.kernel_mr = FixedHash::try_from(kernel_mmr.get_merkle_root().unwrap()).unwrap();
// block.header.witness_mr = FixedHash::try_from(witness_mmr.get_merkle_root().unwrap()).unwrap();
Expand Down Expand Up @@ -362,13 +362,13 @@ mod test {

use super::*;
use crate::{
chain_storage::calculate_validator_node_mr,
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,
ValidatorNodeMmr,
WitnessMmr,
};

Expand Down Expand Up @@ -423,7 +423,7 @@ mod test {

let mut witness_mmr = WitnessMmr::new(Vec::new());
let mut output_mmr = MutableOutputMmr::new(Vec::new(), Bitmap::create()).unwrap();
let mut vn_mmr = ValidatorNodeMmr::new(Vec::new());
let mut vn_nodes = Vec::new();
for o in block.block().body.outputs() {
witness_mmr.push(o.witness_hash().to_vec()).unwrap();
output_mmr.push(o.hash().to_vec()).unwrap();
Expand All @@ -434,19 +434,20 @@ mod test {
.as_ref()
.and_then(|f| f.validator_node_registration())
.unwrap();
vn_mmr
.push(
reg.derive_shard_key(None, VnEpoch(0), VnEpoch(0), block.hash())
.to_vec(),
)
.unwrap();
vn_nodes.push((
reg.public_key.clone(),
reg.derive_shard_key(None, VnEpoch(0), VnEpoch(0), block.hash()),
));
}
}

assert_eq!(kernel_mmr.get_merkle_root().unwrap(), block.header().kernel_mr,);
assert_eq!(witness_mmr.get_merkle_root().unwrap(), block.header().witness_mr,);
assert_eq!(output_mmr.get_merkle_root().unwrap(), block.header().output_mr,);
assert_eq!(vn_mmr.get_merkle_root().unwrap(), block.header().validator_node_mr);
assert_eq!(
calculate_validator_node_mr(&vn_nodes).unwrap(),
block.header().validator_node_mr,
);

// Check that the faucet UTXOs balance (the faucet_value consensus constant is set correctly and faucet kernel
// is correct)
Expand Down
1 change: 1 addition & 0 deletions base_layer/core/src/chain_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub use block_add_result::BlockAddResult;
mod blockchain_database;
pub use blockchain_database::{
calculate_mmr_roots,
calculate_validator_node_mr,
fetch_header,
fetch_headers,
fetch_target_difficulty_for_next_block,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ impl<B: BlockchainBackend + 'static> BlockValidator<B> {
output.verify_metadata_signature()?;
output.verify_validator_node_signature()?;
helpers::check_not_duplicate_txo(&*db, output)?;
helpers::check_validator_node_registration_utxo(&constants, output)?;
commitment_sum = &commitment_sum + &output.commitment;
}
if !bypass_range_proof_verification {
Expand Down
2 changes: 2 additions & 0 deletions base_layer/core/src/validation/block_validators/orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::{
check_permitted_output_types,
check_sorting_and_duplicates,
check_total_burned,
check_validator_node_registration_utxo,
validate_versions,
},
OrphanValidation,
Expand Down Expand Up @@ -107,6 +108,7 @@ impl OrphanValidation for OrphanBlockValidator {
);
for output in block.body.outputs() {
check_permitted_output_types(constants, output)?;
check_validator_node_registration_utxo(constants, output)?;
}
trace!(target: LOG_TARGET, "SV - Permitted output type ok for {} ", &block_id);
check_total_burned(&block.body)?;
Expand Down
1 change: 1 addition & 0 deletions base_layer/core/src/validation/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ pub fn check_outputs<B: BlockchainBackend>(
check_permitted_output_types(constants, output)?;
check_tari_script_byte_size(&output.script, max_script_size)?;
check_not_duplicate_txo(db, output)?;
check_validator_node_registration_utxo(constants, output)?;
}
Ok(())
}
Expand Down
13 changes: 9 additions & 4 deletions base_layer/core/tests/helpers/block_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ use tari_common::configuration::Network;
use tari_common_types::types::{Commitment, FixedHash, PublicKey};
use tari_core::{
blocks::{Block, BlockHeader, BlockHeaderAccumulatedData, ChainBlock, ChainHeader, NewBlockTemplate},
chain_storage::{BlockAddResult, BlockchainBackend, BlockchainDatabase, ChainStorageError},
chain_storage::{
calculate_validator_node_mr,
BlockAddResult,
BlockchainBackend,
BlockchainDatabase,
ChainStorageError,
},
consensus::{emission::Emission, ConsensusConstants, ConsensusManager, ConsensusManagerBuilder},
covenants::Covenant,
proof_of_work::{sha3x_difficulty, AchievedTargetDifficulty, Difficulty},
Expand Down Expand Up @@ -58,7 +64,6 @@ use tari_core::{
KernelMmr,
KernelMmrHasherBlake256,
MutableOutputMmr,
ValidatorNodeMmr,
WitnessMmr,
WitnessMmrHasherBlake256,
};
Expand Down Expand Up @@ -160,14 +165,14 @@ fn print_new_genesis_block(network: Network) {
witness_mmr.push(utxo.witness_hash().to_vec()).unwrap();
let mut output_mmr = MutableOutputMmr::new(Vec::new(), Bitmap::create()).unwrap();
output_mmr.push(utxo.hash().to_vec()).unwrap();
let vn_mmr = ValidatorNodeMmr::new(Vec::new());
let vn_mr = calculate_validator_node_mr(&[]).unwrap();

header.kernel_mr = FixedHash::try_from(kernel_mmr.get_merkle_root().unwrap()).unwrap();
header.kernel_mmr_size += 1;
header.output_mr = FixedHash::try_from(output_mmr.get_merkle_root().unwrap()).unwrap();
header.witness_mr = FixedHash::try_from(witness_mmr.get_merkle_root().unwrap()).unwrap();
header.output_mmr_size += 1;
header.validator_node_mr = FixedHash::try_from(vn_mmr.get_merkle_root().unwrap()).unwrap();
header.validator_node_mr = FixedHash::try_from(vn_mr).unwrap();

// header.kernel_mr = kernel.hash();
// header.kernel_mmr_size += 1;
Expand Down

0 comments on commit f21a76b

Please sign in to comment.