-
Notifications
You must be signed in to change notification settings - Fork 44
Feat: implement a signature processor for DMQ #2477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jpraynaud
wants to merge
8
commits into
main
Choose a base branch
from
jpraynaud/2470-signature-processor-dmq
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+314
−8
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5ec7422
feat(aggregator): add a 'SignatureConsumer' trait
jpraynaud 3eb9fce
feat(aggregator): implement a 'SignatureConsumerNoop' for 'SignatureC…
jpraynaud 36d410b
feat(aggregator): add a 'SignatureProcessor' trait
jpraynaud d5f4069
feat(aggregator): implement a 'SequentialSignatureProcessor' for 'Sig…
jpraynaud abf5efc
feat(aggregator): add build 'SequentialSignatureProcessor' in DI
jpraynaud 70bbddd
feat(aggregator): wire 'SequentialSignatureProcessor' in serve command
jpraynaud a59552a
refactor(signer): avoid redundancies in signature publisher sub modul…
jpraynaud 1097177
chore(aggregator): fix clippy warnings
jpraynaud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
mithril-aggregator/src/services/signature_consumer/interface.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use mithril_common::{ | ||
entities::{SignedEntityType, SingleSignature}, | ||
StdResult, | ||
}; | ||
|
||
/// A signature consumer which blocks until a messages are available. | ||
#[cfg_attr(test, mockall::automock)] | ||
#[async_trait::async_trait] | ||
pub trait SignatureConsumer: Sync + Send { | ||
/// Returns signatures when available | ||
async fn get_signatures(&self) -> StdResult<Vec<(SingleSignature, SignedEntityType)>>; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod interface; | ||
mod noop; | ||
|
||
pub use interface::*; | ||
pub use noop::*; |
42 changes: 42 additions & 0 deletions
42
mithril-aggregator/src/services/signature_consumer/noop.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::future; | ||
|
||
use async_trait::async_trait; | ||
|
||
use super::SignatureConsumer; | ||
|
||
/// A no-op implementation of the [SignatureConsumer] trait that will never return signatures. | ||
pub struct SignatureConsumerNoop; | ||
|
||
#[async_trait] | ||
impl SignatureConsumer for SignatureConsumerNoop { | ||
async fn get_signatures( | ||
&self, | ||
) -> mithril_common::StdResult< | ||
Vec<( | ||
mithril_common::entities::SingleSignature, | ||
mithril_common::entities::SignedEntityType, | ||
)>, | ||
> { | ||
future::pending().await | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use anyhow::anyhow; | ||
use tokio::time::{sleep, Duration}; | ||
|
||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn signature_consumer_noop_never_returns() { | ||
let consumer = SignatureConsumerNoop; | ||
|
||
let result = tokio::select!( | ||
_res = sleep(Duration::from_millis(100)) => {Err(anyhow!("Timeout"))}, | ||
_res = consumer.get_signatures() => {Ok(())}, | ||
); | ||
|
||
result.expect_err("Should have timed out"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
use std::sync::Arc; | ||
|
||
use slog::{error, warn, Logger}; | ||
|
||
use mithril_common::{logging::LoggerExtensions, StdResult}; | ||
use tokio::sync::Mutex; | ||
|
||
use super::{CertifierService, SignatureConsumer}; | ||
|
||
/// A signature processor which receives signature and processes them. | ||
#[cfg_attr(test, mockall::automock)] | ||
#[async_trait::async_trait] | ||
pub trait SignatureProcessor: Sync + Send { | ||
/// Processes the signatures received from the consumer. | ||
async fn process_signatures(&self) -> StdResult<()>; | ||
|
||
/// Starts the processor, which will run indefinitely, processing signatures as they arrive. | ||
async fn run(&self) -> StdResult<()> { | ||
loop { | ||
self.process_signatures().await?; | ||
} | ||
} | ||
|
||
/// Stops the processor. This method should be called to gracefully shut down the processor. | ||
async fn stop(&self) -> StdResult<()>; | ||
} | ||
|
||
/// A sequential signature processor receives messages and processes them sequentially | ||
pub struct SequentialSignatureProcessor { | ||
consumer: Arc<dyn SignatureConsumer>, | ||
certifier: Arc<dyn CertifierService>, | ||
logger: Logger, | ||
stop: Mutex<bool>, | ||
} | ||
|
||
impl SequentialSignatureProcessor { | ||
/// Creates a new `SignatureProcessor` instance. | ||
pub fn new( | ||
consumer: Arc<dyn SignatureConsumer>, | ||
certifier: Arc<dyn CertifierService>, | ||
logger: Logger, | ||
) -> Self { | ||
Self { | ||
consumer, | ||
certifier, | ||
logger: logger.new_with_component_name::<Self>(), | ||
stop: Mutex::new(false), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl SignatureProcessor for SequentialSignatureProcessor { | ||
async fn process_signatures(&self) -> StdResult<()> { | ||
if *self.stop.lock().await { | ||
warn!(self.logger, "Stopped signature processor"); | ||
return Ok(()); | ||
} | ||
|
||
match self.consumer.get_signatures().await { | ||
Ok(signatures) => { | ||
for (signature, signed_entity_type) in signatures { | ||
if let Err(e) = self | ||
.certifier | ||
.register_single_signature(&signed_entity_type, &signature) | ||
.await | ||
{ | ||
error!(self.logger, "Error dispatching single signature"; "error" => ?e); | ||
} | ||
} | ||
} | ||
Err(e) => { | ||
error!(self.logger, "Error consuming single signatures"; "error" => ?e); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn stop(&self) -> StdResult<()> { | ||
warn!(self.logger, "Stopping signature processor..."); | ||
*self.stop.lock().await = true; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use anyhow::anyhow; | ||
use mithril_common::{ | ||
entities::{Epoch, SignedEntityType}, | ||
test_utils::fake_data, | ||
}; | ||
use mockall::predicate::eq; | ||
use tokio::time::{sleep, Duration}; | ||
|
||
use crate::{ | ||
services::{MockCertifierService, MockSignatureConsumer, SignatureRegistrationStatus}, | ||
test_tools::TestLogger, | ||
}; | ||
|
||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn processor_process_signatures_succeeds() { | ||
let logger = TestLogger::stdout(); | ||
let mock_consumer = { | ||
let mut mock_consumer = MockSignatureConsumer::new(); | ||
mock_consumer | ||
.expect_get_signatures() | ||
.returning(|| { | ||
Ok(vec![ | ||
( | ||
fake_data::single_signature(vec![1, 2, 3]), | ||
SignedEntityType::MithrilStakeDistribution(Epoch(1)), | ||
), | ||
( | ||
fake_data::single_signature(vec![4, 5, 6]), | ||
SignedEntityType::MithrilStakeDistribution(Epoch(2)), | ||
), | ||
]) | ||
}) | ||
.times(1); | ||
mock_consumer | ||
}; | ||
let mock_certifier = { | ||
let mut mock_certifier = MockCertifierService::new(); | ||
mock_certifier | ||
.expect_register_single_signature() | ||
.with( | ||
eq(SignedEntityType::MithrilStakeDistribution(Epoch(1))), | ||
eq(fake_data::single_signature(vec![1, 2, 3])), | ||
) | ||
.returning(|_, _| Ok(SignatureRegistrationStatus::Registered)) | ||
.times(1); | ||
mock_certifier | ||
.expect_register_single_signature() | ||
.with( | ||
eq(SignedEntityType::MithrilStakeDistribution(Epoch(2))), | ||
eq(fake_data::single_signature(vec![4, 5, 6])), | ||
) | ||
.returning(|_, _| Ok(SignatureRegistrationStatus::Registered)) | ||
.times(1); | ||
|
||
mock_certifier | ||
}; | ||
let processor = SequentialSignatureProcessor::new( | ||
Arc::new(mock_consumer), | ||
Arc::new(mock_certifier), | ||
logger, | ||
); | ||
|
||
processor | ||
.process_signatures() | ||
.await | ||
.expect("Failed to process signatures"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn processor_run_succeeds() { | ||
let logger = TestLogger::stdout(); | ||
let mock_consumer = { | ||
let mut mock_consumer = MockSignatureConsumer::new(); | ||
mock_consumer | ||
.expect_get_signatures() | ||
.returning(|| Err(anyhow!("Error consuming signatures"))) | ||
.times(1); | ||
mock_consumer | ||
.expect_get_signatures() | ||
.returning(|| { | ||
Ok(vec![( | ||
fake_data::single_signature(vec![1, 2, 3]), | ||
SignedEntityType::MithrilStakeDistribution(Epoch(1)), | ||
)]) | ||
}) | ||
.times(1); | ||
mock_consumer | ||
.expect_get_signatures() | ||
.returning(|| Ok(vec![])); | ||
mock_consumer | ||
}; | ||
let mock_certifier = { | ||
let mut mock_certifier = MockCertifierService::new(); | ||
mock_certifier | ||
.expect_register_single_signature() | ||
.with( | ||
eq(SignedEntityType::MithrilStakeDistribution(Epoch(1))), | ||
eq(fake_data::single_signature(vec![1, 2, 3])), | ||
) | ||
.returning(|_, _| Ok(SignatureRegistrationStatus::Registered)) | ||
.times(1); | ||
|
||
mock_certifier | ||
}; | ||
let processor = SequentialSignatureProcessor::new( | ||
Arc::new(mock_consumer), | ||
Arc::new(mock_certifier), | ||
logger, | ||
); | ||
|
||
tokio::select!( | ||
_res = processor.run() => {}, | ||
_res = sleep(Duration::from_millis(10)) => { | ||
processor.stop().await.expect("Failed to stop processor"); | ||
}, | ||
); | ||
} | ||
} |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
mod delayer; | ||
mod http; | ||
mod interface; | ||
mod signature_publisher_delayer; | ||
mod signature_publisher_noop; | ||
mod signature_publisher_retrier; | ||
mod noop; | ||
mod retrier; | ||
|
||
pub use delayer::*; | ||
pub use interface::*; | ||
pub use signature_publisher_delayer::*; | ||
pub use signature_publisher_noop::*; | ||
pub use signature_publisher_retrier::*; | ||
pub use noop::*; | ||
pub use retrier::*; |
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.