git.delta.rocks / unique-network / refs/commits / d0d6ce422c83

difftreelog

Merge pull request #1012 from UniqueNetwork/feature/lookahead-leftovers

Yaroslav Bolyukin2023-10-16parents: #0a49946 #700b0c8.patch.diff
in: master
Fix lookahead collator build

11 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6450,6 +6450,7 @@
  "cumulus-pallet-parachain-system",
  "cumulus-pallet-xcm",
  "cumulus-pallet-xcmp-queue",
+ "cumulus-primitives-aura",
  "cumulus-primitives-core",
  "cumulus-primitives-timestamp",
  "cumulus-primitives-utility",
@@ -8109,12 +8110,14 @@
  "pallet-evm-coder-substrate",
  "pallet-nonfungible",
  "pallet-refungible",
+ "pallet-structure",
  "parity-scale-codec",
  "scale-info",
  "sp-core",
  "sp-io",
  "sp-runtime",
  "sp-std",
+ "up-common",
  "up-data-structs",
 ]
 
@@ -14732,6 +14735,7 @@
  "cumulus-client-consensus-proposer",
  "cumulus-client-network",
  "cumulus-client-service",
+ "cumulus-primitives-aura",
  "cumulus-primitives-core",
  "cumulus-primitives-parachain-inherent",
  "cumulus-relay-chain-inprocess-interface",
modifiedCargo.tomldiffbeforeafterboth
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -100,6 +100,7 @@
 cumulus-pallet-parachain-system = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0" }
 cumulus-pallet-xcm = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0" }
 cumulus-pallet-xcmp-queue = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0" }
+cumulus-primitives-aura = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0" }
 cumulus-primitives-core = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0" }
 cumulus-primitives-parachain-inherent = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0" }
 cumulus-primitives-timestamp = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0" }
modifiednode/cli/Cargo.tomldiffbeforeafterboth
--- a/node/cli/Cargo.toml
+++ b/node/cli/Cargo.toml
@@ -36,6 +36,7 @@
 cumulus-client-consensus-proposer = { workspace = true }
 cumulus-client-network = { workspace = true }
 cumulus-client-service = { workspace = true }
+cumulus-primitives-aura = { workspace = true }
 cumulus-primitives-core = { workspace = true }
 cumulus-primitives-parachain-inherent = { features = ["std"], workspace = true }
 cumulus-relay-chain-inprocess-interface = { workspace = true }
@@ -113,7 +114,9 @@
 	'quartz-runtime?/gov-test-timings',
 	'unique-runtime?/gov-test-timings',
 ]
-lookahead = []
+lookahead = [
+	'opal-runtime/lookahead'
+]
 pov-estimate = [
 	'opal-runtime/pov-estimate',
 	'quartz-runtime?/pov-estimate',
modifiednode/cli/src/command.rsdiffbeforeafterboth
--- a/node/cli/src/command.rs
+++ b/node/cli/src/command.rs
@@ -396,6 +396,8 @@
 			}
 		}
 		#[cfg(feature = "try-runtime")]
+		// embedded try-runtime cli will be removed soon.
+		#[allow(deprecated)]
 		Some(Subcommand::TryRuntime(cmd)) => {
 			use std::{future::Future, pin::Pin};
 
modifiednode/cli/src/service.rsdiffbeforeafterboth
--- a/node/cli/src/service.rs
+++ b/node/cli/src/service.rs
@@ -221,6 +221,14 @@
 	{
 	}
 );
+#[cfg(not(feature = "lookahead"))]
+ez_bounds!(
+	pub trait LookaheadApiDep {}
+);
+#[cfg(feature = "lookahead")]
+ez_bounds!(
+	pub trait LookaheadApiDep: cumulus_primitives_aura::AuraUnincludedSegmentApi<Block> {}
+);
 
 /// Starts a `ServiceBuilder` for a full service.
 ///
@@ -358,6 +366,7 @@
 		+ Sync
 		+ 'static,
 	RuntimeApi::RuntimeApi: RuntimeApiDep<Runtime> + 'static,
+	RuntimeApi::RuntimeApi: LookaheadApiDep,
 	Runtime: RuntimeInstance,
 	ExecutorDispatch: NativeExecutionDispatch + 'static,
 {
@@ -687,6 +696,8 @@
 	announce_block: Arc<dyn Fn(Hash, Option<Vec<u8>>) + Send + Sync>,
 }
 
