Skip to content

Commit 464027b

Browse files
committed
refactor(protocol-config): use new shared aggregator client
1 parent 11709aa commit 464027b

File tree

7 files changed

+82
-155
lines changed

7 files changed

+82
-155
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/mithril-protocol-config/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ include = ["**/*.rs", "Cargo.toml", "README.md", ".gitignore"]
1212
[dependencies]
1313
anyhow = { workspace = true }
1414
async-trait = { workspace = true }
15+
mithril-aggregator-client = { path = "../mithril-aggregator-client" }
1516
mithril-common = { path = "../../mithril-common" }
1617
tokio = { workspace = true }
1718

1819
[dev-dependencies]
19-
http = "1.3.1"
2020
httpmock = "0.8.1"
21-
mockall = { workspace = true }
21+
serde_json = { workspace = true }
2222
slog = { workspace = true }
2323
slog-async = { workspace = true }
2424
slog-term = { workspace = true }

internal/mithril-protocol-config/src/http_client/http_impl.rs renamed to internal/mithril-protocol-config/src/http.rs

Lines changed: 64 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,24 @@ use anyhow::{Context, anyhow};
44
use async_trait::async_trait;
55
use std::sync::Arc;
66

7+
use mithril_aggregator_client::AggregatorHttpClient;
8+
use mithril_aggregator_client::query::GetProtocolConfigurationQuery;
79
use mithril_common::StdResult;
810
use mithril_common::entities::Epoch;
9-
use mithril_common::messages::ProtocolConfigurationMessage;
1011

1112
use crate::interface::MithrilNetworkConfigurationProvider;
1213
use crate::model::{MithrilNetworkConfiguration, MithrilNetworkConfigurationForEpoch};
1314

14-
/// Trait to retrieve protocol configuration
15-
#[cfg_attr(test, mockall::automock)]
16-
#[async_trait]
17-
pub trait ProtocolConfigurationRetrieverFromAggregator: Sync + Send {
18-
/// Retrieves protocol configuration for a given epoch from the aggregator
19-
async fn retrieve_protocol_configuration(
20-
&self,
21-
epoch: Epoch,
22-
) -> StdResult<ProtocolConfigurationMessage>;
23-
}
24-
2515
/// Structure implementing MithrilNetworkConfigurationProvider using HTTP.
2616
pub struct HttpMithrilNetworkConfigurationProvider {
27-
protocol_configuration_retriever: Arc<dyn ProtocolConfigurationRetrieverFromAggregator>,
17+
aggregator_http_client: Arc<AggregatorHttpClient>,
2818
}
2919

