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

Commit

Permalink
Validate chains are registered on startup
Browse files Browse the repository at this point in the history
Previously chains were checked lazily at the time their pubkeys were
fetched or signing was requested.

This adds an eager check at the time clients start.

It would probably be better if this check mapped to some sort of
known-good handle (e.g. `Index` into a generational arena), but this
is enough to catch the error eagerly instead of lazily.

I encountered this personally during the `cosmoshub-3` upgrade.
  • Loading branch information
tony-iqlusion committed Dec 13, 2019
1 parent e93c687 commit 78884d2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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

0 comments on commit 78884d2

Please sign in to comment.