@@ -4,36 +4,24 @@ use anyhow::{Context, anyhow};
44use async_trait:: async_trait;
55use std:: sync:: Arc ;
66
7+ use mithril_aggregator_client:: AggregatorHttpClient ;
8+ use mithril_aggregator_client:: query:: GetProtocolConfigurationQuery ;
79use mithril_common:: StdResult ;
810use mithril_common:: entities:: Epoch ;
9- use mithril_common:: messages:: ProtocolConfigurationMessage ;
1011
1112use crate :: interface:: MithrilNetworkConfigurationProvider ;
1213use 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.
2616pub struct HttpMithrilNetworkConfigurationProvider {
27- protocol_configuration_retriever : Arc < dyn ProtocolConfigurationRetrieverFromAggregator > ,
17+ aggregator_http_client : Arc < AggregatorHttpClient > ,
2818}
2919
3020impl 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) ]
8784mod 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 ) )
0 commit comments