Update Attesters struct

- Renames variables
- Moves total balance calculation into struct
This commit is contained in:
Paul Hauner 2019-03-14 12:49:48 +11:00
parent a319144835
commit 95599ddc66
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
3 changed files with 88 additions and 192 deletions

View File

@ -4,14 +4,13 @@ use ssz::TreeHash;
use state_processing::{ use state_processing::{
per_epoch_processing, per_epoch_processing,
per_epoch_processing::{ per_epoch_processing::{
calculate_active_validator_indices, calculate_attester_sets, clean_attestations, calculate_attester_sets, clean_attestations, process_crosslinks, process_eth1_data,
process_crosslinks, process_eth1_data, process_justification, process_justification, process_rewards_and_penalities, process_validator_registry,
process_rewards_and_penalities, process_validator_registry, update_active_tree_index_roots, update_active_tree_index_roots, update_latest_slashed_balances,
update_latest_slashed_balances,
}, },
}; };
use types::test_utils::TestingBeaconStateBuilder; use types::test_utils::TestingBeaconStateBuilder;
use types::{validator_registry::get_active_validator_indices, *}; use types::*;
pub const BENCHING_SAMPLE_SIZE: usize = 10; pub const BENCHING_SAMPLE_SIZE: usize = 10;
pub const SMALL_BENCHING_SAMPLE_SIZE: usize = 10; pub const SMALL_BENCHING_SAMPLE_SIZE: usize = 10;
@ -73,64 +72,6 @@ pub fn bench_epoch_processing_n_validators(c: &mut Criterion, validator_count: u
/// ///
/// `desc` will be added to the title of each bench. /// `desc` will be added to the title of each bench.
fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSpec, desc: &str) { fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSpec, desc: &str) {
let state_clone = state.clone();
let spec_clone = spec.clone();
c.bench(
&format!("{}/epoch_processing", desc),
Benchmark::new("calculate_active_validator_indices", move |b| {
b.iter_batched(
|| state_clone.clone(),
|mut state| {
calculate_active_validator_indices(&mut state, &spec_clone);
state
},
criterion::BatchSize::SmallInput,
)
})
.sample_size(BENCHING_SAMPLE_SIZE),
);
let state_clone = state.clone();
let spec_clone = spec.clone();
let active_validator_indices = calculate_active_validator_indices(&state, &spec);
c.bench(
&format!("{}/epoch_processing", desc),
Benchmark::new("calculate_current_total_balance", move |b| {
b.iter_batched(
|| state_clone.clone(),
|state| {
state.get_total_balance(&active_validator_indices[..], &spec_clone);
state
},
criterion::BatchSize::SmallInput,
)
})
.sample_size(BENCHING_SAMPLE_SIZE),
);
let state_clone = state.clone();
let spec_clone = spec.clone();
c.bench(
&format!("{}/epoch_processing", desc),
Benchmark::new("calculate_previous_total_balance", move |b| {
b.iter_batched(
|| state_clone.clone(),
|state| {
state.get_total_balance(
&get_active_validator_indices(
&state.validator_registry,
state.previous_epoch(&spec_clone),
)[..],
&spec_clone,
);
state
},
criterion::BatchSize::SmallInput,
)
})
.sample_size(BENCHING_SAMPLE_SIZE),
);
let state_clone = state.clone(); let state_clone = state.clone();
let spec_clone = spec.clone(); let spec_clone = spec.clone();
c.bench( c.bench(
@ -150,15 +91,13 @@ fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSp
let state_clone = state.clone(); let state_clone = state.clone();
let spec_clone = spec.clone(); let spec_clone = spec.clone();
let active_validator_indices = calculate_active_validator_indices(&state, &spec);
c.bench( c.bench(
&format!("{}/epoch_processing", desc), &format!("{}/epoch_processing", desc),
Benchmark::new("calculate_attester_sets", move |b| { Benchmark::new("calculate_attester_sets", move |b| {
b.iter_batched( b.iter_batched(
|| state_clone.clone(), || state_clone.clone(),
|mut state| { |mut state| {
calculate_attester_sets(&mut state, &active_validator_indices, &spec_clone) calculate_attester_sets(&mut state, &spec_clone).unwrap();
.unwrap();
state state
}, },
criterion::BatchSize::SmallInput, criterion::BatchSize::SmallInput,
@ -169,14 +108,7 @@ fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSp
let state_clone = state.clone(); let state_clone = state.clone();
let spec_clone = spec.clone(); let spec_clone = spec.clone();
let previous_epoch = state.previous_epoch(&spec); let attesters = calculate_attester_sets(&state, &spec).unwrap();
let active_validator_indices = calculate_active_validator_indices(&state, &spec);
let attesters = calculate_attester_sets(&state, &active_validator_indices, &spec).unwrap();
let current_total_balance = state.get_total_balance(&active_validator_indices[..], &spec);
let previous_total_balance = state.get_total_balance(
&get_active_validator_indices(&state.validator_registry, previous_epoch)[..],
&spec,
);
c.bench( c.bench(
&format!("{}/epoch_processing", desc), &format!("{}/epoch_processing", desc),
Benchmark::new("process_justification", move |b| { Benchmark::new("process_justification", move |b| {
@ -185,10 +117,10 @@ fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSp
|mut state| { |mut state| {
process_justification( process_justification(
&mut state, &mut state,
current_total_balance, attesters.balances.current_epoch_total,
previous_total_balance, attesters.balances.previous_epoch_total,
attesters.balances.previous_epoch_boundary, attesters.balances.previous_epoch_boundary_attesters,
attesters.balances.current_epoch_boundary, attesters.balances.current_epoch_boundary_attesters,
&spec_clone, &spec_clone,
); );
state state
@ -215,13 +147,7 @@ fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSp
let mut state_clone = state.clone(); let mut state_clone = state.clone();
let spec_clone = spec.clone(); let spec_clone = spec.clone();
let previous_epoch = state.previous_epoch(&spec); let attesters = calculate_attester_sets(&state, &spec).unwrap();
let active_validator_indices = calculate_active_validator_indices(&state, &spec);
let attesters = calculate_attester_sets(&state, &active_validator_indices, &spec).unwrap();
let previous_total_balance = state.get_total_balance(
&get_active_validator_indices(&state.validator_registry, previous_epoch)[..],
&spec,
);
let winning_root_for_shards = process_crosslinks(&mut state_clone, &spec).unwrap(); let winning_root_for_shards = process_crosslinks(&mut state_clone, &spec).unwrap();
c.bench( c.bench(
&format!("{}/epoch_processing", desc), &format!("{}/epoch_processing", desc),
@ -232,7 +158,6 @@ fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSp
process_rewards_and_penalities( process_rewards_and_penalities(
&mut state, &mut state,
&mut attesters, &mut attesters,
previous_total_balance,
&winning_root_for_shards, &winning_root_for_shards,
&spec_clone, &spec_clone,
) )
@ -262,32 +187,8 @@ fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSp
.sample_size(BENCHING_SAMPLE_SIZE), .sample_size(BENCHING_SAMPLE_SIZE),
); );
let mut state_clone = state.clone(); let state_clone = state.clone();
let spec_clone = spec.clone(); let spec_clone = spec.clone();
let previous_epoch = state.previous_epoch(&spec);
let active_validator_indices = calculate_active_validator_indices(&state, &spec);
let attesters = calculate_attester_sets(&state, &active_validator_indices, &spec).unwrap();
let current_total_balance = state.get_total_balance(&active_validator_indices[..], spec);
let previous_total_balance = state.get_total_balance(
&get_active_validator_indices(&state.validator_registry, previous_epoch)[..],
&spec,
);
assert_eq!(
state_clone.finalized_epoch, state_clone.validator_registry_update_epoch,
"The last registry update should be at the last finalized epoch."
);
process_justification(
&mut state_clone,
current_total_balance,
previous_total_balance,
attesters.balances.previous_epoch_boundary,
attesters.balances.current_epoch_boundary,
spec,
);
assert!(
state_clone.finalized_epoch > state_clone.validator_registry_update_epoch,
"The state should have been finalized."
);
c.bench( c.bench(
&format!("{}/epoch_processing", desc), &format!("{}/epoch_processing", desc),
Benchmark::new("process_validator_registry", move |b| { Benchmark::new("process_validator_registry", move |b| {

View File

@ -26,32 +26,21 @@ pub type WinningRootHashSet = HashMap<u64, WinningRoot>;
/// ///
/// Spec v0.4.0 /// Spec v0.4.0
pub fn per_epoch_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> { pub fn per_epoch_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> {
let previous_epoch = state.previous_epoch(spec);
// Ensure all of the caches are built. // Ensure all of the caches are built.
state.build_epoch_cache(RelativeEpoch::Previous, spec)?; state.build_epoch_cache(RelativeEpoch::Previous, spec)?;
state.build_epoch_cache(RelativeEpoch::Current, spec)?; state.build_epoch_cache(RelativeEpoch::Current, spec)?;
state.build_epoch_cache(RelativeEpoch::Next, spec)?; state.build_epoch_cache(RelativeEpoch::Next, spec)?;
let active_validator_indices = calculate_active_validator_indices(&state, spec); let mut attesters = calculate_attester_sets(&state, spec)?;
let current_total_balance = state.get_total_balance(&active_validator_indices[..], spec);
let previous_total_balance = state.get_total_balance(
&get_active_validator_indices(&state.validator_registry, previous_epoch)[..],
spec,
);
let mut attesters = calculate_attester_sets(&state, &active_validator_indices, spec)?;
process_eth1_data(state, spec); process_eth1_data(state, spec);
process_justification( process_justification(
state, state,
current_total_balance, attesters.balances.current_epoch_total,
previous_total_balance, attesters.balances.previous_epoch_total,
attesters.balances.previous_epoch_boundary, attesters.balances.previous_epoch_boundary_attesters,
attesters.balances.current_epoch_boundary, attesters.balances.current_epoch_boundary_attesters,
spec, spec,
); );
@ -59,13 +48,7 @@ pub fn per_epoch_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result
let winning_root_for_shards = process_crosslinks(state, spec)?; let winning_root_for_shards = process_crosslinks(state, spec)?;
// Rewards and Penalities // Rewards and Penalities
process_rewards_and_penalities( process_rewards_and_penalities(state, &mut attesters, &winning_root_for_shards, spec)?;
state,
&mut attesters,
previous_total_balance,
&winning_root_for_shards,
spec,
)?;
// Ejections // Ejections
state.process_ejections(spec); state.process_ejections(spec);
@ -104,12 +87,12 @@ pub fn calculate_active_validator_indices(state: &BeaconState, spec: &ChainSpec)
/// Spec v0.4.0 /// Spec v0.4.0
pub fn calculate_attester_sets( pub fn calculate_attester_sets(
state: &BeaconState, state: &BeaconState,
active_validator_indices: &[usize],
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<Attesters, BeaconStateError> { ) -> Result<Attesters, BeaconStateError> {
let mut attesters = Attesters::empty(state.validator_registry.len()); let mut attesters = Attesters::new(state, spec);
attesters.process_active_validator_indices(&active_validator_indices);
attesters.process_attestations(&state, &state.latest_attestations, spec)?; attesters.process_attestations(&state, &state.latest_attestations, spec)?;
Ok(attesters) Ok(attesters)
} }
@ -285,12 +268,13 @@ pub fn process_crosslinks(
pub fn process_rewards_and_penalities( pub fn process_rewards_and_penalities(
state: &mut BeaconState, state: &mut BeaconState,
attesters: &mut Attesters, attesters: &mut Attesters,
previous_total_balance: u64,
winning_root_for_shards: &WinningRootHashSet, winning_root_for_shards: &WinningRootHashSet,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<(), Error> { ) -> Result<(), Error> {
let next_epoch = state.next_epoch(spec); let next_epoch = state.next_epoch(spec);
let previous_total_balance = attesters.balances.previous_epoch_total;
let base_reward_quotient = previous_total_balance.integer_sqrt() / spec.base_reward_quotient; let base_reward_quotient = previous_total_balance.integer_sqrt() / spec.base_reward_quotient;
if base_reward_quotient == 0 { if base_reward_quotient == 0 {
@ -317,34 +301,35 @@ pub fn process_rewards_and_penalities(
if epochs_since_finality <= 4 { if epochs_since_finality <= 4 {
// Expected FFG source // Expected FFG source
if status.is_previous_epoch { if status.is_previous_epoch_attester {
safe_add_assign!( safe_add_assign!(
balance, balance,
base_reward * attesters.balances.previous_epoch / previous_total_balance base_reward * attesters.balances.previous_epoch_attesters
/ previous_total_balance
); );
} else if status.is_active { } else if status.is_active_in_previous_epoch {
safe_sub_assign!(balance, base_reward); safe_sub_assign!(balance, base_reward);
} }
// Expected FFG target // Expected FFG target
if status.is_previous_epoch_boundary { if status.is_previous_epoch_boundary_attester {
safe_add_assign!( safe_add_assign!(
balance, balance,
base_reward * attesters.balances.previous_epoch_boundary base_reward * attesters.balances.previous_epoch_boundary_attesters
/ previous_total_balance / previous_total_balance
); );
} else if status.is_active { } else if status.is_active_in_previous_epoch {
safe_sub_assign!(balance, base_reward); safe_sub_assign!(balance, base_reward);
} }
// Expected beacon chain head // Expected beacon chain head
if status.is_previous_epoch_head { if status.is_previous_epoch_head_attester {
safe_add_assign!( safe_add_assign!(
balance, balance,
base_reward * attesters.balances.previous_epoch_head base_reward * attesters.balances.previous_epoch_head_attesters
/ previous_total_balance / previous_total_balance
); );
} else if status.is_active { } else if status.is_active_in_previous_epoch {
safe_sub_assign!(balance, base_reward); safe_sub_assign!(balance, base_reward);
}; };
} else { } else {
@ -355,14 +340,14 @@ pub fn process_rewards_and_penalities(
spec, spec,
); );
if status.is_active { if status.is_active_in_previous_epoch {
if !status.is_previous_epoch { if !status.is_previous_epoch_attester {
safe_sub_assign!(balance, inactivity_penalty); safe_sub_assign!(balance, inactivity_penalty);
} }
if !status.is_previous_epoch_boundary { if !status.is_previous_epoch_boundary_attester {
safe_sub_assign!(balance, inactivity_penalty); safe_sub_assign!(balance, inactivity_penalty);
} }
if !status.is_previous_epoch_head { if !status.is_previous_epoch_head_attester {
safe_sub_assign!(balance, inactivity_penalty); safe_sub_assign!(balance, inactivity_penalty);
} }
@ -393,7 +378,7 @@ pub fn process_rewards_and_penalities(
for (index, _validator) in state.validator_registry.iter().enumerate() { for (index, _validator) in state.validator_registry.iter().enumerate() {
let status = &attesters.statuses[index]; let status = &attesters.statuses[index];
if status.is_previous_epoch { if status.is_previous_epoch_attester {
let proposer_index = status.inclusion_info.proposer_index; let proposer_index = status.inclusion_info.proposer_index;
let inclusion_distance = status.inclusion_info.distance; let inclusion_distance = status.inclusion_info.distance;

View File

@ -42,28 +42,31 @@ impl InclusionInfo {
#[derive(Default, Clone)] #[derive(Default, Clone)]
pub struct AttesterStatus { pub struct AttesterStatus {
pub is_active: bool, pub is_active_in_current_epoch: bool,
pub is_active_in_previous_epoch: bool,
pub is_current_epoch: bool, pub is_current_epoch_attester: bool,
pub is_current_epoch_boundary: bool, pub is_current_epoch_boundary_attester: bool,
pub is_previous_epoch: bool, pub is_previous_epoch_attester: bool,
pub is_previous_epoch_boundary: bool, pub is_previous_epoch_boundary_attester: bool,
pub is_previous_epoch_head: bool, pub is_previous_epoch_head_attester: bool,
pub inclusion_info: InclusionInfo, pub inclusion_info: InclusionInfo,
pub winning_root_info: Option<WinningRootInfo>, pub winning_root_info: Option<WinningRootInfo>,
} }
impl AttesterStatus { impl AttesterStatus {
/// Note: does not update the winning root info.
pub fn update(&mut self, other: &Self) { pub fn update(&mut self, other: &Self) {
// Update all the bool fields, only updating `self` if `other` is true (never setting // Update all the bool fields, only updating `self` if `other` is true (never setting
// `self` to false). // `self` to false).
set_self_if_other_is_true!(self, other, is_active); set_self_if_other_is_true!(self, other, is_active_in_current_epoch);
set_self_if_other_is_true!(self, other, is_current_epoch); set_self_if_other_is_true!(self, other, is_active_in_previous_epoch);
set_self_if_other_is_true!(self, other, is_current_epoch_boundary); set_self_if_other_is_true!(self, other, is_current_epoch_attester);
set_self_if_other_is_true!(self, other, is_previous_epoch); set_self_if_other_is_true!(self, other, is_current_epoch_boundary_attester);
set_self_if_other_is_true!(self, other, is_previous_epoch_boundary); set_self_if_other_is_true!(self, other, is_previous_epoch_attester);
set_self_if_other_is_true!(self, other, is_previous_epoch_head); set_self_if_other_is_true!(self, other, is_previous_epoch_boundary_attester);
set_self_if_other_is_true!(self, other, is_previous_epoch_head_attester);
self.inclusion_info.update(&other.inclusion_info); self.inclusion_info.update(&other.inclusion_info);
} }
@ -71,11 +74,13 @@ impl AttesterStatus {
#[derive(Default, Clone)] #[derive(Default, Clone)]
pub struct TotalBalances { pub struct TotalBalances {
pub current_epoch: u64, pub current_epoch_total: u64,
pub current_epoch_boundary: u64, pub previous_epoch_total: u64,
pub previous_epoch: u64, pub current_epoch_attesters: u64,
pub previous_epoch_boundary: u64, pub current_epoch_boundary_attesters: u64,
pub previous_epoch_head: u64, pub previous_epoch_attesters: u64,
pub previous_epoch_boundary_attesters: u64,
pub previous_epoch_head_attesters: u64,
} }
#[derive(Clone)] #[derive(Clone)]
@ -85,22 +90,27 @@ pub struct Attesters {
} }
impl Attesters { impl Attesters {
pub fn empty(num_validators: usize) -> Self { pub fn new(state: &BeaconState, spec: &ChainSpec) -> Self {
Self { let mut statuses = Vec::with_capacity(state.validator_registry.len());
statuses: vec![AttesterStatus::default(); num_validators], let mut balances = TotalBalances::default();
balances: TotalBalances::default(),
}
}
pub fn process_active_validator_indices(&mut self, active_validator_indices: &[usize]) { for (i, validator) in state.validator_registry.iter().enumerate() {
let status = AttesterStatus { let mut status = AttesterStatus::default();
is_active: true,
..AttesterStatus::default()
};
for &i in active_validator_indices { if validator.is_active_at(state.current_epoch(spec)) {
self.statuses[i].update(&status); status.is_active_in_current_epoch = true;
balances.current_epoch_total += state.get_effective_balance(i, spec);
}
if validator.is_active_at(state.previous_epoch(spec)) {
status.is_active_in_previous_epoch = true;
balances.previous_epoch_total += state.get_effective_balance(i, spec);
}
statuses.push(status);
} }
Self { statuses, balances }
} }
pub fn process_attestations( pub fn process_attestations(
@ -119,16 +129,16 @@ impl Attesters {
// Profile this attestation, updating the total balances and generating an // Profile this attestation, updating the total balances and generating an
// `AttesterStatus` object that applies to all participants in the attestation. // `AttesterStatus` object that applies to all participants in the attestation.
if is_from_epoch(a, state.current_epoch(spec), spec) { if is_from_epoch(a, state.current_epoch(spec), spec) {
self.balances.current_epoch += attesting_balance; self.balances.current_epoch_attesters += attesting_balance;
status.is_current_epoch = true; status.is_current_epoch_attester = true;
if has_common_epoch_boundary_root(a, state, state.current_epoch(spec), spec)? { if has_common_epoch_boundary_root(a, state, state.current_epoch(spec), spec)? {
self.balances.current_epoch_boundary += attesting_balance; self.balances.current_epoch_boundary_attesters += attesting_balance;
status.is_current_epoch_boundary = true; status.is_current_epoch_boundary_attester = true;
} }
} else if is_from_epoch(a, state.previous_epoch(spec), spec) { } else if is_from_epoch(a, state.previous_epoch(spec), spec) {
self.balances.previous_epoch += attesting_balance; self.balances.previous_epoch_attesters += attesting_balance;
status.is_previous_epoch = true; status.is_previous_epoch_attester = true;
// The inclusion slot and distance are only required for previous epoch attesters. // The inclusion slot and distance are only required for previous epoch attesters.
status.inclusion_info = InclusionInfo { status.inclusion_info = InclusionInfo {
@ -138,13 +148,13 @@ impl Attesters {
}; };
if has_common_epoch_boundary_root(a, state, state.previous_epoch(spec), spec)? { if has_common_epoch_boundary_root(a, state, state.previous_epoch(spec), spec)? {
self.balances.previous_epoch_boundary += attesting_balance; self.balances.previous_epoch_boundary_attesters += attesting_balance;
status.is_previous_epoch_boundary = true; status.is_previous_epoch_boundary_attester = true;
} }
if has_common_beacon_block_root(a, state, spec)? { if has_common_beacon_block_root(a, state, spec)? {
self.balances.previous_epoch_head += attesting_balance; self.balances.previous_epoch_head_attesters += attesting_balance;
status.is_previous_epoch_head = true; status.is_previous_epoch_head_attester = true;
} }
} }