Skip to content

Commit c0d5a77

Browse files
committed
add test
1 parent 708eff5 commit c0d5a77

File tree

1 file changed

+70
-22
lines changed

1 file changed

+70
-22
lines changed

gossip/src/push_active_set.rs

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ impl Default for PushActiveSet {
5353
}
5454
}
5555

56+
#[cfg(test)]
57+
impl PushActiveSet {
58+
fn new_static() -> Self {
59+
Self {
60+
entries: Default::default(),
61+
alpha: DEFAULT_ALPHA,
62+
mode: WeightingMode::Static,
63+
}
64+
}
65+
}
66+
5667
// Keys are gossip nodes to push messages to.
5768
// Values are which origins the node has pruned.
5869
#[derive(Default)]
@@ -148,7 +159,6 @@ impl PushActiveSet {
148159
let f_scaled = ((num_unstaked * SCALE as usize) + nodes.len() / 2) / nodes.len();
149160
let alpha_target = ALPHA_MIN + (f_scaled as u64).min(SCALE);
150161
self.alpha = filter_alpha(self.alpha, alpha_target, ALPHA_MIN, ALPHA_MAX);
151-
println!("alpha_target: {}, alpha: {}", alpha_target, self.alpha);
152162

153163
for (k, entry) in self.entries.iter_mut().enumerate() {
154164
let weights: Vec<u64> = buckets
@@ -297,15 +307,15 @@ mod tests {
297307
}
298308

299309
#[test]
300-
fn test_push_active_set() {
310+
fn test_push_active_set_static_weighting() {
301311
const CLUSTER_SIZE: usize = 117;
302312
let mut rng = ChaChaRng::from_seed([189u8; 32]);
303313
let pubkey = Pubkey::new_unique();
304314
let nodes: Vec<_> = repeat_with(Pubkey::new_unique).take(20).collect();
305315
let stakes = repeat_with(|| rng.gen_range(1..MAX_STAKE));
306316
let mut stakes: HashMap<_, _> = nodes.iter().copied().zip(stakes).collect();
307317
stakes.insert(pubkey, rng.gen_range(1..MAX_STAKE));
308-
let mut active_set = PushActiveSet::default();
318+
let mut active_set = PushActiveSet::new_static();
309319
assert!(active_set.entries.iter().all(|entry| entry.0.is_empty()));
310320
active_set.rotate(&mut rng, 5, CLUSTER_SIZE, &nodes, &stakes);
311321
assert!(active_set.entries.iter().all(|entry| entry.0.len() == 5));
@@ -317,28 +327,9 @@ mod tests {
317327
}
318328
let other = &nodes[5];
319329
let origin = &nodes[17];
320-
321-
// Debug: print expected nodes
322-
// let expected_indices = [13, 5, 18, 16, 0];
323-
// println!("Expected nodes:");
324-
// for (i, &k) in expected_indices.iter().enumerate() {
325-
// println!(" [{}] nodes[{}] = {:?}", i, k, nodes[k]);
326-
// }
327-
328-
// // Debug: print actual nodes returned by get_nodes
329-
// let actual_nodes: Vec<_> = active_set
330-
// .get_nodes(&pubkey, origin, |_| false, &stakes)
331-
// .collect();
332-
// println!("Actual nodes returned by get_nodes:");
333-
// for (i, node) in actual_nodes.iter().enumerate() {
334-
// println!(" [{}] = {:?}", i, node);
335-
// }
336-
337330
assert!(active_set
338331
.get_nodes(&pubkey, origin, |_| false, &stakes)
339332
.eq([13, 5, 18, 16, 0].into_iter().map(|k| &nodes[k])));
340-
341-
///////////////////////////
342333
assert!(active_set
343334
.get_nodes(&pubkey, other, |_| false, &stakes)
344335
.eq([13, 18, 16, 0].into_iter().map(|k| &nodes[k])));
@@ -371,6 +362,63 @@ mod tests {
371362
.eq([16, 7, 11].into_iter().map(|k| &nodes[k])));
372363
}
373364

365+
#[test]
366+
fn test_push_active_set_dynamic_weighting() {
367+
const CLUSTER_SIZE: usize = 117;
368+
let mut rng = ChaChaRng::from_seed([14u8; 32]);
369+
let pubkey = Pubkey::new_unique();
370+
let nodes: Vec<_> = repeat_with(Pubkey::new_unique).take(20).collect();
371+
let stakes = repeat_with(|| rng.gen_range(1..MAX_STAKE));
372+
let mut stakes: HashMap<_, _> = nodes.iter().copied().zip(stakes).collect();
373+
stakes.insert(pubkey, rng.gen_range(1..MAX_STAKE));
374+
let mut active_set = PushActiveSet::default();
375+
assert!(active_set.entries.iter().all(|entry| entry.0.is_empty()));
376+
active_set.rotate(&mut rng, 5, CLUSTER_SIZE, &nodes, &stakes);
377+
assert!(active_set.entries.iter().all(|entry| entry.0.len() == 5));
378+
// Assert that for all entries, each filter already prunes the key.
379+
for entry in &active_set.entries {
380+
for (node, filter) in entry.0.iter() {
381+
assert!(filter.contains(node));
382+
}
383+
}
384+
let other = &nodes[6];
385+
let origin = &nodes[17];
386+
assert!(active_set
387+
.get_nodes(&pubkey, origin, |_| false, &stakes)
388+
.eq([7, 6, 2, 4, 12].into_iter().map(|k| &nodes[k])));
389+
assert!(active_set
390+
.get_nodes(&pubkey, other, |_| false, &stakes)
391+
.eq([7, 2, 4, 12].into_iter().map(|k| &nodes[k])));
392+
393+
active_set.prune(&pubkey, &nodes[6], &[*origin], &stakes);
394+
active_set.prune(&pubkey, &nodes[11], &[*origin], &stakes);
395+
active_set.prune(&pubkey, &nodes[4], &[*origin], &stakes);
396+
assert!(active_set
397+
.get_nodes(&pubkey, origin, |_| false, &stakes)
398+
.eq([7, 2, 12].into_iter().map(|k| &nodes[k])));
399+
assert!(active_set
400+
.get_nodes(&pubkey, other, |_| false, &stakes)
401+
.eq([7, 2, 4, 12].into_iter().map(|k| &nodes[k])));
402+
active_set.rotate(&mut rng, 7, CLUSTER_SIZE, &nodes, &stakes);
403+
assert!(active_set.entries.iter().all(|entry| entry.0.len() == 7));
404+
assert!(active_set
405+
.get_nodes(&pubkey, origin, |_| false, &stakes)
406+
.eq([2, 12, 16, 9, 14].into_iter().map(|k| &nodes[k])));
407+
assert!(active_set
408+
.get_nodes(&pubkey, other, |_| false, &stakes)
409+
.eq([2, 4, 12, 16, 9, 14].into_iter().map(|k| &nodes[k])));
410+
let origins = [*origin, *other];
411+
active_set.prune(&pubkey, &nodes[2], &origins, &stakes);
412+
active_set.prune(&pubkey, &nodes[12], &origins, &stakes);
413+
active_set.prune(&pubkey, &nodes[9], &origins, &stakes);
414+
assert!(active_set
415+
.get_nodes(&pubkey, origin, |_| false, &stakes)
416+
.eq([16, 14].into_iter().map(|k| &nodes[k])));
417+
assert!(active_set
418+
.get_nodes(&pubkey, other, |_| false, &stakes)
419+
.eq([4, 16, 14].into_iter().map(|k| &nodes[k])));
420+
}
421+
374422
#[test]
375423
fn test_push_active_set_entry() {
376424
const NUM_BLOOM_FILTER_ITEMS: usize = 100;

0 commit comments

Comments
 (0)