Skip to content

Commit ca65a23

Browse files
committed
fix: return finalized state as SSZ
1 parent eae7adb commit ca65a23

File tree

3 files changed

+27
-39
lines changed

3 files changed

+27
-39
lines changed

crates/net/rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ tokio.workspace = true
1515
ethlambda-metrics.workspace = true
1616
tracing.workspace = true
1717
ethlambda-storage.workspace = true
18+
ethlambda-types.workspace = true
1819
serde.workspace = true
1920
serde_json.workspace = true
2021

crates/net/rpc/src/lib.rs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use std::net::SocketAddr;
22

33
use axum::{Json, Router, http::HeaderValue, http::header, response::IntoResponse, routing::get};
44
use ethlambda_storage::Store;
5+
use ethlambda_types::primitives::Encode;
56

6-
const JSON_CONTENT_TYPE: &str = "application/json; charset=utf-8";
7+
pub(crate) const JSON_CONTENT_TYPE: &str = "application/json; charset=utf-8";
8+
pub(crate) const SSZ_CONTENT_TYPE: &str = "application/octet-stream";
79

810
pub mod metrics;
911

@@ -37,7 +39,7 @@ async fn get_latest_finalized_state(
3739
let state = store
3840
.get_state(&finalized.root)
3941
.expect("finalized state exists");
40-
json_response(state)
42+
ssz_response(state.as_ssz_bytes())
4143
}
4244

4345
async fn get_latest_justified_state(
@@ -56,6 +58,15 @@ fn json_response<T: serde::Serialize>(value: T) -> axum::response::Response {
5658
response
5759
}
5860

61+
fn ssz_response(bytes: Vec<u8>) -> axum::response::Response {
62+
let mut response = bytes.into_response();
63+
response.headers_mut().insert(
64+
header::CONTENT_TYPE,
65+
HeaderValue::from_static(SSZ_CONTENT_TYPE),
66+
);
67+
response
68+
}
69+
5970
#[cfg(test)]
6071
mod tests {
6172
use super::*;
@@ -137,12 +148,15 @@ mod tests {
137148

138149
#[tokio::test]
139150
async fn test_get_latest_finalized_state() {
151+
use ethlambda_types::primitives::Encode;
152+
140153
let state = create_test_state();
141154
let store = Store::from_genesis(state);
142155

143-
// Get the expected state from the store to build expected JSON
156+
// Get the expected state from the store
144157
let finalized = store.latest_finalized();
145158
let expected_state = store.get_state(&finalized.root).unwrap();
159+
let expected_ssz = expected_state.as_ssz_bytes();
146160

147161
let app = build_api_router(store);
148162

@@ -157,39 +171,12 @@ mod tests {
157171
.unwrap();
158172

159173
assert_eq!(response.status(), StatusCode::OK);
160-
161-
let body = response.into_body().collect().await.unwrap().to_bytes();
162-
let returned_state: serde_json::Value = serde_json::from_slice(&body).unwrap();
163-
164-
let header = &expected_state.latest_block_header;
165174
assert_eq!(
166-
returned_state,
167-
json!({
168-
"config": {
169-
"genesis_time": expected_state.config.genesis_time
170-
},
171-
"slot": expected_state.slot,
172-
"latest_block_header": {
173-
"slot": header.slot,
174-
"proposer_index": header.proposer_index,
175-
"parent_root": format!("{:#x}", header.parent_root),
176-
"state_root": format!("{:#x}", header.state_root),
177-
"body_root": format!("{:#x}", header.body_root)
178-
},
179-
"latest_justified": {
180-
"slot": expected_state.latest_justified.slot,
181-
"root": format!("{:#x}", expected_state.latest_justified.root)
182-
},
183-
"latest_finalized": {
184-
"slot": expected_state.latest_finalized.slot,
185-
"root": format!("{:#x}", expected_state.latest_finalized.root)
186-
},
187-
"historical_block_hashes": [],
188-
"justified_slots": "0x01",
189-
"validators": [],
190-
"justifications_roots": [],
191-
"justifications_validators": "0x01"
192-
})
175+
response.headers().get(header::CONTENT_TYPE).unwrap(),
176+
SSZ_CONTENT_TYPE
193177
);
178+
179+
let body = response.into_body().collect().await.unwrap().to_bytes();
180+
assert_eq!(body.as_ref(), expected_ssz.as_slice());
194181
}
195182
}

crates/net/rpc/src/metrics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ pub fn start_prometheus_metrics_api() -> Router {
1010

1111
pub(crate) async fn get_health() -> impl IntoResponse {
1212
let mut response = r#"{"status":"healthy","service":"lean-spec-api"}"#.into_response();
13-
let content_type = HeaderValue::from_static("application/json; charset=utf-8");
14-
response
15-
.headers_mut()
16-
.insert(header::CONTENT_TYPE, content_type);
13+
response.headers_mut().insert(
14+
header::CONTENT_TYPE,
15+
HeaderValue::from_static(crate::JSON_CONTENT_TYPE),
16+
);
1717
response
1818
}
1919

0 commit comments

Comments
 (0)