From bd052d8da488b8bca1e5c226c45a396ab6214a37 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Silva?= <andre.beat@gmail.com>
Date: Tue, 30 Jul 2019 22:08:20 +0100
Subject: [PATCH] babe: Disable unused median calculation (#3251)

* babe: don't execute median algorithm

* babe: checked sub in median calculation

* node: bump impl_version to make ci happy

* babe: remove forbid dead_code
---
 substrate/core/consensus/babe/src/lib.rs | 17 ++++++-----------
 substrate/node/runtime/src/lib.rs        |  2 +-
 2 files changed, 7 insertions(+), 12 deletions(-)

diff --git a/substrate/core/consensus/babe/src/lib.rs b/substrate/core/consensus/babe/src/lib.rs
index 4a4261b8dd8..fae6fb3c32f 100644
--- a/substrate/core/consensus/babe/src/lib.rs
+++ b/substrate/core/consensus/babe/src/lib.rs
@@ -19,7 +19,6 @@
 //! BABE (Blind Assignment for Blockchain Extension) consensus in Substrate.
 
 #![forbid(unsafe_code, missing_docs, unused_must_use, unused_imports, unused_variables)]
-#![cfg_attr(not(test), forbid(dead_code))]
 pub use babe_primitives::*;
 pub use consensus_common::SyncOracle;
 use consensus_common::ImportResult;
@@ -581,6 +580,7 @@ impl<C> BabeVerifier<C> {
 	}
 }
 
+#[allow(dead_code)]
 fn median_algorithm(
 	median_required_blocks: u64,
 	slot_duration: u64,
@@ -612,8 +612,12 @@ fn median_algorithm(
 			.get(num_timestamps / 2)
 			.expect("we have at least one timestamp, so this is a valid index; qed");
 
+		let now = Instant::now();
+		if now >= median {
+			time_source.0.replace(now - median);
+		}
+
 		time_source.1.clear();
-		time_source.0.replace(Instant::now() - median);
 	} else {
 		time_source.1.push((Instant::now(), slot_now))
 	}
@@ -706,15 +710,6 @@ impl<B: BlockT, C> Verifier<B> for BabeVerifier<C> where
 					fork_choice: ForkChoiceStrategy::LongestChain,
 				};
 
-				// FIXME: this should eventually be moved to BabeBlockImport
-				median_algorithm(
-					self.config.0.median_required_blocks,
-					self.config.get(),
-					slot_number,
-					slot_now,
-					&mut *self.time_source.0.lock(),
-				);
-
 				Ok((import_block, Default::default()))
 			}
 			CheckedHeader::Deferred(a, b) => {
diff --git a/substrate/node/runtime/src/lib.rs b/substrate/node/runtime/src/lib.rs
index 6be19f02ae0..af1812f34dd 100644
--- a/substrate/node/runtime/src/lib.rs
+++ b/substrate/node/runtime/src/lib.rs
@@ -80,7 +80,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
 	// implementation changes and behavior does not, then leave spec_version as
 	// is and increment impl_version.
 	spec_version: 123,
-	impl_version: 124,
+	impl_version: 125,
 	apis: RUNTIME_API_VERSIONS,
 };
 
-- 
GitLab