1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::{
config::chain::HookConfig,
error::{Error, ErrorKind::HookError},
prelude::*,
};
use serde::Deserialize;
use std::{process::Command, time::Duration};
use tendermint::block;
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,
});
}
}