Skip to content

Commit

Permalink
feat: random network redundancy adjustment test
Browse files Browse the repository at this point in the history
  • Loading branch information
BastienFaivre committed Jan 9, 2025
1 parent ba5109c commit c8f429f
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 173 deletions.
1 change: 1 addition & 0 deletions dog/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ where
from: author.to_bytes(),
seqno,
data: data.clone(),
// Signature and key fields are not included in the signature
signature: vec![],
key: vec![],
};
Expand Down
1 change: 1 addition & 0 deletions dog/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl DogCodec {
}

let mut transaction_sig = transaction.clone();
// Signature and key fields are not part of the signature
transaction_sig.signature = vec![];
transaction_sig.key = vec![];
let mut buf = Vec::with_capacity(transaction_sig.get_size());
Expand Down
4 changes: 3 additions & 1 deletion dog/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ pub struct RawTransaction {
pub seqno: u64,
/// The content of the transaction.
pub data: Vec<u8>,

/// FIELDS BELOW ARE NOT PART OF THE SIGNATURE
/// The signature of the transaction if it is signed.
pub signature: Option<Vec<u8>>,
/// The public key of the transaction if it is signed.
pub key: Option<Vec<u8>>,
// TODO: carry history of reached peers?
}

impl From<RawTransaction> for proto::Transaction {
Expand Down
45 changes: 43 additions & 2 deletions dog/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,42 @@ pub struct Test<const N: usize> {
}

impl<const N: usize> Test<N> {
// Returns the bootstrap sets for a random network of N nodes.
// The random network is generated by creating a random spanning tree of N nodes.
// Then, we add additional random edges to the network. This number is randomly chosen between
// 1 and (N*(N-1))/2 - N, which is the maximum number of edges to get a fully connected network.
pub fn random_network() -> [Vec<usize>; N] {
assert!(N > 2);

let mut rng = rand::thread_rng();
let mut bootstrap_sets = [const { Vec::new() }; N];

// Create a random spanning tree
for i in 1..N {
let parent = rng.gen_range(0..i);
bootstrap_sets[i].push(parent);
bootstrap_sets[parent].push(i);
}

// Add additional random edges
let max_edges = ((N - 1) * (N - 1)) / 2; // Not N * (N - 1) because we already have N - 1 edges
let num_edges = rng.gen_range(0..max_edges);
for _ in 1..num_edges {
let i = rng.gen_range(0..N);
let j = rng.gen_range(0..N);
if i != j && !bootstrap_sets[i].contains(&j) {
bootstrap_sets[i].push(j);
bootstrap_sets[j].push(i);
}
}

bootstrap_sets
}

pub fn new_with_unique_config(
config: libp2p_dog::Config,
bootstrap_sets: [Vec<usize>; N],
signed_transactions: bool,
) -> Result<Self, Box<dyn std::error::Error>> {
let base_port = rand::thread_rng().gen_range(21000..50000);

Expand All @@ -31,7 +64,7 @@ impl<const N: usize> Test<N> {
})
.collect();

TestNode::new(addr, bootstrap_set, config.clone()).unwrap()
TestNode::new(addr, bootstrap_set, config.clone(), signed_transactions).unwrap()
})
.collect::<Vec<_>>()
.try_into()
Expand Down Expand Up @@ -78,6 +111,7 @@ pub enum Event {
}

pub struct TestNode {
signed_transactions: bool,
keypair: Keypair,
peer_id: PeerId,
addr: Multiaddr,
Expand All @@ -93,13 +127,15 @@ impl TestNode {
addr: Multiaddr,
bootstrap_set: Vec<Multiaddr>,
config: libp2p_dog::Config,
signed_transactions: bool,
) -> Result<Self, Box<dyn std::error::Error>> {
let keypair = Keypair::generate_ed25519();
let peer_id = PeerId::from_public_key(&keypair.public());

let (tx_event, rx_event) = mpsc::unbounded_channel();

Ok(Self {
signed_transactions,
keypair,
peer_id,
addr,
Expand All @@ -124,6 +160,7 @@ impl TestNode {
}

pub fn spawn(&mut self) -> JoinHandle<()> {
let signed_transactions = self.signed_transactions;
let keypair = self.keypair.clone();
let addr = self.addr.clone();
let bootstrap_set = self.bootstrap_set.clone();
Expand All @@ -144,7 +181,11 @@ impl TestNode {
.unwrap()
.with_behaviour(|key| {
libp2p_dog::Behaviour::<IdentityTransform>::new(
libp2p_dog::TransactionAuthenticity::Signed(key.clone()),
if signed_transactions {
libp2p_dog::TransactionAuthenticity::Signed(key.clone())
} else {
libp2p_dog::TransactionAuthenticity::Author(key.public().to_peer_id())
},
config,
)
.expect("Failed to create dog behaviour")
Expand Down
Loading

0 comments on commit c8f429f

Please sign in to comment.