Skip to content

Commit

Permalink
Merge readiness endpoint (sigp#3349)
Browse files Browse the repository at this point in the history
## Issue Addressed

Resolves final task in sigp#3260

## Proposed Changes

Adds a lighthouse http endpoint to indicate merge readiness.

Blocked on sigp#3339
  • Loading branch information
pawanjay176 committed Jul 21, 2022
1 parent e328684 commit 612cdb7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
39 changes: 28 additions & 11 deletions beacon_node/beacon_chain/src/merge_readiness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! transition.
use crate::{BeaconChain, BeaconChainTypes};
use execution_layer::Error as EngineError;
use serde::{Deserialize, Serialize, Serializer};
use std::fmt;
use std::fmt::Write;
use types::*;
Expand All @@ -11,10 +11,13 @@ use types::*;
const SECONDS_IN_A_WEEK: u64 = 604800;
pub const MERGE_READINESS_PREPARATION_SECONDS: u64 = SECONDS_IN_A_WEEK;

#[derive(Default, Debug)]
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct MergeConfig {
#[serde(serialize_with = "serialize_uint256")]
pub terminal_total_difficulty: Option<Uint256>,
#[serde(skip_serializing_if = "Option::is_none")]
pub terminal_block_hash: Option<ExecutionBlockHash>,
#[serde(skip_serializing_if = "Option::is_none")]
pub terminal_block_hash_epoch: Option<Epoch>,
}

Expand Down Expand Up @@ -73,15 +76,19 @@ impl MergeConfig {
}

/// Indicates if a node is ready for the Bellatrix upgrade and subsequent merge transition.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "type")]
pub enum MergeReadiness {
/// The node is ready, as far as we can tell.
Ready {
config: MergeConfig,
current_difficulty: Result<Uint256, String>,
#[serde(serialize_with = "serialize_uint256")]
current_difficulty: Option<Uint256>,
},
/// The transition configuration with the EL failed, there might be a problem with
/// connectivity, authentication or a difference in configuration.
ExchangeTransitionConfigurationFailed(EngineError),
ExchangeTransitionConfigurationFailed { error: String },
/// The EL can be reached and has the correct configuration, however it's not yet synced.
NotSynced,
/// The user has not configured this node to use an execution endpoint.
Expand All @@ -102,11 +109,11 @@ impl fmt::Display for MergeReadiness {
params, current_difficulty
)
}
MergeReadiness::ExchangeTransitionConfigurationFailed(e) => write!(
MergeReadiness::ExchangeTransitionConfigurationFailed { error } => write!(
f,
"Could not confirm the transition configuration with the \
execution endpoint: {:?}",
e
error
),
MergeReadiness::NotSynced => write!(
f,
Expand Down Expand Up @@ -145,18 +152,17 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
if let Err(e) = el.exchange_transition_configuration(&self.spec).await {
// The EL was either unreachable, responded with an error or has a different
// configuration.
return MergeReadiness::ExchangeTransitionConfigurationFailed(e);
return MergeReadiness::ExchangeTransitionConfigurationFailed {
error: format!("{:?}", e),
};
}

if !el.is_synced_for_notifier().await {
// The EL is not synced.
return MergeReadiness::NotSynced;
}
let params = MergeConfig::from_chainspec(&self.spec);
let current_difficulty = el
.get_current_difficulty()
.await
.map_err(|_| "Failed to get current difficulty from execution node".to_string());
let current_difficulty = el.get_current_difficulty().await.ok();
MergeReadiness::Ready {
config: params,
current_difficulty,
Expand All @@ -167,3 +173,14 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
}
}
}

/// Utility function to serialize a Uint256 as a decimal string.
fn serialize_uint256<S>(val: &Option<Uint256>, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match val {
Some(v) => v.to_string().serialize(s),
None => s.serialize_none(),
}
}
4 changes: 2 additions & 2 deletions beacon_node/client/src/notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ async fn merge_readiness_logging<T: BeaconChainTypes>(
"terminal_total_difficulty" => %ttd,
"current_difficulty" => current_difficulty
.map(|d| d.to_string())
.unwrap_or_else(|_| "??".into()),
.unwrap_or_else(|| "??".into()),
)
}
MergeConfig {
Expand All @@ -382,7 +382,7 @@ async fn merge_readiness_logging<T: BeaconChainTypes>(
"config" => ?other
),
},
readiness @ MergeReadiness::ExchangeTransitionConfigurationFailed(_) => {
readiness @ MergeReadiness::ExchangeTransitionConfigurationFailed { error: _ } => {
error!(
log,
"Not ready for merge";
Expand Down
13 changes: 13 additions & 0 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2887,6 +2887,18 @@ pub fn serve<T: BeaconChainTypes>(
})
});

// GET lighthouse/merge_readiness
let get_lighthouse_merge_readiness = warp::path("lighthouse")
.and(warp::path("merge_readiness"))
.and(warp::path::end())
.and(chain_filter.clone())
.and_then(|chain: Arc<BeaconChain<T>>| async move {
let merge_readiness = chain.check_merge_readiness().await;
Ok::<_, warp::reject::Rejection>(warp::reply::json(&api_types::GenericResponse::from(
merge_readiness,
)))
});

let get_events = eth1_v1
.and(warp::path("events"))
.and(warp::path::end())
Expand Down Expand Up @@ -3015,6 +3027,7 @@ pub fn serve<T: BeaconChainTypes>(
.or(get_lighthouse_block_rewards.boxed())
.or(get_lighthouse_attestation_performance.boxed())
.or(get_lighthouse_block_packing_efficiency.boxed())
.or(get_lighthouse_merge_readiness.boxed())
.or(get_events.boxed()),
)
.or(warp::post().and(
Expand Down
21 changes: 20 additions & 1 deletion book/src/api-lighthouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,23 @@ Caveats:
loading a state on a boundary is most efficient.

[block_reward_src]:
https://github.com/sigp/lighthouse/tree/unstable/common/eth2/src/lighthouse/block_rewards.rs
https://github.com/sigp/lighthouse/tree/unstable/common/eth2/src/lighthouse/block_rewards.rs


### `/lighthouse/merge_readiness`

```bash
curl -X GET "http://localhost:5052/lighthouse/merge_readiness"
```

```
{
"data":{
"type":"ready",
"config":{
"terminal_total_difficulty":"6400"
},
"current_difficulty":"4800"
}
}
```

0 comments on commit 612cdb7

Please sign in to comment.