mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-19 21:04:41 +00:00
Advance state to next slot after importing block (#2174)
## Issue Addressed
NA
## Proposed Changes
Add an optimization to perform `per_slot_processing` from the *leading-edge* of block processing to the *trailing-edge*. Ultimately, this allows us to import the block at slot `n` faster because we used the tail-end of slot `n - 1` to perform `per_slot_processing`.
Additionally, add a "block proposer cache" which allows us to cache the block proposer for some epoch. Since we're now doing trailing-edge `per_slot_processing`, we can prime this cache with the values for the next epoch before those blocks arrive (assuming those blocks don't have some weird forking).
There were several ancillary changes required to achieve this:
- Remove the `state_root` field of `BeaconSnapshot`, since there's no need to know it on a `pre_state` and in all other cases we can just read it from `block.state_root()`.
- This caused some "dust" changes of `snapshot.beacon_state_root` to `snapshot.beacon_state_root()`, where the `BeaconSnapshot::beacon_state_root()` func just reads the state root from the block.
- Rename `types::ShuffingId` to `AttestationShufflingId`. I originally did this because I added a `ProposerShufflingId` struct which turned out to be not so useful. I thought this new name was more descriptive so I kept it.
- Address https://github.com/ethereum/eth2.0-specs/pull/2196
- Add a debug log when we get a block with an unknown parent. There was previously no logging around this case.
- Add a function to `BeaconState` to compute all proposers for an epoch without re-computing the active indices for each slot.
## Additional Info
- ~~Blocked on #2173~~
- ~~Blocked on #2179~~ That PR was wrapped into this PR.
- There's potentially some places where we could avoid computing the proposer indices in `per_block_processing` but I haven't done this here. These would be an optimization beyond the issue at hand (improving block propagation times) and I think this PR is already doing enough. We can come back for that later.
## TODO
- [x] Tidy, improve comments.
- [x] ~~Try avoid computing proposer index in `per_block_processing`?~~
This commit is contained in:
@@ -4,8 +4,9 @@
|
||||
|
||||
use crate::metrics;
|
||||
use parking_lot::RwLock;
|
||||
use slog::{crit, info, Logger};
|
||||
use slog::{crit, error, info, warn, Logger};
|
||||
use slot_clock::SlotClock;
|
||||
use state_processing::per_epoch_processing::ValidatorStatus;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::convert::TryFrom;
|
||||
use std::io;
|
||||
@@ -325,6 +326,103 @@ impl<T: EthSpec> ValidatorMonitor<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process_validator_statuses(&self, epoch: Epoch, summaries: &[ValidatorStatus]) {
|
||||
for monitored_validator in self.validators.values() {
|
||||
// We subtract two from the state of the epoch that generated these summaries.
|
||||
//
|
||||
// - One to account for it being the previous epoch.
|
||||
// - One to account for the state advancing an epoch whilst generating the validator
|
||||
// statuses.
|
||||
let prev_epoch = epoch - 2;
|
||||
if let Some(i) = monitored_validator.index {
|
||||
let i = i as usize;
|
||||
let id = &monitored_validator.id;
|
||||
|
||||
if let Some(summary) = summaries.get(i) {
|
||||
if summary.is_previous_epoch_attester {
|
||||
let lag = summary
|
||||
.inclusion_info
|
||||
.map(|i| format!("{} slot(s)", i.delay.saturating_sub(1).to_string()))
|
||||
.unwrap_or_else(|| "??".to_string());
|
||||
|
||||
info!(
|
||||
self.log,
|
||||
"Previous epoch attestation success";
|
||||
"inclusion_lag" => lag,
|
||||
"matched_target" => summary.is_previous_epoch_target_attester,
|
||||
"matched_head" => summary.is_previous_epoch_head_attester,
|
||||
"epoch" => prev_epoch,
|
||||
"validator" => id,
|
||||
|
||||
)
|
||||
} else if summary.is_active_in_previous_epoch
|
||||
&& !summary.is_previous_epoch_attester
|
||||
{
|
||||
error!(
|
||||
self.log,
|
||||
"Previous epoch attestation missing";
|
||||
"epoch" => prev_epoch,
|
||||
"validator" => id,
|
||||
)
|
||||
}
|
||||
|
||||
if summary.is_previous_epoch_attester {
|
||||
metrics::inc_counter_vec(
|
||||
&metrics::VALIDATOR_MONITOR_PREV_EPOCH_ON_CHAIN_ATTESTER_HIT,
|
||||
&[id],
|
||||
);
|
||||
} else {
|
||||
metrics::inc_counter_vec(
|
||||
&metrics::VALIDATOR_MONITOR_PREV_EPOCH_ON_CHAIN_ATTESTER_MISS,
|
||||
&[id],
|
||||
);
|
||||
}
|
||||
if summary.is_previous_epoch_head_attester {
|
||||
metrics::inc_counter_vec(
|
||||
&metrics::VALIDATOR_MONITOR_PREV_EPOCH_ON_CHAIN_HEAD_ATTESTER_HIT,
|
||||
&[id],
|
||||
);
|
||||
} else {
|
||||
metrics::inc_counter_vec(
|
||||
&metrics::VALIDATOR_MONITOR_PREV_EPOCH_ON_CHAIN_HEAD_ATTESTER_MISS,
|
||||
&[id],
|
||||
);
|
||||
warn!(
|
||||
self.log,
|
||||
"Attested to an incorrect head";
|
||||
"epoch" => prev_epoch,
|
||||
"validator" => id,
|
||||
);
|
||||
}
|
||||
if summary.is_previous_epoch_target_attester {
|
||||
metrics::inc_counter_vec(
|
||||
&metrics::VALIDATOR_MONITOR_PREV_EPOCH_ON_CHAIN_TARGET_ATTESTER_HIT,
|
||||
&[id],
|
||||
);
|
||||
} else {
|
||||
metrics::inc_counter_vec(
|
||||
&metrics::VALIDATOR_MONITOR_PREV_EPOCH_ON_CHAIN_TARGET_ATTESTER_MISS,
|
||||
&[id],
|
||||
);
|
||||
warn!(
|
||||
self.log,
|
||||
"Attested to an incorrect target";
|
||||
"epoch" => prev_epoch,
|
||||
"validator" => id,
|
||||
);
|
||||
}
|
||||
if let Some(inclusion_info) = summary.inclusion_info {
|
||||
metrics::set_int_gauge(
|
||||
&metrics::VALIDATOR_MONITOR_PREV_EPOCH_ON_CHAIN_INCLUSION_DISTANCE,
|
||||
&[id],
|
||||
inclusion_info.delay as i64,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_validator_id(&self, validator_index: u64) -> Option<&str> {
|
||||
self.indices
|
||||
.get(&validator_index)
|
||||
@@ -945,9 +1043,18 @@ pub fn get_block_delay_ms<T: EthSpec, S: SlotClock>(
|
||||
seen_timestamp: Duration,
|
||||
block: &BeaconBlock<T>,
|
||||
slot_clock: &S,
|
||||
) -> Duration {
|
||||
get_slot_delay_ms::<S>(seen_timestamp, block.slot, slot_clock)
|
||||
}
|
||||
|
||||
/// Returns the delay between the start of `slot` and `seen_timestamp`.
|
||||
pub fn get_slot_delay_ms<S: SlotClock>(
|
||||
seen_timestamp: Duration,
|
||||
slot: Slot,
|
||||
slot_clock: &S,
|
||||
) -> Duration {
|
||||
slot_clock
|
||||
.start_of(block.slot)
|
||||
.start_of(slot)
|
||||
.and_then(|slot_start| seen_timestamp.checked_sub(slot_start))
|
||||
.unwrap_or_else(|| Duration::from_secs(0))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user