diff --git a/src/client.rs b/src/client.rs index 643bcd9..347f78a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -6,12 +6,13 @@ //! as a "Key Management System". use crate::{ + chain, config::ValidatorConfig, error::{Error, ErrorKind}, prelude::*, session::Session, }; -use std::{panic, process, thread, time::Duration}; +use std::{panic, process::exit, thread, time::Duration}; /// Join handle type used by our clients type JoinHandle = thread::JoinHandle>; @@ -36,6 +37,8 @@ pub struct Client { impl Client { /// Spawn a new client, returning a handle so it can be joined pub fn spawn(config: ValidatorConfig) -> Self { + register_chain(&config.chain_id); + let name = format!("{}@{}", &config.chain_id, &config.addr); let handle = thread::Builder::new() @@ -43,7 +46,7 @@ impl Client { .spawn(move || main_loop(config)) .unwrap_or_else(|e| { status_err!("error spawning thread: {}", e); - process::exit(1); + exit(1); }); Self { name, handle } @@ -82,6 +85,20 @@ fn main_loop(config: ValidatorConfig) -> Result<(), Error> { Ok(()) } +/// Ensure chain with given ID is properly registered +pub fn register_chain(chain_id: &chain::Id) { + let registry = chain::REGISTRY.get(); + + debug!("registering chain: {}", chain_id); + registry.get_chain(chain_id).unwrap_or_else(|| { + status_err!( + "unregistered chain: {} (add it to tmkms.toml's [[chain]] section)", + chain_id + ); + exit(1); + }); +} + /// Open a new session and run the session loop pub fn run_client(config: ValidatorConfig) -> Result<(), Error> { panic::catch_unwind(move || Session::open(config)?.request_loop()) diff --git a/src/session.rs b/src/session.rs index 9599f69..ea8b8e1 100644 --- a/src/session.rs +++ b/src/session.rs @@ -127,7 +127,11 @@ impl Session { request.validate()?; let registry = chain::REGISTRY.get(); + + // unwrap is acceptable here as chain presence is validated in client.rs's + // `register_chain` function. let chain = registry.get_chain(&self.config.chain_id).unwrap(); + let (_, request_state) = parse_request(&request)?; let mut chain_state = chain.state.lock().unwrap(); @@ -178,6 +182,8 @@ impl Session { /// Get the public key for (the only) public key in the keyring fn get_public_key(&mut self, _request: &PubKeyRequest) -> Result { + // unwrap is acceptable here as chain presence is validated in client.rs's + // `register_chain` function. let registry = chain::REGISTRY.get(); let chain = registry.get_chain(&self.config.chain_id).unwrap();