From f6641eaaeec33d145c1781a2b47173b09422834b Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 23 May 2023 05:21:53 +1000 Subject: [PATCH] cleanup(gossip): Use a separate named constant for the gossip interval (#6704) * Use a named consttant for the gossip interval * Update tests --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- zebrad/src/components/inbound/tests/fake_peer_set.rs | 10 +++++----- zebrad/src/components/mempool/gossip.rs | 7 +++++-- zebrad/src/components/sync.rs | 10 ++++++++++ zebrad/src/components/sync/gossip.rs | 9 +++++---- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/zebrad/src/components/inbound/tests/fake_peer_set.rs b/zebrad/src/components/inbound/tests/fake_peer_set.rs index 55816fd4e..be7154198 100644 --- a/zebrad/src/components/inbound/tests/fake_peer_set.rs +++ b/zebrad/src/components/inbound/tests/fake_peer_set.rs @@ -35,7 +35,7 @@ use crate::{ gossip_mempool_transaction_id, unmined_transactions_in_blocks, Config as MempoolConfig, Mempool, MempoolError, SameEffectsChainRejectionError, UnboxMempoolError, }, - sync::{self, BlockGossipError, SyncStatus, TIPS_RESPONSE_TIMEOUT}, + sync::{self, BlockGossipError, SyncStatus, PEER_GOSSIP_DELAY}, }, BoxError, }; @@ -421,7 +421,7 @@ async fn mempool_transaction_expiration() -> Result<(), crate::BoxError> { hs.insert(tx1_id); // Transaction and Block IDs are gossipped, in any order, after waiting for the gossip delay - tokio::time::sleep(TIPS_RESPONSE_TIMEOUT).await; + tokio::time::sleep(PEER_GOSSIP_DELAY).await; let possible_requests = &mut [ Request::AdvertiseTransactionIds(hs), Request::AdvertiseBlock(block_two.hash()), @@ -490,7 +490,7 @@ async fn mempool_transaction_expiration() -> Result<(), crate::BoxError> { .unwrap(); // Test the block is gossiped, after waiting for the multi-gossip delay - tokio::time::sleep(TIPS_RESPONSE_TIMEOUT).await; + tokio::time::sleep(PEER_GOSSIP_DELAY).await; peer_set .expect_request(Request::AdvertiseBlock(block_three.hash())) .await @@ -567,7 +567,7 @@ async fn mempool_transaction_expiration() -> Result<(), crate::BoxError> { ); // Test transaction 2 is gossiped, after waiting for the multi-gossip delay - tokio::time::sleep(TIPS_RESPONSE_TIMEOUT).await; + tokio::time::sleep(PEER_GOSSIP_DELAY).await; let mut hs = HashSet::new(); hs.insert(tx2_id); @@ -598,7 +598,7 @@ async fn mempool_transaction_expiration() -> Result<(), crate::BoxError> { .unwrap(); // Test the block is gossiped, after waiting for the multi-gossip delay - tokio::time::sleep(TIPS_RESPONSE_TIMEOUT).await; + tokio::time::sleep(PEER_GOSSIP_DELAY).await; peer_set .expect_request(Request::AdvertiseBlock(block.hash())) .await diff --git a/zebrad/src/components/mempool/gossip.rs b/zebrad/src/components/mempool/gossip.rs index eefa2d53a..6d3b2b638 100644 --- a/zebrad/src/components/mempool/gossip.rs +++ b/zebrad/src/components/mempool/gossip.rs @@ -16,7 +16,10 @@ use zebra_network::MAX_TX_INV_IN_SENT_MESSAGE; use zebra_network as zn; -use crate::{components::sync::TIPS_RESPONSE_TIMEOUT, BoxError}; +use crate::{ + components::sync::{PEER_GOSSIP_DELAY, TIPS_RESPONSE_TIMEOUT}, + BoxError, +}; /// The maximum number of channel messages we will combine into a single peer broadcast. pub const MAX_CHANGES_BEFORE_SEND: usize = 10; @@ -96,6 +99,6 @@ where // // in practice, transactions arrive every 1-20 seconds, // so waiting 6 seconds can delay transaction propagation, in order to reduce peer load - tokio::time::sleep(TIPS_RESPONSE_TIMEOUT).await; + tokio::time::sleep(PEER_GOSSIP_DELAY).await; } } diff --git a/zebrad/src/components/sync.rs b/zebrad/src/components/sync.rs index 8679f66d6..2e984b3af 100644 --- a/zebrad/src/components/sync.rs +++ b/zebrad/src/components/sync.rs @@ -107,6 +107,16 @@ pub const MAX_TIPS_RESPONSE_HASH_COUNT: usize = 500; /// failure loop. pub const TIPS_RESPONSE_TIMEOUT: Duration = Duration::from_secs(6); +/// Controls how long we wait between gossiping successive blocks or transactions. +/// +/// ## Correctness +/// +/// If this timeout is set too high, blocks and transactions won't propagate through +/// the network efficiently. +/// +/// If this timeout is set too low, the peer set and remote peers can get overloaded. +pub const PEER_GOSSIP_DELAY: Duration = Duration::from_secs(7); + /// Controls how long we wait for a block download request to complete. /// /// This timeout makes sure that the syncer doesn't hang when: diff --git a/zebrad/src/components/sync/gossip.rs b/zebrad/src/components/sync/gossip.rs index a6fcb3b49..9cb02c652 100644 --- a/zebrad/src/components/sync/gossip.rs +++ b/zebrad/src/components/sync/gossip.rs @@ -9,9 +9,10 @@ use tower::{timeout::Timeout, Service, ServiceExt}; use zebra_network as zn; use zebra_state::ChainTipChange; -use crate::BoxError; - -use super::{SyncStatus, TIPS_RESPONSE_TIMEOUT}; +use crate::{ + components::sync::{SyncStatus, PEER_GOSSIP_DELAY, TIPS_RESPONSE_TIMEOUT}, + BoxError, +}; use BlockGossipError::*; @@ -90,6 +91,6 @@ where // // in practice, we expect blocks to arrive approximately every 75 seconds, // so waiting 6 seconds won't make much difference - tokio::time::sleep(TIPS_RESPONSE_TIMEOUT).await; + tokio::time::sleep(PEER_GOSSIP_DELAY).await; } }