Skip to content

Commit

Permalink
Abstract tendermint_node::run into 3 distinct phases
Browse files Browse the repository at this point in the history
Before the function had 3 phases, that was jumbled into 1 whole.

There was:
1. setup
2. running
3. response handling

Since this structure is rather well defined, and not much data is
shared between these phases, we just turn the function into
dispatching into functions that reprsent each stage
  • Loading branch information
mariari authored and tzemanovic committed Dec 22, 2023
1 parent 0712c33 commit 4f3bb08
Showing 1 changed file with 64 additions and 20 deletions.
84 changes: 64 additions & 20 deletions apps/src/lib/node/ledger/tendermint_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use sha2::{Digest, Sha256};
use thiserror::Error;
use tokio::fs::{File, OpenOptions};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::process::Command;
use tokio::process::{Child, Command};
use tokio::sync::oneshot::error::RecvError;
use tokio::sync::oneshot::{Receiver, Sender};

use crate::cli::namada_version;
use crate::config;
Expand Down Expand Up @@ -80,10 +82,26 @@ pub async fn run(
genesis_time: DateTimeUtc,
proxy_app_address: String,
config: config::Ledger,
abort_recv: tokio::sync::oneshot::Receiver<
tokio::sync::oneshot::Sender<()>,
>,
abort_recv: Receiver<Sender<()>>,
) -> Result<()> {
let (home_dir_string, tendermint_path) =
initalize_config(home_dir, chain_id, genesis_time, config).await?;
let tendermint_node =
start_node(proxy_app_address, home_dir_string, tendermint_path)?;

tracing::info!("CometBFT node started");

handle_node_response(tendermint_node, abort_recv).await
}

/// Setup the tendermint configuration. We return the tendermint path and home
/// directory
async fn initalize_config(
home_dir: PathBuf,
chain_id: ChainId,
genesis_time: DateTimeUtc,
config: config::Ledger,
) -> Result<(String, String)> {
let home_dir_string = home_dir.to_string_lossy().to_string();
let tendermint_path = from_env_or_default()?;
let mode = config.shell.tendermint_mode.to_str().to_owned();
Expand All @@ -101,8 +119,16 @@ pub async fn run(
write_tm_genesis(&home_dir, chain_id, genesis_time).await?;

update_tendermint_config(&home_dir, config.cometbft).await?;
Ok((home_dir_string, tendermint_path))
}

let mut tendermint_node = Command::new(&tendermint_path);
/// Startup the node
fn start_node(
proxy_app_address: String,
home_dir_string: String,
tendermint_path: String,
) -> Result<Child> {
let mut tendermint_node = Command::new(tendermint_path);
tendermint_node.args([
"start",
"--proxy_app",
Expand All @@ -119,12 +145,17 @@ pub async fn run(
tendermint_node.stdout(Stdio::null());
}

let mut tendermint_node = tendermint_node
tendermint_node
.kill_on_drop(true)
.spawn()
.map_err(Error::StartUp)?;
tracing::info!("CometBFT node started");
.map_err(Error::StartUp)
}

/// Handle the node response
async fn handle_node_response(
mut tendermint_node: Child,
abort_recv: Receiver<Sender<()>>,
) -> Result<()> {
tokio::select! {
status = tendermint_node.wait() => {
match status {
Expand All @@ -141,22 +172,30 @@ pub async fn run(
}
},
resp_sender = abort_recv => {
match resp_sender {
Ok(resp_sender) => {
tracing::info!("Shutting down Tendermint node...");
tendermint_node.kill().await.unwrap();
resp_sender.send(()).unwrap();
},
Err(err) => {
tracing::error!("The Tendermint abort sender has unexpectedly dropped: {}", err);
tracing::info!("Shutting down Tendermint node...");
tendermint_node.kill().await.unwrap();
}
}
handle_abort(resp_sender, &mut tendermint_node).await;
Ok(())
}
}
}
// Handle tendermint aborting
async fn handle_abort(
resp_sender: std::result::Result<Sender<()>, RecvError>,
node: &mut Child,
) {
match resp_sender {
Ok(resp_sender) => {
tracing_kill(node).await;
resp_sender.send(()).unwrap();
}
Err(err) => {
tracing::error!(
"The Tendermint abort sender has unexpectedly dropped: {}",
err
);
tracing_kill(node).await;
}
}
}

pub fn reset(tendermint_dir: impl AsRef<Path>) -> Result<()> {
let tendermint_path = from_env_or_default()?;
Expand Down Expand Up @@ -449,6 +488,11 @@ async fn write_tm_genesis(
})
}

async fn tracing_kill(node: &mut Child) {
tracing::info!("Shutting down Tendermint node...");
node.kill().await.unwrap();
}

fn ensure_empty(path: &PathBuf) -> std::io::Result<std::fs::File> {
std::fs::OpenOptions::new()
.create(true)
Expand Down

0 comments on commit 4f3bb08

Please sign in to comment.