mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-14 18:32:42 +00:00
Cache validator balances and allow them to be served over the HTTP API (#3863)
## Issue Addressed #3804 ## Proposed Changes - Add `total_balance` to the validator monitor and adjust the number of historical epochs which are cached. - Allow certain values in the cache to be served out via the HTTP API without requiring a state read. ## Usage ``` curl -X POST "http://localhost:5052/lighthouse/ui/validator_info" -d '{"indices": [0]}' -H "Content-Type: application/json" | jq ``` ``` { "data": { "validators": { "0": { "info": [ { "epoch": 172981, "total_balance": 36566388519 }, ... { "epoch": 172990, "total_balance": 36566496513 } ] }, "1": { "info": [ { "epoch": 172981, "total_balance": 36355797968 }, ... { "epoch": 172990, "total_balance": 36355905962 } ] } } } } ``` ## Additional Info This requires no historical states to operate which mean it will still function on the freshly checkpoint synced node, however because of this, the values will populate each epoch (up to a maximum of 10 entries). Another benefit of this method, is that we can easily cache any other values which would normally require a state read and serve them via the same endpoint. However, we would need be cautious about not overly increasing block processing time by caching values from complex computations. This also caches some of the validator metrics directly, rather than pulling them from the Prometheus metrics when the API is called. This means when the validator count exceeds the individual monitor threshold, the cached values will still be available. Co-authored-by: Paul Hauner <paul@paulhauner.com>
This commit is contained in:
@@ -29,7 +29,7 @@ const TOTAL_LABEL: &str = "total";
|
||||
|
||||
/// The validator monitor collects per-epoch data about each monitored validator. Historical data
|
||||
/// will be kept around for `HISTORIC_EPOCHS` before it is pruned.
|
||||
pub const HISTORIC_EPOCHS: usize = 4;
|
||||
pub const HISTORIC_EPOCHS: usize = 10;
|
||||
|
||||
/// Once the validator monitor reaches this number of validators it will stop
|
||||
/// tracking their metrics/logging individually in an effort to reduce
|
||||
@@ -45,7 +45,7 @@ pub enum Error {
|
||||
|
||||
/// Contains data pertaining to one validator for one epoch.
|
||||
#[derive(Default)]
|
||||
struct EpochSummary {
|
||||
pub struct EpochSummary {
|
||||
/*
|
||||
* Attestations with a target in the current epoch.
|
||||
*/
|
||||
@@ -103,6 +103,12 @@ struct EpochSummary {
|
||||
pub proposer_slashings: usize,
|
||||
/// The number of attester slashings observed.
|
||||
pub attester_slashings: usize,
|
||||
|
||||
/*
|
||||
* Other validator info helpful for the UI.
|
||||
*/
|
||||
/// The total balance of the validator.
|
||||
pub total_balance: Option<u64>,
|
||||
}
|
||||
|
||||
impl EpochSummary {
|
||||
@@ -176,18 +182,60 @@ impl EpochSummary {
|
||||
pub fn register_attester_slashing(&mut self) {
|
||||
self.attester_slashings += 1;
|
||||
}
|
||||
|
||||
pub fn register_validator_total_balance(&mut self, total_balance: u64) {
|
||||
self.total_balance = Some(total_balance)
|
||||
}
|
||||
}
|
||||
|
||||
type SummaryMap = HashMap<Epoch, EpochSummary>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ValidatorMetrics {
|
||||
pub attestation_hits: u64,
|
||||
pub attestation_misses: u64,
|
||||
pub attestation_head_hits: u64,
|
||||
pub attestation_head_misses: u64,
|
||||
pub attestation_target_hits: u64,
|
||||
pub attestation_target_misses: u64,
|
||||
}
|
||||
|
||||
impl ValidatorMetrics {
|
||||
pub fn increment_hits(&mut self) {
|
||||
self.attestation_hits += 1;
|
||||
}
|
||||
|
||||
pub fn increment_misses(&mut self) {
|
||||
self.attestation_misses += 1;
|
||||
}
|
||||
|
||||
pub fn increment_target_hits(&mut self) {
|
||||
self.attestation_target_hits += 1;
|
||||
}
|
||||
|
||||
pub fn increment_target_misses(&mut self) {
|
||||
self.attestation_target_misses += 1;
|
||||
}
|
||||
|
||||
pub fn increment_head_hits(&mut self) {
|
||||
self.attestation_head_hits += 1;
|
||||
}
|
||||
|
||||
pub fn increment_head_misses(&mut self) {
|
||||
self.attestation_head_misses += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// A validator that is being monitored by the `ValidatorMonitor`.
|
||||
struct MonitoredValidator {
|
||||
pub struct MonitoredValidator {
|
||||
/// A human-readable identifier for the validator.
|
||||
pub id: String,
|
||||
/// The validator index in the state.
|
||||
pub index: Option<u64>,
|
||||
/// A history of the validator over time.
|
||||
pub summaries: RwLock<SummaryMap>,
|
||||
/// Validator metrics to be exposed over the HTTP API.
|
||||
pub metrics: RwLock<ValidatorMetrics>,
|
||||
}
|
||||
|
||||
impl MonitoredValidator {
|
||||
@@ -198,6 +246,7 @@ impl MonitoredValidator {
|
||||
.unwrap_or_else(|| pubkey.to_string()),
|
||||
index,
|
||||
summaries: <_>::default(),
|
||||
metrics: <_>::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,6 +301,20 @@ impl MonitoredValidator {
|
||||
fn touch_epoch_summary(&self, epoch: Epoch) {
|
||||
self.with_epoch_summary(epoch, |_| {});
|
||||
}
|
||||
|
||||
fn get_from_epoch_summary<F, U>(&self, epoch: Epoch, func: F) -> Option<U>
|
||||
where
|
||||
F: Fn(Option<&EpochSummary>) -> Option<U>,
|
||||
{
|
||||
let summaries = self.summaries.read();
|
||||
func(summaries.get(&epoch))
|
||||
}
|
||||
|
||||
pub fn get_total_balance(&self, epoch: Epoch) -> Option<u64> {
|
||||
self.get_from_epoch_summary(epoch, |summary_opt| {
|
||||
summary_opt.and_then(|summary| summary.total_balance)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Holds a collection of `MonitoredValidator` and is notified about a variety of events on the P2P
|
||||
@@ -347,12 +410,20 @@ impl<T: EthSpec> ValidatorMonitor<T> {
|
||||
if let Some(i) = monitored_validator.index {
|
||||
monitored_validator.touch_epoch_summary(current_epoch);
|
||||
|
||||
let i = i as usize;
|
||||
|
||||
// Cache relevant validator info.
|
||||
if let Some(balance) = state.balances().get(i) {
|
||||
monitored_validator.with_epoch_summary(current_epoch, |summary| {
|
||||
summary.register_validator_total_balance(*balance)
|
||||
});
|
||||
}
|
||||
|
||||
// Only log the per-validator metrics if it's enabled.
|
||||
if !self.individual_tracking() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let i = i as usize;
|
||||
let id = &monitored_validator.id;
|
||||
|
||||
if let Some(balance) = state.balances().get(i) {
|
||||
@@ -479,6 +550,25 @@ impl<T: EthSpec> ValidatorMonitor<T> {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Store some metrics directly to be re-exposed on the HTTP API.
|
||||
let mut validator_metrics = monitored_validator.metrics.write();
|
||||
if previous_epoch_matched_any {
|
||||
validator_metrics.increment_hits();
|
||||
if previous_epoch_matched_target {
|
||||
validator_metrics.increment_target_hits()
|
||||
} else {
|
||||
validator_metrics.increment_target_misses()
|
||||
}
|
||||
if previous_epoch_matched_head {
|
||||
validator_metrics.increment_head_hits()
|
||||
} else {
|
||||
validator_metrics.increment_head_misses()
|
||||
}
|
||||
} else {
|
||||
validator_metrics.increment_misses()
|
||||
}
|
||||
drop(validator_metrics);
|
||||
|
||||
// Indicates if any attestation made it on-chain.
|
||||
//
|
||||
// For Base states, this will be *any* attestation whatsoever. For Altair states,
|
||||
@@ -717,6 +807,14 @@ impl<T: EthSpec> ValidatorMonitor<T> {
|
||||
self.validators.values().map(|val| val.id.clone()).collect()
|
||||
}
|
||||
|
||||
pub fn get_monitored_validator(&self, index: u64) -> Option<&MonitoredValidator> {
|
||||
if let Some(pubkey) = self.indices.get(&index) {
|
||||
self.validators.get(pubkey)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// If `self.auto_register == true`, add the `validator_index` to `self.monitored_validators`.
|
||||
/// Otherwise, do nothing.
|
||||
pub fn auto_register_local_validator(&mut self, validator_index: u64) {
|
||||
|
||||
Reference in New Issue
Block a user