Skip to content

Commit b9862bd

Browse files
committed
refactor: Simplify code
1 parent adfed4e commit b9862bd

File tree

3 files changed

+33
-43
lines changed

3 files changed

+33
-43
lines changed

mithril-signer/src/runtime/runner.rs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use mithril_common::entities::{
1313
};
1414
use mithril_common::StdResult;
1515
use mithril_persistence::store::StakeStorer;
16+
use tokio::sync::RwLockReadGuard;
1617

1718
use crate::dependency_injection::SignerDependencyContainer;
18-
use crate::services::MithrilProtocolInitializerBuilder;
19+
use crate::services::{EpochService, MithrilProtocolInitializerBuilder};
1920
use crate::Configuration;
2021

2122
/// This trait is mainly intended for mocking.
@@ -103,22 +104,20 @@ impl SignerRunner {
103104

104105
/// Get the current signers with their stake.
105106
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
112110
}
113111

114112
/// Get the next signers with their stake.
115113
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
122121
}
123122
}
124123

@@ -158,9 +157,14 @@ impl Runner for SignerRunner {
158157
async fn register_signer_to_aggregator(&self) -> StdResult<()> {
159158
debug!("RUNNER: register_signer_to_aggregator");
160159

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+
};
164168

165169
let epoch_offset_to_recording_epoch = epoch.offset_to_recording_epoch();
166170
let stake_distribution = self
@@ -204,7 +208,7 @@ impl Runner for SignerRunner {
204208
};
205209
let protocol_initializer = MithrilProtocolInitializerBuilder::build(
206210
stake,
207-
protocol_parameters,
211+
&protocol_parameters,
208212
self.config.kes_secret_key_path.clone(),
209213
kes_period,
210214
)?;
@@ -256,12 +260,7 @@ impl Runner for SignerRunner {
256260
}
257261

258262
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()?;
265264

266265
if let Some(protocol_initializer) = self
267266
.services
@@ -271,15 +270,12 @@ impl Runner for SignerRunner {
271270
{
272271
debug!(" > got protocol initializer for this epoch ({epoch})");
273272

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+
);
283279
} else {
284280
warn!(" > NO protocol initializer found for this epoch ({epoch})",);
285281
}

mithril-signer/src/runtime/state_machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub enum SignerState {
2828
/// Time point when signer transited to the state.
2929
time_point: TimePoint,
3030

31-
/// Last signed entity type that the signer signed.
31+
/// Last signed entity type that the signer signed since beginning of this state.
3232
last_signed_entity_type: Option<SignedEntityType>,
3333
},
3434

mithril-signer/src/services/epoch_service.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,10 @@ impl EpochService for MithrilEpochService {
182182
party_id: PartyId,
183183
protocol_initializer: ProtocolInitializer,
184184
) -> StdResult<bool> {
185-
let current_signer = self
186-
.current_signers()?
187-
.iter()
188-
.find(|s| s.party_id == party_id)
189-
.cloned();
190-
let can_sign = current_signer.map_or(false, |s| {
191-
s.verification_key == protocol_initializer.verification_key().into()
192-
});
193-
194-
Ok(can_sign)
185+
Ok(self.current_signers()?.iter().any(|s| {
186+
s.party_id == party_id
187+
&& s.verification_key == protocol_initializer.verification_key().into()
188+
}))
195189
}
196190
}
197191

0 commit comments

Comments
 (0)