From 68e2fa39155e98f573ec73c1d277cdc9998c58eb Mon Sep 17 00:00:00 2001 From: Quentin Le Sceller Date: Wed, 4 Apr 2018 13:14:58 -0400 Subject: [PATCH] Randomize Dandelion timer (#928) --- grin/src/dandelion_monitor.rs | 1 - pool/src/pool.rs | 31 ++++++++++++++++++------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/grin/src/dandelion_monitor.rs b/grin/src/dandelion_monitor.rs index 9fb5d72b0..3fc578b6b 100644 --- a/grin/src/dandelion_monitor.rs +++ b/grin/src/dandelion_monitor.rs @@ -51,7 +51,6 @@ pub fn monitor_transactions( let time_transaction = time_stem_transactions.get(tx_hash).unwrap(); let interval = now_utc().to_timespec().sec - time_transaction; - // TODO Randomize between 30 and 60 seconds if interval >= config.dandelion_embargo { let source = TxSource { debug_name: "dandelion-monitor".to_string(), diff --git a/pool/src/pool.rs b/pool/src/pool.rs index 03bb82b3d..f5d10a305 100644 --- a/pool/src/pool.rs +++ b/pool/src/pool.rs @@ -252,6 +252,11 @@ where let stem_propagation = random <= self.config.dandelion_probability; let mut will_stem = stem && stem_propagation; + // Track the case where a parent of a transaction is in stempool + let mut parent_in_stempool = false; + // The timer attached to this transaction + let mut timer: i64 = 0; + // The next issue is to identify all unspent outputs that // this transaction will consume and make sure they exist in the set. let mut pool_refs: Vec = Vec::new(); @@ -269,17 +274,13 @@ where match self.search_for_best_output(&output) { Parent::PoolTransaction { tx_ref: x } => pool_refs.push(base.with_source(Some(x))), Parent::StemPoolTransaction { tx_ref: x } => { - if will_stem { - // Going to stem this transaction if parent is in stempool it's ok. - debug!(LOGGER, "Going in stempool"); - pool_refs.push(base.with_source(Some(x))); - } else { - will_stem = true; - debug!( - LOGGER, - "Parent is in stempool, force transaction to go in stempool" - ); - pool_refs.push(base.with_source(Some(x))); + will_stem = true; + parent_in_stempool = true; + debug!(LOGGER, "Parent is in stempool, going in stempool"); + pool_refs.push(base.with_source(Some(x))); + let temp_timer = self.time_stem_transactions.get(&x).unwrap().clone(); + if temp_timer > timer { + timer = temp_timer; } } Parent::BlockTransaction => { @@ -299,6 +300,11 @@ where let is_orphan = orphan_refs.len() > 0; + // In the case the parent is not in stempool we randomize the timer + if !parent_in_stempool { + timer = time::now_utc().to_timespec().sec + rand::thread_rng().gen_range(0, 31); + } + // Next we examine the outputs this transaction creates and ensure // that they do not already exist. // I believe its worth preventing duplicate outputs from being @@ -344,8 +350,7 @@ where self.adapter.stem_tx_accepted(&tx); self.stem_transactions.insert(tx_hash, Box::new(tx)); // Track this transaction - self.time_stem_transactions - .insert(tx_hash, time::now_utc().to_timespec().sec); + self.time_stem_transactions.insert(tx_hash, timer); } else { // Fluff phase: transaction is added to memory pool and broadcasted normally self.pool.add_pool_transaction(