Skip to content

Commit

Permalink
refactor!: introduce enum for peer db config
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Nov 20, 2024
1 parent 01dfa57 commit 7ff45fe
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 19 deletions.
4 changes: 3 additions & 1 deletion example/testnet4.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use kyoto::core::messages::NodeMessage;
use kyoto::{chain::checkpoints::HeaderCheckpoint, core::builder::NodeBuilder};
use kyoto::{Address, Network, TrustedPeer};
use kyoto::{Address, Network, PeerStoreSizeConfig, TrustedPeer};
use std::collections::HashSet;
use std::{net::Ipv4Addr, str::FromStr};

Expand Down Expand Up @@ -34,6 +34,8 @@ async fn main() {
.add_scripts(addresses)
// Only scan blocks strictly after an anchor checkpoint
.anchor_checkpoint(checkpoint)
// Store a limited number of peers
.peer_db_size(PeerStoreSizeConfig::Limit(256))
// The number of connections we would like to maintain
.num_required_peers(1)
// Create the node and client
Expand Down
6 changes: 4 additions & 2 deletions example/tor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use kyoto::core::messages::NodeMessage;
use kyoto::db::memory::peers::StatelessPeerStore;
use kyoto::db::sqlite::headers::SqliteHeaderDb;
use kyoto::{chain::checkpoints::HeaderCheckpoint, core::builder::NodeBuilder};
use kyoto::{BlockHash, ConnectionType, TorClient, TorClientConfig, TrustedPeer};
use kyoto::{
BlockHash, ConnectionType, PeerStoreSizeConfig, TorClient, TorClientConfig, TrustedPeer,
};
use std::collections::HashSet;
use std::str::FromStr;

Expand Down Expand Up @@ -54,7 +56,7 @@ async fn main() {
// The number of connections we would like to maintain
.num_required_peers(2)
// We only maintain a list of 32 peers in memory
.peer_db_size(256)
.peer_db_size(PeerStoreSizeConfig::Limit(32))
// Connect to peers over Tor
.set_connection_type(ConnectionType::Tor(tor))
// Build without the default databases
Expand Down
8 changes: 4 additions & 4 deletions src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
chain::checkpoints::HeaderCheckpoint,
db::traits::{HeaderStore, PeerStore},
};
use crate::{ConnectionType, TrustedPeer};
use crate::{ConnectionType, PeerStoreSizeConfig, TrustedPeer};

#[cfg(feature = "database")]
/// The default node returned from the [`NodeBuilder`](crate::core).
Expand Down Expand Up @@ -116,9 +116,9 @@ impl NodeBuilder {
/// Set the desired number of peers for the database to keep track of. For limited or in-memory peer storage,
/// this number may be small, however a sufficient margin of peers should be set so the node can try many options
/// when downloading compact block filters. For nodes that store peers on disk, more peers will typically result in
/// fewer errors. If none is provided, a limit of 4096 will be used.
pub fn peer_db_size(mut self, target_num: u32) -> Self {
self.config.target_peer_size = target_num;
/// fewer errors. If none is provided, no limit to the size of the store will be introduced.
pub fn peer_db_size(mut self, target: PeerStoreSizeConfig) -> Self {
self.config.target_peer_size = target;
self
}

Expand Down
9 changes: 5 additions & 4 deletions src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use std::{collections::HashSet, path::PathBuf, time::Duration};

use bitcoin::ScriptBuf;

use crate::{chain::checkpoints::HeaderCheckpoint, ConnectionType, TrustedPeer};
use crate::{
chain::checkpoints::HeaderCheckpoint, ConnectionType, PeerStoreSizeConfig, TrustedPeer,
};

use super::FilterSyncPolicy;

const TARGET_PEER_DB_SIZE: u32 = 4096;
const REQUIRED_PEERS: u8 = 1;
const TIMEOUT_SECS: u64 = 5;
// sec min hour
Expand All @@ -19,7 +20,7 @@ pub(crate) struct NodeConfig {
pub data_path: Option<PathBuf>,
pub header_checkpoint: Option<HeaderCheckpoint>,
pub connection_type: ConnectionType,
pub target_peer_size: u32,
pub target_peer_size: PeerStoreSizeConfig,
pub response_timeout: Duration,
pub max_connection_time: Duration,
pub filter_sync_policy: FilterSyncPolicy,
Expand All @@ -34,7 +35,7 @@ impl Default for NodeConfig {
data_path: Default::default(),
header_checkpoint: Default::default(),
connection_type: Default::default(),
target_peer_size: TARGET_PEER_DB_SIZE,
target_peer_size: PeerStoreSizeConfig::default(),
response_timeout: Duration::from_secs(TIMEOUT_SECS),
max_connection_time: Duration::from_secs(TWO_HOUR),
filter_sync_policy: Default::default(),
Expand Down
4 changes: 2 additions & 2 deletions src/core/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
core::{error::FetchHeaderError, peer_map::PeerMap},
db::traits::{HeaderStore, PeerStore},
filters::{cfheader_chain::AppendAttempt, error::CFilterSyncError},
ConnectionType, FailurePayload, TrustedPeer, TxBroadcastPolicy,
ConnectionType, FailurePayload, PeerStoreSizeConfig, TrustedPeer, TxBroadcastPolicy,
};

use super::{
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
scripts: HashSet<ScriptBuf>,
header_checkpoint: Option<HeaderCheckpoint>,
required_peers: PeerRequirement,
target_peer_size: u32,
target_peer_size: PeerStoreSizeConfig,
connection_type: ConnectionType,
timeout_config: PeerTimeoutConfig,
filter_sync_policy: FilterSyncPolicy,
Expand Down
17 changes: 11 additions & 6 deletions src/core/peer_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
traits::{ClearNetConnection, NetworkConnector},
},
prelude::{default_port_from_network, Median, Netgroup},
ConnectionType, TrustedPeer,
ConnectionType, PeerStoreSizeConfig, TrustedPeer,
};

use super::{
Expand Down Expand Up @@ -66,7 +66,7 @@ pub(crate) struct PeerMap<P: PeerStore> {
connector: Arc<Mutex<dyn NetworkConnector + Send + Sync>>,
whitelist: Whitelist,
dialog: Dialog,
target_db_size: u32,
target_db_size: PeerStoreSizeConfig,
net_groups: HashSet<String>,
timeout_config: PeerTimeoutConfig,
}
Expand All @@ -81,7 +81,7 @@ impl<P: PeerStore> PeerMap<P> {
whitelist: Whitelist,
dialog: Dialog,
connection_type: ConnectionType,
target_db_size: u32,
target_db_size: PeerStoreSizeConfig,
timeout_config: PeerTimeoutConfig,
) -> Self {
let connector: Arc<Mutex<dyn NetworkConnector + Send + Sync>> = match connection_type {
Expand Down Expand Up @@ -296,9 +296,14 @@ impl<P: PeerStore> PeerMap<P> {

// Do we need peers
pub async fn need_peers(&mut self) -> Result<bool, PeerManagerError<P::Error>> {
let mut db = self.db.lock().await;
let num_unbanned = db.num_unbanned().await?;
Ok(num_unbanned < self.target_db_size)
match self.target_db_size {
PeerStoreSizeConfig::Unbounded => Ok(true),
PeerStoreSizeConfig::Limit(limit) => {
let mut db = self.db.lock().await;
let num_unbanned = db.num_unbanned().await?;
Ok(num_unbanned < limit)
}
}
}

// Add peers to the database that were gossiped over the p2p network
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,15 @@ pub enum ConnectionType {
#[cfg(feature = "tor")]
Tor(TorClient<PreferredRuntime>),
}

/// Configure how many peers will be stored.
#[derive(Debug, Default, Clone)]
pub enum PeerStoreSizeConfig {
/// Add new peers to the store regardless of the current size. For memory-limited [`PeerStore`]
/// implementations, consider using a bounded size.
#[default]
Unbounded,
/// Bound the size of the [`PeerStore`]. When set, no new peers will be requested if the database
/// has at least this amount of peers.
Limit(u32),
}

0 comments on commit 7ff45fe

Please sign in to comment.