diff --git a/README.md b/README.md index 3501217..bc6513b 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ RUSTFLAGS="-C target-cpu=native" cargo run --profile maxperf * Response ```json - {"ethBlockNumber":5380696,"slot":4449152,"timestamp":1709124660,"timestampDiff":1396} + {"slot":4454752,"timestamp":1709191840,"timestampDiff":1716} ``` ### Get current Avail head diff --git a/src/main.rs b/src/main.rs index cc46144..666ac24 100644 --- a/src/main.rs +++ b/src/main.rs @@ -134,7 +134,6 @@ struct EthProofResponse { #[serde(rename_all = "camelCase")] struct HeadResponse { pub slot: u64, - pub eth_block_number: u32, pub timestamp: u64, pub timestamp_diff: u64, } @@ -414,44 +413,17 @@ async fn get_eth_head(State(state): State>) -> impl IntoResponse { sp_core::bytes::from_hex(timestamp_storage_response.as_str()).unwrap(); let timestamp_input = &mut timestamp_from_hex.as_slice(); let timestamp: u64 = Decode::decode(timestamp_input).unwrap(); - - let url = format!("{}/{}", state.beaconchain_base_url, slot); - let resp = state.request_client.get(url).send().await; - - match resp { - Ok(ok) => { - if let Ok(response_data) = ok.json::().await { - let now = Utc::now().timestamp() as u64; - ( - StatusCode::OK, - [("Cache-Control", "public, max-age=31536000")], - Json(json!(HeadResponse { - slot, - timestamp, - timestamp_diff: (now - timestamp), - eth_block_number: response_data.data.exec_block_number, - })), - ) - } else { - tracing::error!("Cannot get beacon api response."); - ( - StatusCode::INTERNAL_SERVER_ERROR, - [("Cache-Control", "max-age=300, must-revalidate")], - Json(json!({ "error": "Cannot get beacon api response"})), - ) - } - } - Err(err) => { - tracing::error!("Cannot get beacon api response: {:?}.", err); - ( - StatusCode::INTERNAL_SERVER_ERROR, - [("Cache-Control", "max-age=300, must-revalidate")], - Json(json!({ "error": err.to_string()})), - ) - } - } + let now = Utc::now().timestamp() as u64; + ( + StatusCode::OK, + [("Cache-Control", "public, max-age=31536000")], + Json(json!(HeadResponse { + slot, + timestamp, + timestamp_diff: (now - timestamp), + })), + ) } - Err(err) => { tracing::error!("Cannot get timestamp storage: {:?}", err); ( @@ -464,11 +436,19 @@ async fn get_eth_head(State(state): State>) -> impl IntoResponse { } Err(err) => { tracing::error!("Cannot get head storage: {:?}", err.to_string()); - ( - StatusCode::INTERNAL_SERVER_ERROR, - [("Cache-Control", "max-age=300, must-revalidate")], - Json(json!({ "error": err.to_string()})), - ) + if err.to_string().ends_with("status code: 429") { + ( + StatusCode::TOO_MANY_REQUESTS, + [("Cache-Control", "max-age=300, must-revalidate")], + Json(json!({ "error": err.to_string()})), + ) + } else { + ( + StatusCode::INTERNAL_SERVER_ERROR, + [("Cache-Control", "max-age=300, must-revalidate")], + Json(json!({ "error": err.to_string()})), + ) + } } } }