2022-07-25 08:23:00 +00:00
|
|
|
use crate::api_types::{
|
|
|
|
EndpointVersion, ExecutionOptimisticForkVersionedResponse, ForkVersionedResponse,
|
|
|
|
};
|
2021-10-28 01:18:04 +00:00
|
|
|
use eth2::CONSENSUS_VERSION_HEADER;
|
2021-08-06 00:47:31 +00:00
|
|
|
use serde::Serialize;
|
2021-10-28 01:18:04 +00:00
|
|
|
use types::{ForkName, InconsistentFork};
|
|
|
|
use warp::reply::{self, Reply, WithHeader};
|
2021-08-06 00:47:31 +00:00
|
|
|
|
|
|
|
pub const V1: EndpointVersion = EndpointVersion(1);
|
|
|
|
pub const V2: EndpointVersion = EndpointVersion(2);
|
|
|
|
|
|
|
|
pub fn fork_versioned_response<T: Serialize>(
|
|
|
|
endpoint_version: EndpointVersion,
|
2021-10-28 01:18:04 +00:00
|
|
|
fork_name: ForkName,
|
2021-08-06 00:47:31 +00:00
|
|
|
data: T,
|
|
|
|
) -> Result<ForkVersionedResponse<T>, warp::reject::Rejection> {
|
|
|
|
let fork_name = if endpoint_version == V1 {
|
|
|
|
None
|
|
|
|
} else if endpoint_version == V2 {
|
2021-10-28 01:18:04 +00:00
|
|
|
Some(fork_name)
|
2021-08-06 00:47:31 +00:00
|
|
|
} else {
|
|
|
|
return Err(unsupported_version_rejection(endpoint_version));
|
|
|
|
};
|
|
|
|
Ok(ForkVersionedResponse {
|
|
|
|
version: fork_name,
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-25 08:23:00 +00:00
|
|
|
pub fn execution_optimistic_fork_versioned_response<T: Serialize>(
|
|
|
|
endpoint_version: EndpointVersion,
|
|
|
|
fork_name: ForkName,
|
|
|
|
execution_optimistic: bool,
|
|
|
|
data: T,
|
|
|
|
) -> Result<ExecutionOptimisticForkVersionedResponse<T>, warp::reject::Rejection> {
|
|
|
|
let fork_name = if endpoint_version == V1 {
|
|
|
|
None
|
|
|
|
} else if endpoint_version == V2 {
|
|
|
|
Some(fork_name)
|
|
|
|
} else {
|
|
|
|
return Err(unsupported_version_rejection(endpoint_version));
|
|
|
|
};
|
|
|
|
Ok(ExecutionOptimisticForkVersionedResponse {
|
|
|
|
version: fork_name,
|
|
|
|
execution_optimistic: Some(execution_optimistic),
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-28 01:18:04 +00:00
|
|
|
/// Add the `Eth-Consensus-Version` header to a response.
|
|
|
|
pub fn add_consensus_version_header<T: Reply>(reply: T, fork_name: ForkName) -> WithHeader<T> {
|
|
|
|
reply::with_header(reply, CONSENSUS_VERSION_HEADER, fork_name.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inconsistent_fork_rejection(error: InconsistentFork) -> warp::reject::Rejection {
|
|
|
|
warp_utils::reject::custom_server_error(format!("wrong fork: {:?}", error))
|
|
|
|
}
|
|
|
|
|
2021-08-06 00:47:31 +00:00
|
|
|
pub fn unsupported_version_rejection(version: EndpointVersion) -> warp::reject::Rejection {
|
|
|
|
warp_utils::reject::custom_bad_request(format!("Unsupported endpoint version: {}", version))
|
|
|
|
}
|