Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for a node to sync before broadcasting protocol txs #2001

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Wait for a node to sync before broadcasting protocol txs
([\#2001](https://github.com/anoma/namada/pull/2001))
37 changes: 37 additions & 0 deletions apps/src/lib/node/ledger/broadcaster.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;
Expand Down
Loading