+// Clones ignored for optional lookahead collator
+#[allow(clippy::redundant_clone)]
 pub fn start_consensus<ExecutorDispatch, RuntimeApi, Runtime>(
 	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,
 	transaction_pool: Arc<
@@ -701,6 +712,7 @@
 		+ Sync
 		+ 'static,
 	RuntimeApi::RuntimeApi: RuntimeApiDep<Runtime> + 'static,
+	RuntimeApi::RuntimeApi: LookaheadApiDep,
 	Runtime: RuntimeInstance,
 {
 	let StartConsensusParameters {
@@ -735,12 +747,12 @@
 		client.clone(),
 	);
 
-	let block_import = ParachainBlockImport::new(client.clone(), backend);
+	let block_import = ParachainBlockImport::new(client.clone(), backend.clone());
 
 	let params = BuildAuraConsensusParams {
 		create_inherent_data_providers: move |_, ()| async move { Ok(()) },
 		block_import,
-		para_client: client,
+		para_client: client.clone(),
 		#[cfg(feature = "lookahead")]
 		para_backend: backend,
 		para_id,
@@ -751,10 +763,19 @@
 		proposer,
 		collator_service,
 		// With async-baking, we allowed to be both slower (longer authoring) and faster (multiple para blocks per relay block)
+		#[cfg(not(feature = "lookahead"))]
 		authoring_duration: Duration::from_millis(500),
+		#[cfg(feature = "lookahead")]
+		authoring_duration: Duration::from_millis(1500),
 		overseer_handle,
 		#[cfg(feature = "lookahead")]
-		code_hash_provider: || {},
+		code_hash_provider: move |block_hash| {
+			client
+				.code_at(block_hash)
+				.ok()
+				.map(cumulus_primitives_core::relay_chain::ValidationCode)
+				.map(|c| c.hash())
+		},
 		collator_key,
 		relay_chain_slot_duration,
 	};
@@ -762,7 +783,10 @@
 	task_manager.spawn_essential_handle().spawn(
 		"aura",
 		None,
+		#[cfg(not(feature = "lookahead"))]
 		run_aura::<_, AuraAuthorityPair, _, _, _, _, _, _, _>(params),
+		#[cfg(feature = "lookahead")]
+		run_aura::<_, AuraAuthorityPair, _, _, _, _, _, _, _, _, _>(params),
 	);
 	Ok(())
 }
modifiedpallets/inflation/src/lib.rsdiffbeforeafterboth
before · pallets/inflation/src/lib.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! # Inflation18//!19//! The inflation pallet is designed to increase the number of tokens at certain intervals.20//! With each iteration, increases the `total_issuance` value for the native token.21//! Executing an `on_initialize` hook at the beginning of each block, causing inflation to begin.22//!23//! ## Interface24//!25//! ### Dispatchable Functions26//!27//! * `start_inflation` - This method sets the inflation start date. Can be only called once.28//! Inflation start block can be backdated and will catch up. The method will create Treasury29//! account if it does not exist and perform the first inflation deposit.3031// #![recursion_limit = "1024"]32#![cfg_attr(not(feature = "std"), no_std)]3334#[cfg(feature = "runtime-benchmarks")]35mod benchmarking;3637#[cfg(test)]38mod tests;3940use frame_support::traits::{41	fungible::{Balanced, Inspect, Mutate},42	tokens::Precision,43	Get,44};45use frame_system::pallet_prelude::BlockNumberFor;46pub use pallet::*;47use sp_runtime::{traits::BlockNumberProvider, Perbill};48use sp_std::convert::TryInto;4950type BalanceOf<T> =51	<<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;5253pub const YEAR: u32 = 5_259_600; // 6-second block54								 // pub const YEAR: u32 = 2_629_800; // 12-second block55pub const TOTAL_YEARS_UNTIL_FLAT: u32 = 9;56pub const START_INFLATION_PERCENT: u32 = 10;57pub const END_INFLATION_PERCENT: u32 = 4;5859#[frame_support::pallet]60pub mod pallet {61	use frame_support::pallet_prelude::*;62	use frame_system::pallet_prelude::*;6364	use super::*;6566	#[pallet::config]67	pub trait Config: frame_system::Config {68		type Currency: Balanced<Self::AccountId>69			+ Inspect<Self::AccountId>70			+ Mutate<Self::AccountId>;71		type TreasuryAccountId: Get<Self::AccountId>;7273		// The block number provider74		type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;7576		/// Number of blocks that pass between treasury balance updates due to inflation77		#[pallet::constant]78		type InflationBlockInterval: Get<BlockNumberFor<Self>>;79	}8081	#[pallet::pallet]82	pub struct Pallet<T>(_);8384	/// starting year total issuance85	#[pallet::storage]86	pub type StartingYearTotalIssuance<T: Config> =87		StorageValue<Value = BalanceOf<T>, QueryKind = ValueQuery>;8889	/// Current inflation for `InflationBlockInterval` number of blocks90	#[pallet::storage]91	pub type BlockInflation<T: Config> = StorageValue<Value = BalanceOf<T>, QueryKind = ValueQuery>;9293	/// Next target (relay) block when inflation will be applied94	#[pallet::storage]95	pub type NextInflationBlock<T: Config> =96		StorageValue<Value = BlockNumberFor<T>, QueryKind = ValueQuery>;9798	/// Next target (relay) block when inflation is recalculated99	#[pallet::storage]100	pub type NextRecalculationBlock<T: Config> =101		StorageValue<Value = BlockNumberFor<T>, QueryKind = ValueQuery>;102103	/// Relay block when inflation has started104	#[pallet::storage]105	pub type StartBlock<T: Config> =106		StorageValue<Value = BlockNumberFor<T>, QueryKind = ValueQuery>;107108	#[pallet::hooks]109	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {110		fn on_initialize(_: BlockNumberFor<T>) -> Weight111		where112			BlockNumberFor<T>: From<u32>,113		{114			let mut consumed_weight = Weight::zero();115			let mut add_weight = |reads, writes, weight| {116				consumed_weight += T::DbWeight::get().reads_writes(reads, writes);117				consumed_weight += weight;118			};119120			let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);121			let current_relay_block = T::BlockNumberProvider::current_block_number();122			let next_inflation: BlockNumberFor<T> = <NextInflationBlock<T>>::get();123			add_weight(1, 0, Weight::from_parts(5_000_000, 0));124125			// Apply inflation every InflationBlockInterval blocks126			// If next_inflation == 0, this means inflation wasn't yet initialized127			if (next_inflation != 0u32.into()) && (current_relay_block >= next_inflation) {128				// Recalculate inflation on the first block of the year (or if it is not initialized yet)129				// Do the "current_relay_block >= next_recalculation" check in the "current_relay_block >= next_inflation"130				// block because it saves InflationBlockInterval DB reads for NextRecalculationBlock.131				let next_recalculation: BlockNumberFor<T> = <NextRecalculationBlock<T>>::get();132				add_weight(1, 0, Weight::zero());133				if current_relay_block >= next_recalculation {134					Self::recalculate_inflation(next_recalculation);135					add_weight(0, 4, Weight::from_parts(5_000_000, 0));136				}137138				T::Currency::mint_into(&T::TreasuryAccountId::get(), <BlockInflation<T>>::get())139					.ok();140141				// Update inflation block142				<NextInflationBlock<T>>::set(next_inflation + block_interval.into());143144				add_weight(3, 3, Weight::from_parts(10_000_000, 0));145			}146147			consumed_weight148		}149	}150151	#[pallet::call]152	impl<T: Config> Pallet<T> {153		/// This method sets the inflation start date. Can be only called once.154		/// Inflation start block can be backdated and will catch up. The method will create Treasury155		/// account if it does not exist and perform the first inflation deposit.156		///157		/// # Permissions158		///159		/// * Root160		///161		/// # Arguments162		///163		/// * inflation_start_relay_block: The relay chain block at which inflation should start164		#[pallet::call_index(0)]165		// Constant weights are deprecated,166		// but in this case writing benchmark is not feasible, `start_inflation` call167		// might be even moved to GenesisConfig168		#[pallet::weight(Weight::from_parts(0, 0))]169		pub fn start_inflation(170			origin: OriginFor<T>,171			inflation_start_relay_block: BlockNumberFor<T>,172		) -> DispatchResult173		where174			BlockNumberFor<T>: From<u32>,175		{176			ensure_root(origin)?;177178			// Start inflation if it has not been yet initialized179			if <StartBlock<T>>::get() == 0u32.into() {180				// Set inflation global start block181				<StartBlock<T>>::set(inflation_start_relay_block);182183				// Recalculate inflation. This can be backdated and will catch up.184				Self::recalculate_inflation(inflation_start_relay_block);185				let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);186				<NextInflationBlock<T>>::set(inflation_start_relay_block + block_interval.into());187188				// First time deposit - create Treasury account so that we can call deposit_into_existing everywhere else189				let _ = T::Currency::deposit(190					&T::TreasuryAccountId::get(),191					<BlockInflation<T>>::get(),192					Precision::Exact,193				)?;194			}195196			Ok(())197		}198	}199}200201impl<T: Config> Pallet<T> {202	pub fn recalculate_inflation(recalculation_block: BlockNumberFor<T>) {203		let current_year: u32 = ((recalculation_block - <StartBlock<T>>::get())204			/ BlockNumberFor::<T>::from(YEAR))205		.try_into()206		.unwrap_or(0);207		let block_interval: u32 = T::InflationBlockInterval::get().try_into().unwrap_or(0);208209		let one_percent = Perbill::from_percent(1);210211		if current_year <= TOTAL_YEARS_UNTIL_FLAT {212			let amount: BalanceOf<T> = Perbill::from_rational(213				block_interval214					* (START_INFLATION_PERCENT * TOTAL_YEARS_UNTIL_FLAT215						- current_year * (START_INFLATION_PERCENT - END_INFLATION_PERCENT)),216				YEAR * TOTAL_YEARS_UNTIL_FLAT,217			) * (one_percent * T::Currency::total_issuance());218			<BlockInflation<T>>::put(amount);219		} else {220			let amount: BalanceOf<T> =221				Perbill::from_rational(block_interval * END_INFLATION_PERCENT, YEAR)222					* (one_percent * T::Currency::total_issuance());223			<BlockInflation<T>>::put(amount);224		}225		<StartingYearTotalIssuance<T>>::set(T::Currency::total_issuance());226227		// Update recalculation and inflation blocks228		<NextRecalculationBlock<T>>::set(recalculation_block + YEAR.into());229	}230}
modifiedprimitives/common/src/constants.rsdiffbeforeafterboth
--- a/primitives/common/src/constants.rs
+++ b/primitives/common/src/constants.rs
@@ -23,7 +23,10 @@
 
 use crate::types::{Balance, BlockNumber};
 
+#[cfg(not(feature = "lookahead"))]
 pub const MILLISECS_PER_BLOCK: u64 = 12000;
+#[cfg(feature = "lookahead")]
+pub const MILLISECS_PER_BLOCK: u64 = 3000;
 pub const MILLISECS_PER_RELAY_BLOCK: u64 = 6000;
 
 pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;
modifiedruntime/common/config/pallets/mod.rsdiffbeforeafterboth
--- a/runtime/common/config/pallets/mod.rs
+++ b/runtime/common/config/pallets/mod.rs
@@ -21,7 +21,7 @@
 	traits::{ConstU32, ConstU64, Currency},
 };
 use sp_arithmetic::Perbill;
