note future changes when importing block

This commit is contained in:
Robert Habermeier
2018-10-25 16:48:51 +02:00
parent fb1401ab79
commit 08890f2bc1
7 changed files with 138 additions and 44 deletions
@@ -16,23 +16,33 @@
//! Utilities for dealing with authorities, authority sets, and handoffs.
use parking_lot::RwLock;
use substrate_primitives::AuthorityId;
use std::cmp::Ord;
use std::ops::Add;
use std::sync::Arc;
/// A shared authority set.
pub(crate) struct SharedAuthoritySet<H, N> {
inner: RwLock<AuthoritySet<H, N>>,
inner: Arc<RwLock<AuthoritySet<H, N>>>,
}
impl<H, N> Clone for SharedAuthoritySet<H, N> {
fn clone(&self) -> Self {
SharedAuthoritySet { inner: self.inner.clone() }
}
}
impl<H, N> SharedAuthoritySet<H, N> {
/// The genesis authority set.
pub(crate) fn genesis(initial: Vec<(AuthorityId, usize)>) -> Self {
pub(crate) fn genesis(initial: Vec<(AuthorityId, u64)>) -> Self {
SharedAuthoritySet {
inner: RwLock::new(AuthoritySet {
inner: Arc::new(RwLock::new(AuthoritySet {
current_authorities: initial,
set_id: 0,
pending_changes: Vec::new(),
})
}))
}
}
@@ -42,33 +52,35 @@ impl<H, N> SharedAuthoritySet<H, N> {
{
f(&*self.inner.read())
}
}
impl<H, N: Add + Clone> SharedAuthoritySet<H, N> {
impl<H, N: Add<Output=N> + Ord + Clone> SharedAuthoritySet<H, N> {
/// Note an upcoming pending transition.
pub(crate) fn add_pending_change(&self, pending: PendingChange<H, N>) {
let idx = self.pending_changes
.binary_search_by_key(|change| change.effective_number())
let mut inner = self.inner.write();
let idx = inner.pending_changes
.binary_search_by_key(&pending.effective_number(), |change| change.effective_number())
.unwrap_or_else(|i| i);
self.pending_changes.insert(idx);
inner.pending_changes.insert(idx, pending);
}
/// Get the earliest limit-block number, if any.
pub(crate) fn current_limit(&self) -> Option<&N> {
self.pending_changes.get(0).map(|change| &change.effective_number());
pub(crate) fn current_limit(&self) -> Option<N> {
self.inner.read().pending_changes.get(0).map(|change| change.effective_number().clone())
}
}
impl<H, N> From<AuthoritySet<H, N>> for SharedAuthoritySet<H, N> {
fn from(set: AuthoritySet<H, N>) -> Self {
SharedAuthoritySet { inner: RwLock::new(set) }
SharedAuthoritySet { inner: Arc::new(RwLock::new(set)) }
}
}
/// A set of authorities.
#[derive(Encode, Decode)]
pub(crate) struct AuthoritySet<H, N> {
current_authorities: Vec<(AuthorityId, usize)>,
current_authorities: Vec<(AuthorityId, u64)>,
set_id: u64,
pending_changes: Vec<PendingChange<H, N>>,
}
@@ -80,7 +92,7 @@ pub(crate) struct AuthoritySet<H, N> {
#[derive(Encode, Decode)]
pub(crate) struct PendingChange<H, N> {
/// The new authorities and weights to apply.
pub(crate) next_authorities: Vec<(AuthorityId, usize)>,
pub(crate) next_authorities: Vec<(AuthorityId, u64)>,
/// How deep in the finalized chain the announcing block must be
/// before the change is applied.
pub(crate) finalization_depth: N,
@@ -90,9 +102,9 @@ pub(crate) struct PendingChange<H, N> {
pub(crate) canon_hash: H,
}
impl<H, N: Add + Clone> PendingChange<H, N> {
impl<H, N: Add<Output=N> + Clone> PendingChange<H, N> {
/// Returns the effective number.
fn effective_number(&self) -> N {
self.canon_height + self.finalization_depth
self.canon_height.clone() + self.finalization_depth.clone()
}
}