Skip to content

Commit 1439f5b

Browse files
authored
A0-4382: Better types and minor reshuffles (#548)
* Better types and minor reshuffles This does almost nothing by itself, but it should make the following changes (separating out initial unit collection, and consolidating request logic) much simpler. * Review improvements
1 parent 4b3d8f2 commit 1439f5b

File tree

15 files changed

+434
-348
lines changed

15 files changed

+434
-348
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ More details are available [in the book][reference-link-implementation-details].
6060
- Import AlephBFT in your crate
6161
```toml
6262
[dependencies]
63-
aleph-bft = "^0.42"
63+
aleph-bft = "^0.43"
6464
```
6565
- The main entry point is the `run_session` function, which returns a Future that runs the
6666
consensus algorithm.

consensus/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "aleph-bft"
3-
version = "0.42.2"
3+
version = "0.43.0"
44
edition = "2021"
55
authors = ["Cardinal Cryptography"]
66
categories = ["algorithms", "data-structures", "cryptography", "database"]

consensus/src/dag/reconstruction/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<D: Data, H: Hasher, K: MultiKeychain> From<ReconstructedUnit<Signed<FullUni
131131
}
132132

133133
/// What we need to request to reconstruct units.
134-
#[derive(Debug, PartialEq, Eq)]
134+
#[derive(Eq, PartialEq, Debug, Clone)]
135135
pub enum Request<H: Hasher> {
136136
/// We need a unit at this coordinate.
137137
Coord(UnitCoord),

consensus/src/dissemination/mod.rs

Lines changed: 139 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,156 @@
11
use crate::{
2+
dag::Request as ReconstructionRequest,
3+
network::UnitMessage,
24
runway::{NewestUnitResponse, Salt},
3-
units::{UncheckedSignedUnit, UnitCoord},
4-
Data, Hasher, NodeIndex, Signature, UncheckedSigned,
5+
units::UncheckedSignedUnit,
6+
Data, Hasher, NodeIndex, Recipient, Signature, UncheckedSigned,
57
};
68

79
mod responder;
10+
mod task;
811

912
pub use responder::Responder;
13+
pub use task::DisseminationTask;
14+
15+
/// Some form of message with the intended recipients.
16+
#[derive(Eq, PartialEq, Debug, Clone)]
17+
pub struct Addressed<T> {
18+
message: T,
19+
recipients: Vec<Recipient>,
20+
}
21+
22+
impl<T> Addressed<T> {
23+
/// Message with the given recipients.
24+
pub fn new(message: T, recipients: Vec<Recipient>) -> Self {
25+
Addressed {
26+
message,
27+
recipients,
28+
}
29+
}
30+
31+
/// Message with the single specified recipient.
32+
pub fn addressed_to(message: T, node_id: NodeIndex) -> Self {
33+
Addressed::new(message, vec![Recipient::Node(node_id)])
34+
}
35+
36+
/// All the recipients of this message.
37+
pub fn recipients(&self) -> &Vec<Recipient> {
38+
&self.recipients
39+
}
40+
41+
/// The associated message.
42+
pub fn message(&self) -> &T {
43+
&self.message
44+
}
45+
46+
/// Convert the underlying message. Cannot be done through a `From` implementation due to it
47+
/// overriding the blanked identity `From` implementation.
48+
pub fn into<U: From<T>>(self) -> Addressed<U> {
49+
let Addressed {
50+
message,
51+
recipients,
52+
} = self;
53+
Addressed {
54+
message: message.into(),
55+
recipients,
56+
}
57+
}
58+
}
1059

1160
/// Possible requests for information from other nodes.
12-
#[derive(Debug)]
13-
pub enum Request<H: Hasher> {
14-
Coord(UnitCoord),
15-
Parents(H::Hash),
61+
#[derive(Eq, PartialEq, Debug, Clone)]
62+
pub enum DisseminationRequest<H: Hasher> {
63+
/// A request for unit information in normal operation.
64+
Unit(ReconstructionRequest<H>),
65+
/// Request for what the specified node thinks about our newest unit.
1666
NewestUnit(NodeIndex, Salt),
1767
}
1868

69+
impl<H: Hasher> From<ReconstructionRequest<H>> for DisseminationRequest<H> {
70+
fn from(request: ReconstructionRequest<H>) -> Self {
71+
DisseminationRequest::Unit(request)
72+
}
73+
}
74+
1975
/// Responses to requests.
20-
#[derive(Debug)]
21-
pub enum Response<H: Hasher, D: Data, S: Signature> {
76+
#[derive(Eq, PartialEq, Debug, Clone)]
77+
pub enum DisseminationResponse<H: Hasher, D: Data, S: Signature> {
78+
/// Response to a coord request, just a single unit.
2279
Coord(UncheckedSignedUnit<H, D, S>),
80+
/// All the parents of the specified unit.
2381
Parents(H::Hash, Vec<UncheckedSignedUnit<H, D, S>>),
82+
/// The newest unit response for initial unit collection.
2483
NewestUnit(UncheckedSigned<NewestUnitResponse<H, D, S>, S>),
2584
}
85+
86+
/// A message that has to be passed between committee members for consensus to work.
87+
#[derive(Eq, PartialEq, Debug, Clone)]
88+
pub enum DisseminationMessage<H: Hasher, D: Data, S: Signature> {
89+
/// Unit, either broadcast or in response to a coord request.
90+
Unit(UncheckedSignedUnit<H, D, S>),
91+
/// Request coming from the specified node for something.
92+
Request(NodeIndex, DisseminationRequest<H>),
93+
/// Response to a parent request.
94+
ParentsResponse(H::Hash, Vec<UncheckedSignedUnit<H, D, S>>),
95+
/// Response to initial unit collection.
96+
NewestUnitResponse(UncheckedSigned<NewestUnitResponse<H, D, S>, S>),
97+
}
98+
99+
impl<H: Hasher, D: Data, S: Signature> From<UnitMessage<H, D, S>>
100+
for DisseminationMessage<H, D, S>
101+
{
102+
fn from(message: UnitMessage<H, D, S>) -> Self {
103+
use DisseminationMessage::*;
104+
match message {
105+
UnitMessage::Unit(u) => Unit(u),
106+
UnitMessage::CoordRequest(node_id, coord) => {
107+
Request(node_id, ReconstructionRequest::Coord(coord).into())
108+
}
109+
UnitMessage::ParentsRequest(node_id, hash) => {
110+
Request(node_id, ReconstructionRequest::ParentsOf(hash).into())
111+
}
112+
UnitMessage::ParentsResponse(h, units) => ParentsResponse(h, units),
113+
UnitMessage::NewestRequest(node_id, salt) => {
114+
Request(node_id, DisseminationRequest::NewestUnit(node_id, salt))
115+
}
116+
UnitMessage::NewestResponse(response) => NewestUnitResponse(response),
117+
}
118+
}
119+
}
120+
121+
impl<H: Hasher, D: Data, S: Signature> From<DisseminationMessage<H, D, S>>
122+
for UnitMessage<H, D, S>
123+
{
124+
fn from(message: DisseminationMessage<H, D, S>) -> Self {
125+
use DisseminationMessage::*;
126+
match message {
127+
Unit(u) => UnitMessage::Unit(u),
128+
Request(node_id, DisseminationRequest::Unit(ReconstructionRequest::Coord(coord))) => {
129+
UnitMessage::CoordRequest(node_id, coord)
130+
}
131+
Request(
132+
node_id,
133+
DisseminationRequest::Unit(ReconstructionRequest::ParentsOf(hash)),
134+
) => UnitMessage::ParentsRequest(node_id, hash),
135+
ParentsResponse(h, units) => UnitMessage::ParentsResponse(h, units),
136+
Request(node_id, DisseminationRequest::NewestUnit(_, salt)) => {
137+
UnitMessage::NewestRequest(node_id, salt)
138+
}
139+
NewestUnitResponse(response) => UnitMessage::NewestResponse(response),
140+
}
141+
}
142+
}
143+
144+
impl<H: Hasher, D: Data, S: Signature> From<DisseminationResponse<H, D, S>>
145+
for DisseminationMessage<H, D, S>
146+
{
147+
fn from(message: DisseminationResponse<H, D, S>) -> Self {
148+
use DisseminationMessage::*;
149+
use DisseminationResponse::*;
150+
match message {
151+
Coord(u) => Unit(u),
152+
Parents(h, units) => ParentsResponse(h, units),
153+
NewestUnit(response) => NewestUnitResponse(response),
154+
}
155+
}
156+
}

0 commit comments

Comments
 (0)