-use sp_runtime::traits::AccountIdConversion;
+use sp_runtime::traits::{AccountIdConversion, BlockNumberProvider};
 use up_common::{
 	constants::*,
 	types::{AccountId, Balance, BlockNumber},
@@ -105,12 +105,34 @@
 	pub const InflationBlockInterval: BlockNumber = 100; // every time per how many blocks inflation is applied
 }
 
+/// Pallet-inflation needs block number in on_initialize, where there is no `validation_data` exists yet
+pub struct OnInitializeBlockNumberProvider;
+impl BlockNumberProvider for OnInitializeBlockNumberProvider {
+	type BlockNumber = BlockNumber;
+
+	fn current_block_number() -> Self::BlockNumber {
+		use hex_literal::hex;
+		use parity_scale_codec::Decode;
+		use sp_io::storage;
+		// TODO: Replace with the following code after https://github.com/paritytech/polkadot-sdk/commit/3ea497b5a0fdda252f9c5a3c257cfaf8685f02fd lands
+		// <cumulus_pallet_parachain_system::Pallet<Runtime>>::last_relay_block_number()
+
+		// ParachainSystem.LastRelayChainBlockNumber
+		let Some(encoded) = storage::get(&hex!("45323df7cc47150b3930e2666b0aa313a2bca190d36bd834cc73a38fc213ecbd")) else {
+			// First parachain block
+			return Default::default()
+		};
+		BlockNumber::decode(&mut encoded.as_ref())
+			.expect("typeof(RelayBlockNumber) == typeof(BlockNumber) == u32; qed")
+	}
+}
+
 /// Used for the pallet inflation
 impl pallet_inflation::Config for Runtime {
 	type Currency = Balances;
 	type TreasuryAccountId = TreasuryAccountId;
 	type InflationBlockInterval = InflationBlockInterval;
-	type BlockNumberProvider = RelayChainBlockNumberProvider<Runtime>;
+	type OnInitializeBlockNumberProvider = OnInitializeBlockNumberProvider;
 }
 
 impl pallet_unique::Config for Runtime {
modifiedruntime/common/config/parachain.rsdiffbeforeafterboth
--- a/runtime/common/config/parachain.rs
+++ b/runtime/common/config/parachain.rs
@@ -38,9 +38,29 @@
 	type ReservedDmpWeight = ReservedDmpWeight;
 	type ReservedXcmpWeight = ReservedXcmpWeight;
 	type XcmpMessageHandler = XcmpQueue;
+	#[cfg(not(feature = "lookahead"))]
 	type CheckAssociatedRelayNumber = cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases;
+	#[cfg(feature = "lookahead")]
+	type CheckAssociatedRelayNumber =
+		cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
 }
 
 impl parachain_info::Config for Runtime {}
 
 impl cumulus_pallet_aura_ext::Config for Runtime {}
