Skip to content
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

Minor Libp2p changes #3477

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions crates/hotshot/src/traits/networking/libp2p_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#[cfg(feature = "hotshot-testing")]
use std::str::FromStr;
use std::{
cmp::min,
collections::{BTreeSet, HashSet},
fmt::Debug,
net::SocketAddr,
Expand All @@ -15,7 +16,7 @@ use std::{
time::Duration,
};

use anyhow::anyhow;
use anyhow::{anyhow, Context};
use async_compatibility_layer::{
art::{async_sleep, async_spawn},
channel::{
Expand Down Expand Up @@ -60,7 +61,7 @@ use libp2p_networking::{
spawn_network_node, MeshParams,
NetworkEvent::{self, DirectRequest, DirectResponse, GossipMsg},
NetworkNodeConfig, NetworkNodeConfigBuilder, NetworkNodeHandle, NetworkNodeHandleError,
NetworkNodeReceiver, NetworkNodeType,
NetworkNodeReceiver, NetworkNodeType, DEFAULT_REPLICATION_FACTOR,
},
reexport::{Multiaddr, ResponseChannel},
};
Expand Down Expand Up @@ -397,11 +398,23 @@ impl<K: SignatureKey + 'static> Libp2pNetwork<K> {
.parse()?;

// Build our libp2p configuration from our global, network configuration
let mut config_builder = NetworkNodeConfigBuilder::default();
let mut config_builder: NetworkNodeConfigBuilder = NetworkNodeConfigBuilder::default();

// The replication factor is the minimum of [the default and 2/3 the number of nodes]
let Some(default_replication_factor) = DEFAULT_REPLICATION_FACTOR else {
return Err(anyhow!("Default replication factor not supplied"));
};

let replication_factor = NonZeroUsize::new(min(
default_replication_factor.get(),
config.config.num_nodes_with_stake.get() * 2 / 3,
))
.with_context(|| "Failed to calculate replication factor")?;

config_builder
.server_mode(libp2p_config.server_mode)
.identity(keypair)
.replication_factor(replication_factor)
.bound_addr(Some(bind_address.clone()))
.mesh_params(Some(MeshParams {
mesh_n_high: libp2p_config.mesh_n_high,
Expand Down
2 changes: 1 addition & 1 deletion crates/libp2p-networking/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub use self::{
node::{
network_node_handle_error, spawn_network_node, MeshParams, NetworkNode, NetworkNodeConfig,
NetworkNodeConfigBuilder, NetworkNodeConfigBuilderError, NetworkNodeHandle,
NetworkNodeHandleError, NetworkNodeReceiver,
NetworkNodeHandleError, NetworkNodeReceiver, DEFAULT_REPLICATION_FACTOR,
},
};
#[cfg(not(any(async_executor_impl = "async-std", async_executor_impl = "tokio")))]
Expand Down
9 changes: 8 additions & 1 deletion crates/libp2p-networking/src/network/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use tracing::{debug, error, info, info_span, instrument, warn, Instrument};
pub use self::{
config::{
MeshParams, NetworkNodeConfig, NetworkNodeConfigBuilder, NetworkNodeConfigBuilderError,
DEFAULT_REPLICATION_FACTOR,
},
handle::{
network_node_handle_error, spawn_network_node, NetworkNodeHandle, NetworkNodeHandleError,
Expand Down Expand Up @@ -351,11 +352,17 @@ impl NetworkNode {
/// Once replicated upon all nodes, the caller is notified over
/// `chan`. If there is an error, a [`super::error::DHTError`] is
/// sent instead.
///
/// # Panics
/// If the default replication factor is `None`
pub fn put_record(&mut self, mut query: KadPutQuery) {
let record = Record::new(query.key.clone(), query.value.clone());
match self.swarm.behaviour_mut().dht.put_record(
record,
libp2p::kad::Quorum::N(self.dht_handler.replication_factor()),
libp2p::kad::Quorum::N(
NonZeroUsize::try_from(self.dht_handler.replication_factor().get() / 2)
.expect("replication factor should be bigger than 0"),
),
) {
Err(e) => {
// failed try again later
Expand Down