Skip to content

Commit

Permalink
Merge branch 'master' into pg/add-tracing-support
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou committed Jan 16, 2025
2 parents b964051 + be2404c commit 58c0c7a
Show file tree
Hide file tree
Showing 158 changed files with 6,924 additions and 1,719 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks-quick.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ jobs:
# Fixes "detected dubious ownership" error in the ci
git config --global --add safe.directory '*'
python3 scripts/generate-umbrella.py --sdk . --version 0.1.0
cargo +nightly fmt --all
cargo +nightly fmt -p polkadot-sdk
if [ -n "$(git status --porcelain)" ]; then
cat <<EOF
Expand Down
36 changes: 34 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ members = [
"substrate/frame/asset-conversion",
"substrate/frame/asset-conversion/ops",
"substrate/frame/asset-rate",
"substrate/frame/asset-rewards",
"substrate/frame/assets",
"substrate/frame/assets-freezer",
"substrate/frame/atomic-swap",
Expand Down Expand Up @@ -850,7 +851,7 @@ linked-hash-map = { version = "0.5.4" }
linked_hash_set = { version = "0.1.4" }
linregress = { version = "0.5.1" }
lite-json = { version = "0.2.0", default-features = false }
litep2p = { version = "0.8.4", features = ["websocket"] }
litep2p = { version = "0.9.0", features = ["websocket"] }
log = { version = "0.4.22", default-features = false }
macro_magic = { version = "0.5.1" }
maplit = { version = "1.0.2" }
Expand Down Expand Up @@ -893,6 +894,7 @@ pallet-asset-conversion = { path = "substrate/frame/asset-conversion", default-f
pallet-asset-conversion-ops = { path = "substrate/frame/asset-conversion/ops", default-features = false }
pallet-asset-conversion-tx-payment = { path = "substrate/frame/transaction-payment/asset-conversion-tx-payment", default-features = false }
pallet-asset-rate = { path = "substrate/frame/asset-rate", default-features = false }
pallet-asset-rewards = { path = "substrate/frame/asset-rewards", default-features = false }
pallet-asset-tx-payment = { path = "substrate/frame/transaction-payment/asset-tx-payment", default-features = false }
pallet-assets = { path = "substrate/frame/assets", default-features = false }
pallet-assets-freezer = { path = "substrate/frame/assets-freezer", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions cumulus/client/consensus/aura/src/collators/lookahead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ where
);
Some(super::can_build_upon::<_, _, P>(
slot_now,
relay_slot,
timestamp,
block_hash,
included_block,
Expand Down
30 changes: 17 additions & 13 deletions cumulus/client/consensus/aura/src/collators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use polkadot_primitives::{
ValidationCodeHash,
};
use sc_consensus_aura::{standalone as aura_internal, AuraApi};
use sp_api::ProvideRuntimeApi;
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_core::Pair;
use sp_keystore::KeystorePtr;
use sp_timestamp::Timestamp;
Expand Down Expand Up @@ -160,7 +160,8 @@ async fn cores_scheduled_for_para(
// Checks if we own the slot at the given block and whether there
// is space in the unincluded segment.
async fn can_build_upon<Block: BlockT, Client, P>(
slot: Slot,
para_slot: Slot,
relay_slot: Slot,
timestamp: Timestamp,
parent_hash: Block::Hash,
included_block: Block::Hash,
Expand All @@ -169,25 +170,28 @@ async fn can_build_upon<Block: BlockT, Client, P>(
) -> Option<SlotClaim<P::Public>>
where
Client: ProvideRuntimeApi<Block>,
Client::Api: AuraApi<Block, P::Public> + AuraUnincludedSegmentApi<Block>,
Client::Api: AuraApi<Block, P::Public> + AuraUnincludedSegmentApi<Block> + ApiExt<Block>,
P: Pair,
P::Public: Codec,
P::Signature: Codec,
{
let runtime_api = client.runtime_api();
let authorities = runtime_api.authorities(parent_hash).ok()?;
let author_pub = aura_internal::claim_slot::<P>(slot, &authorities, keystore).await?;
let author_pub = aura_internal::claim_slot::<P>(para_slot, &authorities, keystore).await?;

// Here we lean on the property that building on an empty unincluded segment must always
// be legal. Skipping the runtime API query here allows us to seamlessly run this
// collator against chains which have not yet upgraded their runtime.
if parent_hash != included_block &&
!runtime_api.can_build_upon(parent_hash, included_block, slot).ok()?
{
return None
}
let Ok(Some(api_version)) =
runtime_api.api_version::<dyn AuraUnincludedSegmentApi<Block>>(parent_hash)
else {
return (parent_hash == included_block)
.then(|| SlotClaim::unchecked::<P>(author_pub, para_slot, timestamp));
};

let slot = if api_version > 1 { relay_slot } else { para_slot };

Some(SlotClaim::unchecked::<P>(author_pub, slot, timestamp))
runtime_api
.can_build_upon(parent_hash, included_block, slot)
.ok()?
.then(|| SlotClaim::unchecked::<P>(author_pub, para_slot, timestamp))
}

/// Use [`cumulus_client_consensus_common::find_potential_parents`] to find parachain blocks that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use cumulus_primitives_aura::AuraUnincludedSegmentApi;
use cumulus_primitives_core::{GetCoreSelectorApi, PersistedValidationData};
use cumulus_relay_chain_interface::RelayChainInterface;

use polkadot_primitives::Id as ParaId;
use polkadot_primitives::{Block as RelayBlock, Id as ParaId};

use futures::prelude::*;
use sc_client_api::{backend::AuxStore, BlockBackend, BlockOf, UsageProvider};
Expand Down Expand Up @@ -302,8 +302,17 @@ where
// on-chain data.
collator.collator_service().check_block_status(parent_hash, &parent_header);

let Ok(relay_slot) =
sc_consensus_babe::find_pre_digest::<RelayBlock>(relay_parent_header)
.map(|babe_pre_digest| babe_pre_digest.slot())
else {
tracing::error!(target: crate::LOG_TARGET, "Relay chain does not contain babe slot. This should never happen.");
continue;
};

let slot_claim = match crate::collators::can_build_upon::<_, _, P>(
para_slot.slot,
relay_slot,
para_slot.timestamp,
parent_hash,
included_block,
Expand Down
11 changes: 4 additions & 7 deletions cumulus/client/parachain-inherent/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
use crate::{ParachainInherentData, INHERENT_IDENTIFIER};
use codec::Decode;
use cumulus_primitives_core::{
relay_chain, relay_chain::UpgradeGoAhead, InboundDownwardMessage, InboundHrmpMessage, ParaId,
PersistedValidationData,
relay_chain,
relay_chain::{Slot, UpgradeGoAhead},
InboundDownwardMessage, InboundHrmpMessage, ParaId, PersistedValidationData,
};
use cumulus_primitives_parachain_inherent::MessageQueueChain;
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
Expand All @@ -28,9 +29,6 @@ use sp_inherents::{InherentData, InherentDataProvider};
use sp_runtime::traits::Block;
use std::collections::BTreeMap;

/// Relay chain slot duration, in milliseconds.
pub const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000;

/// Inherent data provider that supplies mocked validation data.
///
/// This is useful when running a node that is not actually backed by any relay chain.
Expand Down Expand Up @@ -175,8 +173,7 @@ impl<R: Send + Sync + GenerateRandomness<u64>> InherentDataProvider
// Calculate the mocked relay block based on the current para block
let relay_parent_number =
self.relay_offset + self.relay_blocks_per_para_block * self.current_para_block;
sproof_builder.current_slot =
((relay_parent_number / RELAY_CHAIN_SLOT_DURATION_MILLIS) as u64).into();
sproof_builder.current_slot = Slot::from(relay_parent_number as u64);

sproof_builder.upgrade_go_ahead = self.upgrade_go_ahead;
// Process the downward messages and set up the correct head
Expand Down
8 changes: 7 additions & 1 deletion cumulus/pallets/aura-ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ sp-runtime = { workspace = true }
cumulus-pallet-parachain-system = { workspace = true }

[dev-dependencies]

# Cumulus
cumulus-pallet-parachain-system = { workspace = true, default-features = true }
cumulus-primitives-core = { workspace = true, default-features = true }
cumulus-test-relay-sproof-builder = { workspace = true, default-features = true }

# Substrate
sp-core = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }
sp-version = { workspace = true, default-features = true }

[features]
default = ["std"]
Expand Down
42 changes: 25 additions & 17 deletions cumulus/pallets/aura-ext/src/consensus_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
//! block velocity.
//!
//! The velocity `V` refers to the rate of block processing by the relay chain.
use super::{pallet, Aura};
use core::{marker::PhantomData, num::NonZeroU32};
use cumulus_pallet_parachain_system::{
Expand Down Expand Up @@ -54,8 +53,23 @@ where
let velocity = V.max(1);
let relay_chain_slot = state_proof.read_slot().expect("failed to read relay chain slot");

let (slot, authored) =
pallet::SlotInfo::<T>::get().expect("slot info is inserted on block initialization");
let (relay_chain_slot, authored_in_relay) = match pallet::RelaySlotInfo::<T>::get() {
Some((slot, authored)) if slot == relay_chain_slot => (slot, authored),
Some((slot, _)) if slot < relay_chain_slot => (relay_chain_slot, 0),
Some((slot, _)) => {
panic!("Slot moved backwards: stored_slot={slot:?}, relay_chain_slot={relay_chain_slot:?}")
},
None => (relay_chain_slot, 0),
};

// We need to allow one additional block to be built to fill the unincluded segment.
if authored_in_relay > velocity {
panic!("authored blocks limit is reached for the slot: relay_chain_slot={relay_chain_slot:?}, authored={authored_in_relay:?}, velocity={velocity:?}");
}

pallet::RelaySlotInfo::<T>::put((relay_chain_slot, authored_in_relay + 1));

let para_slot = pallet_aura::CurrentSlot::<T>::get();

// Convert relay chain timestamp.
let relay_chain_timestamp =
Expand All @@ -67,19 +81,16 @@ where

// Check that we are not too far in the future. Since we expect `V` parachain blocks
// during the relay chain slot, we can allow for `V` parachain slots into the future.
if *slot > *para_slot_from_relay + u64::from(velocity) {
if *para_slot > *para_slot_from_relay + u64::from(velocity) {
panic!(
"Parachain slot is too far in the future: parachain_slot: {:?}, derived_from_relay_slot: {:?} velocity: {:?}",
slot,
"Parachain slot is too far in the future: parachain_slot={:?}, derived_from_relay_slot={:?} velocity={:?}, relay_chain_slot={:?}",
para_slot,
para_slot_from_relay,
velocity
velocity,
relay_chain_slot
);
}

// We need to allow authoring multiple blocks in the same slot.
if slot != para_slot_from_relay && authored > velocity {
panic!("authored blocks limit is reached for the slot")
}
let weight = T::DbWeight::get().reads(1);

(
Expand Down Expand Up @@ -110,7 +121,7 @@ impl<
/// is more recent than the included block itself.
pub fn can_build_upon(included_hash: T::Hash, new_slot: Slot) -> bool {
let velocity = V.max(1);
let (last_slot, authored_so_far) = match pallet::SlotInfo::<T>::get() {
let (last_slot, authored_so_far) = match pallet::RelaySlotInfo::<T>::get() {
None => return true,
Some(x) => x,
};
Expand All @@ -123,11 +134,8 @@ impl<
return false
}

// TODO: This logic needs to be adjusted.
// It checks that we have not authored more than `V + 1` blocks in the slot.
// As a slot however, we take the parachain slot here. Velocity should
// be measured in relation to the relay chain slot.
// https://github.com/paritytech/polkadot-sdk/issues/3967
// Check that we have not authored more than `V + 1` parachain blocks in the current relay
// chain slot.
if last_slot == new_slot {
authored_so_far < velocity + 1
} else {
Expand Down
Loading

0 comments on commit 58c0c7a

Please sign in to comment.