mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-22 14:24:44 +00:00
Implement standard eth2.0 API (#1569)
- Resolves #1550 - Resolves #824 - Resolves #825 - Resolves #1131 - Resolves #1411 - Resolves #1256 - Resolve #1177 - Includes the `ShufflingId` struct initially defined in #1492. That PR is now closed and the changes are included here, with significant bug fixes. - Implement the https://github.com/ethereum/eth2.0-APIs in a new `http_api` crate using `warp`. This replaces the `rest_api` crate. - Add a new `common/eth2` crate which provides a wrapper around `reqwest`, providing the HTTP client that is used by the validator client and for testing. This replaces the `common/remote_beacon_node` crate. - Create a `http_metrics` crate which is a dedicated server for Prometheus metrics (they are no longer served on the same port as the REST API). We now have flags for `--metrics`, `--metrics-address`, etc. - Allow the `subnet_id` to be an optional parameter for `VerifiedUnaggregatedAttestation::verify`. This means it does not need to be provided unnecessarily by the validator client. - Move `fn map_attestation_committee` in `mod beacon_chain::attestation_verification` to a new `fn with_committee_cache` on the `BeaconChain` so the same cache can be used for obtaining validator duties. - Add some other helpers to `BeaconChain` to assist with common API duties (e.g., `block_root_at_slot`, `head_beacon_block_root`). - Change the `NaiveAggregationPool` so it can index attestations by `hash_tree_root(attestation.data)`. This is a requirement of the API. - Add functions to `BeaconChainHarness` to allow it to create slashings and exits. - Allow for `eth1::Eth1NetworkId` to go to/from a `String`. - Add functions to the `OperationPool` to allow getting all objects in the pool. - Add function to `BeaconState` to check if a committee cache is initialized. - Fix bug where `seconds_per_eth1_block` was not transferring over from `YamlConfig` to `ChainSpec`. - Add the `deposit_contract_address` to `YamlConfig` and `ChainSpec`. We needed to be able to return it in an API response. - Change some uses of serde `serialize_with` and `deserialize_with` to a single use of `with` (code quality). - Impl `Display` and `FromStr` for several BLS fields. - Check for clock discrepancy when VC polls BN for sync state (with +/- 1 slot tolerance). This is not intended to be comprehensive, it was just easy to do. - See #1434 for a per-endpoint overview. - Seeking clarity here: https://github.com/ethereum/eth2.0-APIs/issues/75 - [x] Add docs for prom port to close #1256 - [x] Follow up on this #1177 - [x] ~~Follow up with #1424~~ Will fix in future PR. - [x] Follow up with #1411 - [x] ~~Follow up with #1260~~ Will fix in future PR. - [x] Add quotes to all integers. - [x] Remove `rest_types` - [x] Address missing beacon block error. (#1629) - [x] ~~Add tests for lighthouse/peers endpoints~~ Wontfix - [x] ~~Follow up with validator status proposal~~ Tracked in #1434 - [x] Unify graffiti structs - [x] ~~Start server when waiting for genesis?~~ Will fix in future PR. - [x] TODO in http_api tests - [x] Move lighthouse endpoints off /eth/v1 - [x] Update docs to link to standard - ~~Blocked on #1586~~ Co-authored-by: Michael Sproul <michael@sigmaprime.io>
This commit is contained in:
168
common/warp_utils/src/reject.rs
Normal file
168
common/warp_utils/src/reject.rs
Normal file
@@ -0,0 +1,168 @@
|
||||
use eth2::types::ErrorMessage;
|
||||
use std::convert::Infallible;
|
||||
use warp::{http::StatusCode, reject::Reject};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BeaconChainError(pub beacon_chain::BeaconChainError);
|
||||
|
||||
impl Reject for BeaconChainError {}
|
||||
|
||||
pub fn beacon_chain_error(e: beacon_chain::BeaconChainError) -> warp::reject::Rejection {
|
||||
warp::reject::custom(BeaconChainError(e))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BeaconStateError(pub types::BeaconStateError);
|
||||
|
||||
impl Reject for BeaconStateError {}
|
||||
|
||||
pub fn beacon_state_error(e: types::BeaconStateError) -> warp::reject::Rejection {
|
||||
warp::reject::custom(BeaconStateError(e))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ArithError(pub safe_arith::ArithError);
|
||||
|
||||
impl Reject for ArithError {}
|
||||
|
||||
pub fn arith_error(e: safe_arith::ArithError) -> warp::reject::Rejection {
|
||||
warp::reject::custom(ArithError(e))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SlotProcessingError(pub state_processing::SlotProcessingError);
|
||||
|
||||
impl Reject for SlotProcessingError {}
|
||||
|
||||
pub fn slot_processing_error(e: state_processing::SlotProcessingError) -> warp::reject::Rejection {
|
||||
warp::reject::custom(SlotProcessingError(e))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BlockProductionError(pub beacon_chain::BlockProductionError);
|
||||
|
||||
impl Reject for BlockProductionError {}
|
||||
|
||||
pub fn block_production_error(e: beacon_chain::BlockProductionError) -> warp::reject::Rejection {
|
||||
warp::reject::custom(BlockProductionError(e))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CustomNotFound(pub String);
|
||||
|
||||
impl Reject for CustomNotFound {}
|
||||
|
||||
pub fn custom_not_found(msg: String) -> warp::reject::Rejection {
|
||||
warp::reject::custom(CustomNotFound(msg))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CustomBadRequest(pub String);
|
||||
|
||||
impl Reject for CustomBadRequest {}
|
||||
|
||||
pub fn custom_bad_request(msg: String) -> warp::reject::Rejection {
|
||||
warp::reject::custom(CustomBadRequest(msg))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CustomServerError(pub String);
|
||||
|
||||
impl Reject for CustomServerError {}
|
||||
|
||||
pub fn custom_server_error(msg: String) -> warp::reject::Rejection {
|
||||
warp::reject::custom(CustomServerError(msg))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BroadcastWithoutImport(pub String);
|
||||
|
||||
impl Reject for BroadcastWithoutImport {}
|
||||
|
||||
pub fn broadcast_without_import(msg: String) -> warp::reject::Rejection {
|
||||
warp::reject::custom(BroadcastWithoutImport(msg))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ObjectInvalid(pub String);
|
||||
|
||||
impl Reject for ObjectInvalid {}
|
||||
|
||||
pub fn object_invalid(msg: String) -> warp::reject::Rejection {
|
||||
warp::reject::custom(ObjectInvalid(msg))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NotSynced(pub String);
|
||||
|
||||
impl Reject for NotSynced {}
|
||||
|
||||
pub fn not_synced(msg: String) -> warp::reject::Rejection {
|
||||
warp::reject::custom(NotSynced(msg))
|
||||
}
|
||||
|
||||
/// This function receives a `Rejection` and tries to return a custom
|
||||
/// value, otherwise simply passes the rejection along.
|
||||
pub async fn handle_rejection(err: warp::Rejection) -> Result<impl warp::Reply, Infallible> {
|
||||
let code;
|
||||
let message;
|
||||
|
||||
if err.is_not_found() {
|
||||
code = StatusCode::NOT_FOUND;
|
||||
message = "NOT_FOUND".to_string();
|
||||
} else if let Some(e) = err.find::<warp::filters::body::BodyDeserializeError>() {
|
||||
message = format!("BAD_REQUEST: body deserialize error: {}", e);
|
||||
code = StatusCode::BAD_REQUEST;
|
||||
} else if let Some(e) = err.find::<warp::reject::InvalidQuery>() {
|
||||
code = StatusCode::BAD_REQUEST;
|
||||
message = format!("BAD_REQUEST: invalid query: {}", e);
|
||||
} else if let Some(e) = err.find::<crate::reject::BeaconChainError>() {
|
||||
code = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
message = format!("UNHANDLED_ERROR: {:?}", e.0);
|
||||
} else if let Some(e) = err.find::<crate::reject::BeaconStateError>() {
|
||||
code = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
message = format!("UNHANDLED_ERROR: {:?}", e.0);
|
||||
} else if let Some(e) = err.find::<crate::reject::SlotProcessingError>() {
|
||||
code = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
message = format!("UNHANDLED_ERROR: {:?}", e.0);
|
||||
} else if let Some(e) = err.find::<crate::reject::BlockProductionError>() {
|
||||
code = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
message = format!("UNHANDLED_ERROR: {:?}", e.0);
|
||||
} else if let Some(e) = err.find::<crate::reject::CustomNotFound>() {
|
||||
code = StatusCode::NOT_FOUND;
|
||||
message = format!("NOT_FOUND: {}", e.0);
|
||||
} else if let Some(e) = err.find::<crate::reject::CustomBadRequest>() {
|
||||
code = StatusCode::BAD_REQUEST;
|
||||
message = format!("BAD_REQUEST: {}", e.0);
|
||||
} else if let Some(e) = err.find::<crate::reject::CustomServerError>() {
|
||||
code = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
message = format!("INTERNAL_SERVER_ERROR: {}", e.0);
|
||||
} else if let Some(e) = err.find::<crate::reject::BroadcastWithoutImport>() {
|
||||
code = StatusCode::ACCEPTED;
|
||||
message = format!(
|
||||
"ACCEPTED: the object was broadcast to the network without being \
|
||||
fully imported to the local database: {}",
|
||||
e.0
|
||||
);
|
||||
} else if let Some(e) = err.find::<crate::reject::ObjectInvalid>() {
|
||||
code = StatusCode::BAD_REQUEST;
|
||||
message = format!("BAD_REQUEST: Invalid object: {}", e.0);
|
||||
} else if let Some(e) = err.find::<crate::reject::NotSynced>() {
|
||||
code = StatusCode::SERVICE_UNAVAILABLE;
|
||||
message = format!("SERVICE_UNAVAILABLE: beacon node is syncing: {}", e.0);
|
||||
} else if err.find::<warp::reject::MethodNotAllowed>().is_some() {
|
||||
code = StatusCode::METHOD_NOT_ALLOWED;
|
||||
message = "METHOD_NOT_ALLOWED".to_string();
|
||||
} else {
|
||||
code = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
message = "UNHANDLED_REJECTION".to_string();
|
||||
}
|
||||
|
||||
let json = warp::reply::json(&ErrorMessage {
|
||||
code: code.as_u16(),
|
||||
message,
|
||||
stacktraces: vec![],
|
||||
});
|
||||
|
||||
Ok(warp::reply::with_status(json, code))
|
||||
}
|
||||
Reference in New Issue
Block a user