Update rand & hbbft.

This commit is contained in:
c0gent 2019-01-22 09:50:16 -08:00
parent 161b31f583
commit 94546b0e2c
No known key found for this signature in database
GPG Key ID: 9CC25E71A743E892
7 changed files with 27 additions and 34 deletions

View File

@ -22,7 +22,6 @@ no-simd = ["hbbft/no-simd"]
[dependencies]
log = "0.4"
# env_logger = "*"
env_logger = "0.5"
clap = "2"
failure = "0.1"
@ -35,7 +34,7 @@ num-bigint = "*"
colored = "*"
itertools = "*"
pairing = "*"
rand = "0.4.2"
rand = "0.6"
serde = "1"
serde_bytes = "~0.10.4"
serde_derive = "1"
@ -58,8 +57,7 @@ version = "*"
# git = "https://github.com/c0gent/hbbft"
git = "https://github.com/poanetwork/hbbft"
# branch = "master"
rev = "ceb416a6"
# rev = "d4a7b19adb7021efcefdf735f6dd2c45cce0a1d2"
rev = "d4a7b19"
# path = "../hbbft"
[profile.release]

View File

@ -11,7 +11,7 @@ extern crate serde_derive;
use chrono::Local;
use clap::{App, Arg, ArgMatches};
use hydrabadger::{Blockchain, Config, Hydrabadger, MiningError, Uid};
use rand::Rng;
use rand::{Rng, distributions::Standard};
use std::collections::HashSet;
use std::env;
use std::io::Write;
@ -97,7 +97,7 @@ pub struct Transaction(pub Vec<u8>);
impl Transaction {
fn random(len: usize) -> Transaction {
Transaction(rand::thread_rng().gen_iter().take(len).collect())
Transaction(rand::thread_rng().sample_iter(&Standard).take(len).collect())
}
}

View File

