Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(base_layer/core): fixes incorrect validator node merkle root calculation #5005

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2022, The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use anyhow::Error;
use async_trait::async_trait;
use clap::Parser;
use tari_common_types::{epoch::VnEpoch, types::PublicKey};
use tari_utilities::hex::to_hex;

use super::{CommandContext, HandleCommand};
use crate::table::Table;

/// Lists the peer connections currently held by this node
#[derive(Debug, Parser)]
pub struct Args {
epoch: Option<VnEpoch>,
}

#[async_trait]
impl HandleCommand<Args> for CommandContext {
async fn handle_command(&mut self, args: Args) -> Result<(), Error> {
self.list_validator_nodes(args).await
}
}

impl CommandContext {
async fn print_validator_nodes_list(&mut self, vns: &[(PublicKey, [u8; 32])]) {
let num_vns = vns.len();
let mut table = Table::new();
table.set_titles(vec!["Public Key", "Shard ID"]);
for (public_key, shard_key) in vns {
table.add_row(row![public_key, to_hex(shard_key),]);
}

table.print_stdout();

println!();
println!("{} active validator(s)", num_vns);
}
}

impl CommandContext {
/// Function to process the list-connections command
pub async fn list_validator_nodes(&mut self, args: Args) -> Result<(), Error> {
let metadata = self.blockchain_db.get_chain_metadata().await?;
let constants = self
.consensus_rules
.consensus_constants(metadata.height_of_longest_chain());
let height = args
.epoch
.map(|epoch| constants.epoch_to_block_height(epoch))
.unwrap_or_else(|| metadata.height_of_longest_chain());
let vns = self.blockchain_db.fetch_active_validator_nodes(height).await?;

println!();
println!(
"Registered validator nodes for epoch {}",
constants.block_height_to_epoch(height).as_u64()
);
println!("----------------------------------");
if vns.is_empty() {
println!("No active validator nodes.");
} else {
println!();
self.print_validator_nodes_list(&vns).await;
}
Ok(())
}
}
4 changes: 4 additions & 0 deletions applications/tari_base_node/src/commands/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ mod list_connections;
mod list_headers;
mod list_peers;
mod list_reorgs;
mod list_validator_nodes;
mod period_stats;
mod ping_peer;
mod quit;
Expand Down Expand Up @@ -131,6 +132,7 @@ pub enum Command {
Whoami(whoami::Args),
GetStateInfo(get_state_info::Args),
GetNetworkStats(get_network_stats::Args),
ListValidatorNodes(list_validator_nodes::Args),
Quit(quit::Args),
Exit(quit::Args),
Watch(watch_command::Args),
Expand Down Expand Up @@ -225,6 +227,7 @@ impl CommandContext {
Command::GetMempoolTx(_) |
Command::Status(_) |
Command::Watch(_) |
Command::ListValidatorNodes(_) |
Command::Quit(_) |
Command::Exit(_) => 30,
// These commands involve intense blockchain db operations and needs a lot of time to complete
Expand Down Expand Up @@ -289,6 +292,7 @@ impl HandleCommand<Command> for CommandContext {
Command::ListBannedPeers(args) => self.handle_command(args).await,
Command::Quit(args) | Command::Exit(args) => self.handle_command(args).await,
Command::Watch(args) => self.handle_command(args).await,
Command::ListValidatorNodes(args) => self.handle_command(args).await,
}
}
}
Expand Down
34 changes: 23 additions & 11 deletions base_layer/common_types/src/epoch.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
// the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
// following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.

use std::str::FromStr;

use newtype_ops::newtype_ops;
use serde::{Deserialize, Serialize};

