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

Commit

Permalink
tendermint-rs: Move Address into the crate; node::Id
Browse files Browse the repository at this point in the history
Moves the ValidatorAddr type into the Tendermint crate, as it's
generally useful for any remote address.

Also moves the `PeerId` type from `secret_connection` into a `node::Id`
crate which isn't gated on the rest of the Secret Connection code.
  • Loading branch information
tony-iqlusion committed Apr 15, 2019
1 parent 9c7ceb8 commit 53c9931
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 134 deletions.
10 changes: 5 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! as a "Key Management System".

use crate::{
config::{ValidatorAddr, ValidatorConfig},
config::ValidatorConfig,
error::{KmsError, KmsErrorKind},
keyring::SecretKeyEncoding,
session::Session,
Expand All @@ -21,7 +21,7 @@ use std::{
thread::{self, JoinHandle},
time::Duration,
};
use tendermint::{chain, secret_connection};
use tendermint::{chain, node, secret_connection, Address};

/// How long to wait after a crash before respawning (in seconds)
pub const RESPAWN_DELAY: u64 = 1;
Expand Down Expand Up @@ -68,7 +68,7 @@ fn client_loop(config: ValidatorConfig, should_term: &Arc<AtomicBool>) {
}

let session_result = match &addr {
ValidatorAddr::Tcp {
Address::Tcp {
peer_id,
host,
port,
Expand All @@ -82,7 +82,7 @@ fn client_loop(config: ValidatorConfig, should_term: &Arc<AtomicBool>) {
return;
}
},
ValidatorAddr::Unix { socket_path } => unix_session(chain_id, socket_path, should_term),
Address::Unix { path } => unix_session(chain_id, path, should_term),
};

if let Err(e) = session_result {
Expand All @@ -107,7 +107,7 @@ fn client_loop(config: ValidatorConfig, should_term: &Arc<AtomicBool>) {
/// Create a TCP connection to a validator (encrypted with SecretConnection)
fn tcp_session(
chain_id: chain::Id,
validator_peer_id: Option<secret_connection::PeerId>,
validator_peer_id: Option<node::Id>,
host: &str,
port: u16,
secret_key_path: &Path,
Expand Down
8 changes: 2 additions & 6 deletions src/config/validator/mod.rs → src/config/validator.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
mod addr;

use std::path::PathBuf;
use tendermint::chain;

pub use self::addr::ValidatorAddr;
use tendermint::{chain, Address};

/// Validator configuration
#[derive(Clone, Deserialize, Debug)]
pub struct ValidatorConfig {
/// Address of the validator (`tcp://` or `unix://`)
pub addr: ValidatorAddr,
pub addr: Address,

/// Chain ID of the Tendermint network this validator is part of
pub chain_id: chain::Id,
Expand Down
90 changes: 0 additions & 90 deletions src/config/validator/addr.rs

This file was deleted.

3 changes: 2 additions & 1 deletion src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::{
use subtle::ConstantTimeEq;
use tendermint::{
amino_types::{PingRequest, PingResponse, PubKeyRequest, PubKeyResponse},
node,
secret_connection::{self, SecretConnection},
};

Expand All @@ -41,7 +42,7 @@ impl Session<SecretConnection<TcpStream>> {
/// Create a new session with the validator at the given address/port
pub fn connect_tcp(
chain_id: chain::Id,
validator_peer_id: Option<secret_connection::PeerId>,
validator_peer_id: Option<node::Id>,
host: &str,
port: u16,
secret_connection_key: &ed25519::Seed,
Expand Down
93 changes: 93 additions & 0 deletions tendermint-rs/src/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//! Remote addresses (`tcp://` or `unix://`)

use crate::node;
use failure::{bail, Error};
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize, Serializer};
use std::{
fmt::{self, Display},
path::PathBuf,
str::{self, FromStr},
};

/// Remote address (TCP or UNIX socket)
#[derive(Clone, Debug)]
pub enum Address {
/// TCP connections
Tcp {
/// Remote peer ID
peer_id: Option<node::Id>,

/// Hostname or IP address
host: String,

/// Port
port: u16,
},

/// UNIX domain sockets
Unix {
/// Path to a UNIX domain socket path
path: PathBuf,
},
}

impl<'de> Deserialize<'de> for Address {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Self::from_str(&String::deserialize(deserializer)?)
.map_err(|e| D::Error::custom(format!("{}", e)))
}
}

impl Display for Address {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Address::Tcp { host, port, .. } => write!(f, "tcp://{}:{}", host, port),
Address::Unix { path } => write!(f, "unix://{}", path.display()),
}
}
}

impl FromStr for Address {
type Err = Error;

fn from_str(addr: &str) -> Result<Self, Error> {
if addr.starts_with("tcp://") {
let authority_parts = addr[6..].split('@').collect::<Vec<_>>();

let (peer_id, authority) = match authority_parts.len() {
1 => (None, authority_parts[0]),
2 => (Some(authority_parts[0].parse()?), authority_parts[1]),
_ => bail!("invalid tcp:// address: {}", addr),
};

let host_and_port: Vec<&str> = authority.split(':').collect();

if host_and_port.len() != 2 {
bail!("invalid tcp:// address: {}", addr);
}

let host = host_and_port[0].to_owned();

match host_and_port[1].parse() {
Ok(port) => Ok(Address::Tcp {
peer_id,
host,
port,
}),
Err(_) => bail!("invalid tcp:// address (bad port): {}", addr),
}
} else if addr.starts_with("unix://") {
Ok(Address::Unix {
path: PathBuf::from(&addr[7..]),
})
} else {
bail!("invalid address: {}", addr)
}
}
}

impl Serialize for Address {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.to_string().serialize(serializer)
}
}
3 changes: 3 additions & 0 deletions tendermint-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ extern crate prost_amino as prost;
#[macro_use]
extern crate prost_amino_derive as prost_derive;

pub mod address;
pub mod algorithm;
#[cfg(feature = "amino-types")]
pub mod amino_types;
pub mod block;
pub mod chain;
pub mod error;
pub mod hash;
pub mod node;
pub mod public_keys;
#[cfg(feature = "secret-connection")]
pub mod secret_connection;
Expand All @@ -40,6 +42,7 @@ pub mod timestamp;
#[cfg(feature = "secret-connection")]
pub use crate::secret_connection::SecretConnection;
pub use crate::{
address::*,
algorithm::*,
block::{ParseHeight as ParseBlockHeight, ParseId as ParseBlockId},
chain::ParseId as ParseChainId,
Expand Down
Loading

0 comments on commit 53c9931

Please sign in to comment.