@@ -13,9 +13,10 @@ use mithril_common::entities::{
13
13
} ;
14
14
use mithril_common:: StdResult ;
15
15
use mithril_persistence:: store:: StakeStorer ;
16
+ use tokio:: sync:: RwLockReadGuard ;
16
17
17
18
use crate :: dependency_injection:: SignerDependencyContainer ;
18
- use crate :: services:: MithrilProtocolInitializerBuilder ;
19
+ use crate :: services:: { EpochService , MithrilProtocolInitializerBuilder } ;
19
20
use crate :: Configuration ;
20
21
21
22
/// This trait is mainly intended for mocking.
@@ -103,22 +104,20 @@ impl SignerRunner {
103
104
104
105
/// Get the current signers with their stake.
105
106
async fn get_current_signers_with_stake ( & self ) -> StdResult < Vec < SignerWithStake > > {
106
- self . services
107
- . epoch_service
108
- . read ( )
109
- . await
110
- . current_signers_with_stake ( )
111
- . await
107
+ let epoch_service = self . epoch_service_read ( ) . await ;
108
+
109
+ epoch_service. current_signers_with_stake ( ) . await
112
110
}
113
111
114
112
/// Get the next signers with their stake.
115
113
async fn get_next_signers_with_stake ( & self ) -> StdResult < Vec < SignerWithStake > > {
116
- self . services
117
- . epoch_service
118
- . read ( )
119
- . await
120
- . next_signers_with_stake ( )
121
- . await
114
+ let epoch_service = self . epoch_service_read ( ) . await ;
115
+
116
+ epoch_service. next_signers_with_stake ( ) . await
117
+ }
118
+
119
+ async fn epoch_service_read ( & self ) -> RwLockReadGuard < ' _ , dyn EpochService > {
120
+ self . services . epoch_service . read ( ) . await
122
121
}
123
122
}
124
123
@@ -158,9 +157,14 @@ impl Runner for SignerRunner {
158
157
async fn register_signer_to_aggregator ( & self ) -> StdResult < ( ) > {
159
158
debug ! ( "RUNNER: register_signer_to_aggregator" ) ;
160
159
161
- let epoch_service = self . services . epoch_service . read ( ) . await ;
162
- let epoch = epoch_service. epoch_of_current_data ( ) ?;
163
- let protocol_parameters = epoch_service. next_protocol_parameters ( ) ?;
160
+ let ( epoch, protocol_parameters) = {
161
+ // Release the lock quickly at the end of this scope.
162
+ let epoch_service = self . services . epoch_service . read ( ) . await ;
163
+ let epoch = epoch_service. epoch_of_current_data ( ) ?;
164
+ let protocol_parameters = epoch_service. next_protocol_parameters ( ) ?;
165
+
166
+ ( epoch, protocol_parameters. clone ( ) )
167
+ } ;
164
168
165
169
let epoch_offset_to_recording_epoch = epoch. offset_to_recording_epoch ( ) ;
166
170
let stake_distribution = self
@@ -204,7 +208,7 @@ impl Runner for SignerRunner {
204
208
} ;
205
209
let protocol_initializer = MithrilProtocolInitializerBuilder :: build (
206
210
stake,
207
- protocol_parameters,
211
+ & protocol_parameters,
208
212
self . config . kes_secret_key_path . clone ( ) ,
209
213
kes_period,
210
214
) ?;
@@ -256,12 +260,7 @@ impl Runner for SignerRunner {
256
260
}
257
261
258
262
async fn can_sign_current_epoch ( & self ) -> StdResult < bool > {
259
- let epoch = self
260
- . services
261
- . epoch_service
262
- . read ( )
263
- . await
264
- . epoch_of_current_data ( ) ?;
263
+ let epoch = self . epoch_service_read ( ) . await . epoch_of_current_data ( ) ?;
265
264
266
265
if let Some ( protocol_initializer) = self
267
266
. services
@@ -271,15 +270,12 @@ impl Runner for SignerRunner {
271
270
{
272
271
debug ! ( " > got protocol initializer for this epoch ({epoch})" ) ;
273
272
274
- return self
275
- . services
276
- . epoch_service
277
- . read ( )
278
- . await
279
- . can_signer_sign_current_epoch (
280
- self . services . single_signer . get_party_id ( ) ,
281
- protocol_initializer,
282
- ) ;
273
+ let epoch_service = self . epoch_service_read ( ) . await ;
274
+
275
+ return epoch_service. can_signer_sign_current_epoch (
276
+ self . services . single_signer . get_party_id ( ) ,
277
+ protocol_initializer,
278
+ ) ;
283
279
} else {
284
280
warn ! ( " > NO protocol initializer found for this epoch ({epoch})" , ) ;
285
281
}
0 commit comments