From 235ba3f64481efd679e873f786ae37a50ddfa953 Mon Sep 17 00:00:00 2001 From: skoupidi Date: Tue, 25 Jun 2024 16:00:38 +0300 Subject: [PATCH] darkfid: purge current forks on network dc --- bin/darkfid/src/task/consensus.rs | 1 + bin/darkfid/src/task/sync.rs | 4 ++-- src/validator/consensus.rs | 10 ++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/bin/darkfid/src/task/consensus.rs b/bin/darkfid/src/task/consensus.rs index aa505d15d..3b62aae9f 100644 --- a/bin/darkfid/src/task/consensus.rs +++ b/bin/darkfid/src/task/consensus.rs @@ -147,6 +147,7 @@ pub async fn consensus_init_task( Err(Error::NetworkNotConnected) => { // Sync node again *node.validator.synced.write().await = false; + node.validator.consensus.purge_forks().await?; if !config.skip_sync { sync_task(&node, checkpoint).await?; } else { diff --git a/bin/darkfid/src/task/sync.rs b/bin/darkfid/src/task/sync.rs index 7df7a8ef5..c14f09885 100644 --- a/bin/darkfid/src/task/sync.rs +++ b/bin/darkfid/src/task/sync.rs @@ -204,8 +204,8 @@ async fn synced_peers( let _ = subscription.receive().await; subscription.unsubscribe().await; - info!(target: "darkfid::task::sync::synced_peers", "Sleeping a bit to allow for more nodes to connect..."); - sleep(node.p2p.settings().outbound_connect_timeout).await; + info!(target: "darkfid::task::sync::synced_peers", "Sleeping for {comms_timeout} to allow for more nodes to connect..."); + sleep(comms_timeout).await; } Ok(tips) diff --git a/src/validator/consensus.rs b/src/validator/consensus.rs index 6c46613d8..b464bfd78 100644 --- a/src/validator/consensus.rs +++ b/src/validator/consensus.rs @@ -492,6 +492,16 @@ impl Consensus { Ok(()) } + + /// Auxiliary function to fully purge current forks and leave only a new empty fork. + pub async fn purge_forks(&self) -> Result<()> { + debug!(target: "validator::consensus::purge_forks", "Purging current forks..."); + let mut forks = self.forks.write().await; + *forks = vec![Fork::new(self.blockchain.clone(), self.module.read().await.clone()).await?]; + drop(forks); + debug!(target: "validator::consensus::purge_forks", "Forks purged!"); + Ok(()) + } } /// This struct represents a block proposal, used for consensus.