Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit d477819

Browse files
authored
Merge pull request #34 from paritytech/rh-candidate-agreement-glue
Candidate agreement "glue" code
2 parents b38b633 + ad9913a commit d477819

File tree

10 files changed

+1816
-320
lines changed

10 files changed

+1816
-320
lines changed

Cargo.lock

+12-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

candidate-agreement/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ version = "0.1.0"
44
authors = ["Parity Technologies <admin@parity.io>"]
55

66
[dependencies]
7-
futures = "0.1"
8-
polkadot-primitives = { path = "../primitives" }
7+
futures = "0.1.17"
8+
parking_lot = "0.4"
9+
tokio-timer = "0.1.2"

candidate-agreement/src/bft/accumulator.rs

+42-47
Original file line numberDiff line numberDiff line change
@@ -117,37 +117,37 @@ struct VoteCounts {
117117

118118
/// Accumulates messages for a given round of BFT consensus.
119119
///
120-
/// This isn't tied to the "view" of a single validator. It
120+
/// This isn't tied to the "view" of a single authority. It
121121
/// keeps accurate track of the state of the BFT consensus based
122122
/// on all messages imported.
123123
#[derive(Debug)]
124-
pub struct Accumulator<Candidate, Digest, ValidatorId, Signature>
124+
pub struct Accumulator<Candidate, Digest, AuthorityId, Signature>
125125
where
126126
Candidate: Eq + Clone,
127127
Digest: Hash + Eq + Clone,
128-
ValidatorId: Hash + Eq,
128+
AuthorityId: Hash + Eq,
129129
Signature: Eq + Clone,
130130
{
131131
round_number: usize,
132132
threshold: usize,
133-
round_proposer: ValidatorId,
133+
round_proposer: AuthorityId,
134134
proposal: Option<Candidate>,
135-
prepares: HashMap<ValidatorId, (Digest, Signature)>,
136-
commits: HashMap<ValidatorId, (Digest, Signature)>,
135+
prepares: HashMap<AuthorityId, (Digest, Signature)>,
136+
commits: HashMap<AuthorityId, (Digest, Signature)>,
137137
vote_counts: HashMap<Digest, VoteCounts>,
138-
advance_round: HashSet<ValidatorId>,
138+
advance_round: HashSet<AuthorityId>,
139139
state: State<Candidate, Digest, Signature>,
140140
}
141141

142-
impl<Candidate, Digest, ValidatorId, Signature> Accumulator<Candidate, Digest, ValidatorId, Signature>
142+
impl<Candidate, Digest, AuthorityId, Signature> Accumulator<Candidate, Digest, AuthorityId, Signature>
143143
where
144144
Candidate: Eq + Clone,
145145
Digest: Hash + Eq + Clone,
146-
ValidatorId: Hash + Eq,
146+
AuthorityId: Hash + Eq,
147147
Signature: Eq + Clone,
148148
{
149149
/// Create a new state accumulator.
150-
pub fn new(round_number: usize, threshold: usize, round_proposer: ValidatorId) -> Self {
150+
pub fn new(round_number: usize, threshold: usize, round_proposer: AuthorityId) -> Self {
151151
Accumulator {
152152
round_number,
153153
threshold,
@@ -171,11 +171,6 @@ impl<Candidate, Digest, ValidatorId, Signature> Accumulator<Candidate, Digest, V
171171
self.round_number.clone()
172172
}
173173

174-
/// Get the round proposer.
175-
pub fn round_proposer(&self) -> &ValidatorId {
176-
&self.round_proposer
177-
}
178-
179174
pub fn proposal(&self) -> Option<&Candidate> {
180175
self.proposal.as_ref()
181176
}
@@ -189,7 +184,7 @@ impl<Candidate, Digest, ValidatorId, Signature> Accumulator<Candidate, Digest, V
189184
/// and authorization should have already been checked.
190185
pub fn import_message(
191186
&mut self,
192-
message: LocalizedMessage<Candidate, Digest, ValidatorId, Signature>,
187+
message: LocalizedMessage<Candidate, Digest, AuthorityId, Signature>,
193188
)
194189
{
195190
// message from different round.
@@ -210,7 +205,7 @@ impl<Candidate, Digest, ValidatorId, Signature> Accumulator<Candidate, Digest, V
210205
fn import_proposal(
211206
&mut self,
212207
proposal: Candidate,
213-
sender: ValidatorId,
208+
sender: AuthorityId,
214209
) {
215210
if sender != self.round_proposer || self.proposal.is_some() { return }
216211

@@ -221,7 +216,7 @@ impl<Candidate, Digest, ValidatorId, Signature> Accumulator<Candidate, Digest, V
221216
fn import_prepare(
222217
&mut self,
223218
digest: Digest,
224-
sender: ValidatorId,
219+
sender: AuthorityId,
225220
signature: Signature,
226221
) {
227222
// ignore any subsequent prepares by the same sender.
@@ -264,7 +259,7 @@ impl<Candidate, Digest, ValidatorId, Signature> Accumulator<Candidate, Digest, V
264259
fn import_commit(
265260
&mut self,
266261
digest: Digest,
267-
sender: ValidatorId,
262+
sender: AuthorityId,
268263
signature: Signature,
269264
) {
270265
// ignore any subsequent commits by the same sender.
@@ -304,7 +299,7 @@ impl<Candidate, Digest, ValidatorId, Signature> Accumulator<Candidate, Digest, V
304299

305300
fn import_advance_round(
306301
&mut self,
307-
sender: ValidatorId,
302+
sender: AuthorityId,
308303
) {
309304
self.advance_round.insert(sender);
310305

@@ -332,7 +327,7 @@ mod tests {
332327
pub struct Digest(usize);
333328

334329
#[derive(Hash, PartialEq, Eq, Debug)]
335-
pub struct ValidatorId(usize);
330+
pub struct AuthorityId(usize);
336331

337332
#[derive(PartialEq, Eq, Clone, Debug)]
338333
pub struct Signature(usize, usize);
@@ -347,7 +342,7 @@ mod tests {
347342

348343
let check_message = |r, d: &Digest, s: &Signature| {
349344
if r == 2 && d.0 == 600 && s.0 == 600 {
350-
Some(ValidatorId(s.1))
345+
Some(AuthorityId(s.1))
351346
} else {
352347
None
353348
}
@@ -370,19 +365,19 @@ mod tests {
370365

371366
#[test]
372367
fn accepts_proposal_from_proposer_only() {
373-
let mut accumulator = Accumulator::<_, Digest, _, _>::new(1, 7, ValidatorId(8));
368+
let mut accumulator = Accumulator::<_, Digest, _, _>::new(1, 7, AuthorityId(8));
374369
assert_eq!(accumulator.state(), &State::Begin);
375370

376371
accumulator.import_message(LocalizedMessage {
377-
sender: ValidatorId(5),
372+
sender: AuthorityId(5),
378373
signature: Signature(999, 5),
379374
message: Message::Propose(1, Candidate(999)),
380375
});
381376

382377
assert_eq!(accumulator.state(), &State::Begin);
383378

384379
accumulator.import_message(LocalizedMessage {
385-
sender: ValidatorId(8),
380+
sender: AuthorityId(8),
386381
signature: Signature(999, 8),
387382
message: Message::Propose(1, Candidate(999)),
388383
});
@@ -392,11 +387,11 @@ mod tests {
392387

393388
#[test]
394389
fn reaches_prepare_phase() {
395-
let mut accumulator = Accumulator::new(1, 7, ValidatorId(8));
390+
let mut accumulator = Accumulator::new(1, 7, AuthorityId(8));
396391
assert_eq!(accumulator.state(), &State::Begin);
397392

398393
accumulator.import_message(LocalizedMessage {
399-
sender: ValidatorId(8),
394+
sender: AuthorityId(8),
400395
signature: Signature(999, 8),
401396
message: Message::Propose(1, Candidate(999)),
402397
});
@@ -405,7 +400,7 @@ mod tests {
405400

406401
for i in 0..6 {
407402
accumulator.import_message(LocalizedMessage {
408-
sender: ValidatorId(i),
403+
sender: AuthorityId(i),
409404
signature: Signature(999, i),
410405
message: Message::Prepare(1, Digest(999)),
411406
});
@@ -414,7 +409,7 @@ mod tests {
414409
}
415410

416411
accumulator.import_message(LocalizedMessage {
417-
sender: ValidatorId(7),
412+
sender: AuthorityId(7),
418413
signature: Signature(999, 7),
419414
message: Message::Prepare(1, Digest(999)),
420415
});
@@ -427,11 +422,11 @@ mod tests {
427422

428423
#[test]
429424
fn prepare_to_commit() {
430-
let mut accumulator = Accumulator::new(1, 7, ValidatorId(8));
425+
let mut accumulator = Accumulator::new(1, 7, AuthorityId(8));
431426
assert_eq!(accumulator.state(), &State::Begin);
432427

433428
accumulator.import_message(LocalizedMessage {
434-
sender: ValidatorId(8),
429+
sender: AuthorityId(8),
435430
signature: Signature(999, 8),
436431
message: Message::Propose(1, Candidate(999)),
437432
});
@@ -440,7 +435,7 @@ mod tests {
440435

441436
for i in 0..6 {
442437
accumulator.import_message(LocalizedMessage {
443-
sender: ValidatorId(i),
438+
sender: AuthorityId(i),
444439
signature: Signature(999, i),
445440
message: Message::Prepare(1, Digest(999)),
446441
});
@@ -449,7 +444,7 @@ mod tests {
449444
}
450445

451446
accumulator.import_message(LocalizedMessage {
452-
sender: ValidatorId(7),
447+
sender: AuthorityId(7),
453448
signature: Signature(999, 7),
454449
message: Message::Prepare(1, Digest(999)),
455450
});
@@ -461,7 +456,7 @@ mod tests {
461456

462457
for i in 0..6 {
463458
accumulator.import_message(LocalizedMessage {
464-
sender: ValidatorId(i),
459+
sender: AuthorityId(i),
465460
signature: Signature(999, i),
466461
message: Message::Commit(1, Digest(999)),
467462
});
@@ -473,7 +468,7 @@ mod tests {
473468
}
474469

475470
accumulator.import_message(LocalizedMessage {
476-
sender: ValidatorId(7),
471+
sender: AuthorityId(7),
477472
signature: Signature(999, 7),
478473
message: Message::Commit(1, Digest(999)),
479474
});
@@ -486,11 +481,11 @@ mod tests {
486481

487482
#[test]
488483
fn prepare_to_advance() {
489-
let mut accumulator = Accumulator::new(1, 7, ValidatorId(8));
484+
let mut accumulator = Accumulator::new(1, 7, AuthorityId(8));
490485
assert_eq!(accumulator.state(), &State::Begin);
491486

492487
accumulator.import_message(LocalizedMessage {
493-
sender: ValidatorId(8),
488+
sender: AuthorityId(8),
494489
signature: Signature(999, 8),
495490
message: Message::Propose(1, Candidate(999)),
496491
});
@@ -499,7 +494,7 @@ mod tests {
499494

500495
for i in 0..7 {
501496
accumulator.import_message(LocalizedMessage {
502-
sender: ValidatorId(i),
497+
sender: AuthorityId(i),
503498
signature: Signature(999, i),
504499
message: Message::Prepare(1, Digest(999)),
505500
});
@@ -512,7 +507,7 @@ mod tests {
512507

513508
for i in 0..6 {
514509
accumulator.import_message(LocalizedMessage {
515-
sender: ValidatorId(i),
510+
sender: AuthorityId(i),
516511
signature: Signature(999, i),
517512
message: Message::AdvanceRound(1),
518513
});
@@ -524,7 +519,7 @@ mod tests {
524519
}
525520

526521
accumulator.import_message(LocalizedMessage {
527-
sender: ValidatorId(7),
522+
sender: AuthorityId(7),
528523
signature: Signature(999, 7),
529524
message: Message::AdvanceRound(1),
530525
});
@@ -537,12 +532,12 @@ mod tests {
537532

538533
#[test]
539534
fn conclude_different_than_proposed() {
540-
let mut accumulator = Accumulator::<Candidate, _, _, _>::new(1, 7, ValidatorId(8));
535+
let mut accumulator = Accumulator::<Candidate, _, _, _>::new(1, 7, AuthorityId(8));
541536
assert_eq!(accumulator.state(), &State::Begin);
542537

543538
for i in 0..7 {
544539
accumulator.import_message(LocalizedMessage {
545-
sender: ValidatorId(i),
540+
sender: AuthorityId(i),
546541
signature: Signature(999, i),
547542
message: Message::Prepare(1, Digest(999)),
548543
});
@@ -555,7 +550,7 @@ mod tests {
555550

556551
for i in 0..7 {
557552
accumulator.import_message(LocalizedMessage {
558-
sender: ValidatorId(i),
553+
sender: AuthorityId(i),
559554
signature: Signature(999, i),
560555
message: Message::Commit(1, Digest(999)),
561556
});
@@ -569,12 +564,12 @@ mod tests {
569564

570565
#[test]
571566
fn begin_to_advance() {
572-
let mut accumulator = Accumulator::<Candidate, Digest, _, _>::new(1, 7, ValidatorId(8));
567+
let mut accumulator = Accumulator::<Candidate, Digest, _, _>::new(1, 7, AuthorityId(8));
573568
assert_eq!(accumulator.state(), &State::Begin);
574569

575570
for i in 0..7 {
576571
accumulator.import_message(LocalizedMessage {
577-
sender: ValidatorId(i),
572+
sender: AuthorityId(i),
578573
signature: Signature(1, i),
579574
message: Message::AdvanceRound(1),
580575
});
@@ -588,12 +583,12 @@ mod tests {
588583

589584
#[test]
590585
fn conclude_without_prepare() {
591-
let mut accumulator = Accumulator::<Candidate, _, _, _>::new(1, 7, ValidatorId(8));
586+
let mut accumulator = Accumulator::<Candidate, _, _, _>::new(1, 7, AuthorityId(8));
592587
assert_eq!(accumulator.state(), &State::Begin);
593588

594589
for i in 0..7 {
595590
accumulator.import_message(LocalizedMessage {
596-
sender: ValidatorId(i),
591+
sender: AuthorityId(i),
597592
signature: Signature(999, i),
598593
message: Message::Commit(1, Digest(999)),
599594
});

0 commit comments

Comments
 (0)