Skip to content

Commit

Permalink
Merge branch 'unstable' of https://github.com/sigp/lighthouse into co…
Browse files Browse the repository at this point in the history
…mponent-search-loop
  • Loading branch information
realbigsean committed May 3, 2024
2 parents 3764895 + 1af3f0f commit c35f5af
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 16 deletions.
8 changes: 7 additions & 1 deletion beacon_node/http_api/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ lazy_static::lazy_static! {
);
pub static ref HTTP_API_BLOCK_BROADCAST_DELAY_TIMES: Result<HistogramVec> = try_create_histogram_vec(
"http_api_block_broadcast_delay_times",
"Time between start of the slot and when the block was broadcast",
"Time between start of the slot and when the block completed broadcast and processing",
&["provenance"]
);
pub static ref HTTP_API_BLOCK_GOSSIP_TIMES: Result<HistogramVec> = try_create_histogram_vec_with_buckets(
"http_api_block_gossip_times",
"Time between receiving the block on HTTP and publishing it on gossip",
decimal_buckets(-2, 2),
&["provenance"]
);
pub static ref HTTP_API_BLOCK_PUBLISHED_LATE_TOTAL: Result<IntCounter> = try_create_int_counter(
Expand Down
18 changes: 17 additions & 1 deletion beacon_node/http_api/src/publish_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ pub async fn publish_block<T: BeaconChainTypes, B: IntoGossipVerifiedBlockConten
ProvenancedBlock::Local(block_contents, _) => (block_contents, true),
ProvenancedBlock::Builder(block_contents, _) => (block_contents, false),
};
let provenance = if is_locally_built_block {
"local"
} else {
"builder"
};
let block = block_contents.inner_block().clone();
let delay = get_block_delay_ms(seen_timestamp, block.message(), &chain.slot_clock);
debug!(log, "Signed block received in HTTP API"; "slot" => block.slot());
Expand All @@ -75,7 +80,18 @@ pub async fn publish_block<T: BeaconChainTypes, B: IntoGossipVerifiedBlockConten
.checked_sub(seen_timestamp)
.unwrap_or_else(|| Duration::from_secs(0));

info!(log, "Signed block published to network via HTTP API"; "slot" => block.slot(), "publish_delay" => ?publish_delay);
metrics::observe_timer_vec(
&metrics::HTTP_API_BLOCK_GOSSIP_TIMES,
&[provenance],
publish_delay,
);

info!(
log,
"Signed block published to network via HTTP API";
"slot" => block.slot(),
"publish_delay_ms" => publish_delay.as_millis()
);

