This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tendermint-rs: Move Address into the crate; node::Id
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
1 parent
9c7ceb8
commit 53c9931
Showing
9 changed files
with
137 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.