Simplify DandelionConfig (#1115)

* Simplify DandelionConfig and make fields optional
This commit is contained in:
Quentin Le Sceller 2018-06-01 18:41:39 -04:00 committed by GitHub
parent 68c7cff6db
commit d6b689bada
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 73 additions and 127 deletions

1
Cargo.lock generated
View file

@ -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)",

View file

@ -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" }

View file

@ -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};

View file

@ -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<Handshake>,
pub peers: Arc<Peers>,
@ -51,7 +50,6 @@ impl Server {
db_root: String,
mut capab: Capabilities,
config: P2PConfig,
dandelion_config: DandelionConfig,
adapter: Arc<ChainAdapter>,
genesis: Hash,
stop: Arc<AtomicBool>,
@ -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)),

View file

@ -90,23 +90,6 @@ impl<T> From<mpsc::TrySendError<T>> 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 {

View file

@ -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)),

View file

@ -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<u64>,
/// 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<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,
#[serde = "default_dandelion_patience_secs"]
pub patience_secs: Option<u64>,
/// Dandelion stem probability (stem 90% of the time, fluff 10% etc.)
pub stem_probability: usize,
#[serde = "default_dandelion_stem_probability"]
pub stem_probability: Option<usize>,
}
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<u64> {
Some(DANDELION_RELAY_SECS)
}
fn default_dandelion_embargo_secs() -> Option<u64> {
Some(DANDELION_EMBARGO_SECS)
}
fn default_dandelion_patience_secs() -> Option<u64> {
Some(DANDELION_PATIENCE_SECS)
}
fn default_dandelion_stem_probability() -> Option<usize> {
Some(DANDELION_STEM_PROBABILITY)
}
/// Transaction pool configuration

View file

@ -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<String>,
}
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,

View file

@ -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<T>(
}
// 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![];

View file

@ -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<p2p::Server>,
capabilities: p2p::Capabilities,
dandelion_config: DandelionConfig,
seed_list: Box<Fn() -> Vec<SocketAddr> + Send>,
stop: Arc<AtomicBool>,
) {
@ -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<p2p::Peers>, dandelion_config: p2p::DandelionConfig) {
fn update_dandelion_relay(peers: Arc<p2p::Peers>, 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<p2p::Peers>, 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();
}

View file

@ -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(),
);