From 60d1a0bdc3abd3e319666d37c3c0599fc4486792 Mon Sep 17 00:00:00 2001
From: Chris Sosnin <48099298+slumber@users.noreply.github.com>
Date: Fri, 8 Sep 2023 19:05:01 +0300
Subject: [PATCH] lookahead collator: read allowed ancestry len from relay
 client (#1180)

* Read allowed ancestry len from active config

* extract load_abridged_host_configuration

---------

Co-authored-by: Javier Viola <javier@parity.io>
---
 .../consensus/aura/src/collators/lookahead.rs | 37 +++++++++++++------
 cumulus/client/consensus/common/src/lib.rs    | 19 +++++++++-
 2 files changed, 43 insertions(+), 13 deletions(-)

diff --git a/cumulus/client/consensus/aura/src/collators/lookahead.rs b/cumulus/client/consensus/aura/src/collators/lookahead.rs
index 58d8e9d1a06..b1a7cde55c2 100644
--- a/cumulus/client/consensus/aura/src/collators/lookahead.rs
+++ b/cumulus/client/consensus/aura/src/collators/lookahead.rs
@@ -34,7 +34,8 @@
 use codec::{Codec, Encode};
 use cumulus_client_collator::service::ServiceInterface as CollatorServiceInterface;
 use cumulus_client_consensus_common::{
-	self as consensus_common, ParachainBlockImportMarker, ParentSearchParams,
+	self as consensus_common, load_abridged_host_configuration, ParachainBlockImportMarker,
+	ParentSearchParams,
 };
 use cumulus_client_consensus_proposer::ProposerInterface;
 use cumulus_primitives_aura::AuraUnincludedSegmentApi;
@@ -416,16 +417,30 @@ where
 	Some(SlotClaim::unchecked::<P>(author_pub, slot, timestamp))
 }
 
+/// Reads allowed ancestry length parameter from the relay chain storage at the given relay parent.
+///
+/// Falls back to 0 in case of an error.
 async fn max_ancestry_lookback(
-	_relay_parent: PHash,
-	_relay_client: &impl RelayChainInterface,
+	relay_parent: PHash,
+	relay_client: &impl RelayChainInterface,
 ) -> usize {
-	// TODO [https://github.com/paritytech/cumulus/issues/2706]
-	// We need to read the relay-chain state to know what the maximum
-	// age truly is, but that depends on those pallets existing.
-	//
-	// For now, just provide the conservative value of '2'.
-	// Overestimating can cause problems, as we'd be building on forks of the
-	// chain that can never get included. Underestimating is less of an issue.
-	2
+	match load_abridged_host_configuration(relay_parent, relay_client).await {
+		Ok(Some(config)) => config.async_backing_params.allowed_ancestry_len as usize,
+		Ok(None) => {
+			tracing::error!(
+				target: crate::LOG_TARGET,
+				"Active config is missing in relay chain storage",
+			);
+			0
+		},
+		Err(err) => {
+			tracing::error!(
+				target: crate::LOG_TARGET,
+				?err,
+				?relay_parent,
+				"Failed to read active config from relay chain client",
+			);
+			0
+		},
+	}
 }
diff --git a/cumulus/client/consensus/common/src/lib.rs b/cumulus/client/consensus/common/src/lib.rs
index edd97cf02a2..29ee7356ee2 100644
--- a/cumulus/client/consensus/common/src/lib.rs
+++ b/cumulus/client/consensus/common/src/lib.rs
@@ -20,8 +20,8 @@ use polkadot_primitives::{
 };
 
 use cumulus_primitives_core::{
-	relay_chain::{BlockId as RBlockId, OccupiedCoreAssumption},
-	ParaId,
+	relay_chain::{self, BlockId as RBlockId, OccupiedCoreAssumption},
+	AbridgedHostConfiguration, ParaId,
 };
 use cumulus_relay_chain_interface::{RelayChainError, RelayChainInterface};
 
@@ -412,3 +412,18 @@ pub fn relay_slot_and_timestamp(
 		})
 		.ok()
 }
+
+/// Reads abridged host configuration from the relay chain storage at the given relay parent.
+pub async fn load_abridged_host_configuration(
+	relay_parent: PHash,
+	relay_client: &impl RelayChainInterface,
+) -> Result<Option<AbridgedHostConfiguration>, RelayChainError> {
+	relay_client
+		.get_storage_by_key(relay_parent, relay_chain::well_known_keys::ACTIVE_CONFIG)
+		.await?
+		.map(|bytes| {
+			AbridgedHostConfiguration::decode(&mut &bytes[..])
+				.map_err(RelayChainError::DeserializationError)
+		})
+		.transpose()
+}
-- 
GitLab