mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-11 18:04:18 +00:00
Return eth1-related data via the API (#1797)
## Issue Addressed - Related to #1691 ## Proposed Changes Adds the following API endpoints: - `GET lighthouse/eth1/syncing`: status about how synced we are with Eth1. - `GET lighthouse/eth1/block_cache`: all locally cached eth1 blocks. - `GET lighthouse/eth1/deposit_cache`: all locally cached eth1 deposits. Additionally: - Moves some types from the `beacon_node/eth1` to the `common/eth2` crate, so they can be used in the API without duplication. - Allow `update_deposit_cache` and `update_block_cache` to take an optional head block number to avoid duplicate requests. ## Additional Info TBC
This commit is contained in:
@@ -63,6 +63,7 @@ pub struct Context<T: BeaconChainTypes> {
|
||||
pub chain: Option<Arc<BeaconChain<T>>>,
|
||||
pub network_tx: Option<UnboundedSender<NetworkMessage<T::EthSpec>>>,
|
||||
pub network_globals: Option<Arc<NetworkGlobals<T::EthSpec>>>,
|
||||
pub eth1_service: Option<eth1::Service>,
|
||||
pub log: Logger,
|
||||
}
|
||||
|
||||
@@ -300,6 +301,19 @@ pub fn serve<T: BeaconChainTypes>(
|
||||
}
|
||||
});
|
||||
|
||||
// Create a `warp` filter that provides access to the Eth1 service.
|
||||
let inner_ctx = ctx.clone();
|
||||
let eth1_service_filter = warp::any()
|
||||
.map(move || inner_ctx.eth1_service.clone())
|
||||
.and_then(|eth1_service| async move {
|
||||
match eth1_service {
|
||||
Some(eth1_service) => Ok(eth1_service),
|
||||
None => Err(warp_utils::reject::custom_not_found(
|
||||
"The Eth1 service is not started. Use --eth1 on the CLI.".to_string(),
|
||||
)),
|
||||
}
|
||||
});
|
||||
|
||||
// Create a `warp` filter that rejects request whilst the node is syncing.
|
||||
let not_while_syncing_filter = warp::any()
|
||||
.and(network_globals.clone())
|
||||
@@ -1806,6 +1820,80 @@ pub fn serve<T: BeaconChainTypes>(
|
||||
})
|
||||
});
|
||||
|
||||
// GET lighthouse/eth1/syncing
|
||||
let get_lighthouse_eth1_syncing = warp::path("lighthouse")
|
||||
.and(warp::path("eth1"))
|
||||
.and(warp::path("syncing"))
|
||||
.and(warp::path::end())
|
||||
.and(chain_filter.clone())
|
||||
.and_then(|chain: Arc<BeaconChain<T>>| {
|
||||
blocking_json_task(move || {
|
||||
let head_info = chain
|
||||
.head_info()
|
||||
.map_err(warp_utils::reject::beacon_chain_error)?;
|
||||
let current_slot = chain
|
||||
.slot()
|
||||
.map_err(warp_utils::reject::beacon_chain_error)?;
|
||||
|
||||
chain
|
||||
.eth1_chain
|
||||
.as_ref()
|
||||
.ok_or_else(|| {
|
||||
warp_utils::reject::custom_not_found(
|
||||
"Eth1 sync is disabled. See the --eth1 CLI flag.".to_string(),
|
||||
)
|
||||
})
|
||||
.and_then(|eth1| {
|
||||
eth1.sync_status(head_info.genesis_time, current_slot, &chain.spec)
|
||||
.ok_or_else(|| {
|
||||
warp_utils::reject::custom_server_error(
|
||||
"Unable to determine Eth1 sync status".to_string(),
|
||||
)
|
||||
})
|
||||
})
|
||||
.map(api_types::GenericResponse::from)
|
||||
})
|
||||
});
|
||||
|
||||
// GET lighthouse/eth1/block_cache
|
||||
let get_lighthouse_eth1_block_cache = warp::path("lighthouse")
|
||||
.and(warp::path("eth1"))
|
||||
.and(warp::path("block_cache"))
|
||||
.and(warp::path::end())
|
||||
.and(eth1_service_filter.clone())
|
||||
.and_then(|eth1_service: eth1::Service| {
|
||||
blocking_json_task(move || {
|
||||
Ok(api_types::GenericResponse::from(
|
||||
eth1_service
|
||||
.blocks()
|
||||
.read()
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect::<Vec<_>>(),
|
||||
))
|
||||
})
|
||||
});
|
||||
|
||||
// GET lighthouse/eth1/deposit_cache
|
||||
let get_lighthouse_eth1_deposit_cache = warp::path("lighthouse")
|
||||
.and(warp::path("eth1"))
|
||||
.and(warp::path("deposit_cache"))
|
||||
.and(warp::path::end())
|
||||
.and(eth1_service_filter)
|
||||
.and_then(|eth1_service: eth1::Service| {
|
||||
blocking_json_task(move || {
|
||||
Ok(api_types::GenericResponse::from(
|
||||
eth1_service
|
||||
.deposits()
|
||||
.read()
|
||||
.cache
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect::<Vec<_>>(),
|
||||
))
|
||||
})
|
||||
});
|
||||
|
||||
// GET lighthouse/beacon/states/{state_id}/ssz
|
||||
let get_lighthouse_beacon_states_ssz = warp::path("lighthouse")
|
||||
.and(warp::path("beacon"))
|
||||
@@ -1872,6 +1960,9 @@ pub fn serve<T: BeaconChainTypes>(
|
||||
.or(get_lighthouse_proto_array.boxed())
|
||||
.or(get_lighthouse_validator_inclusion_global.boxed())
|
||||
.or(get_lighthouse_validator_inclusion.boxed())
|
||||
.or(get_lighthouse_eth1_syncing.boxed())
|
||||
.or(get_lighthouse_eth1_block_cache.boxed())
|
||||
.or(get_lighthouse_eth1_deposit_cache.boxed())
|
||||
.or(get_lighthouse_beacon_states_ssz.boxed())
|
||||
.boxed(),
|
||||
)
|
||||
|
||||
@@ -169,6 +169,9 @@ impl ApiTester {
|
||||
|
||||
*network_globals.sync_state.write() = SyncState::Synced;
|
||||
|
||||
let eth1_service =
|
||||
eth1::Service::new(eth1::Config::default(), log.clone(), chain.spec.clone());
|
||||
|
||||
let context = Arc::new(Context {
|
||||
config: Config {
|
||||
enabled: true,
|
||||
@@ -179,6 +182,7 @@ impl ApiTester {
|
||||
chain: Some(chain.clone()),
|
||||
network_tx: Some(network_tx),
|
||||
network_globals: Some(Arc::new(network_globals)),
|
||||
eth1_service: Some(eth1_service),
|
||||
log,
|
||||
});
|
||||
let ctx = context.clone();
|
||||
@@ -1643,6 +1647,32 @@ impl ApiTester {
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn test_get_lighthouse_eth1_syncing(self) -> Self {
|
||||
self.client.get_lighthouse_eth1_syncing().await.unwrap();
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn test_get_lighthouse_eth1_block_cache(self) -> Self {
|
||||
let blocks = self.client.get_lighthouse_eth1_block_cache().await.unwrap();
|
||||
|
||||
assert!(blocks.data.is_empty());
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn test_get_lighthouse_eth1_deposit_cache(self) -> Self {
|
||||
let deposits = self
|
||||
.client
|
||||
.get_lighthouse_eth1_deposit_cache()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(deposits.data.is_empty());
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn test_get_lighthouse_beacon_states_ssz(self) -> Self {
|
||||
for state_id in self.interesting_state_ids() {
|
||||
let result = self
|
||||
@@ -1920,6 +1950,12 @@ async fn lighthouse_endpoints() {
|
||||
.await
|
||||
.test_get_lighthouse_validator_inclusion_global()
|
||||
.await
|
||||
.test_get_lighthouse_eth1_syncing()
|
||||
.await
|
||||
.test_get_lighthouse_eth1_block_cache()
|
||||
.await
|
||||
.test_get_lighthouse_eth1_deposit_cache()
|
||||
.await
|
||||
.test_get_lighthouse_beacon_states_ssz()
|
||||
.await;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user