Expand All @@ -42,3 +46,11 @@ impl VnEpoch {
newtype_ops! { [VnEpoch] {add sub mul div} {:=} Self Self }
newtype_ops! { [VnEpoch] {add sub mul div} {:=} &Self &Self }
newtype_ops! { [VnEpoch] {add sub mul div} {:=} Self &Self }

impl FromStr for VnEpoch {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(VnEpoch(s.parse::<u64>().map_err(|e| e.to_string())?))
}
}
2 changes: 1 addition & 1 deletion base_layer/core/src/base_node/sync/header_sync/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub enum BlockHeaderSyncError {
#[error(
"Validator node MMR at height {height} is not correct. Expected {actual} to equal the computed {computed}"
)]
ValidatorNodeMmmr {
ValidatorNodeMmr {
height: u64,
actual: String,
computed: String,
Expand Down
3 changes: 2 additions & 1 deletion base_layer/core/src/blocks/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,15 @@ impl Display for BlockHeader {
)?;
writeln!(
fmt,
"Merkle roots:\nInputs: {},\nOutputs: {} ({})\nWitness: {}\nKernels: {} ({})\n",
"Merkle roots:\nInputs: {},\nOutputs: {} ({})\nWitness: {}\nKernels: {} ({})",
self.input_mr.to_hex(),
self.output_mr.to_hex(),
self.output_mmr_size,
self.witness_mr.to_hex(),
self.kernel_mr.to_hex(),
self.kernel_mmr_size
)?;
writeln!(fmt, "ValidatorNode: {}\n", self.validator_node_mr.to_hex())?;
writeln!(
fmt,
"Total offset: {}\nTotal script offset: {}\nNonce: {}\nProof of work:\n{}",
Expand Down
5 changes: 4 additions & 1 deletion base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,10 @@ pub fn calculate_validator_node_mr(
.to_vec()
}

let vn_mmr = ValidatorNodeMmr::new(validator_nodes.iter().map(hash_node).collect());
let mut vn_mmr = ValidatorNodeMmr::new(Vec::new());
for vn in validator_nodes {
vn_mmr.push(hash_node(vn))?;
}
let merkle_root = vn_mmr.get_merkle_root()?;
Ok(merkle_root)
}
Expand Down
54 changes: 9 additions & 45 deletions base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use log::*;
use serde::{Deserialize, Serialize};
use tari_common_types::{
chain_metadata::ChainMetadata,
epoch::VnEpoch,
types::{BlockHash, Commitment, HashOutput, PublicKey, Signature},
};
use tari_storage::lmdb_store::{db, LMDBBuilder, LMDBConfig, LMDBStore};
Expand Down Expand Up @@ -1409,10 +1410,13 @@ impl LMDBDatabase {
) -> Result<(), ChainStorageError> {
let store = self.validator_node_store(txn);
let constants = self.get_consensus_constants(header.height);
let current_epoch = constants.block_height_to_current_epoch(header.height);
let current_epoch = constants.block_height_to_epoch(header.height);

let prev_shard_key = store.get_shard_key(
(current_epoch.as_u64() - constants.validator_node_validity_period().as_u64()) * constants.epoch_length(),
current_epoch
.as_u64()
.saturating_sub(constants.validator_node_validity_period().as_u64()) *
constants.epoch_length(),
current_epoch.as_u64() * constants.epoch_length(),
vn_reg.public_key(),
)?;
Expand All @@ -1423,7 +1427,7 @@ impl LMDBDatabase {
&header.prev_hash,
);

let next_epoch = constants.block_height_to_next_epoch(header.height);
let next_epoch = constants.block_height_to_epoch(header.height) + VnEpoch(1);
let validator_node = ValidatorNodeEntry {
shard_key,
start_epoch: next_epoch,
Expand Down Expand Up @@ -2492,42 +2496,14 @@ impl BlockchainBackend for LMDBDatabase {
let constants = self.consensus_manager.consensus_constants(height);

// Get the current epoch for the height
let end_epoch = constants.block_height_to_current_epoch(height);
let end_epoch = constants.block_height_to_epoch(height);
// Subtract the registration validaty period to get the start epoch
let start_epoch = end_epoch.saturating_sub(constants.validator_node_validity_period());
// Convert these back to height as validators regs are indexed by height
let start_height = start_epoch.as_u64() * constants.epoch_length();
let end_height = end_epoch.as_u64() * constants.epoch_length();
let nodes = vn_store.get_vn_set(start_height, end_height)?;
Ok(nodes)

// let validator_node_validaty_period = constants.validator_node_validity_period();
// let mut pub_keys = HashMap::new();
//
// let end = height + validator_node_validaty_period;
// for h in height..end {
// lmdb_get_multiple(&txn, &self.validator_nodes_ending, &h.to_le_bytes())?
// .into_iter()
// .for_each(|v: ValidatorNodeEntry| {
// if v.from_height <= height {
// if let Some((shard_key, start)) =
// pub_keys.insert(v.public_key.clone(), (v.shard_key, v.from_height))
// {
// // If the node is already in the map, check if the start height is higher. If it is,
// replace // the old value with the new one.
// if start > v.from_height {
// pub_keys.insert(v.public_key, (shard_key, start));
// }
// }
// }
// });
// }
//
// // now remove the heights
// Ok(pub_keys
// .into_iter()
// .map(|(pk, (shard_key, _))| (pk, shard_key))
// .collect())
}

fn get_shard_key(&self, height: u64, public_key: PublicKey) -> Result<Option<[u8; 32]>, ChainStorageError> {
Expand All @@ -2536,24 +2512,12 @@ impl BlockchainBackend for LMDBDatabase {
let constants = self.get_consensus_constants(height);

// Get the epoch height boundaries for our query
let current_epoch = constants.block_height_to_current_epoch(height);
let current_epoch = constants.block_height_to_epoch(height);
let start_epoch = current_epoch.saturating_sub(constants.validator_node_validity_period());
let start_height = start_epoch.as_u64() * constants.epoch_length();
let end_height = current_epoch.as_u64() * constants.epoch_length();
let maybe_shard_id = store.get_shard_key(start_height, end_height, &public_key)?;
Ok(maybe_shard_id)

// let txn = self.read_transaction()?;
// let mut validator_nodes: Vec<ValidatorNodeEntry> =
// lmdb_fetch_matching_after(&txn, &self.validator_nodes, public_key.as_bytes())?;
// validator_nodes = validator_nodes
// .into_iter()
// .filter(|a| a.from_height <= height && height <= a.to_height)
// .collect();
// // get the last one
// validator_nodes.sort_by(|a, b| a.from_height.cmp(&b.from_height));
//
// Ok(validator_nodes.into_iter().map(|a| a.shard_key).last())
}

fn fetch_template_registrations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,8 @@ impl<'a, Txn: Deref<Target = ConstTransaction<'a>>> ValidatorNodeStore<'a, Txn>
let key = ShardIdIndexKey::try_from_parts(&[public_key.as_bytes(), &start_height.to_be_bytes()])
.expect("fetch_shard_key: Composite key length is incorrect");

// TODO: unused_assignments is a false positive, we might be able to remove this exclusion in future
#[allow(unused_assignments)]
let mut shard_key = None;
// Find the first entry at or above start_height
match cursor.seek_range::<ShardIdIndexKey>(key.as_bytes())? {
let mut shard_key = match cursor.seek_range::<ShardIdIndexKey>(key.as_bytes())? {
Some((key, s)) => {
if key[0..32] != *public_key.as_bytes() {
return Ok(None);
Expand All @@ -201,10 +198,10 @@ impl<'a, Txn: Deref<Target = ConstTransaction<'a>>> ValidatorNodeStore<'a, Txn>
if height > end_height {
return Ok(None);
}
shard_key = Some(s);
Some(s)
},
None => return Ok(None),
}
};

// If there are any subsequent entries less than the end height, use that instead.
while let Some((key, s)) = cursor.next::<ShardIdIndexKey>()? {
Expand Down
Loading