Skip to content

Commit fbdbb05

Browse files
committed
Refactor client api version
1 parent c8645ff commit fbdbb05

File tree

8 files changed

+84
-111
lines changed

8 files changed

+84
-111
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-client/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ futures = "0.3"
2020
hex = "0.4.3"
2121
mithril-common = { path = "../mithril-common" }
2222
reqwest = { version = "0.11", features = ["json", "stream"] }
23+
semver = "1.0"
2324
serde = { version = "1.0", features = ["derive"] }
2425
serde_json = "1.0"
2526
slog = { version = "2.7.0", features = ["max_level_trace", "release_max_level_debug"] }

mithril-client/src/aggregator.rs

Lines changed: 57 additions & 62 deletions
Large diffs are not rendered by default.

mithril-client/src/commands/download.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ use std::{error::Error, fmt::Display, sync::Arc};
22

33
use clap::Parser;
44
use config::{builder::DefaultState, ConfigBuilder};
5-
use mithril_common::{
6-
api_version::APIVersionProvider,
7-
entities::Epoch,
8-
era::{EraChecker, SupportedEra},
9-
};
5+
use mithril_common::api_version::APIVersionProvider;
106
use serde::Serialize;
117
use slog_scope::debug;
128

@@ -41,17 +37,10 @@ impl DownloadCommand {
4137
.map_err(|e| format!("configuration deserialize error: {e}"))?;
4238
debug!("{:?}", config);
4339
let runtime = Runtime::new(config.network.clone());
44-
// TODO: This does not allow the client to handle an upgraded API version of a new supported era after the switch
45-
// In order to do so, we should retrieve the list of supported versions from the API Version provider and test them sequentially until one hopefully succeeds
46-
let era_checker = Arc::new(EraChecker::new(
47-
SupportedEra::eras().first().unwrap().to_owned(),
48-
Epoch(0),
49-
));
50-
let api_version_provider = Arc::new(APIVersionProvider::new(era_checker.clone()));
5140
let aggregator_handler = AggregatorHTTPClient::new(
5241
config.network.clone(),
5342
config.aggregator_endpoint,
54-
api_version_provider,
43+
APIVersionProvider::compute_all_versions_sorted()?,
5544
);
5645
let (from, to) = runtime
5746
.download_snapshot(

mithril-client/src/commands/list.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ use std::{error::Error, sync::Arc};
33
use clap::Parser;
44
use cli_table::{print_stdout, WithTitle};
55
use config::{builder::DefaultState, ConfigBuilder};
6-
use mithril_common::{
7-
api_version::APIVersionProvider,
8-
entities::Epoch,
9-
era::{EraChecker, SupportedEra},
10-
};
6+
use mithril_common::api_version::APIVersionProvider;
117
use slog_scope::debug;
128

139
use crate::{AggregatorHTTPClient, Config, Runtime};
@@ -34,17 +30,10 @@ impl ListCommand {
3430
.map_err(|e| format!("configuration deserialize error: {e}"))?;
3531
debug!("{:?}", config);
3632
let runtime = Runtime::new(config.network.clone());
37-
// TODO: This does not allow the client to handle an upgraded API version of a new supported era after the switch
38-
// In order to do so, we should retrieve the list of supported versions from the API Version provider and test them sequentially until one hopefully succeeds
39-
let era_checker = Arc::new(EraChecker::new(
40-
SupportedEra::eras().first().unwrap().to_owned(),
41-
Epoch(0),
42-
));
43-
let api_version_provider = Arc::new(APIVersionProvider::new(era_checker.clone()));
4433
let aggregator_handler = AggregatorHTTPClient::new(
4534
config.network.clone(),
4635
config.aggregator_endpoint,
47-
api_version_provider,
36+
APIVersionProvider::compute_all_versions_sorted()?,
4837
);
4938
let snapshot_list_items = runtime.list_snapshots(Arc::new(aggregator_handler)).await?;
5039

mithril-client/src/commands/restore.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ use mithril_common::{
99
cache::ImmutableFileDigestCacheProvider,
1010
cache::JsonImmutableFileDigestCacheProviderBuilder, CardanoImmutableDigester,
1111
},
12-
entities::Epoch,
13-
era::{EraChecker, SupportedEra},
1412
};
1513
use slog_scope::{debug, warn};
1614
use std::{error::Error, path::Path, sync::Arc};
@@ -52,17 +50,10 @@ impl RestoreCommand {
5250
.map_err(|e| format!("configuration deserialize error: {e}"))?;
5351
debug!("{:?}", config);
5452
let mut runtime = Runtime::new(config.network.clone());
55-
let era_checker = Arc::new(EraChecker::new(
56-
SupportedEra::eras().first().unwrap().to_owned(),
57-
Epoch(0),
58-
));
59-
// TODO: This does not allow the client to handle an upgraded API version of a new supported era after the switch
60-
// In order to do so, we should retrieve the list of supported versions from the API Version provider and test them sequentially until one hopefully succeeds
61-
let api_version_provider = Arc::new(APIVersionProvider::new(era_checker.clone()));
6253
let aggregator_handler = AggregatorHTTPClient::new(
6354
config.clone().network,
6455
config.clone().aggregator_endpoint,
65-
api_version_provider,
56+
APIVersionProvider::compute_all_versions_sorted()?,
6657
);
6758
let certificate_verifier = Box::new(MithrilCertificateVerifier::new(slog_scope::logger()));
6859
let genesis_verification_key = key_decode_hex(&config.genesis_verification_key)?;

mithril-client/src/commands/show.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ use std::{error::Error, sync::Arc};
33
use clap::Parser;
44
use cli_table::{print_stdout, WithTitle};
55
use config::{builder::DefaultState, ConfigBuilder};
6-
use mithril_common::{
7-
api_version::APIVersionProvider,
8-
entities::Epoch,
9-
era::{EraChecker, SupportedEra},
10-
};
6+
use mithril_common::api_version::APIVersionProvider;
117
use slog_scope::debug;
128

139
use crate::{convert_to_field_items, AggregatorHTTPClient, Config, Runtime};
@@ -37,17 +33,10 @@ impl ShowCommand {
3733
.map_err(|e| format!("configuration deserialize error: {e}"))?;
3834
debug!("{:?}", config);
3935
let runtime = Runtime::new(config.network.clone());
40-
// TODO: This does not allow the client to handle an upgraded API version of a new supported era after the switch
41-
// In order to do so, we should retrieve the list of supported versions from the API Version provider and test them sequentially until one hopefully succeeds
42-
let era_checker = Arc::new(EraChecker::new(
43-
SupportedEra::eras().first().unwrap().to_owned(),
44-
Epoch(0),
45-
));
46-
let api_version_provider = Arc::new(APIVersionProvider::new(era_checker.clone()));
4736
let aggregator_handler = AggregatorHTTPClient::new(
4837
config.network.clone(),
4938
config.aggregator_endpoint,
50-
api_version_provider,
39+
APIVersionProvider::compute_all_versions_sorted()?,
5140
);
5241
let snapshot = runtime
5342
.show_snapshot(Arc::new(aggregator_handler), &self.digest)

mithril-common/src/api_version.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ impl APIVersionProvider {
7575
Ok(VersionReq::parse(&version_req)?)
7676
}
7777

78+
/// Compute all the sorted list of all versions
79+
pub fn compute_all_versions_sorted() -> Result<Vec<Version>, APIVersionProviderError> {
80+
let mut versions = Vec::new();
81+
for version_raw in get_open_api_versions_mapping().into_values() {
82+
versions.push(Version::parse(&version_raw)?)
83+
}
84+
versions.sort();
85+
Ok(versions)
86+
}
87+
7888
/// Update open api versions. Test only
7989
#[cfg(any(test, feature = "test_only"))]
8090
pub fn update_open_api_versions(
@@ -170,4 +180,12 @@ mod test {
170180
.to_string()
171181
)
172182
}
183+
184+
#[test]
185+
fn test_compute_all_versions_sorted() {
186+
let all_versions_sorted = APIVersionProvider::compute_all_versions_sorted()
187+
.expect("Computing the list of all sorted versions should not fail");
188+
189+
assert!(!all_versions_sorted.is_empty());
190+
}
173191
}

0 commit comments

Comments
 (0)