+
+/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included
+/// into the relay chain.
+#[cfg(feature = "lookahead")]
+const UNINCLUDED_SEGMENT_CAPACITY: u32 = 3;
+/// How many parachain blocks are processed by the relay chain per parent. Limits the
+/// number of blocks authored per slot.
+#[cfg(feature = "lookahead")]
+const BLOCK_PROCESSING_VELOCITY: u32 = 2;
+#[cfg(feature = "lookahead")]
+pub type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook<
+	Runtime,
+	{ MILLISECS_PER_RELAY_BLOCK as u32 },
+	BLOCK_PROCESSING_VELOCITY,
+	UNINCLUDED_SEGMENT_CAPACITY,
+>;
modifiedruntime/common/runtime_apis.rsdiffbeforeafterboth
--- a/runtime/common/runtime_apis.rs
+++ b/runtime/common/runtime_apis.rs
@@ -679,6 +679,16 @@
 				}
 			}
 
+			#[cfg(feature = "lookahead")]
+			impl cumulus_primitives_aura::AuraUnincludedSegmentApi<Block> for Runtime {
+				fn can_build_upon(
+					included_hash: <Block as BlockT>::Hash,
+					slot: cumulus_primitives_aura::Slot,
+				) -> bool {
+					$crate::config::parachain::ConsensusHook::can_build_upon(included_hash, slot)
+				}
+			}
+
 			/// Should never be used, yet still required because of https://github.com/paritytech/polkadot-sdk/issues/27
 			/// Not allowed to panic, because rpc may be called using native runtime, thus causing thread panic.
 			impl fp_rpc::ConvertTransactionRuntimeApi<Block> for Runtime {
modifiedruntime/opal/Cargo.tomldiffbeforeafterboth
--- a/runtime/opal/Cargo.toml
+++ b/runtime/opal/Cargo.toml
@@ -69,6 +69,7 @@
 	'cumulus-pallet-parachain-system/std',
 	'cumulus-pallet-xcm/std',
 	'cumulus-pallet-xcmp-queue/std',
+	'cumulus-primitives-aura/std',
 	'cumulus-primitives-core/std',
 	'cumulus-primitives-utility/std',
 	'frame-executive/std',
@@ -230,6 +231,7 @@
 preimage = []
 refungible = []
 session-test-timings = []
+lookahead = []
 
 ################################################################################
 # local dependencies
@@ -240,6 +242,7 @@
 cumulus-pallet-parachain-system = { workspace = true }
 cumulus-pallet-xcm = { workspace = true }
 cumulus-pallet-xcmp-queue = { workspace = true }
+cumulus-primitives-aura = { workspace = true }
 cumulus-primitives-core = { workspace = true }
 cumulus-primitives-timestamp = { workspace = true }
 cumulus-primitives-utility = { workspace = true }