Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Validate chains are registered on startup #376

Merged
merged 1 commit into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<(), Error>>;
Expand All @@ -36,14 +37,16 @@ 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()
.name(name.clone())
.spawn(move || main_loop(config))
.unwrap_or_else(|e| {
status_err!("error spawning thread: {}", e);
process::exit(1);
exit(1);
});

Self { name, handle }
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 6 additions & 0 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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<Response, Error> {
// 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();

Expand Down