2018-08-04 18:54:30 -07:00
|
|
|
#![cfg_attr(feature = "nightly", feature(alloc_system))]
|
2018-09-10 12:07:53 -07:00
|
|
|
#![cfg_attr(feature = "nightly", feature(proc_macro))]
|
2018-11-08 06:49:02 -08:00
|
|
|
#![cfg_attr(feature = "cargo-clippy",
|
|
|
|
allow(large_enum_variant, new_without_default_derive, expect_fun_call, or_fun_call,
|
|
|
|
useless_format, cyclomatic_complexity, needless_pass_by_value, module_inception,
|
|
|
|
match_bool))]
|
2018-06-30 12:00:23 -07:00
|
|
|
|
2018-08-04 18:54:30 -07:00
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
extern crate alloc_system;
|
2018-06-30 12:00:23 -07:00
|
|
|
extern crate clap;
|
2018-07-02 19:36:12 -07:00
|
|
|
extern crate env_logger;
|
2018-08-04 18:54:30 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate failure;
|
2018-06-30 12:00:23 -07:00
|
|
|
extern crate crossbeam;
|
2018-07-08 15:08:37 -07:00
|
|
|
// #[macro_use] extern crate crossbeam_channel;
|
2018-07-02 19:36:12 -07:00
|
|
|
extern crate chrono;
|
2018-09-28 06:13:31 -07:00
|
|
|
extern crate crypto;
|
2018-07-02 19:36:12 -07:00
|
|
|
extern crate num_bigint;
|
2018-09-28 06:13:31 -07:00
|
|
|
extern crate num_traits;
|
2018-08-04 18:54:30 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate futures;
|
2018-09-28 06:13:31 -07:00
|
|
|
extern crate byteorder;
|
|
|
|
extern crate bytes;
|
|
|
|
extern crate rand;
|
2018-07-02 19:36:12 -07:00
|
|
|
extern crate tokio;
|
|
|
|
extern crate tokio_codec;
|
2018-07-08 15:08:37 -07:00
|
|
|
extern crate tokio_io;
|
|
|
|
extern crate uuid;
|
2018-08-04 18:54:30 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
2018-07-08 15:08:37 -07:00
|
|
|
extern crate bincode;
|
2018-07-12 15:32:39 -07:00
|
|
|
extern crate clear_on_drop;
|
2018-07-08 15:08:37 -07:00
|
|
|
extern crate hbbft;
|
2018-09-28 06:13:31 -07:00
|
|
|
extern crate parking_lot;
|
|
|
|
extern crate serde;
|
|
|
|
extern crate serde_bytes;
|
|
|
|
extern crate tokio_serde_bincode;
|
2018-06-30 12:00:23 -07:00
|
|
|
|
2018-08-04 18:54:30 -07:00
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
use alloc_system::System;
|
2018-07-28 15:43:13 -07:00
|
|
|
|
2018-08-04 18:54:30 -07:00
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
#[global_allocator]
|
|
|
|
static A: System = System;
|
2018-07-28 15:43:13 -07:00
|
|
|
|
2018-07-08 15:08:37 -07:00
|
|
|
// pub mod network;
|
2018-07-02 19:36:12 -07:00
|
|
|
pub mod blockchain;
|
2018-09-28 06:13:31 -07:00
|
|
|
pub mod hydrabadger;
|
2018-07-17 14:06:23 -07:00
|
|
|
pub mod peer;
|
2018-07-02 19:36:12 -07:00
|
|
|
|
2018-09-28 06:13:31 -07:00
|
|
|
use bytes::{Bytes, BytesMut};
|
|
|
|
use futures::{sync::mpsc, AsyncSink, StartSend};
|
|
|
|
use rand::{Rand, Rng};
|
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
2018-07-20 19:07:49 -07:00
|
|
|
use std::{
|
2018-07-21 23:51:23 -07:00
|
|
|
collections::BTreeMap,
|
2018-09-27 14:42:47 -07:00
|
|
|
fmt::{self, Debug},
|
|
|
|
marker::PhantomData,
|
2018-09-28 06:13:31 -07:00
|
|
|
net::SocketAddr,
|
|
|
|
ops::Deref,
|
2018-07-20 19:07:49 -07:00
|
|
|
};
|
2018-10-02 10:30:01 -07:00
|
|
|
use tokio::{io, net::TcpStream, prelude::*, codec::{Framed, LengthDelimitedCodec}};
|
2018-07-20 19:07:49 -07:00
|
|
|
use uuid::Uuid;
|
|
|
|
use hbbft::{
|
2018-11-14 05:13:01 -08:00
|
|
|
crypto::{PublicKey, PublicKeySet, SecretKey, Signature},
|
2018-11-13 06:22:55 -08:00
|
|
|
dynamic_honey_badger::{JoinPlan, Message as DhbMessage, DynamicHoneyBadger, Change as DhbChange},
|
2018-09-28 06:13:31 -07:00
|
|
|
sync_key_gen::{Ack, Part},
|
2018-11-08 06:49:02 -08:00
|
|
|
DaStep as MessagingStep,
|
2018-10-12 12:51:35 -07:00
|
|
|
Contribution as HbbftContribution,
|
2018-07-20 19:07:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
pub use blockchain::{Blockchain, MiningError};
|
2018-10-23 11:28:29 -07:00
|
|
|
pub use hydrabadger::{Config, Hydrabadger, HydrabadgerWeak};
|
2018-10-02 22:30:55 -07:00
|
|
|
// TODO: Create a separate, library-wide error type.
|
2018-09-28 06:13:31 -07:00
|
|
|
pub use hydrabadger::Error;
|
2018-10-02 22:30:55 -07:00
|
|
|
pub use hbbft::dynamic_honey_badger::Batch;
|
2018-10-15 15:10:36 -07:00
|
|
|
pub use hydrabadger::StateDsct;
|
2018-07-20 19:07:49 -07:00
|
|
|
|
|
|
|
/// Transmit half of the wire message channel.
|
|
|
|
// TODO: Use a bounded tx/rx (find a sensible upper bound):
|
2018-09-27 14:42:47 -07:00
|
|
|
type WireTx<T> = mpsc::UnboundedSender<WireMessage<T>>;
|
2018-07-20 19:07:49 -07:00
|
|
|
|
|
|
|
/// Receive half of the wire message channel.
|
|
|
|
// TODO: Use a bounded tx/rx (find a sensible upper bound):
|
2018-09-27 14:42:47 -07:00
|
|
|
type WireRx<T> = mpsc::UnboundedReceiver<WireMessage<T>>;
|
2018-07-20 19:07:49 -07:00
|
|
|
|
|
|
|
/// Transmit half of the internal message channel.
|
|
|
|
// TODO: Use a bounded tx/rx (find a sensible upper bound):
|
2018-09-27 14:42:47 -07:00
|
|
|
type InternalTx<T> = mpsc::UnboundedSender<InternalMessage<T>>;
|
2018-07-20 19:07:49 -07:00
|
|
|
|
|
|
|
/// Receive half of the internal message channel.
|
|
|
|
// TODO: Use a bounded tx/rx (find a sensible upper bound):
|
2018-09-27 14:42:47 -07:00
|
|
|
type InternalRx<T> = mpsc::UnboundedReceiver<InternalMessage<T>>;
|
2018-07-20 19:07:49 -07:00
|
|
|
|
2018-10-02 09:47:11 -07:00
|
|
|
/// Transmit half of the batch output channel.
|
|
|
|
// TODO: Use a bounded tx/rx (find a sensible upper bound):
|
2018-10-03 20:14:04 -07:00
|
|
|
type BatchTx<T> = mpsc::UnboundedSender<Batch<T, Uid>>;
|
2018-10-02 09:47:11 -07:00
|
|
|
|
|
|
|
/// Receive half of the batch output channel.
|
|
|
|
// TODO: Use a bounded tx/rx (find a sensible upper bound):
|
2018-10-03 20:14:04 -07:00
|
|
|
pub type BatchRx<T> = mpsc::UnboundedReceiver<Batch<T, Uid>>;
|
2018-10-02 09:47:11 -07:00
|
|
|
|
2018-11-01 06:52:08 -07:00
|
|
|
/// Transmit half of the epoch number output channel.
|
|
|
|
// TODO: Use a bounded tx/rx (find a sensible upper bound):
|
|
|
|
type EpochTx = mpsc::UnboundedSender<u64>;
|
|
|
|
|
|
|
|
/// Receive half of the epoch number output channel.
|
|
|
|
// TODO: Use a bounded tx/rx (find a sensible upper bound):
|
|
|
|
pub type EpochRx = mpsc::UnboundedReceiver<u64>;
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-28 06:13:31 -07:00
|
|
|
pub trait Contribution:
|
|
|
|
HbbftContribution + Clone + Debug + Serialize + DeserializeOwned + 'static
|
|
|
|
{
|
|
|
|
}
|
|
|
|
impl<C> Contribution for C where
|
|
|
|
C: HbbftContribution + Clone + Debug + Serialize + DeserializeOwned + 'static
|
|
|
|
{}
|
2018-07-20 19:07:49 -07:00
|
|
|
|
|
|
|
/// A unique identifier.
|
|
|
|
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
|
|
|
|
pub struct Uid(pub(crate) Uuid);
|
|
|
|
|
|
|
|
impl Uid {
|
|
|
|
/// Returns a new, random `Uid`.
|
|
|
|
pub fn new() -> Uid {
|
|
|
|
Uid(Uuid::new_v4())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Rand for Uid {
|
|
|
|
fn rand<R: Rng>(_rng: &mut R) -> Uid {
|
|
|
|
Uid::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Uid {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::LowerHex::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Uid {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::LowerHex::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message = DhbMessage<Uid>;
|
2018-10-03 20:14:04 -07:00
|
|
|
type Step<T> = MessagingStep<DynamicHoneyBadger<T, Uid>>;
|
2018-11-13 06:22:55 -08:00
|
|
|
type Change = DhbChange<Uid>;
|
2018-07-20 19:07:49 -07:00
|
|
|
|
|
|
|
/// A peer's incoming (listening) address.
|
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
|
|
|
|
pub struct InAddr(pub SocketAddr);
|
|
|
|
|
|
|
|
impl Deref for InAddr {
|
|
|
|
type Target = SocketAddr;
|
|
|
|
fn deref(&self) -> &SocketAddr {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for InAddr {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "InAddr({})", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An internal address used to respond to a connected peer.
|
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
|
|
|
|
pub struct OutAddr(pub SocketAddr);
|
|
|
|
|
|
|
|
impl Deref for OutAddr {
|
|
|
|
type Target = SocketAddr;
|
|
|
|
fn deref(&self) -> &SocketAddr {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for OutAddr {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "OutAddr({})", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Nodes of the network.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct NetworkNodeInfo {
|
|
|
|
pub(crate) uid: Uid,
|
|
|
|
pub(crate) in_addr: InAddr,
|
|
|
|
pub(crate) pk: PublicKey,
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:21:33 -08:00
|
|
|
type ActiveNetworkInfo = (Vec<NetworkNodeInfo>, PublicKeySet, BTreeMap<Uid, PublicKey>);
|
|
|
|
|
2018-07-20 19:07:49 -07:00
|
|
|
/// The current state of the network.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum NetworkState {
|
|
|
|
None,
|
|
|
|
Unknown(Vec<NetworkNodeInfo>),
|
2018-07-23 16:01:27 -07:00
|
|
|
AwaitingMorePeersForKeyGeneration(Vec<NetworkNodeInfo>),
|
|
|
|
GeneratingKeys(Vec<NetworkNodeInfo>, BTreeMap<Uid, PublicKey>),
|
2018-11-15 15:21:33 -08:00
|
|
|
Active(ActiveNetworkInfo),
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Messages sent over the network between nodes.
|
2018-11-14 15:42:06 -08:00
|
|
|
///
|
2018-11-19 12:10:47 -08:00
|
|
|
/// [`Message`](enum.WireMessageKind.html#variant.Message) variants are among
|
|
|
|
/// those verified.
|
2018-07-20 19:07:49 -07:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2018-09-27 14:42:47 -07:00
|
|
|
pub enum WireMessageKind<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
HelloFromValidator(Uid, InAddr, PublicKey, NetworkState),
|
|
|
|
HelloRequestChangeAdd(Uid, InAddr, PublicKey),
|
|
|
|
WelcomeReceivedChangeAdd(Uid, PublicKey, NetworkState),
|
|
|
|
RequestNetworkState,
|
|
|
|
NetworkState(NetworkState),
|
|
|
|
Goodbye,
|
|
|
|
#[serde(with = "serde_bytes")]
|
2018-11-14 15:42:06 -08:00
|
|
|
// TODO(c0gent): Remove.
|
2018-07-20 19:07:49 -07:00
|
|
|
Bytes(Bytes),
|
2018-11-14 15:42:06 -08:00
|
|
|
/// A Honey Badger message.
|
|
|
|
///
|
|
|
|
/// All received messages are verified against the senders public key
|
|
|
|
/// using an attached signature.
|
2018-07-23 12:01:13 -07:00
|
|
|
Message(Uid, Message),
|
2018-11-14 15:42:06 -08:00
|
|
|
// TODO(c0gent): Remove.
|
2018-10-03 20:14:04 -07:00
|
|
|
Transaction(Uid, T),
|
2018-07-20 19:07:49 -07:00
|
|
|
KeyGenPart(Part),
|
2018-07-23 14:12:18 -07:00
|
|
|
KeyGenAck(Ack),
|
2018-11-14 15:42:06 -08:00
|
|
|
JoinPlan(JoinPlan<Uid>),
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Messages sent over the network between nodes.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2018-09-27 14:42:47 -07:00
|
|
|
pub struct WireMessage<T> {
|
|
|
|
kind: WireMessageKind<T>,
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
impl<T: Contribution> WireMessage<T> {
|
2018-09-28 06:13:31 -07:00
|
|
|
pub fn hello_from_validator(
|
|
|
|
src_uid: Uid,
|
|
|
|
in_addr: InAddr,
|
|
|
|
pk: PublicKey,
|
|
|
|
net_state: NetworkState,
|
|
|
|
) -> WireMessage<T> {
|
2018-07-23 12:01:13 -07:00
|
|
|
WireMessageKind::HelloFromValidator(src_uid, in_addr, pk, net_state).into()
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a `HelloRequestChangeAdd` variant.
|
2018-09-28 06:13:31 -07:00
|
|
|
pub fn hello_request_change_add(
|
|
|
|
src_uid: Uid,
|
|
|
|
in_addr: InAddr,
|
|
|
|
pk: PublicKey,
|
|
|
|
) -> WireMessage<T> {
|
2018-11-19 12:10:47 -08:00
|
|
|
WireMessageKind::HelloRequestChangeAdd(src_uid, in_addr, pk).into()
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a `WelcomeReceivedChangeAdd` variant.
|
2018-09-28 06:13:31 -07:00
|
|
|
pub fn welcome_received_change_add(
|
|
|
|
src_uid: Uid,
|
|
|
|
pk: PublicKey,
|
|
|
|
net_state: NetworkState,
|
|
|
|
) -> WireMessage<T> {
|
2018-11-19 12:10:47 -08:00
|
|
|
WireMessageKind::WelcomeReceivedChangeAdd(src_uid, pk, net_state).into()
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
2018-07-23 12:34:08 -07:00
|
|
|
/// Returns an `Input` variant.
|
2018-10-03 20:14:04 -07:00
|
|
|
pub fn transaction(src_uid: Uid, txn: T) -> WireMessage<T> {
|
2018-11-19 12:10:47 -08:00
|
|
|
WireMessageKind::Transaction(src_uid, txn).into()
|
2018-07-23 12:34:08 -07:00
|
|
|
}
|
|
|
|
|
2018-07-21 23:51:23 -07:00
|
|
|
/// Returns a `Message` variant.
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn message(src_uid: Uid, msg: Message) -> WireMessage<T> {
|
2018-11-19 12:10:47 -08:00
|
|
|
WireMessageKind::Message(src_uid, msg).into()
|
2018-07-21 23:51:23 -07:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn key_gen_part(part: Part) -> WireMessage<T> {
|
2018-11-19 12:10:47 -08:00
|
|
|
WireMessageKind::KeyGenPart(part).into()
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn key_gen_part_ack(outcome: Ack) -> WireMessage<T> {
|
2018-07-23 14:12:18 -07:00
|
|
|
WireMessageKind::KeyGenAck(outcome).into()
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn join_plan(jp: JoinPlan<Uid>) -> WireMessage<T> {
|
2018-07-21 23:51:23 -07:00
|
|
|
WireMessageKind::JoinPlan(jp).into()
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the wire message kind.
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn kind(&self) -> &WireMessageKind<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
&self.kind
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consumes this `WireMessage` into its kind.
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn into_kind(self) -> WireMessageKind<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
self.kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
impl<T: Contribution> From<WireMessageKind<T>> for WireMessage<T> {
|
|
|
|
fn from(kind: WireMessageKind<T>) -> WireMessage<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
WireMessage { kind }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 05:13:01 -08:00
|
|
|
/// A serialized `WireMessage` signed by the sender.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct SignedWireMessage {
|
|
|
|
message: Vec<u8>,
|
|
|
|
sig: Signature,
|
|
|
|
}
|
|
|
|
|
2018-07-20 19:07:49 -07:00
|
|
|
/// A stream/sink of `WireMessage`s connected to a socket.
|
2018-11-14 15:42:06 -08:00
|
|
|
pub struct WireMessages<T: Contribution> {
|
2018-10-02 10:30:01 -07:00
|
|
|
framed: Framed<TcpStream, LengthDelimitedCodec>,
|
2018-11-19 12:10:47 -08:00
|
|
|
local_sk: SecretKey,
|
|
|
|
peer_pk: Option<PublicKey>,
|
2018-09-27 14:42:47 -07:00
|
|
|
_t: PhantomData<T>,
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
impl<T: Contribution> WireMessages<T> {
|
2018-11-19 12:10:47 -08:00
|
|
|
pub fn new(socket: TcpStream, local_sk: SecretKey) -> WireMessages<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
WireMessages {
|
2018-10-02 10:30:01 -07:00
|
|
|
framed: Framed::new(socket, LengthDelimitedCodec::new()),
|
2018-11-19 12:10:47 -08:00
|
|
|
local_sk,
|
|
|
|
peer_pk: None,
|
2018-09-27 14:42:47 -07:00
|
|
|
_t: PhantomData,
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 12:10:47 -08:00
|
|
|
pub fn set_peer_public_key(&mut self, peer_pk: PublicKey) {
|
|
|
|
assert!(self.peer_pk.map(|pk| pk == peer_pk).unwrap_or(true));
|
|
|
|
self.peer_pk = Some(peer_pk);
|
|
|
|
}
|
|
|
|
|
2018-07-20 19:07:49 -07:00
|
|
|
pub fn socket(&self) -> &TcpStream {
|
|
|
|
self.framed.get_ref()
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn send_msg(&mut self, msg: WireMessage<T>) -> Result<(), Error> {
|
2018-07-20 19:07:49 -07:00
|
|
|
self.start_send(msg)?;
|
|
|
|
let _ = self.poll_complete()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
impl<T: Contribution> Stream for WireMessages<T> {
|
|
|
|
type Item = WireMessage<T>;
|
2018-07-20 19:07:49 -07:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
match try_ready!(self.framed.poll()) {
|
|
|
|
Some(frame) => {
|
2018-11-14 05:13:01 -08:00
|
|
|
let s_msg: SignedWireMessage =
|
|
|
|
bincode::deserialize(&frame.freeze()).map_err(Error::Serde)?;
|
|
|
|
let msg: WireMessage<T> =
|
|
|
|
bincode::deserialize(&s_msg.message).map_err(Error::Serde)?;
|
2018-11-14 15:42:06 -08:00
|
|
|
|
2018-11-19 12:10:47 -08:00
|
|
|
// Verify signature for certain variants.
|
|
|
|
match msg.kind {
|
|
|
|
| WireMessageKind::Message(..)
|
|
|
|
| WireMessageKind::KeyGenAck(..)
|
|
|
|
| WireMessageKind::KeyGenPart(..) => {
|
|
|
|
let peer_pk = self.peer_pk.ok_or(Error::VerificationMessageReceivedUnknownPeer)?;
|
|
|
|
if !peer_pk.verify(&s_msg.sig, &s_msg.message) {
|
|
|
|
return Err(Error::InvalidSignature);
|
|
|
|
}
|
2018-11-14 05:13:01 -08:00
|
|
|
}
|
2018-11-19 12:10:47 -08:00
|
|
|
_ => {}
|
2018-11-14 05:13:01 -08:00
|
|
|
}
|
2018-11-14 15:42:06 -08:00
|
|
|
|
2018-11-14 05:13:01 -08:00
|
|
|
Ok(Async::Ready(Some(msg)))
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
2018-09-28 06:13:31 -07:00
|
|
|
None => Ok(Async::Ready(None)),
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
impl<T: Contribution> Sink for WireMessages<T> {
|
|
|
|
type SinkItem = WireMessage<T>;
|
2018-07-20 19:07:49 -07:00
|
|
|
type SinkError = Error;
|
|
|
|
|
|
|
|
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
|
|
|
|
// TODO: Reuse buffer:
|
|
|
|
let mut serialized = BytesMut::new();
|
2018-08-03 06:35:15 -07:00
|
|
|
|
2018-11-14 05:13:01 -08:00
|
|
|
let message = bincode::serialize(&item).map_err(Error::Serde)?;
|
2018-11-19 12:10:47 -08:00
|
|
|
let sig = self.local_sk.sign(&message);
|
2018-11-14 05:13:01 -08:00
|
|
|
|
|
|
|
match bincode::serialize(&SignedWireMessage { message, sig }) {
|
2018-07-20 19:07:49 -07:00
|
|
|
Ok(s) => serialized.extend_from_slice(&s),
|
|
|
|
Err(err) => return Err(Error::Io(io::Error::new(io::ErrorKind::Other, err))),
|
|
|
|
}
|
2018-10-02 10:30:01 -07:00
|
|
|
match self.framed.start_send(serialized.freeze()) {
|
2018-07-20 19:07:49 -07:00
|
|
|
Ok(async_sink) => match async_sink {
|
|
|
|
AsyncSink::Ready => Ok(AsyncSink::Ready),
|
|
|
|
AsyncSink::NotReady(_) => Ok(AsyncSink::NotReady(item)),
|
|
|
|
},
|
2018-09-28 06:13:31 -07:00
|
|
|
Err(err) => Err(Error::Io(err)),
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
|
|
|
self.framed.poll_complete().map_err(Error::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(&mut self) -> Poll<(), Self::SinkError> {
|
|
|
|
self.framed.close().map_err(Error::from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A message between internal threads/tasks.
|
|
|
|
#[derive(Clone, Debug)]
|
2018-09-27 14:42:47 -07:00
|
|
|
pub enum InternalMessageKind<T: Contribution> {
|
|
|
|
Wire(WireMessage<T>),
|
2018-07-20 19:07:49 -07:00
|
|
|
HbMessage(Message),
|
2018-11-13 06:22:55 -08:00
|
|
|
HbContribution(T),
|
|
|
|
HbChange(Change),
|
2018-07-20 19:07:49 -07:00
|
|
|
PeerDisconnect,
|
2018-07-21 23:51:23 -07:00
|
|
|
NewIncomingConnection(InAddr, PublicKey, bool),
|
2018-07-20 19:07:49 -07:00
|
|
|
NewOutgoingConnection,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A message between internal threads/tasks.
|
|
|
|
#[derive(Clone, Debug)]
|
2018-09-27 14:42:47 -07:00
|
|
|
pub struct InternalMessage<T: Contribution> {
|
2018-07-20 19:07:49 -07:00
|
|
|
src_uid: Option<Uid>,
|
|
|
|
src_addr: OutAddr,
|
2018-09-27 14:42:47 -07:00
|
|
|
kind: InternalMessageKind<T>,
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
impl<T: Contribution> InternalMessage<T> {
|
2018-09-28 06:13:31 -07:00
|
|
|
pub fn new(
|
|
|
|
src_uid: Option<Uid>,
|
|
|
|
src_addr: OutAddr,
|
|
|
|
kind: InternalMessageKind<T>,
|
|
|
|
) -> InternalMessage<T> {
|
|
|
|
InternalMessage {
|
|
|
|
src_uid,
|
|
|
|
src_addr,
|
|
|
|
kind,
|
|
|
|
}
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a new `InternalMessage` without a uid.
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn new_without_uid(src_addr: OutAddr, kind: InternalMessageKind<T>) -> InternalMessage<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
InternalMessage::new(None, src_addr, kind)
|
|
|
|
}
|
|
|
|
|
2018-09-28 06:13:31 -07:00
|
|
|
pub fn wire(
|
|
|
|
src_uid: Option<Uid>,
|
|
|
|
src_addr: OutAddr,
|
|
|
|
wire_message: WireMessage<T>,
|
|
|
|
) -> InternalMessage<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
InternalMessage::new(src_uid, src_addr, InternalMessageKind::Wire(wire_message))
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn hb_message(src_uid: Uid, src_addr: OutAddr, msg: Message) -> InternalMessage<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
InternalMessage::new(Some(src_uid), src_addr, InternalMessageKind::HbMessage(msg))
|
|
|
|
}
|
|
|
|
|
2018-11-13 06:22:55 -08:00
|
|
|
pub fn hb_contribution(src_uid: Uid, src_addr: OutAddr, contrib: T) -> InternalMessage<T> {
|
|
|
|
InternalMessage::new(Some(src_uid), src_addr, InternalMessageKind::HbContribution(contrib))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hb_vote(src_uid: Uid, src_addr: OutAddr, change: Change) -> InternalMessage<T> {
|
|
|
|
InternalMessage::new(Some(src_uid), src_addr, InternalMessageKind::HbChange(change))
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn peer_disconnect(src_uid: Uid, src_addr: OutAddr) -> InternalMessage<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
InternalMessage::new(Some(src_uid), src_addr, InternalMessageKind::PeerDisconnect)
|
|
|
|
}
|
|
|
|
|
2018-09-28 06:13:31 -07:00
|
|
|
pub fn new_incoming_connection(
|
|
|
|
src_uid: Uid,
|
|
|
|
src_addr: OutAddr,
|
|
|
|
src_in_addr: InAddr,
|
|
|
|
src_pk: PublicKey,
|
|
|
|
request_change_add: bool,
|
|
|
|
) -> InternalMessage<T> {
|
|
|
|
InternalMessage::new(
|
|
|
|
Some(src_uid),
|
|
|
|
src_addr,
|
|
|
|
InternalMessageKind::NewIncomingConnection(src_in_addr, src_pk, request_change_add),
|
|
|
|
)
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn new_outgoing_connection(src_addr: OutAddr) -> InternalMessage<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
InternalMessage::new_without_uid(src_addr, InternalMessageKind::NewOutgoingConnection)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the source unique identifier this message was received in.
|
|
|
|
pub fn src_uid(&self) -> Option<&Uid> {
|
|
|
|
self.src_uid.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the source socket this message was received on.
|
|
|
|
pub fn src_addr(&self) -> &OutAddr {
|
|
|
|
&self.src_addr
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the internal message kind.
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn kind(&self) -> &InternalMessageKind<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
&self.kind
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consumes this `InternalMessage` into its parts.
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn into_parts(self) -> (Option<Uid>, OutAddr, InternalMessageKind<T>) {
|
2018-07-20 19:07:49 -07:00
|
|
|
(self.src_uid, self.src_addr, self.kind)
|
|
|
|
}
|
|
|
|
}
|