@@ -10,7 +10,9 @@ use std::sync::Arc;
1010use warp:: Future ;
1111use warp:: { http:: Method , http:: StatusCode , Filter } ;
1212
13- use super :: dependency:: { DependencyManager , MultiSignerWrapper , SnapshotStorerWrapper } ;
13+ use super :: dependency:: {
14+ BeaconStoreWrapper , DependencyManager , MultiSignerWrapper , SnapshotStorerWrapper ,
15+ } ;
1416use super :: multi_signer;
1517
1618const SERVER_BASE_PATH : & str = "aggregator" ;
@@ -67,10 +69,11 @@ mod router {
6769
6870 /// GET /certificate-pending
6971 pub fn certificate_pending (
70- _dependency_manager : Arc < DependencyManager > ,
72+ dependency_manager : Arc < DependencyManager > ,
7173 ) -> impl Filter < Extract = impl warp:: Reply , Error = warp:: Rejection > + Clone {
7274 warp:: path!( "certificate-pending" )
7375 . and ( warp:: get ( ) )
76+ . and ( with_beacon_store ( dependency_manager) )
7477 . and_then ( handlers:: certificate_pending)
7578 }
7679
@@ -138,19 +141,44 @@ mod router {
138141 ) -> impl Filter < Extract = ( MultiSignerWrapper , ) , Error = Infallible > + Clone {
139142 warp:: any ( ) . map ( move || dependency_manager. multi_signer . as_ref ( ) . unwrap ( ) . clone ( ) )
140143 }
144+
145+ /// With beacon store middleware
146+ fn with_beacon_store (
147+ dependency_manager : Arc < DependencyManager > ,
148+ ) -> impl Filter < Extract = ( BeaconStoreWrapper , ) , Error = Infallible > + Clone {
149+ warp:: any ( ) . map ( move || dependency_manager. beacon_store . as_ref ( ) . unwrap ( ) . clone ( ) )
150+ }
141151}
142152
143153mod handlers {
144154 use super :: * ;
145155
146156 /// Certificate Pending
147- pub async fn certificate_pending ( ) -> Result < impl warp:: Reply , Infallible > {
157+ pub async fn certificate_pending (
158+ beacon_store : BeaconStoreWrapper ,
159+ ) -> Result < impl warp:: Reply , Infallible > {
148160 debug ! ( "certificate_pending" ) ;
149161
150- // Certificate pending
151- let certificate_pending = fake_data:: certificate_pending ( ) ;
162+ let beacon_store = beacon_store. read ( ) . await ;
163+ match beacon_store. get_current_beacon ( ) . await {
164+ Ok ( Some ( beacon) ) => {
165+ let mut certificate_pending = fake_data:: certificate_pending ( ) ;
166+ certificate_pending. beacon = beacon;
152167
153- Ok ( warp:: reply:: json ( & certificate_pending) )
168+ Ok ( warp:: reply:: with_status (
169+ warp:: reply:: json ( & certificate_pending) ,
170+ StatusCode :: OK ,
171+ ) )
172+ }
173+ Ok ( None ) => Ok ( warp:: reply:: with_status (
174+ warp:: reply:: json ( & Null ) ,
175+ StatusCode :: NO_CONTENT ,
176+ ) ) ,
177+ Err ( err) => Ok ( warp:: reply:: with_status (
178+ warp:: reply:: json ( & entities:: Error :: new ( "MITHRIL-E0006" . to_string ( ) , err) ) ,
179+ StatusCode :: INTERNAL_SERVER_ERROR ,
180+ ) ) ,
181+ }
154182 }
155183
156184 /// Certificate by certificate hash
@@ -329,6 +357,7 @@ mod tests {
329357 use tokio:: sync:: RwLock ;
330358 use warp:: test:: request;
331359
360+ use super :: super :: beacon_store:: MockBeaconStore ;
332361 use super :: super :: entities:: * ;
333362 use super :: super :: multi_signer:: MockMultiSigner ;
334363 use super :: super :: snapshot_store:: MockSnapshotStorer ;
@@ -345,9 +374,66 @@ mod tests {
345374
346375 #[ tokio:: test]
347376 async fn test_certificate_pending_get_ok ( ) {
348- let dependency_manager = setup_dependency_manager ( ) ;
377+ let mut beacon_store = MockBeaconStore :: new ( ) ;
378+ beacon_store
379+ . expect_get_current_beacon ( )
380+ . return_once ( || Ok ( Some ( fake_data:: beacon ( ) ) ) ) ;
381+ let mut dependency_manager = setup_dependency_manager ( ) ;
382+ let method = Method :: GET . as_str ( ) ;
383+ let path = "/certificate-pending" ;
384+ dependency_manager. with_beacon_store ( Arc :: new ( RwLock :: new ( beacon_store) ) ) ;
385+
386+ let response = request ( )
387+ . method ( method)
388+ . path ( & format ! ( "/{}{}" , SERVER_BASE_PATH , path) )
389+ . reply ( & router:: routes ( Arc :: new ( dependency_manager) ) )
390+ . await ;
391+
392+ APISpec :: from_file ( API_SPEC_FILE )
393+ . method ( method)
394+ . path ( path)
395+ . validate_request ( & Null )
396+ . unwrap ( )
397+ . validate_response ( & response)
398+ . expect ( "OpenAPI error" ) ;
399+ }
400+
401+ #[ tokio:: test]
402+ async fn test_certificate_pending_get_ok_204 ( ) {
403+ let mut beacon_store = MockBeaconStore :: new ( ) ;
404+ beacon_store
405+ . expect_get_current_beacon ( )
406+ . return_once ( || Ok ( None ) ) ;
407+ let mut dependency_manager = setup_dependency_manager ( ) ;
408+ let method = Method :: GET . as_str ( ) ;
409+ let path = "/certificate-pending" ;
410+ dependency_manager. with_beacon_store ( Arc :: new ( RwLock :: new ( beacon_store) ) ) ;
411+
412+ let response = request ( )
413+ . method ( method)
414+ . path ( & format ! ( "/{}{}" , SERVER_BASE_PATH , path) )
415+ . reply ( & router:: routes ( Arc :: new ( dependency_manager) ) )
416+ . await ;
417+
418+ APISpec :: from_file ( API_SPEC_FILE )
419+ . method ( method)
420+ . path ( path)
421+ . validate_request ( & Null )
422+ . unwrap ( )
423+ . validate_response ( & response)
424+ . expect ( "OpenAPI error" ) ;
425+ }
426+
427+ #[ tokio:: test]
428+ async fn test_certificate_pending_get_ko ( ) {
429+ let mut beacon_store = MockBeaconStore :: new ( ) ;
430+ beacon_store
431+ . expect_get_current_beacon ( )
432+ . return_once ( || Err ( "an error occurred" . to_string ( ) ) ) ;
433+ let mut dependency_manager = setup_dependency_manager ( ) ;
349434 let method = Method :: GET . as_str ( ) ;
350435 let path = "/certificate-pending" ;
436+ dependency_manager. with_beacon_store ( Arc :: new ( RwLock :: new ( beacon_store) ) ) ;
351437
352438 let response = request ( )
353439 . method ( method)
0 commit comments