Die error_chain, die (#10747)
* Replace error chain for network error * Fix usages and add manual From impls * OnDemand Error and remove remaining dependencies * Die error_chain, die. * DIE * Hasta la vista, baby
This commit is contained in:
@@ -37,7 +37,7 @@ use tiny_keccak::Keccak;
|
||||
use ethkey::crypto;
|
||||
use handshake::Handshake;
|
||||
use io::{IoContext, StreamToken};
|
||||
use network::{Error, ErrorKind};
|
||||
use network::Error;
|
||||
|
||||
const ENCRYPTED_HEADER_LEN: usize = 32;
|
||||
const RECEIVE_PAYLOAD: Duration = Duration::from_secs(30);
|
||||
@@ -358,7 +358,7 @@ impl EncryptedConnection {
|
||||
let mut header = RlpStream::new();
|
||||
let len = payload.len();
|
||||
if len > MAX_PAYLOAD_SIZE {
|
||||
bail!(ErrorKind::OversizedPacket);
|
||||
return Err(Error::OversizedPacket);
|
||||
}
|
||||
header.append_raw(&[(len >> 16) as u8, (len >> 8) as u8, len as u8], 1);
|
||||
header.append_raw(&[0xc2u8, 0x80u8, 0x80u8], 1);
|
||||
@@ -386,14 +386,14 @@ impl EncryptedConnection {
|
||||
/// Decrypt and authenticate an incoming packet header. Prepare for receiving payload.
|
||||
fn read_header(&mut self, header: &[u8]) -> Result<(), Error> {
|
||||
if header.len() != ENCRYPTED_HEADER_LEN {
|
||||
return Err(ErrorKind::Auth.into());
|
||||
return Err(Error::Auth);
|
||||
}
|
||||
EncryptedConnection::update_mac(&mut self.ingress_mac, &mut self.mac_encoder, &header[0..16]);
|
||||
let mac = &header[16..];
|
||||
let mut expected = H256::zero();
|
||||
self.ingress_mac.clone().finalize(expected.as_bytes_mut());
|
||||
if mac != &expected[0..16] {
|
||||
return Err(ErrorKind::Auth.into());
|
||||
return Err(Error::Auth);
|
||||
}
|
||||
|
||||
let mut hdec = H128::default();
|
||||
@@ -422,7 +422,7 @@ impl EncryptedConnection {
|
||||
let padding = (16 - (self.payload_len % 16)) % 16;
|
||||
let full_length = self.payload_len + padding + 16;
|
||||
if payload.len() != full_length {
|
||||
return Err(ErrorKind::Auth.into());
|
||||
return Err(Error::Auth);
|
||||
}
|
||||
self.ingress_mac.update(&payload[0..payload.len() - 16]);
|
||||
EncryptedConnection::update_mac(&mut self.ingress_mac, &mut self.mac_encoder, &[0u8; 0]);
|
||||
@@ -430,7 +430,7 @@ impl EncryptedConnection {
|
||||
let mut expected = H128::default();
|
||||
self.ingress_mac.clone().finalize(expected.as_bytes_mut());
|
||||
if mac != &expected[..] {
|
||||
return Err(ErrorKind::Auth.into());
|
||||
return Err(Error::Auth);
|
||||
}
|
||||
|
||||
let mut packet = vec![0u8; self.payload_len];
|
||||
|
||||
@@ -27,7 +27,7 @@ use parity_bytes::Bytes;
|
||||
use rlp::{Rlp, RlpStream};
|
||||
|
||||
use ethkey::{KeyPair, recover, Secret, sign};
|
||||
use network::{Error, ErrorKind};
|
||||
use network::Error;
|
||||
use network::IpFilter;
|
||||
use node_table::*;
|
||||
use PROTOCOL_VERSION;
|
||||
@@ -482,12 +482,12 @@ impl<'a> Discovery<'a> {
|
||||
pub fn on_packet(&mut self, packet: &[u8], from: SocketAddr) -> Result<Option<TableUpdates>, Error> {
|
||||
// validate packet
|
||||
if packet.len() < 32 + 65 + 4 + 1 {
|
||||
return Err(ErrorKind::BadProtocol.into());
|
||||
return Err(Error::BadProtocol);
|
||||
}
|
||||
|
||||
let hash_signed = keccak(&packet[32..]);
|
||||
if hash_signed[..] != packet[0..32] {
|
||||
return Err(ErrorKind::BadProtocol.into());
|
||||
return Err(Error::BadProtocol);
|
||||
}
|
||||
|
||||
let signed = &packet[(32 + 65)..];
|
||||
@@ -512,7 +512,7 @@ impl<'a> Discovery<'a> {
|
||||
let secs_since_epoch = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||
if self.check_timestamps && timestamp < secs_since_epoch {
|
||||
debug!(target: "discovery", "Expired packet");
|
||||
return Err(ErrorKind::Expired.into());
|
||||
return Err(Error::Expired);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ use ethkey::{Generator, KeyPair, Public, Random, recover, Secret, sign};
|
||||
use ethkey::crypto::{ecdh, ecies};
|
||||
use host::HostInfo;
|
||||
use io::{IoContext, StreamToken};
|
||||
use network::{Error, ErrorKind};
|
||||
use network::Error;
|
||||
use node_table::NodeId;
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
@@ -166,7 +166,7 @@ impl Handshake {
|
||||
trace!(target: "network", "Received handshake auth from {:?}", self.connection.remote_addr_str());
|
||||
if data.len() != V4_AUTH_PACKET_SIZE {
|
||||
debug!(target: "network", "Wrong auth packet size");
|
||||
return Err(ErrorKind::BadProtocol.into());
|
||||
return Err(Error::BadProtocol);
|
||||
}
|
||||
self.auth_cipher = data.to_vec();
|
||||
match ecies::decrypt(secret, &[], data) {
|
||||
@@ -183,7 +183,7 @@ impl Handshake {
|
||||
let total = ((u16::from(data[0]) << 8 | (u16::from(data[1]))) as usize) + 2;
|
||||
if total < V4_AUTH_PACKET_SIZE {
|
||||
debug!(target: "network", "Wrong EIP8 auth packet size");
|
||||
return Err(ErrorKind::BadProtocol.into());
|
||||
return Err(Error::BadProtocol);
|
||||
}
|
||||
let rest = total - data.len();
|
||||
self.state = HandshakeState::ReadingAuthEip8;
|
||||
@@ -212,7 +212,7 @@ impl Handshake {
|
||||
trace!(target: "network", "Received handshake ack from {:?}", self.connection.remote_addr_str());
|
||||
if data.len() != V4_ACK_PACKET_SIZE {
|
||||
debug!(target: "network", "Wrong ack packet size");
|
||||
return Err(ErrorKind::BadProtocol.into());
|
||||
return Err(Error::BadProtocol);
|
||||
}
|
||||
self.ack_cipher = data.to_vec();
|
||||
match ecies::decrypt(secret, &[], data) {
|
||||
@@ -226,7 +226,7 @@ impl Handshake {
|
||||
let total = (((u16::from(data[0])) << 8 | (u16::from(data[1]))) as usize) + 2;
|
||||
if total < V4_ACK_PACKET_SIZE {
|
||||
debug!(target: "network", "Wrong EIP8 ack packet size");
|
||||
return Err(ErrorKind::BadProtocol.into());
|
||||
return Err(Error::BadProtocol);
|
||||
}
|
||||
let rest = total - data.len();
|
||||
self.state = HandshakeState::ReadingAckEip8;
|
||||
|
||||
@@ -44,7 +44,7 @@ use io::*;
|
||||
use ip_utils::{map_external_address, select_public_address};
|
||||
use network::{NetworkConfiguration, NetworkIoMessage, PacketId, PeerId, ProtocolId};
|
||||
use network::{NetworkContext as NetworkContextTrait, NonReservedPeerMode};
|
||||
use network::{DisconnectReason, Error, ErrorKind, NetworkProtocolHandler, SessionInfo};
|
||||
use network::{DisconnectReason, Error, NetworkProtocolHandler, SessionInfo};
|
||||
use network::{ConnectionDirection, ConnectionFilter};
|
||||
use network::client_version::ClientVersion;
|
||||
use node_table::*;
|
||||
@@ -157,7 +157,7 @@ impl<'s> NetworkContextTrait for NetworkContext<'s> {
|
||||
|
||||
fn respond(&self, packet_id: PacketId, data: Vec<u8>) -> Result<(), Error> {
|
||||
assert!(self.session.is_some(), "Respond called without network context");
|
||||
self.session_id.map_or_else(|| Err(ErrorKind::Expired.into()), |id| self.send(id, packet_id, data))
|
||||
self.session_id.map_or_else(|| Err(Error::Expired), |id| self.send(id, packet_id, data))
|
||||
}
|
||||
|
||||
fn disable_peer(&self, peer: PeerId) {
|
||||
@@ -719,8 +719,8 @@ impl Host {
|
||||
Err(e) => {
|
||||
let s = session.lock();
|
||||
trace!(target: "network", "Session read error: {}:{:?} ({:?}) {:?}", token, s.id(), s.remote_addr(), e);
|
||||
match *e.kind() {
|
||||
ErrorKind::Disconnect(DisconnectReason::IncompatibleProtocol) | ErrorKind::Disconnect(DisconnectReason::UselessPeer) => {
|
||||
match e {
|
||||
Error::Disconnect(DisconnectReason::IncompatibleProtocol) | Error::Disconnect(DisconnectReason::UselessPeer) => {
|
||||
if let Some(id) = s.id() {
|
||||
if !self.reserved_nodes.read().contains(id) {
|
||||
let mut nodes = self.nodes.write();
|
||||
|
||||
@@ -68,8 +68,6 @@ extern crate bytes;
|
||||
extern crate crypto as rcrypto;
|
||||
#[cfg(test)]
|
||||
extern crate env_logger;
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
extern crate ethcore_io as io;
|
||||
extern crate ethcore_network as network;
|
||||
extern crate ethereum_types;
|
||||
|
||||
@@ -31,7 +31,7 @@ use serde_json;
|
||||
|
||||
use discovery::{NodeEntry, TableUpdates};
|
||||
use ip_utils::*;
|
||||
use network::{AllowIP, Error, ErrorKind, IpFilter};
|
||||
use network::{AllowIP, Error, IpFilter};
|
||||
|
||||
/// Node public key
|
||||
pub type NodeId = H512;
|
||||
@@ -133,8 +133,8 @@ impl FromStr for NodeEndpoint {
|
||||
address: a,
|
||||
udp_port: a.port()
|
||||
}),
|
||||
Ok(None) => bail!(ErrorKind::AddressResolve(None)),
|
||||
Err(_) => Err(ErrorKind::AddressParse.into()) // always an io::Error of InvalidInput kind
|
||||
Ok(None) => return Err(Error::AddressResolve(None.into())),
|
||||
Err(_) => Err(Error::AddressParse) // always an io::Error of InvalidInput kind
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -216,7 +216,7 @@ impl FromStr for Node {
|
||||
type Err = Error;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let (id, endpoint) = if s.len() > 136 && &s[0..8] == "enode://" && &s[136..137] == "@" {
|
||||
(s[8..136].parse().map_err(|_| ErrorKind::InvalidNodeId)?, NodeEndpoint::from_str(&s[137..])?)
|
||||
(s[8..136].parse().map_err(|_| Error::InvalidNodeId)?, NodeEndpoint::from_str(&s[137..])?)
|
||||
}
|
||||
else {
|
||||
(NodeId::default(), NodeEndpoint::from_str(s)?)
|
||||
@@ -629,21 +629,21 @@ mod tests {
|
||||
fn endpoint_parse_empty_ip_string_returns_error() {
|
||||
let endpoint = NodeEndpoint::from_str("");
|
||||
assert!(endpoint.is_err());
|
||||
assert_matches!(endpoint.unwrap_err().kind(), &ErrorKind::AddressParse);
|
||||
assert_matches!(endpoint.unwrap_err(), Error::AddressParse);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn endpoint_parse_invalid_ip_string_returns_error() {
|
||||
let endpoint = NodeEndpoint::from_str("beef");
|
||||
assert!(endpoint.is_err());
|
||||
assert_matches!(endpoint.unwrap_err().kind(), &ErrorKind::AddressParse);
|
||||
assert_matches!(endpoint.unwrap_err(), Error::AddressParse);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn endpoint_parse_valid_ip_without_port_returns_error() {
|
||||
let endpoint = NodeEndpoint::from_str("123.123.123.123");
|
||||
assert!(endpoint.is_err());
|
||||
assert_matches!(endpoint.unwrap_err().kind(), &ErrorKind::AddressParse);
|
||||
assert_matches!(endpoint.unwrap_err(), Error::AddressParse);
|
||||
let endpoint = NodeEndpoint::from_str("123.123.123.123:123");
|
||||
assert!(endpoint.is_ok())
|
||||
}
|
||||
@@ -668,11 +668,11 @@ mod tests {
|
||||
fn node_parse_fails_for_invalid_urls() {
|
||||
let node = Node::from_str("foo");
|
||||
assert!(node.is_err());
|
||||
assert_matches!(node.unwrap_err().kind(), &ErrorKind::AddressParse);
|
||||
assert_matches!(node.unwrap_err(), Error::AddressParse);
|
||||
|
||||
let node = Node::from_str("enode://foo@bar");
|
||||
assert!(node.is_err());
|
||||
assert_matches!(node.unwrap_err().kind(), &ErrorKind::AddressParse);
|
||||
assert_matches!(node.unwrap_err(), Error::AddressParse);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -30,7 +30,7 @@ use connection::{Connection, EncryptedConnection, MAX_PAYLOAD_SIZE, Packet};
|
||||
use handshake::Handshake;
|
||||
use host::*;
|
||||
use io::{IoContext, StreamToken};
|
||||
use network::{DisconnectReason, Error, ErrorKind, PeerCapabilityInfo, ProtocolId, SessionInfo};
|
||||
use network::{DisconnectReason, Error, PeerCapabilityInfo, ProtocolId, SessionInfo};
|
||||
use network::client_version::ClientVersion;
|
||||
use network::SessionCapabilityInfo;
|
||||
use node_table::NodeId;
|
||||
@@ -256,10 +256,10 @@ impl Session {
|
||||
where Message: Send + Sync + Clone {
|
||||
if protocol.is_some() && (self.info.capabilities.is_empty() || !self.had_hello) {
|
||||
debug!(target: "network", "Sending to unconfirmed session {}, protocol: {:?}, packet: {}", self.token(), protocol.as_ref().map(|p| str::from_utf8(&p[..]).unwrap_or("??")), packet_id);
|
||||
bail!(ErrorKind::BadProtocol);
|
||||
return Err(Error::BadProtocol);
|
||||
}
|
||||
if self.expired() {
|
||||
return Err(ErrorKind::Expired.into());
|
||||
return Err(Error::Expired);
|
||||
}
|
||||
let mut i = 0usize;
|
||||
let pid = match protocol {
|
||||
@@ -281,7 +281,7 @@ impl Session {
|
||||
let mut payload = data; // create a reference with local lifetime
|
||||
if self.compression {
|
||||
if payload.len() > MAX_PAYLOAD_SIZE {
|
||||
bail!(ErrorKind::OversizedPacket);
|
||||
return Err(Error::OversizedPacket);
|
||||
}
|
||||
let len = snappy::compress_into(&payload, &mut compressed);
|
||||
trace!(target: "network", "compressed {} to {}", payload.len(), len);
|
||||
@@ -331,16 +331,16 @@ impl Session {
|
||||
fn read_packet<Message>(&mut self, io: &IoContext<Message>, packet: &Packet, host: &HostInfo) -> Result<SessionData, Error>
|
||||
where Message: Send + Sync + Clone {
|
||||
if packet.data.len() < 2 {
|
||||
return Err(ErrorKind::BadProtocol.into());
|
||||
return Err(Error::BadProtocol);
|
||||
}
|
||||
let packet_id = packet.data[0];
|
||||
if packet_id != PACKET_HELLO && packet_id != PACKET_DISCONNECT && !self.had_hello {
|
||||
return Err(ErrorKind::BadProtocol.into());
|
||||
return Err(Error::BadProtocol);
|
||||
}
|
||||
let data = if self.compression {
|
||||
let compressed = &packet.data[1..];
|
||||
if snappy::decompressed_len(&compressed)? > MAX_PAYLOAD_SIZE {
|
||||
bail!(ErrorKind::OversizedPacket);
|
||||
return Err(Error::OversizedPacket);
|
||||
}
|
||||
snappy::decompress(&compressed)?
|
||||
} else {
|
||||
@@ -358,7 +358,7 @@ impl Session {
|
||||
if self.had_hello {
|
||||
debug!(target:"network", "Disconnected: {}: {:?}", self.token(), DisconnectReason::from_u8(reason));
|
||||
}
|
||||
Err(ErrorKind::Disconnect(DisconnectReason::from_u8(reason)).into())
|
||||
Err(Error::Disconnect(DisconnectReason::from_u8(reason)))
|
||||
}
|
||||
PACKET_PING => {
|
||||
self.send_pong(io)?;
|
||||
@@ -500,7 +500,7 @@ impl Session {
|
||||
rlp.append(&(reason as u32));
|
||||
self.send_packet(io, None, PACKET_DISCONNECT, &rlp.drain()).ok();
|
||||
}
|
||||
ErrorKind::Disconnect(reason).into()
|
||||
Error::Disconnect(reason)
|
||||
}
|
||||
|
||||
fn send<Message>(&mut self, io: &IoContext<Message>, data: &[u8]) -> Result<(), Error> where Message: Send + Sync + Clone {
|
||||
|
||||
Reference in New Issue
Block a user