Skip to content

Commit c55e43b

Browse files
committed
wip(client-cli)
1 parent 6b35ce4 commit c55e43b

File tree

5 files changed

+97
-32
lines changed

5 files changed

+97
-32
lines changed

internal/mithril-aggregator-discovery/src/model.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ impl MithrilNetwork {
2525
}
2626
}
2727

28+
impl From<String> for MithrilNetwork {
29+
fn from(name: String) -> Self {
30+
MithrilNetwork::new(name)
31+
}
32+
}
33+
2834
/// Representation of an aggregator endpoint
2935
#[derive(Debug, Clone, PartialEq, Eq)]
3036
pub struct AggregatorEndpoint {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use clap::Parser;
2+
3+
use mithril_client::{AggregatorDiscoveryType, ClientBuilder, MithrilNetwork, MithrilResult};
4+
5+
/// Clap command to select an aggregator from the available ones with automatic discovery.
6+
#[derive(Parser, Debug, Clone)]
7+
pub struct AggregatorSelectCommand {
8+
/// Path to the Cardano node database directory.
9+
#[clap(long)]
10+
network: MithrilNetwork,
11+
}
12+
13+
impl AggregatorSelectCommand {
14+
/// Main command execution
15+
pub async fn execute(&self) -> MithrilResult<()> {
16+
let aggregator_endpoints =
17+
ClientBuilder::new(AggregatorDiscoveryType::Automatic(self.network.to_owned()))
18+
.with_default_aggregator_discoverer()
19+
.discover_aggregator(&self.network)?;
20+
21+
for endpoint in aggregator_endpoints.iter() {
22+
println!("Discovered aggregator endpoint: {:?}", endpoint);
23+
}
24+
25+
Ok(())
26+
}
27+
}

mithril-client-cli/src/commands/tools/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
//! Provides utility subcommands such as converting restored InMemory UTxO-HD ledger snapshot
44
//! to different flavors (Legacy, LMDB).
55
6+
mod aggregator_discovery;
67
mod snapshot_converter;
78

9+
pub use aggregator_discovery::*;
810
pub use snapshot_converter::*;
911

1012
use anyhow::anyhow;
@@ -18,13 +20,17 @@ pub enum ToolsCommands {
1820
/// UTxO-HD related commands
1921
#[clap(subcommand, name = "utxo-hd")]
2022
UTxOHD(UTxOHDCommands),
23+
/// Aggregator discovery related commands
24+
#[clap(subcommand, name = "aggregator-discovery")]
25+
AggregatorDiscovery(AggregatorDiscoveryCommands),
2126
}
2227

2328
impl ToolsCommands {
2429
/// Execute Tools command
2530
pub async fn execute(&self) -> MithrilResult<()> {
2631
match self {
2732
Self::UTxOHD(cmd) => cmd.execute().await,
33+
Self::AggregatorDiscovery(cmd) => cmd.execute().await,
2834
}
2935
}
3036
}
@@ -52,3 +58,20 @@ impl UTxOHDCommands {
5258
}
5359
}
5460
}
61+
62+
/// Aggregator discovery related commands
63+
#[derive(Subcommand, Debug, Clone)]
64+
pub enum AggregatorDiscoveryCommands {
65+
/// Select an aggregator from the available ones with automatic discovery
66+
#[clap(arg_required_else_help = false)]
67+
Select(AggregatorSelectCommand),
68+
}
69+
70+
impl AggregatorDiscoveryCommands {
71+
/// Execute Aggregator discovery command
72+
pub async fn execute(&self) -> MithrilResult<()> {
73+
match self {
74+
Self::Select(cmd) => cmd.execute().await,
75+
}
76+
}
77+
}

mithril-client/src/client.rs

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use std::collections::HashMap;
99
use std::sync::Arc;
1010

1111
use mithril_aggregator_discovery::{
12-
AggregatorDiscoverer, CapableAggregatorDiscoverer, HttpConfigAggregatorDiscoverer,
13-
MithrilNetwork, RequiredAggregatorCapabilities,
12+
AggregatorDiscoverer, AggregatorEndpoint, CapableAggregatorDiscoverer,
13+
HttpConfigAggregatorDiscoverer, MithrilNetwork, RequiredAggregatorCapabilities,
1414
};
1515
use mithril_common::api_version::APIVersionProvider;
1616
use 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}'")

mithril-client/src/type_alias.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,6 @@ pub mod common {
8989

9090
/// Required capabilities for an aggregator.
9191
pub use mithril_aggregator_discovery::RequiredAggregatorCapabilities;
92+
93+
/// Mithril network
94+
pub use mithril_aggregator_discovery::MithrilNetwork;

0 commit comments

Comments
 (0)