Skip to content

Commit 4676c3a

Browse files
committed
Relocated function for realistic stake distribution
1 parent 1d2553a commit 4676c3a

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

crypto-benchmarks.rs/src/primitive.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ use num_traits::FromPrimitive;
55
use pallas::ledger::primitives::{byron::Blake2b256, Hash};
66
use pallas::ledger::traverse::time::Slot;
77
use quickcheck::{Arbitrary, Gen};
8-
use rand::prelude::Distribution;
9-
use rand::rngs::StdRng;
10-
use rand::SeedableRng;
118
use serde::{Deserialize, Deserializer, Serialize, Serializer};
12-
use statrs::distribution::ContinuousCDF;
13-
use statrs::distribution::{Beta, Uniform};
149
use std::collections::BTreeMap;
1510

11+
use crate::realism::realistic_stake_dist;
1612
use crate::util::{arbitrary_fixed_bytes, deserialize_fixed_bytes, serialize_fixed_bytes};
1713

1814
pub use pallas::ledger::primitives::PoolKeyhash;
@@ -27,28 +23,6 @@ pub fn arbitrary_coin(g: &mut Gen) -> Coin {
2723
u64::arbitrary(g) % 999999 + 1
2824
}
2925

30-
pub(crate) fn realistic_stake_dist(
31-
g: &mut Gen,
32-
total: u64,
33-
n: usize,
34-
alpha: f64,
35-
beta: f64,
36-
) -> Vec<Coin> {
37-
let rng = &mut StdRng::seed_from_u64(u64::arbitrary(g));
38-
let noise = Uniform::new(0.75, 1.25).unwrap();
39-
let curve = Beta::new(alpha, beta).unwrap();
40-
let cum: Vec<f64> = (0..n)
41-
.map(|i| curve.cdf((i as f64) / (total as f64)))
42-
.collect();
43-
let dif: Vec<f64> = (1..n)
44-
.map(|i| (cum[i] - cum[i - 1]) * noise.sample(rng))
45-
.collect();
46-
let scale: f64 = (total as f64) / dif.iter().sum::<f64>();
47-
dif.iter()
48-
.map(|coin| (scale * *coin).round() as Coin)
49-
.collect()
50-
}
51-
5226
pub fn arbitrary_stake_distribution(
5327
g: &mut Gen,
5428
total: u64,

crypto-benchmarks.rs/src/realism.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
use rand::prelude::Distribution;
2+
use rand::rngs::StdRng;
3+
use rand::SeedableRng;
4+
use pallas::ledger::primitives::Coin;
15
use quickcheck::{Arbitrary, Gen};
2-
3-
use crate::primitive::Coin;
6+
use statrs::distribution::ContinuousCDF;
7+
use statrs::distribution::{Beta, Uniform};
48

59
pub fn realistic_pool_count(g: &mut Gen) -> usize {
610
usize::arbitrary(g) % 1500 + 1500
@@ -13,3 +17,25 @@ pub fn realistic_total_stake(g: &mut Gen) -> Coin {
1317
pub fn realistic_voters(g: &mut Gen, pools: usize) -> usize {
1418
pools * (usize::arbitrary(g) % 500 + 500) / 1000
1519
}
20+
21+
pub(crate) fn realistic_stake_dist(
22+
g: &mut Gen,
23+
total: u64,
24+
n: usize,
25+
alpha: f64,
26+
beta: f64,
27+
) -> Vec<Coin> {
28+
let rng = &mut StdRng::seed_from_u64(u64::arbitrary(g));
29+
let noise = Uniform::new(0.75, 1.25).unwrap();
30+
let curve = Beta::new(alpha, beta).unwrap();
31+
let cum: Vec<f64> = (0..n)
32+
.map(|i| curve.cdf((i as f64) / (total as f64)))
33+
.collect();
34+
let dif: Vec<f64> = (1..n)
35+
.map(|i| (cum[i] - cum[i - 1]) * noise.sample(rng))
36+
.collect();
37+
let scale: f64 = (total as f64) / dif.iter().sum::<f64>();
38+
dif.iter()
39+
.map(|coin| (scale * *coin).round() as Coin)
40+
.collect()
41+
}

0 commit comments

Comments
 (0)