hydrabadger/src/lib.rs

450 lines
13 KiB
Rust
Raw Normal View History

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;
// #[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;
extern crate tokio_io;
extern crate rand;
2018-07-02 19:36:12 -07:00
extern crate bytes;
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;
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;
extern crate hbbft;
2018-06-30 12:00:23 -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
// 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::{
collections::BTreeMap,
fmt::{self, Debug},
2018-07-20 19:07:49 -07:00
net::{SocketAddr},
ops::Deref,
marker::PhantomData,
2018-07-20 19:07:49 -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::{
traits::Contribution as HbbftContribution,
crypto::{PublicKey, PublicKeySet},
2018-07-20 19:07:49 -07:00
sync_key_gen::{Part, Ack},
messaging::Step as MessagingStep,
dynamic_honey_badger::{Message as DhbMessage, JoinPlan},
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):
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):
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):
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):
type InternalRx<T> = mpsc::UnboundedReceiver<InternalMessage<T>>;
2018-07-20 19:07:49 -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>;
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>),
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)]
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),
Transactions(Uid, Vec<T>),
2018-07-20 19:07:49 -07:00
KeyGenPart(Part),
KeyGenAck(Ack),
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)]
pub struct WireMessage<T> {
kind: WireMessageKind<T>,
2018-07-20 19:07:49 -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,
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.
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)
-> 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.
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), }
}
/// Returns a `Message` variant.
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), }
}
pub fn key_gen_part(part: Part) -> WireMessage<T> {
2018-07-20 19:07:49 -07:00
WireMessage { kind: WireMessageKind::KeyGenPart(part) }
}
pub fn key_gen_part_ack(outcome: Ack) -> WireMessage<T> {
WireMessageKind::KeyGenAck(outcome).into()
2018-07-20 19:07:49 -07:00
}
pub fn join_plan(jp: JoinPlan<Uid>) -> WireMessage<T> {
WireMessageKind::JoinPlan(jp).into()
2018-07-20 19:07:49 -07:00
}
/// Returns the wire message kind.
pub fn kind(&self) -> &WireMessageKind<T> {
2018-07-20 19:07:49 -07:00
&self.kind
}
/// Consumes this `WireMessage` into its kind.
pub fn into_kind(self) -> WireMessageKind<T> {
2018-07-20 19:07:49 -07:00
self.kind
}
}
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)]
pub struct WireMessages<T> {
2018-07-20 19:07:49 -07:00
framed: Framed<TcpStream>,
_t: PhantomData<T>,
2018-07-20 19:07:49 -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),
_t: PhantomData,
2018-07-20 19:07:49 -07:00
}
}
pub fn socket(&self) -> &TcpStream {
self.framed.get_ref()
}
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(())
}
}
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))
}
}
}
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)]
pub enum InternalMessageKind<T: Contribution> {
Wire(WireMessage<T>),
2018-07-20 19:07:49 -07:00
HbMessage(Message),
HbInput(Input<T>),
2018-07-20 19:07:49 -07:00
PeerDisconnect,
NewIncomingConnection(InAddr, PublicKey, bool),
2018-07-20 19:07:49 -07:00
NewOutgoingConnection,
}
/// A message between internal threads/tasks.
#[derive(Clone, Debug)]
pub struct InternalMessage<T: Contribution> {
2018-07-20 19:07:49 -07:00
src_uid: Option<Uid>,
src_addr: OutAddr,
kind: InternalMessageKind<T>,
2018-07-20 19:07:49 -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.
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)
}
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))
}
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))
}
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))
}
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,
src_pk: PublicKey, request_change_add: bool) -> InternalMessage<T> {
2018-07-20 19:07:49 -07:00
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
}
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.
pub fn kind(&self) -> &InternalMessageKind<T> {
2018-07-20 19:07:49 -07:00
&self.kind
}
/// Consumes this `InternalMessage` into its parts.
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)
}
}