use crate::{
config::chain::HookConfig,
error::{Error, ErrorKind::HookError},
prelude::*,
};
use cometbft::block;
use serde::Deserialize;
use std::{process::Command, time::Duration};
use wait_timeout::ChildExt;
const DEFAULT_TIMEOUT_SECS: u64 = 1;
pub const BLOCK_HEIGHT_SANITY_LIMIT: u64 = 9000;
pub fn run(config: &HookConfig) -> Result<Output, Error> {
let mut child = Command::new(&config.cmd[0])
.args(&config.cmd[1..])
.spawn()?;
let timeout = Duration::from_secs(config.timeout_secs.unwrap_or(DEFAULT_TIMEOUT_SECS));
match child.wait_timeout(timeout)? {
Some(status) => {
if status.success() {
if let Some(stdout) = child.stdout {
Ok(serde_json::from_reader(stdout)?)
} else {
fail!(HookError, "couldn't consume stdout from child");
}
} else {
fail!(HookError, "subcommand returned status {:?}", status.code())
}
}
None => {
child.kill()?;
child.wait()?;
fail!(HookError, "subcommand timed out after {:?}", timeout)
}
}
}
#[derive(Debug, Deserialize)]
pub struct Output {
pub latest_block_height: block::Height,
}
#[cfg(test)]
mod tests {
use crate::config::chain::HookConfig;
#[test]
fn hook_test() {
let _ = super::run(&HookConfig {
cmd: ["todo", "real", "example"]
.iter()
.map(|str| str.into())
.collect(),
timeout_secs: Some(0),
fail_closed: true,
});
}
}