@@ -8,9 +8,11 @@ use acropolis_common::{
88 accounts:: { AccountsStateQuery , AccountsStateQueryResponse } ,
99 epochs:: { EpochsStateQuery , EpochsStateQueryResponse } ,
1010 parameters:: { ParametersStateQuery , ParametersStateQueryResponse } ,
11+ pools:: { PoolsStateQuery , PoolsStateQueryResponse } ,
1112 spdd:: { SPDDStateQuery , SPDDStateQueryResponse } ,
1213 utils:: query_state,
1314 } ,
15+ serialization:: Bech32WithHrp ,
1416} ;
1517use anyhow:: { anyhow, Result } ;
1618use caryatid_sdk:: Context ;
@@ -417,9 +419,71 @@ pub async fn handle_epoch_total_blocks_blockfrost(
417419}
418420
419421pub async fn handle_epoch_pool_blocks_blockfrost (
420- _context : Arc < Context < Message > > ,
421- _params : Vec < String > ,
422- _handlers_config : Arc < HandlersConfig > ,
422+ context : Arc < Context < Message > > ,
423+ params : Vec < String > ,
424+ handlers_config : Arc < HandlersConfig > ,
423425) -> Result < RESTResponse > {
424- Ok ( RESTResponse :: with_text ( 501 , "Not implemented" ) )
426+ if params. len ( ) != 2 {
427+ return Ok ( RESTResponse :: with_text (
428+ 400 ,
429+ "Expected two parameters: an epoch number and a pool ID" ,
430+ ) ) ;
431+ }
432+ let epoch_number_param = & params[ 0 ] ;
433+ let pool_id_param = & params[ 1 ] ;
434+
435+ let epoch_number = match epoch_number_param. parse :: < u64 > ( ) {
436+ Ok ( num) => num,
437+ Err ( _) => {
438+ return Ok ( RESTResponse :: with_text (
439+ 400 ,
440+ "Invalid epoch number parameter" ,
441+ ) ) ;
442+ }
443+ } ;
444+
445+ let Ok ( spo) = Vec :: < u8 > :: from_bech32_with_hrp ( pool_id_param, "pool" ) else {
446+ return Ok ( RESTResponse :: with_text (
447+ 400 ,
448+ & format ! ( "Invalid Bech32 stake pool ID: {pool_id_param}" ) ,
449+ ) ) ;
450+ } ;
451+
452+ // query Pool's Blocks by epoch from spo-state
453+ let msg = Arc :: new ( Message :: StateQuery ( StateQuery :: Pools (
454+ PoolsStateQuery :: GetBlocksByPoolAndEpoch {
455+ pool_id : spo. clone ( ) ,
456+ epoch : epoch_number,
457+ } ,
458+ ) ) ) ;
459+
460+ let blocks = query_state (
461+ & context,
462+ & handlers_config. pools_query_topic ,
463+ msg,
464+ |message| match message {
465+ Message :: StateQueryResponse ( StateQueryResponse :: Pools (
466+ PoolsStateQueryResponse :: BlocksByPoolAndEpoch ( blocks) ,
467+ ) ) => Ok ( blocks) ,
468+ Message :: StateQueryResponse ( StateQueryResponse :: Pools (
469+ PoolsStateQueryResponse :: Error ( e) ,
470+ ) ) => Err ( anyhow:: anyhow!(
471+ "Internal server error while retrieving pool block hashes by epoch: {e}"
472+ ) ) ,
473+ _ => Err ( anyhow:: anyhow!( "Unexpected message type" ) ) ,
474+ } ,
475+ )
476+ . await ?;
477+
478+ // NOTE:
479+ // Need to query chain_store
480+ // to get block_hash for each block height
481+
482+ match serde_json:: to_string_pretty ( & blocks) {
483+ Ok ( json) => Ok ( RESTResponse :: with_json ( 200 , & json) ) ,
484+ Err ( e) => Ok ( RESTResponse :: with_text (
485+ 500 ,
486+ & format ! ( "Internal server error while retrieving pool block hashes by epoch: {e}" ) ,
487+ ) ) ,
488+ }
425489}
0 commit comments