From b6b738e83a71c8c70e04abb483d2cd832f65bb29 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 31 Jan 2019 20:27:15 +1100 Subject: [PATCH] Improve test_harness efficiency for attesatations Reduce the amount of attester.poll() calls by using a hashset --- .../beacon_chain/test_harness/src/harness.rs | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/beacon_node/beacon_chain/test_harness/src/harness.rs b/beacon_node/beacon_chain/test_harness/src/harness.rs index 0b493b64a..3cdfc8024 100644 --- a/beacon_node/beacon_chain/test_harness/src/harness.rs +++ b/beacon_node/beacon_chain/test_harness/src/harness.rs @@ -8,8 +8,10 @@ use db::{ use log::debug; use rayon::prelude::*; use slot_clock::TestingSlotClock; +use std::collections::HashSet; use std::fs::File; use std::io::prelude::*; +use std::iter::FromIterator; use std::sync::Arc; use types::{BeaconBlock, ChainSpec, FreeAttestation, Keypair, Validator}; @@ -121,15 +123,34 @@ impl BeaconChainHarness { pub fn gather_free_attesations(&mut self) -> Vec { let present_slot = self.beacon_chain.present_slot().unwrap(); + let attesting_validators = self + .beacon_chain + .state(present_slot) + .unwrap() + .get_crosslink_committees_at_slot(present_slot, &self.spec) + .unwrap() + .iter() + .fold(vec![], |mut acc, (committee, _slot)| { + acc.append(&mut committee.clone()); + acc + }); + let attesting_validators: HashSet = + HashSet::from_iter(attesting_validators.iter().cloned()); + let free_attestations: Vec = self .validators .par_iter_mut() - .filter_map(|validator| { - // Advance the validator slot. - validator.set_slot(present_slot); + .enumerate() + .filter_map(|(i, validator)| { + if attesting_validators.contains(&i) { + // Advance the validator slot. + validator.set_slot(present_slot); - // Prompt the validator to produce an attestation (if required). - validator.produce_free_attestation().ok() + // Prompt the validator to produce an attestation (if required). + validator.produce_free_attestation().ok() + } else { + None + } }) .collect();