2018-04-02 13:26:40 -07:00
|
|
|
|
//! Reliable broadcast algorithm instance.
|
2018-04-25 06:07:16 -07:00
|
|
|
|
use std::collections::{HashMap, VecDeque};
|
2018-03-22 15:47:44 -07:00
|
|
|
|
use std::fmt::Debug;
|
|
|
|
|
use std::hash::Hash;
|
2018-04-25 12:41:46 -07:00
|
|
|
|
use std::sync::{Arc, Mutex, RwLock};
|
2018-03-20 09:32:19 -07:00
|
|
|
|
use crossbeam;
|
2018-03-16 11:12:14 -07:00
|
|
|
|
use proto::*;
|
|
|
|
|
use std::marker::{Send, Sync};
|
2018-04-11 09:57:30 -07:00
|
|
|
|
use merkle::{Hashable, MerkleTree};
|
2018-03-27 13:59:38 -07:00
|
|
|
|
use merkle::proof::{Proof, Lemma, Positioned};
|
2018-04-06 09:39:15 -07:00
|
|
|
|
use reed_solomon_erasure as rse;
|
2018-03-27 13:59:38 -07:00
|
|
|
|
use reed_solomon_erasure::ReedSolomon;
|
2018-04-06 09:39:15 -07:00
|
|
|
|
use crossbeam_channel::{Sender, Receiver, SendError, RecvError};
|
2018-04-05 05:09:46 -07:00
|
|
|
|
|
2018-04-24 03:29:13 -07:00
|
|
|
|
use messaging::{Target, TargetedMessage, SourcedMessage,
|
2018-04-25 12:41:46 -07:00
|
|
|
|
NodeUid, Algorithm, ProposedValue, QMessage, MessageLoopState,
|
2018-04-25 06:07:16 -07:00
|
|
|
|
Handler, LocalMessage, RemoteMessage, AlgoMessage,
|
|
|
|
|
RemoteNode};
|
2018-04-24 09:31:21 -07:00
|
|
|
|
use messaging;
|
2018-03-22 15:47:44 -07:00
|
|
|
|
|
2018-04-25 12:41:46 -07:00
|
|
|
|
struct BroadcastState {
|
2018-04-25 06:07:16 -07:00
|
|
|
|
root_hash: Option<Vec<u8>>,
|
|
|
|
|
leaf_values: Vec<Option<Box<[u8]>>>,
|
|
|
|
|
leaf_values_num: usize,
|
2018-04-25 12:41:46 -07:00
|
|
|
|
echo_num: usize,
|
|
|
|
|
readys: HashMap<Vec<u8>, usize>,
|
|
|
|
|
ready_sent: bool,
|
|
|
|
|
ready_to_decode: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Broadcast {
|
|
|
|
|
uid: NodeUid,
|
|
|
|
|
num_nodes: usize,
|
|
|
|
|
num_faulty_nodes: usize,
|
|
|
|
|
parity_shard_num: usize,
|
|
|
|
|
data_shard_num: usize,
|
|
|
|
|
coding: ReedSolomon,
|
|
|
|
|
/// Mutable state
|
|
|
|
|
state: RwLock<BroadcastState>
|
2018-04-24 03:29:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Broadcast {
|
2018-04-25 12:41:46 -07:00
|
|
|
|
pub fn new(uid: NodeUid, num_nodes: usize) -> Result<Self, Error> {
|
|
|
|
|
let num_faulty_nodes = (num_nodes - 1) / 3;
|
|
|
|
|
let parity_shard_num = 2 * num_faulty_nodes;
|
|
|
|
|
let data_shard_num = num_nodes - parity_shard_num;
|
|
|
|
|
let coding = ReedSolomon::new(data_shard_num, parity_shard_num)?;
|
|
|
|
|
|
|
|
|
|
Ok(Broadcast {
|
2018-04-25 06:07:16 -07:00
|
|
|
|
uid,
|
|
|
|
|
num_nodes,
|
2018-04-25 12:41:46 -07:00
|
|
|
|
num_faulty_nodes: num_faulty_nodes,
|
|
|
|
|
parity_shard_num: parity_shard_num,
|
|
|
|
|
data_shard_num: data_shard_num,
|
|
|
|
|
coding: coding,
|
|
|
|
|
state: RwLock::new(BroadcastState {
|
|
|
|
|
root_hash: None,
|
|
|
|
|
leaf_values: vec![None; num_nodes],
|
|
|
|
|
leaf_values_num: 0,
|
|
|
|
|
echo_num: 0,
|
|
|
|
|
readys: HashMap::new(),
|
|
|
|
|
ready_sent: false,
|
|
|
|
|
ready_to_decode: false,
|
|
|
|
|
})
|
|
|
|
|
})
|
2018-04-24 03:29:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 12:41:46 -07:00
|
|
|
|
// The message-driven interface function for calls from the main message
|
|
|
|
|
// loop.
|
|
|
|
|
pub fn on_message<E>(&self, m: QMessage, tx: Sender<QMessage>) ->
|
2018-04-24 09:31:21 -07:00
|
|
|
|
Result<MessageLoopState, E>
|
|
|
|
|
where E: From<Error> + From<messaging::Error>
|
|
|
|
|
{
|
2018-04-25 06:07:16 -07:00
|
|
|
|
match m {
|
|
|
|
|
QMessage::Local(LocalMessage {
|
|
|
|
|
dst: _,
|
|
|
|
|
message
|
|
|
|
|
}) => {
|
|
|
|
|
match message {
|
|
|
|
|
AlgoMessage::Broadcast(value) => {
|
|
|
|
|
Err(Error::NotImplemented).map_err(E::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ => Err(Error::UnexpectedMessage).map_err(E::from)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
QMessage::Remote(RemoteMessage {
|
|
|
|
|
node: RemoteNode::Node(uid),
|
|
|
|
|
message
|
|
|
|
|
}) => {
|
2018-04-25 12:41:46 -07:00
|
|
|
|
let mut state = self.state.write().unwrap();
|
|
|
|
|
let no_outgoing =
|
|
|
|
|
Ok(MessageLoopState::Processing(VecDeque::new()));
|
|
|
|
|
|
|
|
|
|
// A value received. Record the value and multicast an echo.
|
2018-04-25 06:07:16 -07:00
|
|
|
|
if let Message::Broadcast(b) = message { match b {
|
|
|
|
|
BroadcastMessage::Value(p) => {
|
|
|
|
|
if uid != self.uid {
|
|
|
|
|
// Ignore value messages from unrelated remote nodes.
|
2018-04-25 12:41:46 -07:00
|
|
|
|
no_outgoing
|
2018-04-25 06:07:16 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
2018-04-25 12:41:46 -07:00
|
|
|
|
// Initialise the root hash if not already
|
|
|
|
|
// initialised.
|
|
|
|
|
if let None = state.root_hash {
|
|
|
|
|
state.root_hash = Some(p.root_hash.clone());
|
2018-04-25 06:07:16 -07:00
|
|
|
|
debug!("Node {} Value root hash {:?}",
|
|
|
|
|
self.uid, HexBytes(&p.root_hash));
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 12:41:46 -07:00
|
|
|
|
if let Some(ref h) = state.root_hash.clone() {
|
2018-04-25 06:07:16 -07:00
|
|
|
|
if p.validate(h.as_slice()) {
|
|
|
|
|
// Save the leaf value for reconstructing
|
|
|
|
|
// the tree later.
|
2018-04-25 12:41:46 -07:00
|
|
|
|
state.leaf_values[index_of_proof(&p)] =
|
2018-04-25 06:07:16 -07:00
|
|
|
|
Some(p.value.clone().into_boxed_slice());
|
2018-04-25 12:41:46 -07:00
|
|
|
|
state.leaf_values_num += 1;
|
2018-04-25 06:07:16 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Enqueue a broadcast of an echo of this proof.
|
|
|
|
|
Ok(MessageLoopState::Processing(VecDeque::from(
|
|
|
|
|
vec![RemoteMessage {
|
|
|
|
|
node: RemoteNode::All,
|
|
|
|
|
message: Message::Broadcast(
|
|
|
|
|
BroadcastMessage::Echo(p))
|
|
|
|
|
}]
|
|
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2018-04-25 12:41:46 -07:00
|
|
|
|
// An echo received. Verify the proof it contains.
|
|
|
|
|
BroadcastMessage::Echo(p) => {
|
|
|
|
|
if let None = state.root_hash {
|
|
|
|
|
if uid == self.uid {
|
|
|
|
|
state.root_hash = Some(p.root_hash.clone());
|
|
|
|
|
debug!("Node {} Echo root hash {:?}",
|
|
|
|
|
self.uid, state.root_hash);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call validate with the root hash as argument.
|
|
|
|
|
if let Some(ref h) = state.root_hash.clone() {
|
|
|
|
|
if p.validate(h.as_slice()) {
|
|
|
|
|
state.echo_num += 1;
|
|
|
|
|
// Save the leaf value for reconstructing the
|
|
|
|
|
// tree later.
|
|
|
|
|
state.leaf_values[index_of_proof(&p)] =
|
|
|
|
|
Some(p.value.clone().into_boxed_slice());
|
|
|
|
|
state.leaf_values_num += 1;
|
|
|
|
|
|
|
|
|
|
// Upon receiving 2f + 1 matching READY(h)
|
|
|
|
|
// messages, wait for N − 2 f ECHO messages,
|
|
|
|
|
// then decode v. Return the decoded v
|
|
|
|
|
if state.ready_to_decode &&
|
|
|
|
|
state.leaf_values_num >=
|
|
|
|
|
self.num_nodes - 2 * self.num_faulty_nodes
|
|
|
|
|
{
|
|
|
|
|
let value =
|
|
|
|
|
decode_from_shards(&mut state.leaf_values,
|
|
|
|
|
&self.coding,
|
|
|
|
|
self.data_shard_num,
|
|
|
|
|
h)?;
|
|
|
|
|
tx.send(QMessage::Local(LocalMessage {
|
|
|
|
|
dst: Algorithm::CommonSubset,
|
|
|
|
|
message: AlgoMessage::Broadcast(value)
|
|
|
|
|
})).map_err(Error::from)?;
|
|
|
|
|
|
|
|
|
|
no_outgoing
|
|
|
|
|
}
|
|
|
|
|
else if state.leaf_values_num >=
|
|
|
|
|
self.num_nodes - self.num_faulty_nodes
|
|
|
|
|
{
|
|
|
|
|
let result: Result<ProposedValue, Error> =
|
|
|
|
|
decode_from_shards(&mut state.leaf_values,
|
|
|
|
|
&self.coding,
|
|
|
|
|
self.data_shard_num,
|
|
|
|
|
h);
|
|
|
|
|
match result { Ok(_) => {
|
|
|
|
|
// if Ready has not yet been sent,
|
|
|
|
|
// multicast Ready
|
|
|
|
|
if !state.ready_sent {
|
|
|
|
|
state.ready_sent = true;
|
|
|
|
|
|
|
|
|
|
Ok(MessageLoopState::Processing(
|
|
|
|
|
VecDeque::from(vec![RemoteMessage {
|
|
|
|
|
node: RemoteNode::All,
|
|
|
|
|
message: Message::Broadcast(
|
|
|
|
|
BroadcastMessage::Ready(
|
|
|
|
|
h.to_owned()))
|
|
|
|
|
}])
|
|
|
|
|
))
|
|
|
|
|
} else { no_outgoing }
|
|
|
|
|
}, Err(e) => Err(E::from(e)) }
|
|
|
|
|
} else { no_outgoing }
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
debug!("Broadcast/{} cannot validate Echo {:?}",
|
|
|
|
|
self.uid, p);
|
|
|
|
|
no_outgoing
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
error!("Broadcast/{} root hash not initialised",
|
|
|
|
|
self.uid);
|
|
|
|
|
no_outgoing
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_ => Err(Error::NotImplemented).map_err(E::from)
|
|
|
|
|
}}
|
2018-04-25 06:07:16 -07:00
|
|
|
|
else {
|
|
|
|
|
Err(Error::UnexpectedMessage).map_err(E::from)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_ => Err(Error::UnexpectedMessage).map_err(E::from)
|
|
|
|
|
}
|
2018-04-24 09:31:21 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 12:41:46 -07:00
|
|
|
|
impl<'a, E> Handler<E> for Broadcast
|
2018-04-24 09:31:21 -07:00
|
|
|
|
where E: From<Error> + From<messaging::Error> {
|
|
|
|
|
fn handle(&self, m: QMessage, tx: Sender<QMessage>) ->
|
|
|
|
|
Result<MessageLoopState, E>
|
|
|
|
|
{
|
2018-04-25 12:41:46 -07:00
|
|
|
|
self.on_message(m, tx)
|
2018-04-24 03:29:13 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-14 03:27:17 -07:00
|
|
|
|
|
2018-04-03 15:08:26 -07:00
|
|
|
|
/// Broadcast algorithm instance.
|
2018-03-27 13:59:38 -07:00
|
|
|
|
///
|
2018-04-03 15:08:26 -07:00
|
|
|
|
/// The ACS algorithm requires multiple broadcast instances running
|
2018-04-02 13:26:40 -07:00
|
|
|
|
/// asynchronously, see Figure 4 in the HBBFT paper. Those are N asynchronous
|
2018-04-03 15:08:26 -07:00
|
|
|
|
/// coroutines, each responding to values from one particular remote node. The
|
2018-04-02 13:26:40 -07:00
|
|
|
|
/// paper doesn't make it clear though how other messages - Echo and Ready - are
|
|
|
|
|
/// distributed over the instances. Also it appears that the sender of a message
|
|
|
|
|
/// might become part of the message for this to work.
|
2018-04-05 05:09:46 -07:00
|
|
|
|
pub struct Instance<'a, T: 'a + Clone + Debug + Send + Sync> {
|
|
|
|
|
/// The transmit side of the channel to comms threads.
|
2018-04-24 03:29:13 -07:00
|
|
|
|
tx: &'a Sender<TargetedMessage<ProposedValue>>,
|
2018-04-05 05:09:46 -07:00
|
|
|
|
/// The receive side of the channel from comms threads.
|
2018-04-24 03:29:13 -07:00
|
|
|
|
rx: &'a Receiver<SourcedMessage<ProposedValue>>,
|
2018-03-29 10:19:41 -07:00
|
|
|
|
/// Value to be broadcast.
|
2018-04-03 15:08:26 -07:00
|
|
|
|
broadcast_value: Option<T>,
|
|
|
|
|
/// This instance's index for identification against its comms task.
|
2018-04-05 05:09:46 -07:00
|
|
|
|
node_index: usize,
|
2018-04-04 04:18:57 -07:00
|
|
|
|
/// Number of nodes participating in broadcast.
|
|
|
|
|
num_nodes: usize,
|
|
|
|
|
/// Maximum allowed number of faulty nodes.
|
|
|
|
|
num_faulty_nodes: usize
|
2018-03-14 17:03:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 10:28:41 -07:00
|
|
|
|
impl<'a, T: Clone + Debug + Hashable + Send + Sync
|
|
|
|
|
+ Into<Vec<u8>> + From<Vec<u8>>>
|
2018-04-02 13:26:40 -07:00
|
|
|
|
Instance<'a, T>
|
2018-03-27 13:59:38 -07:00
|
|
|
|
{
|
2018-04-24 03:29:13 -07:00
|
|
|
|
pub fn new(tx: &'a Sender<TargetedMessage<ProposedValue>>,
|
|
|
|
|
rx: &'a Receiver<SourcedMessage<ProposedValue>>,
|
2018-04-03 15:08:26 -07:00
|
|
|
|
broadcast_value: Option<T>,
|
2018-04-05 05:09:46 -07:00
|
|
|
|
num_nodes: usize,
|
2018-04-03 15:08:26 -07:00
|
|
|
|
node_index: usize) ->
|
|
|
|
|
Self
|
2018-03-27 13:59:38 -07:00
|
|
|
|
{
|
2018-04-02 13:26:40 -07:00
|
|
|
|
Instance {
|
2018-03-19 10:12:20 -07:00
|
|
|
|
tx: tx,
|
|
|
|
|
rx: rx,
|
2018-03-29 10:19:41 -07:00
|
|
|
|
broadcast_value: broadcast_value,
|
2018-04-04 04:18:57 -07:00
|
|
|
|
node_index: node_index,
|
2018-04-05 05:09:46 -07:00
|
|
|
|
num_nodes: num_nodes,
|
|
|
|
|
num_faulty_nodes: (num_nodes - 1) / 3
|
2018-03-14 17:03:21 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-15 16:43:58 -07:00
|
|
|
|
|
2018-03-23 15:54:40 -07:00
|
|
|
|
/// Broadcast stage task returning the computed values in case of success,
|
|
|
|
|
/// and an error in case of failure.
|
2018-04-14 03:27:17 -07:00
|
|
|
|
pub fn run(&mut self) -> Result<T, Error> {
|
2018-03-27 13:59:38 -07:00
|
|
|
|
// Broadcast state machine thread.
|
|
|
|
|
let bvalue = self.broadcast_value.to_owned();
|
2018-04-14 03:27:17 -07:00
|
|
|
|
let result: Result<T, Error>;
|
2018-03-29 10:19:41 -07:00
|
|
|
|
let result_r = Arc::new(Mutex::new(None));
|
|
|
|
|
let result_r_scoped = result_r.clone();
|
2018-03-23 15:54:40 -07:00
|
|
|
|
|
2018-03-20 09:32:19 -07:00
|
|
|
|
crossbeam::scope(|scope| {
|
|
|
|
|
scope.spawn(move || {
|
2018-03-29 10:19:41 -07:00
|
|
|
|
*result_r_scoped.lock().unwrap() =
|
2018-04-05 05:09:46 -07:00
|
|
|
|
Some(inner_run(self.tx, self.rx, bvalue,
|
2018-04-04 04:18:57 -07:00
|
|
|
|
self.node_index, self.num_nodes,
|
|
|
|
|
self.num_faulty_nodes));
|
2018-03-20 09:32:19 -07:00
|
|
|
|
});
|
2018-03-16 11:12:14 -07:00
|
|
|
|
});
|
2018-03-29 10:19:41 -07:00
|
|
|
|
if let Some(ref r) = *result_r.lock().unwrap() {
|
|
|
|
|
result = r.to_owned();
|
2018-03-23 15:54:40 -07:00
|
|
|
|
}
|
2018-03-29 10:19:41 -07:00
|
|
|
|
else {
|
2018-04-06 09:39:15 -07:00
|
|
|
|
result = Err(Error::Threading);
|
2018-03-29 10:19:41 -07:00
|
|
|
|
}
|
|
|
|
|
result
|
2018-03-14 17:03:21 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-22 15:47:44 -07:00
|
|
|
|
|
2018-03-29 09:23:02 -07:00
|
|
|
|
/// Errors returned by the broadcast instance.
|
2018-04-06 09:39:15 -07:00
|
|
|
|
#[derive(Debug, Clone)]
|
2018-04-14 03:27:17 -07:00
|
|
|
|
pub enum Error {
|
2018-03-29 09:23:02 -07:00
|
|
|
|
RootHashMismatch,
|
2018-04-03 15:08:26 -07:00
|
|
|
|
Threading,
|
2018-04-06 09:39:15 -07:00
|
|
|
|
ProofConstructionFailed,
|
|
|
|
|
ReedSolomon(rse::Error),
|
2018-04-25 12:41:46 -07:00
|
|
|
|
Send(SendError<QMessage>),
|
|
|
|
|
SendDeprecated(SendError<TargetedMessage<ProposedValue>>),
|
2018-04-24 09:31:21 -07:00
|
|
|
|
Recv(RecvError),
|
2018-04-25 06:07:16 -07:00
|
|
|
|
UnexpectedMessage,
|
2018-04-24 09:31:21 -07:00
|
|
|
|
NotImplemented
|
2018-04-06 09:39:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 03:27:17 -07:00
|
|
|
|
impl From<rse::Error> for Error
|
2018-04-13 10:28:41 -07:00
|
|
|
|
{
|
2018-04-14 03:27:17 -07:00
|
|
|
|
fn from(err: rse::Error) -> Error { Error::ReedSolomon(err) }
|
2018-04-06 09:39:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 12:41:46 -07:00
|
|
|
|
impl From<SendError<QMessage>> for Error
|
|
|
|
|
{
|
|
|
|
|
fn from(err: SendError<QMessage>) -> Error { Error::Send(err) }
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-24 03:29:13 -07:00
|
|
|
|
impl From<SendError<TargetedMessage<ProposedValue>>> for Error
|
2018-04-06 09:39:15 -07:00
|
|
|
|
{
|
2018-04-25 12:41:46 -07:00
|
|
|
|
fn from(err: SendError<TargetedMessage<ProposedValue>>) ->
|
|
|
|
|
Error { Error::SendDeprecated(err) }
|
2018-04-06 09:39:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 03:27:17 -07:00
|
|
|
|
impl From<RecvError> for Error
|
2018-04-13 10:28:41 -07:00
|
|
|
|
{
|
2018-04-14 03:27:17 -07:00
|
|
|
|
fn from(err: RecvError) -> Error { Error::Recv(err) }
|
2018-03-29 09:23:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 06:51:58 -07:00
|
|
|
|
/// Breaks the input value into shards of equal length and encodes them -- and
|
2018-04-03 15:08:26 -07:00
|
|
|
|
/// some extra parity shards -- with a Reed-Solomon erasure coding scheme. The
|
|
|
|
|
/// returned value contains the shard assigned to this node. That shard doesn't
|
|
|
|
|
/// need to be sent anywhere. It is returned to the broadcast instance and gets
|
|
|
|
|
/// recorded immediately.
|
2018-04-02 13:26:40 -07:00
|
|
|
|
fn send_shards<'a, T>(value: T,
|
2018-04-24 03:29:13 -07:00
|
|
|
|
tx: &'a Sender<TargetedMessage<ProposedValue>>,
|
2018-04-04 04:18:57 -07:00
|
|
|
|
coding: &ReedSolomon) ->
|
2018-04-24 03:29:13 -07:00
|
|
|
|
Result<Proof<ProposedValue>, Error>
|
2018-04-13 10:28:41 -07:00
|
|
|
|
where T: Clone + Debug + Hashable + Send + Sync + Into<Vec<u8>> + From<Vec<u8>>
|
2018-03-28 06:51:58 -07:00
|
|
|
|
{
|
2018-04-04 04:18:57 -07:00
|
|
|
|
let data_shard_num = coding.data_shard_count();
|
|
|
|
|
let parity_shard_num = coding.parity_shard_count();
|
|
|
|
|
|
|
|
|
|
debug!("Data shards: {}, parity shards: {}",
|
|
|
|
|
data_shard_num, parity_shard_num);
|
2018-04-11 07:44:59 -07:00
|
|
|
|
let mut v: Vec<u8> = T::into(value);
|
2018-04-14 03:27:17 -07:00
|
|
|
|
// Insert the length of `v` so it can be decoded without the padding.
|
|
|
|
|
let payload_len = v.len() as u8;
|
|
|
|
|
v.insert(0, payload_len); // TODO: Handle messages larger than 255 bytes.
|
2018-04-06 11:31:20 -07:00
|
|
|
|
let value_len = v.len();
|
2018-03-28 06:51:58 -07:00
|
|
|
|
// Size of a Merkle tree leaf value, in bytes.
|
2018-04-06 11:31:20 -07:00
|
|
|
|
let shard_len = if value_len % data_shard_num > 0 {
|
|
|
|
|
value_len / data_shard_num + 1
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
value_len / data_shard_num
|
|
|
|
|
};
|
|
|
|
|
// Pad the last data shard with zeros. Fill the parity shards with zeros.
|
2018-04-14 03:27:17 -07:00
|
|
|
|
v.resize(shard_len * (data_shard_num + parity_shard_num), 0);
|
2018-03-28 06:51:58 -07:00
|
|
|
|
|
2018-04-14 03:27:17 -07:00
|
|
|
|
debug!("value_len {}, shard_len {}", value_len, shard_len);
|
2018-04-06 11:31:20 -07:00
|
|
|
|
|
2018-03-28 06:51:58 -07:00
|
|
|
|
// Divide the vector into chunks/shards.
|
|
|
|
|
let shards_iter = v.chunks_mut(shard_len);
|
|
|
|
|
// Convert the iterator over slices into a vector of slices.
|
|
|
|
|
let mut shards: Vec<&mut [u8]> = Vec::new();
|
|
|
|
|
for s in shards_iter {
|
|
|
|
|
shards.push(s);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-06 11:31:20 -07:00
|
|
|
|
debug!("Shards before encoding: {:?}", shards);
|
|
|
|
|
|
2018-03-28 06:51:58 -07:00
|
|
|
|
// Construct the parity chunks/shards
|
2018-04-06 09:39:15 -07:00
|
|
|
|
coding.encode(shards.as_mut_slice())?;
|
2018-03-28 06:51:58 -07:00
|
|
|
|
|
2018-04-04 04:18:57 -07:00
|
|
|
|
debug!("Shards: {:?}", shards);
|
|
|
|
|
|
2018-04-24 03:29:13 -07:00
|
|
|
|
let shards_t: Vec<ProposedValue> =
|
2018-04-14 03:27:17 -07:00
|
|
|
|
shards.into_iter().map(|s| s.to_vec()).collect();
|
2018-03-28 06:51:58 -07:00
|
|
|
|
|
|
|
|
|
// Convert the Merkle tree into a partial binary tree for later
|
|
|
|
|
// deconstruction into compound branches.
|
|
|
|
|
let mtree = MerkleTree::from_vec(&::ring::digest::SHA256, shards_t);
|
|
|
|
|
|
2018-04-04 04:18:57 -07:00
|
|
|
|
// Default result in case of `gen_proof` error.
|
2018-04-06 09:39:15 -07:00
|
|
|
|
let mut result = Err(Error::ProofConstructionFailed);
|
2018-04-03 15:08:26 -07:00
|
|
|
|
|
2018-03-28 06:51:58 -07:00
|
|
|
|
// Send each proof to a node.
|
2018-04-14 03:27:17 -07:00
|
|
|
|
for (i, leaf_value) in mtree.iter().enumerate() {
|
|
|
|
|
let proof = mtree.gen_proof(leaf_value.to_vec());
|
2018-03-28 06:51:58 -07:00
|
|
|
|
if let Some(proof) = proof {
|
2018-04-03 15:08:26 -07:00
|
|
|
|
if i == 0 {
|
|
|
|
|
// The first proof is addressed to this node.
|
|
|
|
|
result = Ok(proof);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// Rest of the proofs are sent to remote nodes.
|
2018-04-05 05:09:46 -07:00
|
|
|
|
tx.send(
|
|
|
|
|
TargetedMessage {
|
2018-04-06 05:57:48 -07:00
|
|
|
|
target: Target::Node(i),
|
2018-04-05 05:09:46 -07:00
|
|
|
|
message: Message::Broadcast(
|
|
|
|
|
BroadcastMessage::Value(proof))
|
2018-04-06 09:39:15 -07:00
|
|
|
|
})?;
|
2018-04-03 15:08:26 -07:00
|
|
|
|
}
|
2018-03-28 06:51:58 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-03 15:08:26 -07:00
|
|
|
|
|
|
|
|
|
result
|
2018-03-28 06:51:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-23 15:54:40 -07:00
|
|
|
|
/// The main loop of the broadcast task.
|
2018-04-24 03:29:13 -07:00
|
|
|
|
fn inner_run<'a, T>(tx: &'a Sender<TargetedMessage<ProposedValue>>,
|
|
|
|
|
rx: &'a Receiver<SourcedMessage<ProposedValue>>,
|
2018-04-03 15:08:26 -07:00
|
|
|
|
broadcast_value: Option<T>,
|
2018-04-05 05:09:46 -07:00
|
|
|
|
node_index: usize,
|
|
|
|
|
num_nodes: usize,
|
2018-04-04 04:18:57 -07:00
|
|
|
|
num_faulty_nodes: usize) ->
|
2018-04-14 03:27:17 -07:00
|
|
|
|
Result<T, Error>
|
2018-04-13 10:28:41 -07:00
|
|
|
|
where T: Clone + Debug + Hashable + Send + Sync + Into<Vec<u8>> + From<Vec<u8>>
|
2018-03-22 15:47:44 -07:00
|
|
|
|
{
|
2018-03-27 13:59:38 -07:00
|
|
|
|
// Erasure coding scheme: N - 2f value shards and 2f parity shards
|
2018-04-04 04:18:57 -07:00
|
|
|
|
let parity_shard_num = 2 * num_faulty_nodes;
|
|
|
|
|
let data_shard_num = num_nodes - parity_shard_num;
|
2018-04-06 09:39:15 -07:00
|
|
|
|
let coding = ReedSolomon::new(data_shard_num, parity_shard_num)?;
|
2018-04-03 15:08:26 -07:00
|
|
|
|
// currently known leaf values
|
2018-04-04 04:18:57 -07:00
|
|
|
|
let mut leaf_values: Vec<Option<Box<[u8]>>> = vec![None; num_nodes];
|
2018-04-03 15:08:26 -07:00
|
|
|
|
// Write-once root hash of a tree broadcast from the sender associated with
|
|
|
|
|
// this instance.
|
|
|
|
|
let mut root_hash: Option<Vec<u8>> = None;
|
|
|
|
|
// number of non-None leaf values
|
|
|
|
|
let mut leaf_values_num = 0;
|
2018-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
|
|
// Split the value into chunks/shards, encode them with erasure codes.
|
|
|
|
|
// Assemble a Merkle tree from data and parity shards. Take all proofs from
|
|
|
|
|
// this tree and send them, each to its own node.
|
|
|
|
|
if let Some(v) = broadcast_value {
|
2018-04-06 10:42:04 -07:00
|
|
|
|
send_shards(v, tx, &coding)
|
|
|
|
|
.map(|proof| {
|
|
|
|
|
// Record the first proof as if it were sent by the node to
|
|
|
|
|
// itself.
|
2018-04-03 15:08:26 -07:00
|
|
|
|
let h = proof.root_hash.clone();
|
|
|
|
|
if proof.validate(h.as_slice()) {
|
|
|
|
|
// Save the leaf value for reconstructing the tree later.
|
|
|
|
|
leaf_values[index_of_proof(&proof)] =
|
2018-04-14 03:27:17 -07:00
|
|
|
|
Some(proof.value.clone().into_boxed_slice());
|
2018-04-03 15:08:26 -07:00
|
|
|
|
leaf_values_num = leaf_values_num + 1;
|
|
|
|
|
root_hash = Some(h);
|
|
|
|
|
}
|
2018-04-06 10:42:04 -07:00
|
|
|
|
})?
|
2018-03-27 13:59:38 -07:00
|
|
|
|
}
|
2018-03-23 15:54:40 -07:00
|
|
|
|
|
2018-03-28 15:38:02 -07:00
|
|
|
|
// return value
|
2018-04-14 03:27:17 -07:00
|
|
|
|
let mut result: Option<Result<T, Error>> = None;
|
2018-03-28 15:38:02 -07:00
|
|
|
|
// Number of times Echo was received with the same root hash.
|
|
|
|
|
let mut echo_num = 0;
|
|
|
|
|
// Number of times Ready was received with the same root hash.
|
2018-04-06 10:42:04 -07:00
|
|
|
|
let mut readys: HashMap<Vec<u8>, usize> = HashMap::new();
|
2018-03-28 15:38:02 -07:00
|
|
|
|
let mut ready_sent = false;
|
2018-03-29 09:23:02 -07:00
|
|
|
|
let mut ready_to_decode = false;
|
2018-03-28 15:38:02 -07:00
|
|
|
|
|
2018-03-22 15:47:44 -07:00
|
|
|
|
// TODO: handle exit conditions
|
2018-04-06 09:39:15 -07:00
|
|
|
|
while result.is_none() {
|
2018-03-22 15:47:44 -07:00
|
|
|
|
// Receive a message from the socket IO task.
|
2018-04-06 09:39:15 -07:00
|
|
|
|
let message = rx.recv()?;
|
2018-04-05 05:09:46 -07:00
|
|
|
|
if let SourcedMessage {
|
|
|
|
|
source: i,
|
|
|
|
|
message: Message::Broadcast(message)
|
|
|
|
|
} = message {
|
2018-03-22 15:47:44 -07:00
|
|
|
|
match message {
|
|
|
|
|
// A value received. Record the value and multicast an echo.
|
|
|
|
|
BroadcastMessage::Value(p) => {
|
2018-04-03 15:08:26 -07:00
|
|
|
|
if i != node_index {
|
|
|
|
|
// Ignore value messages from unrelated remote nodes.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 15:38:02 -07:00
|
|
|
|
if let None = root_hash {
|
|
|
|
|
root_hash = Some(p.root_hash.clone());
|
2018-04-06 08:04:28 -07:00
|
|
|
|
debug!("Node {} Value root hash {:?}",
|
2018-04-14 03:27:17 -07:00
|
|
|
|
node_index, HexBytes(&p.root_hash));
|
2018-03-28 15:38:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let &Some(ref h) = &root_hash {
|
|
|
|
|
if p.validate(h.as_slice()) {
|
|
|
|
|
// Save the leaf value for reconstructing the tree
|
|
|
|
|
// later.
|
|
|
|
|
leaf_values[index_of_proof(&p)] =
|
2018-04-14 03:27:17 -07:00
|
|
|
|
Some(p.value.clone().into_boxed_slice());
|
2018-03-28 15:38:02 -07:00
|
|
|
|
leaf_values_num = leaf_values_num + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Broadcast an echo of this proof.
|
2018-04-05 05:09:46 -07:00
|
|
|
|
tx.send(TargetedMessage {
|
|
|
|
|
target: Target::All,
|
|
|
|
|
message: Message::Broadcast(BroadcastMessage::Echo(p))
|
2018-04-06 09:39:15 -07:00
|
|
|
|
})?
|
2018-03-22 15:47:44 -07:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// An echo received. Verify the proof it contains.
|
|
|
|
|
BroadcastMessage::Echo(p) => {
|
2018-03-28 15:38:02 -07:00
|
|
|
|
if let None = root_hash {
|
2018-04-06 08:04:28 -07:00
|
|
|
|
if i == node_index {
|
|
|
|
|
root_hash = Some(p.root_hash.clone());
|
|
|
|
|
debug!("Node {} Echo root hash {:?}",
|
|
|
|
|
node_index, root_hash);
|
|
|
|
|
}
|
2018-03-28 15:38:02 -07:00
|
|
|
|
}
|
2018-03-22 15:47:44 -07:00
|
|
|
|
|
2018-03-28 15:38:02 -07:00
|
|
|
|
// call validate with the root hash as argument
|
|
|
|
|
if let &Some(ref h) = &root_hash {
|
|
|
|
|
if p.validate(h.as_slice()) {
|
|
|
|
|
echo_num += 1;
|
|
|
|
|
// Save the leaf value for reconstructing the tree
|
|
|
|
|
// later.
|
|
|
|
|
leaf_values[index_of_proof(&p)] =
|
2018-04-14 03:27:17 -07:00
|
|
|
|
Some(p.value.clone().into_boxed_slice());
|
2018-03-28 15:38:02 -07:00
|
|
|
|
leaf_values_num = leaf_values_num + 1;
|
|
|
|
|
|
2018-03-29 09:23:02 -07:00
|
|
|
|
// upon receiving 2f + 1 matching READY(h)
|
|
|
|
|
// messages, wait for N − 2 f ECHO messages, then
|
|
|
|
|
// decode v
|
|
|
|
|
if ready_to_decode &&
|
|
|
|
|
leaf_values_num >=
|
2018-04-04 04:18:57 -07:00
|
|
|
|
num_nodes - 2 * num_faulty_nodes
|
2018-03-29 09:23:02 -07:00
|
|
|
|
{
|
|
|
|
|
result = Some(
|
|
|
|
|
decode_from_shards(&mut leaf_values,
|
|
|
|
|
&coding,
|
|
|
|
|
data_shard_num, h));
|
2018-03-22 15:47:44 -07:00
|
|
|
|
}
|
2018-03-29 09:23:02 -07:00
|
|
|
|
else if leaf_values_num >=
|
2018-04-04 04:18:57 -07:00
|
|
|
|
num_nodes - num_faulty_nodes
|
2018-03-29 09:23:02 -07:00
|
|
|
|
{
|
|
|
|
|
result = Some(
|
|
|
|
|
decode_from_shards(&mut leaf_values,
|
|
|
|
|
&coding,
|
|
|
|
|
data_shard_num, h));
|
|
|
|
|
// if Ready has not yet been sent, multicast
|
|
|
|
|
// Ready
|
|
|
|
|
if !ready_sent {
|
|
|
|
|
ready_sent = true;
|
2018-04-05 05:09:46 -07:00
|
|
|
|
tx.send(TargetedMessage {
|
|
|
|
|
target: Target::All,
|
|
|
|
|
message: Message::Broadcast(
|
|
|
|
|
BroadcastMessage::Ready(
|
|
|
|
|
h.to_owned()))
|
2018-04-06 09:39:15 -07:00
|
|
|
|
})?;
|
2018-03-29 09:23:02 -07:00
|
|
|
|
}
|
2018-03-23 15:54:40 -07:00
|
|
|
|
}
|
2018-03-22 15:47:44 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-03-23 15:54:40 -07:00
|
|
|
|
|
2018-04-06 10:42:04 -07:00
|
|
|
|
BroadcastMessage::Ready(ref hash) => {
|
|
|
|
|
// Update the number Ready has been received with this hash.
|
|
|
|
|
*readys.entry(hash.to_vec()).or_insert(1) += 1;
|
|
|
|
|
|
2018-03-28 15:38:02 -07:00
|
|
|
|
// Check that the root hash matches.
|
|
|
|
|
if let &Some(ref h) = &root_hash {
|
2018-04-06 10:42:04 -07:00
|
|
|
|
let ready_num: usize = *readys.get(h).unwrap_or(&0);
|
2018-03-23 15:54:40 -07:00
|
|
|
|
|
2018-03-28 15:38:02 -07:00
|
|
|
|
// Upon receiving f + 1 matching Ready(h) messages, if
|
|
|
|
|
// Ready has not yet been sent, multicast Ready(h).
|
2018-04-04 04:18:57 -07:00
|
|
|
|
if (ready_num == num_faulty_nodes + 1) &&
|
2018-03-28 15:38:02 -07:00
|
|
|
|
!ready_sent
|
|
|
|
|
{
|
2018-04-05 05:09:46 -07:00
|
|
|
|
tx.send(TargetedMessage {
|
|
|
|
|
target: Target::All,
|
|
|
|
|
message: Message::Broadcast(
|
|
|
|
|
BroadcastMessage::Ready(
|
|
|
|
|
h.to_vec()))
|
2018-04-06 09:39:15 -07:00
|
|
|
|
})?;
|
2018-03-28 15:38:02 -07:00
|
|
|
|
}
|
2018-03-23 15:54:40 -07:00
|
|
|
|
|
2018-03-28 15:38:02 -07:00
|
|
|
|
// Upon receiving 2f + 1 matching Ready(h) messages,
|
|
|
|
|
// wait for N − 2f Echo messages, then decode v.
|
2018-04-04 04:18:57 -07:00
|
|
|
|
if ready_num > 2 * num_faulty_nodes {
|
2018-03-29 09:23:02 -07:00
|
|
|
|
// Wait for N - 2f Echo messages, then decode v.
|
2018-04-04 04:18:57 -07:00
|
|
|
|
if echo_num >= num_nodes - 2 * num_faulty_nodes {
|
2018-03-29 09:23:02 -07:00
|
|
|
|
result = Some(
|
|
|
|
|
decode_from_shards(&mut leaf_values,
|
|
|
|
|
&coding,
|
|
|
|
|
data_shard_num, h));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ready_to_decode = true;
|
|
|
|
|
}
|
2018-03-28 15:38:02 -07:00
|
|
|
|
}
|
2018-03-23 15:54:40 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-22 15:47:44 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2018-04-06 08:04:28 -07:00
|
|
|
|
error!("Incorrect message from the socket: {:?}", message);
|
2018-03-22 15:47:44 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-06 09:39:15 -07:00
|
|
|
|
// result is not a None, safe to extract value
|
2018-03-29 09:23:02 -07:00
|
|
|
|
result.unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn decode_from_shards<T>(leaf_values: &mut Vec<Option<Box<[u8]>>>,
|
|
|
|
|
coding: &ReedSolomon,
|
|
|
|
|
data_shard_num: usize,
|
|
|
|
|
root_hash: &Vec<u8>) ->
|
2018-04-14 03:27:17 -07:00
|
|
|
|
Result<T, Error>
|
2018-04-11 09:57:30 -07:00
|
|
|
|
where T: Clone + Debug + Hashable + Send + Sync + From<Vec<u8>> + Into<Vec<u8>>
|
2018-03-29 09:23:02 -07:00
|
|
|
|
{
|
|
|
|
|
// Try to interpolate the Merkle tree using the Reed-Solomon erasure coding
|
|
|
|
|
// scheme.
|
2018-04-06 09:39:15 -07:00
|
|
|
|
coding.reconstruct_shards(leaf_values.as_mut_slice())?;
|
2018-03-29 09:23:02 -07:00
|
|
|
|
|
|
|
|
|
// Recompute the Merkle tree root.
|
|
|
|
|
//
|
2018-04-14 03:27:17 -07:00
|
|
|
|
// Collect shards for tree construction.
|
2018-04-24 03:29:13 -07:00
|
|
|
|
let mut shards: Vec<ProposedValue> = Vec::new();
|
2018-03-29 09:23:02 -07:00
|
|
|
|
for l in leaf_values.iter() {
|
|
|
|
|
if let Some(ref v) = *l {
|
2018-04-14 03:27:17 -07:00
|
|
|
|
shards.push(v.to_vec());
|
2018-03-29 09:23:02 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Construct the Merkle tree.
|
2018-04-14 03:27:17 -07:00
|
|
|
|
let mtree = MerkleTree::from_vec(&::ring::digest::SHA256, shards);
|
2018-03-29 09:23:02 -07:00
|
|
|
|
// If the root hash of the reconstructed tree does not match the one
|
|
|
|
|
// received with proofs then abort.
|
|
|
|
|
if *mtree.root_hash() != *root_hash {
|
|
|
|
|
// NOTE: The paper does not define the meaning of *abort*. But it is
|
|
|
|
|
// sensible not to continue trying to reconstruct the tree after this
|
|
|
|
|
// point. This instance must have received incorrect shards.
|
2018-04-06 09:39:15 -07:00
|
|
|
|
Err(Error::RootHashMismatch)
|
2018-03-29 09:23:02 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// Reconstruct the value from the data shards.
|
|
|
|
|
Ok(glue_shards(mtree, data_shard_num))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Concatenates the first `n` leaf values of a Merkle tree `m` in one value of
|
|
|
|
|
/// type `T`. This is useful for reconstructing the data value held in the tree
|
|
|
|
|
/// and forgetting the leaves that contain parity information.
|
2018-04-24 03:29:13 -07:00
|
|
|
|
fn glue_shards<T>(m: MerkleTree<ProposedValue>, n: usize) -> T
|
2018-04-11 07:44:59 -07:00
|
|
|
|
where T: From<Vec<u8>> + Into<Vec<u8>>
|
2018-03-29 09:23:02 -07:00
|
|
|
|
{
|
|
|
|
|
let mut t: Vec<u8> = Vec::new();
|
|
|
|
|
let mut i = 0;
|
|
|
|
|
|
|
|
|
|
for s in m.into_iter() {
|
|
|
|
|
i += 1;
|
|
|
|
|
if i > n {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-04-14 03:27:17 -07:00
|
|
|
|
for b in s {
|
2018-03-29 09:23:02 -07:00
|
|
|
|
t.push(b);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-14 03:27:17 -07:00
|
|
|
|
let payload_len = t[0] as usize;
|
|
|
|
|
debug!("Glued data shards {:?}", &t[1..(payload_len + 1)]);
|
2018-04-06 08:04:28 -07:00
|
|
|
|
|
2018-04-14 03:27:17 -07:00
|
|
|
|
Vec::into(t[1..(payload_len + 1)].to_vec())
|
2018-03-23 15:54:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// An additional path conversion operation on `Lemma` to allow reconstruction
|
|
|
|
|
/// of erasure-coded `Proof` from `Lemma`s. The output path, when read from left
|
|
|
|
|
/// to right, goes from leaf to root (LSB order).
|
2018-03-28 15:38:02 -07:00
|
|
|
|
fn path_of_lemma(lemma: &Lemma) -> Vec<bool> {
|
2018-03-23 15:54:40 -07:00
|
|
|
|
match lemma.sub_lemma {
|
|
|
|
|
None => {
|
|
|
|
|
match lemma.sibling_hash {
|
|
|
|
|
// lemma terminates with no leaf
|
|
|
|
|
None => vec![],
|
|
|
|
|
// the leaf is on the right
|
|
|
|
|
Some(Positioned::Left(_)) => vec![true],
|
|
|
|
|
// the leaf is on the left
|
|
|
|
|
Some(Positioned::Right(_)) => vec![false],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some(ref l) => {
|
2018-03-28 15:38:02 -07:00
|
|
|
|
let mut p = path_of_lemma(l.as_ref());
|
2018-03-23 15:54:40 -07:00
|
|
|
|
|
|
|
|
|
match lemma.sibling_hash {
|
|
|
|
|
// lemma terminates
|
|
|
|
|
None => (),
|
|
|
|
|
// lemma branches out to the right
|
|
|
|
|
Some(Positioned::Left(_)) => p.push(true),
|
|
|
|
|
// lemma branches out to the left
|
|
|
|
|
Some(Positioned::Right(_)) => p.push(false),
|
|
|
|
|
}
|
|
|
|
|
p
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Further conversion of a binary tree path into an array index.
|
2018-03-28 15:38:02 -07:00
|
|
|
|
fn index_of_path(mut path: Vec<bool>) -> usize {
|
2018-03-23 15:54:40 -07:00
|
|
|
|
let mut idx = 0;
|
|
|
|
|
// Convert to the MSB order.
|
|
|
|
|
path.reverse();
|
|
|
|
|
|
|
|
|
|
for &dir in path.iter() {
|
2018-04-11 05:21:50 -07:00
|
|
|
|
idx = idx << 1;
|
|
|
|
|
if dir == true {
|
|
|
|
|
idx = idx | 1;
|
2018-03-23 15:54:40 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
idx
|
2018-03-22 15:47:44 -07:00
|
|
|
|
}
|
2018-03-28 15:38:02 -07:00
|
|
|
|
|
|
|
|
|
/// Computes the Merkle tree leaf index of a value in a given proof.
|
2018-04-24 03:29:13 -07:00
|
|
|
|
fn index_of_proof(p: &Proof<ProposedValue>) -> usize {
|
2018-03-28 15:38:02 -07:00
|
|
|
|
index_of_path(path_of_lemma(&p.lemma))
|
|
|
|
|
}
|