Skip to content

Commit d055f7d

Browse files
authored
Merge pull request paritytech#106 from paritytech/andre/fix-finality-notification
Fix sending of finality notification
2 parents 8bd982f + 41fcf8b commit d055f7d

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

src/lib.rs

+10-20
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ impl<H, N> Precommit<H, N> {
113113

114114
/// A primary proposed block, this is a broadcast of the last round's estimate.
115115
#[derive(Clone, PartialEq, Eq)]
116-
#[cfg_attr(any(feature = "std", test), derive(
117-
Debug))]
116+
#[cfg_attr(any(feature = "std", test), derive(Debug))]
118117
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
119118
pub struct PrimaryPropose<H, N> {
120119
/// The target block's hash.
@@ -130,8 +129,7 @@ impl<H, N> PrimaryPropose<H, N> {
130129
}
131130

132131
#[derive(Clone, PartialEq)]
133-
#[cfg_attr(any(feature = "std", test), derive(
134-
Debug))]
132+
#[cfg_attr(any(feature = "std", test), derive(Debug))]
135133
pub enum Error {
136134
NotDescendent,
137135
}
@@ -219,8 +217,7 @@ pub struct Equivocation<Id, V, S> {
219217

220218
/// A protocol message or vote.
221219
#[derive(Clone, PartialEq, Eq)]
222-
#[cfg_attr(any(feature = "std", test), derive(
223-
Debug))]
220+
#[cfg_attr(any(feature = "std", test), derive(Debug))]
224221
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
225222
pub enum Message<H, N> {
226223
/// A prevote message.
@@ -247,8 +244,7 @@ impl<H, N: Copy> Message<H, N> {
247244

248245
/// A signed message.
249246
#[derive(Clone, PartialEq, Eq)]
250-
#[cfg_attr(any(feature = "std", test), derive(
251-
Debug))]
247+
#[cfg_attr(any(feature = "std", test), derive(Debug))]
252248
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
253249
pub struct SignedMessage<H, N, S, Id> {
254250
/// The internal message which has been signed.
@@ -270,8 +266,7 @@ impl<H, N: Copy, S, Id> SignedMessage<H, N, S, Id> {
270266

271267
/// A commit message which is an aggregate of precommits.
272268
#[derive(Clone, PartialEq, Eq)]
273-
#[cfg_attr(any(feature = "std", test), derive(
274-
Debug))]
269+
#[cfg_attr(any(feature = "std", test), derive(Debug))]
275270
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
276271
pub struct Commit<H, N, S, Id> {
277272
/// The target block's hash.
@@ -284,8 +279,7 @@ pub struct Commit<H, N, S, Id> {
284279

285280
/// A signed prevote message.
286281
#[derive(Clone, PartialEq, Eq)]
287-
#[cfg_attr(any(feature = "std", test), derive(
288-
Debug))]
282+
#[cfg_attr(any(feature = "std", test), derive(Debug))]
289283
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
290284
pub struct SignedPrevote<H, N, S, Id> {
291285
/// The prevote message which has been signed.
@@ -298,8 +292,7 @@ pub struct SignedPrevote<H, N, S, Id> {
298292

299293
/// A signed precommit message.
300294
#[derive(Clone, PartialEq, Eq)]
301-
#[cfg_attr(any(feature = "std", test), derive(
302-
Debug))]
295+
#[cfg_attr(any(feature = "std", test), derive(Debug))]
303296
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
304297
pub struct SignedPrecommit<H, N, S, Id> {
305298
/// The precommit message which has been signed.
@@ -312,8 +305,7 @@ pub struct SignedPrecommit<H, N, S, Id> {
312305

313306
/// A commit message with compact representation of authentication data.
314307
#[derive(Clone, PartialEq, Eq)]
315-
#[cfg_attr(any(feature = "std", test), derive(
316-
Debug))]
308+
#[cfg_attr(any(feature = "std", test), derive(Debug))]
317309
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
318310
pub struct CompactCommit<H, N, S, Id> {
319311
/// The target block's hash.
@@ -332,8 +324,7 @@ pub struct CompactCommit<H, N, S, Id> {
332324
/// This message contains a "base", which is a block all of the vote-targets are
333325
/// a descendent of.
334326
#[derive(Clone, PartialEq, Eq)]
335-
#[cfg_attr(any(feature = "std", test), derive(
336-
Debug))]
327+
#[cfg_attr(any(feature = "std", test), derive(Debug))]
337328
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
338329
pub struct CatchUp<H, N, S, Id> {
339330
/// Round number.
@@ -520,8 +511,7 @@ pub fn process_commit_validation_result<H, N>(
520511

521512
/// Historical votes seen in a round.
522513
#[derive(Default, Clone, PartialEq, Eq)]
523-
#[cfg_attr(any(feature = "std", test), derive(
524-
Debug))]
514+
#[cfg_attr(any(feature = "std", test), derive(Debug))]
525515
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
526516
pub struct HistoricalVotes<H, N, S, Id> {
527517
seen: Vec<SignedMessage<H, N, S, Id>>,

src/round.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ impl<Id: Ord + Eq + Clone, Vote: Clone + Eq, Signature: Clone + Eq> VoteTracker<
160160

161161
/// State of the round.
162162
#[derive(PartialEq, Clone)]
163-
#[cfg_attr(any(feature = "std", test), derive(
164-
Debug))]
163+
#[cfg_attr(any(feature = "std", test), derive(Debug))]
165164
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
166165
pub struct State<H, N> {
167166
/// The prevote-GHOST block.

src/voter/voting_round.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -608,17 +608,23 @@ impl<H, N, E: Environment<H, N>> VotingRound<H, N, E> where
608608

609609
// notify when new blocks are finalized or when the round-estimate is updated
610610
fn notify(&mut self, last_state: RoundState<H, N>, new_state: RoundState<H, N>) {
611-
if last_state == new_state { return }
612-
613-
if let Some(ref b) = self.bridged_round_state {
614-
b.update(new_state.clone());
611+
if last_state != new_state {
612+
if let Some(ref b) = self.bridged_round_state {
613+
b.update(new_state.clone());
614+
}
615615
}
616616

617-
if last_state.finalized != new_state.finalized && new_state.completable {
618-
// send notification only when the round is completable and we've cast votes.
619-
// this is a workaround that ensures when we re-instantiate the voter after
620-
// a shutdown, we never re-create the same round with a base that was finalized
621-
// in this round or after.
617+
// send notification only when the round is completable and we've cast votes.
618+
// this is a workaround that ensures when we re-instantiate the voter after
619+
// a shutdown, we never re-create the same round with a base that was finalized
620+
// in this round or after.
621+
// we try to notify if either the round state changed or if we haven't
622+
// sent any notification yet (this is to guard against seeing enough
623+
// votes to finalize before having precommited)
624+
let state_changed = last_state.finalized != new_state.finalized;
625+
let sent_finality_notifications = self.best_finalized.is_some();
626+
627+
if new_state.completable && (state_changed || !sent_finality_notifications) {
622628
if let (&Some(State::Precommitted), Some((ref f_hash, ref f_number))) = (&self.state, new_state.finalized) {
623629
let commit = Commit {
624630
target_hash: f_hash.clone(),

0 commit comments

Comments
 (0)