From 89769b27e4a41dc27a3e2543479534ac851add61 Mon Sep 17 00:00:00 2001 From: Stanimal Date: Tue, 22 Jun 2021 17:30:32 +0400 Subject: [PATCH] Network separation and protocol versioning implementation - 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 --- Cargo.lock | 216 ++++++++++++++---- applications/tari_base_node/src/bootstrap.rs | 2 +- applications/tari_base_node/src/builder.rs | 4 +- .../tari_base_node/src/command_handler.rs | 4 +- .../src/grpc/base_node_grpc_server.rs | 9 +- applications/tari_base_node/src/main.rs | 2 +- applications/tari_base_node/src/recovery.rs | 8 +- .../tari_console_wallet/src/init/mod.rs | 20 +- .../tari_console_wallet/src/ui/app.rs | 2 +- .../src/ui/state/app_state.rs | 2 +- .../tari_merge_mining_proxy/src/proxy.rs | 2 +- .../base_node/sync/header_sync/validator.rs | 3 +- .../src/chain_storage/blockchain_database.rs | 6 +- .../core/src/consensus/consensus_constants.rs | 8 +- .../core/src/consensus/consensus_manager.rs | 13 +- base_layer/core/src/consensus/mod.rs | 2 +- base_layer/core/src/consensus/network.rs | 54 ++--- .../core/src/mempool/reorg_pool/reorg_pool.rs | 3 +- .../unconfirmed_pool/unconfirmed_pool.rs | 3 +- .../core/src/test_helpers/blockchain.rs | 13 +- base_layer/core/src/test_helpers/mod.rs | 12 +- .../core/src/transactions/coinbase_builder.rs | 3 +- base_layer/core/src/validation/test.rs | 3 +- base_layer/core/tests/async_db.rs | 2 +- base_layer/core/tests/base_node_rpc.rs | 8 +- base_layer/core/tests/block_validation.rs | 8 +- .../chain_storage_tests/chain_backend.rs | 3 +- .../chain_storage_tests/chain_storage.rs | 3 +- .../core/tests/helpers/block_builders.rs | 8 +- base_layer/core/tests/helpers/nodes.rs | 23 +- .../core/tests/helpers/sample_blockchains.rs | 3 +- .../core/tests/helpers/test_blockchain.rs | 3 +- base_layer/core/tests/mempool.rs | 15 +- base_layer/core/tests/node_comms_interface.rs | 39 +--- base_layer/core/tests/node_service.rs | 49 ++-- base_layer/core/tests/node_state_machine.rs | 5 +- base_layer/p2p/src/initialization.rs | 13 +- base_layer/p2p/src/lib.rs | 9 + .../p2p/src/services/liveness/service.rs | 6 +- base_layer/p2p/src/test_utils.rs | 6 +- base_layer/wallet/src/config.rs | 6 +- .../wallet/src/output_manager_service/mod.rs | 8 +- base_layer/wallet/src/testnet_utils.rs | 21 +- .../wallet/src/transaction_service/service.rs | 8 +- .../tests/output_manager_service/service.rs | 3 +- .../tests/support/comms_and_services.rs | 9 +- .../tests/transaction_service/service.rs | 6 +- base_layer/wallet/tests/wallet/mod.rs | 26 +-- base_layer/wallet_ffi/src/lib.rs | 12 +- common/src/configuration/global.rs | 47 +--- common/src/configuration/loader.rs | 6 +- common/src/configuration/mod.rs | 11 +- common/src/configuration/network.rs | 85 +++++++ common/src/configuration/writer.rs | 8 +- common/src/lib.rs | 13 +- comms/Cargo.toml | 2 +- comms/dht/Cargo.toml | 1 - comms/dht/src/config.rs | 11 +- comms/dht/src/consts.rs | 3 +- comms/dht/src/dht.rs | 4 +- comms/dht/src/discovery/service.rs | 2 +- comms/dht/src/envelope.rs | 18 +- comms/dht/src/inbound/message.rs | 4 +- comms/dht/src/inbound/mod.rs | 3 - comms/dht/src/inbound/validate.rs | 138 ----------- comms/dht/src/network_discovery/on_connect.rs | 2 +- comms/dht/src/outbound/broadcast.rs | 24 +- comms/dht/src/outbound/message.rs | 7 +- comms/dht/src/outbound/serialize.rs | 8 +- comms/dht/src/proto/envelope.proto | 38 +-- comms/dht/src/proto/mod.rs | 32 +-- comms/dht/src/test_utils/makers.rs | 7 +- comms/src/builder/mod.rs | 23 +- comms/src/connection_manager/common.rs | 29 +-- comms/src/connection_manager/dialer.rs | 29 +-- comms/src/connection_manager/error.rs | 2 - comms/src/connection_manager/listener.rs | 26 ++- comms/src/connection_manager/manager.rs | 11 +- comms/src/connection_manager/tests/manager.rs | 6 +- comms/src/connection_manager/wire_mode.rs | 9 +- comms/src/lib.rs | 1 - comms/src/peer_manager/peer.rs | 11 - comms/src/peer_manager/peer_storage.rs | 3 +- comms/src/proto/identity.proto | 13 +- comms/src/protocol/identity.rs | 90 ++++++-- comms/src/protocol/messaging/protocol.rs | 2 +- comms/src/protocol/mod.rs | 3 + .../{consts.rs => protocol/network_info.rs} | 26 ++- comms/src/types.rs | 4 - 89 files changed, 725 insertions(+), 733 deletions(-) create mode 100644 common/src/configuration/network.rs delete mode 100644 comms/dht/src/inbound/validate.rs rename comms/src/{consts.rs => protocol/network_info.rs} (61%) diff --git a/Cargo.lock b/Cargo.lock index 31932363cc..b3e708d4a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,15 @@ dependencies = [ "generic-array 0.14.4", ] +[[package]] +name = "aead" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "922b33332f54fc0ad13fa3e514601e8d30fb54e1f3eadc36643f6526db645621" +dependencies = [ + "generic-array 0.14.4", +] + [[package]] name = "aes" version = "0.6.0" @@ -37,17 +46,43 @@ dependencies = [ "cipher 0.2.5", ] +[[package]] +name = "aes" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "495ee669413bfbe9e8cace80f4d3d78e6d8c8d99579f97fb93bde351b185f2d4" +dependencies = [ + "cfg-if 1.0.0", + "cipher 0.3.0", + "cpufeatures", + "opaque-debug 0.3.0", +] + [[package]] name = "aes-gcm" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5278b5fabbb9bd46e24aa69b2fdea62c99088e0a950a9be40e3e0101298f88da" dependencies = [ - "aead", - "aes", + "aead 0.3.2", + "aes 0.6.0", "cipher 0.2.5", - "ctr", - "ghash", + "ctr 0.6.0", + "ghash 0.3.1", + "subtle 2.4.0", +] + +[[package]] +name = "aes-gcm" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc3be92e19a7ef47457b8e6f90707e12b6ac5d20c6f3866584fa3be0787d839f" +dependencies = [ + "aead 0.4.1", + "aes 0.7.4", + "cipher 0.3.0", + "ctr 0.7.0", + "ghash 0.4.2", "subtle 2.4.0", ] @@ -315,13 +350,14 @@ dependencies = [ ] [[package]] -name = "blake2-rfc" -version = "0.2.18" +name = "blake2" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400" +checksum = "10a5720225ef5daecf08657f23791354e1685a8c91a4c60c7f3d3b2892f978f4" dependencies = [ - "arrayvec 0.4.12", - "constant_time_eq", + "crypto-mac 0.8.0", + "digest 0.9.0", + "opaque-debug 0.3.0", ] [[package]] @@ -467,7 +503,7 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b9434b9a5aa1450faa3f9cb14ea0e8c53bb5d2b3c1bfd1ab4fc03e9f33fbfb0" dependencies = [ - "rustc_version", + "rustc_version 0.2.3", ] [[package]] @@ -525,15 +561,20 @@ dependencies = [ "cfg-if 1.0.0", "cipher 0.3.0", "cpufeatures", + "zeroize", ] [[package]] -name = "chacha20-poly1305-aead" -version = "0.1.2" +name = "chacha20poly1305" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77d2058ba29594f69c75e8a9018e0485e3914ca5084e3613cd64529042f5423b" +checksum = "1580317203210c517b6d44794abfbe600698276db18127e37ad3e69bf5e848e5" dependencies = [ - "constant_time_eq", + "aead 0.4.1", + "chacha20", + "cipher 0.3.0", + "poly1305", + "zeroize", ] [[package]] @@ -944,6 +985,15 @@ dependencies = [ "cipher 0.2.5", ] +[[package]] +name = "ctr" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a232f92a03f37dd7d7dd2adc67166c77e9cd88de5b019b9a9eecfaeaf7bfd481" +dependencies = [ + "cipher 0.3.0", +] + [[package]] name = "curl-sys" version = "0.4.40+curl-7.75.0" @@ -1546,7 +1596,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97304e4cd182c3846f7575ced3890c53012ce534ad9114046b0a9e00bb30a375" dependencies = [ "opaque-debug 0.3.0", - "polyval", + "polyval 0.4.5", +] + +[[package]] +name = "ghash" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bbd60caa311237d508927dbba7594b483db3ef05faa55172fcf89b1bcda7853" +dependencies = [ + "opaque-debug 0.3.0", + "polyval 0.5.1", ] [[package]] @@ -2616,6 +2676,15 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +[[package]] +name = "pest" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53" +dependencies = [ + "ucd-trie", +] + [[package]] name = "petgraph" version = "0.5.1" @@ -2690,6 +2759,17 @@ version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" +[[package]] +name = "poly1305" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fe800695325da85083cd23b56826fccb2e2dc29b218e7811a6f33bc93f414be" +dependencies = [ + "cpufeatures", + "opaque-debug 0.3.0", + "universal-hash", +] + [[package]] name = "polyval" version = "0.4.5" @@ -2701,6 +2781,18 @@ dependencies = [ "universal-hash", ] +[[package]] +name = "polyval" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e597450cbf209787f0e6de80bf3795c6b2356a380ee87837b545aded8dbc1823" +dependencies = [ + "cfg-if 1.0.0", + "cpufeatures", + "opaque-debug 0.3.0", + "universal-hash", +] + [[package]] name = "ppv-lite86" version = "0.2.10" @@ -3234,7 +3326,16 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" dependencies = [ - "semver", + "semver 0.9.0", +] + +[[package]] +name = "rustc_version" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee" +dependencies = [ + "semver 0.11.0", ] [[package]] @@ -3361,7 +3462,16 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" dependencies = [ - "semver-parser", + "semver-parser 0.7.0", +] + +[[package]] +name = "semver" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" +dependencies = [ + "semver-parser 0.10.2", ] [[package]] @@ -3370,6 +3480,15 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +[[package]] +name = "semver-parser" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" +dependencies = [ + "pest", +] + [[package]] name = "serde" version = "0.8.23" @@ -3496,6 +3615,19 @@ dependencies = [ "opaque-debug 0.2.3", ] +[[package]] +name = "sha2" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b362ae5752fd2137731f9fa25fd4d9058af34666ca1966fb969119cc35719f12" +dependencies = [ + "block-buffer 0.9.0", + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.9.0", + "opaque-debug 0.3.0", +] + [[package]] name = "sha3" version = "0.8.2" @@ -3570,17 +3702,17 @@ checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" [[package]] name = "snow" -version = "0.6.2" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afb767eee7d257ba202f0b9b08673bc13b22281632ef45267b19f13100accd2f" +checksum = "6142f7c25e94f6fd25a32c3348ec230df9109b463f59c8c7acc4bd34936babb7" dependencies = [ - "arrayref", - "blake2-rfc", - "chacha20-poly1305-aead", - "rand 0.7.3", - "rand_core 0.5.1", - "rustc_version", - "sha2", + "aes-gcm 0.9.2", + "blake2 0.9.1", + "chacha20poly1305", + "rand 0.8.3", + "rand_core 0.6.2", + "rustc_version 0.3.3", + "sha2 0.9.5", "subtle 2.4.0", "x25519-dalek", ] @@ -3871,7 +4003,7 @@ dependencies = [ "prost-build", "serde 1.0.126", "serde_json", - "sha2", + "sha2 0.8.2", "structopt", "tari_storage", "tari_test_utils", @@ -3897,7 +4029,7 @@ dependencies = [ "anyhow", "async-trait", "bitflags 1.2.1", - "blake2", + "blake2 0.8.1", "bytes 0.5.6", "chrono", "cidr", @@ -4036,7 +4168,7 @@ version = "0.8.11" dependencies = [ "bincode", "bitflags 1.2.1", - "blake2", + "blake2 0.8.1", "bytes 0.4.12", "chrono", "config", @@ -4086,7 +4218,7 @@ version = "0.9.0" source = "git+ssh://git@github.com/tari-project/tari-crypto.git?branch=main#45fba2160694ac19f51d6233fd5465da8a1614ee" dependencies = [ "base64 0.10.1", - "blake2", + "blake2 0.8.1", "blake3", "cbindgen", "clear_on_drop", @@ -4099,7 +4231,7 @@ dependencies = [ "rmp-serde", "serde 1.0.126", "serde_json", - "sha2", + "sha2 0.8.2", "sha3 0.9.1", "tari_bulletproofs", "tari_utilities", @@ -4110,7 +4242,7 @@ dependencies = [ name = "tari_infra_derive" version = "0.8.11" dependencies = [ - "blake2", + "blake2 0.8.1", "proc-macro2 0.4.30", "quote 0.6.13", "syn 0.15.44", @@ -4125,7 +4257,7 @@ dependencies = [ "serde 1.0.126", "serde_derive", "serde_json", - "sha2", + "sha2 0.8.2", "tari_crypto", "thiserror", ] @@ -4196,7 +4328,7 @@ name = "tari_mmr" version = "0.8.11" dependencies = [ "bincode", - "blake2", + "blake2 0.8.1", "criterion", "croaring", "digest 0.8.1", @@ -4328,9 +4460,9 @@ dependencies = [ name = "tari_wallet" version = "0.8.11" dependencies = [ - "aes-gcm", + "aes-gcm 0.8.0", "bincode", - "blake2", + "blake2 0.8.1", "chrono", "crossbeam-channel 0.3.9", "diesel", @@ -5043,6 +5175,12 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" +[[package]] +name = "ucd-trie" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" + [[package]] name = "uint" version = "0.9.0" @@ -5405,11 +5543,11 @@ dependencies = [ [[package]] name = "x25519-dalek" -version = "0.6.0" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "637ff90c9540fa3073bb577e65033069e4bae7c79d49d74aa3ffdf5342a53217" +checksum = "5a0c105152107e3b96f6a00a65e86ce82d9b125230e1c4302940eca58ff71f4f" dependencies = [ - "curve25519-dalek 2.1.2", + "curve25519-dalek 3.1.0", "rand_core 0.5.1", "zeroize", ] diff --git a/applications/tari_base_node/src/bootstrap.rs b/applications/tari_base_node/src/bootstrap.rs index 78a3b34c38..85235f6a57 100644 --- a/applications/tari_base_node/src/bootstrap.rs +++ b/applications/tari_base_node/src/bootstrap.rs @@ -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(), @@ -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() diff --git a/applications/tari_base_node/src/builder.rs b/applications/tari_base_node/src/builder.rs index 716dec3e07..c321e43b71 100644 --- a/applications/tari_base_node/src/builder.rs +++ b/applications/tari_base_node/src/builder.rs @@ -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, @@ -193,7 +193,7 @@ async fn build_node_context( ) -> Result { //---------------------------------- 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( diff --git a/applications/tari_base_node/src/command_handler.rs b/applications/tari_base_node/src/command_handler.rs index 03f5557356..d662d7c936 100644 --- a/applications/tari_base_node/src/command_handler.rs +++ b/applications/tari_base_node/src/command_handler.rs @@ -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}, @@ -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, diff --git a/applications/tari_base_node/src/grpc/base_node_grpc_server.rs b/applications/tari_base_node/src/grpc/base_node_grpc_server.rs index 58970de17c..0a416e11ae 100644 --- a/applications/tari_base_node/src/grpc/base_node_grpc_server.rs +++ b/applications/tari_base_node/src/grpc/base_node_grpc_server.rs @@ -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::{ @@ -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, @@ -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, consensus_rules: ConsensusManager, @@ -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, } @@ -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 { diff --git a/applications/tari_base_node/src/main.rs b/applications/tari_base_node/src/main.rs index 1d8bcecc5f..ff48b12ea2 100644 --- a/applications/tari_base_node/src/main.rs +++ b/applications/tari_base_node/src/main.rs @@ -219,7 +219,7 @@ async fn run_node(node_config: Arc, 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(), ); diff --git a/applications/tari_base_node/src/recovery.rs b/applications/tari_base_node/src/recovery.rs index cdabc8d95a..d8893a5b80 100644 --- a/applications/tari_base_node/src/recovery.rs +++ b/applications/tari_base_node/src/recovery.rs @@ -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, @@ -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::{ @@ -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( @@ -140,7 +140,7 @@ async fn do_recovery( 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), diff --git a/applications/tari_console_wallet/src/init/mod.rs b/applications/tari_console_wallet/src/init/mod.rs index 60784e0b19..ef022864ca 100644 --- a/applications/tari_console_wallet/src/init/mod.rs +++ b/applications/tari_console_wallet/src/init/mod.rs @@ -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, @@ -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, @@ -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() @@ -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, @@ -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), diff --git a/applications/tari_console_wallet/src/ui/app.rs b/applications/tari_console_wallet/src/ui/app.rs index 1b96bbbd21..b9df53d852 100644 --- a/applications/tari_console_wallet/src/ui/app.rs +++ b/applications/tari_console_wallet/src/ui/app.rs @@ -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; diff --git a/applications/tari_console_wallet/src/ui/state/app_state.rs b/applications/tari_console_wallet/src/ui/state/app_state.rs index 8812d3bf4d..1374cf7d9d 100644 --- a/applications/tari_console_wallet/src/ui/state/app_state.rs +++ b/applications/tari_console_wallet/src/ui/state/app_state.rs @@ -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, diff --git a/applications/tari_merge_mining_proxy/src/proxy.rs b/applications/tari_merge_mining_proxy/src/proxy.rs index 3fa815abd3..9acd9f76e3 100644 --- a/applications/tari_merge_mining_proxy/src/proxy.rs +++ b/applications/tari_merge_mining_proxy/src/proxy.rs @@ -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, diff --git a/base_layer/core/src/base_node/sync/header_sync/validator.rs b/base_layer/core/src/base_node/sync/header_sync/validator.rs index 15f33782f6..55c6dd4d9e 100644 --- a/base_layer/core/src/base_node/sync/header_sync/validator.rs +++ b/base_layer/core/src/base_node/sync/header_sync/validator.rs @@ -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, AsyncBlockchainDb) { diff --git a/base_layer/core/src/chain_storage/blockchain_database.rs b/base_layer/core/src/chain_storage/blockchain_database.rs index f31f4c075c..e74e919794 100644 --- a/base_layer/core/src/chain_storage/blockchain_database.rs +++ b/base_layer/core/src/chain_storage/blockchain_database.rs @@ -1914,8 +1914,7 @@ mod test { chain_strength_comparer::strongest_chain, consensus_constants::PowAlgorithmConstants, ConsensusConstantsBuilder, - ConsensusManagerBuilder, - Network, + ConsensusManager, }, proof_of_work::AchievedTargetDifficulty, test_helpers::{ @@ -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() { @@ -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() diff --git a/base_layer/core/src/consensus/consensus_constants.rs b/base_layer/core/src/consensus/consensus_constants.rs index 96f8ec8303..52c0620e8c 100644 --- a/base_layer/core/src/consensus/consensus_constants.rs +++ b/base_layer/core/src/consensus/consensus_constants.rs @@ -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. @@ -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"), } } diff --git a/base_layer/core/src/consensus/consensus_manager.rs b/base_layer/core/src/consensus/consensus_manager.rs index db7765df35..0663d153ca 100644 --- a/base_layer/core/src/consensus/consensus_manager.rs +++ b/base_layer/core/src/consensus/consensus_manager.rs @@ -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)] @@ -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(), @@ -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 } } @@ -148,7 +149,7 @@ struct ConsensusManagerInner { /// This is the inner struct used to control all consensus values. pub consensus_constants: Vec, /// 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 @@ -160,7 +161,7 @@ struct ConsensusManagerInner { /// Constructor for the consensus manager struct pub struct ConsensusManagerBuilder { consensus_constants: Vec, - network: Network, + network: NetworkConsensus, gen_block: Option, chain_strength_comparer: Option>, } @@ -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, } diff --git a/base_layer/core/src/consensus/mod.rs b/base_layer/core/src/consensus/mod.rs index 1ddfdedc61..f0432fbf29 100644 --- a/base_layer/core/src/consensus/mod.rs +++ b/base_layer/core/src/consensus/mod.rs @@ -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; diff --git a/base_layer/core/src/consensus/network.rs b/base_layer/core/src/consensus/network.rs index 4866852af5..55e17e3c98 100644 --- a/base_layer/core/src/consensus/network.rs +++ b/base_layer/core/src/consensus/network.rs @@ -21,46 +21,32 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use super::consensus_constants::ConsensusConstants; -use tari_common::configuration::Network as GlobalNetwork; -/// Specifies the configured chain network. +use tari_common::configuration::Network; + +/// Represents the consensus used for a given network #[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub enum Network { - /// Mainnet of Tari, currently should panic if network is set to this. - MainNet, - /// Alpha net version - // Rincewind, - /// Second test net version - Ridcully, - /// Third test net - Stibbons, - /// Fourth test net, includes tari script - Weatherwax, - /// Local network constants used inside of unit and integration tests. Contains the genesis block to be used for - /// that chain. - LocalNet, -} +pub struct NetworkConsensus(Network); -impl Network { +impl NetworkConsensus { pub fn create_consensus_constants(&self) -> Vec { - match self { - Network::MainNet => ConsensusConstants::mainnet(), - Network::Ridcully => ConsensusConstants::ridcully(), - Network::Stibbons => ConsensusConstants::stibbons(), - Network::Weatherwax => ConsensusConstants::weatherwax(), - Network::LocalNet => ConsensusConstants::localnet(), + use Network::*; + match self.as_network() { + MainNet => ConsensusConstants::mainnet(), + Ridcully => ConsensusConstants::ridcully(), + Stibbons => ConsensusConstants::stibbons(), + Weatherwax => ConsensusConstants::weatherwax(), + LocalNet => ConsensusConstants::localnet(), } } + + #[inline] + pub fn as_network(self) -> Network { + self.0 + } } -impl From for Network { - fn from(global_network: GlobalNetwork) -> Self { - match global_network { - GlobalNetwork::MainNet => Network::MainNet, - GlobalNetwork::Ridcully => Network::Ridcully, - GlobalNetwork::Stibbons => Network::Stibbons, - GlobalNetwork::Weatherwax => Network::Weatherwax, - GlobalNetwork::LocalNet => Network::LocalNet, - GlobalNetwork::Rincewind => unimplemented!("Rincewind has been retired"), - } +impl From for NetworkConsensus { + fn from(global_network: Network) -> Self { + Self(global_network) } } diff --git a/base_layer/core/src/mempool/reorg_pool/reorg_pool.rs b/base_layer/core/src/mempool/reorg_pool/reorg_pool.rs index bd102bbef6..5626242474 100644 --- a/base_layer/core/src/mempool/reorg_pool/reorg_pool.rs +++ b/base_layer/core/src/mempool/reorg_pool/reorg_pool.rs @@ -154,12 +154,13 @@ impl Clone for ReorgPool { mod test { use super::*; use crate::{ - consensus::{ConsensusManagerBuilder, Network}, + consensus::ConsensusManagerBuilder, test_helpers::create_orphan_block, transactions::tari_amount::MicroTari, tx, }; use std::{thread, time::Duration}; + use tari_common::configuration::Network; #[test] fn test_insert_rlu_and_ttl() { diff --git a/base_layer/core/src/mempool/unconfirmed_pool/unconfirmed_pool.rs b/base_layer/core/src/mempool/unconfirmed_pool/unconfirmed_pool.rs index 4c9a328326..5cfe2b6125 100644 --- a/base_layer/core/src/mempool/unconfirmed_pool/unconfirmed_pool.rs +++ b/base_layer/core/src/mempool/unconfirmed_pool/unconfirmed_pool.rs @@ -304,7 +304,7 @@ impl UnconfirmedPool { mod test { use super::*; use crate::{ - consensus::{ConsensusManagerBuilder, Network}, + consensus::ConsensusManagerBuilder, test_helpers::create_orphan_block, transactions::{ fee::Fee, @@ -316,6 +316,7 @@ mod test { }, tx, }; + use tari_common::configuration::Network; use tari_crypto::script; #[test] diff --git a/base_layer/core/src/test_helpers/blockchain.rs b/base_layer/core/src/test_helpers/blockchain.rs index dfde3a1d13..9d91017976 100644 --- a/base_layer/core/src/test_helpers/blockchain.rs +++ b/base_layer/core/src/test_helpers/blockchain.rs @@ -41,13 +41,7 @@ use crate::{ PrunedOutput, Validators, }, - consensus::{ - chain_strength_comparer::ChainStrengthComparerBuilder, - ConsensusConstantsBuilder, - ConsensusManager, - ConsensusManagerBuilder, - Network, - }, + consensus::{chain_strength_comparer::ChainStrengthComparerBuilder, ConsensusConstantsBuilder, ConsensusManager}, transactions::{ transaction::{TransactionInput, TransactionKernel, TransactionOutput}, types::{CryptoFactories, HashOutput, Signature}, @@ -64,6 +58,7 @@ use std::{ ops::Deref, path::{Path, PathBuf}, }; +use tari_common::configuration::Network; use tari_common_types::chain_metadata::ChainMetadata; use tari_storage::lmdb_store::LMDBConfig; use tari_test_utils::paths::create_temporary_data_path; @@ -73,7 +68,7 @@ pub fn create_new_blockchain() -> BlockchainDatabase { let network = Network::Weatherwax; let consensus_constants = ConsensusConstantsBuilder::new(network).build(); let genesis = get_weatherwax_genesis_block(); - let consensus_manager = ConsensusManagerBuilder::new(network) + let consensus_manager = ConsensusManager::builder(network) .with_consensus_constants(consensus_constants) .with_block(genesis) .on_ties(ChainStrengthComparerBuilder::new().by_height().build()) @@ -121,7 +116,7 @@ pub fn create_store_with_consensus(rules: ConsensusManager) -> BlockchainDatabas } pub fn create_test_blockchain_db() -> BlockchainDatabase { let network = Network::Weatherwax; - let rules = ConsensusManagerBuilder::new(network).build(); + let rules = ConsensusManager::builder(network).build(); create_store_with_consensus(rules) } diff --git a/base_layer/core/src/test_helpers/mod.rs b/base_layer/core/src/test_helpers/mod.rs index c8a4c0cc8c..cf8c19bd32 100644 --- a/base_layer/core/src/test_helpers/mod.rs +++ b/base_layer/core/src/test_helpers/mod.rs @@ -27,19 +27,15 @@ pub mod blockchain; use crate::{ blocks::{Block, BlockHeader}, - consensus::ConsensusManager, - transactions::transaction::Transaction, -}; - -use crate::{ chain_storage::{BlockHeaderAccumulatedData, ChainHeader}, - consensus::{ConsensusManagerBuilder, Network}, + consensus::ConsensusManager, crypto::tari_utilities::Hashable, proof_of_work::{sha3_difficulty, AchievedTargetDifficulty, Difficulty}, - transactions::{types::CryptoFactories, CoinbaseBuilder}, + transactions::{transaction::Transaction, types::CryptoFactories, CoinbaseBuilder}, }; use rand::{distributions::Alphanumeric, Rng}; use std::{iter, path::Path, sync::Arc}; +use tari_common::configuration::Network; use tari_comms::PeerManager; use tari_storage::{lmdb_store::LMDBBuilder, LMDBWrapper}; @@ -57,7 +53,7 @@ pub fn create_block(block_version: u16, block_height: u64, transactions: Vec (CoinbaseBuilder, ConsensusManager, CryptoFactories) { diff --git a/base_layer/core/src/validation/test.rs b/base_layer/core/src/validation/test.rs index 54fdf87573..9216f44cad 100644 --- a/base_layer/core/src/validation/test.rs +++ b/base_layer/core/src/validation/test.rs @@ -22,10 +22,11 @@ use crate::{ blocks::BlockHeader, - consensus::{ConsensusManagerBuilder, Network}, + consensus::ConsensusManagerBuilder, test_helpers::{blockchain::create_store_with_consensus, create_chain_header}, validation::header_iter::HeaderIter, }; +use tari_common::configuration::Network; #[test] fn header_iter_empty_and_invalid_height() { diff --git a/base_layer/core/tests/async_db.rs b/base_layer/core/tests/async_db.rs index da1ca8ffe4..a69e8c4c14 100644 --- a/base_layer/core/tests/async_db.rs +++ b/base_layer/core/tests/async_db.rs @@ -30,10 +30,10 @@ use helpers::{ sample_blockchains::{create_blockchain_db_no_cut_through, create_new_blockchain}, }; use std::ops::Deref; +use tari_common::configuration::Network; use tari_core::{ blocks::Block, chain_storage::{async_db::AsyncBlockchainDb, BlockAddResult}, - consensus::Network, transactions::{ helpers::schema_to_transaction, tari_amount::T, diff --git a/base_layer/core/tests/base_node_rpc.rs b/base_layer/core/tests/base_node_rpc.rs index 995247a3c0..1f6b8a737f 100644 --- a/base_layer/core/tests/base_node_rpc.rs +++ b/base_layer/core/tests/base_node_rpc.rs @@ -49,6 +49,7 @@ use crate::helpers::{ nodes::{BaseNodeBuilder, NodeInterfaces}, }; use std::convert::TryFrom; +use tari_common::configuration::Network; use tari_comms::protocol::rpc::mock::RpcRequestMock; use tari_core::{ base_node::{ @@ -64,7 +65,7 @@ use tari_core::{ state_machine_service::states::{ListeningInfo, StateInfo, StatusInfo}, }, chain_storage::ChainBlock, - consensus::{ConsensusManager, ConsensusManagerBuilder, Network}, + consensus::{ConsensusManager, ConsensusManagerBuilder, NetworkConsensus}, crypto::tari_utilities::Hashable, proto::{ base_node::{FetchMatchingUtxos, Signatures as SignaturesProto}, @@ -92,7 +93,7 @@ fn setup() -> ( Runtime, TempDir, ) { - let network = Network::LocalNet; + let network = NetworkConsensus::from(Network::LocalNet); let consensus_constants = network.create_consensus_constants(); let factories = CryptoFactories::default(); let mut runtime = Runtime::new().unwrap(); @@ -100,8 +101,7 @@ fn setup() -> ( let (block0, utxo0) = create_genesis_block_with_coinbase_value(&factories, 100_000_000.into(), &consensus_constants[0]); - let consensus_manager = ConsensusManagerBuilder::new(network) - .with_consensus_constants(consensus_constants[0].clone()) + let consensus_manager = ConsensusManagerBuilder::new(network.as_network()) .with_block(block0.clone()) .build(); diff --git a/base_layer/core/tests/block_validation.rs b/base_layer/core/tests/block_validation.rs index 0d19e19436..ec6d7b96e4 100644 --- a/base_layer/core/tests/block_validation.rs +++ b/base_layer/core/tests/block_validation.rs @@ -23,15 +23,11 @@ use crate::helpers::block_builders::chain_block_with_new_coinbase; use monero::blockdata::block::Block as MoneroBlock; use std::sync::Arc; +use tari_common::configuration::Network; use tari_core::{ blocks::{Block, BlockHeaderValidationError}, chain_storage::{BlockchainDatabase, BlockchainDatabaseConfig, ChainStorageError, Validators}, - consensus::{ - consensus_constants::PowAlgorithmConstants, - ConsensusConstantsBuilder, - ConsensusManagerBuilder, - Network, - }, + consensus::{consensus_constants::PowAlgorithmConstants, ConsensusConstantsBuilder, ConsensusManagerBuilder}, crypto::tari_utilities::hex::Hex, proof_of_work::{ monero_rx, diff --git a/base_layer/core/tests/chain_storage_tests/chain_backend.rs b/base_layer/core/tests/chain_storage_tests/chain_backend.rs index 5e59b45155..77ee4747e9 100644 --- a/base_layer/core/tests/chain_storage_tests/chain_backend.rs +++ b/base_layer/core/tests/chain_storage_tests/chain_backend.rs @@ -21,9 +21,10 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::helpers::database::create_orphan_block; +use tari_common::configuration::Network; use tari_core::{ chain_storage::{create_lmdb_database, BlockchainBackend, ChainStorageError, DbKey, DbTransaction, DbValue}, - consensus::{ConsensusManagerBuilder, Network}, + consensus::ConsensusManagerBuilder, test_helpers::blockchain::create_test_db, tx, }; diff --git a/base_layer/core/tests/chain_storage_tests/chain_storage.rs b/base_layer/core/tests/chain_storage_tests/chain_storage.rs index 1e3eede9f1..b3d50b82fa 100644 --- a/base_layer/core/tests/chain_storage_tests/chain_storage.rs +++ b/base_layer/core/tests/chain_storage_tests/chain_storage.rs @@ -38,6 +38,7 @@ use crate::helpers::{ test_blockchain::TestBlockchain, }; use rand::{rngs::OsRng, RngCore}; +use tari_common::configuration::Network; use tari_common_types::types::BlockHash; use tari_core::{ blocks::{genesis_block, Block, BlockHeader}, @@ -51,7 +52,7 @@ use tari_core::{ DbTransaction, Validators, }, - consensus::{emission::Emission, ConsensusConstantsBuilder, ConsensusManagerBuilder, Network}, + consensus::{emission::Emission, ConsensusConstantsBuilder, ConsensusManagerBuilder}, proof_of_work::Difficulty, test_helpers::blockchain::{ create_store_with_consensus, diff --git a/base_layer/core/tests/helpers/block_builders.rs b/base_layer/core/tests/helpers/block_builders.rs index 462c8dad2f..95098e986d 100644 --- a/base_layer/core/tests/helpers/block_builders.rs +++ b/base_layer/core/tests/helpers/block_builders.rs @@ -23,6 +23,7 @@ use croaring::Bitmap; use rand::{rngs::OsRng, RngCore}; use std::{iter::repeat_with, sync::Arc}; +use tari_common::configuration::Network; use tari_core::{ blocks::{Block, BlockHeader, NewBlockTemplate}, chain_storage::{ @@ -34,7 +35,7 @@ use tari_core::{ ChainHeader, ChainStorageError, }, - consensus::{emission::Emission, ConsensusConstants, ConsensusManager, ConsensusManagerBuilder, Network}, + consensus::{emission::Emission, ConsensusConstants, ConsensusManager, ConsensusManagerBuilder}, proof_of_work::{sha3_difficulty, AchievedTargetDifficulty, Difficulty}, transactions::{ helpers::{ @@ -67,9 +68,6 @@ use tari_crypto::{ }; use tari_mmr::MutableMmr; -const _MAINNET: Network = Network::MainNet; -const _WEATHERWAX: Network = Network::Weatherwax; - pub fn create_coinbase( factories: &CryptoFactories, value: MicroTari, @@ -111,7 +109,7 @@ fn genesis_template( // This is a helper function to generate and print out a block that can be used as the genesis block. // #[test] pub fn _create_act_gen_block() { - let network = _WEATHERWAX; + let network = Network::Weatherwax; let consensus_manager: ConsensusManager = ConsensusManagerBuilder::new(network).build(); let factories = CryptoFactories::default(); let mut header = BlockHeader::new(consensus_manager.consensus_constants(0).blockchain_version()); diff --git a/base_layer/core/tests/helpers/nodes.rs b/base_layer/core/tests/helpers/nodes.rs index fde899d4d1..ebb4b6a3e1 100644 --- a/base_layer/core/tests/helpers/nodes.rs +++ b/base_layer/core/tests/helpers/nodes.rs @@ -24,6 +24,7 @@ use crate::helpers::mock_state_machine::MockBaseNodeStateMachine; use futures::Sink; use rand::{distributions::Alphanumeric, rngs::OsRng, Rng}; use std::{error::Error, iter, path::Path, sync::Arc, time::Duration}; +use tari_common::configuration::Network; use tari_comms::{ peer_manager::{NodeIdentity, PeerFeatures}, protocol::messaging::MessagingEventSender, @@ -40,7 +41,7 @@ use tari_core::{ StateMachineHandle, }, chain_storage::{BlockchainDatabase, Validators}, - consensus::{ConsensusManager, ConsensusManagerBuilder, Network}, + consensus::{ConsensusManager, ConsensusManagerBuilder, NetworkConsensus}, mempool::{ service::{LocalMempoolService, MempoolHandle}, Mempool, @@ -106,13 +107,13 @@ pub struct BaseNodeBuilder { liveness_service_config: Option, validators: Option>, consensus_manager: Option, - network: Network, + network: NetworkConsensus, } #[allow(dead_code)] impl BaseNodeBuilder { /// Create a new BaseNodeBuilder - pub fn new(network: Network) -> Self { + pub fn new(network: NetworkConsensus) -> Self { Self { node_identity: None, peers: None, @@ -189,7 +190,7 @@ impl BaseNodeBuilder { MockValidator::new(true), ) }); - let network = self.network; + let network = self.network.as_network(); let consensus_manager = self .consensus_manager .unwrap_or_else(|| ConsensusManagerBuilder::new(network).build()); @@ -234,11 +235,11 @@ pub fn create_network_with_2_base_nodes( let bob_node_identity = random_node_identity(); let network = Network::LocalNet; - let (alice_node, consensus_manager) = BaseNodeBuilder::new(network) + let (alice_node, consensus_manager) = BaseNodeBuilder::new(network.into()) .with_node_identity(alice_node_identity.clone()) .with_peers(vec![bob_node_identity.clone()]) .start(runtime, data_path); - let (bob_node, consensus_manager) = BaseNodeBuilder::new(network) + let (bob_node, consensus_manager) = BaseNodeBuilder::new(network.into()) .with_node_identity(bob_node_identity) .with_peers(vec![alice_node_identity]) .with_consensus_manager(consensus_manager) @@ -262,14 +263,14 @@ pub fn create_network_with_2_base_nodes_with_config>( let alice_node_identity = random_node_identity(); let bob_node_identity = random_node_identity(); let network = Network::LocalNet; - let (alice_node, consensus_manager) = BaseNodeBuilder::new(network) + let (alice_node, consensus_manager) = BaseNodeBuilder::new(network.into()) .with_node_identity(alice_node_identity.clone()) .with_base_node_service_config(base_node_service_config) .with_mempool_service_config(mempool_service_config) .with_liveness_service_config(liveness_service_config.clone()) .with_consensus_manager(consensus_manager) .start(runtime, data_path.as_ref().join("alice").as_os_str().to_str().unwrap()); - let (bob_node, consensus_manager) = BaseNodeBuilder::new(network) + let (bob_node, consensus_manager) = BaseNodeBuilder::new(network.into()) .with_node_identity(bob_node_identity) .with_peers(vec![alice_node_identity]) .with_base_node_service_config(base_node_service_config) @@ -322,14 +323,14 @@ pub fn create_network_with_3_base_nodes_with_config>( bob_node_identity.node_id().short_str(), carol_node_identity.node_id().short_str() ); - let (carol_node, consensus_manager) = BaseNodeBuilder::new(network) + let (carol_node, consensus_manager) = BaseNodeBuilder::new(network.into()) .with_node_identity(carol_node_identity.clone()) .with_base_node_service_config(base_node_service_config) .with_mempool_service_config(mempool_service_config) .with_liveness_service_config(liveness_service_config.clone()) .with_consensus_manager(consensus_manager) .start(runtime, data_path.as_ref().join("carol").as_os_str().to_str().unwrap()); - let (bob_node, consensus_manager) = BaseNodeBuilder::new(network) + let (bob_node, consensus_manager) = BaseNodeBuilder::new(network.into()) .with_node_identity(bob_node_identity.clone()) .with_peers(vec![carol_node_identity.clone()]) .with_base_node_service_config(base_node_service_config) @@ -337,7 +338,7 @@ pub fn create_network_with_3_base_nodes_with_config>( .with_liveness_service_config(liveness_service_config.clone()) .with_consensus_manager(consensus_manager) .start(runtime, data_path.as_ref().join("bob").as_os_str().to_str().unwrap()); - let (alice_node, consensus_manager) = BaseNodeBuilder::new(network) + let (alice_node, consensus_manager) = BaseNodeBuilder::new(network.into()) .with_node_identity(alice_node_identity) .with_peers(vec![bob_node_identity, carol_node_identity]) .with_base_node_service_config(base_node_service_config) diff --git a/base_layer/core/tests/helpers/sample_blockchains.rs b/base_layer/core/tests/helpers/sample_blockchains.rs index 169357181e..ddc6398fed 100644 --- a/base_layer/core/tests/helpers/sample_blockchains.rs +++ b/base_layer/core/tests/helpers/sample_blockchains.rs @@ -23,6 +23,7 @@ use crate::helpers::block_builders::{create_genesis_block, generate_new_block}; +use tari_common::configuration::Network; use tari_core::{ chain_storage::{ create_lmdb_database, @@ -32,7 +33,7 @@ use tari_core::{ LMDBDatabase, Validators, }, - consensus::{ConsensusConstants, ConsensusConstantsBuilder, ConsensusManager, ConsensusManagerBuilder, Network}, + consensus::{ConsensusConstants, ConsensusConstantsBuilder, ConsensusManager, ConsensusManagerBuilder}, test_helpers::blockchain::{create_store_with_consensus, TempDatabase}, transactions::{ tari_amount::{uT, T}, diff --git a/base_layer/core/tests/helpers/test_blockchain.rs b/base_layer/core/tests/helpers/test_blockchain.rs index 769dc19f30..b602f4d056 100644 --- a/base_layer/core/tests/helpers/test_blockchain.rs +++ b/base_layer/core/tests/helpers/test_blockchain.rs @@ -30,9 +30,10 @@ use crate::helpers::{ use log::*; use rand::{rngs::OsRng, RngCore}; use std::{collections::HashMap, sync::Arc}; +use tari_common::configuration::Network; use tari_core::{ chain_storage::{BlockAddResult, BlockchainDatabase}, - consensus::{ConsensusManager, Network}, + consensus::ConsensusManager, test_helpers::blockchain::TempDatabase, transactions::types::CryptoFactories, }; diff --git a/base_layer/core/tests/mempool.rs b/base_layer/core/tests/mempool.rs index 9c47c60c9c..1a61bf2f26 100644 --- a/base_layer/core/tests/mempool.rs +++ b/base_layer/core/tests/mempool.rs @@ -38,6 +38,7 @@ use helpers::{ use tari_crypto::keys::PublicKey as PublicKeyTrait; // use crate::helpers::database::create_store; use std::{ops::Deref, sync::Arc, time::Duration}; +use tari_common::configuration::Network; use tari_comms_dht::domain_message::OutboundDomainMessage; use tari_core::{ base_node::{ @@ -45,7 +46,7 @@ use tari_core::{ service::BaseNodeServiceConfig, state_machine_service::states::{ListeningInfo, StateInfo, StatusInfo}, }, - consensus::{ConsensusConstantsBuilder, ConsensusManagerBuilder, Network}, + consensus::{ConsensusConstantsBuilder, ConsensusManager, NetworkConsensus}, mempool::{Mempool, MempoolConfig, MempoolServiceConfig, MempoolServiceError, TxStorageResponse}, proof_of_work::Difficulty, proto, @@ -428,7 +429,7 @@ fn request_response_get_stats() { .with_emission_amounts(100_000_000.into(), &EMISSION, 100.into()) .build(); let (block0, utxo) = create_genesis_block(&factories, &consensus_constants); - let consensus_manager = ConsensusManagerBuilder::new(network) + let consensus_manager = ConsensusManager::builder(network) .with_consensus_constants(consensus_constants) .with_block(block0) .build(); @@ -483,7 +484,7 @@ fn request_response_get_tx_state_by_excess_sig() { .with_emission_amounts(100_000_000.into(), &EMISSION, 100.into()) .build(); let (block0, utxo) = create_genesis_block(&factories, &consensus_constants); - let consensus_manager = ConsensusManagerBuilder::new(network) + let consensus_manager = ConsensusManager::builder(network) .with_consensus_constants(consensus_constants) .with_block(block0) .build(); @@ -553,7 +554,7 @@ fn receive_and_propagate_transaction() { .with_emission_amounts(100_000_000.into(), &EMISSION, 100.into()) .build(); let (block0, utxo) = create_genesis_block(&factories, &consensus_constants); - let consensus_manager = ConsensusManagerBuilder::new(network) + let consensus_manager = ConsensusManager::builder(network) .with_consensus_constants(consensus_constants) .with_block(block0) .build(); @@ -748,7 +749,7 @@ fn consensus_validation_large_tx() { fn service_request_timeout() { let mut runtime = Runtime::new().unwrap(); let network = Network::LocalNet; - let consensus_manager = ConsensusManagerBuilder::new(network).build(); + let consensus_manager = ConsensusManager::builder(network).build(); let mempool_service_config = MempoolServiceConfig { request_timeout: Duration::from_millis(1), ..Default::default() @@ -789,13 +790,13 @@ fn block_event_and_reorg_event_handling() { // When block B2B is submitted with TX2B, TX3B, then TX2A, TX3A are discarded (Not Stored) let factories = CryptoFactories::default(); let network = Network::LocalNet; - let consensus_constants = network.create_consensus_constants(); + let consensus_constants = NetworkConsensus::from(network).create_consensus_constants(); let mut runtime = Runtime::new().unwrap(); let temp_dir = tempdir().unwrap(); let (block0, utxos0) = create_genesis_block_with_coinbase_value(&factories, 100_000_000.into(), &consensus_constants[0]); - let consensus_manager = ConsensusManagerBuilder::new(network) + let consensus_manager = ConsensusManager::builder(network) .with_consensus_constants(consensus_constants[0].clone()) .with_block(block0.clone()) .build(); diff --git a/base_layer/core/tests/node_comms_interface.rs b/base_layer/core/tests/node_comms_interface.rs index 23b9794006..7677b76860 100644 --- a/base_layer/core/tests/node_comms_interface.rs +++ b/base_layer/core/tests/node_comms_interface.rs @@ -26,6 +26,7 @@ mod helpers; use futures::{channel::mpsc, StreamExt}; use helpers::block_builders::append_block; use std::sync::Arc; +use tari_common::configuration::Network; use tari_common_types::chain_metadata::ChainMetadata; use tari_comms::peer_manager::NodeId; use tari_core::{ @@ -33,9 +34,9 @@ use tari_core::{ comms_interface::{CommsInterfaceError, InboundNodeCommsHandlers, NodeCommsRequest, NodeCommsResponse}, OutboundNodeCommsInterface, }, - blocks::{genesis_block, BlockBuilder, BlockHeader}, + blocks::{BlockBuilder, BlockHeader}, chain_storage::{BlockchainDatabaseConfig, DbTransaction, HistoricalBlock, Validators}, - consensus::{ConsensusManagerBuilder, Network}, + consensus::{ConsensusManager, NetworkConsensus}, mempool::{Mempool, MempoolConfig}, test_helpers::blockchain::{create_store_with_consensus_and_validators_and_config, create_test_blockchain_db}, transactions::{helpers::create_utxo, tari_amount::MicroTari, types::CryptoFactories}, @@ -80,7 +81,7 @@ async fn inbound_get_metadata() { let mempool = new_mempool(); let network = Network::LocalNet; - let consensus_manager = ConsensusManagerBuilder::new(network).build(); + let consensus_manager = ConsensusManager::builder(network).build(); let (block_event_sender, _) = broadcast::channel(50); let (request_sender, _) = reply_channel::unbounded(); let (block_sender, _) = mpsc::unbounded(); @@ -111,7 +112,7 @@ async fn inbound_fetch_kernel_by_excess_sig() { let mempool = new_mempool(); let network = Network::LocalNet; - let consensus_manager = ConsensusManagerBuilder::new(network).build(); + let consensus_manager = ConsensusManager::builder(network).build(); let (block_event_sender, _) = broadcast::channel(50); let (request_sender, _) = reply_channel::unbounded(); let (block_sender, _) = mpsc::unbounded(); @@ -160,10 +161,7 @@ async fn inbound_fetch_headers() { let store = create_test_blockchain_db(); let mempool = new_mempool(); let network = Network::LocalNet; - let consensus_constants = network.create_consensus_constants(); - let consensus_manager = ConsensusManagerBuilder::new(network) - .with_consensus_constants(consensus_constants[0].clone()) - .build(); + let consensus_manager = ConsensusManager::builder(network).build(); let (block_event_sender, _) = broadcast::channel(50); let (request_sender, _) = reply_channel::unbounded(); let (block_sender, _) = mpsc::unbounded(); @@ -214,10 +212,7 @@ async fn inbound_fetch_utxos() { let store = create_test_blockchain_db(); let mempool = new_mempool(); let network = Network::LocalNet; - let consensus_constants = network.create_consensus_constants(); - let consensus_manager = ConsensusManagerBuilder::new(network) - .with_consensus_constants(consensus_constants[0].clone()) - .build(); + let consensus_manager = ConsensusManager::builder(network).build(); let (block_event_sender, _) = broadcast::channel(50); let (request_sender, _) = reply_channel::unbounded(); let (block_sender, _) = mpsc::unbounded(); @@ -277,10 +272,7 @@ async fn inbound_fetch_txos() { let mempool = new_mempool(); let (block_event_sender, _) = broadcast::channel(50); let network = Network::LocalNet; - let consensus_constants = network.create_consensus_constants(); - let consensus_manager = ConsensusManagerBuilder::new(network) - .with_consensus_constants(consensus_constants[0].clone()) - .build(); + let consensus_manager = ConsensusManager::builder(network).build(); let (request_sender, _) = reply_channel::unbounded(); let (block_sender, _) = mpsc::unbounded(); let outbound_nci = OutboundNodeCommsInterface::new(request_sender, block_sender); @@ -324,7 +316,7 @@ async fn outbound_fetch_blocks() { let (block_sender, _) = mpsc::unbounded(); let mut outbound_nci = OutboundNodeCommsInterface::new(request_sender, block_sender); let network = Network::LocalNet; - let consensus_constants = network.create_consensus_constants(); + let consensus_constants = NetworkConsensus::from(network).create_consensus_constants(); let gb = BlockBuilder::new(consensus_constants[0].blockchain_version()).build(); let block = HistoricalBlock::new(gb, 0, Default::default(), vec![], 0); let block_response = NodeCommsResponse::HistoricalBlocks(vec![block.clone()]); @@ -343,10 +335,7 @@ async fn inbound_fetch_blocks() { let mempool = new_mempool(); let (block_event_sender, _) = broadcast::channel(50); let network = Network::LocalNet; - let consensus_constants = network.create_consensus_constants(); - let consensus_manager = ConsensusManagerBuilder::new(network) - .with_consensus_constants(consensus_constants[0].clone()) - .build(); + let consensus_manager = ConsensusManager::builder(network).build(); let (request_sender, _) = reply_channel::unbounded(); let (block_sender, _) = mpsc::unbounded(); let outbound_nci = OutboundNodeCommsInterface::new(request_sender, block_sender); @@ -375,12 +364,8 @@ async fn inbound_fetch_blocks() { // Test needs to be updated to new pruned structure. async fn inbound_fetch_blocks_before_horizon_height() { let network = Network::LocalNet; - let consensus_constants = network.create_consensus_constants(); - let block0 = genesis_block::get_weatherwax_genesis_block(); - let consensus_manager = ConsensusManagerBuilder::new(network) - .with_consensus_constants(consensus_constants[0].clone()) - .with_block(block0.clone()) - .build(); + let consensus_manager = ConsensusManager::builder(network).build(); + let block0 = consensus_manager.get_genesis_block(); let validators = Validators::new( MockValidator::new(true), MockValidator::new(true), diff --git a/base_layer/core/tests/node_service.rs b/base_layer/core/tests/node_service.rs index 021871907d..37bb4c153d 100644 --- a/base_layer/core/tests/node_service.rs +++ b/base_layer/core/tests/node_service.rs @@ -42,6 +42,7 @@ use helpers::{ }, }; use std::time::Duration; +use tari_common::configuration::Network; use tari_comms::protocol::messaging::MessagingEvent; use tari_core::{ base_node::{ @@ -51,7 +52,7 @@ use tari_core::{ }, blocks::NewBlock, chain_storage::ChainBlock, - consensus::{ConsensusConstantsBuilder, ConsensusManagerBuilder, Network}, + consensus::{ConsensusConstantsBuilder, ConsensusManager, NetworkConsensus}, mempool::MempoolServiceConfig, proof_of_work::PowAlgorithm, transactions::{ @@ -78,7 +79,7 @@ fn request_response_get_metadata() { .with_emission_amounts(100_000_000.into(), &EMISSION, 100.into()) .build(); let (block0, _) = create_genesis_block(&factories, &consensus_constants); - let consensus_manager = ConsensusManagerBuilder::new(network) + let consensus_manager = ConsensusManager::builder(network) .with_consensus_constants(consensus_constants) .with_block(block0) .build(); @@ -111,7 +112,7 @@ fn request_and_response_fetch_blocks() { .with_emission_amounts(100_000_000.into(), &EMISSION, 100.into()) .build(); let (block0, _) = create_genesis_block(&factories, &consensus_constants); - let consensus_manager = ConsensusManagerBuilder::new(network) + let consensus_manager = ConsensusManager::builder(network) .with_consensus_constants(consensus_constants) .with_block(block0.clone()) .build(); @@ -168,7 +169,7 @@ fn request_and_response_fetch_blocks_with_hashes() { .with_emission_amounts(100_000_000.into(), &EMISSION, 100.into()) .build(); let (block0, _) = create_genesis_block(&factories, &consensus_constants); - let consensus_manager = ConsensusManagerBuilder::new(network) + let consensus_manager = ConsensusManager::builder(network) .with_consensus_constants(consensus_constants) .with_block(block0.clone()) .build(); @@ -248,25 +249,25 @@ fn propagate_and_forward_many_valid_blocks() { .with_emission_amounts(100_000_000.into(), &EMISSION, 100.into()) .build(); let (block0, _) = create_genesis_block(&factories, &consensus_constants); - let rules = ConsensusManagerBuilder::new(network) + let rules = ConsensusManager::builder(network) .with_consensus_constants(consensus_constants) .with_block(block0.clone()) .build(); - let (mut alice_node, rules) = BaseNodeBuilder::new(network) + let (mut alice_node, rules) = BaseNodeBuilder::new(network.into()) .with_node_identity(alice_node_identity.clone()) .with_consensus_manager(rules) .start(&mut runtime, temp_dir.path().join("alice").to_str().unwrap()); - let (mut bob_node, rules) = BaseNodeBuilder::new(network) + let (mut bob_node, rules) = BaseNodeBuilder::new(network.into()) .with_node_identity(bob_node_identity.clone()) .with_peers(vec![alice_node_identity]) .with_consensus_manager(rules) .start(&mut runtime, temp_dir.path().join("bob").to_str().unwrap()); - let (mut carol_node, rules) = BaseNodeBuilder::new(network) + let (mut carol_node, rules) = BaseNodeBuilder::new(network.into()) .with_node_identity(carol_node_identity.clone()) .with_peers(vec![bob_node_identity.clone()]) .with_consensus_manager(rules) .start(&mut runtime, temp_dir.path().join("carol").to_str().unwrap()); - let (mut dan_node, rules) = BaseNodeBuilder::new(network) + let (mut dan_node, rules) = BaseNodeBuilder::new(network.into()) .with_node_identity(dan_node_identity) .with_peers(vec![carol_node_identity, bob_node_identity]) .with_consensus_manager(rules) @@ -357,20 +358,20 @@ fn propagate_and_forward_invalid_block_hash() { .with_emission_amounts(100_000_000.into(), &EMISSION, 100.into()) .build(); let (block0, _) = create_genesis_block(&factories, &consensus_constants); - let rules = ConsensusManagerBuilder::new(network) + let rules = ConsensusManager::builder(network) .with_consensus_constants(consensus_constants) .with_block(block0.clone()) .build(); - let (mut alice_node, rules) = BaseNodeBuilder::new(network) + let (mut alice_node, rules) = BaseNodeBuilder::new(network.into()) .with_node_identity(alice_node_identity.clone()) .with_consensus_manager(rules) .start(&mut runtime, temp_dir.path().join("alice").to_str().unwrap()); - let (mut bob_node, rules) = BaseNodeBuilder::new(network) + let (mut bob_node, rules) = BaseNodeBuilder::new(network.into()) .with_node_identity(bob_node_identity.clone()) .with_peers(vec![alice_node_identity]) .with_consensus_manager(rules) .start(&mut runtime, temp_dir.path().join("bob").to_str().unwrap()); - let (mut carol_node, rules) = BaseNodeBuilder::new(network) + let (mut carol_node, rules) = BaseNodeBuilder::new(network.into()) .with_node_identity(carol_node_identity) .with_peers(vec![bob_node_identity]) .with_consensus_manager(rules) @@ -457,18 +458,18 @@ fn propagate_and_forward_invalid_block() { .with_emission_amounts(100_000_000.into(), &EMISSION, 100.into()) .build(); let (block0, _) = create_genesis_block(&factories, &consensus_constants); - let rules = ConsensusManagerBuilder::new(network) + let rules = ConsensusManager::builder(network) .with_consensus_constants(consensus_constants) .with_block(block0.clone()) .build(); let stateless_block_validator = OrphanBlockValidator::new(rules.clone(), factories); let mock_validator = MockValidator::new(false); - let (mut dan_node, rules) = BaseNodeBuilder::new(network) + let (mut dan_node, rules) = BaseNodeBuilder::new(network.into()) .with_node_identity(dan_node_identity.clone()) .with_consensus_manager(rules) .start(&mut runtime, temp_dir.path().join("dan").to_str().unwrap()); - let (mut carol_node, rules) = BaseNodeBuilder::new(network) + let (mut carol_node, rules) = BaseNodeBuilder::new(network.into()) .with_node_identity(carol_node_identity.clone()) .with_peers(vec![dan_node_identity.clone()]) .with_consensus_manager(rules) @@ -478,13 +479,13 @@ fn propagate_and_forward_invalid_block() { stateless_block_validator.clone(), ) .start(&mut runtime, temp_dir.path().join("carol").to_str().unwrap()); - let (mut bob_node, rules) = BaseNodeBuilder::new(network) + let (mut bob_node, rules) = BaseNodeBuilder::new(network.into()) .with_node_identity(bob_node_identity.clone()) .with_peers(vec![dan_node_identity]) .with_consensus_manager(rules) .with_validators(mock_validator.clone(), mock_validator, stateless_block_validator) .start(&mut runtime, temp_dir.path().join("bob").to_str().unwrap()); - let (mut alice_node, rules) = BaseNodeBuilder::new(network) + let (mut alice_node, rules) = BaseNodeBuilder::new(network.into()) .with_node_identity(alice_node_identity) .with_peers(vec![bob_node_identity, carol_node_identity]) .with_consensus_manager(rules) @@ -554,7 +555,7 @@ fn propagate_and_forward_invalid_block() { fn service_request_timeout() { let mut runtime = Runtime::new().unwrap(); let network = Network::LocalNet; - let consensus_manager = ConsensusManagerBuilder::new(network).build(); + let consensus_manager = ConsensusManager::builder(network).build(); let base_node_service_config = BaseNodeServiceConfig { service_request_timeout: Duration::from_millis(1), fetch_blocks_timeout: Default::default(), @@ -585,7 +586,7 @@ fn local_get_metadata() { let temp_dir = tempdir().unwrap(); let network = Network::LocalNet; let (mut node, consensus_manager) = - BaseNodeBuilder::new(network).start(&mut runtime, temp_dir.path().to_str().unwrap()); + BaseNodeBuilder::new(network.into()).start(&mut runtime, temp_dir.path().to_str().unwrap()); let db = &node.blockchain_db; let block0 = db.fetch_block(0).unwrap().try_into_chain_block().unwrap(); let block1 = append_block(db, &block0, vec![], &consensus_manager, 1.into()).unwrap(); @@ -606,13 +607,13 @@ fn local_get_new_block_template_and_get_new_block() { let mut runtime = Runtime::new().unwrap(); let temp_dir = tempdir().unwrap(); let network = Network::LocalNet; - let consensus_constants = network.create_consensus_constants(); + let consensus_constants = NetworkConsensus::from(network).create_consensus_constants(); let (block0, outputs) = create_genesis_block_with_utxos(&factories, &[T, T], &consensus_constants[0]); - let rules = ConsensusManagerBuilder::new(network) + let rules = ConsensusManager::builder(network) .with_consensus_constants(consensus_constants[0].clone()) .with_block(block0) .build(); - let (mut node, _rules) = BaseNodeBuilder::new(network) + let (mut node, _rules) = BaseNodeBuilder::new(network.into()) .with_consensus_manager(rules) .start(&mut runtime, temp_dir.path().to_str().unwrap()); @@ -649,7 +650,7 @@ fn local_submit_block() { let temp_dir = tempdir().unwrap(); let network = Network::LocalNet; let (mut node, consensus_manager) = - BaseNodeBuilder::new(network).start(&mut runtime, temp_dir.path().to_str().unwrap()); + BaseNodeBuilder::new(network.into()).start(&mut runtime, temp_dir.path().to_str().unwrap()); let db = &node.blockchain_db; let mut event_stream = node.local_nci.get_block_event_stream(); diff --git a/base_layer/core/tests/node_state_machine.rs b/base_layer/core/tests/node_state_machine.rs index c9224d004f..bcbeeea436 100644 --- a/base_layer/core/tests/node_state_machine.rs +++ b/base_layer/core/tests/node_state_machine.rs @@ -30,6 +30,7 @@ use helpers::{ nodes::{create_network_with_2_base_nodes_with_config, wait_until_online, BaseNodeBuilder}, }; use std::{thread, time::Duration}; +use tari_common::configuration::Network; use tari_core::{ base_node::{ chain_metadata_service::PeerChainMetadata, @@ -42,7 +43,7 @@ use tari_core::{ }, SyncValidators, }, - consensus::{ConsensusConstantsBuilder, ConsensusManagerBuilder, Network}, + consensus::{ConsensusConstantsBuilder, ConsensusManagerBuilder}, mempool::MempoolServiceConfig, proof_of_work::randomx_factory::RandomXFactory, test_helpers::blockchain::create_test_blockchain_db, @@ -141,7 +142,7 @@ fn test_event_channel() { let temp_dir = tempdir().unwrap(); let mut runtime = Runtime::new().unwrap(); let (node, consensus_manager) = - BaseNodeBuilder::new(Network::Weatherwax).start(&mut runtime, temp_dir.path().to_str().unwrap()); + BaseNodeBuilder::new(Network::Weatherwax.into()).start(&mut runtime, temp_dir.path().to_str().unwrap()); // let shutdown = Shutdown::new(); let db = create_test_blockchain_db(); let shutdown = Shutdown::new(); diff --git a/base_layer/p2p/src/initialization.rs b/base_layer/p2p/src/initialization.rs index 38a4c1bcf7..a8b890cf95 100644 --- a/base_layer/p2p/src/initialization.rs +++ b/base_layer/p2p/src/initialization.rs @@ -25,6 +25,8 @@ use crate::{ dns_seed::DnsSeedResolver, seed_peer::SeedPeer, transport::{TorConfig, TransportType}, + MAJOR_NETWORK_VERSION, + MINOR_NETWORK_VERSION, }; use fs2::FileExt; use futures::{channel::mpsc, future, Sink}; @@ -40,6 +42,7 @@ use std::{ sync::Arc, time::{Duration, Instant}, }; +use tari_common::configuration::Network; use tari_comms::{ backoff::ConstantBackoff, peer_manager::{NodeIdentity, Peer, PeerFeatures, PeerManagerError}, @@ -48,6 +51,7 @@ use tari_comms::{ protocol::{ messaging::{MessagingEventSender, MessagingProtocolExtension}, rpc::RpcServer, + NodeNetworkInfo, }, tor, tor::HiddenServiceControllerError, @@ -120,6 +124,8 @@ pub struct CommsConfig { pub outbound_buffer_size: usize, /// Configuration for DHT pub dht: DhtConfig, + /// The p2p network currently being connected to. + pub network: Network, /// The identity of this node on the network pub node_identity: Arc, /// The type of transport to use @@ -534,7 +540,12 @@ impl ServiceInitializer for P2pInitializer { let mut builder = CommsBuilder::new() .with_shutdown_signal(context.get_shutdown_signal()) .with_node_identity(config.node_identity.clone()) - .with_user_agent(&config.user_agent); + .with_node_info(NodeNetworkInfo { + major_version: MAJOR_NETWORK_VERSION, + minor_version: MINOR_NETWORK_VERSION, + network_byte: config.network.as_byte(), + user_agent: config.user_agent.clone(), + }); if config.allow_test_addresses { builder = builder.allow_test_addresses(); diff --git a/base_layer/p2p/src/lib.rs b/base_layer/p2p/src/lib.rs index 694dc48b32..fdf12e983d 100644 --- a/base_layer/p2p/src/lib.rs +++ b/base_layer/p2p/src/lib.rs @@ -45,4 +45,13 @@ pub mod services; pub mod tari_message; pub mod transport; +// Re-export +pub use tari_common::configuration::Network; + pub const DEFAULT_DNS_SEED_RESOLVER: &str = "1.1.1.1:53"; + +/// Major network version. Peers will refuse connections if this value differs +pub const MAJOR_NETWORK_VERSION: u32 = 0; +/// Minor network version. This should change with each time the network protocol has changed in a backward-compatible +/// way. +pub const MINOR_NETWORK_VERSION: u32 = 0; diff --git a/base_layer/p2p/src/services/liveness/service.rs b/base_layer/p2p/src/services/liveness/service.rs index 71b1c4258b..3374ec9777 100644 --- a/base_layer/p2p/src/services/liveness/service.rs +++ b/base_layer/p2p/src/services/liveness/service.rs @@ -315,7 +315,7 @@ mod test { test_utils::mocks::create_connectivity_mock, }; use tari_comms_dht::{ - envelope::{DhtMessageHeader, DhtMessageType, Network}, + envelope::{DhtMessageHeader, DhtMessageType}, outbound::{DhtOutboundRequest, MessageSendState, SendMessageResponse}, }; use tari_crypto::keys::PublicKey; @@ -425,12 +425,12 @@ mod test { ); DomainMessage { dht_header: DhtMessageHeader { - version: 0, + major: 0, + minor: 0, destination: Default::default(), origin_mac: Vec::new(), ephemeral_public_key: None, message_type: DhtMessageType::None, - network: Network::LocalTest, flags: Default::default(), message_tag: MessageTag::new(), expires: None, diff --git a/base_layer/p2p/src/test_utils.rs b/base_layer/p2p/src/test_utils.rs index 4dac942a8c..816f080fec 100644 --- a/base_layer/p2p/src/test_utils.rs +++ b/base_layer/p2p/src/test_utils.rs @@ -28,7 +28,7 @@ use tari_comms::{ peer_manager::{NodeIdentity, Peer, PeerFeatures, PeerFlags}, }; use tari_comms_dht::{ - envelope::{DhtMessageFlags, DhtMessageHeader, DhtMessageType, Network, NodeDestination}, + envelope::{DhtMessageFlags, DhtMessageHeader, DhtMessageType, NodeDestination}, inbound::DhtInboundMessage, }; @@ -62,12 +62,12 @@ pub fn make_node_identity() -> Arc { pub fn make_dht_header(trace: MessageTag) -> DhtMessageHeader { DhtMessageHeader { - version: 0, + major: 0, + minor: 0, destination: NodeDestination::Unknown, origin_mac: Vec::new(), ephemeral_public_key: None, message_type: DhtMessageType::None, - network: Network::LocalTest, flags: DhtMessageFlags::NONE, message_tag: trace, expires: None, diff --git a/base_layer/wallet/src/config.rs b/base_layer/wallet/src/config.rs index e182fb8a07..cd17024068 100644 --- a/base_layer/wallet/src/config.rs +++ b/base_layer/wallet/src/config.rs @@ -26,7 +26,7 @@ use crate::{ transaction_service::config::TransactionServiceConfig, }; use std::time::Duration; -use tari_core::{consensus::Network, transactions::types::CryptoFactories}; +use tari_core::{consensus::NetworkConsensus, transactions::types::CryptoFactories}; use tari_p2p::initialization::CommsConfig; pub const KEY_MANAGER_COMMS_SECRET_KEY_BRANCH_KEY: &str = "comms"; @@ -39,7 +39,7 @@ pub struct WalletConfig { pub output_manager_service_config: Option, pub buffer_size: usize, pub rate_limit: usize, - pub network: Network, + pub network: NetworkConsensus, pub base_node_service_config: BaseNodeServiceConfig, pub scan_for_utxo_interval: Duration, } @@ -51,7 +51,7 @@ impl WalletConfig { factories: CryptoFactories, transaction_service_config: Option, output_manager_service_config: Option, - network: Network, + network: NetworkConsensus, base_node_service_config: Option, buffer_size: Option, rate_limit: Option, diff --git a/base_layer/wallet/src/output_manager_service/mod.rs b/base_layer/wallet/src/output_manager_service/mod.rs index 108a3d4954..aac9c7b882 100644 --- a/base_layer/wallet/src/output_manager_service/mod.rs +++ b/base_layer/wallet/src/output_manager_service/mod.rs @@ -34,7 +34,7 @@ use futures::future; use log::*; use tari_comms::{connectivity::ConnectivityRequester, types::CommsSecretKey}; use tari_core::{ - consensus::{ConsensusConstantsBuilder, Network}, + consensus::{ConsensusConstantsBuilder, NetworkConsensus}, transactions::types::CryptoFactories, }; use tari_service_framework::{ @@ -70,7 +70,7 @@ where T: OutputManagerBackend config: OutputManagerServiceConfig, backend: Option, factories: CryptoFactories, - network: Network, + network: NetworkConsensus, master_secret_key: CommsSecretKey, } @@ -81,7 +81,7 @@ where T: OutputManagerBackend + 'static config: OutputManagerServiceConfig, backend: T, factories: CryptoFactories, - network: Network, + network: NetworkConsensus, master_secret_key: CommsSecretKey, ) -> Self { Self { @@ -118,7 +118,7 @@ where T: OutputManagerBackend + 'static .expect("Cannot start Output Manager Service without setting a storage backend"); let factories = self.factories.clone(); let config = self.config.clone(); - let constants = ConsensusConstantsBuilder::new(self.network).build(); + let constants = ConsensusConstantsBuilder::new(self.network.as_network()).build(); let master_secret_key = self.master_secret_key.clone(); context.spawn_when_ready(move |handles| async move { let transaction_service = handles.expect_handle::(); diff --git a/base_layer/wallet/src/testnet_utils.rs b/base_layer/wallet/src/testnet_utils.rs index baa407d075..69dcdf97cd 100644 --- a/base_layer/wallet/src/testnet_utils.rs +++ b/base_layer/wallet/src/testnet_utils.rs @@ -62,22 +62,19 @@ use tari_comms::{ transports::MemoryTransport, types::{CommsPublicKey, CommsSecretKey}, }; -use tari_comms_dht::{envelope::Network as DhtNetwork, DhtConfig}; -use tari_core::{ - consensus::Network, - transactions::{ - helpers::{create_unblinded_output, TestParams as TestParamsHelpers}, - tari_amount::MicroTari, - transaction::{OutputFeatures, Transaction, TransactionInput, UnblindedOutput}, - types::{BlindingFactor, CryptoFactories, PrivateKey, PublicKey}, - }, +use tari_comms_dht::DhtConfig; +use tari_core::transactions::{ + helpers::{create_unblinded_output, TestParams as TestParamsHelpers}, + tari_amount::MicroTari, + transaction::{OutputFeatures, Transaction, TransactionInput, UnblindedOutput}, + types::{BlindingFactor, CryptoFactories, PrivateKey, PublicKey}, }; use tari_crypto::{ keys::{PublicKey as PublicKeyTrait, SecretKey as SecretKeyTrait}, script, tari_utilities::hex::Hex, }; -use tari_p2p::{initialization::CommsConfig, transport::TransportType}; +use tari_p2p::{initialization::CommsConfig, transport::TransportType, Network}; use tari_shutdown::{Shutdown, ShutdownSignal}; use tokio::{runtime::Handle, time::delay_for}; @@ -140,6 +137,7 @@ pub async fn create_wallet( .expect("Could not construct Node Identity"), ); let comms_config = CommsConfig { + network: Network::Weatherwax, transport_type: TransportType::Memory { listener_address: public_address, }, @@ -151,7 +149,6 @@ pub async fn create_wallet( user_agent: "/tari/wallet/test".to_string(), dht: DhtConfig { discovery_request_timeout: Duration::from_secs(30), - network: DhtNetwork::Weatherwax, allow_test_addresses: true, ..Default::default() }, @@ -169,7 +166,7 @@ pub async fn create_wallet( factories, None, None, - Network::Weatherwax, + Network::Weatherwax.into(), None, None, None, diff --git a/base_layer/wallet/src/transaction_service/service.rs b/base_layer/wallet/src/transaction_service/service.rs index 2cb5410b96..953499edc3 100644 --- a/base_layer/wallet/src/transaction_service/service.rs +++ b/base_layer/wallet/src/transaction_service/service.rs @@ -87,9 +87,6 @@ use tari_crypto::{keys::DiffieHellmanSharedSecret, script, tari_utilities::ByteA use tari_p2p::domain_message::DomainMessage; use tari_service_framework::{reply_channel, reply_channel::Receiver}; use tari_shutdown::ShutdownSignal; - -#[cfg(feature = "test_harness")] -use tokio::runtime::Handle; use tokio::{sync::broadcast, task::JoinHandle}; const LOG_TARGET: &str = "wallet::transaction_service::service"; @@ -2053,7 +2050,7 @@ where _tx_id: TxId, amount: MicroTari, source_public_key: CommsPublicKey, - handle: Handle, + handle: tokio::runtime::Handle, ) -> Result<(), TransactionServiceError> { use crate::{ base_node_service::{handle::BaseNodeServiceHandle, mock_base_node_service::MockBaseNodeService}, @@ -2068,7 +2065,8 @@ where transaction_service::{handle::TransactionServiceHandle, storage::models::InboundTransaction}, }; use tari_comms::types::CommsSecretKey; - use tari_core::consensus::{ConsensusConstantsBuilder, Network}; + use tari_core::consensus::ConsensusConstantsBuilder; + use tari_p2p::Network; use tempfile::tempdir; let (_sender, receiver) = reply_channel::unbounded(); diff --git a/base_layer/wallet/tests/output_manager_service/service.rs b/base_layer/wallet/tests/output_manager_service/service.rs index 76c89535dd..ea5f5bf52e 100644 --- a/base_layer/wallet/tests/output_manager_service/service.rs +++ b/base_layer/wallet/tests/output_manager_service/service.rs @@ -40,7 +40,7 @@ use tari_comms::{ }; use tari_core::{ base_node::rpc::BaseNodeWalletRpcServer, - consensus::{ConsensusConstantsBuilder, Network}, + consensus::ConsensusConstantsBuilder, transactions::{ fee::Fee, helpers::{create_unblinded_output, TestParams as TestParamsHelpers}, @@ -62,6 +62,7 @@ use tari_crypto::{ script, script::TariScript, }; +use tari_p2p::Network; use tari_service_framework::reply_channel; use tari_shutdown::Shutdown; use tari_wallet::{ diff --git a/base_layer/wallet/tests/support/comms_and_services.rs b/base_layer/wallet/tests/support/comms_and_services.rs index 9f89017328..5ca3c15d51 100644 --- a/base_layer/wallet/tests/support/comms_and_services.rs +++ b/base_layer/wallet/tests/support/comms_and_services.rs @@ -30,10 +30,7 @@ use tari_comms::{ types::CommsPublicKey, CommsNode, }; -use tari_comms_dht::{ - envelope::{DhtMessageHeader, Network}, - Dht, -}; +use tari_comms_dht::{envelope::DhtMessageHeader, Dht}; use tari_p2p::{ comms_connector::{InboundDomainConnector, PeerMessage}, domain_message::DomainMessage, @@ -85,12 +82,12 @@ pub fn create_dummy_message(inner: T, public_key: &CommsPublicKey) -> DomainM ); DomainMessage { dht_header: DhtMessageHeader { + major: Default::default(), + minor: Default::default(), ephemeral_public_key: None, origin_mac: Vec::new(), - version: Default::default(), message_type: Default::default(), flags: Default::default(), - network: Network::LocalTest, destination: Default::default(), message_tag: MessageTag::new(), expires: None, diff --git a/base_layer/wallet/tests/transaction_service/service.rs b/base_layer/wallet/tests/transaction_service/service.rs index 334dc3291b..e39da4a8ab 100644 --- a/base_layer/wallet/tests/transaction_service/service.rs +++ b/base_layer/wallet/tests/transaction_service/service.rs @@ -67,7 +67,7 @@ use tari_core::{ proto::wallet_rpc::{TxLocation, TxQueryResponse, TxSubmissionRejectionReason, TxSubmissionResponse}, rpc::BaseNodeWalletRpcServer, }, - consensus::{ConsensusConstantsBuilder, Network}, + consensus::ConsensusConstantsBuilder, proto::base_node as base_node_proto, transactions::{ fee::Fee, @@ -88,7 +88,7 @@ use tari_crypto::{ script, script::{ExecutionStack, TariScript}, }; -use tari_p2p::{comms_connector::pubsub_connector, domain_message::DomainMessage}; +use tari_p2p::{comms_connector::pubsub_connector, domain_message::DomainMessage, Network}; use tari_service_framework::{reply_channel, RegisterHandle, StackBuilder}; use tari_shutdown::{Shutdown, ShutdownSignal}; use tari_wallet::{ @@ -193,7 +193,7 @@ pub fn setup_transaction_service< OutputManagerServiceConfig::default(), oms_backend, factories.clone(), - Network::Weatherwax, + Network::Weatherwax.into(), CommsSecretKey::default(), )) .add_initializer(TransactionServiceInitializer::new( diff --git a/base_layer/wallet/tests/wallet/mod.rs b/base_layer/wallet/tests/wallet/mod.rs index 890a09ed77..336082dd6a 100644 --- a/base_layer/wallet/tests/wallet/mod.rs +++ b/base_layer/wallet/tests/wallet/mod.rs @@ -41,13 +41,10 @@ use tari_comms::{ types::{CommsPublicKey, CommsSecretKey}, }; use tari_comms_dht::DhtConfig; -use tari_core::{ - consensus::Network, - transactions::{ - helpers::{create_unblinded_output, TestParams}, - tari_amount::{uT, MicroTari}, - types::{CryptoFactories, PrivateKey, PublicKey}, - }, +use tari_core::transactions::{ + helpers::{create_unblinded_output, TestParams}, + tari_amount::{uT, MicroTari}, + types::{CryptoFactories, PrivateKey, PublicKey}, }; use tari_crypto::{ common::Blake256, @@ -55,7 +52,7 @@ use tari_crypto::{ keys::{PublicKey as PublicKeyTrait, SecretKey}, script, }; -use tari_p2p::{initialization::CommsConfig, transport::TransportType, DEFAULT_DNS_SEED_RESOLVER}; +use tari_p2p::{initialization::CommsConfig, transport::TransportType, Network, DEFAULT_DNS_SEED_RESOLVER}; use tari_shutdown::{Shutdown, ShutdownSignal}; use tari_wallet::{ contacts_service::storage::database::Contact, @@ -98,9 +95,11 @@ async fn create_wallet( passphrase: Option, recovery_master_key: Option, ) -> Result { + const NETWORK: Network = Network::Weatherwax; let node_identity = NodeIdentity::random(&mut OsRng, get_next_memory_address(), PeerFeatures::COMMUNICATION_NODE).unwrap(); let comms_config = CommsConfig { + network: NETWORK, node_identity: Arc::new(node_identity.clone()), transport_type: TransportType::Memory { listener_address: node_identity.public_address(), @@ -144,7 +143,7 @@ async fn create_wallet( factories, Some(transaction_service_config), None, - Network::Weatherwax, + NETWORK.into(), None, None, None, @@ -677,6 +676,7 @@ async fn test_import_utxo() { let temp_dir = tempdir().unwrap(); let (wallet_backend, tx_backend, oms_backend, contacts_backend, _temp_dir) = make_wallet_databases(None); let comms_config = CommsConfig { + network: Network::Weatherwax, node_identity: Arc::new(alice_identity.clone()), transport_type: TransportType::Tcp { listener_address: "/ip4/127.0.0.1/tcp/0".parse().unwrap(), @@ -701,7 +701,7 @@ async fn test_import_utxo() { factories.clone(), None, None, - Network::Weatherwax, + Network::Weatherwax.into(), None, None, None, @@ -763,8 +763,6 @@ async fn test_import_utxo() { #[cfg(feature = "test_harness")] #[tokio_macros::test] async fn test_data_generation() { - use tari_comms_dht::envelope::Network as DhtNetwork; - let mut shutdown = Shutdown::new(); use tari_wallet::testnet_utils::generate_wallet_test_data; let factories = CryptoFactories::default(); @@ -772,6 +770,7 @@ async fn test_data_generation() { NodeIdentity::random(&mut OsRng, get_next_memory_address(), PeerFeatures::COMMUNICATION_NODE).unwrap(); let temp_dir = tempdir().unwrap(); let comms_config = CommsConfig { + network: Network::Weatherwax, node_identity: Arc::new(node_id.clone()), transport_type: TransportType::Memory { listener_address: node_id.public_address(), @@ -782,7 +781,6 @@ async fn test_data_generation() { outbound_buffer_size: 100, dht: DhtConfig { discovery_request_timeout: Duration::from_millis(500), - network: DhtNetwork::Weatherwax, allow_test_addresses: true, ..Default::default() }, @@ -801,7 +799,7 @@ async fn test_data_generation() { factories, None, None, - Network::Weatherwax, + Network::Weatherwax.into(), None, None, None, diff --git a/base_layer/wallet_ffi/src/lib.rs b/base_layer/wallet_ffi/src/lib.rs index 6ad34e5453..8d4464f720 100644 --- a/base_layer/wallet_ffi/src/lib.rs +++ b/base_layer/wallet_ffi/src/lib.rs @@ -162,11 +162,8 @@ use tari_comms::{ tor, types::CommsSecretKey, }; -use tari_comms_dht::{envelope::Network as DhtNetwork, DbConnectionUrl, DhtConfig}; -use tari_core::{ - consensus::Network, - transactions::{tari_amount::MicroTari, transaction::OutputFeatures, types::CryptoFactories}, -}; +use tari_comms_dht::{DbConnectionUrl, DhtConfig}; +use tari_core::transactions::{tari_amount::MicroTari, transaction::OutputFeatures, types::CryptoFactories}; use tari_crypto::{ keys::{PublicKey, SecretKey}, script::ExecutionStack, @@ -214,6 +211,7 @@ use tari_wallet::{ use tari_core::transactions::types::Signature; use tari_crypto::script::TariScript; +use tari_p2p::Network; use tari_wallet::{ types::ValidationRetryStrategy, util::emoji::EmojiIdError, @@ -2628,6 +2626,7 @@ pub unsafe extern "C" fn comms_config_create( match ni { Ok(ni) => { let config = TariCommsConfig { + network: Network::Weatherwax, node_identity: Arc::new(ni), transport_type: (*transport_type).clone(), datastore_path, @@ -2638,7 +2637,6 @@ pub unsafe extern "C" fn comms_config_create( discovery_request_timeout: Duration::from_secs(discovery_timeout_in_secs), database_url: DbConnectionUrl::File(dht_database_path), auto_join: true, - network: DhtNetwork::Weatherwax, saf_msg_validity: Duration::from_secs(saf_message_duration_in_secs), ..Default::default() }, @@ -2910,7 +2908,7 @@ pub unsafe extern "C" fn wallet_create( ..Default::default() }), None, - Network::Weatherwax, + Network::Weatherwax.into(), None, None, None, diff --git a/common/src/configuration/global.rs b/common/src/configuration/global.rs index 0792ef5976..8e0f8aadb5 100644 --- a/common/src/configuration/global.rs +++ b/common/src/configuration/global.rs @@ -22,12 +22,11 @@ // //! # Global configuration of tari base layer system -use super::ConfigurationError; +use crate::{configuration::Network, ConfigurationError}; use config::{Config, ConfigError, Environment}; use multiaddr::Multiaddr; use std::{ convert::TryInto, - fmt::{Display, Formatter, Result as FormatResult}, net::SocketAddr, num::{NonZeroU16, TryFromIntError}, path::PathBuf, @@ -786,50 +785,6 @@ fn config_string(prefix: &str, network: &str, key: &str) -> String { format!("{}.{}.{}", prefix, network, key) } -//--------------------------------------------- Network type ------------------------------------------// -#[derive(Clone, Debug, PartialEq, Copy)] -pub enum Network { - MainNet, - Rincewind, - LocalNet, - Ridcully, - Stibbons, - Weatherwax, -} - -impl FromStr for Network { - type Err = ConfigurationError; - - fn from_str(value: &str) -> Result { - match value.to_lowercase().as_str() { - "rincewind" => Ok(Self::Rincewind), - "ridcully" => Ok(Self::Ridcully), - "stibbons" => Ok(Self::Stibbons), - "weatherwax" => Ok(Self::Weatherwax), - "mainnet" => Ok(Self::MainNet), - "localnet" => Ok(Self::LocalNet), - invalid => Err(ConfigurationError::new( - "network", - &format!("Invalid network option: {}", invalid), - )), - } - } -} - -impl Display for Network { - fn fmt(&self, f: &mut Formatter) -> FormatResult { - let msg = match self { - Self::MainNet => "mainnet", - Self::Rincewind => "rincewind", - Self::Ridcully => "ridcully", - Self::Stibbons => "stibbons", - Self::Weatherwax => "weatherwax", - Self::LocalNet => "localnet", - }; - f.write_str(msg) - } -} - //--------------------------------------------- Database type ------------------------------------------// #[derive(Debug, Clone)] pub enum DatabaseType { diff --git a/common/src/configuration/loader.rs b/common/src/configuration/loader.rs index 67f45c8062..7844ede1e9 100644 --- a/common/src/configuration/loader.rs +++ b/common/src/configuration/loader.rs @@ -49,13 +49,13 @@ //! } //! //! # let mut config = Config::new(); -//! config.set("my_node.network", "rincewind"); -//! config.set("my_node.rincewind.welcome_message", "nice to see you at unseen"); +//! config.set("my_node.network", "weatherwax"); +//! config.set("my_node.weatherwax.welcome_message", "nice to see you at unseen"); //! let my_config = ::load_from(&config).unwrap(); //! assert_eq!(my_config.welcome_message, "nice to see you at unseen"); //! ``` -use super::Network; +use crate::configuration::Network; use config::Config; use std::{ error::Error, diff --git a/common/src/configuration/mod.rs b/common/src/configuration/mod.rs index 23953f3617..d1e9b50594 100644 --- a/common/src/configuration/mod.rs +++ b/common/src/configuration/mod.rs @@ -25,12 +25,12 @@ //! ... //! [base_node] //! # common vars for all base_node instances -//! [base_node.rincewind] +//! [base_node.weatherwax] //! # overrides for rincewnd testnet //! [base_node.mainnet] //! # overrides for mainnet //! [wallet] -//! [wallet.rincewind] +//! [wallet.weatherwax] //! # etc.. //! ``` @@ -38,11 +38,8 @@ pub mod bootstrap; pub mod error; pub mod global; pub mod loader; +mod network; +pub use network::Network; pub mod seconds; pub mod utils; pub mod writer; - -pub use bootstrap::ConfigBootstrap; -pub use global::{CommsTransport, DatabaseType, GlobalConfig, Network, SocksAuthentication, TorControlAuthentication}; -pub use loader::ConfigurationError; -pub use utils::{default_config, install_default_config_file, load_configuration}; diff --git a/common/src/configuration/network.rs b/common/src/configuration/network.rs new file mode 100644 index 0000000000..a4eecceb92 --- /dev/null +++ b/common/src/configuration/network.rs @@ -0,0 +1,85 @@ +// Copyright 2021, 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 crate::ConfigurationError; +use std::{ + fmt, + fmt::{Display, Formatter}, + str::FromStr, +}; + +/// Represents the available Tari p2p networks. Only nodes with matching byte values will be able to connect, so these +/// should never be changed once released. +#[repr(u8)] +#[derive(Clone, Debug, PartialEq, Eq, Copy)] +pub enum Network { + MainNet = 0x00, + LocalNet = 0x10, + Ridcully = 0x21, + Stibbons = 0x22, + Weatherwax = 0x23, +} + +impl Network { + pub fn as_byte(self) -> u8 { + self as u8 + } +} + +impl Default for Network { + fn default() -> Self { + Network::MainNet + } +} + +impl FromStr for Network { + type Err = ConfigurationError; + + fn from_str(value: &str) -> Result { + use Network::*; + match value.to_lowercase().as_str() { + "ridcully" => Ok(Ridcully), + "stibbons" => Ok(Stibbons), + "weatherwax" => Ok(Weatherwax), + "mainnet" => Ok(MainNet), + "localnet" => Ok(LocalNet), + invalid => Err(ConfigurationError::new( + "network", + &format!("Invalid network option: {}", invalid), + )), + } + } +} + +impl Display for Network { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + use Network::*; + let msg = match self { + MainNet => "mainnet", + Ridcully => "ridcully", + Stibbons => "stibbons", + Weatherwax => "weatherwax", + LocalNet => "localnet", + }; + f.write_str(msg) + } +} diff --git a/common/src/configuration/writer.rs b/common/src/configuration/writer.rs index d14fe50148..4ffaf0026d 100644 --- a/common/src/configuration/writer.rs +++ b/common/src/configuration/writer.rs @@ -45,7 +45,7 @@ //! }; //! // Merging configs into resulting structure, accounting preset network params //! let mut config = Config::new(); -//! config.set(&MyNodeConfig::network_config_key(), "rincewind"); +//! config.set(&MyNodeConfig::network_config_key(), "weatherwax"); //! main_config.merge_into(&mut config).unwrap(); //! node_config.merge_into(&mut config).unwrap(); //! @@ -57,15 +57,15 @@ //! name = "test_server" //! //! [my_node] -//! network = "rincewind" +//! network = "weatherwax" //! -//! [my_node.rincewind] +//! [my_node.weatherwax] //! address = "localhost" //! port = 3001 //! "# //! ); //! ``` -use super::{loader::ConfigPath, ConfigurationError}; +use super::loader::{ConfigPath, ConfigurationError}; use config::Config; /// Configuration writer based on ConfigPath selectors diff --git a/common/src/lib.rs b/common/src/lib.rs index c95bb2f460..cba66cd643 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -63,7 +63,7 @@ //! # use tari_test_utils::random::string; //! # use tempfile::tempdir; //! # use structopt::StructOpt; -//! # use tari_common::configuration::bootstrap::ApplicationType; +//! # use tari_common::configuration::{Network, bootstrap::ApplicationType}; //! let mut args = ConfigBootstrap::from_args(); //! # let temp_dir = tempdir().unwrap(); //! # args.base_path = temp_dir.path().to_path_buf(); @@ -78,19 +78,20 @@ #[cfg(feature = "build")] pub mod build; -pub mod configuration; #[macro_use] mod logging; -pub use configuration::error::ConfigError; - -pub mod dir_utils; +pub mod configuration; pub use configuration::{ bootstrap::{install_configuration, ConfigBootstrap}, - global::{CommsTransport, DatabaseType, GlobalConfig, Network, SocksAuthentication, TorControlAuthentication}, + error::ConfigError, + global::{CommsTransport, DatabaseType, GlobalConfig, SocksAuthentication, TorControlAuthentication}, loader::{ConfigLoader, ConfigPath, ConfigurationError, DefaultConfigLoader, NetworkConfigPath}, utils::{default_config, install_default_config_file, load_configuration}, }; + +pub mod dir_utils; + pub use logging::initialize_logging; pub const DEFAULT_CONFIG: &str = "config/config.toml"; diff --git a/comms/Cargo.toml b/comms/Cargo.toml index a265c3e505..d239dd433b 100644 --- a/comms/Cargo.toml +++ b/comms/Cargo.toml @@ -33,7 +33,7 @@ prost = "=0.6.1" rand = "0.7.2" serde = "1.0.119" serde_derive = "1.0.119" -snow = {version="=0.6.2", features=["default-resolver"]} +snow = {version="=0.8.0", features=["default-resolver"]} thiserror = "1.0.20" tokio = {version="~0.2.19", features=["blocking", "time", "tcp", "dns", "sync", "stream", "signal"]} tokio-util = {version="0.2.0", features=["codec"]} diff --git a/comms/dht/Cargo.toml b/comms/dht/Cargo.toml index c85d065f7d..5a95519de0 100644 --- a/comms/dht/Cargo.toml +++ b/comms/dht/Cargo.toml @@ -10,7 +10,6 @@ license = "BSD-3-Clause" edition = "2018" [dependencies] -tari_common = { version = "^0.8", path = "../../common"} tari_comms = { version = "^0.8", path = "../", features = ["rpc"]} tari_comms_rpc_macros = { version = "^0.8", path = "../rpc_macros"} tari_crypto = { git = "ssh://git@github.com/tari-project/tari-crypto.git", branch = "main" } diff --git a/comms/dht/src/config.rs b/comms/dht/src/config.rs index 5694984a95..3e55c647e8 100644 --- a/comms/dht/src/config.rs +++ b/comms/dht/src/config.rs @@ -20,7 +20,7 @@ // 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 crate::{envelope::Network, network_discovery::NetworkDiscoveryConfig, storage::DbConnectionUrl}; +use crate::{network_discovery::NetworkDiscoveryConfig, storage::DbConnectionUrl}; use std::time::Duration; #[derive(Debug, Clone)] @@ -93,8 +93,6 @@ pub struct DhtConfig { /// The interval to change the random pool peers. /// Default: 2 hours pub connectivity_random_pool_refresh: Duration, - /// The active Network. Default: TestNet - pub network: Network, /// Network discovery config pub network_discovery: NetworkDiscoveryConfig, /// Length of time to ban a peer if the peer misbehaves at the DHT-level. @@ -122,15 +120,11 @@ impl DhtConfig { } pub fn default_mainnet() -> Self { - Self { - network: Network::MainNet, - ..Default::default() - } + Default::default() } pub fn default_local_test() -> Self { Self { - network: Network::LocalTest, database_url: DbConnectionUrl::Memory, saf_auto_request: false, auto_join: false, @@ -169,7 +163,6 @@ impl Default for DhtConfig { connectivity_random_pool_refresh: Duration::from_secs(2 * 60 * 60), auto_join: false, join_cooldown_interval: Duration::from_secs(10 * 60), - network: Network::TestNet, network_discovery: Default::default(), ban_duration: Duration::from_secs(6 * 60 * 60), allow_test_addresses: false, diff --git a/comms/dht/src/consts.rs b/comms/dht/src/consts.rs index 69695780ac..9f3f5eac35 100644 --- a/comms/dht/src/consts.rs +++ b/comms/dht/src/consts.rs @@ -21,4 +21,5 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. /// Version for DHT envelope -pub const DHT_ENVELOPE_HEADER_VERSION: u32 = 0; +pub const DHT_MAJOR_VERSION: u32 = 0; +pub const DHT_MINOR_VERSION: u32 = 0; diff --git a/comms/dht/src/dht.rs b/comms/dht/src/dht.rs index fcd746b7b3..dcdeea5730 100644 --- a/comms/dht/src/dht.rs +++ b/comms/dht/src/dht.rs @@ -290,7 +290,6 @@ impl Dht { ServiceBuilder::new() .layer(MetricsLayer::new(self.metrics_collector.clone())) .layer(inbound::DeserializeLayer::new(self.peer_manager.clone())) - .layer(inbound::ValidateLayer::new(self.config.network)) .layer(DedupLayer::new(self.dht_requester())) .layer(tower_filter::FilterLayer::new(self.unsupported_saf_messages_filter())) .layer(MessageLoggingLayer::new(format!( @@ -352,8 +351,7 @@ impl Dht { Arc::clone(&self.node_identity), self.dht_requester(), self.discovery_service_requester(), - self.config.network, - chrono::Duration::from_std(self.config.saf_msg_validity).unwrap(), + self.config.saf_msg_validity, )) .layer(MessageLoggingLayer::new(format!( "Outbound [{}]", diff --git a/comms/dht/src/discovery/service.rs b/comms/dht/src/discovery/service.rs index 5bd9c29b8d..5023e50418 100644 --- a/comms/dht/src/discovery/service.rs +++ b/comms/dht/src/discovery/service.rs @@ -247,7 +247,7 @@ impl DhtDiscoveryService { .filter_map(|addr| addr.parse().ok()) .collect::>(); - validate_peer_addresses(&addresses, self.config.network.is_localtest()) + validate_peer_addresses(&addresses, self.config.allow_test_addresses) .map_err(|err| DhtDiscoveryError::InvalidPeerMultiaddr(err.to_string()))?; let peer = self diff --git a/comms/dht/src/envelope.rs b/comms/dht/src/envelope.rs index 7f5bb318d9..0b93546dbb 100644 --- a/comms/dht/src/envelope.rs +++ b/comms/dht/src/envelope.rs @@ -34,7 +34,7 @@ use tari_utilities::{ByteArray, ByteArrayError}; use thiserror::Error; // Re-export applicable protos -pub use crate::proto::envelope::{dht_header::Destination, DhtEnvelope, DhtHeader, DhtMessageType, Network}; +pub use crate::proto::envelope::{dht_header::Destination, DhtEnvelope, DhtHeader, DhtMessageType}; use chrono::{DateTime, NaiveDateTime, Utc}; use prost_types::Timestamp; use tari_utilities::epoch_time::EpochTime; @@ -128,14 +128,14 @@ impl DhtMessageType { /// It is preferable to not to expose the generated prost structs publicly. #[derive(Clone, Debug, PartialEq, Eq)] pub struct DhtMessageHeader { - pub version: u32, + pub major: u32, + pub minor: u32, pub destination: NodeDestination, /// Encoded DhtOrigin. This can refer to the same peer that sent the message /// or another peer if the message is being propagated. pub origin_mac: Vec, pub ephemeral_public_key: Option, pub message_type: DhtMessageType, - pub network: Network, pub flags: DhtMessageFlags, pub message_tag: MessageTag, pub expires: Option, @@ -155,8 +155,8 @@ impl Display for DhtMessageHeader { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { write!( f, - "DhtMessageHeader (Dest:{}, Type:{:?}, Network:{:?}, Flags:{:?}, Trace:{})", - self.destination, self.message_type, self.network, self.flags, self.message_tag + "DhtMessageHeader (Dest:{}, Type:{:?}, Flags:{:?}, Trace:{})", + self.destination, self.message_type, self.flags, self.message_tag ) } } @@ -183,12 +183,12 @@ impl TryFrom for DhtMessageHeader { let expires: Option> = header.expires.map(timestamp_to_datetime); Ok(Self { - version: header.version, + major: header.major, + minor: header.minor, destination, origin_mac: header.origin_mac, ephemeral_public_key, message_type: DhtMessageType::from_i32(header.message_type).ok_or(DhtMessageError::InvalidMessageType)?, - network: Network::from_i32(header.network).ok_or(DhtMessageError::InvalidNetwork)?, flags: DhtMessageFlags::from_bits(header.flags).ok_or(DhtMessageError::InvalidMessageFlags)?, message_tag: MessageTag::from(header.message_tag), expires: expires.map(datetime_to_epochtime), @@ -211,7 +211,8 @@ impl From for DhtHeader { fn from(header: DhtMessageHeader) -> Self { let expires = header.expires.map(epochtime_to_datetime); Self { - version: header.version, + major: header.major, + minor: header.minor, ephemeral_public_key: header .ephemeral_public_key .as_ref() @@ -220,7 +221,6 @@ impl From for DhtHeader { origin_mac: header.origin_mac, destination: Some(header.destination.into()), message_type: header.message_type as i32, - network: header.network as i32, flags: header.flags.bits(), message_tag: header.message_tag.as_value(), expires: expires.map(datetime_to_timestamp), diff --git a/comms/dht/src/inbound/message.rs b/comms/dht/src/inbound/message.rs index c32f3a0df1..a49ae4b073 100644 --- a/comms/dht/src/inbound/message.rs +++ b/comms/dht/src/inbound/message.rs @@ -21,7 +21,7 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::{ - consts::DHT_ENVELOPE_HEADER_VERSION, + consts::DHT_MAJOR_VERSION, envelope::{DhtMessageFlags, DhtMessageHeader}, }; use std::{ @@ -49,7 +49,7 @@ impl DhtInboundMessage { pub fn new(tag: MessageTag, dht_header: DhtMessageHeader, source_peer: Arc, body: Vec) -> Self { Self { tag, - version: DHT_ENVELOPE_HEADER_VERSION, + version: DHT_MAJOR_VERSION, dht_header, source_peer, is_saf_message: false, diff --git a/comms/dht/src/inbound/mod.rs b/comms/dht/src/inbound/mod.rs index 69d9689f67..ec6f22acbf 100644 --- a/comms/dht/src/inbound/mod.rs +++ b/comms/dht/src/inbound/mod.rs @@ -36,6 +36,3 @@ mod error; mod message; pub use message::{DecryptedDhtMessage, DhtInboundMessage}; - -mod validate; -pub use validate::ValidateLayer; diff --git a/comms/dht/src/inbound/validate.rs b/comms/dht/src/inbound/validate.rs deleted file mode 100644 index 39a1a42c97..0000000000 --- a/comms/dht/src/inbound/validate.rs +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright 2019, 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 crate::{inbound::DhtInboundMessage, proto::envelope::Network}; -use futures::{future, future::Either, task::Context}; -use log::*; -use std::task::Poll; -use tari_comms::pipeline::PipelineError; -use tower::{layer::Layer, util::Oneshot, Service, ServiceExt}; - -const LOG_TARGET: &str = "comms::dht::validate"; - -/// # DHT validation middleware -/// -/// Takes in a `DhtInboundMessage` and checks the message header for any invalid fields -/// If an invalid message is detected a rejection message is sent to the sending peer. -#[derive(Clone)] -pub struct ValidateMiddleware { - next_service: S, - target_network: Network, -} - -impl ValidateMiddleware { - pub fn new(service: S, target_network: Network) -> Self { - Self { - next_service: service, - target_network, - } - } -} - -impl Service for ValidateMiddleware -where S: Service + Clone + Send + 'static -{ - type Error = PipelineError; - type Future = Either, future::Ready>>; - type Response = (); - - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - self.next_service.poll_ready(cx) - } - - fn call(&mut self, message: DhtInboundMessage) -> Self::Future { - let next_service = self.next_service.clone(); - let target_network = self.target_network; - if message.dht_header.network == target_network && message.dht_header.is_valid() { - trace!( - target: LOG_TARGET, - "Passing message {} to next service (Trace: {})", - message.tag, - message.dht_header.message_tag - ); - Either::Left(next_service.oneshot(message)) - } else { - debug!( - target: LOG_TARGET, - "Message is for another network (want = {:?} got = {:?}) or message header is invalid. Discarding the \ - message (Trace: {}).", - target_network, - message.dht_header.network, - message.dht_header.message_tag - ); - Either::Right(future::ready(Ok(()))) - } - } -} - -pub struct ValidateLayer { - target_network: Network, -} - -impl ValidateLayer { - pub fn new(target_network: Network) -> Self { - Self { target_network } - } -} - -impl Layer for ValidateLayer { - type Service = ValidateMiddleware; - - fn layer(&self, service: S) -> Self::Service { - ValidateMiddleware::new(service, self.target_network) - } -} - -#[cfg(test)] -mod test { - use super::*; - use crate::{ - envelope::DhtMessageFlags, - test_utils::{assert_send_static_service, make_dht_inbound_message, make_node_identity, service_spy}, - }; - use tari_test_utils::panic_context; - use tokio::runtime::Runtime; - - #[test] - fn process_message() { - let mut rt = Runtime::new().unwrap(); - let spy = service_spy(); - - let mut validate = ValidateLayer::new(Network::LocalTest).layer(spy.to_service::()); - assert_send_static_service(&validate); - - panic_context!(cx); - - assert!(validate.poll_ready(&mut cx).is_ready()); - let node_identity = make_node_identity(); - let mut msg = make_dht_inbound_message(&node_identity, Vec::new(), DhtMessageFlags::empty(), false); - msg.dht_header.network = Network::MainNet; - - rt.block_on(validate.call(msg.clone())).unwrap(); - assert_eq!(spy.call_count(), 0); - - msg.dht_header.network = Network::LocalTest; - - rt.block_on(validate.call(msg)).unwrap(); - assert_eq!(spy.call_count(), 1); - } -} diff --git a/comms/dht/src/network_discovery/on_connect.rs b/comms/dht/src/network_discovery/on_connect.rs index 5a41a6ecbc..b93657f061 100644 --- a/comms/dht/src/network_discovery/on_connect.rs +++ b/comms/dht/src/network_discovery/on_connect.rs @@ -170,7 +170,7 @@ impl OnConnect { } let addresses = peer.addresses.iter(); - match validate_peer_addresses(addresses, self.config().network.is_localtest()) { + match validate_peer_addresses(addresses, self.config().allow_test_addresses) { Ok(_) => { debug!( target: LOG_TARGET, diff --git a/comms/dht/src/outbound/broadcast.rs b/comms/dht/src/outbound/broadcast.rs index 3df9fb2919..cf27343495 100644 --- a/comms/dht/src/outbound/broadcast.rs +++ b/comms/dht/src/outbound/broadcast.rs @@ -33,7 +33,7 @@ use crate::{ message_send_state::MessageSendState, SendMessageResponse, }, - proto::envelope::{DhtMessageType, Network, OriginMac}, + proto::envelope::{DhtMessageType, OriginMac}, }; use bytes::Bytes; use chrono::{DateTime, Utc}; @@ -47,7 +47,7 @@ use futures::{ }; use log::*; use rand::rngs::OsRng; -use std::{sync::Arc, task::Poll}; +use std::{sync::Arc, task::Poll, time::Duration}; use tari_comms::{ message::{MessageExt, MessageTag}, peer_manager::{NodeId, NodeIdentity, Peer}, @@ -68,7 +68,6 @@ pub struct BroadcastLayer { dht_requester: DhtRequester, dht_discovery_requester: DhtDiscoveryRequester, node_identity: Arc, - target_network: Network, message_validity_window: chrono::Duration, } @@ -77,15 +76,14 @@ impl BroadcastLayer { node_identity: Arc, dht_requester: DhtRequester, dht_discovery_requester: DhtDiscoveryRequester, - target_network: Network, - message_validity_window: chrono::Duration, + message_validity_window: Duration, ) -> Self { BroadcastLayer { dht_requester, dht_discovery_requester, node_identity, - target_network, - message_validity_window, + message_validity_window: chrono::Duration::from_std(message_validity_window) + .expect("message_validity_window is too large"), } } } @@ -99,7 +97,6 @@ impl Layer for BroadcastLayer { Arc::clone(&self.node_identity), self.dht_requester.clone(), self.dht_discovery_requester.clone(), - self.target_network, self.message_validity_window, ) } @@ -113,7 +110,6 @@ pub struct BroadcastMiddleware { dht_requester: DhtRequester, dht_discovery_requester: DhtDiscoveryRequester, node_identity: Arc, - target_network: Network, message_validity_window: chrono::Duration, } @@ -123,7 +119,6 @@ impl BroadcastMiddleware { node_identity: Arc, dht_requester: DhtRequester, dht_discovery_requester: DhtDiscoveryRequester, - target_network: Network, message_validity_window: chrono::Duration, ) -> Self { Self { @@ -131,7 +126,6 @@ impl BroadcastMiddleware { dht_requester, dht_discovery_requester, node_identity, - target_network, message_validity_window, } } @@ -157,7 +151,6 @@ where Arc::clone(&self.node_identity), self.dht_requester.clone(), self.dht_discovery_requester.clone(), - self.target_network, msg, self.message_validity_window, ) @@ -172,7 +165,6 @@ struct BroadcastTask { dht_requester: DhtRequester, dht_discovery_requester: DhtDiscoveryRequester, request: Option, - target_network: Network, message_validity_window: chrono::Duration, } type FinalMessageParts = (Option>, Option, Bytes); @@ -185,7 +177,6 @@ where S: Service node_identity: Arc, dht_requester: DhtRequester, dht_discovery_requester: DhtDiscoveryRequester, - target_network: Network, request: DhtOutboundRequest, message_validity_window: chrono::Duration, ) -> Self { @@ -194,7 +185,6 @@ where S: Service node_identity, dht_requester, dht_discovery_requester, - target_network, request: Some(request), message_validity_window, } @@ -441,7 +431,6 @@ where S: Service destination_node_id: node_id, destination: destination.clone(), dht_message_type, - network: self.target_network, dht_flags, custom_header: custom_header.clone(), body: body.clone(), @@ -592,7 +581,6 @@ mod test { node_identity, dht_requester, dht_discover_requester, - Network::LocalTest, chrono::Duration::seconds(10800), ); assert_send_static_service(&service); @@ -637,7 +625,6 @@ mod test { Arc::new(node_identity), dht_requester, dht_discover_requester, - Network::LocalTest, chrono::Duration::seconds(10800), ); let (reply_tx, reply_rx) = oneshot::channel(); @@ -688,7 +675,6 @@ mod test { Arc::new(node_identity), dht_requester, dht_discover_requester, - Network::LocalTest, chrono::Duration::seconds(10800), ); let (reply_tx, reply_rx) = oneshot::channel(); diff --git a/comms/dht/src/outbound/message.rs b/comms/dht/src/outbound/message.rs index 4de9d95cbc..52356ec364 100644 --- a/comms/dht/src/outbound/message.rs +++ b/comms/dht/src/outbound/message.rs @@ -21,7 +21,7 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::{ - envelope::{DhtMessageFlags, DhtMessageHeader, DhtMessageType, Network, NodeDestination}, + envelope::{DhtMessageFlags, DhtMessageHeader, DhtMessageType, NodeDestination}, outbound::{message_params::FinalSendMessageParams, message_send_state::MessageSendStates}, }; use bytes::Bytes; @@ -164,7 +164,6 @@ pub struct DhtOutboundMessage { pub destination: NodeDestination, pub dht_message_type: DhtMessageType, pub reply: MessagingReplyTx, - pub network: Network, pub dht_flags: DhtMessageFlags, pub is_broadcast: bool, pub expires: Option, @@ -178,8 +177,8 @@ impl fmt::Display for DhtOutboundMessage { .map(|h| format!("{} (Propagated)", h)) .unwrap_or_else(|| { format!( - "Network: {:?}, Flags: {:?}, Destination: {}, Trace: {}", - self.network, self.dht_flags, self.destination, self.tag, + "Flags: {:?}, Destination: {}, Trace: {}", + self.dht_flags, self.destination, self.tag, ) }); write!( diff --git a/comms/dht/src/outbound/serialize.rs b/comms/dht/src/outbound/serialize.rs index 9734556f82..97c7c0df58 100644 --- a/comms/dht/src/outbound/serialize.rs +++ b/comms/dht/src/outbound/serialize.rs @@ -21,7 +21,7 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::{ - consts::DHT_ENVELOPE_HEADER_VERSION, + consts::{DHT_MAJOR_VERSION, DHT_MINOR_VERSION}, outbound::message::DhtOutboundMessage, proto::envelope::{DhtEnvelope, DhtHeader}, }; @@ -64,6 +64,7 @@ where fn call(&mut self, message: DhtOutboundMessage) -> Self::Future { let next_service = self.inner.clone(); + let DhtOutboundMessage { tag, destination_node_id, @@ -72,7 +73,6 @@ where ephemeral_public_key, destination, dht_message_type, - network, dht_flags, origin_mac, reply, @@ -86,11 +86,11 @@ where destination_node_id.short_str() ); let dht_header = custom_header.map(DhtHeader::from).unwrap_or_else(|| DhtHeader { - version: DHT_ENVELOPE_HEADER_VERSION, + major: DHT_MAJOR_VERSION, + minor: DHT_MINOR_VERSION, origin_mac: origin_mac.map(|b| b.to_vec()).unwrap_or_else(Vec::new), ephemeral_public_key: ephemeral_public_key.map(|e| e.to_vec()).unwrap_or_else(Vec::new), message_type: dht_message_type as i32, - network: network as i32, flags: dht_flags.bits(), destination: Some(destination.into()), message_tag: tag.as_value(), diff --git a/comms/dht/src/proto/envelope.proto b/comms/dht/src/proto/envelope.proto index 590e8479f9..c1e6407d7d 100644 --- a/comms/dht/src/proto/envelope.proto +++ b/comms/dht/src/proto/envelope.proto @@ -20,48 +20,32 @@ enum DhtMessageType { } message DhtHeader { - uint32 version = 1; + uint32 major = 1; + uint32 minor = 2; oneof destination { // The sender has chosen not to disclose the message destination - bool unknown = 2; + bool unknown = 3; // Destined for a particular public key - bytes public_key = 3; + bytes public_key = 4; // Destined for a particular node id, or network region - bytes node_id = 4; + bytes node_id = 5; } // Origin public key of the message. This can be the same peer that sent the message // or another peer if the message should be forwarded. This is optional but MUST be specified // if the ENCRYPTED flag is set. // If an ephemeral_public_key is specified, this MUST be encrypted using a derived ECDH shared key - bytes origin_mac = 5; + bytes origin_mac = 6; // Ephemeral public key component of the ECDH shared key. MUST be specified if the ENCRYPTED flag is set. - bytes ephemeral_public_key = 6; + bytes ephemeral_public_key = 7; // The type of message - DhtMessageType message_type = 7; - // The network for which this message is intended (e.g. TestNet, MainNet etc.) - Network network = 8; - uint32 flags = 9; + DhtMessageType message_type = 8; + uint32 flags = 10; // Message trace ID // TODO: Remove for mainnet or when testing message traces is not required - uint64 message_tag = 10; + uint64 message_tag = 11; // Expiry timestamp for the message - google.protobuf.Timestamp expires = 11; -} - -enum Network { - // Main net (default) - NetworkMainNet = 0; - // Test net - NetworkTestNet = 1; - // Network used for local tests - NetworkLocalTest = 2; - // Ridcully - NetworkRidcully = 3; - // Stibbons - NetworkStibbons = 4; - // Weatherwax - NetworkWeatherwax = 5; + google.protobuf.Timestamp expires = 12; } message DhtEnvelope { diff --git a/comms/dht/src/proto/mod.rs b/comms/dht/src/proto/mod.rs index c591fcb20a..4d3899c8c4 100644 --- a/comms/dht/src/proto/mod.rs +++ b/comms/dht/src/proto/mod.rs @@ -20,10 +20,9 @@ // 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 crate::proto::{dht::JoinMessage, envelope::Network}; +use crate::proto::dht::JoinMessage; use rand::{rngs::OsRng, RngCore}; use std::{convert::TryInto, fmt}; -use tari_common::Network as GlobalNetwork; use tari_comms::{ multiaddr::Multiaddr, peer_manager::{NodeId, Peer, PeerFeatures, PeerFlags}, @@ -52,35 +51,6 @@ pub mod message_header { tari_comms::outdir_include!("tari.dht.message_header.rs"); } -//---------------------------------- Network impl --------------------------------------------// - -impl envelope::Network { - pub fn is_mainnet(self) -> bool { - matches!(self, Network::MainNet) - } - - pub fn is_testnet(self) -> bool { - matches!(self, Network::TestNet) - } - - pub fn is_localtest(self) -> bool { - matches!(self, Network::LocalTest) - } -} - -impl From for Network { - fn from(gn: GlobalNetwork) -> Self { - match gn { - GlobalNetwork::MainNet => Network::MainNet, - GlobalNetwork::Rincewind => Network::TestNet, - GlobalNetwork::LocalNet => Network::LocalTest, - GlobalNetwork::Ridcully => Network::Ridcully, - GlobalNetwork::Stibbons => Network::Stibbons, - GlobalNetwork::Weatherwax => Network::Weatherwax, - } - } -} - //---------------------------------- JoinMessage --------------------------------------------// impl> From for JoinMessage { diff --git a/comms/dht/src/test_utils/makers.rs b/comms/dht/src/test_utils/makers.rs index 92d3d8f1c2..f91592be2e 100644 --- a/comms/dht/src/test_utils/makers.rs +++ b/comms/dht/src/test_utils/makers.rs @@ -24,7 +24,7 @@ use crate::{ envelope::{DhtMessageFlags, DhtMessageHeader, NodeDestination}, inbound::DhtInboundMessage, outbound::message::DhtOutboundMessage, - proto::envelope::{DhtEnvelope, DhtMessageType, Network, OriginMac}, + proto::envelope::{DhtEnvelope, DhtMessageType, OriginMac}, }; use rand::rngs::OsRng; use std::{convert::TryInto, sync::Arc}; @@ -77,7 +77,8 @@ pub fn make_dht_header( trace: MessageTag, ) -> DhtMessageHeader { DhtMessageHeader { - version: 0, + major: 0, + minor: 0, destination: NodeDestination::Unknown, ephemeral_public_key: if flags.is_encrypted() { Some(e_pk.clone()) } else { None }, origin_mac: if include_origin { @@ -86,7 +87,6 @@ pub fn make_dht_header( Vec::new() }, message_type: DhtMessageType::None, - network: Network::LocalTest, flags, message_tag: trace, expires: None, @@ -184,7 +184,6 @@ pub fn create_outbound_message(body: &[u8]) -> DhtOutboundMessage { destination_node_id: NodeId::default(), destination: Default::default(), dht_message_type: Default::default(), - network: Network::LocalTest, dht_flags: Default::default(), custom_header: None, body: body.to_vec().into(), diff --git a/comms/src/builder/mod.rs b/comms/src/builder/mod.rs index 6d2748ce7b..e3667a1e4a 100644 --- a/comms/src/builder/mod.rs +++ b/comms/src/builder/mod.rs @@ -47,7 +47,7 @@ use crate::{ connectivity::{ConnectivityConfig, ConnectivityRequester}, multiaddr::Multiaddr, peer_manager::{NodeIdentity, PeerManager}, - protocol::ProtocolExtensions, + protocol::{NodeNetworkInfo, ProtocolExtensions}, tor, types::CommsDatabase, }; @@ -106,7 +106,26 @@ impl CommsBuilder { /// Set the user agent string for this comms node. This string is sent once when establishing a connection. pub fn with_user_agent(mut self, user_agent: T) -> Self { - self.connection_manager_config.user_agent = user_agent.to_string(); + self.connection_manager_config.network_info.user_agent = user_agent.to_string(); + self + } + + /// Set a network byte as per [RFC-173 Versioning](https://rfc.tari.com/RFC-0173_Versioning.html) + pub fn with_network_byte(mut self, network_byte: u8) -> Self { + self.connection_manager_config.network_info.network_byte = network_byte; + self + } + + /// Set a network info (versions etc) as per [RFC-173 Versioning](https://rfc.tari.com/RFC-0173_Versioning.html) + pub fn with_node_info(mut self, node_info: NodeNetworkInfo) -> Self { + self.connection_manager_config.network_info = node_info; + self + } + + /// Set a network major and minor version as per [RFC-173 Versioning](https://rfc.tari.com/RFC-0173_Versioning.html) + pub fn with_node_version(mut self, major_version: u32, minor_version: u32) -> Self { + self.connection_manager_config.network_info.major_version = major_version; + self.connection_manager_config.network_info.minor_version = minor_version; self } diff --git a/comms/src/connection_manager/common.rs b/comms/src/connection_manager/common.rs index e3993fb419..73bd20b6ad 100644 --- a/comms/src/connection_manager/common.rs +++ b/comms/src/connection_manager/common.rs @@ -28,13 +28,13 @@ use crate::{ peer_manager::{NodeId, NodeIdentity, Peer, PeerFeatures, PeerFlags}, proto::identity::PeerIdentityMsg, protocol, - protocol::ProtocolId, + protocol::{NodeNetworkInfo, ProtocolId}, types::CommsPublicKey, PeerManager, }; use futures::StreamExt; use log::*; -use tari_crypto::tari_utilities::ByteArray; +use std::convert::TryFrom; const LOG_TARGET: &str = "comms::connection_manager::common"; @@ -46,7 +46,7 @@ pub async fn perform_identity_exchange<'p, P: IntoIterator Result { let mut control = muxer.get_yamux_control(); let stream = match direction { @@ -64,20 +64,11 @@ pub async fn perform_identity_exchange<'p, P: IntoIterator bool { - match NodeId::from_key(public_key) { - Ok(expected_node_id) => &expected_node_id == node_id, - Err(_) => false, - } -} - /// Validate the peer identity info. /// /// The following process is used to validate the peer: @@ -96,19 +87,11 @@ pub async fn validate_and_add_peer_from_peer_identity( dialed_addr: Option<&Multiaddr>, allow_test_addrs: bool, ) -> Result<(NodeId, Vec), ConnectionManagerError> { - // let peer_manager = peer_manager.inner(); - // Validate the given node id for base nodes - // TODO: This is technically a domain-level rule - let peer_node_id = - NodeId::from_bytes(&peer_identity.node_id).map_err(|_| ConnectionManagerError::PeerIdentityInvalidNodeId)?; - if !is_valid_base_node_node_id(&peer_node_id, &authenticated_public_key) { - return Err(ConnectionManagerError::PeerIdentityInvalidNodeId); - } - + let peer_node_id = NodeId::from_public_key(&authenticated_public_key); let addresses = peer_identity .addresses .into_iter() - .filter_map(|addr_str| addr_str.parse().ok()) + .filter_map(|addr_bytes| Multiaddr::try_from(addr_bytes).ok()) .collect::>(); // TODO: #banheuristic diff --git a/comms/src/connection_manager/dialer.rs b/comms/src/connection_manager/dialer.rs index fc6f00e884..973de6953c 100644 --- a/comms/src/connection_manager/dialer.rs +++ b/comms/src/connection_manager/dialer.rs @@ -28,7 +28,6 @@ use crate::{ dial_state::DialState, manager::{ConnectionManagerConfig, ConnectionManagerEvent}, peer_connection, - wire_mode::WireMode, }, multiaddr::Multiaddr, multiplexing::Yamux, @@ -53,7 +52,6 @@ use futures::{ }; use log::*; use std::{collections::HashMap, sync::Arc, time::Duration}; -use tari_crypto::tari_utilities::hex::Hex; use tari_shutdown::{Shutdown, ShutdownSignal}; use tokio::time; @@ -269,20 +267,18 @@ where self.cancel_signals.insert(peer.node_id.clone(), dial_cancel); let backoff = Arc::clone(&self.backoff); - let max_attempts = self.config.max_dial_attempts; let dial_state = DialState::new(peer, reply_tx, cancel_signal); let node_identity = Arc::clone(&self.node_identity); let peer_manager = self.peer_manager.clone(); let conn_man_notifier = self.conn_man_notifier.clone(); let supported_protocols = self.our_supported_protocols.clone(); - let user_agent = self.config.user_agent.clone(); let noise_config = self.noise_config.clone(); - let allow_test_addresses = self.config.allow_test_addresses; + let config = self.config.clone(); let dial_fut = async move { let (dial_state, dial_result) = - Self::dial_peer_with_retry(dial_state, noise_config, transport, backoff, max_attempts).await; + Self::dial_peer_with_retry(dial_state, noise_config, transport, backoff, &config).await; let cancel_signal = dial_state.get_cancel_signal(); @@ -304,8 +300,7 @@ where authenticated_public_key, conn_man_notifier, supported_protocols, - user_agent, - allow_test_addresses, + &config, cancel_signal, ) .await; @@ -343,8 +338,7 @@ where authenticated_public_key: CommsPublicKey, conn_man_notifier: mpsc::Sender, our_supported_protocols: Vec, - user_agent: String, - allow_test_addresses: bool, + config: &ConnectionManagerConfig, cancel_signal: ShutdownSignal, ) -> Result { static CONNECTION_DIRECTION: ConnectionDirection = ConnectionDirection::Outbound; @@ -366,7 +360,7 @@ where &node_identity, CONNECTION_DIRECTION, &our_supported_protocols, - user_agent, + config.network_info.clone(), ) .await?; if cancel_signal.is_terminated() { @@ -378,7 +372,7 @@ where trace!( target: LOG_TARGET, "Peer identity exchange succeeded on Outbound connection for peer '{}' (Features = {:?})", - peer_identity.node_id.to_hex(), + authenticated_public_key, features ); trace!(target: LOG_TARGET, "{:?}", peer_identity); @@ -392,7 +386,7 @@ where authenticated_public_key, peer_identity, Some(&dialed_addr), - allow_test_addresses, + config.allow_test_addresses, ) .await?; @@ -425,7 +419,7 @@ where noise_config: NoiseConfig, transport: TTransport, backoff: Arc, - max_attempts: usize, + config: &ConnectionManagerConfig, ) -> (DialState, DialResult) { // Container for dial state let mut dial_state = Some(dial_state); @@ -448,7 +442,7 @@ where futures::select! { _ = delay => { debug!(target: LOG_TARGET, "[Attempt {}] Connecting to peer '{}'", current_state.num_attempts(), current_state.peer.node_id.short_str()); - match Self::dial_peer(current_state, &noise_config, ¤t_transport).await { + match Self::dial_peer(current_state, &noise_config, ¤t_transport, config.network_info.network_byte).await { (state, Ok((socket, addr))) => { debug!(target: LOG_TARGET, "Dial succeeded for peer '{}' after {} attempt(s)", state.peer.node_id.short_str(), state.num_attempts()); break (state, Ok((socket, addr))); @@ -456,7 +450,7 @@ where // Inflight dial was cancelled (state, Err(ConnectionManagerError::DialCancelled)) => break (state, Err(ConnectionManagerError::DialCancelled)), (state, Err(_err)) => { - if state.num_attempts() >= max_attempts { + if state.num_attempts() >= config.max_dial_attempts { break (state, Err(ConnectionManagerError::ConnectFailedMaximumAttemptsReached)); } @@ -482,6 +476,7 @@ where dial_state: DialState, noise_config: &NoiseConfig, transport: &TTransport, + network_byte: u8, ) -> ( DialState, Result<(NoiseSocket, Multiaddr), ConnectionManagerError>, @@ -510,7 +505,7 @@ where ); socket - .write(&[WireMode::Comms as u8]) + .write(&[network_byte]) .await .map_err(|_| ConnectionManagerError::WireFormatSendFailed)?; diff --git a/comms/src/connection_manager/error.rs b/comms/src/connection_manager/error.rs index 3d35c52ed6..3e1b1dc877 100644 --- a/comms/src/connection_manager/error.rs +++ b/comms/src/connection_manager/error.rs @@ -64,8 +64,6 @@ pub enum ConnectionManagerError { NoiseError(String), #[error("Incoming listener stream unexpectedly closed")] IncomingListenerStreamClosed, - #[error("The peer offered a NodeId that failed to validate against it's public key")] - PeerIdentityInvalidNodeId, #[error("Peer is banned, denying connection")] PeerBanned, #[error("Unable to parse any of the network addresses offered by the connecting peer")] diff --git a/comms/src/connection_manager/listener.rs b/comms/src/connection_manager/listener.rs index 5df3c0384d..545062100c 100644 --- a/comms/src/connection_manager/listener.rs +++ b/comms/src/connection_manager/listener.rs @@ -52,7 +52,6 @@ use std::{ }, time::Duration, }; -use tari_crypto::tari_utilities::hex::Hex; use tari_shutdown::ShutdownSignal; use tokio::time; @@ -195,14 +194,12 @@ where let noise_config = self.noise_config.clone(); let config = self.config.clone(); let our_supported_protocols = self.our_supported_protocols.clone(); - let allow_test_addresses = self.config.allow_test_addresses; let liveness_session_count = self.liveness_session_count.clone(); - let user_agent = self.config.user_agent.clone(); let shutdown_signal = self.shutdown_signal.clone(); let inbound_fut = async move { match Self::read_wire_format(&mut socket, config.time_to_first_byte).await { - Some(WireMode::Comms) => { + Some(WireMode::Comms(byte)) if byte == config.network_info.network_byte => { let this_node_id_str = node_identity.node_id().short_str(); let result = Self::perform_socket_upgrade_procedure( node_identity, @@ -212,8 +209,7 @@ where socket, peer_addr, our_supported_protocols, - user_agent, - allow_test_addresses, + &config, ) .await; @@ -244,6 +240,15 @@ where }, } }, + Some(WireMode::Comms(byte)) => { + warn!( + target: LOG_TARGET, + "Peer at address '{}' sent invalid wire format byte. Expected {:x?} got: {:x?} ", + peer_addr, + config.network_info.network_byte, + byte, + ); + }, Some(WireMode::Liveness) => { if liveness_session_count.load(Ordering::SeqCst) > 0 && Self::is_address_in_liveness_cidr_range(&peer_addr, &config.liveness_cidr_allowlist) @@ -293,8 +298,7 @@ where socket: TTransport::Output, peer_addr: Multiaddr, our_supported_protocols: Vec, - user_agent: String, - allow_test_addresses: bool, + config: &ConnectionManagerConfig, ) -> Result { static CONNECTION_DIRECTION: ConnectionDirection = ConnectionDirection::Inbound; debug!( @@ -331,7 +335,7 @@ where &node_identity, CONNECTION_DIRECTION, &our_supported_protocols, - user_agent, + config.network_info.clone(), ) .await?; @@ -339,7 +343,7 @@ where debug!( target: LOG_TARGET, "Peer identity exchange succeeded on Inbound connection for peer '{}' (Features = {:?})", - peer_identity.node_id.to_hex(), + authenticated_public_key, features ); trace!(target: LOG_TARGET, "{:?}", peer_identity); @@ -350,7 +354,7 @@ where authenticated_public_key, peer_identity, None, - allow_test_addresses, + config.allow_test_addresses, ) .await?; diff --git a/comms/src/connection_manager/manager.rs b/comms/src/connection_manager/manager.rs index e7c5460de8..58b17de26b 100644 --- a/comms/src/connection_manager/manager.rs +++ b/comms/src/connection_manager/manager.rs @@ -32,10 +32,9 @@ use crate::{ multiplexing::Substream, noise::NoiseConfig, peer_manager::{NodeId, NodeIdentity}, - protocol::{ProtocolEvent, ProtocolId, Protocols}, + protocol::{NodeNetworkInfo, ProtocolEvent, ProtocolId, Protocols}, runtime, transports::Transport, - types::DEFAULT_LISTENER_ADDRESS, PeerManager, }; use futures::{ @@ -107,24 +106,25 @@ pub struct ConnectionManagerConfig { /// Set to true to allow peers to send loopback, local-link and other addresses normally not considered valid for /// peer-to-peer comms. Default: false pub allow_test_addresses: bool, + /// Version information for this node + pub network_info: NodeNetworkInfo, /// The maximum time to wait for the first byte before closing the connection. Default: 7s pub time_to_first_byte: Duration, /// The number of liveness check sessions to allow. Default: 0 pub liveness_max_sessions: usize, /// CIDR blocks that allowlist liveness checks. Default: Localhost only (127.0.0.1/32) pub liveness_cidr_allowlist: Vec, - /// The user agent string for this node - pub user_agent: String, } impl Default for ConnectionManagerConfig { fn default() -> Self { Self { - listener_address: DEFAULT_LISTENER_ADDRESS + listener_address: "/ip4/0.0.0.0/tcp/7898" .parse() .expect("DEFAULT_LISTENER_ADDRESS is malformed"), max_dial_attempts: 3, max_simultaneous_inbound_connects: 20, + network_info: Default::default(), #[cfg(not(test))] allow_test_addresses: false, // This must always be true for internal crate tests @@ -133,7 +133,6 @@ impl Default for ConnectionManagerConfig { liveness_max_sessions: 0, time_to_first_byte: Duration::from_secs(7), liveness_cidr_allowlist: vec![cidr::AnyIpCidr::V4("127.0.0.1/32".parse().unwrap())], - user_agent: Default::default(), } } } diff --git a/comms/src/connection_manager/tests/manager.rs b/comms/src/connection_manager/tests/manager.rs index e0731adaa1..ecad64586d 100644 --- a/comms/src/connection_manager/tests/manager.rs +++ b/comms/src/connection_manager/tests/manager.rs @@ -108,7 +108,7 @@ async fn dial_success() { node_identity: node_identity1.clone(), ..Default::default() }; - config.connection_manager_config.user_agent = "node1".to_string(); + config.connection_manager_config.network_info.user_agent = "node1".to_string(); config }, peer_manager1.clone(), @@ -127,7 +127,7 @@ async fn dial_success() { node_identity: node_identity2.clone(), ..Default::default() }; - config.connection_manager_config.user_agent = "node2".to_string(); + config.connection_manager_config.network_info.user_agent = "node2".to_string(); config }, peer_manager2.clone(), @@ -308,7 +308,7 @@ async fn dial_cancelled() { dial_backoff_duration: Duration::from_secs(100), ..Default::default() }; - config.connection_manager_config.user_agent = "node1".to_string(); + config.connection_manager_config.network_info.user_agent = "node1".to_string(); config }, peer_manager1.clone(), diff --git a/comms/src/connection_manager/wire_mode.rs b/comms/src/connection_manager/wire_mode.rs index 57344537ad..e2421c078b 100644 --- a/comms/src/connection_manager/wire_mode.rs +++ b/comms/src/connection_manager/wire_mode.rs @@ -22,13 +22,11 @@ use std::convert::TryFrom; -const COMMS_WIRE_MODE: u8 = 0x07; const LIVENESS_WIRE_MODE: u8 = 0x46; // E -#[repr(u8)] pub enum WireMode { - Comms = COMMS_WIRE_MODE, - Liveness = LIVENESS_WIRE_MODE, + Comms(u8), + Liveness, } impl TryFrom for WireMode { @@ -36,9 +34,8 @@ impl TryFrom for WireMode { fn try_from(value: u8) -> Result { match value { - COMMS_WIRE_MODE => Ok(WireMode::Comms), LIVENESS_WIRE_MODE => Ok(WireMode::Liveness), - _ => Err(()), + v => Ok(WireMode::Comms(v)), } } } diff --git a/comms/src/lib.rs b/comms/src/lib.rs index 0d1a7d3a25..5e9864fc32 100644 --- a/comms/src/lib.rs +++ b/comms/src/lib.rs @@ -40,7 +40,6 @@ pub mod framing; mod common; pub use common::rate_limit; -mod consts; mod multiplexing; pub use multiplexing::Substream; diff --git a/comms/src/peer_manager/peer.rs b/comms/src/peer_manager/peer.rs index 69637ff735..cb944f6fe1 100644 --- a/comms/src/peer_manager/peer.rs +++ b/comms/src/peer_manager/peer.rs @@ -27,7 +27,6 @@ use super::{ PeerFeatures, }; use crate::{ - consts::PEER_OFFLINE_COOLDOWN_PERIOD, net_address::MultiaddressesWithStats, protocol::ProtocolId, types::CommsPublicKey, @@ -143,16 +142,6 @@ impl Peer { &self.supported_protocols } - /// Returns true if the last connection attempt has failed within the constant - /// [PEER_OFFLINE_COOLDOWN_PERIOD](crate::consts::PEER_OFFLINE_COOLDOWN_PERIOD). - pub fn is_recently_offline(&self) -> bool { - self.connection_stats.failed_attempts() > 1 && - self.connection_stats - .time_since_last_failure() - .map(|last_failure| last_failure <= PEER_OFFLINE_COOLDOWN_PERIOD) - .unwrap_or(false) - } - /// Returns true if the peer is marked as offline pub fn is_offline(&self) -> bool { self.offline_at.is_some() diff --git a/comms/src/peer_manager/peer_storage.rs b/comms/src/peer_manager/peer_storage.rs index c85bb1758a..46e9d00c6d 100644 --- a/comms/src/peer_manager/peer_storage.rs +++ b/comms/src/peer_manager/peer_storage.rs @@ -21,7 +21,6 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::{ - consts::PEER_MANAGER_MAX_FLOOD_PEERS, peer_manager::{ node_id::{NodeDistance, NodeId}, peer::{Peer, PeerFlags}, @@ -40,6 +39,8 @@ use std::{collections::HashMap, time::Duration}; use tari_storage::{IterationResult, KeyValueStore}; const LOG_TARGET: &str = "comms::peer_manager::peer_storage"; +/// The maximum number of peers to return from the flood_identities method in peer manager +const PEER_MANAGER_MAX_FLOOD_PEERS: usize = 1000; /// PeerStorage provides a mechanism to keep a datastore and a local copy of all peers in sync and allow fast searches /// using the node_id, public key or net_address of a peer. diff --git a/comms/src/proto/identity.proto b/comms/src/proto/identity.proto index ad6a2543a9..bf4d9f5a78 100644 --- a/comms/src/proto/identity.proto +++ b/comms/src/proto/identity.proto @@ -3,9 +3,12 @@ syntax = "proto3"; package tari.comms.identity; message PeerIdentityMsg { - bytes node_id = 1; - repeated string addresses = 2; - uint64 features = 3; - repeated bytes supported_protocols = 4; - string user_agent = 5; + repeated bytes addresses = 1; + uint64 features = 2; + repeated bytes supported_protocols = 3; + string user_agent = 4; + // Major node version. This must match the current node's version in order for the connection to be established. + uint32 major = 5; + // Minor node version. This indicates minor non-breaking changes. + uint32 minor = 6; } diff --git a/comms/src/protocol/identity.rs b/comms/src/protocol/identity.rs index eee8b05fc1..46a0b87b08 100644 --- a/comms/src/protocol/identity.rs +++ b/comms/src/protocol/identity.rs @@ -25,25 +25,24 @@ use crate::{ message::MessageExt, peer_manager::NodeIdentity, proto::identity::PeerIdentityMsg, - protocol::{ProtocolError, ProtocolId, ProtocolNegotiation}, + protocol::{NodeNetworkInfo, ProtocolError, ProtocolId, ProtocolNegotiation}, }; use futures::{AsyncRead, AsyncWrite, SinkExt, StreamExt}; use log::*; use prost::Message; use std::{io, time::Duration}; -use tari_crypto::tari_utilities::ByteArray; use thiserror::Error; use tokio::time; use tokio_util::codec::{Framed, LengthDelimitedCodec}; -pub static IDENTITY_PROTOCOL: ProtocolId = ProtocolId::from_static(b"/tari/identity/1.0.0"); +pub static IDENTITY_PROTOCOL: ProtocolId = ProtocolId::from_static(b"t/identity/1.0"); const LOG_TARGET: &str = "comms::protocol::identity"; pub async fn identity_exchange<'p, TSocket, P>( node_identity: &NodeIdentity, direction: ConnectionDirection, our_supported_protocols: P, - user_agent: String, + network_info: NodeNetworkInfo, mut socket: TSocket, ) -> Result where @@ -85,11 +84,12 @@ where // Send this node's identity let msg_bytes = PeerIdentityMsg { - node_id: node_identity.node_id().to_vec(), - addresses: vec![node_identity.public_address().to_string()], + addresses: vec![node_identity.public_address().to_vec()], features: node_identity.features().bits(), supported_protocols, - user_agent, + major: network_info.major_version, + minor: network_info.minor_version, + user_agent: network_info.user_agent, } .to_encoded_bytes(); @@ -102,6 +102,17 @@ where .ok_or(IdentityProtocolError::PeerUnexpectedCloseConnection)??; let identity_msg = PeerIdentityMsg::decode(msg_bytes)?; + if identity_msg.major != network_info.major_version { + warn!( + target: LOG_TARGET, + "Peer sent mismatching major protocol version '{}'. This node has version '{}.{}'", + identity_msg.major, + network_info.major_version, + network_info.minor_version + ); + return Err(IdentityProtocolError::ProtocolVersionMismatch); + } + Ok(identity_msg) } @@ -119,6 +130,8 @@ pub enum IdentityProtocolError { PeerUnexpectedCloseConnection, #[error("Timeout waiting for peer to send identity information")] Timeout, + #[error("Protocol version mismatch")] + ProtocolVersionMismatch, } impl From for IdentityProtocolError { @@ -150,12 +163,12 @@ mod test { use crate::{ connection_manager::ConnectionDirection, peer_manager::PeerFeatures, + protocol::{IdentityProtocolError, NodeNetworkInfo}, runtime, test_utils::node_identity::build_node_identity, transports::{MemoryTransport, Transport}, }; use futures::{future, StreamExt}; - use tari_crypto::tari_utilities::ByteArray; #[runtime::test_basic] async fn identity_exchange() { @@ -176,14 +189,20 @@ mod test { &node_identity1, ConnectionDirection::Inbound, &[], - Default::default(), + NodeNetworkInfo { + minor_version: 1, + ..Default::default() + }, in_sock, ), super::identity_exchange( &node_identity2, ConnectionDirection::Outbound, &[], - Default::default(), + NodeNetworkInfo { + minor_version: 2, + ..Default::default() + }, out_sock, ), ) @@ -193,12 +212,55 @@ mod test { let identity2 = result1.unwrap(); let identity1 = result2.unwrap(); - assert_eq!(identity1.node_id, node_identity1.node_id().to_vec()); assert_eq!(identity1.features, node_identity1.features().bits()); - assert_eq!(identity1.addresses, vec![node_identity1.public_address().to_string()]); + assert_eq!(identity1.addresses, vec![node_identity1.public_address().to_vec()]); - assert_eq!(identity2.node_id, node_identity2.node_id().to_vec()); assert_eq!(identity2.features, node_identity2.features().bits()); - assert_eq!(identity2.addresses, vec![node_identity2.public_address().to_string()]); + assert_eq!(identity2.addresses, vec![node_identity2.public_address().to_vec()]); + } + + #[runtime::test_basic] + async fn fail_cases() { + let transport = MemoryTransport; + let addr = "/memory/0".parse().unwrap(); + let (mut listener, addr) = transport.listen(addr).unwrap().await.unwrap(); + + let (out_sock, in_sock) = future::join(transport.dial(addr).unwrap(), listener.next()).await; + + let out_sock = out_sock.unwrap(); + let in_sock = in_sock.unwrap().map(|(f, _)| f).unwrap().await.unwrap(); + + let node_identity1 = build_node_identity(PeerFeatures::COMMUNICATION_NODE); + let node_identity2 = build_node_identity(PeerFeatures::COMMUNICATION_CLIENT); + + let (result1, result2) = future::join( + super::identity_exchange( + &node_identity1, + ConnectionDirection::Inbound, + &[], + NodeNetworkInfo { + major_version: 0, + ..Default::default() + }, + in_sock, + ), + super::identity_exchange( + &node_identity2, + ConnectionDirection::Outbound, + &[], + NodeNetworkInfo { + major_version: 1, + ..Default::default() + }, + out_sock, + ), + ) + .await; + + let err = result1.unwrap_err(); + assert!(matches!(err, IdentityProtocolError::ProtocolVersionMismatch)); + + let err = result2.unwrap_err(); + assert!(matches!(err, IdentityProtocolError::ProtocolVersionMismatch)); } } diff --git a/comms/src/protocol/messaging/protocol.rs b/comms/src/protocol/messaging/protocol.rs index 5ef5e8de95..6e53e80e6c 100644 --- a/comms/src/protocol/messaging/protocol.rs +++ b/comms/src/protocol/messaging/protocol.rs @@ -50,7 +50,7 @@ use tokio::sync::broadcast; use tokio_util::codec::{Framed, LengthDelimitedCodec}; const LOG_TARGET: &str = "comms::protocol::messaging"; -pub(super) static MESSAGING_PROTOCOL: Bytes = Bytes::from_static(b"/tari/messaging/0.1.0"); +pub(super) static MESSAGING_PROTOCOL: Bytes = Bytes::from_static(b"t/msg/0.1"); const INTERNAL_MESSAGING_EVENT_CHANNEL_SIZE: usize = 150; /// The maximum amount of inbound messages to accept within the `RATE_LIMIT_RESTOCK_INTERVAL` window diff --git a/comms/src/protocol/mod.rs b/comms/src/protocol/mod.rs index 7f22371851..27628a183f 100644 --- a/comms/src/protocol/mod.rs +++ b/comms/src/protocol/mod.rs @@ -32,6 +32,9 @@ pub use identity::{identity_exchange, IdentityProtocolError, IDENTITY_PROTOCOL}; mod negotiation; pub use negotiation::ProtocolNegotiation; +mod network_info; +pub use network_info::NodeNetworkInfo; + mod protocols; pub use protocols::{ProtocolEvent, ProtocolNotification, ProtocolNotificationRx, ProtocolNotificationTx, Protocols}; diff --git a/comms/src/consts.rs b/comms/src/protocol/network_info.rs similarity index 61% rename from comms/src/consts.rs rename to comms/src/protocol/network_info.rs index 3773cc15c0..7a122233a1 100644 --- a/comms/src/consts.rs +++ b/comms/src/protocol/network_info.rs @@ -1,4 +1,4 @@ -// Copyright 2019 The Tari Project +// Copyright 2021, The Tari Project // // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the // following conditions are met: @@ -20,11 +20,19 @@ // 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::time::Duration; - -/// The maximum number of peers to return from the flood_identities method in peer manager -pub const PEER_MANAGER_MAX_FLOOD_PEERS: usize = 1000; - -/// The amount of time to consider a peer to be offline (i.e. dial to peer will fail without trying) after a failed -/// connection attempt -pub const PEER_OFFLINE_COOLDOWN_PERIOD: Duration = Duration::from_secs(60); +/// Represents the current nodes network info +#[derive(Debug, Clone, Default)] +pub struct NodeNetworkInfo { + /// Major protocol version. This indicates the protocol version that is supported by this node. A peer MAY reject + /// the connection if a remote peer advertises a different major version number. + pub major_version: u32, + /// Minor protocol version. A version number that represents backward-compatible protocol changes. A peer SHOULD + /// NOT reject the connection if a remote peer advertises a different minor version number. + pub minor_version: u32, + /// The byte that MUST be sent (outbound connections) or MUST be received (inbound connections) for a connection to + /// be established. This byte cannot be 0x46 (E) because that is reserved for liveness. + /// Default: 0x00 + pub network_byte: u8, + /// The user agent string for this node + pub user_agent: String, +} diff --git a/comms/src/types.rs b/comms/src/types.rs index aa3e6e3105..847ecc8476 100644 --- a/comms/src/types.rs +++ b/comms/src/types.rs @@ -28,10 +28,6 @@ use tari_storage::HashmapDatabase; #[cfg(not(test))] use tari_storage::LMDBWrapper; -/// The default port that control services listen on -pub const DEFAULT_CONTROL_PORT_ADDRESS: &str = "/ip4/0.0.0.0/tcp/7899"; -pub const DEFAULT_LISTENER_ADDRESS: &str = "/ip4/0.0.0.0/tcp/7898"; - /// Specify the digest type for the signature challenges pub type Challenge = Blake256;