3020
impl HttpMithrilNetworkConfigurationProvider {
3121
/// HttpMithrilNetworkConfigurationProvider factory
32-
pub fn new(
33-
protocol_configuration_retriever: Arc<dyn ProtocolConfigurationRetrieverFromAggregator>,
34-
) -> Self {
22+
pub fn new(aggregator_http_client: Arc<AggregatorHttpClient>) -> Self {
3523
Self {
36-
protocol_configuration_retriever,
24+
aggregator_http_client,
3725
}
3826
}
3927
}
@@ -52,21 +40,30 @@ impl MithrilNetworkConfigurationProvider for HttpMithrilNetworkConfigurationProv
5240
let registration_epoch = epoch.offset_to_next_signer_retrieval_epoch().next();
5341

5442
let configuration_for_aggregation: MithrilNetworkConfigurationForEpoch = self
55-
.protocol_configuration_retriever
56-
.retrieve_protocol_configuration(aggregation_epoch)
43+
.aggregator_http_client
44+
.send(GetProtocolConfigurationQuery::epoch(aggregation_epoch))
5745
.await?
46+
.ok_or(anyhow!(
47+
"Missing network configuration for aggregation epoch {aggregation_epoch}"
48+
))?
5849
.into();
5950

6051
let configuration_for_next_aggregation = self
61-
.protocol_configuration_retriever
62-
.retrieve_protocol_configuration(next_aggregation_epoch)
52+
.aggregator_http_client
53+
.send(GetProtocolConfigurationQuery::epoch(next_aggregation_epoch))
6354
.await?
55+
.ok_or(anyhow!(
56+
"Missing network configuration for next aggregation epoch {next_aggregation_epoch}"
57+
))?
6458
.into();
6559

6660
let configuration_for_registration = self
67-
.protocol_configuration_retriever
68-
.retrieve_protocol_configuration(registration_epoch)
61+
.aggregator_http_client
62+
.send(GetProtocolConfigurationQuery::epoch(registration_epoch))
6963
.await?
64+
.ok_or(anyhow!(
65+
"Missing network configuration for registration epoch {registration_epoch}"
66+
))?
7067
.into();
7168

7269
configuration_for_aggregation.signed_entity_types_config.cardano_transactions.clone()
@@ -85,65 +82,57 @@ impl MithrilNetworkConfigurationProvider for HttpMithrilNetworkConfigurationProv
8582

8683
#[cfg(test)]
8784
mod tests {
88-
use mockall::predicate::eq;
85+
use httpmock::MockServer;
8986
use std::sync::Arc;
9087

91-
use mithril_common::{
92-
entities::{Epoch, ProtocolParameters},
93-
messages::ProtocolConfigurationMessage,
94-
test::double::Dummy,
95-
};
88+
use mithril_common::entities::ProtocolParameters;
89+
use mithril_common::messages::ProtocolConfigurationMessage;
90+
use mithril_common::test::double::Dummy;
91+
92+
use crate::test::test_tools::TestLogger;
9693

97-
use crate::{
98-
http_client::http_impl::{
99-
HttpMithrilNetworkConfigurationProvider,
100-
MockProtocolConfigurationRetrieverFromAggregator,
101-
},
102-
interface::MithrilNetworkConfigurationProvider,
103-
};
94+
use super::*;
10495

10596
#[tokio::test]
10697
async fn test_get_network_configuration_retrieve_configurations_for_aggregation_next_aggregation_and_registration()
10798
{
108-
let mut protocol_configuration_retriever =
109-
MockProtocolConfigurationRetrieverFromAggregator::new();
110-
111-
protocol_configuration_retriever
112-
.expect_retrieve_protocol_configuration()
113-
.once()
114-
.with(eq(Epoch(41)))
115-
.returning(|_| {
116-
Ok(ProtocolConfigurationMessage {
117-
protocol_parameters: ProtocolParameters::new(1000, 100, 0.1),
118-
..Dummy::dummy()
119-
})
120-
});
121-
122-
protocol_configuration_retriever
123-
.expect_retrieve_protocol_configuration()
124-
.once()
125-
.with(eq(Epoch(42)))
126-
.returning(|_| {
127-
Ok(ProtocolConfigurationMessage {
128-
protocol_parameters: ProtocolParameters::new(2000, 200, 0.2),
129-
..Dummy::dummy()
130-
})
131-
});
132-
133-
protocol_configuration_retriever
134-
.expect_retrieve_protocol_configuration()
135-
.once()
136-
.with(eq(Epoch(43)))
137-
.returning(|_| {
138-
Ok(ProtocolConfigurationMessage {
139-
protocol_parameters: ProtocolParameters::new(3000, 300, 0.3),
140-
..Dummy::dummy()
141-
})
142-
});
143-
144-
let mithril_configuration_provider = HttpMithrilNetworkConfigurationProvider::new(
145-
Arc::new(protocol_configuration_retriever),
146-
);
99+
let configuration_epoch_41 = ProtocolConfigurationMessage {
100+
protocol_parameters: ProtocolParameters::new(1000, 100, 0.1),
101+
..Dummy::dummy()
102+
};
103+
let configuration_epoch_42 = ProtocolConfigurationMessage {
104+
protocol_parameters: ProtocolParameters::new(2000, 200, 0.2),
105+
..Dummy::dummy()
106+
};
107+
let configuration_epoch_43 = ProtocolConfigurationMessage {
108+
protocol_parameters: ProtocolParameters::new(3000, 300, 0.3),
109+
..Dummy::dummy()
110+
};
111+
112+
let server = MockServer::start();
113+
server.mock(|when, then| {
114+
when.path("/protocol-configuration/41");
115+
then.status(200)
116+
.body(serde_json::to_string(&configuration_epoch_41).unwrap());
117+
});
118+
server.mock(|when, then| {
119+
when.path("/protocol-configuration/42");
120+
then.status(200)
121+
.body(serde_json::to_string(&configuration_epoch_42).unwrap());
122+
});
123+
server.mock(|when, then| {
124+
when.path("/protocol-configuration/43");
125+
then.status(200)
126+
.body(serde_json::to_string(&configuration_epoch_43).unwrap());
127+
});
128+
129+
let mithril_configuration_provider =
130+
HttpMithrilNetworkConfigurationProvider::new(Arc::new(
131+
AggregatorHttpClient::builder(server.base_url())
132+
.with_logger(TestLogger::stdout())
133+
.build()
134+
.unwrap(),
135+
));
147136

148137
let configuration = mithril_configuration_provider
149138
.get_network_configuration(Epoch(42))
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#![warn(missing_docs)]
22
//! This crate provides mechanisms to read and check the configuration parameters of a Mithril network.
33
4-
/// HTTP client Implementation for interacting with Mithril Network.
5-
pub mod http_client {
6-
pub mod http_impl;
7-
}
4+
pub mod http;
85
pub mod interface;
96
pub mod model;
107
pub mod test;

mithril-signer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ chrono = { workspace = true }
2323
clap = { workspace = true }
2424
config = { workspace = true }
2525
hex = { workspace = true }
26+
mithril-aggregator-client = { path = "../internal/mithril-aggregator-client" }
2627
mithril-cardano-node-chain = { path = "../internal/cardano-node/mithril-cardano-node-chain" }
2728
mithril-cardano-node-internal-database = { path = "../internal/cardano-node/mithril-cardano-node-internal-database" }
2829
mithril-cli-helper = { path = "../internal/mithril-cli-helper" }

mithril-signer/src/dependency_injection/builder.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use anyhow::{Context, anyhow};
66
use slog::Logger;
77
use tokio::sync::{Mutex, RwLock};
88

9+
use mithril_aggregator_client::AggregatorHttpClient;
910
use mithril_cardano_node_chain::{
1011
chain_observer::{CardanoCliRunner, ChainObserver, ChainObserverBuilder, ChainObserverType},
1112
chain_reader::PallasChainReader,
@@ -41,7 +42,7 @@ use mithril_persistence::database::repository::CardanoTransactionRepository;
4142
use mithril_persistence::database::{ApplicationNodeType, SqlMigration};
4243
use mithril_persistence::sqlite::{ConnectionBuilder, SqliteConnection, SqliteConnectionPool};
4344

44-
use mithril_protocol_config::http_client::http_impl::HttpMithrilNetworkConfigurationProvider;
45+
use mithril_protocol_config::http::HttpMithrilNetworkConfigurationProvider;
4546

4647
#[cfg(feature = "future_dmq")]
4748
use mithril_dmq::{DmqMessageBuilder, DmqPublisherClientPallas};
@@ -269,6 +270,14 @@ impl<'a> DependenciesBuilder<'a> {
269270
));
270271

271272
let api_version_provider = Arc::new(APIVersionProvider::new(era_checker.clone()));
273+
let aggregator_client_for_network_config = Arc::new(
274+
AggregatorHttpClient::builder(self.config.aggregator_endpoint.clone())
275+
.with_relay_endpoint(self.config.relay_endpoint.clone())
276+
.with_api_version_provider(api_version_provider.clone())
277+
.with_timeout(Duration::from_millis(HTTP_REQUEST_TIMEOUT_DURATION))
278+
.with_logger(self.root_logger())
279+
.build()?,
280+
);
272281
let aggregator_client = Arc::new(AggregatorHTTPClient::new(
273282
self.config.aggregator_endpoint.clone(),
274283
self.config.relay_endpoint.clone(),
@@ -376,7 +385,7 @@ impl<'a> DependenciesBuilder<'a> {
376385
));
377386
let metrics_service = Arc::new(MetricsService::new(self.root_logger())?);
378387
let network_configuration_service = Arc::new(HttpMithrilNetworkConfigurationProvider::new(
379-
aggregator_client.clone(),
388+
aggregator_client_for_network_config,
380389
));
381390
let preloader_activation = CardanoTransactionsPreloaderActivationSigner::new(
382391
network_configuration_service.clone(),

mithril-signer/src/services/aggregator_client.rs

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use anyhow::anyhow;
22
use async_trait::async_trait;
3-
use mithril_common::StdResult;
4-
use mithril_common::messages::ProtocolConfigurationMessage;
5-
use mithril_protocol_config::http_client::http_impl::ProtocolConfigurationRetrieverFromAggregator;
63
use reqwest::header::{self, HeaderValue};
74
use reqwest::{self, Client, Proxy, RequestBuilder, Response, StatusCode};
85
use semver::Version;
@@ -381,40 +378,6 @@ impl AggregatorClient for AggregatorHTTPClient {
381378
}
382379
}
383380

384-
#[async_trait]
385-
impl ProtocolConfigurationRetrieverFromAggregator for AggregatorHTTPClient {
386-
async fn retrieve_protocol_configuration(
387-
&self,
388-
epoch: Epoch,
389-
) -> StdResult<ProtocolConfigurationMessage> {
390-
debug!(self.logger, "Retrieve protocol configuration");
391-
let url = format!(
392-
"{}/protocol-configuration/{}",
393-
self.aggregator_endpoint, epoch
394-
);
395-
let response = self
396-
.prepare_request_builder(self.prepare_http_client()?.get(url.clone()))
397-
.send()
398-
.await;
399-
400-
match response {
401-
Ok(response) => match response.status() {
402-
StatusCode::OK => {
403-
self.warn_if_api_version_mismatch(&response);
404-
match response.json::<ProtocolConfigurationMessage>().await {
405-
Ok(message) => Ok(message),
406-
Err(err) => {
407-
Err(AggregatorClientError::JsonParseFailed(anyhow!(err)).into())
408-
}
409-
}
410-
}
411-
_ => Err(AggregatorClientError::from_response(response).await.into()),
412-
},
413-
Err(err) => Err(AggregatorClientError::RemoteServerUnreachable(anyhow!(err)).into()),
414-
}
415-
}
416-
}
417-
418381
#[cfg(test)]
419382
pub(crate) mod dumb {
420383
use mithril_common::test::double::Dummy;
@@ -681,39 +644,6 @@ mod tests {
681644
);
682645
}
683646

684-
mod protocol_configuration {
685-
686-
use super::*;
687-
688-
#[tokio::test]
689-
async fn test_ok_200() {
690-
let (server, client) = setup_server_and_client();
691-
let message_expected = ProtocolConfigurationMessage::dummy();
692-
let _server_mock = server.mock(|when, then| {
693-
when.path("/protocol-configuration/42");
694-
then.status(200).body(json!(message_expected).to_string());
695-
});
696-
697-
let message = client.retrieve_protocol_configuration(Epoch(42)).await.unwrap();
698-
699-
assert_eq!(message_expected, message);
700-
}
701-
702-
#[tokio::test]
703-
async fn test_ko_500() {
704-
let (server, client) = setup_server_and_client();
705-
let _server_mock = server.mock(|when, then| {
706-
when.path("/protocol-configuration/42");
707-
then.status(500).body("an error occurred");
708-
});
709-
710-
client
711-
.retrieve_protocol_configuration(Epoch(42))
712-
.await
713-
.expect_err("should throw a error");
714-
}
715-
}
716-
717647
#[tokio::test]
718648
async fn test_register_signer_ok_201() {
719649
let epoch = Epoch(1);

0 commit comments

Comments
 (0)