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-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 crypto;
|
|
|
|
extern crate chrono;
|
|
|
|
extern crate num_traits;
|
|
|
|
extern crate num_bigint;
|
2018-08-04 18:54:30 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate futures;
|
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 rand;
|
2018-07-02 19:36:12 -07:00
|
|
|
extern crate bytes;
|
2018-07-08 15:08:37 -07:00
|
|
|
extern crate uuid;
|
|
|
|
extern crate byteorder;
|
2018-08-04 18:54:30 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
2018-07-08 16:23:10 -07:00
|
|
|
extern crate serde;
|
|
|
|
extern crate serde_bytes;
|
2018-07-08 15:08:37 -07:00
|
|
|
extern crate bincode;
|
|
|
|
extern crate tokio_serde_bincode;
|
2018-07-12 15:32:39 -07:00
|
|
|
extern crate parking_lot;
|
|
|
|
extern crate clear_on_drop;
|
2018-07-08 15:08:37 -07:00
|
|
|
extern crate hbbft;
|
2018-06-30 12:00:23 -07:00
|
|
|
|
2018-08-04 20:03:09 -07:00
|
|
|
|
|
|
|
// Config {
|
|
|
|
// batch_size: DEFAULT_BATCH_SIZE,
|
|
|
|
// txn_gen_count: DEFAULT_TXN_GEN_COUNT,
|
|
|
|
// txn_gen_interval: DEFAULT_TXN_GEN_INTERVAL,
|
|
|
|
// txn_bytes: DEFAULT_TXN_BYTES,
|
|
|
|
// keygen_peer_count: DEFAULT_KEYGEN_PEER_COUNT,
|
|
|
|
// output_extra_delay_ms: DEFAULT_OUTPUT_EXTRA_DELAY_MS,
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
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 hydrabadger;
|
|
|
|
pub mod blockchain;
|
2018-07-17 14:06:23 -07:00
|
|
|
pub mod peer;
|
2018-07-02 19:36:12 -07:00
|
|
|
|
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},
|
2018-07-20 19:07:49 -07:00
|
|
|
net::{SocketAddr},
|
|
|
|
ops::Deref,
|
2018-09-27 14:42:47 -07:00
|
|
|
marker::PhantomData,
|
2018-07-20 19:07:49 -07:00
|
|
|
};
|
2018-09-27 14:42:47 -07:00
|
|
|
use serde::{Serialize, de::DeserializeOwned};
|
2018-07-20 19:07:49 -07:00
|
|
|
use futures::{
|
|
|
|
StartSend, AsyncSink,
|
|
|
|
sync::mpsc,
|
|
|
|
};
|
|
|
|
use tokio::{
|
|
|
|
io,
|
|
|
|
net::{TcpStream},
|
|
|
|
prelude::*,
|
|
|
|
};
|
|
|
|
use tokio_io::codec::length_delimited::Framed;
|
|
|
|
use bytes::{BytesMut, Bytes};
|
|
|
|
use rand::{Rng, Rand};
|
|
|
|
use uuid::Uuid;
|
2018-08-03 06:35:15 -07:00
|
|
|
// use bincode::{serialize, deserialize};
|
2018-07-20 19:07:49 -07:00
|
|
|
use hbbft::{
|
2018-09-27 14:42:47 -07:00
|
|
|
traits::Contribution as HbbftContribution,
|
2018-07-21 23:51:23 -07:00
|
|
|
crypto::{PublicKey, PublicKeySet},
|
2018-07-20 19:07:49 -07:00
|
|
|
sync_key_gen::{Part, Ack},
|
2018-07-23 10:35:34 -07:00
|
|
|
messaging::Step as MessagingStep,
|
2018-07-21 23:51:23 -07:00
|
|
|
dynamic_honey_badger::{Message as DhbMessage, JoinPlan},
|
2018-07-23 10:35:34 -07:00
|
|
|
queueing_honey_badger::{QueueingHoneyBadger, Input as QhbInput},
|
2018-07-20 19:07:49 -07:00
|
|
|
};
|
|
|
|
|
2018-07-23 09:04:10 -07:00
|
|
|
pub use hydrabadger::{Hydrabadger, Config};
|
2018-07-20 19:07:49 -07:00
|
|
|
pub use blockchain::{Blockchain, MiningError};
|
|
|
|
|
|
|
|
// FIME: TEMPORARY -- Create another error type.
|
|
|
|
pub use hydrabadger::{Error};
|
|
|
|
|
|
|
|
|
|
|
|
/// 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-09-27 14:42:47 -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-09-27 14:42:47 -07:00
|
|
|
type Step<T> = MessagingStep<QueueingHoneyBadger<Vec<T>, Uid>>;
|
|
|
|
type Input<T> = QhbInput<Vec<T>, 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,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 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-07-21 23:51:23 -07:00
|
|
|
Active((Vec<NetworkNodeInfo>, PublicKeySet, BTreeMap<Uid, PublicKey>)),
|
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 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")]
|
|
|
|
Bytes(Bytes),
|
2018-07-23 12:01:13 -07:00
|
|
|
Message(Uid, Message),
|
2018-09-27 14:42:47 -07:00
|
|
|
Transactions(Uid, Vec<T>),
|
2018-07-20 19:07:49 -07:00
|
|
|
KeyGenPart(Part),
|
2018-07-23 14:12:18 -07:00
|
|
|
KeyGenAck(Ack),
|
2018-07-21 23:51:23 -07:00
|
|
|
JoinPlan(JoinPlan<Uid>)
|
2018-07-20 19:07:49 -07:00
|
|
|
// TargetedMessage(TargetedMessage<Uid>),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 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-07-23 12:01:13 -07:00
|
|
|
pub fn hello_from_validator(src_uid: Uid, in_addr: InAddr, pk: PublicKey,
|
2018-09-27 14:42:47 -07:00
|
|
|
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-27 14:42:47 -07:00
|
|
|
pub fn hello_request_change_add(src_uid: Uid, in_addr: InAddr, pk: PublicKey) -> WireMessage<T> {
|
2018-07-23 12:01:13 -07:00
|
|
|
WireMessage { kind: WireMessageKind::HelloRequestChangeAdd(src_uid, in_addr, pk), }
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a `WelcomeReceivedChangeAdd` variant.
|
2018-07-23 12:01:13 -07:00
|
|
|
pub fn welcome_received_change_add(src_uid: Uid, pk: PublicKey, net_state: NetworkState)
|
2018-09-27 14:42:47 -07:00
|
|
|
-> WireMessage<T> {
|
2018-07-23 12:01:13 -07:00
|
|
|
WireMessage { kind: WireMessageKind::WelcomeReceivedChangeAdd(src_uid, pk, net_state) }
|
2018-07-20 19:07:49 -07:00
|
|
|
}
|
|
|
|
|
2018-07-23 12:34:08 -07:00
|
|
|
/// Returns an `Input` variant.
|
2018-09-27 14:42:47 -07:00
|
|
|
pub fn transaction(src_uid: Uid, txns: Vec<T>) -> WireMessage<T> {
|
2018-07-23 12:34:08 -07:00
|
|
|
WireMessage { kind: WireMessageKind::Transactions(src_uid, txns), }
|
|
|
|
}
|
|
|
|
|
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-07-23 12:01:13 -07:00
|
|
|
WireMessage { kind: WireMessageKind::Message(src_uid, msg), }
|
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-07-20 19:07:49 -07:00
|
|
|
WireMessage { kind: WireMessageKind::KeyGenPart(part) }
|
|
|
|
}
|
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A stream/sink of `WireMessage`s connected to a socket.
|
|
|
|
#[derive(Debug)]
|
2018-09-27 14:42:47 -07:00
|
|
|
pub struct WireMessages<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
framed: Framed<TcpStream>,
|
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> {
|
|
|
|
pub fn new(socket: TcpStream) -> WireMessages<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
WireMessages {
|
|
|
|
framed: Framed::new(socket),
|
2018-09-27 14:42:47 -07:00
|
|
|
_t: PhantomData,
|
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) => {
|
|
|
|
Ok(Async::Ready(Some(
|
|
|
|
// deserialize_from(frame.reader()).map_err(Error::Serde)?
|
2018-08-03 06:35:15 -07:00
|
|
|
bincode::deserialize(&frame.freeze()).map_err(Error::Serde)?
|
2018-07-20 19:07:49 -07:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
None => Ok(Async::Ready(None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// Downgraded from bincode 1.0:
|
|
|
|
//
|
|
|
|
// Original: `bincode::serialize(&item)`
|
|
|
|
//
|
|
|
|
match bincode::serialize(&item, bincode::Bounded(1 << 20)) {
|
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))),
|
|
|
|
}
|
|
|
|
match self.framed.start_send(serialized) {
|
|
|
|
Ok(async_sink) => match async_sink {
|
|
|
|
AsyncSink::Ready => Ok(AsyncSink::Ready),
|
|
|
|
AsyncSink::NotReady(_) => Ok(AsyncSink::NotReady(item)),
|
|
|
|
},
|
|
|
|
Err(err) => Err(Error::Io(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-27 14:42:47 -07:00
|
|
|
HbInput(Input<T>),
|
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> {
|
|
|
|
pub fn new(src_uid: Option<Uid>, src_addr: OutAddr, kind: InternalMessageKind<T>)
|
|
|
|
-> InternalMessage<T> {
|
2018-09-10 08:21:58 -07:00
|
|
|
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-27 14:42:47 -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-09-27 14:42:47 -07:00
|
|
|
pub fn hb_input(src_uid: Uid, src_addr: OutAddr, input: Input<T>) -> InternalMessage<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
InternalMessage::new(Some(src_uid), src_addr, InternalMessageKind::HbInput(input))
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_incoming_connection(src_uid: Uid, src_addr: OutAddr, src_in_addr: InAddr,
|
2018-09-27 14:42:47 -07:00
|
|
|
src_pk: PublicKey, request_change_add: bool) -> InternalMessage<T> {
|
2018-07-20 19:07:49 -07:00
|
|
|
InternalMessage::new(Some(src_uid), src_addr,
|
2018-07-21 23:51:23 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|