Skip to content

Commit

Permalink
Network separation and protocol versioning implementation
Browse files Browse the repository at this point in the history
- Changes `WireMode::Comms` variant to contain a single byte.
- Allows the `WireMode::Comms` byte to be set from domain layer (tari_p2p).
- [partially network breaking] Removes redundant network specifier from messaging
  protocol.
- Removes DHT Network enum.
- Updates Network configuration enum to be representable as a byte.
- Network configuration enum is now the only Network specifier.
- Changes consensus Network enum to a struct (NetworkConsensus) that wraps
  the common Network enum.
- [network breaking] Use byte representation of multiaddr in identity
  message, rather than string. This reduces the bytes exchanged when
  establishing a new peer connection.
- [network breaking] Shave a few bytes off of identity and messaging protocol IDs
  to match the format of other protocols.
- [network breaking] Add major and minor versions to messaging protocol
- [network breaking] Add major version check when establishing a
  connection
- [network breaking] Remove NodeId from PeerIdentityMsg as this can be derived from the
  public key obtained from the noise handshake
  • Loading branch information
sdbondi committed Jul 2, 2021
1 parent 9b281ce commit 89769b2
Show file tree
Hide file tree
Showing 89 changed files with 725 additions and 733 deletions.
216 changes: 177 additions & 39 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion applications/tari_base_node/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ where B: BlockchainBackend + 'static

