Stop authoring blocks when offline (#1655)

* Don't author blocks when offline

* Increased canonicalization delay

* Fixed test
This commit is contained in:
Arkadiy Paronyan
2019-02-01 17:17:53 +01:00
committed by Gav Wood
parent 2155e44e13
commit 641bb7cb46
7 changed files with 26 additions and 2 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ use crate::storage_cache::{CachingState, SharedCache, new_shared_cache};
use log::{trace, debug, warn}; use log::{trace, debug, warn};
pub use state_db::PruningMode; pub use state_db::PruningMode;
const CANONICALIZATION_DELAY: u64 = 256; const CANONICALIZATION_DELAY: u64 = 4096;
const MIN_BLOCKS_TO_KEEP_CHANGES_TRIES_FOR: u64 = 32768; const MIN_BLOCKS_TO_KEEP_CHANGES_TRIES_FOR: u64 = 32768;
const STATE_CACHE_SIZE_BYTES: usize = 16 * 1024 * 1024; const STATE_CACHE_SIZE_BYTES: usize = 16 * 1024 * 1024;
+5
View File
@@ -272,6 +272,11 @@ pub fn start_aura<B, C, E, I, SO, Error>(
} }
}; };
if sync_oracle.is_offline() && authorities.len() > 1 {
debug!(target: "aura", "Skipping proposal slot. Waiting for the netork.");
return Either::B(future::ok(()));
}
let proposal_work = match slot_author(slot_num, &authorities) { let proposal_work = match slot_author(slot_num, &authorities) {
None => return Either::B(future::ok(())), None => return Either::B(future::ok(())),
Some(author) => if author.0 == public_key.0 { Some(author) => if author.0 == public_key.0 {
@@ -88,6 +88,9 @@ pub trait SyncOracle {
/// Whether the synchronization service is undergoing major sync. /// Whether the synchronization service is undergoing major sync.
/// Returns true if so. /// Returns true if so.
fn is_major_syncing(&self) -> bool; fn is_major_syncing(&self) -> bool;
/// Whether the synchronization service is offline.
/// Returns true if so.
fn is_offline(&self) -> bool;
} }
/// A synchronization oracle for when there is no network. /// A synchronization oracle for when there is no network.
@@ -96,10 +99,14 @@ pub struct NoNetwork;
impl SyncOracle for NoNetwork { impl SyncOracle for NoNetwork {
fn is_major_syncing(&self) -> bool { false } fn is_major_syncing(&self) -> bool { false }
fn is_offline(&self) -> bool { false }
} }
impl<T: SyncOracle> SyncOracle for Arc<T> { impl<T: SyncOracle> SyncOracle for Arc<T> {
fn is_major_syncing(&self) -> bool { fn is_major_syncing(&self) -> bool {
T::is_major_syncing(&*self) T::is_major_syncing(&*self)
} }
fn is_offline(&self) -> bool {
T::is_offline(&*self)
}
} }
+1 -1
View File
@@ -433,7 +433,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
} }
debug!(target: "sync", "{} clogging messages:", clogging_messages.len()); debug!(target: "sync", "{} clogging messages:", clogging_messages.len());
for msg_bytes in clogging_messages { for msg_bytes in clogging_messages.take(5) {
if let Some(msg) = <Message<B> as Decode>::decode(&mut Cursor::new(msg_bytes)) { if let Some(msg) = <Message<B> as Decode>::decode(&mut Cursor::new(msg_bytes)) {
debug!(target: "sync", "{:?}", msg); debug!(target: "sync", "{:?}", msg);
} else { } else {
+3
View File
@@ -234,6 +234,9 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> ::consensus::
fn is_major_syncing(&self) -> bool { fn is_major_syncing(&self) -> bool {
self.handler.sync().read().status().is_major_syncing() self.handler.sync().read().status().is_major_syncing()
} }
fn is_offline(&self) -> bool {
self.handler.sync().read().status().is_offline()
}
} }
impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H:ExHashT> Drop for Service<B, S, H> { impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H:ExHashT> Drop for Service<B, S, H> {
+8
View File
@@ -263,6 +263,8 @@ pub struct Status<B: BlockT> {
pub state: SyncState, pub state: SyncState,
/// Target sync block number. /// Target sync block number.
pub best_seen_block: Option<NumberFor<B>>, pub best_seen_block: Option<NumberFor<B>>,
/// Number of peers participating in syncing.
pub num_peers: u32,
} }
impl<B: BlockT> Status<B> { impl<B: BlockT> Status<B> {
@@ -274,6 +276,11 @@ impl<B: BlockT> Status<B> {
SyncState::Downloading => true, SyncState::Downloading => true,
} }
} }
/// Are we all alone?
pub fn is_offline(&self) -> bool {
self.num_peers == 0
}
} }
impl<B: BlockT> ChainSync<B> { impl<B: BlockT> ChainSync<B> {
@@ -315,6 +322,7 @@ impl<B: BlockT> ChainSync<B> {
Status { Status {
state: state, state: state,
best_seen_block: best_seen, best_seen_block: best_seen,
num_peers: self.peers.len() as u32,
} }
} }
+1
View File
@@ -33,6 +33,7 @@ impl network::SyncProvider<Block> for Status {
sync: SyncStatus { sync: SyncStatus {
state: if self.is_syncing { SyncState::Downloading } else { SyncState::Idle }, state: if self.is_syncing { SyncState::Downloading } else { SyncState::Idle },
best_seen_block: None, best_seen_block: None,
num_peers: self.peers as u32,
}, },
num_peers: self.peers, num_peers: self.peers,
num_active_peers: 0, num_active_peers: 0,