Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement proper mechanism for sampling superblock signing committees in V2_0 #2565

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions data_structures/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4888,7 +4888,7 @@ mod tests {

use crate::{
proto::versioning::{ProtocolVersion, VersionedHashable},
superblock::{mining_build_superblock, ARSIdentities},
superblock::{mining_build_superblock, Census},
transaction::{CommitTransactionBody, RevealTransactionBody, VTTransactionBody},
};

Expand Down Expand Up @@ -6829,7 +6829,7 @@ mod tests {

let expected_order = vec![p1_bls, p2_bls, p3_bls];
let ordered_identities = rep_engine.get_rep_ordered_ars_list();
let ars_identities = ARSIdentities::new(ordered_identities);
let ars_identities = Census::new(ordered_identities);

assert_eq!(
expected_order,
Expand Down Expand Up @@ -6871,7 +6871,7 @@ mod tests {

let expected_order = vec![p1_bls, p2_bls, p3_bls];
let ordered_identities = rep_engine.get_rep_ordered_ars_list();
let ars_identities = ARSIdentities::new(ordered_identities);
let ars_identities = Census::new(ordered_identities);

assert_eq!(
expected_order,
Expand Down Expand Up @@ -6929,7 +6929,7 @@ mod tests {

let expected_order = vec![p1_bls, p2_bls, p4_bls, p5_bls, p3_bls];
let ordered_identities = rep_engine.get_rep_ordered_ars_list();
let ars_identities = ARSIdentities::new(ordered_identities);
let ars_identities = Census::new(ordered_identities);

assert_eq!(
expected_order,
Expand Down
16 changes: 0 additions & 16 deletions data_structures/src/staking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,22 +287,6 @@ pub struct CoinsAndAddresses<Coins, Address> {
pub addresses: StakeKey<Address>,
}

/// Allows telling the `census` method in `Stakes` to source addresses from its internal `by_coins`
/// following different strategies.
#[repr(u8)]
#[derive(Clone, Copy, Debug)]
pub enum CensusStrategy {
/// Retrieve all addresses, ordered by decreasing power.
All = 0,
/// Retrieve every Nth address, ordered by decreasing power.
StepBy(usize) = 1,
/// Retrieve the most powerful N addresses, ordered by decreasing power.
Take(usize) = 2,
/// Retrieve a total of N addresses, evenly distributed from the index, ordered by decreasing
/// power.
Evenly(usize) = 3,
}

impl<const UNIT: u8, Address, Coins, Epoch, Nonce, Power> Serialize
for Stakes<UNIT, Address, Coins, Epoch, Nonce, Power>
where
Expand Down
45 changes: 17 additions & 28 deletions data_structures/src/staking/stakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,34 +260,23 @@ where
self.by_key.len()
}

/// Obtain a list of stakers, conveniently ordered by one of several strategies.
///
/// ## Strategies
///
/// - `All`: retrieve all addresses, ordered by decreasing power.
/// - `StepBy`: retrieve every Nth address, ordered by decreasing power.
/// - `Take`: retrieve the most powerful N addresses, ordered by decreasing power.
/// - `Evenly`: retrieve a total of N addresses, evenly distributed from the index, ordered by
/// decreasing power.
pub fn census(
&self,
capability: Capability,
epoch: Epoch,
strategy: CensusStrategy,
) -> Box<dyn Iterator<Item = StakeKey<Address>> + '_> {
let iterator = self.by_rank(capability, epoch).map(|(address, _)| address);

match strategy {
CensusStrategy::All => Box::new(iterator),
CensusStrategy::StepBy(step) => Box::new(iterator.step_by(step)),
CensusStrategy::Take(head) => Box::new(iterator.take(head)),
CensusStrategy::Evenly(count) => {
let collected = iterator.collect::<Vec<_>>();
let step = collected.len() / count;

Box::new(collected.into_iter().step_by(step).take(count))
}
}
/// Quickly count how many different validators are recorded into this data structure.
pub fn validators_count(&self) -> usize {
self.by_validator.len()
}

/// Retrieve all validators, ordered by the total amount of stake that they operate.
pub fn validators_by_stake(&self) -> impl Iterator<Item = (Address, Coins)> + Clone + '_ {
self.by_validator
.iter()
.map(|(address, entries)| {
let coins = entries.iter().fold(Coins::zero(), |acc, entry| {
acc + entry.value.read().unwrap().coins
});

(address.clone(), coins)
})
.sorted_by_key(|(_, coins)| *coins)
}

/// Tells what is the power of an identity in the network on a certain epoch.
Expand Down
Loading
Loading