Skip to content

Commit 408aa05

Browse files
committed
Restore tests in vrf::threshold
1 parent 77c7778 commit 408aa05

File tree

2 files changed

+153
-86
lines changed

2 files changed

+153
-86
lines changed

node/common/src/service/block_producer/vrf_evaluator.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,70 @@ impl node::block_producer_effectful::vrf_evaluator_effectful::BlockProducerVrfEv
7878
}
7979
}
8080
}
81+
82+
#[cfg(test)]
83+
mod tests {
84+
use std::str::FromStr;
85+
86+
// use mina_signer::keypair;
87+
use node::account::AccountSecretKey;
88+
89+
use super::*;
90+
91+
#[test]
92+
#[ignore]
93+
fn test_vrf() {
94+
let json = std::fs::read_to_string("/tmp/vrf.json").unwrap();
95+
let vrf_evaluator_input: VrfEvaluatorInput = serde_json::from_str(&json).unwrap();
96+
97+
let private = "SOME_KEY";
98+
let private = AccountSecretKey::from_str(private).unwrap();
99+
let keypair: Keypair = private.into();
100+
101+
let VrfEvaluatorInput {
102+
epoch_seed,
103+
delegator_table,
104+
global_slot,
105+
total_currency,
106+
staking_ledger_hash: _,
107+
} = &vrf_evaluator_input;
108+
109+
let now = std::time::Instant::now();
110+
111+
let vrf_result = delegator_table
112+
.par_iter()
113+
.map(|(index, (pub_key, stake))| {
114+
let vrf_input = VrfEvaluationInput {
115+
producer_key: keypair.clone(),
116+
global_slot: *global_slot,
117+
epoch_seed: epoch_seed.clone(),
118+
account_pub_key: pub_key.clone(),
119+
delegator_index: *index,
120+
delegated_stake: (*stake).into(),
121+
total_currency: (*total_currency).into(),
122+
};
123+
// let now = redux::Instant::now();
124+
let vrf_result = vrf::evaluate_vrf(vrf_input).unwrap();
125+
// let elapsed = now.elapsed();
126+
// let slot = global_slot;
127+
// eprintln!("vrf::evaluate_vrf: {elapsed:?} slot:{slot:?} index:{index:?}");
128+
// openmina_core::info!(openmina_core::log::system_time(); "vrf::evaluate_vrf: {elapsed:?} slot:{slot:?} index:{index:?}");
129+
130+
// nevaluated.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
131+
132+
// the first delegate that won the slot
133+
if let VrfEvaluationOutput::SlotWon(_) = vrf_result {
134+
return Some(vrf_result);
135+
}
136+
None
137+
})
138+
.collect::<Vec<_>>();
139+
140+
let elapsed = now.elapsed();
141+
let slot = vrf_evaluator_input.global_slot;
142+
let ndelegator = vrf_evaluator_input.delegator_table.len();
143+
// let nevaluated = nevaluated.load(std::sync::atomic::Ordering::Relaxed);
144+
eprintln!("TOTAL vrf::evaluate_vrf: {elapsed:?} slot:{slot:?} ndelegators:{ndelegator:?}");
145+
dbg!(vrf_result);
146+
}
147+
}

