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

Track the most recently active validators and sample the superblock committee from them #2448

Open
wants to merge 2 commits into
base: 2.0-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
14 changes: 7 additions & 7 deletions data_structures/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4592,7 +4592,7 @@ mod tests {

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

Expand Down Expand Up @@ -6473,11 +6473,11 @@ 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 validator_identities = ValidatorIdentities::new(ordered_identities);

assert_eq!(
expected_order,
ars_identities.get_rep_ordered_bn256_list(&alt_keys)
validator_identities.get_ordered_bn256_list(&alt_keys)
);
}

Expand Down Expand Up @@ -6515,11 +6515,11 @@ 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 validator_identities = ValidatorIdentities::new(ordered_identities);

assert_eq!(
expected_order,
ars_identities.get_rep_ordered_bn256_list(&alt_keys)
validator_identities.get_ordered_bn256_list(&alt_keys)
);
}

Expand Down Expand Up @@ -6573,11 +6573,11 @@ 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 validator_identities = ValidatorIdentities::new(ordered_identities);

assert_eq!(
expected_order,
ars_identities.get_rep_ordered_bn256_list(&alt_keys)
validator_identities.get_ordered_bn256_list(&alt_keys)
);
}

Expand Down
51 changes: 47 additions & 4 deletions data_structures/src/staking/stakes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::{btree_map::Entry, BTreeMap},
cmp::PartialOrd,
collections::{btree_map::Entry, BTreeMap, HashSet},
fmt::{Debug, Display},
ops::{Add, Div, Mul, Sub},
};
Expand Down Expand Up @@ -80,6 +81,9 @@ where
/// it never gets persisted and rather always read from constants, or hide the field and the related method
/// behind a #[test] thing.
minimum_stakeable: Option<Coins>,
/// A listing of all active validators (mined a block or solved a data request) indexed by the validator address
/// and the epoch of when it was active as value
by_active: BTreeMap<Address, Epoch>,
}

impl<Address, Coins, Epoch, Power> Stakes<Address, Coins, Epoch, Power>
Expand Down Expand Up @@ -108,7 +112,8 @@ where
+ Debug
+ Display
+ Send
+ Sync,
+ Sync
+ PartialOrd,
Power: Copy + Default + Ord + Add<Output = Power> + Div<Output = Power>,
u64: From<Coins> + From<Power>,
{
Expand Down Expand Up @@ -251,6 +256,7 @@ where
// No need to keep the entry if the stake has gone to zero
if final_coins.is_zero() {
by_address_entry.remove();
self.by_active.remove(&key.validator);
self.by_coins.remove(&CoinsAndAddresses {
coins: initial_coins,
addresses: key,
Expand Down Expand Up @@ -311,6 +317,41 @@ where
}
}

/// Return the total number of validators.
pub fn validator_count(&self, active_since: Option<Epoch>) -> usize {
match active_since {
None => self.by_active.len(),
Some(epoch) => {
let mut count = 0;
for (_, active) in self.by_active.iter() {
if *active >= epoch {
count += 1;
}
}
count
}
}
}

/// Get a vector of all validator that were active since a given epoch
pub fn get_active_validators(&self, active_since: Epoch) -> Vec<Address> {
self.by_active
.iter()
.filter(|(_, epoch)| **epoch >= active_since)
.map(|(address, _)| address.clone())
.collect()
}

/// Update the active epoch of the validators passed as an argument
pub fn update_active_epoch(&mut self, validators: HashSet<Address>, active: Epoch) {
for validator in validators {
self.by_active
.entry(validator)
.and_modify(|validator| *validator = active)
.or_insert(active);
}
}

/// Query stakes by stake key.
#[inline(always)]
fn query_by_key(&self, key: StakeKey<Address>) -> StakingResult<Coins, Address, Coins, Epoch> {
Expand Down Expand Up @@ -372,7 +413,8 @@ where
+ Debug
+ Display
+ Send
+ Sync,
+ Sync
+ PartialOrd,
Power: Add<Output = Power> + Copy + Default + Div<Output = Power> + Ord + Debug,
Wit: Mul<Epoch, Output = Power>,
u64: From<Wit> + From<Power>,
Expand Down Expand Up @@ -419,7 +461,8 @@ where
+ Debug
+ Send
+ Sync
+ Display,
+ Display
+ PartialOrd,
Power: Add<Output = Power> + Copy + Default + Div<Output = Power> + Ord + Debug,
Wit: Mul<Epoch, Output = Power>,
u64: From<Wit> + From<Power>,
Expand Down
Loading
Loading