match block.as_ref() {
SignedBeaconBlock::Base(_)
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/network/src/sync/block_lookups/parent_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) fn compute_parent_chains(nodes: &[Node]) -> Vec<NodeChain> {
// Iterate blocks with no children
for tip in nodes {
let mut block_root = tip.block_root;
if parent_to_child.get(&block_root).is_none() {
if !parent_to_child.contains_key(&block_root) {
let mut chain = vec![];

// Resolve chain of blocks
Expand Down
5 changes: 3 additions & 2 deletions consensus/state_processing/src/per_block_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ pub fn get_expected_withdrawals<E: EthSpec>(
let mut withdrawal_index = state.next_withdrawal_index()?;
let mut validator_index = state.next_withdrawal_validator_index()?;
let mut withdrawals = vec![];
let fork_name = state.fork_name_unchecked();

let bound = std::cmp::min(
state.validators().len() as u64,
Expand All @@ -518,7 +519,7 @@ pub fn get_expected_withdrawals<E: EthSpec>(
let balance = *state.balances().get(validator_index as usize).ok_or(
BeaconStateError::BalancesOutOfBounds(validator_index as usize),
)?;
if validator.is_fully_withdrawable_at(balance, epoch, spec) {
if validator.is_fully_withdrawable_at(balance, epoch, spec, fork_name) {
withdrawals.push(Withdrawal {
index: withdrawal_index,
validator_index,
Expand All @@ -528,7 +529,7 @@ pub fn get_expected_withdrawals<E: EthSpec>(
amount: balance,
});
withdrawal_index.safe_add_assign(1)?;
} else if validator.is_partially_withdrawable_validator(balance, spec) {
} else if validator.is_partially_withdrawable_validator(balance, spec, fork_name) {
withdrawals.push(Withdrawal {
index: withdrawal_index,
validator_index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ pub fn process_registry_updates<E: EthSpec>(
validator.is_active_at(current_epoch)
&& validator.effective_balance <= spec.ejection_balance
};
let fork_name = state.fork_name_unchecked();
let indices_to_update: Vec<_> = state
.validators()
.iter()
.enumerate()
.filter(|(_, validator)| {
validator.is_eligible_for_activation_queue(spec) || is_ejectable(validator)
validator.is_eligible_for_activation_queue(spec, fork_name) || is_ejectable(validator)
})
.map(|(idx, _)| idx)
.collect();

for index in indices_to_update {
let validator = state.get_validator_mut(index)?;
if validator.is_eligible_for_activation_queue(spec) {
if validator.is_eligible_for_activation_queue(spec, fork_name) {
validator.activation_eligibility_epoch = current_epoch.safe_add(1)?;
}
if is_ejectable(validator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ fn process_single_registry_update(
) -> Result<(), Error> {
let current_epoch = state_ctxt.current_epoch;

if validator.is_eligible_for_activation_queue(spec) {
if validator.is_eligible_for_activation_queue(spec, state_ctxt.fork_name) {
validator.make_mut()?.activation_eligibility_epoch = current_epoch.safe_add(1)?;
}

Expand Down
96 changes: 89 additions & 7 deletions consensus/types/src/validator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
test_utils::TestRandom, Address, BeaconState, ChainSpec, Epoch, EthSpec, Hash256,
test_utils::TestRandom, Address, BeaconState, ChainSpec, Epoch, EthSpec, ForkName, Hash256,
PublicKeyBytes,
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -57,8 +57,31 @@ impl Validator {

/// Returns `true` if the validator is eligible to join the activation queue.
///
/// Modified in electra
pub fn is_eligible_for_activation_queue(&self, spec: &ChainSpec) -> bool {
/// Calls the correct function depending on the provided `fork_name`.
pub fn is_eligible_for_activation_queue(
&self,
spec: &ChainSpec,
current_fork: ForkName,
) -> bool {
if current_fork >= ForkName::Electra {
self.is_eligible_for_activation_queue_electra(spec)
} else {
self.is_eligible_for_activation_queue_base(spec)
}
}

/// Returns `true` if the validator is eligible to join the activation queue.
///
/// Spec v0.12.1
fn is_eligible_for_activation_queue_base(&self, spec: &ChainSpec) -> bool {
self.activation_eligibility_epoch == spec.far_future_epoch
&& self.effective_balance == spec.max_effective_balance
}

/// Returns `true` if the validator is eligible to join the activation queue.
///
/// Modified in electra as part of EIP 7251.
fn is_eligible_for_activation_queue_electra(&self, spec: &ChainSpec) -> bool {
self.activation_eligibility_epoch == spec.far_future_epoch
&& self.effective_balance >= spec.min_activation_balance
}
Expand Down Expand Up @@ -131,17 +154,76 @@ impl Validator {

/// Returns `true` if the validator is fully withdrawable at some epoch.
///
/// Note: Modified in electra.
pub fn is_fully_withdrawable_at(&self, balance: u64, epoch: Epoch, spec: &ChainSpec) -> bool {
/// Calls the correct function depending on the provided `fork_name`.
pub fn is_fully_withdrawable_at(
&self,
balance: u64,
epoch: Epoch,
spec: &ChainSpec,
current_fork: ForkName,
) -> bool {
if current_fork >= ForkName::Electra {
self.is_fully_withdrawable_at_electra(balance, epoch, spec)
} else {
self.is_fully_withdrawable_at_capella(balance, epoch, spec)
}
}

/// Returns `true` if the validator is fully withdrawable at some epoch.
fn is_fully_withdrawable_at_capella(
&self,
balance: u64,
epoch: Epoch,
spec: &ChainSpec,
) -> bool {
self.has_eth1_withdrawal_credential(spec) && self.withdrawable_epoch <= epoch && balance > 0
}

/// Returns `true` if the validator is fully withdrawable at some epoch.
///
/// Modified in electra as part of EIP 7251.
fn is_fully_withdrawable_at_electra(
&self,
balance: u64,
epoch: Epoch,
spec: &ChainSpec,
) -> bool {
self.has_execution_withdrawal_credential(spec)
&& self.withdrawable_epoch <= epoch
&& balance > 0
}

/// Returns `true` if the validator is partially withdrawable.
///
/// Note: Modified in electra.
pub fn is_partially_withdrawable_validator(&self, balance: u64, spec: &ChainSpec) -> bool {
/// Calls the correct function depending on the provided `fork_name`.
pub fn is_partially_withdrawable_validator(
&self,
balance: u64,
spec: &ChainSpec,
current_fork: ForkName,
) -> bool {
if current_fork >= ForkName::Electra {
self.is_partially_withdrawable_validator_electra(balance, spec)
} else {
self.is_partially_withdrawable_validator_capella(balance, spec)
}
}

/// Returns `true` if the validator is partially withdrawable.
fn is_partially_withdrawable_validator_capella(&self, balance: u64, spec: &ChainSpec) -> bool {
self.has_eth1_withdrawal_credential(spec)
&& self.effective_balance == spec.max_effective_balance
&& balance > spec.max_effective_balance
}

/// Returns `true` if the validator is partially withdrawable.
///
/// Modified in electra as part of EIP 7251.
pub fn is_partially_withdrawable_validator_electra(
&self,
balance: u64,
spec: &ChainSpec,
) -> bool {
let max_effective_balance = self.get_validator_max_effective_balance(spec);
let has_max_effective_balance = self.effective_balance == max_effective_balance;
let has_excess_balance = balance > max_effective_balance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub const POOL_SIZE: u32 = 1;
#[cfg(not(test))]
pub const CONNECTION_TIMEOUT: Duration = Duration::from_secs(5);
#[cfg(test)]
pub const CONNECTION_TIMEOUT: Duration = Duration::from_millis(500);
pub const CONNECTION_TIMEOUT: Duration = Duration::from_secs(1);

/// Supported version of the interchange format.
pub const SUPPORTED_INTERCHANGE_FORMAT_VERSION: u64 = 5;
Expand Down

0 comments on commit c35f5af

Please sign in to comment.