Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tokio 1.0 upgrade #290

Merged
merged 1 commit into from
Feb 3, 2021
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
506 changes: 166 additions & 340 deletions Cargo.lock

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ keywords = ["cosmos", "ed25519", "kms", "key-management", "yubihsm"]
edition = "2018"

[dependencies]
abscissa_core = "0.5"
abscissa_tokio = { version = "0.5", optional = true }
bytes = "0.5"
abscissa_core = "=0.6.0-pre.1"
abscissa_tokio = { version = "=0.6.0-pre.1", optional = true }
bytes_v0_5 = { version = "0.5", package = "bytes" }
bytes = "1"
chrono = "0.4"
ed25519-dalek = "1"
getrandom = "0.1"
gumdrop = "0.7"
hkd32 = { version = "0.5", default-features = false, features = ["mnemonic"] }
hkdf = "0.10.0"
hyper = { version = "0.13", optional = true }
hyper-rustls = { version = "0.21", optional = true }
hkdf = "0.10"
hyper = { version = "0.14", optional = true }
hyper-rustls = { version = "0.22", optional = true, features = ["webpki-roots"] }
k256 = { version = "0.7", features = ["ecdsa", "sha256"] }
ledger = { version = "0.2", optional = true }
once_cell = "1.5"
prost = "0.6"
prost = "0.7"
prost-amino = "0.6"
prost-amino-derive = "0.6"
prost-derive = "0.6"
prost-derive = "0.7"
rand_core = { version = "0.5", features = ["std"] }
rpassword = { version = "5", optional = true }
serde = { version = "1", features = ["serde_derive"] }
Expand All @@ -39,17 +40,17 @@ stdtx = { version = "0.4", optional = true }
subtle = "2"
subtle-encoding = { version = "0.5", features = ["bech32-preview"] }
tempfile = "3"
tendermint = { version = "0.17", features = ["secp256k1"] }
tendermint-rpc = { version = "0.17", optional = true, features = ["http-client"] }
tendermint-proto = "0.17"
tendermint-p2p = { version = "0.17", features = ["amino"] }
tendermint = { version = "0.18", features = ["secp256k1"] }
tendermint-rpc = { version = "0.18", optional = true, features = ["http-client"] }
tendermint-proto = "0.18"
tendermint-p2p = { version = "0.18", features = ["amino"] }
thiserror = "1"
wait-timeout = "0.2"
yubihsm = { version = "0.38", features = ["secp256k1", "setup", "usb"], optional = true }
zeroize = "1"

[dev-dependencies]
abscissa_core = { version = "0.5", features = ["testing"] }
abscissa_core = { version = "=0.6.0-pre.1", features = ["testing"] }
byteorder = "1"
rand = "0.7"

Expand Down
5 changes: 4 additions & 1 deletion src/amino_types/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::{
};
use crate::{config::validator::ProtocolVersion, rpc};
use bytes::BufMut;
use bytes_v0_5::BytesMut as BytesMutV05;
use ed25519_dalek as ed25519;
use once_cell::sync::Lazy;
use prost::Message as _;
Expand Down Expand Up @@ -161,7 +162,9 @@ impl SignableMsg for SignProposalRequest {
timestamp: proposal.timestamp,
};

cp.encode_length_delimited(sign_bytes)?;
let mut sign_bytes_v0_5 = BytesMutV05::new();
cp.encode_length_delimited(&mut sign_bytes_v0_5)?;
sign_bytes.put_slice(sign_bytes_v0_5.as_ref());
}

Ok(true)
Expand Down
5 changes: 4 additions & 1 deletion src/amino_types/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::{
};
use crate::{config::validator::ProtocolVersion, rpc};
use bytes::BufMut;
use bytes_v0_5::BytesMut as BytesMutV05;
use ed25519_dalek as ed25519;
use once_cell::sync::Lazy;
use prost::Message as _;
Expand Down Expand Up @@ -221,7 +222,9 @@ impl SignableMsg for SignVoteRequest {
cv.encode_length_delimited(sign_bytes).unwrap();
} else {
let cv = CanonicalVote::new(vote, chain_id.as_str());
cv.encode_length_delimited(sign_bytes)?;
let mut sign_bytes_v0_5 = BytesMutV05::new();
cv.encode_length_delimited(&mut sign_bytes_v0_5)?;
sign_bytes.put_slice(sign_bytes_v0_5.as_ref());
}

Ok(true)
Expand Down
43 changes: 11 additions & 32 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,18 @@
use crate::{commands::KmsCommand, config::KmsConfig};
use abscissa_core::{
application::{self, AppCell},
config::{self, CfgCell},
trace, Application, FrameworkError, StandardPaths,
};

/// Application state
pub static APPLICATION: AppCell<KmsApplication> = AppCell::new();

/// Obtain a read-only (multi-reader) lock on the application state.
///
/// Panics if the application state has not been initialized.
pub fn app_reader() -> application::lock::Reader<KmsApplication> {
APPLICATION.read()
}

