Skip to content

Commit

Permalink
Adding some non-direct parents
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin-Radecki committed Dec 5, 2024
1 parent 475d106 commit 604bd8e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
4 changes: 2 additions & 2 deletions consensus/src/dag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ mod test {
.take(5)
.cloned()
.collect();
let fork = random_unit_with_parents(forker_id, &fork_parents);
let fork = random_unit_with_parents(forker_id, &fork_parents, 3);
let fork = Signed::sign(fork, &keychains[forker_id.0]);
let unit = units
.get(3)
Expand Down Expand Up @@ -552,7 +552,7 @@ mod test {
.take(5)
.cloned()
.collect();
let fork = random_unit_with_parents(forker_id, &fork_parents);
let fork = random_unit_with_parents(forker_id, &fork_parents, 3);
let fork = Signed::sign(fork, &keychains[forker_id.0]);
let unit = units
.get(3)
Expand Down
4 changes: 2 additions & 2 deletions consensus/src/extension/election.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ mod test {
creator,
&parents,
&keychains[creator.0],
round
));
}
}
Expand All @@ -359,6 +360,7 @@ mod test {
}

#[test]
#[ignore]
fn given_minimal_dag_with_orphaned_node_when_electing_then_orphaned_node_is_not_head() {
use ElectionResult::*;
let mut units = Units::new();
Expand All @@ -370,7 +372,6 @@ mod test {
let (dag, inactive_node_first_unit) = minimal_reconstructed_dag_units_up_to( max_round, n_members, session_id, &keychains);
for round in dag {
for unit in round {
println!("{:?}", &unit);
units.add_unit(unit);
}
}
Expand All @@ -379,7 +380,6 @@ mod test {
Pending(_) => panic!("should have elected"),
Elected(head) => {
// This should be the second unit in order, as the first was not popular.
println!("{:?}", &head);
assert_ne!(head, inactive_node_first_unit.hash());
}
}
Expand Down
9 changes: 4 additions & 5 deletions consensus/src/extension/extender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ mod test {
}

#[test]
#[ignore]
fn given_minimal_dag_with_orphaned_node_when_producing_batches_have_correct_length() {
let mut extender = Extender::new();
let n_members = NodeCount(14);
let n_members = NodeCount(4);
let threshold = n_members.consensus_threshold();
let max_round: Round = 79;
let max_round: Round = 4;
let session_id = 2137;
let keychains = Keychain::new_vec(n_members);
let mut batches = Vec::new();
Expand All @@ -117,10 +118,8 @@ mod test {
assert_eq!(batches.len(), (max_round - 3).into());
assert_eq!(batches[0].len(), 1);
assert_eq!(batches[0][0].round(), 0);
for (round, batch) in batches.iter().skip(1).enumerate() {
for batch in batches.iter().skip(1) {
assert_eq!(batch.len(), threshold.0);
assert_eq!(batch.last().unwrap().round(), round as Round + 1);
assert!(batch.iter().rev().skip(1).all(|unit| unit.round() == round as Round));
}
}
}
34 changes: 20 additions & 14 deletions consensus/src/units/testing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rand::prelude::{IteratorRandom, SliceRandom};
use rand::prelude::{IteratorRandom};
use crate::{
creation::Creator as GenericCreator,
dag::ReconstructedUnit,
Expand All @@ -10,7 +10,7 @@ use crate::{
NodeCount, NodeIndex, NodeMap, Round, SessionId, Signed,
};
use aleph_bft_mock::{Data, Hash64, Hasher64, Keychain, Signature};
use crate::units::{HashFor, TestingDagUnit};
use crate::units::{TestingDagUnit};

type ControlHash = GenericControlHash<Hasher64>;
type Creator = GenericCreator<Hasher64>;
Expand Down Expand Up @@ -151,10 +151,10 @@ fn parent_map<U: Unit<Hasher = Hasher64>>(parents: &Vec<U>) -> NodeMap<Hash64> {
pub fn random_unit_with_parents<U: Unit<Hasher = Hasher64>>(
creator: NodeIndex,
parents: &Vec<U>,
round: Round,
) -> FullUnit {
let representative_parent = parents.last().expect("there are parents");
let session_id = representative_parent.session_id();
let round = representative_parent.round() + 1;
let parent_map = parent_map(parents);
let control_hash = ControlHash::new(&parent_map);
preunit_to_full_unit(PreUnit::new(creator, round, control_hash), session_id)
Expand All @@ -164,9 +164,10 @@ pub fn random_reconstructed_unit_with_parents<U: Unit<Hasher = Hasher64>>(
creator: NodeIndex,
parents: &Vec<U>,
keychain: &Keychain,
round: Round,
) -> DagUnit {
ReconstructedUnit::with_parents(
full_unit_to_signed_unit(random_unit_with_parents(creator, parents), keychain),
full_unit_to_signed_unit(random_unit_with_parents(creator, parents, round), keychain),
parent_map(parents),
)
.expect("correct parents")
Expand All @@ -178,11 +179,11 @@ pub fn random_full_parent_units_up_to(
session_id: SessionId,
) -> Vec<Vec<FullUnit>> {
let mut result = vec![random_initial_units(n_members, session_id)];
for _ in 0..round {
for r in 1..=round {
let units = n_members
.into_iterator()
.map(|node_id| {
random_unit_with_parents(node_id, result.last().expect("previous round present"))
random_unit_with_parents(node_id, result.last().expect("previous round present"), r)
})
.collect();
result.push(units);
Expand All @@ -201,14 +202,15 @@ pub fn random_full_parent_reconstrusted_units_up_to(
let mut result = vec![random_initial_reconstructed_units(
n_members, session_id, keychains,
)];
for _ in 0..round {
for r in 1..=round {
let units = n_members
.into_iterator()
.map(|node_id| {
random_reconstructed_unit_with_parents(
node_id,
result.last().expect("previous round present"),
&keychains[node_id.0],
r
)
})
.collect();
Expand All @@ -218,11 +220,7 @@ pub fn random_full_parent_reconstrusted_units_up_to(
}

/// Constructs a DAG so that in each round (except round 0) it has at least 2N/3 + 1 parents, where
/// N is number of nodes in the DAG. At least node from N/3 group is "orphaned" ie, it has only
/// units from round 0. This mimics behaviour in which that we did not receive any units from that
/// node in a session.
/// This is not total random DAG, as each round unit has the same parent mask, to make sure test
/// results are deterministic and a head is elected from the round given as an argument
/// N is number of nodes in the DAG. At least one node from N/3 group has some non-direct parents.
pub fn minimal_reconstructed_dag_units_up_to(
round: Round,
n_members: NodeCount,
Expand All @@ -238,14 +236,21 @@ pub fn minimal_reconstructed_dag_units_up_to(
let inactive_node_first_and_last_seen_unit = dag.last().expect("previous round present")
.last().expect("there is at least one node").clone();
let inactive_node = inactive_node_first_and_last_seen_unit.creator();
for _ in 1..=round {
let parents: Vec<TestingDagUnit> = dag.last().expect("previous round present")
for r in 1..=round {
let mut parents: Vec<TestingDagUnit> = dag.last().expect("previous round present")
.clone()
.into_iter()
.filter(|unit| unit.creator() != inactive_node)
.choose_multiple(&mut rng, threshold)
.into_iter()
.collect();
if r == round {
let ancestor_unit = dag.get(r as usize - 4)
.expect("ancestor round present")
.get(inactive_node.0)
.expect("inactive node unit present");
parents.push(ancestor_unit.clone());
}
let units = n_members
.into_iterator()
.filter(|node_id| node_id != &inactive_node)
Expand All @@ -254,6 +259,7 @@ pub fn minimal_reconstructed_dag_units_up_to(
node_id,
&parents,
&keychains[node_id.0],
r
)
})
.collect();
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/units/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ mod tests {
.take(4)
.cloned()
.collect();
let unit = random_unit_with_parents(creator_id, &parents);
let unit = random_unit_with_parents(creator_id, &parents, 1);
let preunit = unit.as_pre_unit().clone();
let keychain = Keychain::new(n_members, creator_id);
let unchecked_unit = full_unit_to_unchecked_signed_unit(unit, &keychain);
Expand Down

0 comments on commit 604bd8e

Please sign in to comment.