@@ -2,8 +2,10 @@ use std::net::SocketAddr;
22
33use axum:: { Json , Router , http:: HeaderValue , http:: header, response:: IntoResponse , routing:: get} ;
44use 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
810pub 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
4345async 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) ]
6071mod 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}
0 commit comments