Moved chain/cache building into separate function, and made sure that all REST API endpoints are using this function to get the state.
This commit is contained in:
parent
a2267dc4d3
commit
99c673045c
@ -109,7 +109,7 @@ pub fn get_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
|
|||||||
|
|
||||||
/// HTTP handler to return a `BeaconBlock` root at a given `slot`.
|
/// HTTP handler to return a `BeaconBlock` root at a given `slot`.
|
||||||
pub fn get_block_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
pub fn get_block_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
|
|
||||||
let slot_string = UrlQuery::from_request(&req)?.only_one("slot")?;
|
let slot_string = UrlQuery::from_request(&req)?.only_one("slot")?;
|
||||||
let target = parse_slot(&slot_string)?;
|
let target = parse_slot(&slot_string)?;
|
||||||
@ -126,10 +126,9 @@ pub fn get_block_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiR
|
|||||||
|
|
||||||
/// HTTP handler to return the `Fork` of the current head.
|
/// HTTP handler to return the `Fork` of the current head.
|
||||||
pub fn get_fork<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
pub fn get_fork<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
let (_beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
let chain_head = beacon_chain.head();
|
|
||||||
|
|
||||||
let json: String = serde_json::to_string(&chain_head.beacon_state.fork).map_err(|e| {
|
let json: String = serde_json::to_string(&head_state.fork).map_err(|e| {
|
||||||
ApiError::ServerError(format!("Unable to serialize BeaconState::Fork: {:?}", e))
|
ApiError::ServerError(format!("Unable to serialize BeaconState::Fork: {:?}", e))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@ -141,7 +140,7 @@ pub fn get_fork<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
|
|||||||
/// The `Epoch` parameter can be any epoch number. If it is not specified,
|
/// The `Epoch` parameter can be any epoch number. If it is not specified,
|
||||||
/// the current epoch is assumed.
|
/// the current epoch is assumed.
|
||||||
pub fn get_validators<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
pub fn get_validators<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
|
|
||||||
let epoch = match UrlQuery::from_request(&req) {
|
let epoch = match UrlQuery::from_request(&req) {
|
||||||
// We have some parameters, so make sure it's the epoch one and parse it
|
// We have some parameters, so make sure it's the epoch one and parse it
|
||||||
@ -183,7 +182,7 @@ pub struct StateResponse<T: EthSpec> {
|
|||||||
/// Will not return a state if the request slot is in the future. Will return states higher than
|
/// Will not return a state if the request slot is in the future. Will return states higher than
|
||||||
/// the current head by skipping slots.
|
/// the current head by skipping slots.
|
||||||
pub fn get_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
pub fn get_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
let (beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
|
|
||||||
let (key, value) = match UrlQuery::from_request(&req) {
|
let (key, value) = match UrlQuery::from_request(&req) {
|
||||||
Ok(query) => {
|
Ok(query) => {
|
||||||
@ -199,10 +198,7 @@ pub fn get_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
|
|||||||
}
|
}
|
||||||
Err(ApiError::InvalidQueryParams(_)) => {
|
Err(ApiError::InvalidQueryParams(_)) => {
|
||||||
// No parameters provided at all, use current slot.
|
// No parameters provided at all, use current slot.
|
||||||
(
|
(String::from("slot"), head_state.slot.to_string())
|
||||||
String::from("slot"),
|
|
||||||
beacon_chain.head().beacon_state.slot.to_string(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return Err(e);
|
return Err(e);
|
||||||
@ -237,7 +233,7 @@ pub fn get_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
|
|||||||
/// Will not return a state if the request slot is in the future. Will return states higher than
|
/// Will not return a state if the request slot is in the future. Will return states higher than
|
||||||
/// the current head by skipping slots.
|
/// the current head by skipping slots.
|
||||||
pub fn get_state_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
pub fn get_state_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
|
|
||||||
let slot_string = UrlQuery::from_request(&req)?.only_one("slot")?;
|
let slot_string = UrlQuery::from_request(&req)?.only_one("slot")?;
|
||||||
let slot = parse_slot(&slot_string)?;
|
let slot = parse_slot(&slot_string)?;
|
||||||
@ -254,7 +250,7 @@ pub fn get_state_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiR
|
|||||||
pub fn get_current_finalized_checkpoint<T: BeaconChainTypes + 'static>(
|
pub fn get_current_finalized_checkpoint<T: BeaconChainTypes + 'static>(
|
||||||
req: Request<Body>,
|
req: Request<Body>,
|
||||||
) -> ApiResult {
|
) -> ApiResult {
|
||||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
|
|
||||||
let checkpoint = beacon_chain
|
let checkpoint = beacon_chain
|
||||||
.head()
|
.head()
|
||||||
@ -270,7 +266,7 @@ pub fn get_current_finalized_checkpoint<T: BeaconChainTypes + 'static>(
|
|||||||
|
|
||||||
/// HTTP handler to return a `BeaconState` at the genesis block.
|
/// HTTP handler to return a `BeaconState` at the genesis block.
|
||||||
pub fn get_genesis_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
pub fn get_genesis_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
|
|
||||||
let (_root, state) = state_at_slot(&beacon_chain, Slot::new(0))?;
|
let (_root, state) = state_at_slot(&beacon_chain, Slot::new(0))?;
|
||||||
|
|
||||||
|
@ -172,26 +172,21 @@ pub fn implementation_pending_response(_req: Request<Body>) -> ApiResult {
|
|||||||
|
|
||||||
pub fn get_beacon_chain_from_request<T: BeaconChainTypes + 'static>(
|
pub fn get_beacon_chain_from_request<T: BeaconChainTypes + 'static>(
|
||||||
req: &Request<Body>,
|
req: &Request<Body>,
|
||||||
) -> Result<Arc<BeaconChain<T>>, ApiError> {
|
) -> Result<(Arc<BeaconChain<T>>, BeaconState<T::EthSpec>), ApiError> {
|
||||||
// Get beacon state
|
// Get beacon state
|
||||||
let beacon_chain = req
|
let beacon_chain = req
|
||||||
.extensions()
|
.extensions()
|
||||||
.get::<Arc<BeaconChain<T>>>()
|
.get::<Arc<BeaconChain<T>>>()
|
||||||
.ok_or_else(|| ApiError::ServerError("Beacon chain extension missing".into()))?;
|
.ok_or_else(|| ApiError::ServerError("Beacon chain extension missing".into()))?;
|
||||||
|
let mut head_state = beacon_chain
|
||||||
/*
|
|
||||||
let _state_now = beacon_chain
|
|
||||||
.state_now()
|
.state_now()
|
||||||
.map_err(|e| ApiError::ServerError(format!("Unable to get current BeaconState {:?}", e)))?
|
.map_err(|e| ApiError::ServerError(format!("Unable to get current BeaconState {:?}", e)))?;
|
||||||
.maybe_as_mut_ref()
|
|
||||||
.ok_or(ApiError::ServerError(
|
|
||||||
"Unable to get mutable BeaconState".into(),
|
|
||||||
))?
|
|
||||||
.build_all_caches(&beacon_chain.spec)
|
|
||||||
.map_err(|e| ApiError::ServerError(format!("Unable to build state caches: {:?}", e)))?;
|
|
||||||
*/
|
|
||||||
|
|
||||||
Ok(beacon_chain.clone())
|
if let Some(s) = head_state.maybe_as_mut_ref() {
|
||||||
|
s.build_all_caches(&beacon_chain.spec).ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((beacon_chain.clone(), head_state.clone()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_logger_from_request(req: &Request<Body>) -> slog::Logger {
|
pub fn get_logger_from_request(req: &Request<Body>) -> slog::Logger {
|
||||||
|
@ -30,7 +30,7 @@ pub fn get_prometheus<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiR
|
|||||||
let mut buffer = vec![];
|
let mut buffer = vec![];
|
||||||
let encoder = TextEncoder::new();
|
let encoder = TextEncoder::new();
|
||||||
|
|
||||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
let db_path = req
|
let db_path = req
|
||||||
.extensions()
|
.extensions()
|
||||||
.get::<DBPath>()
|
.get::<DBPath>()
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
|
use crate::helpers::get_beacon_chain_from_request;
|
||||||
use crate::{success_response, ApiResult};
|
use crate::{success_response, ApiResult};
|
||||||
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
use beacon_chain::BeaconChainTypes;
|
||||||
use hyper::{Body, Request};
|
use hyper::{Body, Request};
|
||||||
use std::sync::Arc;
|
|
||||||
use version;
|
use version;
|
||||||
|
|
||||||
/// Read the version string from the current Lighthouse build.
|
/// Read the version string from the current Lighthouse build.
|
||||||
@ -15,8 +15,8 @@ pub fn get_version(_req: Request<Body>) -> ApiResult {
|
|||||||
|
|
||||||
/// Read the genesis time from the current beacon chain state.
|
/// Read the genesis time from the current beacon chain state.
|
||||||
pub fn get_genesis_time<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
pub fn get_genesis_time<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let beacon_chain = req.extensions().get::<Arc<BeaconChain<T>>>().unwrap();
|
let (_beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
let gen_time: u64 = beacon_chain.head().beacon_state.genesis_time;
|
let gen_time: u64 = head_state.genesis_time;
|
||||||
let body = Body::from(
|
let body = Body::from(
|
||||||
serde_json::to_string(&gen_time)
|
serde_json::to_string(&gen_time)
|
||||||
.expect("Genesis should time always have a valid JSON serialization."),
|
.expect("Genesis should time always have a valid JSON serialization."),
|
||||||
|
@ -9,7 +9,7 @@ use types::EthSpec;
|
|||||||
|
|
||||||
/// HTTP handler to return the full spec object.
|
/// HTTP handler to return the full spec object.
|
||||||
pub fn get_spec<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
pub fn get_spec<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
|
|
||||||
let json: String = serde_json::to_string(&beacon_chain.spec)
|
let json: String = serde_json::to_string(&beacon_chain.spec)
|
||||||
.map_err(|e| ApiError::ServerError(format!("Unable to serialize spec: {:?}", e)))?;
|
.map_err(|e| ApiError::ServerError(format!("Unable to serialize spec: {:?}", e)))?;
|
||||||
|
@ -34,10 +34,7 @@ impl ValidatorDuty {
|
|||||||
pub fn get_validator_duties<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
pub fn get_validator_duties<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let log = get_logger_from_request(&req);
|
let log = get_logger_from_request(&req);
|
||||||
slog::trace!(log, "Validator duties requested of API: {:?}", &req);
|
slog::trace!(log, "Validator duties requested of API: {:?}", &req);
|
||||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
let (beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
let mut head_state = beacon_chain
|
|
||||||
.state_now()
|
|
||||||
.map_err(|e| ApiError::ServerError(format!("Unable to get current BeaconState {:?}", e)))?;
|
|
||||||
|
|
||||||
slog::trace!(log, "Got head state from request.");
|
slog::trace!(log, "Got head state from request.");
|
||||||
// Parse and check query parameters
|
// Parse and check query parameters
|
||||||
@ -67,9 +64,6 @@ pub fn get_validator_duties<T: BeaconChainTypes + 'static>(req: Request<Body>) -
|
|||||||
e
|
e
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
if let Some(s) = head_state.maybe_as_mut_ref() {
|
|
||||||
s.build_all_caches(&beacon_chain.spec).ok();
|
|
||||||
}
|
|
||||||
let validators: Vec<PublicKey> = query
|
let validators: Vec<PublicKey> = query
|
||||||
.all_of("validator_pubkeys")?
|
.all_of("validator_pubkeys")?
|
||||||
.iter()
|
.iter()
|
||||||
@ -163,7 +157,7 @@ pub fn get_validator_duties<T: BeaconChainTypes + 'static>(req: Request<Body>) -
|
|||||||
|
|
||||||
/// HTTP Handler to produce a new BeaconBlock from the current state, ready to be signed by a validator.
|
/// HTTP Handler to produce a new BeaconBlock from the current state, ready to be signed by a validator.
|
||||||
pub fn get_new_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
pub fn get_new_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
|
|
||||||
let query = UrlQuery::from_request(&req)?;
|
let query = UrlQuery::from_request(&req)?;
|
||||||
let slot = query
|
let slot = query
|
||||||
@ -203,8 +197,7 @@ pub fn get_new_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
|
|||||||
|
|
||||||
/// HTTP Handler to produce a new Attestation from the current state, ready to be signed by a validator.
|
/// HTTP Handler to produce a new Attestation from the current state, ready to be signed by a validator.
|
||||||
pub fn get_new_attestation<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
pub fn get_new_attestation<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||||
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
|
let (beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
|
||||||
let head_state = &beacon_chain.head().beacon_state;
|
|
||||||
|
|
||||||
let query = UrlQuery::from_request(&req)?;
|
let query = UrlQuery::from_request(&req)?;
|
||||||
let val_pk_str = query
|
let val_pk_str = query
|
||||||
|
Loading…
Reference in New Issue
Block a user