From 248409486ca1b9a453ce400176c40a8a5be26754 Mon Sep 17 00:00:00 2001 From: Stanimal Date: Fri, 6 May 2022 15:21:01 +0200 Subject: [PATCH 1/8] Remove unused Peer::unset_id --- comms/core/src/peer_manager/peer.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/comms/core/src/peer_manager/peer.rs b/comms/core/src/peer_manager/peer.rs index 6dabf87e17..e937863e3f 100644 --- a/comms/core/src/peer_manager/peer.rs +++ b/comms/core/src/peer_manager/peer.rs @@ -159,11 +159,6 @@ impl Peer { .map(|since| Duration::from_millis(u64::try_from(since.num_milliseconds()).unwrap())) } - /// TODO: Remove once we don't have to sync wallet and base node db - pub fn unset_id(&mut self) { - self.id = None; - } - pub(super) fn set_id(&mut self, id: PeerId) { self.id = Some(id); } @@ -174,7 +169,6 @@ impl Peer { } #[allow(clippy::option_option)] - pub fn update( &mut self, net_addresses: Option>, From e8f27e97a8518fc6d9f63a0c0aa6e892e3e19836 Mon Sep 17 00:00:00 2001 From: Stanimal Date: Fri, 6 May 2022 16:16:31 +0200 Subject: [PATCH 2/8] Add SEED flag to Peer --- comms/core/src/peer_manager/peer.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/comms/core/src/peer_manager/peer.rs b/comms/core/src/peer_manager/peer.rs index e937863e3f..208f215229 100644 --- a/comms/core/src/peer_manager/peer.rs +++ b/comms/core/src/peer_manager/peer.rs @@ -53,6 +53,7 @@ bitflags! { #[derive(Default, Deserialize, Serialize)] pub struct PeerFlags: u8 { const NONE = 0x00; + const SEED = 0x01; } } @@ -299,6 +300,15 @@ impl Peer { self } + pub fn add_flags(&mut self, flags: PeerFlags) -> &mut Self { + self.flags |= flags; + self + } + + pub fn is_seed(&self) -> bool { + self.flags.contains(PeerFlags::SEED) + } + pub fn to_short_string(&self) -> String { format!( "{}::{}", From 6548cc84a11ad3b5ba07501bd05d4d648fedc677 Mon Sep 17 00:00:00 2001 From: Stanimal Date: Fri, 6 May 2022 16:17:02 +0200 Subject: [PATCH 3/8] Only sync seed peer if it was updated after being added --- comms/dht/src/peer_validator.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/comms/dht/src/peer_validator.rs b/comms/dht/src/peer_validator.rs index d9e1d71c42..f661a2b5b0 100644 --- a/comms/dht/src/peer_validator.rs +++ b/comms/dht/src/peer_validator.rs @@ -88,11 +88,14 @@ impl<'a> PeerValidator<'a> { .map(|i| i.updated_at()) .expect("unreachable panic"); - // Update if new_peer has newer timestamp than current_peer + // Update if new_peer has newer timestamp than current_peer, and if the newer timestamp is after the + // added date current_peer .identity_signature .as_ref() - .map(|i| i.updated_at() < new_dt) + .map(|i| i.updated_at() < new_dt && ( + !current_peer.is_seed() || + current_peer.added_at < new_dt.naive_utc())) // If None, update to peer with valid signature .unwrap_or(true) }; From 05fcc1230d1c82da73694d069752d48903dd77a1 Mon Sep 17 00:00:00 2001 From: Stanimal Date: Fri, 6 May 2022 16:17:27 +0200 Subject: [PATCH 4/8] Add PeerValidator unit tests --- comms/core/Cargo.toml | 2 +- comms/dht/src/peer_validator.rs | 111 ++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/comms/core/Cargo.toml b/comms/core/Cargo.toml index 675a70c380..79d496dd84 100644 --- a/comms/core/Cargo.toml +++ b/comms/core/Cargo.toml @@ -21,7 +21,7 @@ async-trait = "0.1.36" bitflags = "1.0.4" blake2 = "0.9.0" bytes = { version = "1", features = ["serde"] } -chrono = { version = "0.4.19", default-features = false, features = ["serde"] } +chrono = { version = "0.4.19", default-features = false, features = ["serde", "clock"] } cidr = "0.1.0" clear_on_drop = "=0.2.4" data-encoding = "2.2.0" diff --git a/comms/dht/src/peer_validator.rs b/comms/dht/src/peer_validator.rs index f661a2b5b0..a2fe111ad8 100644 --- a/comms/dht/src/peer_validator.rs +++ b/comms/dht/src/peer_validator.rs @@ -137,3 +137,114 @@ fn validate_node_id(public_key: &CommsPublicKey, node_id: &NodeId) -> Result Date: Fri, 6 May 2022 16:17:55 +0200 Subject: [PATCH 5/8] Set SEED flag on seed peers --- base_layer/p2p/src/initialization.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/base_layer/p2p/src/initialization.rs b/base_layer/p2p/src/initialization.rs index f17e4afde3..648ae58419 100644 --- a/base_layer/p2p/src/initialization.rs +++ b/base_layer/p2p/src/initialization.rs @@ -39,7 +39,7 @@ use rand::{distributions::Alphanumeric, thread_rng, Rng}; use tari_common::configuration::Network; use tari_comms::{ backoff::ConstantBackoff, - peer_manager::{NodeIdentity, Peer, PeerFeatures, PeerManagerError}, + peer_manager::{NodeIdentity, Peer, PeerFeatures, PeerFlags, PeerManagerError}, pipeline, protocol::{ messaging::{MessagingEventSender, MessagingProtocolExtension}, @@ -143,7 +143,7 @@ pub async fn initialize_local_test_comms>( .with_shutdown_signal(shutdown_signal) .build()?; - add_all_peers(&comms.peer_manager(), &comms.node_identity(), seed_peers).await?; + add_seed_peers(&comms.peer_manager(), &comms.node_identity(), seed_peers).await?; // Create outbound channel let (outbound_tx, outbound_rx) = mpsc::channel(10); @@ -384,12 +384,12 @@ fn acquire_exclusive_file_lock(db_path: &Path) -> Result, ) -> Result<(), CommsInitializationError> { - for peer in peers { + for mut peer in peers { if &peer.public_key == node_identity.public_key() { debug!( target: LOG_TARGET, @@ -397,6 +397,7 @@ async fn add_all_peers( ); continue; } + peer.add_flags(PeerFlags::SEED); debug!(target: LOG_TARGET, "Adding seed peer [{}]", peer); peer_manager @@ -544,12 +545,12 @@ impl ServiceInitializer for P2pInitializer { Vec::new() }, }; - add_all_peers(&peer_manager, &node_identity, peers).await?; + add_seed_peers(&peer_manager, &node_identity, peers).await?; // TODO: Use serde let peers = Self::try_parse_seed_peers(&self.seed_config.peer_seeds)?; - add_all_peers(&peer_manager, &node_identity, peers).await?; + add_seed_peers(&peer_manager, &node_identity, peers).await?; context.register_handle(comms.connectivity()); context.register_handle(peer_manager); From 9edcab9a5626742682d48cec68ff6826855d8ef3 Mon Sep 17 00:00:00 2001 From: Stanimal Date: Fri, 6 May 2022 16:18:47 +0200 Subject: [PATCH 6/8] Display peer flags in `get-peer` and `list-peers` --- applications/tari_base_node/src/commands/command/get_peer.rs | 1 + .../tari_base_node/src/commands/command/list_peers.rs | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/applications/tari_base_node/src/commands/command/get_peer.rs b/applications/tari_base_node/src/commands/command/get_peer.rs index 46fe8d9c49..0b1e3c2274 100644 --- a/applications/tari_base_node/src/commands/command/get_peer.rs +++ b/applications/tari_base_node/src/commands/command/get_peer.rs @@ -90,6 +90,7 @@ impl CommandContext { }); println!("User agent: {}", peer.user_agent); println!("Features: {:?}", peer.features); + println!("Flags: {:?}", peer.flags); println!("Supported protocols:"); peer.supported_protocols.iter().for_each(|p| { println!("- {}", String::from_utf8_lossy(p)); diff --git a/applications/tari_base_node/src/commands/command/list_peers.rs b/applications/tari_base_node/src/commands/command/list_peers.rs index 1a0685a777..7587b28e3e 100644 --- a/applications/tari_base_node/src/commands/command/list_peers.rs +++ b/applications/tari_base_node/src/commands/command/list_peers.rs @@ -63,7 +63,9 @@ impl CommandContext { for peer in peers { let info_str = { let mut s = vec![]; - + if peer.is_seed() { + s.push("SEED".to_string()); + } if peer.is_offline() { if !peer.is_banned() { s.push("OFFLINE".to_string()); From c1ae7ef169bc4a15be62038d1cd5f2ac7e9c0271 Mon Sep 17 00:00:00 2001 From: Stanimal Date: Mon, 9 May 2022 08:36:25 +0200 Subject: [PATCH 7/8] minor wording change for cucumber test --- integration_tests/features/Sync.feature | 9 ++++----- integration_tests/features/support/steps.js | 15 ++++++--------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/integration_tests/features/Sync.feature b/integration_tests/features/Sync.feature index 5c43b6f3ee..a76179a51b 100644 --- a/integration_tests/features/Sync.feature +++ b/integration_tests/features/Sync.feature @@ -46,15 +46,14 @@ Feature: Block Sync Then all nodes are at height 20 @critical - Scenario: When a new node joins the network, it should receive all peers + Scenario: When a new node joins the network, it receives all peers Given I have 10 seed nodes And I have a base node NODE1 connected to all seed nodes # additional peer seeds are being included from config.toml [common] - Then NODE1 should have at least 10 peers + Then NODE1 has at least 10 peers Given I have a base node NODE2 connected to node NODE1 - Then NODE1 should have at least 11 peers - Then NODE2 should have at least 11 peers - + Then NODE1 has at least 11 peers + And NODE2 has at least 11 peers Scenario: Pruned mode sync test Given I have a seed node SEED diff --git a/integration_tests/features/support/steps.js b/integration_tests/features/support/steps.js index f7682bb904..1d42f3f6e5 100644 --- a/integration_tests/features/support/steps.js +++ b/integration_tests/features/support/steps.js @@ -407,15 +407,12 @@ Then(/(.*) should have (\d+) peers/, async function (nodeName, peerCount) { expect(peers.length).to.equal(peerCount); }); -Then( - /(.*) should have at least (\d+) peers/, - async function (nodeName, peerCount) { - await sleep(500); - const client = this.getClient(nodeName); - const peers = await client.getPeers(); - expect(peers.length).to.be.greaterThanOrEqual(peerCount); - } -); +Then(/(.*) has at least (\d+) peers/, async function (nodeName, peerCount) { + await sleep(500); + const client = this.getClient(nodeName); + const peers = await client.getPeers(); + expect(peers.length).to.be.greaterThanOrEqual(peerCount); +}); When("I print the world", function () { console.log(this); From e0f9dc6431512709953be11538b86ead72e39921 Mon Sep 17 00:00:00 2001 From: Stanimal Date: Mon, 9 May 2022 11:08:30 +0200 Subject: [PATCH 8/8] cucumber: Poll for new peers --- integration_tests/features/support/steps.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/integration_tests/features/support/steps.js b/integration_tests/features/support/steps.js index 1d42f3f6e5..742e17331f 100644 --- a/integration_tests/features/support/steps.js +++ b/integration_tests/features/support/steps.js @@ -408,10 +408,11 @@ Then(/(.*) should have (\d+) peers/, async function (nodeName, peerCount) { }); Then(/(.*) has at least (\d+) peers/, async function (nodeName, peerCount) { - await sleep(500); const client = this.getClient(nodeName); - const peers = await client.getPeers(); - expect(peers.length).to.be.greaterThanOrEqual(peerCount); + await waitForPredicate(async () => { + const peers = await client.getPeers(); + return peers.length >= peerCount; + }, 10 * 1000); }); When("I print the world", function () {