fn create_comms_config(&self) -> CommsConfig {
CommsConfig {
network: self.config.network,
node_identity: self.node_identity.clone(),
transport_type: self.create_transport_type(),
datastore_path: self.config.peer_db_path.clone(),
Expand All @@ -236,7 +237,6 @@ where B: BlockchainBackend + 'static
database_url: DbConnectionUrl::File(self.config.data_dir.join("dht.db")),
auto_join: true,
allow_test_addresses: self.config.allow_test_addresses,
network: self.config.network.into(),
flood_ban_max_msg_count: self.config.flood_ban_max_msg_count,
saf_msg_validity: self.config.saf_expiry_duration,
..Default::default()
Expand Down
4 changes: 2 additions & 2 deletions applications/tari_base_node/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use tari_comms_dht::Dht;
use tari_core::{
base_node::{state_machine_service::states::StatusInfo, LocalNodeCommsInterface, StateMachineHandle},
chain_storage::{create_lmdb_database, BlockchainDatabase, BlockchainDatabaseConfig, LMDBDatabase, Validators},
consensus::ConsensusManagerBuilder,
consensus::ConsensusManager,
mempool::{service::LocalMempoolService, Mempool, MempoolConfig},
proof_of_work::randomx_factory::{RandomXConfig, RandomXFactory},
transactions::types::CryptoFactories,
Expand Down Expand Up @@ -193,7 +193,7 @@ async fn build_node_context(
) -> Result<BaseNodeContext, anyhow::Error> {
//---------------------------------- Blockchain --------------------------------------------//

let rules = ConsensusManagerBuilder::new(config.network.into()).build();
let rules = ConsensusManager::builder(config.network).build();
let factories = CryptoFactories::default();
let randomx_factory = RandomXFactory::new(RandomXConfig::default(), config.max_randomx_vms);
let validators = Validators::new(
Expand Down
4 changes: 2 additions & 2 deletions applications/tari_base_node/src/command_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use tari_core::{
},
blocks::BlockHeader,
chain_storage::{async_db::AsyncBlockchainDb, ChainHeader, LMDBDatabase},
consensus::{ConsensusManager, Network},
consensus::ConsensusManager,
mempool::service::LocalMempoolService,
proof_of_work::PowAlgorithm,
tari_utilities::{hex::Hex, message_format::MessageFormat},
Expand Down Expand Up @@ -921,7 +921,7 @@ impl CommandHandler {

let start_height = cmp::max(start_height, 1);
let mut prev_header = try_or_print!(db.fetch_chain_header(start_height - 1).await);
let consensus_rules = ConsensusManager::builder(Network::from(network)).build();
let consensus_rules = ConsensusManager::builder(network).build();

writeln!(
output,
Expand Down
9 changes: 5 additions & 4 deletions applications/tari_base_node/src/grpc/base_node_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use tari_app_grpc::{
tari_rpc,
tari_rpc::{CalcType, Sorting},
};
use tari_common::configuration::Network;
use tari_comms::PeerManager;
use tari_core::{
base_node::{
Expand All @@ -43,7 +44,7 @@ use tari_core::{
StateMachineHandle,
},
blocks::{Block, BlockHeader, NewBlockTemplate},
consensus::{emission::Emission, ConsensusManager, ConsensusManagerBuilder, Network},
consensus::{emission::Emission, ConsensusManager, NetworkConsensus},
crypto::tari_utilities::hex::Hex,
mempool::{service::LocalMempoolService, TxStorageResponse},
proof_of_work::PowAlgorithm,
Expand Down Expand Up @@ -75,7 +76,7 @@ const LIST_HEADERS_DEFAULT_NUM_HEADERS: u64 = 10;
pub struct BaseNodeGrpcServer {
node_service: LocalNodeCommsInterface,
mempool_service: LocalMempoolService,
network: Network,
network: NetworkConsensus,
state_machine_handle: StateMachineHandle,
peer_manager: Arc<PeerManager>,
consensus_rules: ConsensusManager,
Expand All @@ -93,7 +94,7 @@ impl BaseNodeGrpcServer {
node_service: local_node,
mempool_service: local_mempool,
consensus_rules: ConsensusManager::builder(network).build(),
network,
network: network.into(),
state_machine_handle,
peer_manager,
}
Expand Down Expand Up @@ -914,7 +915,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
heights = heights
.drain(..cmp::min(heights.len(), GET_TOKENS_IN_CIRCULATION_MAX_HEIGHTS))
.collect();
let consensus_manager = ConsensusManagerBuilder::new(self.network).build();
let consensus_manager = ConsensusManager::builder(self.network.as_network()).build();

let (mut tx, rx) = mpsc::channel(GET_TOKENS_IN_CIRCULATION_PAGE_SIZE);
task::spawn(async move {
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_base_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ async fn run_node(node_config: Arc<GlobalConfig>, bootstrap: ConfigBootstrap) ->
let grpc = crate::grpc::base_node_grpc_server::BaseNodeGrpcServer::new(
ctx.local_node(),
ctx.local_mempool(),
node_config.network.into(),
node_config.network,
ctx.state_machine(),
ctx.base_node_comms().peer_manager(),
);
Expand Down
8 changes: 4 additions & 4 deletions applications/tari_base_node/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::{
sync::Arc,
};
use tari_app_utilities::utilities::ExitCodes;
use tari_common::{DatabaseType, GlobalConfig};
use tari_common::{configuration::Network, DatabaseType, GlobalConfig};
use tari_core::{
chain_storage::{
async_db::AsyncBlockchainDb,
Expand All @@ -41,7 +41,7 @@ use tari_core::{
BlockchainDatabaseConfig,
Validators,
},
consensus::{ConsensusManagerBuilder, Network as NetworkType},
consensus::ConsensusManager,
proof_of_work::randomx_factory::{RandomXConfig, RandomXFactory},
transactions::types::CryptoFactories,
validation::{
Expand Down Expand Up @@ -92,7 +92,7 @@ pub async fn run_recovery(node_config: &GlobalConfig) -> Result<(), anyhow::Erro
return Err(anyhow!("Recovery mode is only available for LMDB"));
},
};
let rules = ConsensusManagerBuilder::new(node_config.network.into()).build();
let rules = ConsensusManager::builder(node_config.network).build();
let factories = CryptoFactories::default();
let randomx_factory = RandomXFactory::new(RandomXConfig::default(), node_config.max_randomx_vms);
let validators = Validators::new(
Expand Down Expand Up @@ -140,7 +140,7 @@ async fn do_recovery<D: BlockchainBackend + 'static>(
source_backend: D,
) -> Result<(), anyhow::Error> {
// We dont care about the values, here, so we just use mock validators, and a mainnet CM.
let rules = ConsensusManagerBuilder::new(NetworkType::LocalNet).build();
let rules = ConsensusManager::builder(Network::LocalNet).build();
let validators = Validators::new(
MockValidator::new(true),
MockValidator::new(true),
Expand Down
20 changes: 4 additions & 16 deletions applications/tari_console_wallet/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ use rpassword::prompt_password_stdout;
use rustyline::Editor;
use std::{fs, path::PathBuf, str::FromStr, sync::Arc};
use tari_app_utilities::utilities::{setup_wallet_transport_type, ExitCodes};
use tari_common::{ConfigBootstrap, GlobalConfig, Network};
use tari_common::{ConfigBootstrap, GlobalConfig};
use tari_comms::{
peer_manager::{Peer, PeerFeatures},
types::CommsSecretKey,
NodeIdentity,
};
use tari_comms_dht::{DbConnectionUrl, DhtConfig};
use tari_core::{
consensus::Network as NetworkType,
transactions::types::{CryptoFactories, PrivateKey},
};
use tari_core::transactions::types::{CryptoFactories, PrivateKey};
use tari_p2p::{
initialization::CommsConfig,
seed_peer::SeedPeer,
Expand Down Expand Up @@ -318,6 +315,7 @@ pub async fn init_wallet(
};

let comms_config = CommsConfig {
network: config.network,
node_identity,
user_agent: format!("tari/wallet/{}", env!("CARGO_PKG_VERSION")),
transport_type,
Expand All @@ -330,7 +328,6 @@ pub async fn init_wallet(
database_url: DbConnectionUrl::File(config.data_dir.join("dht-console-wallet.db")),
auto_join: true,
allow_test_addresses: config.allow_test_addresses,
network: config.network.into(),
flood_ban_max_msg_count: config.flood_ban_max_msg_count,
saf_msg_validity: config.saf_expiry_duration,
..Default::default()
Expand All @@ -345,15 +342,6 @@ pub async fn init_wallet(
dns_seeds_use_dnssec: true,
};

let network = match &config.network {
Network::MainNet => NetworkType::MainNet,
Network::Ridcully => NetworkType::Ridcully,
Network::LocalNet => NetworkType::LocalNet,
Network::Stibbons => NetworkType::Stibbons,
Network::Weatherwax => NetworkType::Weatherwax,
Network::Rincewind => unimplemented!("Rincewind has been retired"),
};

let base_node_service_config = BaseNodeServiceConfig::new(
config.wallet_base_node_service_refresh_interval,
config.wallet_base_node_service_request_max_age,
Expand All @@ -379,7 +367,7 @@ pub async fn init_wallet(
prevent_fee_gt_amount: config.prevent_fee_gt_amount,
..Default::default()
}),
network,
config.network.into(),
Some(base_node_service_config),
Some(config.buffer_size_base_node_wallet),
Some(config.buffer_rate_limit_base_node_wallet),
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_console_wallet/src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::{
},
wallet_modes::PeerConfig,
};
use tari_common::{GlobalConfig, Network};
use tari_common::{configuration::Network, GlobalConfig};
use tari_comms::peer_manager::Peer;
use tari_wallet::WalletSqlite;
use tokio::runtime::Handle;
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_console_wallet/src/ui/state/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use futures::{stream::Fuse, StreamExt};
use log::*;
use qrcode::{render::unicode, QrCode};
use std::{collections::HashMap, sync::Arc};
use tari_common::{GlobalConfig, Network};
use tari_common::{configuration::Network, GlobalConfig};
use tari_comms::{
connectivity::ConnectivityEventRx,
multiaddr::Multiaddr,
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_merge_mining_proxy/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use std::{
time::Instant,
};
use tari_app_grpc::{tari_rpc as grpc, tari_rpc::GetCoinbaseRequest};
use tari_common::{GlobalConfig, Network};
use tari_common::{configuration::Network, GlobalConfig};
use tari_core::{
blocks::{Block, NewBlockTemplate},
proof_of_work::monero_rx,
Expand Down
3 changes: 2 additions & 1 deletion base_layer/core/src/base_node/sync/header_sync/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,12 @@ mod test {
use crate::{
blocks::BlockHeader,
chain_storage::{async_db::AsyncBlockchainDb, BlockHeaderAccumulatedData},
consensus::{ConsensusManager, Network},
consensus::ConsensusManager,
crypto::tari_utilities::{hex::Hex, Hashable},
proof_of_work::{randomx_factory::RandomXFactory, PowAlgorithm},
test_helpers::blockchain::{create_new_blockchain, TempDatabase},
};
use tari_common::configuration::Network;
use tari_test_utils::unpack_enum;

fn setup() -> (BlockHeaderSyncValidator<TempDatabase>, AsyncBlockchainDb<TempDatabase>) {
Expand Down
6 changes: 3 additions & 3 deletions base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1914,8 +1914,7 @@ mod test {
chain_strength_comparer::strongest_chain,
consensus_constants::PowAlgorithmConstants,
ConsensusConstantsBuilder,
ConsensusManagerBuilder,
Network,
ConsensusManager,
},
proof_of_work::AchievedTargetDifficulty,
test_helpers::{
Expand All @@ -1926,6 +1925,7 @@ mod test {
validation::{header_validator::HeaderValidator, mocks::MockValidator},
};
use std::collections::HashMap;
use tari_common::configuration::Network;

#[test]
fn lmdb_fetch_monero_seeds() {
Expand Down Expand Up @@ -2456,7 +2456,7 @@ mod test {
let mock_validator = Box::new(MockValidator::new(true));
// A real validator is needed here to test target difficulties

let consensus = ConsensusManagerBuilder::new(Network::LocalNet)
let consensus = ConsensusManager::builder(Network::LocalNet)
.with_consensus_constants(
ConsensusConstantsBuilder::new(Network::LocalNet)
.clear_proof_of_work()
Expand Down
8 changes: 6 additions & 2 deletions base_layer/core/src/consensus/consensus_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::{
consensus::{network::Network, KERNEL_WEIGHT, WEIGHT_PER_OUTPUT},
consensus::{network::NetworkConsensus, KERNEL_WEIGHT, WEIGHT_PER_OUTPUT},
proof_of_work::{Difficulty, PowAlgorithm},
transactions::tari_amount::{uT, MicroTari, T},
};
use chrono::{DateTime, Duration, Utc};
use std::{collections::HashMap, ops::Add};
use tari_common::configuration::Network;
use tari_crypto::tari_utilities::epoch_time::EpochTime;

/// This is the inner struct used to control all consensus values.
Expand Down Expand Up @@ -400,7 +401,10 @@ impl ConsensusConstantsBuilder {
pub fn new(network: Network) -> Self {
Self {
// TODO: Resolve this unwrap
consensus: network.create_consensus_constants().pop().unwrap(),
consensus: NetworkConsensus::from(network)
.create_consensus_constants()
.pop()
.expect("Empty consensus constants"),
}
}

Expand Down
13 changes: 7 additions & 6 deletions base_layer/core/src/consensus/consensus_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ use crate::{
consensus::{
chain_strength_comparer::{strongest_chain, ChainStrengthComparer},
emission::{Emission, EmissionSchedule},
network::Network,
ConsensusConstants,
NetworkConsensus,
},
proof_of_work::{DifficultyAdjustmentError, PowAlgorithm, TargetDifficultyWindow},
transactions::tari_amount::MicroTari,
};
use std::{convert::TryFrom, sync::Arc};
use tari_common::configuration::Network;
use thiserror::Error;

#[derive(Debug, Error)]
Expand Down Expand Up @@ -71,7 +72,7 @@ impl ConsensusManager {

/// Returns the genesis block for the selected network.
pub fn get_genesis_block(&self) -> ChainBlock {
match self.inner.network {
match self.inner.network.as_network() {
Network::MainNet => get_mainnet_genesis_block(),
Network::Ridcully => get_ridcully_genesis_block(),
Network::Stibbons => get_stibbons_genesis_block(),
Expand Down Expand Up @@ -137,7 +138,7 @@ impl ConsensusManager {
}

/// This is the currently configured chain network.
pub fn network(&self) -> Network {
pub fn network(&self) -> NetworkConsensus {
self.inner.network
}
}
Expand All @@ -148,7 +149,7 @@ struct ConsensusManagerInner {
/// This is the inner struct used to control all consensus values.
pub consensus_constants: Vec<ConsensusConstants>,
/// The configured chain network.
pub network: Network,
pub network: NetworkConsensus,
/// The configuration for the emission schedule for integer only.
pub emission: EmissionSchedule,
/// This allows the user to set a custom Genesis block
Expand All @@ -160,7 +161,7 @@ struct ConsensusManagerInner {
/// Constructor for the consensus manager struct
pub struct ConsensusManagerBuilder {
consensus_constants: Vec<ConsensusConstants>,
network: Network,
network: NetworkConsensus,
gen_block: Option<ChainBlock>,
chain_strength_comparer: Option<Box<dyn ChainStrengthComparer + Send + Sync>>,
}
Expand All @@ -170,7 +171,7 @@ impl ConsensusManagerBuilder {
pub fn new(network: Network) -> Self {
ConsensusManagerBuilder {
consensus_constants: vec![],
network,
network: network.into(),
gen_block: None,
chain_strength_comparer: None,
}
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ pub use consensus_constants::{ConsensusConstants, ConsensusConstantsBuilder};
#[cfg(feature = "base_node")]
pub use consensus_manager::{ConsensusManager, ConsensusManagerBuilder, ConsensusManagerError};
#[cfg(any(feature = "base_node", feature = "transactions"))]
pub use network::Network;
pub use network::NetworkConsensus;
Loading

0 comments on commit 89769b2

Please sign in to comment.