Skip to content

Commit

Permalink
use optimized shuffling in compute_committee and add feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed Mar 27, 2024
1 parent a884634 commit da13a70
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
4 changes: 3 additions & 1 deletion ethereum-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ license = "MIT OR Apache-2.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["serde", "async"]
default = ["serde", "async", "optimized"]
serde = ["hex", "serde_json", "serde_yaml"]
async = ["tokio", "tokio-stream", "async-stream"]
optimized = []
shuffling = [] # supports optimized shuffling routines
secret-key-debug = [
] # enable if you want to be able to print `crypto::SecretKey`
spec-tests = [] # enable extra features for testing
Expand Down
23 changes: 16 additions & 7 deletions ethereum-consensus/src/phase0/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,23 @@ pub fn compute_committee(
count: usize,
context: &Context,
) -> Result<Vec<ValidatorIndex>> {
let start = (indices.len() * index) / count;
let end = (indices.len()) * (index + 1) / count;
let mut committee = vec![0usize; end - start];
for i in start..end {
let index = compute_shuffled_index(i, indices.len(), seed, context)?;
committee[i - start] = indices[index];
if cfg!(feature = "shuffling") {
let index_count = indices.len();
let shuffled_indices = compute_shuffled_indices(indices, seed, context);
let start = index_count * index / count;
let end = index_count * (index + 1) / count;
let committee = shuffled_indices[start..end].to_vec();
Ok(committee)
} else {
let start = indices.len() * index / count;
let end = indices.len() * (index + 1) / count;
let mut committee = vec![0usize; end - start];
for i in start..end {
let index = compute_shuffled_index(i, indices.len(), seed, context)?;
committee[i - start] = indices[index];
}
Ok(committee)
}
Ok(committee)
}

pub fn compute_epoch_at_slot(slot: Slot, context: &Context) -> Epoch {
Expand Down

0 comments on commit da13a70

Please sign in to comment.