Refactor block_processing

- Add the rayon library for parallelization
- Move from std::sync::Rwlock to rayon::RwLock
- Add `state` field to BeaconChain
- Fix major bug in attestation validator where justified slot was
incorrectly looked up.
This commit is contained in:
Paul Hauner
2019-02-01 14:48:09 +11:00
parent b6b738e83a
commit 20e45b3369
20 changed files with 236 additions and 309 deletions

View File

@@ -1,64 +1,80 @@
use criterion::Criterion;
use criterion::{criterion_group, criterion_main};
use criterion::{black_box, criterion_group, criterion_main, Benchmark};
use env_logger::{Builder, Env};
use test_harness::BeaconChainHarness;
use types::ChainSpec;
use types::{ChainSpec, Hash256};
fn mid_epoch_state_transition(c: &mut Criterion) {
let validator_count = 2;
Builder::from_env(Env::default().default_filter_or("debug")).init();
let validator_count = 1000;
let mut rig = BeaconChainHarness::new(ChainSpec::foundation(), validator_count);
let two_and_half_epochs = (rig.spec.epoch_length * 2) + (rig.spec.epoch_length / 2);
let epoch_depth = (rig.spec.epoch_length * 2) + (rig.spec.epoch_length / 2);
for _ in 0..two_and_half_epochs {
for _ in 0..epoch_depth {
rig.advance_chain_with_block();
}
let block = rig.advance_chain_without_block();
let state = rig.beacon_chain.canonical_head().beacon_state.clone();
let state = rig.beacon_chain.state.read().clone();
assert!((state.slot + 1) % rig.spec.epoch_length != 0);
c.bench_function("mid-epoch state transition 10k validators", move |b| {
let block = block.clone();
let state = state.clone();
b.iter(|| {
rig.beacon_chain
.state_transition(state.clone(), &block.clone())
let mut state = state.clone();
black_box(state.per_slot_processing(Hash256::zero(), &rig.spec))
})
});
}
fn epoch_boundary_state_transition(c: &mut Criterion) {
let validator_count = 10_000;
// Builder::from_env(Env::default().default_filter_or("debug")).init();
let validator_count = 10000;
let mut rig = BeaconChainHarness::new(ChainSpec::foundation(), validator_count);
let three_epochs = rig.spec.epoch_length * 3;
let epoch_depth = rig.spec.epoch_length * 2;
for _ in 0..(three_epochs - 1) {
for _ in 0..(epoch_depth - 1) {
rig.advance_chain_with_block();
}
let state = rig.beacon_chain.canonical_head().beacon_state.clone();
assert_eq!(
state.slot % rig.spec.epoch_length,
rig.spec.epoch_length - 1,
);
let block = rig.advance_chain_without_block();
let state = rig.beacon_chain.state.read().clone();
c.bench_function("epoch boundary state transition 10k validators", move |b| {
let block = block.clone();
assert_eq!((state.slot + 1) % rig.spec.epoch_length, 0);
c.bench(
"routines",
Benchmark::new("routine_1", move |b| {
let state = state.clone();
b.iter(|| {
let mut state = state.clone();
black_box(black_box(
state.per_slot_processing(Hash256::zero(), &rig.spec),
))
})
})
.sample_size(5),
);
/*
c.bench_function("mid-epoch state transition 10k validators", move |b| {
let state = state.clone();
b.iter(|| {
let state = rig
.beacon_chain
.state_transition(state.clone(), &block.clone())
.unwrap();
assert_eq!(state.slot % rig.spec.epoch_length, 0);
let mut state = state.clone();
black_box(black_box(
state.per_slot_processing(Hash256::zero(), &rig.spec),
))
})
});
*/
}
criterion_group!(
benches,
mid_epoch_state_transition,
// mid_epoch_state_transition,
epoch_boundary_state_transition
);
criterion_main!(benches);

View File

@@ -113,6 +113,7 @@ impl BeaconChainHarness {
debug!("Incrementing BeaconChain slot to {}.", slot);
self.beacon_chain.slot_clock.set_slot(slot);
self.beacon_chain.advance_state(slot).unwrap();
slot
}
@@ -125,8 +126,8 @@ impl BeaconChainHarness {
let attesting_validators = self
.beacon_chain
.state(present_slot)
.unwrap()
.state
.read()
.get_crosslink_committees_at_slot(present_slot, &self.spec)
.unwrap()
.iter()
@@ -195,13 +196,27 @@ impl BeaconChainHarness {
self.beacon_chain.process_block(block).unwrap();
debug!("...block processed by BeaconChain.");
debug!("Producing free attestations...");
// Produce new attestations.
let free_attestations = self.gather_free_attesations();
debug!("Processing free attestations...");
free_attestations.par_iter().for_each(|free_attestation| {
self.beacon_chain
.process_free_attestation(free_attestation.clone())
.unwrap();
});
debug!("Free attestations processed.");
/*
for free_attestation in free_attestations {
self.beacon_chain
.process_free_attestation(free_attestation)
.unwrap();
}
*/
}
pub fn chain_dump(&self) -> Result<Vec<SlotDump>, DumpError> {

View File

@@ -13,10 +13,10 @@ where
{
fn produce_attestation_data(
&self,
slot: u64,
_slot: u64,
shard: u64,
) -> Result<Option<AttestationData>, NodeError> {
match self.beacon_chain.produce_attestation_data(slot, shard) {
match self.beacon_chain.produce_attestation_data(shard) {
Ok(attestation_data) => Ok(Some(attestation_data)),
Err(e) => Err(NodeError::RemoteFailure(format!("{:?}", e))),
}

View File

@@ -2,6 +2,7 @@ use attester::{Attester, Error as AttestationPollError};
use beacon_chain::BeaconChain;
use block_producer::{BlockProducer, Error as BlockPollError};
use db::MemoryDB;
use log::trace;
use signer::TestSigner;
use slot_clock::TestingSlotClock;
use std::sync::Arc;

View File

@@ -24,7 +24,7 @@ fn it_can_build_on_genesis_block() {
fn it_can_produce_past_first_epoch_boundary() {
Builder::from_env(Env::default().default_filter_or("debug")).init();
let validator_count = 100;
let validator_count = 128 * 1024;
debug!("Starting harness build...");