Skip to content

Commit 37e97cf

Browse files
octolandresilva
andauthored
grandpa-rpc: allow proving finality of blocks from latest authority set (paritytech#8585)
* grandpa: use new latest stored justification in prove_finality * grandpa: include end in range in FinalityProof::unknown_headers * grandpa: typo in comment * grandpa: remove ProvableJustification * grandpa: revert unnecessary changes * grandpa: extend AuthoritySetChangeId and cleanup get_set_id * grandpa: move check_finality_proof to the test module * grandpa: warn on missing authority set changes data * grandpa: add missing use statement * grandpa: simplify finality_proof tests * grandpa: additional tests for finality_proof Co-authored-by: André Silva <andrerfosilva@gmail.com>
1 parent 6181e37 commit 37e97cf

File tree

2 files changed

+344
-231
lines changed

2 files changed

+344
-231
lines changed

client/finality-grandpa/src/authorities.rs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
//! Utilities for dealing with authorities, authority sets, and handoffs.
2020
21+
use std::cmp::Ord;
22+
use std::fmt::Debug;
23+
use std::ops::Add;
24+
2125
use fork_tree::ForkTree;
2226
use parking_lot::MappedMutexGuard;
2327
use finality_grandpa::voter_set::VoterSet;
@@ -27,9 +31,7 @@ use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO};
2731
use sp_finality_grandpa::{AuthorityId, AuthorityList};
2832
use sc_consensus::shared_data::{SharedData, SharedDataLocked};
2933

30-
use std::cmp::Ord;
31-
use std::fmt::Debug;
32-
use std::ops::Add;
34+
use crate::SetId;
3335

3436
/// Error type returned on operations on the `AuthoritySet`.
3537
#[derive(Debug, derive_more::Display)]
@@ -684,6 +686,20 @@ impl<H, N: Add<Output=N> + Clone> PendingChange<H, N> {
684686
#[derive(Debug, Encode, Decode, Clone, PartialEq)]
685687
pub struct AuthoritySetChanges<N>(Vec<(u64, N)>);
686688

689+
/// The response when querying for a set id for a specific block. Either we get a set id
690+
/// together with a block number for the last block in the set, or that the requested block is in the
691+
/// latest set, or that we don't know what set id the given block belongs to.
692+
#[derive(Debug, PartialEq)]
693+
pub enum AuthoritySetChangeId<N> {
694+
/// The requested block is in the latest set.
695+
Latest,
696+
/// Tuple containing the set id and the last block number of that set.
697+
Set(SetId, N),
698+
/// We don't know which set id the request block belongs to (this can only happen due to missing
699+
/// data).
700+
Unknown,
701+
}
702+
687703
impl<N> From<Vec<(u64, N)>> for AuthoritySetChanges<N> {
688704
fn from(changes: Vec<(u64, N)>) -> AuthoritySetChanges<N> {
689705
AuthoritySetChanges(changes)
@@ -699,7 +715,15 @@ impl<N: Ord + Clone> AuthoritySetChanges<N> {
699715
self.0.push((set_id, block_number));
700716
}
701717

702-
pub(crate) fn get_set_id(&self, block_number: N) -> Option<(u64, N)> {
718+
pub(crate) fn get_set_id(&self, block_number: N) -> AuthoritySetChangeId<N> {
719+
if self.0
720+
.last()
721+
.map(|last_auth_change| last_auth_change.1 < block_number)
722+
.unwrap_or(false)
723+
{
724+
return AuthoritySetChangeId::Latest;
725+
}
726+
703727
let idx = self.0
704728
.binary_search_by_key(&block_number, |(_, n)| n.clone())
705729
.unwrap_or_else(|b| b);
@@ -711,16 +735,16 @@ impl<N: Ord + Clone> AuthoritySetChanges<N> {
711735
let (prev_set_id, _) = self.0[idx - 1usize];
712736
if set_id != prev_set_id + 1u64 {
713737
// Without the preceding set_id we don't have a well-defined start.
714-
return None;
738+
return AuthoritySetChangeId::Unknown;
715739
}
716740
} else if set_id != 0 {
717741
// If this is the first index, yet not the first set id then it's not well-defined
718742
// that we are in the right set id.
719-
return None;
743+
return AuthoritySetChangeId::Unknown;
720744
}
721-
Some((set_id, block_number))
745+
AuthoritySetChangeId::Set(set_id, block_number)
722746
} else {
723-
None
747+
AuthoritySetChangeId::Unknown
724748
}
725749
}
726750

@@ -1660,11 +1684,11 @@ mod tests {
16601684
authority_set_changes.append(1, 81);
16611685
authority_set_changes.append(2, 121);
16621686

1663-
assert_eq!(authority_set_changes.get_set_id(20), Some((0, 41)));
1664-
assert_eq!(authority_set_changes.get_set_id(40), Some((0, 41)));
1665-
assert_eq!(authority_set_changes.get_set_id(41), Some((0, 41)));
1666-
assert_eq!(authority_set_changes.get_set_id(42), Some((1, 81)));
1667-
assert_eq!(authority_set_changes.get_set_id(141), None);
1687+
assert_eq!(authority_set_changes.get_set_id(20), AuthoritySetChangeId::Set(0, 41));
1688+
assert_eq!(authority_set_changes.get_set_id(40), AuthoritySetChangeId::Set(0, 41));
1689+
assert_eq!(authority_set_changes.get_set_id(41), AuthoritySetChangeId::Set(0, 41));
1690+
assert_eq!(authority_set_changes.get_set_id(42), AuthoritySetChangeId::Set(1, 81));
1691+
assert_eq!(authority_set_changes.get_set_id(141), AuthoritySetChangeId::Latest);
16681692
}
16691693

16701694
#[test]
@@ -1674,11 +1698,11 @@ mod tests {
16741698
authority_set_changes.append(3, 81);
16751699
authority_set_changes.append(4, 121);
16761700

1677-
assert_eq!(authority_set_changes.get_set_id(20), None);
1678-
assert_eq!(authority_set_changes.get_set_id(40), None);
1679-
assert_eq!(authority_set_changes.get_set_id(41), None);
1680-
assert_eq!(authority_set_changes.get_set_id(42), Some((3, 81)));
1681-
assert_eq!(authority_set_changes.get_set_id(141), None);
1701+
assert_eq!(authority_set_changes.get_set_id(20), AuthoritySetChangeId::Unknown);
1702+
assert_eq!(authority_set_changes.get_set_id(40), AuthoritySetChangeId::Unknown);
1703+
assert_eq!(authority_set_changes.get_set_id(41), AuthoritySetChangeId::Unknown);
1704+
assert_eq!(authority_set_changes.get_set_id(42), AuthoritySetChangeId::Set(3, 81));
1705+
assert_eq!(authority_set_changes.get_set_id(141), AuthoritySetChangeId::Latest);
16821706
}
16831707

16841708
#[test]

0 commit comments

Comments
 (0)