/// Obtain an exclusive mutable lock on the application state.
pub fn app_writer() -> application::lock::Writer<KmsApplication> {
APPLICATION.write()
}

/// Obtain a read-only (multi-reader) lock on the application configuration.
///
/// Panics if the application configuration has not been loaded.
pub fn app_config() -> abscissa_core::config::Reader<KmsApplication> {
abscissa_core::config::Reader::new(&APPLICATION)
}
pub static APP: AppCell<KmsApplication> = AppCell::new();

/// The `tmkms` application
#[derive(Debug)]
pub struct KmsApplication {
/// Application configuration.
config: Option<KmsConfig>,
config: CfgCell<KmsConfig>,

/// Application state.
state: application::State<Self>,
Expand All @@ -41,7 +23,7 @@ pub struct KmsApplication {
impl Default for KmsApplication {
fn default() -> Self {
Self {
config: None,
config: CfgCell::default(),
state: application::State::default(),
}
}
Expand All @@ -58,20 +40,15 @@ impl Application for KmsApplication {
type Paths = StandardPaths;

/// Accessor for application configuration.
fn config(&self) -> &KmsConfig {
self.config.as_ref().expect("not configured yet")
fn config(&self) -> config::Reader<KmsConfig> {
self.config.read()
}

/// Borrow the application state immutably.
fn state(&self) -> &application::State<Self> {
&self.state
}

/// Borrow the application state mutably.
fn state_mut(&mut self) -> &mut application::State<Self> {
&mut self.state
}

/// Register all components used by this application.
///
/// If you would like to add additional components to your application
Expand All @@ -84,7 +61,8 @@ impl Application for KmsApplication {
#[cfg(feature = "tx-signer")]
components.push(Box::new(abscissa_tokio::TokioComponent::new()?));

self.state.components.register(components)
let mut component_registry = self.state.components_mut();
component_registry.register(components)
}

/// Post-configuration lifecycle callback.
Expand All @@ -93,8 +71,9 @@ impl Application for KmsApplication {
/// time in app lifecycle when configuration would be loaded if
/// possible.
fn after_config(&mut self, config: Self::Cfg) -> Result<(), FrameworkError> {
self.state.components.after_config(&config)?;
self.config = Some(config);
let mut component_registry = self.state.components_mut();
component_registry.after_config(&config)?;
self.config.set_once(config);
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/bin/tmkms/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Main entry point for the `tmkms` executable

use tmkms::application::APPLICATION;
use tmkms::application::APP;

/// Boot the `tmkms` application
fn main() {
abscissa_core::boot(&APPLICATION);
abscissa_core::boot(&APP);
}
2 changes: 1 addition & 1 deletion src/commands/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct InitCommand {

impl Runnable for InitCommand {
fn run(&self) {
let config = app_config();
let config = APP.config();

chain::load_config(&config).unwrap_or_else(|e| {
status_err!("error loading configuration: {}", e);
Expand Down
8 changes: 4 additions & 4 deletions src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use abscissa_core::{Command, Options};
use std::{path::PathBuf, process};

#[cfg(feature = "tx-signer")]
use crate::{application::APPLICATION, config::TxSignerConfig, tx_signer::TxSigner};
use crate::{application::APP, config::TxSignerConfig, tx_signer::TxSigner};

/// The `start` command
#[derive(Command, Debug, Options)]
Expand Down Expand Up @@ -44,7 +44,7 @@ impl Runnable for StartCommand {
impl StartCommand {
/// Spawn clients from the app's configuration
fn spawn_clients(&self) -> Vec<Client> {
let config = app_config();
let config = APP.config();

chain::load_config(&config).unwrap_or_else(|e| {
status_err!("error loading configuration: {}", e);
Expand All @@ -71,7 +71,7 @@ fn run_app(validator_clients: Vec<Client>) {
#[cfg(feature = "tx-signer")]
fn run_app(validator_clients: Vec<Client>) {
let signer_config = {
let cfg = app_config();
let cfg = APP.config();

match cfg.tx_signer.len() {
0 => None,
Expand Down Expand Up @@ -114,7 +114,7 @@ fn blocking_wait(validator_clients: Vec<Client>) {
/// Launch the Tokio executor and spawn transaction signers
#[cfg(feature = "tx-signer")]
fn run_async_executor(config: TxSignerConfig) {
abscissa_tokio::run(&APPLICATION, async {
abscissa_tokio::run(&APP, async {
let mut signer = TxSigner::new(&config).unwrap_or_else(|e| {
status_err!("couldn't initialize TX signer: {}", e);
process::exit(1);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/yubihsm/keys/list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! List keys inside the YubiHSM2

use crate::{application::app_config, chain, keyring, prelude::*, Map};
use crate::{chain, keyring, prelude::*, Map};
use abscissa_core::{Command, Options, Runnable};
use k256::elliptic_curve::generic_array::GenericArray;
use std::{path::PathBuf, process};
Expand Down Expand Up @@ -76,7 +76,7 @@ fn load_key_formatters() -> Map<u16, keyring::Format> {

/// Load chain-specific key formatters from the configuration
fn load_chain_formatters() -> Map<chain::Id, keyring::Format> {
let cfg = app_config();
let cfg = APP.config();
let mut map = Map::new();

for chain in &cfg.chain {
Expand Down
6 changes: 3 additions & 3 deletions src/config/provider/yubihsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::KeyType;
use crate::{chain, prelude::*};
use abscissa_core::secret::{CloneableSecret, DebugSecret, ExposeSecret, Secret};
use serde::Deserialize;
use std::{fs, path::PathBuf, process};
use std::{fmt, fs, path::PathBuf, process};
use tendermint::net;
use yubihsm::Credentials;
use zeroize::{Zeroize, Zeroizing};
Expand Down Expand Up @@ -104,8 +104,8 @@ pub struct Password(String);
impl CloneableSecret for Password {}

impl DebugSecret for Password {
fn debug_secret() -> &'static str {
"REDACTED PASSWORD"
fn debug_secret(f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str("REDACTED PASSWORD")
}
}

Expand Down
20 changes: 6 additions & 14 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
//! Application-local prelude: conveniently import types/functions/macros
//! which are generally useful and should be available everywhere.

/// Application state accessors
pub use crate::application::{app_config, app_reader, app_writer};

/// Commonly used Abscissa traits
pub use abscissa_core::{Application, Command, Runnable};

/// Error macros
pub use abscissa_core::{ensure, fail, fatal, format_err};

/// Tracing macros
pub use abscissa_core::tracing::{debug, error, event, info, span, trace, warn, Level};
/// Abscissa core prelude
pub use abscissa_core::prelude::*;

/// Status macros
pub use abscissa_core::{
status_attr_err, status_attr_ok, status_err, status_info, status_ok, status_warn,
};
pub use abscissa_core::{status_attr_err, status_attr_ok};

/// Application state
pub use crate::application::APP;
2 changes: 1 addition & 1 deletion src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use std::io::Read;

use bytes::Bytes;
use bytes_v0_5::Bytes;
use prost::Message as _;
use prost_amino::{encoding::decode_varint, Message as _};
use tendermint_p2p::secret_connection::DATA_MAX_SIZE;
Expand Down
8 changes: 4 additions & 4 deletions src/tx_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl TxSigner {
"[{}] error getting initial block height: {}",
self.chain_id, e
);
time::delay_for(RETRY_DELAY).await
time::sleep(RETRY_DELAY).await
}
}
};
Expand All @@ -137,7 +137,7 @@ impl TxSigner {
"[{}] couldn't get current block height via RPC: {}",
&self.chain_id, e
);
time::delay_for(RETRY_DELAY).await;
time::sleep(RETRY_DELAY).await;
continue;
}
};
Expand Down Expand Up @@ -179,7 +179,7 @@ impl TxSigner {
&self.chain_id, target_height, min_secs
);

time::delay_until(min_deadline).await;
time::sleep_until(min_deadline).await;
}

return Ok(status);
Expand All @@ -193,7 +193,7 @@ impl TxSigner {
return Ok(status);
}

time::delay_for(RPC_POLL_INTERVAL).await
time::sleep(RPC_POLL_INTERVAL).await
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/tx_signer/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
error::{Error, ErrorKind},
prelude::*,
};
use bytes::Buf;
use hyper::{
http::{header, Uri},
Body,
Expand Down Expand Up @@ -40,10 +39,10 @@ impl Client {
self.add_headers(&mut request);

let builder = hyper::Client::builder();
let connector = HttpsConnector::new();
let connector = HttpsConnector::with_webpki_roots(); // TODO: local cert truststore
let response = builder.build(connector).request(request).await?;
let response_body = hyper::body::aggregate(response.into_body()).await?;
let response_json = serde_json::from_slice::<Response>(response_body.bytes())?;
let response_body = hyper::body::to_bytes(response.into_body()).await?;
let response_json = serde_json::from_slice::<Response>(response_body.as_ref())?;

match response_json.status {
Status::Ok => Ok(response_json.tx),
Expand Down
4 changes: 3 additions & 1 deletion src/yubihsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ use std::{
},
};
use yubihsm::{Client, Connector};

#[cfg(feature = "yubihsm-server")]
use zeroize::Zeroizing;

#[cfg(not(feature = "yubihsm-mock"))]
use {
crate::config::provider::yubihsm::AdapterConfig,
Expand Down Expand Up @@ -194,7 +196,7 @@ fn prompt_for_auth_key_password(auth_key_id: u16) -> yubihsm::Credentials {

/// Get the YubiHSM-related configuration
pub fn config() -> YubihsmConfig {
let kms_config = app_config();
let kms_config = APP.config();
let yubihsm_configs = &kms_config.providers.yubihsm;

if yubihsm_configs.len() != 1 {
Expand Down