vrf/src/threshold.rs

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -216,89 +216,89 @@ pub fn bigrational_as_fixed_point<const N: usize>(
216216
// Threshold::new(delegated_stake, total_currency).threshold_met(vrf_out)
217217
// }
218218

219-
// #[cfg(test)]
220-
// mod test {
221-
// use std::str::FromStr;
222-
223-
// use ark_ff::{One, Zero};
224-
// use num::{BigInt, BigRational, ToPrimitive};
225-
226-
// use super::*;
227-
228-
// // TODO: move to regular fns, rework step
229-
// fn first_non_zero(stake: BigInt, total_currency: BigInt, step: BigInt) -> BigInt {
230-
// let ten = BigInt::from_str("10").unwrap();
231-
// let mut stake = stake;
232-
// if step == BigInt::zero() {
233-
// stake + BigInt::one()
234-
// } else {
235-
// loop {
236-
// let thrs = Threshold::new(stake.to_digits(), total_currency.to_digits());
237-
238-
// if thrs.threshold_rational != BigRational::zero() {
239-
// println!("stake: {stake} nanoMINA");
240-
// return first_non_zero(stake - step.clone(), total_currency, step / ten);
241-
// }
242-
// stake += step.clone();
243-
// }
244-
// }
245-
// }
246-
247-
// #[test]
248-
// #[ignore]
249-
// fn test_threshold_nonzero() {
250-
// // let total_currency = BigInt::from_str("1157953132840039233").unwrap();
251-
// // let initial_stake = BigInt::zero();
252-
// // let initial_step = BigInt::from_str("10000000000000000000").unwrap();
253-
254-
// let total_currency = BigInt::from_str("1025422352000001000").unwrap();
255-
// let initial_stake = BigInt::zero();
256-
// let initial_step = BigInt::from_str("10000000000000000000").unwrap();
257-
258-
// let first_non_zero_nanomina =
259-
// first_non_zero(initial_stake, total_currency.clone(), initial_step);
260-
261-
// let last_zero = first_non_zero_nanomina.clone() - BigInt::one();
262-
263-
// let thrs_zero = Threshold::new(last_zero, total_currency.clone());
264-
// assert_eq!(thrs_zero.threshold_rational, BigRational::zero());
265-
266-
// let thrs_first = Threshold::new(first_non_zero_nanomina.clone(), total_currency);
267-
// assert!(thrs_first.threshold_rational > BigRational::zero());
268-
269-
// let first_non_zero_mina = first_non_zero_nanomina.to_f64().unwrap() / 1_000_000_000.0;
270-
271-
// println!("First non zero stake: {first_non_zero_mina} MINA");
272-
// println!(
273-
// "First non zero threshold: {}",
274-
// thrs_first.threshold_rational.to_f64().unwrap()
275-
// );
276-
// }
277-
278-
// #[test]
279-
// #[ignore]
280-
// fn test_threshold_increase() {
281-
// // let total_currency = BigInt::from_str("1157953132840039233").unwrap();
282-
// // let mut stake_nanomina = BigInt::from_str("1104310162392").unwrap();
283-
// // let mut step = BigInt::from_str("1000000000000").unwrap();
284-
285-
// let total_currency = BigInt::from_str("1025422352000001000").unwrap();
286-
// let mut stake_nanomina = BigInt::from_str("2000000000000000").unwrap();
287-
// let mut step = BigInt::from_str("1000000000000").unwrap();
288-
289-
// loop {
290-
// if stake_nanomina > total_currency {
291-
// break;
292-
// }
293-
// let thrs = Threshold::new(stake_nanomina.clone(), total_currency.clone());
294-
// let stake_mina = stake_nanomina.to_f64().unwrap() / 1_000_000_000.0;
295-
// println!(
296-
// "stake: {stake_mina} MINA - threshold: {}",
297-
// thrs.threshold_rational.to_f64().unwrap()
298-
// );
299-
300-
// stake_nanomina += step.clone();
301-
// step *= 2;
302-
// }
303-
// }
304-
// }
219+
#[cfg(test)]
220+
mod test {
221+
use std::str::FromStr;
222+
223+
use ark_ff::{One, Zero};
224+
use num::{BigInt, BigRational, ToPrimitive};
225+
226+
use super::*;
227+
228+
// TODO: move to regular fns, rework step
229+
fn first_non_zero(stake: BigInt, total_currency: BigInt, step: BigInt) -> BigInt {
230+
let ten = BigInt::from_str("10").unwrap();
231+
let mut stake = stake;
232+
if step == BigInt::zero() {
233+
stake + BigInt::one()
234+
} else {
235+
loop {
236+
let thrs = Threshold::new(stake.clone(), total_currency.clone());
237+
238+
if thrs.threshold_rational != BigRational::zero() {
239+
println!("stake: {stake} nanoMINA");
240+
return first_non_zero(stake - step.clone(), total_currency, step / ten);
241+
}
242+
stake += step.clone();
243+
}
244+
}
245+
}
246+
247+
#[test]
248+
#[ignore]
249+
fn test_threshold_nonzero() {
250+
// let total_currency = BigInt::from_str("1157953132840039233").unwrap();
251+
// let initial_stake = BigInt::zero();
252+
// let initial_step = BigInt::from_str("10000000000000000000").unwrap();
253+
254+
let total_currency = BigInt::from_str("1025422352000001000").unwrap();
255+
let initial_stake = BigInt::zero();
256+
let initial_step = BigInt::from_str("10000000000000000000").unwrap();
257+
258+
let first_non_zero_nanomina =
259+
first_non_zero(initial_stake, total_currency.clone(), initial_step);
260+
261+
let last_zero = first_non_zero_nanomina.clone() - BigInt::one();
262+
263+
let thrs_zero = Threshold::new(last_zero, total_currency.clone());
264+
assert_eq!(thrs_zero.threshold_rational, BigRational::zero());
265+
266+
let thrs_first = Threshold::new(first_non_zero_nanomina.clone(), total_currency);
267+
assert!(thrs_first.threshold_rational > BigRational::zero());
268+
269+
let first_non_zero_mina = first_non_zero_nanomina.to_f64().unwrap() / 1_000_000_000.0;
270+
271+
println!("First non zero stake: {first_non_zero_mina} MINA");
272+
println!(
273+
"First non zero threshold: {}",
274+
thrs_first.threshold_rational.to_f64().unwrap()
275+
);
276+
}
277+
278+
#[test]
279+
#[ignore]
280+
fn test_threshold_increase() {
281+
// let total_currency = BigInt::from_str("1157953132840039233").unwrap();
282+
// let mut stake_nanomina = BigInt::from_str("1104310162392").unwrap();
283+
// let mut step = BigInt::from_str("1000000000000").unwrap();
284+
285+
let total_currency = BigInt::from_str("1025422352000001000").unwrap();
286+
let mut stake_nanomina = BigInt::from_str("2000000000000000").unwrap();
287+
let mut step = BigInt::from_str("1000000000000").unwrap();
288+
289+
loop {
290+
if stake_nanomina > total_currency {
291+
break;
292+
}
293+
let thrs = Threshold::new(stake_nanomina.clone(), total_currency.clone());
294+
let stake_mina = stake_nanomina.to_f64().unwrap() / 1_000_000_000.0;
295+
println!(
296+
"stake: {stake_mina} MINA - threshold: {}",
297+
thrs.threshold_rational.to_f64().unwrap()
298+
);
299+
300+
stake_nanomina += step.clone();
301+
step *= 2;
302+
}
303+
}
304+
}

0 commit comments

Comments
 (0)