-
Notifications
You must be signed in to change notification settings - Fork 50
A0-4382: Better types and minor reshuffles #548
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
Merged
Merged
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,25 +1,156 @@ | ||
use crate::{ | ||
dag::Request as ReconstructionRequest, | ||
network::UnitMessage, | ||
runway::{NewestUnitResponse, Salt}, | ||
units::{UncheckedSignedUnit, UnitCoord}, | ||
Data, Hasher, NodeIndex, Signature, UncheckedSigned, | ||
units::UncheckedSignedUnit, | ||
Data, Hasher, NodeIndex, Recipient, Signature, UncheckedSigned, | ||
}; | ||
|
||
mod responder; | ||
mod task; | ||
|
||
pub use responder::Responder; | ||
pub use task::DisseminationTask; | ||
|
||
/// Some form of message with the intended recipients. | ||
#[derive(Eq, PartialEq, Debug, Clone)] | ||
pub struct Addressed<T> { | ||
message: T, | ||
recipients: Vec<Recipient>, | ||
} | ||
|
||
impl<T> Addressed<T> { | ||
/// Message with the given recipients. | ||
pub fn new(message: T, recipients: Vec<Recipient>) -> Self { | ||
Addressed { | ||
message, | ||
recipients, | ||
} | ||
} | ||
|
||
/// Message with the single specified recipient. | ||
pub fn addressed_to(message: T, node_id: NodeIndex) -> Self { | ||
Addressed::new(message, vec![Recipient::Node(node_id)]) | ||
} | ||
|
||
/// All the recipients of this message. | ||
pub fn recipients(&self) -> &Vec<Recipient> { | ||
&self.recipients | ||
} | ||
|
||
/// The associated message. | ||
pub fn message(&self) -> &T { | ||
&self.message | ||
} | ||
|
||
/// Convert the underlying message. Cannot be done through a `From` implementation due to it | ||
/// overriding the blanked identity `From` implementation. | ||
pub fn into<U: From<T>>(self) -> Addressed<U> { | ||
let Addressed { | ||
message, | ||
recipients, | ||
} = self; | ||
Addressed { | ||
message: message.into(), | ||
recipients, | ||
} | ||
} | ||
} | ||
|
||
/// Possible requests for information from other nodes. | ||
#[derive(Debug)] | ||
pub enum Request<H: Hasher> { | ||
Coord(UnitCoord), | ||
Parents(H::Hash), | ||
#[derive(Eq, PartialEq, Debug, Clone)] | ||
pub enum DisseminationRequest<H: Hasher> { | ||
/// A request for unit information in normal operation. | ||
Unit(ReconstructionRequest<H>), | ||
/// Request for what the specified node thinks about our newest unit. | ||
NewestUnit(NodeIndex, Salt), | ||
} | ||
|
||
impl<H: Hasher> From<ReconstructionRequest<H>> for DisseminationRequest<H> { | ||
fn from(request: ReconstructionRequest<H>) -> Self { | ||
DisseminationRequest::Unit(request) | ||
} | ||
} | ||
|
||
/// Responses to requests. | ||
#[derive(Debug)] | ||
pub enum Response<H: Hasher, D: Data, S: Signature> { | ||
#[derive(Eq, PartialEq, Debug, Clone)] | ||
pub enum DisseminationResponse<H: Hasher, D: Data, S: Signature> { | ||
/// Response to a coord request, just a single unit. | ||
Coord(UncheckedSignedUnit<H, D, S>), | ||
/// All the parents of the specified unit. | ||
Parents(H::Hash, Vec<UncheckedSignedUnit<H, D, S>>), | ||
/// The newest unit response for initial unit collection. | ||
NewestUnit(UncheckedSigned<NewestUnitResponse<H, D, S>, S>), | ||
} | ||
|
||
/// A message that has to be passed between committee members for consensus to work. | ||
#[derive(Eq, PartialEq, Debug, Clone)] | ||
pub enum DisseminationMessage<H: Hasher, D: Data, S: Signature> { | ||
/// Unit, either broadcast or in response to a coord request. | ||
Unit(UncheckedSignedUnit<H, D, S>), | ||
/// Request coming from the specified node for something. | ||
Request(NodeIndex, DisseminationRequest<H>), | ||
/// Response to a parent request. | ||
ParentsResponse(H::Hash, Vec<UncheckedSignedUnit<H, D, S>>), | ||
/// Response to initial unit collection. | ||
NewestUnitResponse(UncheckedSigned<NewestUnitResponse<H, D, S>, S>), | ||
} | ||
|
||
impl<H: Hasher, D: Data, S: Signature> From<UnitMessage<H, D, S>> | ||
for DisseminationMessage<H, D, S> | ||
{ | ||
fn from(message: UnitMessage<H, D, S>) -> Self { | ||
use DisseminationMessage::*; | ||
match message { | ||
UnitMessage::Unit(u) => Unit(u), | ||
UnitMessage::CoordRequest(node_id, coord) => { | ||
Request(node_id, ReconstructionRequest::Coord(coord).into()) | ||
} | ||
UnitMessage::ParentsRequest(node_id, hash) => { | ||
Request(node_id, ReconstructionRequest::ParentsOf(hash).into()) | ||
} | ||
UnitMessage::ParentsResponse(h, units) => ParentsResponse(h, units), | ||
UnitMessage::NewestRequest(node_id, salt) => { | ||
Request(node_id, DisseminationRequest::NewestUnit(node_id, salt)) | ||
} | ||
UnitMessage::NewestResponse(response) => NewestUnitResponse(response), | ||
} | ||
} | ||
} | ||
|
||
impl<H: Hasher, D: Data, S: Signature> From<DisseminationMessage<H, D, S>> | ||
for UnitMessage<H, D, S> | ||
{ | ||
fn from(message: DisseminationMessage<H, D, S>) -> Self { | ||
use DisseminationMessage::*; | ||
match message { | ||
Unit(u) => UnitMessage::Unit(u), | ||
Request(node_id, DisseminationRequest::Unit(ReconstructionRequest::Coord(coord))) => { | ||
UnitMessage::CoordRequest(node_id, coord) | ||
} | ||
Request( | ||
node_id, | ||
DisseminationRequest::Unit(ReconstructionRequest::ParentsOf(hash)), | ||
) => UnitMessage::ParentsRequest(node_id, hash), | ||
ParentsResponse(h, units) => UnitMessage::ParentsResponse(h, units), | ||
Request(node_id, DisseminationRequest::NewestUnit(_, salt)) => { | ||
UnitMessage::NewestRequest(node_id, salt) | ||
} | ||
NewestUnitResponse(response) => UnitMessage::NewestResponse(response), | ||
} | ||
} | ||
} | ||
|
||
impl<H: Hasher, D: Data, S: Signature> From<DisseminationResponse<H, D, S>> | ||
for DisseminationMessage<H, D, S> | ||
{ | ||
fn from(message: DisseminationResponse<H, D, S>) -> Self { | ||
use DisseminationMessage::*; | ||
use DisseminationResponse::*; | ||
match message { | ||
Coord(u) => Unit(u), | ||
Parents(h, units) => ParentsResponse(h, units), | ||
NewestUnit(response) => NewestUnitResponse(response), | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.