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
72
mod guard;
mod registry;
pub mod state;
pub use self::{
guard::Guard,
registry::{GlobalRegistry, Registry, REGISTRY},
state::State,
};
use crate::{
config::{chain::ChainConfig, KmsConfig},
error::Error,
keyring::{self, KeyRing},
};
use std::{path::PathBuf, sync::Mutex};
pub use tendermint::chain::Id;
pub struct Chain {
pub id: Id,
pub keyring: KeyRing,
pub state: Mutex<State>,
}
impl Chain {
pub fn from_config(config: &ChainConfig) -> Result<Chain, Error> {
let state_file = match config.state_file {
Some(ref path) => path.to_owned(),
None => PathBuf::from(&format!("{}_priv_validator_state.json", config.id)),
};
let mut state = State::load_state(state_file)?;
if let Some(ref hook) = config.state_hook {
match state::hook::run(hook) {
Ok(hook_output) => state.update_from_hook_output(hook_output)?,
Err(e) => {
if hook.fail_closed {
return Err(e);
} else {
error!("error invoking state hook for chain {}: {}", config.id, e);
}
}
}
}
Ok(Self {
id: config.id,
keyring: KeyRing::new(config.key_format.clone()),
state: Mutex::new(state),
})
}
}
pub fn load_config(config: &KmsConfig) -> Result<(), Error> {
for config in &config.chain {
REGISTRY.register(Chain::from_config(config)?)?;
}
let mut registry = REGISTRY.0.write().unwrap();
keyring::load_config(&mut registry, &config.providers)
}