From 641bb7cb46590892f1d46a0fa63faad3d20e3111 Mon Sep 17 00:00:00 2001 From: Arkadiy Paronyan Date: Fri, 1 Feb 2019 17:17:53 +0100 Subject: [PATCH] Stop authoring blocks when offline (#1655) * Don't author blocks when offline * Increased canonicalization delay * Fixed test --- substrate/core/client/db/src/lib.rs | 2 +- substrate/core/consensus/aura/src/lib.rs | 5 +++++ substrate/core/consensus/common/src/lib.rs | 7 +++++++ substrate/core/network/src/protocol.rs | 2 +- substrate/core/network/src/service.rs | 3 +++ substrate/core/network/src/sync.rs | 8 ++++++++ substrate/core/rpc/src/system/tests.rs | 1 + 7 files changed, 26 insertions(+), 2 deletions(-) diff --git a/substrate/core/client/db/src/lib.rs b/substrate/core/client/db/src/lib.rs index 15a71344a5..9427e28c90 100644 --- a/substrate/core/client/db/src/lib.rs +++ b/substrate/core/client/db/src/lib.rs @@ -56,7 +56,7 @@ use crate::storage_cache::{CachingState, SharedCache, new_shared_cache}; use log::{trace, debug, warn}; 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 STATE_CACHE_SIZE_BYTES: usize = 16 * 1024 * 1024; diff --git a/substrate/core/consensus/aura/src/lib.rs b/substrate/core/consensus/aura/src/lib.rs index a531bd8bba..154bc214f6 100644 --- a/substrate/core/consensus/aura/src/lib.rs +++ b/substrate/core/consensus/aura/src/lib.rs @@ -272,6 +272,11 @@ pub fn start_aura( } }; + 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) { None => return Either::B(future::ok(())), Some(author) => if author.0 == public_key.0 { diff --git a/substrate/core/consensus/common/src/lib.rs b/substrate/core/consensus/common/src/lib.rs index ccbafdb863..904e013256 100644 --- a/substrate/core/consensus/common/src/lib.rs +++ b/substrate/core/consensus/common/src/lib.rs @@ -88,6 +88,9 @@ pub trait SyncOracle { /// Whether the synchronization service is undergoing major sync. /// Returns true if so. 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. @@ -96,10 +99,14 @@ pub struct NoNetwork; impl SyncOracle for NoNetwork { fn is_major_syncing(&self) -> bool { false } + fn is_offline(&self) -> bool { false } } impl SyncOracle for Arc { fn is_major_syncing(&self) -> bool { T::is_major_syncing(&*self) } + fn is_offline(&self) -> bool { + T::is_offline(&*self) + } } diff --git a/substrate/core/network/src/protocol.rs b/substrate/core/network/src/protocol.rs index 2506dcd546..b2e02ea4e3 100644 --- a/substrate/core/network/src/protocol.rs +++ b/substrate/core/network/src/protocol.rs @@ -433,7 +433,7 @@ impl, H: ExHashT> Protocol { } 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) = as Decode>::decode(&mut Cursor::new(msg_bytes)) { debug!(target: "sync", "{:?}", msg); } else { diff --git a/substrate/core/network/src/service.rs b/substrate/core/network/src/service.rs index 05b455aead..7e74c19ff5 100644 --- a/substrate/core/network/src/service.rs +++ b/substrate/core/network/src/service.rs @@ -234,6 +234,9 @@ impl, H: ExHashT> ::consensus:: fn is_major_syncing(&self) -> bool { self.handler.sync().read().status().is_major_syncing() } + fn is_offline(&self) -> bool { + self.handler.sync().read().status().is_offline() + } } impl, H:ExHashT> Drop for Service { diff --git a/substrate/core/network/src/sync.rs b/substrate/core/network/src/sync.rs index dc929899c5..b9fd2d289c 100644 --- a/substrate/core/network/src/sync.rs +++ b/substrate/core/network/src/sync.rs @@ -263,6 +263,8 @@ pub struct Status { pub state: SyncState, /// Target sync block number. pub best_seen_block: Option>, + /// Number of peers participating in syncing. + pub num_peers: u32, } impl Status { @@ -274,6 +276,11 @@ impl Status { SyncState::Downloading => true, } } + + /// Are we all alone? + pub fn is_offline(&self) -> bool { + self.num_peers == 0 + } } impl ChainSync { @@ -315,6 +322,7 @@ impl ChainSync { Status { state: state, best_seen_block: best_seen, + num_peers: self.peers.len() as u32, } } diff --git a/substrate/core/rpc/src/system/tests.rs b/substrate/core/rpc/src/system/tests.rs index 1f13abd7ec..5f6214de4f 100644 --- a/substrate/core/rpc/src/system/tests.rs +++ b/substrate/core/rpc/src/system/tests.rs @@ -33,6 +33,7 @@ impl network::SyncProvider for Status { sync: SyncStatus { state: if self.is_syncing { SyncState::Downloading } else { SyncState::Idle }, best_seen_block: None, + num_peers: self.peers as u32, }, num_peers: self.peers, num_active_peers: 0,