Skip to content

Commit

Permalink
Remove slot mapping call from request and add 429 error.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSasaPrsic committed Feb 29, 2024
1 parent c73a7e4 commit 94034a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 23 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -414,44 +413,17 @@ async fn get_eth_head(State(state): State<Arc<AppState>>) -> 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::<BeaconAPIResponse>().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);
(
Expand All @@ -464,11 +436,19 @@ async fn get_eth_head(State(state): State<Arc<AppState>>) -> 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()})),
)
}
}
}
}
Expand Down

0 comments on commit 94034a2

Please sign in to comment.