From 91e07d53ad9b347137f837a157af7ba446f4d3b1 Mon Sep 17 00:00:00 2001
From: Boluwatife Bakre <tifebakre@yahoo.co.uk>
Date: Wed, 23 Mar 2022 15:34:41 +0100
Subject: [PATCH] Extract MAX_FINALITY_LAG constant from relay_chain_selection
 (#5159)

* Fix for Issue #4788

* Updated fix. Moved constant to node primitives

* cargo fmt

* Update node/primitives/src/lib.rs

Co-authored-by: Andronik <write@reusable.software>

* Update node/core/dispute-coordinator/src/real/initialized.rs

Co-authored-by: Andronik <write@reusable.software>
---
 polkadot/node/core/approval-voting/src/import.rs          | 7 ++++---
 .../node/core/dispute-coordinator/src/real/initialized.rs | 8 ++++----
 polkadot/node/primitives/src/lib.rs                       | 5 +++++
 polkadot/node/service/src/relay_chain_selection.rs        | 7 ++++---
 4 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/polkadot/node/core/approval-voting/src/import.rs b/polkadot/node/core/approval-voting/src/import.rs
index cb8dea17a51..c7d2b158889 100644
--- a/polkadot/node/core/approval-voting/src/import.rs
+++ b/polkadot/node/core/approval-voting/src/import.rs
@@ -29,8 +29,9 @@
 //! We maintain a rolling window of session indices. This starts as empty
 
 use polkadot_node_jaeger as jaeger;
-use polkadot_node_primitives::approval::{
-	self as approval_types, BlockApprovalMeta, RelayVRFStory,
+use polkadot_node_primitives::{
+	approval::{self as approval_types, BlockApprovalMeta, RelayVRFStory},
+	MAX_FINALITY_LAG,
 };
 use polkadot_node_subsystem::{
 	messages::{
@@ -299,7 +300,7 @@ pub(crate) async fn handle_new_head(
 	head: Hash,
 	finalized_number: &Option<BlockNumber>,
 ) -> SubsystemResult<Vec<BlockImportedCandidates>> {
-	const MAX_HEADS_LOOK_BACK: BlockNumber = 500;
+	const MAX_HEADS_LOOK_BACK: BlockNumber = MAX_FINALITY_LAG;
 
 	let mut span = jaeger::Span::new(head, "approval-checking-import");
 
diff --git a/polkadot/node/core/dispute-coordinator/src/real/initialized.rs b/polkadot/node/core/dispute-coordinator/src/real/initialized.rs
index ceb59e0e869..f29a2fe9663 100644
--- a/polkadot/node/core/dispute-coordinator/src/real/initialized.rs
+++ b/polkadot/node/core/dispute-coordinator/src/real/initialized.rs
@@ -31,7 +31,7 @@ use sc_keystore::LocalKeystore;
 
 use polkadot_node_primitives::{
 	CandidateVotes, DisputeMessage, DisputeMessageCheckError, SignedDisputeStatement,
-	DISPUTE_WINDOW,
+	DISPUTE_WINDOW, MAX_FINALITY_LAG,
 };
 use polkadot_node_subsystem::{
 	messages::{
@@ -69,9 +69,9 @@ use super::{
 };
 
 // The capacity and scrape depth are equal to the maximum allowed unfinalized depth.
-const LRU_SCRAPED_BLOCKS_CAPACITY: usize = 500;
-// This is in sync with `MAX_FINALITY_LAG` in relay chain selection.
-const MAX_BATCH_SCRAPE_ANCESTORS: u32 = 500;
+const LRU_SCRAPED_BLOCKS_CAPACITY: usize = MAX_FINALITY_LAG as usize;
+// This is in sync with `MAX_FINALITY_LAG` in relay chain selection & node primitives.
+const MAX_BATCH_SCRAPE_ANCESTORS: u32 = MAX_FINALITY_LAG;
 
 /// After the first active leaves update we transition to `Initialized` state.
 ///
diff --git a/polkadot/node/primitives/src/lib.rs b/polkadot/node/primitives/src/lib.rs
index 2d09d9c9635..9075a168f0b 100644
--- a/polkadot/node/primitives/src/lib.rs
+++ b/polkadot/node/primitives/src/lib.rs
@@ -76,6 +76,11 @@ pub const BACKING_EXECUTION_TIMEOUT: Duration = Duration::from_secs(2);
 /// dispute participants.
 pub const APPROVAL_EXECUTION_TIMEOUT: Duration = Duration::from_secs(6);
 
+/// Linked to `MAX_FINALITY_LAG` in relay chain selection,
+/// `MAX_HEADS_LOOK_BACK` in `approval-voting` and
+/// `MAX_BATCH_SCRAPE_ANCESTORS` in `dispute-coordinator`
+pub const MAX_FINALITY_LAG: u32 = 500;
+
 /// Type of a session window size.
 ///
 /// We are not using `NonZeroU32` here because `expect` and `unwrap` are not yet const, so global
diff --git a/polkadot/node/service/src/relay_chain_selection.rs b/polkadot/node/service/src/relay_chain_selection.rs
index 9f6cd50342e..9e5842f5a90 100644
--- a/polkadot/node/service/src/relay_chain_selection.rs
+++ b/polkadot/node/service/src/relay_chain_selection.rs
@@ -38,6 +38,7 @@
 use super::{HeaderProvider, HeaderProviderProvider};
 use consensus_common::{Error as ConsensusError, SelectChain};
 use futures::channel::oneshot;
+use polkadot_node_primitives::MAX_FINALITY_LAG as PRIMITIVES_MAX_FINALITY_LAG;
 use polkadot_node_subsystem_util::metrics::{self, prometheus};
 use polkadot_overseer::{AllMessages, Handle};
 use polkadot_primitives::v2::{
@@ -53,9 +54,9 @@ use std::sync::Arc;
 /// or disputes.
 ///
 /// This is a safety net that should be removed at some point in the future.
-// Until it's not, make sure to also update `MAX_HEADS_LOOK_BACK` in `approval-voting`
-// and `MAX_BATCH_SCRAPE_ANCESTORS` in `dispute-coordinator` when changing its value.
-const MAX_FINALITY_LAG: polkadot_primitives::v2::BlockNumber = 500;
+// In sync with `MAX_HEADS_LOOK_BACK` in `approval-voting`
+// and `MAX_BATCH_SCRAPE_ANCESTORS` in `dispute-coordinator`.
+const MAX_FINALITY_LAG: polkadot_primitives::v2::BlockNumber = PRIMITIVES_MAX_FINALITY_LAG;
 
 const LOG_TARGET: &str = "parachain::chain-selection";
 
-- 
GitLab