-
Notifications
You must be signed in to change notification settings - Fork 220
[runtime] Create a trait for briding async and parallel code #3636
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
cronokirby
wants to merge
2
commits into
main
Choose a base branch
from
ck/strategist
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,15 +20,14 @@ use commonware_p2p::{ | |
| utils::codec::{wrap, WrappedSender}, | ||
| Blocker, Receiver, Recipients, Sender, | ||
| }; | ||
| use commonware_parallel::Strategy; | ||
| use commonware_runtime::{ | ||
| buffer::paged::CacheRef, | ||
| spawn_cell, | ||
| telemetry::metrics::{ | ||
| histogram, | ||
| status::{CounterExt, GaugeExt, Status}, | ||
| }, | ||
| BufferPooler, Clock, ContextCell, Handle, Metrics, Spawner, Storage, | ||
| BufferPooler, Clock, ContextCell, Handle, Metrics, Spawner, Storage, Strategist, | ||
| }; | ||
| use commonware_storage::journal::segmented::variable::{Config as JConfig, Journal}; | ||
| use commonware_utils::{futures::Pool as FuturesPool, ordered::Quorum, N3f1, PrioritySet}; | ||
|
|
@@ -71,14 +70,13 @@ struct DigestRequest<D: Digest, E: Clock> { | |
|
|
||
| /// Instance of the engine. | ||
| pub struct Engine< | ||
| E: BufferPooler + Clock + Spawner + Storage + Metrics + CryptoRngCore, | ||
| E: BufferPooler + Clock + Spawner + Storage + Metrics + CryptoRngCore + Strategist, | ||
| P: Provider<Scope = Epoch>, | ||
| D: Digest, | ||
| A: Automaton<Context = Height, Digest = D> + Clone, | ||
| Z: Reporter<Activity = Activity<P::Scheme, D>>, | ||
| M: Monitor<Index = Epoch>, | ||
| B: Blocker<PublicKey = <P::Scheme as Scheme>::PublicKey>, | ||
| T: Strategy, | ||
| > { | ||
| // ---------- Interfaces ---------- | ||
| context: ContextCell<E>, | ||
|
|
@@ -87,7 +85,6 @@ pub struct Engine< | |
| provider: P, | ||
| reporter: Z, | ||
| blocker: B, | ||
| strategy: T, | ||
|
|
||
| // Pruning | ||
| /// A tuple representing the epochs to keep in memory. | ||
|
|
@@ -154,18 +151,17 @@ pub struct Engine< | |
| } | ||
|
|
||
| impl< | ||
| E: BufferPooler + Clock + Spawner + Storage + Metrics + CryptoRngCore, | ||
| E: BufferPooler + Clock + Spawner + Storage + Metrics + CryptoRngCore + Strategist, | ||
| P: Provider<Scope = Epoch, Scheme: scheme::Scheme<D>>, | ||
| D: Digest, | ||
| A: Automaton<Context = Height, Digest = D> + Clone, | ||
| Z: Reporter<Activity = Activity<P::Scheme, D>>, | ||
| M: Monitor<Index = Epoch>, | ||
| B: Blocker<PublicKey = <P::Scheme as Scheme>::PublicKey>, | ||
| T: Strategy, | ||
| > Engine<E, P, D, A, Z, M, B, T> | ||
| > Engine<E, P, D, A, Z, M, B> | ||
| { | ||
| /// Creates a new engine with the given context and configuration. | ||
| pub fn new(context: E, cfg: Config<P, D, A, Z, M, B, T>) -> Self { | ||
| pub fn new(context: E, cfg: Config<P, D, A, Z, M, B>) -> Self { | ||
| // TODO(#1833): Metrics should use the post-start context | ||
| let metrics = metrics::Metrics::init(context.clone()); | ||
|
|
||
|
|
@@ -176,7 +172,6 @@ impl< | |
| monitor: cfg.monitor, | ||
| provider: cfg.provider, | ||
| blocker: cfg.blocker, | ||
| strategy: cfg.strategy, | ||
| epoch_bounds: cfg.epoch_bounds, | ||
| window: HeightDelta::new(cfg.window.into()), | ||
| activity_timeout: cfg.activity_timeout, | ||
|
|
@@ -385,7 +380,7 @@ impl< | |
| } | ||
|
|
||
| // Validate that we need to process the ack | ||
| if let Err(err) = self.validate_ack(&ack, &sender) { | ||
| if let Err(err) = self.validate_ack(&ack, &sender).await { | ||
| if err.blockable() { | ||
| commonware_p2p::block!( | ||
| self.blocker, | ||
|
|
@@ -530,9 +525,16 @@ impl< | |
| let filtered = acks | ||
| .values() | ||
| .filter(|a| a.item.digest == ack.item.digest) | ||
| .cloned() | ||
| .collect::<Vec<_>>(); | ||
| if filtered.len() >= quorum as usize { | ||
| if let Some(certificate) = Certificate::from_acks(&*scheme, filtered, &self.strategy) { | ||
| let certificate = self | ||
| .context | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should keep |
||
| .with_strategy(move |strategy| { | ||
| Certificate::from_acks(&*scheme, filtered.iter(), strategy) | ||
| }) | ||
| .await; | ||
| if let Some(certificate) = certificate { | ||
| self.metrics.certificates.inc(); | ||
| self.handle_certificate(certificate).await; | ||
| } | ||
|
|
@@ -613,7 +615,7 @@ impl< | |
| /// Takes a raw ack (from sender) from the p2p network and validates it. | ||
| /// | ||
| /// Returns an error if the ack is invalid. | ||
| fn validate_ack( | ||
| async fn validate_ack( | ||
| &mut self, | ||
| ack: &Ack<P::Scheme, D>, | ||
| sender: &<P::Scheme as Scheme>::PublicKey, | ||
|
|
@@ -679,7 +681,13 @@ impl< | |
| } | ||
|
|
||
| // Validate signature | ||
| if !ack.verify(&mut self.context, &*scheme, &self.strategy) { | ||
| let ack = ack.clone(); | ||
| let mut rng = self.context.clone(); | ||
| let valid = self | ||
| .context | ||
| .with_strategy(move |strategy| ack.verify(&mut rng, &*scheme, strategy)) | ||
| .await; | ||
| if !valid { | ||
| return Err(Error::InvalidAckSignature); | ||
| } | ||
|
|
||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
wonder if there is a way to avoid this clone with some more careful lifetime management?