Fix left and right iterators for `Echo` and `EchoHash` messages

This commit is contained in:
pawanjay176 2019-05-03 22:27:24 +05:30
parent 279c482297
commit 6abc5fea3f
2 changed files with 23 additions and 12 deletions

View File

@ -380,9 +380,12 @@ impl<N: NodeIdT> Broadcast<N> {
let p = p.clone().unwrap();
let echo_msg = Message::Echo(p.clone());
let mut step = Step::default();
// TODO: iterator not correctly describing left of our_id. Use cycle method on
// iterator to correctly specify.
let left = self.netinfo.all_ids_except(self.proposer_id()).take(self.netinfo.num_correct()+self.fault_estimate);
// `N - 2f + g` node ids to the left of our_id (including our_id)
// after arranging all node ids in a circular list.
let left = self.netinfo.all_ids_except(self.proposer_id())
.cycle()
.skip_while(|x| *x == self.our_id())
.take(self.netinfo.num_correct() - self.netinfo.num_faulty()+self.fault_estimate);
for id in left {
let msg = Target::Node(id.clone()).message(echo_msg.clone());
step.messages.push(msg);
@ -403,9 +406,13 @@ impl<N: NodeIdT> Broadcast<N> {
let p = p.clone().unwrap();
let echo_msg = Message::Echo(p.clone());
let mut step = Step::default();
// TODO: iterator not correctly describing right of our_id. Use cycle method on
// iterator to correctly specify.
let right = self.netinfo.all_ids_except(self.proposer_id()).skip(self.netinfo.num_correct()+self.fault_estimate);
// Remaining `2f - g` node ids to the right of our_id (including our_id)
// after arranging all node ids in a circular list.
let right = self.netinfo.all_ids_except(self.proposer_id())
.cycle()
.skip_while(|x| *x == self.our_id())
.skip(self.netinfo.num_correct() - self.netinfo.num_faulty() + self.fault_estimate)
.take_while(|x| *x == self.our_id());
for id in right {
if !self.can_decodes.contains(&(id.clone(), *p.root_hash())) {
let msg = Target::Node(id.clone()).message(echo_msg.clone());
@ -425,9 +432,13 @@ impl<N: NodeIdT> Broadcast<N> {
}
let echo_hash_msg = Message::EchoHash(*hash);
let mut step = Step::default();
// TODO: iterator not correctly describing right of our_id. Use cycle method on
// iterator to correctly specify.
let right = self.netinfo.all_ids_except(self.proposer_id()).skip(self.netinfo.num_correct()+self.fault_estimate);
// Remaining `2f - g` node ids to the right of our_id (including our_id)
// after arranging all node ids in a circular list.
let right = self.netinfo.all_ids_except(self.proposer_id())
.cycle()
.skip_while(|x| *x == self.our_id())
.skip(self.netinfo.num_correct() - self.netinfo.num_faulty() + self.fault_estimate)
.take_while(|x| *x == self.our_id());
for id in right {
let msg = Target::Node(id.clone()).message(echo_hash_msg.clone());
step.messages.push(msg);

View File

@ -82,19 +82,19 @@ impl<N: NodeIdT> NetworkInfo<N> {
/// ID of all nodes in the network.
#[inline]
pub fn all_ids(&self) -> impl Iterator<Item = &N> {
pub fn all_ids(&self) -> impl Iterator<Item = &N> + Clone {
self.public_keys.keys()
}
/// ID of all nodes in the network except passed id.
#[inline]
pub fn all_ids_except<'a>(&'a self, id: &'a N) -> impl Iterator<Item = &'a N> {
pub fn all_ids_except<'a>(&'a self, id: &'a N) -> impl Iterator<Item = &'a N> + Clone {
self.all_ids().filter(move |&x| x != id)
}
/// ID of all nodes in the network except our id.
#[inline]
pub fn other_ids(&self) -> impl Iterator<Item = &N> {
pub fn other_ids(&self) -> impl Iterator<Item = &N> + Clone {
self.all_ids_except(self.our_id())
}