From 0ea463aa94dd804e358955b1ff27bd09e5619719 Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Thu, 19 Oct 2023 13:29:22 +0100 Subject: [PATCH 1/2] Wait for a node to sync before broadcasting protocol txs --- apps/src/lib/node/ledger/broadcaster.rs | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/apps/src/lib/node/ledger/broadcaster.rs b/apps/src/lib/node/ledger/broadcaster.rs index 915a7eee67..54c7ce0899 100644 --- a/apps/src/lib/node/ledger/broadcaster.rs +++ b/apps/src/lib/node/ledger/broadcaster.rs @@ -1,5 +1,7 @@ use std::net::SocketAddr; +use std::ops::ControlFlow; +use namada::types::control_flow::time; use tokio::sync::mpsc::UnboundedReceiver; use crate::facade::tendermint_rpc::{Client, HttpClient}; @@ -26,6 +28,41 @@ impl Broadcaster { /// Loop forever, braodcasting messages that have been received /// by the receiver async fn run_loop(&mut self) { + let result = time::Sleep { + strategy: time::ExponentialBackoff { + base: 2, + as_duration: time::Duration::from_secs, + }, + } + .run(|| async { + let status_result = time::Sleep { + strategy: time::Constant(time::Duration::from_secs(1)), + } + .timeout( + time::Instant::now() + time::Duration::from_secs(30), + || async { + match self.client.status().await { + Ok(status) => ControlFlow::Break(status), + Err(_) => ControlFlow::Continue(()), + } + }, + ) + .await; + let status = match status_result { + Ok(status) => status, + Err(_) => return ControlFlow::Break(Err(())), + }; + if status.sync_info.catching_up { + ControlFlow::Continue(()) + } else { + ControlFlow::Break(Ok(())) + } + }) + .await; + if let Err(()) = result { + tracing::error!("Broadcaster failed to connect to CometBFT node"); + return; + } loop { if let Some(msg) = self.receiver.recv().await { let _ = self.client.broadcast_tx_sync(msg.into()).await; From 92882ffa7689aea62036d149ead88c3df04a4ac6 Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Thu, 19 Oct 2023 14:01:05 +0100 Subject: [PATCH 2/2] Changelog --- .../unreleased/improvements/2001-no-tx-broadcast-on-sync.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/improvements/2001-no-tx-broadcast-on-sync.md diff --git a/.changelog/unreleased/improvements/2001-no-tx-broadcast-on-sync.md b/.changelog/unreleased/improvements/2001-no-tx-broadcast-on-sync.md new file mode 100644 index 0000000000..aab919dd41 --- /dev/null +++ b/.changelog/unreleased/improvements/2001-no-tx-broadcast-on-sync.md @@ -0,0 +1,2 @@ +- Wait for a node to sync before broadcasting protocol txs + ([\#2001](https://github.com/anoma/namada/pull/2001)) \ No newline at end of file