Skip to content

Commit

Permalink
add network to vn node hash and block
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Jan 29, 2024
1 parent 50adef4 commit a398345
Show file tree
Hide file tree
Showing 36 changed files with 202 additions and 41 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use tari_base_node_client::{
BaseNodeClient,
BaseNodeClientError,
};
use tari_common::configuration::Network;
use tari_common_types::types::{Commitment, FixedHash, FixedHashSizeError};
use tari_core::transactions::transaction_components::{
CodeTemplateRegistration,
Expand Down Expand Up @@ -63,6 +64,7 @@ use crate::{
const LOG_TARGET: &str = "tari::dan::base_layer_scanner";

pub fn spawn<TAddr: NodeAddressable + 'static>(
network: Network,
global_db: GlobalDb<SqliteGlobalDbAdapter<TAddr>>,
base_node_client: GrpcBaseNodeClient,
epoch_manager: EpochManagerHandle<TAddr>,
Expand All @@ -75,6 +77,7 @@ pub fn spawn<TAddr: NodeAddressable + 'static>(
) -> JoinHandle<anyhow::Result<()>> {
task::spawn(async move {
let base_layer_scanner = BaseLayerScanner::new(
network,
global_db,
base_node_client,
epoch_manager,
Expand All @@ -92,6 +95,7 @@ pub fn spawn<TAddr: NodeAddressable + 'static>(
}

pub struct BaseLayerScanner<TAddr> {
network: Network,
global_db: GlobalDb<SqliteGlobalDbAdapter<TAddr>>,
last_scanned_height: u64,
last_scanned_tip: Option<FixedHash>,
Expand All @@ -110,6 +114,7 @@ pub struct BaseLayerScanner<TAddr> {

impl<TAddr: NodeAddressable + 'static> BaseLayerScanner<TAddr> {
pub fn new(
network: Network,
global_db: GlobalDb<SqliteGlobalDbAdapter<TAddr>>,
base_node_client: GrpcBaseNodeClient,
epoch_manager: EpochManagerHandle<TAddr>,
Expand All @@ -121,6 +126,7 @@ impl<TAddr: NodeAddressable + 'static> BaseLayerScanner<TAddr> {
base_layer_scanning_interval: Duration,
) -> Self {
Self {
network,
global_db,
last_scanned_tip: None,
last_scanned_height: 0,
Expand Down Expand Up @@ -374,7 +380,7 @@ impl<TAddr: NodeAddressable + 'static> BaseLayerScanner<TAddr> {
});
self.state_store
.with_write_tx(|tx| {
let genesis = Block::genesis();
let genesis = Block::genesis(self.network);

// TODO: This should be proposed in a block...
SubstateRecord {
Expand Down
2 changes: 2 additions & 0 deletions applications/tari_indexer/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub async fn spawn_services(
// Epoch manager
let validator_node_client_factory = TariValidatorNodeRpcClientFactory::new(networking.clone());
let (epoch_manager, _) = tari_epoch_manager::base_layer::spawn_service(
config.network,
EpochManagerConfig {
base_layer_confirmations: consensus_constants.base_layer_confirmations,
committee_size: consensus_constants.committee_size,
Expand All @@ -129,6 +130,7 @@ pub async fn spawn_services(

// Base Node scanner
base_layer_scanner::spawn(
config.network,
global_db,
base_node_client.clone(),
epoch_manager.clone(),
Expand Down
14 changes: 10 additions & 4 deletions applications/tari_validator_node/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ use serde::Serialize;
use sqlite_message_logger::SqliteMessageLogger;
use tari_base_node_client::grpc::GrpcBaseNodeClient;
use tari_common::{
configuration::bootstrap::{grpc_default_port, ApplicationType},
configuration::{
bootstrap::{grpc_default_port, ApplicationType},
Network,
},
exit_codes::{ExitCode, ExitError},
};
use tari_common_types::types::PublicKey;
Expand Down Expand Up @@ -182,10 +185,11 @@ pub async fn spawn_services(
// Connect to shard db
let state_store =
SqliteStateStore::connect(&format!("sqlite://{}", config.validator_node.state_db_path().display()))?;
state_store.with_write_tx(|tx| bootstrap_state(tx))?;
state_store.with_write_tx(|tx| bootstrap_state(tx, config.network))?;

// Epoch manager
let (epoch_manager, join_handle) = tari_epoch_manager::base_layer::spawn_service(
config.network,
// TODO: We should be able to pass consensus constants here. However, these are currently located in dan_core
// which depends on epoch_manager, so would be a circular dependency.
EpochManagerConfig {
Expand Down Expand Up @@ -238,6 +242,7 @@ pub async fn spawn_services(
ConsensusOutboundMessaging::new(loopback_sender, networking.clone(), message_logger.clone());

let (consensus_join_handle, consensus_handle, rx_consensus_to_mempool) = consensus::spawn(
config.network,
state_store.clone(),
keypair.clone(),
epoch_manager.clone(),
Expand Down Expand Up @@ -291,6 +296,7 @@ pub async fn spawn_services(

// Base Node scanner
let join_handle = base_layer_scanner::spawn(
config.network,
global_db.clone(),
base_node_client.clone(),
epoch_manager.clone(),
Expand Down Expand Up @@ -433,13 +439,13 @@ async fn spawn_p2p_rpc(
}

// TODO: Figure out the best way to have the engine shard store mirror these bootstrapped states.
fn bootstrap_state<TTx>(tx: &mut TTx) -> Result<(), StorageError>
fn bootstrap_state<TTx>(tx: &mut TTx, network: Network) -> Result<(), StorageError>
where
TTx: StateStoreWriteTransaction + DerefMut,
TTx::Target: StateStoreReadTransaction,
TTx::Addr: NodeAddressable + Serialize,
{
let genesis_block = Block::genesis();
let genesis_block = Block::genesis(network);
let substate_id = SubstateId::Resource(PUBLIC_IDENTITY_RESOURCE_ADDRESS);
let substate_address = SubstateAddress::from_address(&substate_id, 0);
let mut metadata: Metadata = Default::default();
Expand Down
5 changes: 4 additions & 1 deletion applications/tari_validator_node/src/consensus/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2023 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use tari_common::configuration::Network;
use tari_consensus::hotstuff::{ConsensusWorker, ConsensusWorkerContext, HotstuffWorker};
use tari_dan_storage::consensus_models::TransactionPool;
use tari_epoch_manager::base_layer::EpochManagerHandle;
Expand Down Expand Up @@ -38,6 +39,7 @@ use tari_rpc_state_sync::RpcStateSyncManager;
use crate::p2p::services::messaging::{ConsensusInboundMessaging, ConsensusOutboundMessaging};

pub async fn spawn(
network: Network,
store: SqliteStateStore<PeerAddress>,
keypair: RistrettoKeypair,
epoch_manager: EpochManagerHandle<PeerAddress>,
Expand All @@ -62,6 +64,7 @@ pub async fn spawn(

let hotstuff_worker = HotstuffWorker::<TariConsensusSpec>::new(
validator_addr,
network,
inbound_messaging,
outbound_messaging,
rx_new_transactions,
Expand All @@ -80,7 +83,7 @@ pub async fn spawn(
let context = ConsensusWorkerContext {
epoch_manager: epoch_manager.clone(),
hotstuff: hotstuff_worker,
state_sync: RpcStateSyncManager::new(epoch_manager, store, leader_strategy, client_factory),
state_sync: RpcStateSyncManager::new(network, epoch_manager, store, leader_strategy, client_factory),
tx_current_state,
};

Expand Down
2 changes: 2 additions & 0 deletions dan_layer/common_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ repository.workspace = true
license.workspace = true

[dependencies]
tari_common = { workspace = true }
tari_common_types = { workspace = true }
tari_crypto = { workspace = true, features = ["borsh"] }
tari_engine_types = { workspace = true }
tari_hash_domains = { workspace = true }
tari_bor = { workspace = true, default-features = true }
tari_mmr = { workspace = true }
tari_core = { workspace = true, default-features = false, features = [
Expand Down
7 changes: 1 addition & 6 deletions dan_layer/common_types/src/hashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use blake2::{digest::consts::U32, Blake2b};
use tari_crypto::{hash_domain, hashing::DomainSeparatedHasher};
use tari_hash_domains::ValidatorNodeBmtHashDomain;
use tari_mmr::{BalancedBinaryMerkleProof, BalancedBinaryMerkleTree, MergedBalancedBinaryMerkleProof};

use crate::hasher::{tari_hasher, TariHasher};
Expand Down Expand Up @@ -55,12 +56,6 @@ fn dan_hasher(label: &'static str) -> TariHasher {
tari_hasher::<TariDanConsensusHashDomain>(label)
}

// From tari_core
hash_domain!(
ValidatorNodeBmtHashDomain,
"com.tari.base_layer.core.validator_node_mmr",
1
);
pub type ValidatorNodeBmtHasherBlake2b = DomainSeparatedHasher<Blake2b<U32>, ValidatorNodeBmtHashDomain>;
pub type ValidatorNodeBalancedMerkleTree = BalancedBinaryMerkleTree<ValidatorNodeBmtHasherBlake2b>;
pub type ValidatorNodeMerkleProof = BalancedBinaryMerkleProof<ValidatorNodeBmtHasherBlake2b>;
Expand Down
5 changes: 3 additions & 2 deletions dan_layer/common_types/src/validator_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use blake2::{digest::consts::U32, Blake2b};
use serde::{Deserialize, Serialize};
use tari_common::configuration::Network;
use tari_common_types::types::{FixedHash, PublicKey, Signature};
use tari_core::{consensus::DomainSeparatedConsensusHasher, transactions::TransactionHashDomain};

Expand All @@ -25,8 +26,8 @@ impl ValidatorMetadata {
}
}

pub fn vn_node_hash(public_key: &PublicKey, substate_address: &SubstateAddress) -> FixedHash {
DomainSeparatedConsensusHasher::<TransactionHashDomain, Blake2b<U32>>::new("validator_node")
pub fn vn_node_hash(network: Network, public_key: &PublicKey, substate_address: &SubstateAddress) -> FixedHash {
DomainSeparatedConsensusHasher::<TransactionHashDomain, Blake2b<U32>>::new_with_network("validator_node", network)
.chain(public_key)
.chain(&substate_address.0)
.finalize()
Expand Down
3 changes: 2 additions & 1 deletion dan_layer/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ tari_dan_storage = { workspace = true }
tari_transaction = { workspace = true }
tari_epoch_manager = { workspace = true }

# Used for PublicKey and Signature
# Used for PublicKey and Signature and Network enum
tari_common = { workspace = true }
tari_common_types = { workspace = true }
tari_mmr = { workspace = true }
tari_shutdown = { workspace = true }
Expand Down
14 changes: 13 additions & 1 deletion dan_layer/consensus/src/block_validations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2023 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use tari_common::configuration::Network;
use tari_dan_common_types::{committee::Committee, DerivableFromPublicKey};
use tari_dan_storage::consensus_models::Block;
use tari_epoch_manager::EpochManagerReader;
Expand All @@ -10,6 +11,17 @@ use crate::{
traits::{ConsensusSpec, LeaderStrategy, VoteSignatureService},
};

pub fn check_network(candidate_block: &Block, network: Network) -> Result<(), ProposalValidationError> {
if candidate_block.network() != network {
return Err(ProposalValidationError::InvalidNetwork {
block_network: candidate_block.network().to_string(),
expected_network: network.to_string(),
block_id: *candidate_block.id(),
});
}
Ok(())
}

pub fn check_hash_and_height(candidate_block: &Block) -> Result<(), ProposalValidationError> {
if candidate_block.height().is_zero() || candidate_block.is_genesis() {
return Err(ProposalValidationError::ProposingGenesisBlock {
Expand Down Expand Up @@ -91,7 +103,7 @@ pub async fn check_quorum_certificate<TConsensusSpec: ConsensusSpec>(
let vn = epoch_manager
.get_validator_node_by_public_key(candidate_block.justify().epoch(), signature.public_key())
.await?;
vns.push(vn.node_hash());
vns.push(vn.get_node_hash(candidate_block.network()));
}
let merkle_root = epoch_manager
.get_validator_node_merkle_root(candidate_block.justify().epoch())
Expand Down
3 changes: 3 additions & 0 deletions dan_layer/consensus/src/hotstuff/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: BSD-3-Clause

use log::*;
use tari_common::configuration::Network;
use tari_dan_common_types::{committee::Committee, Epoch, NodeAddressable, NodeHeight};
use tari_dan_storage::consensus_models::{Block, QuorumCertificate};

Expand All @@ -15,6 +16,7 @@ const LOG_TARGET: &str = "tari::dan::consensus::hotstuff::common";
pub const EXHAUST_DIVISOR: u64 = 0;

pub fn calculate_dummy_blocks<TAddr: NodeAddressable, TLeaderStrategy: LeaderStrategy<TAddr>>(
network: Network,
epoch: Epoch,
high_qc: &QuorumCertificate,
new_height: NodeHeight,
Expand Down Expand Up @@ -44,6 +46,7 @@ pub fn calculate_dummy_blocks<TAddr: NodeAddressable, TLeaderStrategy: LeaderStr
loop {
let leader = leader_strategy.get_leader_public_key(local_committee, current_height);
let dummy_block = Block::dummy_block(
network,
*parent_block.block_id(),
leader.clone(),
current_height,
Expand Down
6 changes: 6 additions & 0 deletions dan_layer/consensus/src/hotstuff/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,10 @@ pub enum ProposalValidationError {
QuorumWasNotReached { qc: QuorumCertificate },
#[error("Merkle proof error: {0}")]
BalancedBinaryMerkleProofError(#[from] BalancedBinaryMerkleProofError),
#[error("Invalid network in block {block_id}: expected {expected_network}, given {block_network}")]
InvalidNetwork {
expected_network: String,
block_network: String,
block_id: BlockId,
},
}
13 changes: 12 additions & 1 deletion dan_layer/consensus/src/hotstuff/on_inbound_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
};

use log::*;
use tari_common::configuration::Network;
use tari_dan_common_types::{NodeAddressable, NodeHeight};
use tari_dan_storage::{
consensus_models::{Block, TransactionRecord},
Expand All @@ -18,7 +19,13 @@ use tari_transaction::TransactionId;
use tokio::{sync::mpsc, time};

use crate::{
block_validations::{check_hash_and_height, check_proposed_by_leader, check_quorum_certificate, check_signature},
block_validations::{
check_hash_and_height,
check_network,
check_proposed_by_leader,
check_quorum_certificate,
check_signature,
},
hotstuff::error::HotStuffError,
messages::{HotstuffMessage, ProposalMessage, RequestMissingTransactionsMessage},
traits::{ConsensusSpec, OutboundMessaging},
Expand All @@ -29,6 +36,7 @@ const LOG_TARGET: &str = "tari::dan::consensus::hotstuff::inbound_messages";
pub type IncomingMessageResult<TAddr> = Result<Option<(TAddr, HotstuffMessage)>, NeedsSync<TAddr>>;

pub struct OnInboundMessage<TConsensusSpec: ConsensusSpec> {
network: Network,
store: TConsensusSpec::StateStore,
epoch_manager: TConsensusSpec::EpochManager,
leader_strategy: TConsensusSpec::LeaderStrategy,
Expand All @@ -42,6 +50,7 @@ impl<TConsensusSpec> OnInboundMessage<TConsensusSpec>
where TConsensusSpec: ConsensusSpec
{
pub fn new(
network: Network,
store: TConsensusSpec::StateStore,
epoch_manager: TConsensusSpec::EpochManager,
leader_strategy: TConsensusSpec::LeaderStrategy,
Expand All @@ -50,6 +59,7 @@ where TConsensusSpec: ConsensusSpec
) -> Self {
let (tx_msg_ready, rx_msg_ready) = mpsc::unbounded_channel();
Self {
network,
store,
epoch_manager,
leader_strategy,
Expand Down Expand Up @@ -97,6 +107,7 @@ where TConsensusSpec: ConsensusSpec
}

async fn check_proposal(&mut self, block: Block) -> Result<Option<Block>, HotStuffError> {
check_network(&block, self.network)?;
check_hash_and_height(&block)?;
let committee_for_block = self
.epoch_manager
Expand Down
Loading

0 comments on commit a398345

Please sign in to comment.