Skip to content

Commit

Permalink
fix: do not use bootstrap list with kademlia (#199)
Browse files Browse the repository at this point in the history
* fix: do not use bootstrap list with kademlia

Prior to the peer_manager connecting to bootstrap peers we relied on
kademlia to connect to them. Now not only is it not necessary but the
peer_manager applies an appropriate backoff for redials where kademlia
does not. There should be no change in external behavior except that we
no longer spam dials to disconnected bootstrap peers.

Now the peer_manager (renamed to ceramic_peer_manager) can report of a
peer id is a ceramic peer. If we connect to a ceramic peer we add it to
the kademlia routing table regardless of its reported protocols. This is
because we discovered that peers sometimes do not report they support
the kademlia protocol.

* fix: typo
  • Loading branch information
nathanielc authored Nov 22, 2023
1 parent 1e9cba6 commit ad65ccc
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 209 deletions.
13 changes: 6 additions & 7 deletions one/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ struct DaemonOpts {
)]
swarm_addresses: Vec<String>,

/// Extra bootstrap peer addresses to be used in addition to the official bootstrap addresses.
/// Extra addresses of peers that participate in the Ceramic network.
/// A best-effort attempt will be made to maintain a connection to these addresses.
#[arg(
long,
use_value_delimiter = true,
value_delimiter = ',',
env = "CERAMIC_ONE_EXTRA_BOOTSTRAP_ADDRESSES"
env = "CERAMIC_ONE_EXTRA_CERAMIC_PEER_ADDRESSES"
)]
extra_bootstrap_addresses: Vec<String>,
extra_ceramic_peer_addresses: Vec<String>,

/// Path to storage directory
#[arg(short, long, env = "CERAMIC_ONE_STORE_DIR")]
Expand Down Expand Up @@ -373,14 +373,13 @@ impl Daemon {
max_conns_pending_in: opts.max_conns_pending_in,
max_conns_per_peer: opts.max_conns_per_peer,
idle_connection_timeout: Duration::from_millis(opts.idle_conns_timeout_ms),
// Add extra bootstrap addresses to the list of official bootstrap addresses, so that our bootstrap nodes
// are always included.
bootstrap_peers: opts
// Add extra ceramic peer addresses to the list of official ceramic peer addresses.
ceramic_peers: opts
.network
.bootstrap_addresses()
.into_iter()
.chain(
opts.extra_bootstrap_addresses
opts.extra_ceramic_peer_addresses
.iter()
.map(|addr| addr.parse())
.collect::<Result<Vec<Multiaddr>, multiaddr::Error>>()?,
Expand Down
25 changes: 9 additions & 16 deletions p2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use libp2p::{
store::{MemoryStore, MemoryStoreConfig},
},
mdns::tokio::Behaviour as Mdns,
multiaddr::Protocol,
ping::Behaviour as Ping,
relay,
swarm::behaviour::toggle::Toggle,
Expand All @@ -27,14 +26,14 @@ use libp2p_identity::Keypair;
use recon::{libp2p::Recon, Sha256a};
use tracing::{info, warn};

use self::ceramic_peer_manager::CeramicPeerManager;
pub use self::event::Event;
use self::peer_manager::PeerManager;
use crate::config::Libp2pConfig;
use crate::sqliteblockstore::SQLiteBlockStore;
use crate::Metrics;

mod ceramic_peer_manager;
mod event;
mod peer_manager;

pub const PROTOCOL_VERSION: &str = "ipfs/0.1.0";
pub const AGENT_VERSION: &str = concat!("ceramic-one/", env!("CARGO_PKG_VERSION"));
Expand All @@ -49,6 +48,7 @@ pub(crate) struct NodeBehaviour<I, M> {
// end up being denied because of the limits.
// See https://github.com/libp2p/rust-libp2p/pull/4777#discussion_r1391833734 for more context.
limits: connection_limits::Behaviour,
pub(crate) peer_manager: CeramicPeerManager,
ping: Ping,
identify: identify::Behaviour,
pub(crate) bitswap: Toggle<Bitswap<SQLiteBlockStore>>,
Expand All @@ -59,7 +59,6 @@ pub(crate) struct NodeBehaviour<I, M> {
relay_client: Toggle<relay::client::Behaviour>,
dcutr: Toggle<dcutr::Behaviour>,
pub(crate) gossipsub: Toggle<gossipsub::Behaviour>,
pub(crate) peer_manager: PeerManager,
recon: Toggle<recon::libp2p::Behaviour<I, M>>,
}

Expand Down Expand Up @@ -158,17 +157,11 @@ where
// Provider records are re-published via the [`crate::publisher::Publisher`].
.set_provider_publication_interval(None);

let mut kademlia = kad::Behaviour::with_config(pub_key.to_peer_id(), store, kad_config);
for multiaddr in &config.bootstrap_peers {
// TODO: move parsing into config
let mut addr = multiaddr.to_owned();
if let Some(Protocol::P2p(peer_id)) = addr.pop() {
kademlia.add_address(&peer_id, addr);
} else {
warn!("Could not parse bootstrap addr {}", multiaddr);
}
}
Some(kademlia)
Some(kad::Behaviour::with_config(
pub_key.to_peer_id(),
store,
kad_config,
))
} else {
None
}
Expand Down Expand Up @@ -253,7 +246,7 @@ where
dcutr: dcutr.into(),
relay_client: relay_client.into(),
gossipsub,
peer_manager: PeerManager::new(&config.bootstrap_peers, metrics)?,
peer_manager: CeramicPeerManager::new(&config.ceramic_peers, metrics)?,
limits,
recon: recon.into(),
})
Expand Down
Loading

0 comments on commit ad65ccc

Please sign in to comment.