From d6b689bada7c103fbe549209b8c34fa7e077ee91 Mon Sep 17 00:00:00 2001 From: Quentin Le Sceller Date: Fri, 1 Jun 2018 18:41:39 -0400 Subject: [PATCH] Simplify DandelionConfig (#1115) * Simplify DandelionConfig and make fields optional --- Cargo.lock | 1 + p2p/Cargo.toml | 3 + p2p/src/lib.rs | 5 +- p2p/src/serv.rs | 3 - p2p/src/types.rs | 17 ------ p2p/tests/peer_handshake.rs | 13 ++-- pool/src/types.rs | 52 ++++++++++++++-- servers/src/common/types.rs | 86 +-------------------------- servers/src/grin/dandelion_monitor.rs | 8 +-- servers/src/grin/seed.rs | 8 ++- servers/src/grin/server.rs | 4 +- 11 files changed, 73 insertions(+), 127 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37dce9244..cdabda9c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -698,6 +698,7 @@ dependencies = [ "bytes 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "enum_primitive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "grin_core 0.2.0", + "grin_pool 0.2.0", "grin_store 0.2.0", "grin_util 0.2.0", "net2 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/p2p/Cargo.toml b/p2p/Cargo.toml index 624032380..f5dc2dd38 100644 --- a/p2p/Cargo.toml +++ b/p2p/Cargo.toml @@ -20,3 +20,6 @@ time = "0.1" grin_core = { path = "../core" } grin_store = { path = "../store" } grin_util = { path = "../util" } + +[dev-dependencies] +grin_pool = { path = "../pool" } diff --git a/p2p/src/lib.rs b/p2p/src/lib.rs index e781491a2..83eb0090c 100644 --- a/p2p/src/lib.rs +++ b/p2p/src/lib.rs @@ -53,6 +53,5 @@ pub use peer::Peer; pub use peers::Peers; pub use serv::{DummyAdapter, Server}; pub use store::{PeerData, State}; -pub use types::{Capabilities, ChainAdapter, DandelionConfig, Direction, Error, P2PConfig, - PeerInfo, ReasonForBan, TxHashSetRead, MAX_BLOCK_HEADERS, MAX_LOCATORS, - MAX_PEER_ADDRS}; +pub use types::{Capabilities, ChainAdapter, Direction, Error, P2PConfig, PeerInfo, ReasonForBan, + TxHashSetRead, MAX_BLOCK_HEADERS, MAX_LOCATORS, MAX_PEER_ADDRS}; diff --git a/p2p/src/serv.rs b/p2p/src/serv.rs index fe51c53ed..a7b2dbb56 100644 --- a/p2p/src/serv.rs +++ b/p2p/src/serv.rs @@ -34,7 +34,6 @@ use util::LOGGER; /// peers, receiving connections from other peers and keep track of all of them. pub struct Server { pub config: P2PConfig, - pub dandelion_config: DandelionConfig, capabilities: Capabilities, handshake: Arc, pub peers: Arc, @@ -51,7 +50,6 @@ impl Server { db_root: String, mut capab: Capabilities, config: P2PConfig, - dandelion_config: DandelionConfig, adapter: Arc, genesis: Hash, stop: Arc, @@ -81,7 +79,6 @@ impl Server { } Ok(Server { config: config.clone(), - dandelion_config: dandelion_config.clone(), capabilities: capab, handshake: Arc::new(Handshake::new(genesis, config.clone())), peers: Arc::new(Peers::new(PeerStore::new(db_root)?, adapter, config)), diff --git a/p2p/src/types.rs b/p2p/src/types.rs index 735b97940..8119c9454 100644 --- a/p2p/src/types.rs +++ b/p2p/src/types.rs @@ -90,23 +90,6 @@ impl From> for Error { } } -/// Configuration for "Dandelion". -/// Note: shared between p2p and pool. -/// Look in top-level server config for info on configuring these parameters. -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct DandelionConfig { - /// Choose new Dandelion relay peer every n secs. - pub relay_secs: u64, - /// Dandelion embargo, fluff and broadcast tx if not seen on network before - /// embargo expires. - pub embargo_secs: u64, - /// Dandelion patience timer, fluff/stem processing runs every n secs. - /// Tx aggregation happens on stem txs received within this window. - pub patience_secs: u64, - /// Dandelion stem probability (stem 90% of the time, fluff 10% etc.) - pub stem_probability: usize, -} - /// Configuration for the peer-to-peer server. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct P2PConfig { diff --git a/p2p/tests/peer_handshake.rs b/p2p/tests/peer_handshake.rs index 75bdf1a5d..127d3ed3e 100644 --- a/p2p/tests/peer_handshake.rs +++ b/p2p/tests/peer_handshake.rs @@ -14,6 +14,7 @@ extern crate grin_core as core; extern crate grin_p2p as p2p; +extern crate grin_pool as pool; extern crate grin_util as util; use std::net::{SocketAddr, TcpListener, TcpStream}; @@ -25,6 +26,7 @@ use std::time; use core::core::hash::Hash; use core::core::target::Difficulty; use p2p::Peer; +use pool::DandelionConfig; fn open_port() -> u16 { // use port 0 to allow the OS to assign an open port @@ -47,11 +49,11 @@ fn peer_handshake() { peers_deny: None, ..p2p::P2PConfig::default() }; - let dandelion_config = p2p::DandelionConfig { - relay_secs: 600, - embargo_secs: 30, - patience_secs: 10, - stem_probability: 90, + let dandelion_config = pool::DandelionConfig { + relay_secs: Some(600), + embargo_secs: Some(30), + patience_secs: Some(10), + stem_probability: Some(90), }; let net_adapter = Arc::new(p2p::DummyAdapter {}); let server = Arc::new( @@ -59,7 +61,6 @@ fn peer_handshake() { ".grin".to_owned(), p2p::Capabilities::UNKNOWN, p2p_config.clone(), - dandelion_config.clone(), net_adapter.clone(), Hash::from_vec(&vec![]), Arc::new(AtomicBool::new(false)), diff --git a/pool/src/types.rs b/pool/src/types.rs index 15144de75..58f39f560 100644 --- a/pool/src/types.rs +++ b/pool/src/types.rs @@ -22,21 +22,63 @@ use core::consensus; use core::core::transaction; use core::core::transaction::Transaction; +/// Dandelion relay timer +const DANDELION_RELAY_SECS: u64 = 600; + +/// Dandelion emabargo timer +const DANDELION_EMBARGO_SECS: u64 = 180; + +/// Dandelion patience timer +const DANDELION_PATIENCE_SECS: u64 = 10; + +/// Dandelion stem probability (stem 90% of the time, fluff 10%). +const DANDELION_STEM_PROBABILITY: usize = 90; + /// Configuration for "Dandelion". /// Note: shared between p2p and pool. -/// Look in top-level server config for info on configuring these parameters. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct DandelionConfig { /// Choose new Dandelion relay peer every n secs. - pub relay_secs: u64, + #[serde = "default_dandelion_relay_secs"] + pub relay_secs: Option, /// Dandelion embargo, fluff and broadcast tx if not seen on network before /// embargo expires. - pub embargo_secs: u64, + #[serde = "default_dandelion_embargo_secs"] + pub embargo_secs: Option, /// Dandelion patience timer, fluff/stem processing runs every n secs. /// Tx aggregation happens on stem txs received within this window. - pub patience_secs: u64, + #[serde = "default_dandelion_patience_secs"] + pub patience_secs: Option, /// Dandelion stem probability (stem 90% of the time, fluff 10% etc.) - pub stem_probability: usize, + #[serde = "default_dandelion_stem_probability"] + pub stem_probability: Option, +} + +impl Default for DandelionConfig { + fn default() -> DandelionConfig { + DandelionConfig { + relay_secs: default_dandelion_relay_secs(), + embargo_secs: default_dandelion_embargo_secs(), + patience_secs: default_dandelion_patience_secs(), + stem_probability: default_dandelion_stem_probability(), + } + } +} + +fn default_dandelion_relay_secs() -> Option { + Some(DANDELION_RELAY_SECS) +} + +fn default_dandelion_embargo_secs() -> Option { + Some(DANDELION_EMBARGO_SECS) +} + +fn default_dandelion_patience_secs() -> Option { + Some(DANDELION_PATIENCE_SECS) +} + +fn default_dandelion_stem_probability() -> Option { + Some(DANDELION_STEM_PROBABILITY) } /// Transaction pool configuration diff --git a/servers/src/common/types.rs b/servers/src/common/types.rs index 04c732ccb..b78ff3e9e 100644 --- a/servers/src/common/types.rs +++ b/servers/src/common/types.rs @@ -26,18 +26,6 @@ use pool; use store; use wallet; -/// Dandelion relay timer -const DANDELION_RELAY_SECS: u64 = 600; - -/// Dandelion emabargo timer -const DANDELION_EMBARGO_SECS: u64 = 180; - -/// Dandelion patience timer -const DANDELION_PATIENCE_SECS: u64 = 10; - -/// Dandelion stem probability (stem 90% of the time, fluff 10%). -const DANDELION_STEM_PROBABILITY: usize = 90; - /// Error type wrapping underlying module errors. #[derive(Debug)] pub enum Error { @@ -135,54 +123,6 @@ impl Default for Seeding { } } -fn default_dandelion_stem_probability() -> usize { - DANDELION_STEM_PROBABILITY -} - -fn default_dandelion_relay_secs() -> u64 { - DANDELION_RELAY_SECS -} - -fn default_dandelion_embargo_secs() -> u64 { - DANDELION_EMBARGO_SECS -} - -fn default_dandelion_patience_secs() -> u64 { - DANDELION_PATIENCE_SECS -} - -/// Dandelion config. -/// Note: Used by both p2p and pool components. -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct DandelionConfig { - /// Choose new Dandelion relay peer every n secs. - #[serde = "default_dandelion_relay_secs"] - pub relay_secs: u64, - /// Dandelion embargo, fluff and broadcast tx if not seen on network before - /// embargo expires. - #[serde = "default_dandelion_embargo_secs"] - pub embargo_secs: u64, - /// Dandelion patience timer, fluff/stem processing runs every n secs. - /// Tx aggregation happens on stem txs received within this window. - #[serde = "default_dandelion_patience_secs"] - pub patience_secs: u64, - /// Dandelion stem probability. - #[serde = "default_dandelion_stem_probability"] - pub stem_probability: usize, -} - -/// Default address for peer-to-peer connections. -impl Default for DandelionConfig { - fn default() -> DandelionConfig { - DandelionConfig { - relay_secs: default_dandelion_relay_secs(), - embargo_secs: default_dandelion_embargo_secs(), - patience_secs: default_dandelion_patience_secs(), - stem_probability: default_dandelion_stem_probability(), - } - } -} - /// Full server configuration, aggregating configurations required for the /// different components. #[derive(Debug, Clone, Serialize, Deserialize)] @@ -229,7 +169,7 @@ pub struct ServerConfig { /// Dandelion configuration #[serde(default)] - pub dandelion_config: DandelionConfig, + pub dandelion_config: pool::DandelionConfig, /// Whether to skip the sync timeout on startup /// (To assist testing on solo chains) @@ -249,28 +189,6 @@ pub struct ServerConfig { pub test_miner_wallet_url: Option, } -impl ServerConfig { - /// Adapter for configuring Dandelion on the pool component. - pub fn pool_dandelion_config(&self) -> pool::DandelionConfig { - pool::DandelionConfig { - relay_secs: self.dandelion_config.relay_secs, - embargo_secs: self.dandelion_config.embargo_secs, - patience_secs: self.dandelion_config.patience_secs, - stem_probability: self.dandelion_config.stem_probability, - } - } - - /// Adapter for configuring Dandelion on the p2p component. - pub fn p2p_dandelion_config(&self) -> p2p::DandelionConfig { - p2p::DandelionConfig { - relay_secs: self.dandelion_config.relay_secs, - embargo_secs: self.dandelion_config.embargo_secs, - patience_secs: self.dandelion_config.patience_secs, - stem_probability: self.dandelion_config.stem_probability, - } - } -} - impl Default for ServerConfig { fn default() -> ServerConfig { ServerConfig { @@ -280,7 +198,7 @@ impl Default for ServerConfig { seeding_type: Seeding::default(), seeds: None, p2p_config: p2p::P2PConfig::default(), - dandelion_config: DandelionConfig::default(), + dandelion_config: pool::DandelionConfig::default(), stratum_mining_config: Some(StratumServerConfig::default()), chain_type: ChainTypes::default(), archive_mode: None, diff --git a/servers/src/grin/dandelion_monitor.rs b/servers/src/grin/dandelion_monitor.rs index d8e686361..4e5259cf7 100644 --- a/servers/src/grin/dandelion_monitor.rs +++ b/servers/src/grin/dandelion_monitor.rs @@ -22,7 +22,7 @@ use time::now_utc; use core::core::hash::Hashed; use core::core::transaction; -use p2p::DandelionConfig; +use pool::DandelionConfig; use pool::{BlockChain, PoolEntryState, PoolError, TransactionPool, TxSource}; use util::LOGGER; @@ -52,7 +52,7 @@ pub fn monitor_transactions( } // This is the patience timer, we loop every n secs. - let patience_secs = dandelion_config.patience_secs; + let patience_secs = dandelion_config.patience_secs.unwrap(); thread::sleep(Duration::from_secs(patience_secs)); let tx_pool = tx_pool.clone(); @@ -185,7 +185,7 @@ where for x in &mut fresh_entries.iter_mut() { let random = rng.gen_range(0, 101); - if random <= dandelion_config.stem_probability { + if random <= dandelion_config.stem_probability.unwrap() { x.state = PoolEntryState::ToStem; } else { x.state = PoolEntryState::ToFluff; @@ -203,7 +203,7 @@ where T: BlockChain + Send + Sync + 'static, { let now = now_utc().to_timespec().sec; - let embargo_sec = dandelion_config.embargo_secs + rand::thread_rng().gen_range(0, 31); + let embargo_sec = dandelion_config.embargo_secs.unwrap() + rand::thread_rng().gen_range(0, 31); let cutoff = now - embargo_sec as i64; let mut expired_entries = vec![]; diff --git a/servers/src/grin/seed.rs b/servers/src/grin/seed.rs index 2e0a8d269..d30af8389 100644 --- a/servers/src/grin/seed.rs +++ b/servers/src/grin/seed.rs @@ -29,6 +29,7 @@ use time::{self, now_utc}; use hyper; use p2p; +use pool::DandelionConfig; use util::LOGGER; const SEEDS_URL: &'static str = "http://grin-tech.org/seeds.txt"; @@ -42,6 +43,7 @@ const DNS_SEEDS: &'static [&'static str] = &[ pub fn connect_and_monitor( p2p_server: Arc, capabilities: p2p::Capabilities, + dandelion_config: DandelionConfig, seed_list: Box Vec + Send>, stop: Arc, ) { @@ -73,7 +75,7 @@ pub fn connect_and_monitor( tx.clone(), ); - update_dandelion_relay(peers.clone(), p2p_server.dandelion_config.clone()); + update_dandelion_relay(peers.clone(), dandelion_config.clone()); prev = current_time; } @@ -159,7 +161,7 @@ fn monitor_peers( } } -fn update_dandelion_relay(peers: Arc, dandelion_config: p2p::DandelionConfig) { +fn update_dandelion_relay(peers: Arc, dandelion_config: DandelionConfig) { // Dandelion Relay Updater let dandelion_relay = peers.get_dandelion_relay(); if dandelion_relay.is_empty() { @@ -168,7 +170,7 @@ fn update_dandelion_relay(peers: Arc, dandelion_config: p2p::Dandeli } else { for last_added in dandelion_relay.keys() { let dandelion_interval = now_utc().to_timespec().sec - last_added; - if dandelion_interval >= dandelion_config.relay_secs as i64 { + if dandelion_interval >= dandelion_config.relay_secs.unwrap() as i64 { debug!(LOGGER, "monitor_peers: updating expired dandelion relay"); peers.update_dandelion_relay(); } diff --git a/servers/src/grin/server.rs b/servers/src/grin/server.rs index 3c605ddb5..1801cd91f 100644 --- a/servers/src/grin/server.rs +++ b/servers/src/grin/server.rs @@ -161,7 +161,6 @@ impl Server { config.db_root.clone(), config.capabilities, config.p2p_config.clone(), - config.p2p_dandelion_config(), net_adapter.clone(), genesis.hash(), stop.clone(), @@ -189,6 +188,7 @@ impl Server { seed::connect_and_monitor( p2p_server.clone(), config.capabilities, + config.dandelion_config.clone(), seeder, stop.clone(), ); @@ -232,7 +232,7 @@ impl Server { "Starting dandelion monitor: {}", &config.api_http_addr ); dandelion_monitor::monitor_transactions( - config.p2p_dandelion_config(), + config.dandelion_config.clone(), tx_pool.clone(), stop.clone(), );