-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tests for new peer discovery when the tip is stale #1292
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
142ef81
Peer discovery when the tip is stale
ImplOfAnImpl 7be708f
Fix broken older tests
ImplOfAnImpl e1dccd5
Remove unused code
ImplOfAnImpl e901a77
Appease clippy
ImplOfAnImpl 159376b
Cleanup after self-review
ImplOfAnImpl 308f5a6
Reduce the number of nodes in peer_discovery_on_stale_tip tests
ImplOfAnImpl b0d09b0
Revert changes in ChainstateEvent::NewTip; replace PeerManagerEvent::…
ImplOfAnImpl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) 2021-2023 RBB S.r.l | ||
// opensource@mintlayer.org | ||
// SPDX-License-Identifier: MIT | ||
// Licensed under the MIT License; | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use common::chain::{config::ChainType, ChainConfig}; | ||
use crypto::random::{make_pseudo_rng, seq::IteratorRandom}; | ||
use logging::log; | ||
use p2p_types::socket_address::SocketAddress; | ||
|
||
use crate::config::P2pConfig; | ||
|
||
#[async_trait] | ||
pub trait DnsSeed: Send + Sync { | ||
async fn obtain_addresses(&self) -> Vec<SocketAddress>; | ||
} | ||
|
||
pub struct DefaultDnsSeed { | ||
chain_config: Arc<ChainConfig>, | ||
p2p_config: Arc<P2pConfig>, | ||
} | ||
|
||
impl DefaultDnsSeed { | ||
pub fn new(chain_config: Arc<ChainConfig>, p2p_config: Arc<P2pConfig>) -> Self { | ||
Self { | ||
chain_config, | ||
p2p_config, | ||
} | ||
} | ||
} | ||
|
||
/// Hardcoded seed DNS hostnames | ||
// TODO: Replace with actual values | ||
const DNS_SEEDS_MAINNET: [&str; 0] = []; | ||
const DNS_SEEDS_TESTNET: [&str; 1] = ["testnet-seed.mintlayer.org"]; | ||
|
||
/// Maximum number of records accepted in a single DNS server response | ||
const MAX_DNS_RECORDS: usize = 10; | ||
|
||
#[async_trait] | ||
impl DnsSeed for DefaultDnsSeed { | ||
async fn obtain_addresses(&self) -> Vec<SocketAddress> { | ||
let dns_seed = match self.chain_config.chain_type() { | ||
ChainType::Mainnet => DNS_SEEDS_MAINNET.as_slice(), | ||
ChainType::Testnet => DNS_SEEDS_TESTNET.as_slice(), | ||
ChainType::Regtest | ChainType::Signet => &[], | ||
}; | ||
|
||
if dns_seed.is_empty() { | ||
return Vec::new(); | ||
} | ||
|
||
log::debug!("Resolve DNS seed..."); | ||
let results = futures::future::join_all( | ||
dns_seed | ||
.iter() | ||
.map(|host| tokio::net::lookup_host((*host, self.chain_config.p2p_port()))), | ||
) | ||
.await; | ||
|
||
let mut addresses = Vec::new(); | ||
for result in results { | ||
match result { | ||
Ok(list) => { | ||
list.filter_map(|addr| { | ||
SocketAddress::from_peer_address( | ||
// Convert SocketAddr to PeerAddress | ||
&addr.into(), | ||
*self.p2p_config.allow_discover_private_ips, | ||
) | ||
}) | ||
// Randomize selection because records can be sorted by type (A and AAAA) | ||
.choose_multiple(&mut make_pseudo_rng(), MAX_DNS_RECORDS) | ||
.into_iter() | ||
.for_each(|addr| { | ||
addresses.push(addr); | ||
}); | ||
} | ||
Err(err) => { | ||
log::error!("resolve DNS seed failed: {err}"); | ||
} | ||
} | ||
} | ||
log::debug!("DNS seed records found: {}", addresses.len()); | ||
addresses | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: the logic in this function is not new; it was moved here from
reload_dns_seed