@ -14,7 +14,7 @@ use futures::{
};
use hbbft::crypto::{PublicKey, SecretKey};
use parking_lot::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
use rand::{self, Rand};
use rand::{self, Rng};
use std::{
collections::HashSet,
net::SocketAddr,
@ -81,7 +81,6 @@ impl Default for Config {
/// Shared all over the place.
struct Inner<C: Contribution, N: NodeId> {
/// Node nid:
// nid: Uid,
nid: N,
/// Incoming connection socket.
addr: InAddr,
@ -123,8 +122,7 @@ pub struct Hydrabadger<C: Contribution, N: NodeId> {
impl<C: Contribution, N: NodeId + DeserializeOwned + 'static> Hydrabadger<C, N> {
/// Returns a new Hydrabadger node.
pub fn new(addr: SocketAddr, cfg: Config, nid: N) -> Self {
// let nid = Uid::new();
let secret_key = SecretKey::rand(&mut rand::OsRng::new().expect("Unable to create rng"));
let secret_key: SecretKey = rand::rngs::OsRng::new().expect("Unable to create rng").gen();
let (peer_internal_tx, peer_internal_rx) = mpsc::unbounded();
let (batch_tx, batch_rx) = mpsc::unbounded();

View File

@ -10,7 +10,7 @@ use hbbft::{
crypto::{PublicKey, SecretKey},
sync_key_gen::{Ack, AckOutcome, Part, PartOutcome, SyncKeyGen},
};
use rand;
use rand::{self, FromEntropy};
use std::collections::BTreeMap;
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
@ -184,21 +184,21 @@ impl<N: NodeId> Machine<N> {
let pk = local_sk.public_key();
public_keys.insert(local_nid.clone(), pk);
let mut rng = rand::OsRng::new().expect("Creating OS Rng has failed");
let mut rng = rand::rngs::StdRng::from_entropy();
let (mut sync_key_gen, opt_part) = SyncKeyGen::new(
&mut rng,
local_nid.clone(),
local_sk,
public_keys.clone(),
threshold,
&mut rng,
)
.map_err(Error::SyncKeyGenNew)?;
part = opt_part.expect("This node is not a validator (somehow)!");
trace!("KEY GENERATION: Handling our own `Part`...");
ack = match sync_key_gen
.handle_part(&mut rng, &local_nid, part.clone())
.handle_part(&local_nid, part.clone(), &mut rng)
.expect("Handling our own Part has failed")
{
PartOutcome::Valid(Some(ack)) => ack,
@ -294,9 +294,9 @@ impl<N: NodeId> Machine<N> {
} => {
// TODO: Move this match block into a function somewhere for re-use:
trace!("KEY GENERATION: Handling part from '{:?}'...", src_nid);
let mut rng = rand::OsRng::new().expect("Creating OS Rng has failed");
let mut rng = rand::rngs::OsRng::new().expect("Creating OS Rng has failed");
let skg = sync_key_gen.as_mut().unwrap();
let ack = match skg.handle_part(&mut rng, src_nid, part) {
let ack = match skg.handle_part(src_nid, part, &mut rng) {
Ok(PartOutcome::Valid(Some(ack))) => ack,
Ok(PartOutcome::Invalid(faults)) => panic!(
"Invalid part \

View File

@ -3,7 +3,6 @@ mod hydrabadger;
pub mod key_gen;
mod state;
use rand::Rand;
use self::handler::Handler;
use self::state::{State, StateMachine};
use crate::{Change, Message};
@ -19,7 +18,7 @@ pub const WIRE_MESSAGE_RETRY_MAX: usize = 10;
/// A HoneyBadger input or message.
#[derive(Clone, Debug)]
pub enum InputOrMessage<T, N: Ord + Rand> {
pub enum InputOrMessage<T, N: Ord> {
Change(Change<N>),
Contribution(T),
Message(N, Message<N>),

View File

@ -15,7 +15,7 @@ use hbbft::{
sync_key_gen::Ack,
NetworkInfo,
};
use rand::StdRng;
use rand::{rngs::StdRng, FromEntropy};
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
@ -209,7 +209,7 @@ impl<C: Contribution, N: NodeId> StateMachine<C, N> {
ref mut iom_queue, ..
} => {
let (dhb, dhb_step) =
DynamicHoneyBadger::new_joining(local_nid, local_sk, jp, StdRng::new()?)?;
DynamicHoneyBadger::new_joining(local_nid, local_sk, jp, &mut StdRng::from_entropy())?;
step_queue.push(dhb_step);
iom_queue_ret = iom_queue.take().unwrap();
@ -461,12 +461,13 @@ impl<C: Contribution, N: NodeId> StateMachine<C, N> {
match self.state {
State::Observer { ref mut dhb, .. } | State::Validator { ref mut dhb, .. } => {
trace!("State::handle_iom: Handling: {:?}", iom);
let mut rng = StdRng::from_entropy();
let step_opt = Some({
let dhb = dhb.as_mut().unwrap();
match iom {
InputOrMessage::Contribution(contrib) => dhb.propose(contrib),
InputOrMessage::Contribution(contrib) => dhb.propose(contrib, &mut rng),
InputOrMessage::Change(change) => dhb.vote_for(change),
InputOrMessage::Message(src_nid, msg) => dhb.handle_message(&src_nid, msg),
InputOrMessage::Message(src_nid, msg) => dhb.handle_message(&src_nid, msg, &mut rng),
}
});

View File

@ -24,7 +24,6 @@ extern crate log;
#[macro_use]
extern crate failure;
extern crate crossbeam;
// #[macro_use] extern crate crossbeam_channel;
extern crate chrono;
extern crate crypto;
extern crate num_bigint;
@ -68,9 +67,9 @@ use hbbft::{
Change as DhbChange, DynamicHoneyBadger, JoinPlan, Message as DhbMessage,
},
sync_key_gen::{Ack, Part},
Contribution as HbbftContribution, DaStep as MessagingStep, NodeIdT,
Contribution as HbbftContribution, CpStep as MessagingStep, NodeIdT,
};
use rand::{Rand, Rng};
use rand::{distributions::{Standard, Distribution}, Rng};
use serde::{de::DeserializeOwned, Serialize};
use std::{
collections::BTreeMap,
@ -137,9 +136,9 @@ impl<C> Contribution for C where
{
}
pub trait NodeId: NodeIdT + Serialize + DeserializeOwned + Rand + 'static {}
pub trait NodeId: NodeIdT + Serialize + DeserializeOwned + 'static {}
impl<N> NodeId for N where N: NodeIdT + Serialize + DeserializeOwned + Rand + 'static {}
impl<N> NodeId for N where N: NodeIdT + Serialize + DeserializeOwned + 'static {}
/// A unique identifier.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
@ -152,8 +151,8 @@ impl Uid {
}
}
impl Rand for Uid {
fn rand<R: Rng>(_rng: &mut R) -> Uid {
impl Distribution<Uid> for Standard {
fn sample<R: Rng + ?Sized>(&self, _rng: &mut R) -> Uid {
Uid::new()
}
}
@ -220,7 +219,7 @@ type ActiveNetworkInfo<N> = (Vec<NetworkNodeInfo<N>>, PublicKeySet, BTreeMap<N,
/// The current state of the network.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum NetworkState<N: Ord + Rand> {
pub enum NetworkState<N: Ord> {
None,
Unknown(Vec<NetworkNodeInfo<N>>),
AwaitingMorePeersForKeyGeneration(Vec<NetworkNodeInfo<N>>),
@ -233,7 +232,7 @@ pub enum NetworkState<N: Ord + Rand> {
/// [`Message`](enum.WireMessageKind.html#variant.Message) variants are among
/// those verified.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum WireMessageKind<C, N: Ord + Rand> {
pub enum WireMessageKind<C, N: Ord> {
HelloFromValidator(N, InAddr, PublicKey, NetworkState<N>),
HelloRequestChangeAdd(N, InAddr, PublicKey),
WelcomeReceivedChangeAdd(N, PublicKey, NetworkState<N>),
@ -257,7 +256,7 @@ pub enum WireMessageKind<C, N: Ord + Rand> {
/// Messages sent over the network between nodes.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct WireMessage<C, N: Ord + Rand> {
pub struct WireMessage<C, N: Ord> {
kind: WireMessageKind<C, N>,
}
@ -304,12 +303,10 @@ impl<C: Contribution, N: NodeId> WireMessage<C, N> {
}
pub fn key_gen_part(instance_id: key_gen::InstanceId, part: Part) -> WireMessage<C, N> {
// WireMessageKind::KeyGenPart(part).into()
WireMessage::key_gen(instance_id, key_gen::Message::part(part))
}
pub fn key_gen_ack(instance_id: key_gen::InstanceId, ack: Ack) -> WireMessage<C, N> {
// WireMessageKind::KeyGenAck(outcome).into()
WireMessage::key_gen(instance_id, key_gen::Message::ack(ack))
}