Skip to content

Commit 911388b

Browse files
committed
refactor: renaming functions and add logs
1 parent 23111e8 commit 911388b

File tree

4 files changed

+58
-35
lines changed

4 files changed

+58
-35
lines changed

docs/website/root/mithril/mithril-network/signer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ In its initial version, the **Mithril signer** consists of a primary component:
5656

5757
- A runtime powered by a state machine:
5858
- The runtime operates synchronously and is programmed to run at consistent intervals
59-
- Four potential states exist: **INIT**, **UNREGISTERED**, **REGISTERED NOT ABLE TO SIGN** and **READY TO SIGN**.
59+
- Four potential states exist: **INIT**, **UNREGISTERED**, **READY TO SIGN** and **REGISTERED NOT ABLE TO SIGN**.
6060
- The runtime effectively manages state transitions
6161
- The runtime's framework is depicted in the diagram below:
6262

mithril-signer/src/runtime/runner.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,14 @@ impl Runner for SignerRunner {
268268
.await?
269269
{
270270
debug!(" > got protocol initializer for this epoch ({epoch})");
271-
return epoch_service.can_signer_sign_current_epoch(
271+
if epoch_service.is_signer_included_in_current_stake_distribution(
272272
self.services.single_signer.get_party_id(),
273273
protocol_initializer,
274-
);
274+
)? {
275+
return Ok(true);
276+
} else {
277+
debug!(" > Signer not in current stake distribution. Can NOT sign");
278+
}
275279
} else {
276280
warn!(" > NO protocol initializer found for this epoch ({epoch})",);
277281
}
@@ -280,11 +284,17 @@ impl Runner for SignerRunner {
280284
}
281285

282286
async fn can_sign_signed_entity_type(&self, signed_entity_type: &SignedEntityType) -> bool {
283-
!self
287+
if self
284288
.services
285289
.signed_entity_type_lock
286290
.is_locked(signed_entity_type)
287291
.await
292+
{
293+
debug!(" > signed entity type is locked, can NOT sign");
294+
false
295+
} else {
296+
true
297+
}
288298
}
289299

290300
async fn inform_epoch_settings(&self, epoch_settings: EpochSettings) -> StdResult<()> {
@@ -537,7 +547,7 @@ mod tests {
537547
Ok(vec![])
538548
}
539549

540-
fn can_signer_sign_current_epoch(
550+
fn is_signer_included_in_current_stake_distribution(
541551
&self,
542552
_party_id: PartyId,
543553
_protocol_initializer: ProtocolInitializer,

mithril-signer/src/runtime/state_machine.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub enum SignerState {
2727
ReadyToSign {
2828
/// Epoch when signer transited to the state.
2929
epoch: Epoch,
30-
/// Last signed entity type that the signer signed since beginning of this state.
30+
/// Last signed entity type that the signer signed on this epoch.
3131
last_signed_entity_type: Option<SignedEntityType>,
3232
},
3333

@@ -92,6 +92,13 @@ pub struct StateMachine {
9292
metrics_service: Arc<MetricsService>,
9393
}
9494

95+
enum ReadyToSignTransition {
96+
ToReadyToSign(CertificatePending),
97+
NoTransitionAlreadySigned,
98+
NoTransitionCannotSignPendingCertificate,
99+
NoTransitionNoPendingCertificate,
100+
}
101+
95102
impl StateMachine {
96103
/// Create a new StateMachine instance.
97104
pub fn new(
@@ -213,15 +220,16 @@ impl StateMachine {
213220
nested_error: Some(e),
214221
}
215222
})?;
216-
if let Some(new_state) = self
217-
.ready_to_sign_certificate_next_state(
223+
if let ReadyToSignTransition::ToReadyToSign(certificate) = self
224+
.ready_to_sign_certificate_next_transition(
218225
pending_certificate,
219226
last_signed_entity_type,
220-
epoch,
221227
)
222-
.await?
228+
.await
223229
{
224-
*state = new_state
230+
*state = self
231+
.transition_from_ready_to_sign_to_ready_to_sign(*epoch, &certificate)
232+
.await?;
225233
}
226234
}
227235
},
@@ -233,12 +241,11 @@ impl StateMachine {
233241
Ok(())
234242
}
235243

236-
async fn ready_to_sign_certificate_next_state(
244+
async fn ready_to_sign_certificate_next_transition(
237245
&self,
238246
certificate_pending: Option<CertificatePending>,
239247
last_signed_entity: &Option<SignedEntityType>,
240-
epoch: &Epoch,
241-
) -> Result<Option<SignerState>, RuntimeError> {
248+
) -> ReadyToSignTransition {
242249
fn is_same_signed_entity_type(
243250
signed_entity_type: &Option<SignedEntityType>,
244251
certificate: &CertificatePending,
@@ -257,31 +264,26 @@ impl StateMachine {
257264

258265
match certificate_pending {
259266
Some(certificate) if is_same_signed_entity_type(last_signed_entity, &certificate) => {
260-
info!(" ⋅ same entity type, cannot sign pending certificate, waiting…");
261-
Ok(None)
267+
info!(" ⋅ same entity type, already signed the pending certificate, waiting…");
268+
ReadyToSignTransition::NoTransitionAlreadySigned
262269
}
263270
Some(certificate) if can_sign_signed_entity_type(self, &certificate).await => {
264271
info!(
265-
" ⋅ Epoch has NOT changed but there is a pending certificate";
272+
" ⋅ Epoch has NOT changed we can sign this certificate, transiting to READY_TO_SIGN";
266273
"pending_certificate" => ?certificate,
267274
);
268-
info!(" → we can sign this certificate, transiting to READY_TO_SIGN");
269-
Ok(Some(
270-
self.transition_from_ready_to_sign_to_ready_to_sign(*epoch, &certificate)
271-
.await?,
272-
))
275+
ReadyToSignTransition::ToReadyToSign(certificate)
273276
}
274277
Some(certificate) => {
275278
info!(
276-
" ⋅ Epoch has NOT changed but there is a pending certificate";
279+
" ⋅ Epoch has NOT changed but cannot sign this pending certificate, waiting…";
277280
"pending_certificate" => ?certificate
278281
);
279-
info!(" ⋅ cannot sign this pending certificate, waiting…");
280-
Ok(None)
282+
ReadyToSignTransition::NoTransitionCannotSignPendingCertificate
281283
}
282284
None => {
283285
info!(" ⋅ no pending certificate, waiting…");
284-
Ok(None)
286+
ReadyToSignTransition::NoTransitionNoPendingCertificate
285287
}
286288
}
287289
}

mithril-signer/src/services/epoch_service.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ pub trait EpochService: Sync + Send {
4949
/// Get signers with stake for the next epoch
5050
async fn next_signers_with_stake(&self) -> StdResult<Vec<SignerWithStake>>;
5151

52-
/// Check if a signer can sign the current epoch
53-
fn can_signer_sign_current_epoch(
52+
/// Check if a signer is included in the current stake distribution
53+
fn is_signer_included_in_current_stake_distribution(
5454
&self,
5555
party_id: PartyId,
5656
protocol_initializer: ProtocolInitializer,
@@ -177,7 +177,7 @@ impl EpochService for MithrilEpochService {
177177
.await
178178
}
179179

180-
fn can_signer_sign_current_epoch(
180+
fn is_signer_included_in_current_stake_distribution(
181181
&self,
182182
party_id: PartyId,
183183
protocol_initializer: ProtocolInitializer,
@@ -210,7 +210,8 @@ mod tests {
210210
};
211211

212212
#[test]
213-
fn test_can_signer_sign_returns_error_when_epoch_settings_is_not_set() {
213+
fn test_is_signer_included_in_current_stake_distribution_returns_error_when_epoch_settings_is_not_set(
214+
) {
214215
let party_id = "party_id".to_string();
215216
let protocol_initializer = MithrilProtocolInitializerBuilder::build(
216217
&100,
@@ -223,12 +224,13 @@ mod tests {
223224
let service = MithrilEpochService::new(stake_store);
224225

225226
service
226-
.can_signer_sign_current_epoch(party_id, protocol_initializer)
227+
.is_signer_included_in_current_stake_distribution(party_id, protocol_initializer)
227228
.expect_err("can_signer_sign should return error when epoch settings is not set");
228229
}
229230

230231
#[test]
231-
fn test_can_signer_sign_returns_true_when_signer_verification_key_and_pool_id_found() {
232+
fn test_is_signer_included_in_current_stake_distribution_returns_true_when_signer_verification_key_and_pool_id_found(
233+
) {
232234
let fixtures = MithrilFixtureBuilder::default().with_signers(10).build();
233235
let protocol_initializer = fixtures.signers_fixture()[0]
234236
.protocol_initializer
@@ -249,19 +251,28 @@ mod tests {
249251

250252
let party_id = fixtures.signers_fixture()[0].party_id();
251253
assert!(service
252-
.can_signer_sign_current_epoch(party_id.clone(), protocol_initializer.clone())
254+
.is_signer_included_in_current_stake_distribution(
255+
party_id.clone(),
256+
protocol_initializer.clone()
257+
)
253258
.unwrap());
254259

255260
let party_id_not_included = fixtures.signers_fixture()[6].party_id();
256261
assert!(!service
257-
.can_signer_sign_current_epoch(party_id_not_included, protocol_initializer)
262+
.is_signer_included_in_current_stake_distribution(
263+
party_id_not_included,
264+
protocol_initializer
265+
)
258266
.unwrap());
259267

260268
let protocol_initializer_not_included = fixtures.signers_fixture()[6]
261269
.protocol_initializer
262270
.to_owned();
263271
assert!(!service
264-
.can_signer_sign_current_epoch(party_id, protocol_initializer_not_included)
272+
.is_signer_included_in_current_stake_distribution(
273+
party_id,
274+
protocol_initializer_not_included
275+
)
265276
.unwrap());
266277
}
267278

0 commit comments

Comments
 (0)