@@ -9,8 +9,8 @@ use std::collections::HashMap;
99use std:: sync:: Arc ;
1010
1111use mithril_aggregator_discovery:: {
12- AggregatorDiscoverer , CapableAggregatorDiscoverer , HttpConfigAggregatorDiscoverer ,
13- MithrilNetwork , RequiredAggregatorCapabilities ,
12+ AggregatorDiscoverer , AggregatorEndpoint , CapableAggregatorDiscoverer ,
13+ HttpConfigAggregatorDiscoverer , MithrilNetwork , RequiredAggregatorCapabilities ,
1414} ;
1515use mithril_common:: api_version:: APIVersionProvider ;
1616use mithril_common:: { MITHRIL_CLIENT_TYPE_HEADER , MITHRIL_ORIGIN_TAG_HEADER } ;
@@ -294,8 +294,7 @@ impl ClientBuilder {
294294 None => {
295295 return Err ( anyhow ! (
296296 "The genesis verification key must be provided to build the client with the 'with_genesis_verification_key' function"
297- )
298- . into ( ) ) ;
297+ ) ) ;
299298 }
300299 } ;
301300
@@ -405,40 +404,47 @@ impl ClientBuilder {
405404 } )
406405 }
407406
407+ /// Discover available aggregator endpoints for the given Mithril network and required capabilities.
408+ pub fn discover_aggregator (
409+ & self ,
410+ network : & MithrilNetwork ,
411+ ) -> MithrilResult < impl Iterator < Item = AggregatorEndpoint > > {
412+ match self . aggregator_discoverer . clone ( ) {
413+ Some ( discoverer) => {
414+ let discoverer = if let Some ( capabilities) = & self . aggregator_capabilities {
415+ Arc :: new ( CapableAggregatorDiscoverer :: new (
416+ capabilities. to_owned ( ) ,
417+ discoverer. clone ( ) ,
418+ ) ) as Arc < dyn AggregatorDiscoverer >
419+ } else {
420+ discoverer as Arc < dyn AggregatorDiscoverer >
421+ } ;
422+ tokio:: task:: block_in_place ( move || {
423+ tokio:: runtime:: Handle :: current ( ) . block_on ( async move {
424+ discoverer
425+ . get_available_aggregators ( network. to_owned ( ) )
426+ . await
427+ . with_context ( || "Discovering aggregator endpoint failed" )
428+ } )
429+ } )
430+ }
431+ None => Err ( anyhow ! (
432+ "The aggregator discoverer must be provided to build the client with automatic discovery using the 'with_aggregator_discoverer' function"
433+ ) ) ,
434+ }
435+ }
436+
408437 fn build_aggregator_client (
409438 & self ,
410439 logger : Logger ,
411440 ) -> Result < AggregatorHTTPClient , anyhow:: Error > {
412441 let aggregator_endpoint = match self . aggregator_discovery {
413442 AggregatorDiscoveryType :: Url ( ref url) => url. clone ( ) ,
414- AggregatorDiscoveryType :: Automatic ( ref network) => {
415- match self . aggregator_discoverer . clone ( ) {
416- Some ( discoverer) => {
417- let discoverer = if let Some ( capabilities) = & self . aggregator_capabilities {
418- Arc :: new ( CapableAggregatorDiscoverer :: new (
419- capabilities. to_owned ( ) ,
420- discoverer. clone ( ) ,
421- ) ) as Arc < dyn AggregatorDiscoverer >
422- } else {
423- discoverer as Arc < dyn AggregatorDiscoverer >
424- } ;
425- tokio:: task:: block_in_place ( move || {
426- tokio:: runtime:: Handle :: current ( ) . block_on ( async move {
427- discoverer
428- . get_available_aggregators ( network. to_owned ( ) )
429- . await
430- . with_context ( || "Discovering aggregator endpoint failed" ) ?
431- . next ( )
432- . ok_or ( anyhow ! ( "No aggregator was available through discovery" ) )
433- } )
434- } ) ?
435- . into ( )
436- }
437- None => {
438- return Err ( anyhow ! ( "The aggregator discoverer must be provided to build the client with automatic discovery using the 'with_aggregator_discoverer' function" ) . into ( ) ) ;
439- }
440- }
441- }
443+ AggregatorDiscoveryType :: Automatic ( ref network) => self
444+ . discover_aggregator ( network) ?
445+ . next ( )
446+ . ok_or_else ( || anyhow ! ( "No aggregator was available through discovery" ) ) ?
447+ . into ( ) ,
442448 } ;
443449 let endpoint_url = Url :: parse ( & aggregator_endpoint) . with_context ( || {
444450 format ! ( "Invalid aggregator endpoint, it must be a correctly formed url: '{aggregator_endpoint}'" )
0 commit comments