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 a9cc9f4 commit 60f09ff
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
2 changes: 2 additions & 0 deletions ethereum-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ license = "MIT OR Apache-2.0"
default = ["serde", "async"]
serde = ["hex", "serde_json", "serde_yaml"]
async = ["tokio", "tokio-stream", "async-stream"]
optimized = ["shuffling"]
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
2 changes: 1 addition & 1 deletion ethereum-consensus/src/altair/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,8 +1270,8 @@ pub fn compute_committee(
context: &Context,
) -> Result<Vec<ValidatorIndex>> {
if cfg!(feature = "shuffling") {
let index_count = indices.len();
let shuffled_indices = compute_shuffled_indices(indices, seed, context);
let index_count = indices.len();
let start = index_count * index / count;
let end = index_count * (index + 1) / count;
let committee = shuffled_indices[start..end].to_vec();
Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/bellatrix/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2223,8 +2223,8 @@ pub fn compute_committee(
context: &Context,
) -> Result<Vec<ValidatorIndex>> {
if cfg!(feature = "shuffling") {
let index_count = indices.len();
let shuffled_indices = compute_shuffled_indices(indices, seed, context);
let index_count = indices.len();
let start = index_count * index / count;
let end = index_count * (index + 1) / count;
let committee = shuffled_indices[start..end].to_vec();
Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/capella/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2486,8 +2486,8 @@ pub fn compute_committee(
context: &Context,
) -> Result<Vec<ValidatorIndex>> {
if cfg!(feature = "shuffling") {
let index_count = indices.len();
let shuffled_indices = compute_shuffled_indices(indices, seed, context);
let index_count = indices.len();
let start = index_count * index / count;
let end = index_count * (index + 1) / count;
let committee = shuffled_indices[start..end].to_vec();
Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/deneb/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2557,8 +2557,8 @@ pub fn compute_committee(
context: &Context,
) -> Result<Vec<ValidatorIndex>> {
if cfg!(feature = "shuffling") {
let index_count = indices.len();
let shuffled_indices = compute_shuffled_indices(indices, seed, context);
let index_count = indices.len();
let start = index_count * index / count;
let end = index_count * (index + 1) / count;
let committee = shuffled_indices[start..end].to_vec();
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 shuffled_indices = compute_shuffled_indices(indices, seed, context);
let index_count = indices.len();
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 60f09ff

Please sign in to comment.