Skip to content

Commit

Permalink
refacto: function renaming and clean imports
Browse files Browse the repository at this point in the history
  • Loading branch information
sfauvel committed Sep 16, 2024
1 parent ffe73cc commit 54e66db
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
7 changes: 2 additions & 5 deletions mithril-signer/src/runtime/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use anyhow::Context;
use async_trait::async_trait;
use slog_scope::{debug, info, warn};
use thiserror::Error;

#[cfg(test)]
use mockall::automock;
use tokio::sync::RwLockReadGuard;

use mithril_common::crypto_helper::{KESPeriod, OpCert, ProtocolOpCert, SerDeShelleyFileFormat};
use mithril_common::entities::{
Expand All @@ -13,7 +11,6 @@ use mithril_common::entities::{
};
use mithril_common::StdResult;
use mithril_persistence::store::StakeStorer;
use tokio::sync::RwLockReadGuard;

use crate::dependency_injection::SignerDependencyContainer;
use crate::services::{EpochService, MithrilProtocolInitializerBuilder};
Expand Down Expand Up @@ -121,7 +118,7 @@ impl SignerRunner {
}
}

#[cfg_attr(test, automock)]
#[cfg_attr(test, mockall::automock)]
#[async_trait]
impl Runner for SignerRunner {
async fn get_epoch_settings(&self) -> StdResult<Option<EpochSettings>> {
Expand Down
31 changes: 13 additions & 18 deletions mithril-signer/src/runtime/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ pub enum SignerState {

/// `ReadyToSign` state. The signer is registered and ready to sign new messages.
ReadyToSign {
/// Epoch when signer transited to the state.
/// Epoch when signer transitioned to the state.
epoch: Epoch,
/// Last signed entity type that the signer signed on this epoch.
last_signed_entity_type: Option<SignedEntityType>,
},

/// `RegisteredNotAbleToSign` state. The signer is registered but not able to sign for the duration of the epoch.
RegisteredNotAbleToSign {
/// Epoch when signer transited to the state.
/// Epoch when signer transitioned to the state.
epoch: Epoch,
},
}
Expand All @@ -46,23 +46,17 @@ impl SignerState {

/// Returns `true` if the state in `Unregistered`
pub fn is_unregistered(&self) -> bool {
matches!(*self, SignerState::Unregistered { epoch: _ })
matches!(*self, SignerState::Unregistered { .. })
}

/// Returns `true` if the state in `ReadyToSign`
pub fn is_ready_to_sign(&self) -> bool {
matches!(
*self,
SignerState::ReadyToSign {
epoch: _,
last_signed_entity_type: _
}
)
matches!(*self, SignerState::ReadyToSign { .. })
}

/// Returns `true` if the state in `RegisteredNotAbleToSign`
pub fn is_registered_not_able_to_sign(&self) -> bool {
matches!(*self, SignerState::RegisteredNotAbleToSign { epoch: _ })
matches!(*self, SignerState::RegisteredNotAbleToSign { .. })
}
}

Expand Down Expand Up @@ -678,12 +672,13 @@ mod tests {
.await
.expect("Cycling the state machine should not fail");

if !state_machine.get_state().await.is_ready_to_sign() {
panic!(
"state machine did not return a ReadyToSign state but {:?}",
state_machine.get_state().await
);
}
assert_eq!(
SignerState::ReadyToSign {
epoch: TimePoint::dummy().epoch,
last_signed_entity_type: None
},
state_machine.get_state().await
);
}

#[tokio::test]
Expand Down Expand Up @@ -815,7 +810,7 @@ mod tests {
}

#[tokio::test]
async fn ready_to_sign_to_ready_to_sign_when_same_state_than_previous_one() {
async fn ready_to_sign_to_ready_to_sign_when_same_signed_entity_type() {
let time_point = TimePoint::dummy();
let certificate_pending = CertificatePending {
epoch: time_point.clone().epoch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ async fn test_create_cardano_transaction_single_signature() {
.cycle_unregistered().await.unwrap()

.comment("signer can now create a single signature → ReadyToSign")
.cycle_ready_to_sign_no_registration().await.unwrap()
.cycle_ready_to_sign_without_signature_registration().await.unwrap()

.comment("creating a new certificate pending with a cardano transaction signed entity → ReadyToSign")
.increase_block_number_and_slot_number(70, SlotNumber(80), BlockNumber(170)).await.unwrap()
.cycle_ready_to_sign_with_signature_registration().await.unwrap()

.comment("more cycles do not change the state = ReadyToSign")
.cycle_ready_to_sign_no_registration().await.unwrap()
.cycle_ready_to_sign_no_registration().await.unwrap()
.cycle_ready_to_sign_without_signature_registration().await.unwrap()
.cycle_ready_to_sign_without_signature_registration().await.unwrap()

.comment("new blocks means a new signature with the same stake distribution → ReadyToSign")
.increase_block_number_and_slot_number(125, SlotNumber(205), BlockNumber(295)).await.unwrap()
Expand Down
4 changes: 3 additions & 1 deletion mithril-signer/tests/test_extensions/state_machine_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ impl StateMachineTester {
)
}

pub async fn cycle_ready_to_sign_no_registration(&mut self) -> Result<&mut Self> {
pub async fn cycle_ready_to_sign_without_signature_registration(
&mut self,
) -> Result<&mut Self> {
let metric_before = self
.metrics_service
.signature_registration_success_since_startup_counter_get();
Expand Down

0 comments on commit 54e66db

Please sign in to comment.