Mix: Fix bug in cover traffic ticket generation (#915)
* Fix bug in cover traffic ticket generation * fix hash bug * Clippy happy
This commit is contained in:
parent
360a9aa558
commit
f71db1a7fe
@ -1,3 +1,4 @@
|
|||||||
|
use blake2::digest::consts::U4;
|
||||||
use blake2::Digest;
|
use blake2::Digest;
|
||||||
use futures::{Stream, StreamExt};
|
use futures::{Stream, StreamExt};
|
||||||
use nomos_mix_message::MixMessage;
|
use nomos_mix_message::MixMessage;
|
||||||
@ -84,12 +85,12 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn generate_ticket<Id: Hash + Eq + AsRef<[u8]>>(node_id: Id, r: usize, slot: usize) -> u32 {
|
fn generate_ticket<Id: Hash + Eq + AsRef<[u8]>>(node_id: Id, r: usize, slot: usize) -> u32 {
|
||||||
let mut hasher = blake2::Blake2b512::new();
|
let mut hasher = blake2::Blake2s::<U4>::new();
|
||||||
hasher.update(node_id);
|
hasher.update(node_id);
|
||||||
hasher.update(r.to_be_bytes());
|
hasher.update(r.to_be_bytes());
|
||||||
hasher.update(slot.to_be_bytes());
|
hasher.update(slot.to_be_bytes());
|
||||||
let hash = &hasher.finalize()[..];
|
let hash: [u8; std::mem::size_of::<u32>()] = hasher.finalize()[..].to_vec().try_into().unwrap();
|
||||||
u32::from_be_bytes(hash.try_into().unwrap())
|
u32::from_be_bytes(hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn select_slot<Id: Hash + Eq + AsRef<[u8]> + Copy>(
|
fn select_slot<Id: Hash + Eq + AsRef<[u8]> + Copy>(
|
||||||
@ -100,10 +101,12 @@ fn select_slot<Id: Hash + Eq + AsRef<[u8]> + Copy>(
|
|||||||
winning_probability: f64,
|
winning_probability: f64,
|
||||||
) -> HashSet<u32> {
|
) -> HashSet<u32> {
|
||||||
let i = (slots_per_epoch as f64).div(network_size as f64) * winning_probability;
|
let i = (slots_per_epoch as f64).div(network_size as f64) * winning_probability;
|
||||||
let i = i.ceil() as usize;
|
let size = i.ceil() as usize;
|
||||||
let mut w = HashSet::new();
|
let mut w = HashSet::new();
|
||||||
while w.len() != i {
|
let mut i = 0;
|
||||||
w.insert(generate_ticket(node_id, r, slots_per_epoch));
|
while w.len() != size {
|
||||||
|
w.insert(generate_ticket(node_id, r, i) % slots_per_epoch as u32);
|
||||||
|
i += 1;
|
||||||
}
|
}
|
||||||
w
|
w
|
||||||
}
|
}
|
||||||
@ -111,3 +114,17 @@ fn select_slot<Id: Hash + Eq + AsRef<[u8]> + Copy>(
|
|||||||
fn winning_probability(number_of_hops: usize) -> f64 {
|
fn winning_probability(number_of_hops: usize) -> f64 {
|
||||||
1.0 / number_of_hops as f64
|
1.0 / number_of_hops as f64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::cover_traffic::{generate_ticket, select_slot, winning_probability};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ticket() {
|
||||||
|
generate_ticket(10u32.to_be_bytes(), 1123, 0);
|
||||||
|
for i in (0..1u32) {
|
||||||
|
let slots = select_slot(i.to_be_bytes(), 1234, 100, 21600, winning_probability(1));
|
||||||
|
println!("slots = {slots:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user