Skip to content

Commit

Permalink
Merge branch 'master' into fix-for-deny-then-try
Browse files Browse the repository at this point in the history
  • Loading branch information
acatangiu authored Jan 27, 2025
2 parents 54dadec + db3ff60 commit 90153db
Show file tree
Hide file tree
Showing 19 changed files with 420 additions and 120 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/misc-sync-templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,66 @@ on:
default: ""

jobs:
prepare-chain-spec-artifacts:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- template: minimal
package_name: 'minimal-template-runtime'
runtime_path: './templates/minimal/runtime'
runtime_wasm_path: minimal-template-runtime/minimal_template_runtime.compact.compressed.wasm
relay_chain: 'dev'
- template: parachain
package_name: 'parachain-template-runtime'
runtime_path: './templates/parachain/runtime'
runtime_wasm_path: parachain-template-runtime/parachain_template_runtime.compact.compressed.wasm
relay_chain: 'rococo-local'
steps:
- uses: actions/checkout@v4
with:
ref: "${{ github.event.inputs.stable_release_branch }}"

- name: Setup build environment
run: |
sudo apt-get update
sudo apt-get install -y protobuf-compiler
cargo install --git https://github.com/chevdor/srtool-cli --locked
cargo install --path substrate/bin/utils/chain-spec-builder --locked
srtool pull
- name: Build runtime and generate chain spec
run: |
# Prepare directories
sudo mkdir -p ${{ matrix.template.runtime_path }}/target
sudo chmod -R 777 ${{ matrix.template.runtime_path }}/target
# Build runtime
srtool build --package ${{ matrix.template.package_name }} --runtime-dir ${{ matrix.template.runtime_path }} --root
# Generate chain spec
# Note that para-id is set to 1000 for both minimal/parachain templates.
# `parachain-runtime` is hardcoded to use this parachain id.
# `minimal` template isn't using it, but when started with Omni Node, this para id is required (any number can do it, so setting it to 1000 for convenience).
chain-spec-builder -c dev_chain_spec.json create \
--relay-chain "${{ matrix.template.relay_chain }}" \
--para-id 1000 \
--runtime "${{ matrix.template.runtime_path }}/target/srtool/release/wbuild/${{ matrix.template.runtime_wasm_path }}" \
named-preset development
- name: Prepare upload directory
run: |
mkdir -p artifacts-${{ matrix.template }}
cp dev_chain_spec.json artifacts-${{ matrix.template }}/dev_chain_spec.json
- name: Upload template directory
uses: actions/upload-artifact@v4
with:
name: artifacts-${{ matrix.template }}
path: artifacts-${{ matrix.template }}/dev_chain_spec.json

sync-templates:
needs: prepare-chain-spec-artifacts
runs-on: ubuntu-latest
environment: master
strategy:
Expand All @@ -52,6 +111,12 @@ jobs:
with:
path: polkadot-sdk
ref: "${{ github.event.inputs.stable_release_branch }}"
- name: Download template artifacts
uses: actions/download-artifact@v4
with:
name: artifacts-${{ matrix.template }}
path: templates/${{ matrix.template }}/
if: matrix.template != 'solochain'
- name: Generate a token for the template repository
id: app_token
uses: actions/create-github-app-token@v1.9.3
Expand Down
121 changes: 76 additions & 45 deletions polkadot/runtime/common/src/slots/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub mod pallet {
if let Some((lease_period, first_block)) = Self::lease_period_index(n) {
// If we're beginning a new lease period then handle that.
if first_block {
return Self::manage_lease_period_start(lease_period)
return Self::manage_lease_period_start(lease_period);
}
}

Expand Down Expand Up @@ -237,7 +237,7 @@ impl<T: Config> Pallet<T> {
let mut parachains = Vec::new();
for (para, mut lease_periods) in Leases::<T>::iter() {
if lease_periods.is_empty() {
continue
continue;
}
// ^^ should never be empty since we would have deleted the entry otherwise.

Expand Down Expand Up @@ -381,7 +381,7 @@ impl<T: Config> Leaser<BlockNumberFor<T>> for Pallet<T> {
// attempt.
//
// We bail, not giving any lease and leave it for governance to sort out.
return Err(LeaseError::AlreadyLeased)
return Err(LeaseError::AlreadyLeased);
}
} else if d.len() == i {
// Doesn't exist. This is usual.
Expand Down Expand Up @@ -488,7 +488,7 @@ impl<T: Config> Leaser<BlockNumberFor<T>> for Pallet<T> {
for slot in offset..=offset + period_count {
if let Some(Some(_)) = leases.get(slot) {
// If there exists any lease period, we exit early and return true.
return true
return true;
}
}

Expand Down Expand Up @@ -962,7 +962,7 @@ mod benchmarking {
use polkadot_runtime_parachains::paras;
use sp_runtime::traits::{Bounded, One};

use frame_benchmarking::{account, benchmarks, whitelisted_caller, BenchmarkError};
use frame_benchmarking::v2::*;

use crate::slots::Pallet as Slots;

Expand Down Expand Up @@ -998,10 +998,15 @@ mod benchmarking {
(para, leaser)
}

benchmarks! {
where_clause { where T: paras::Config }
#[benchmarks(
where T: paras::Config,
)]

force_lease {
mod benchmarks {
use super::*;

#[benchmark]
fn force_lease() -> Result<(), BenchmarkError> {
// If there is an offset, we need to be on that block to be able to do lease things.
frame_system::Pallet::<T>::set_block_number(T::LeaseOffset::get() + One::one());
let para = ParaId::from(1337);
Expand All @@ -1012,33 +1017,40 @@ mod benchmarking {
let period_count = 3u32.into();
let origin =
T::ForceOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
}: _<T::RuntimeOrigin>(origin, para, leaser.clone(), amount, period_begin, period_count)
verify {
assert_last_event::<T>(Event::<T>::Leased {
para_id: para,
leaser, period_begin,
period_count,
extra_reserved: amount,
total_amount: amount,
}.into());
}

// Worst case scenario, T on-demand parachains onboard, and C lease holding parachains offboard.
manage_lease_period_start {
// Assume reasonable maximum of 100 paras at any time
let c in 0 .. 100;
let t in 0 .. 100;
#[extrinsic_call]
_(origin as T::RuntimeOrigin, para, leaser.clone(), amount, period_begin, period_count);

assert_last_event::<T>(
Event::<T>::Leased {
para_id: para,
leaser,
period_begin,
period_count,
extra_reserved: amount,
total_amount: amount,
}
.into(),
);

Ok(())
}

// Worst case scenario, T on-demand parachains onboard, and C lease holding parachains
// offboard. Assume reasonable maximum of 100 paras at any time
#[benchmark]
fn manage_lease_period_start(
c: Linear<0, 100>,
t: Linear<0, 100>,
) -> Result<(), BenchmarkError> {
let period_begin = 1u32.into();
let period_count = 4u32.into();

// If there is an offset, we need to be on that block to be able to do lease things.
frame_system::Pallet::<T>::set_block_number(T::LeaseOffset::get() + One::one());

// Make T parathreads (on-demand parachains)
let paras_info = (0..t).map(|i| {
register_a_parathread::<T>(i)
}).collect::<Vec<_>>();
let paras_info = (0..t).map(|i| register_a_parathread::<T>(i)).collect::<Vec<_>>();

T::Registrar::execute_pending_transitions();

Expand All @@ -1053,43 +1065,48 @@ mod benchmarking {
T::Registrar::execute_pending_transitions();

// C lease holding parachains are downgrading to on-demand parachains
for i in 200 .. 200 + c {
let (para, leaser) = register_a_parathread::<T>(i);
for i in 200..200 + c {
let (para, _) = register_a_parathread::<T>(i);
T::Registrar::make_parachain(para)?;
}

T::Registrar::execute_pending_transitions();

for i in 0 .. t {
for i in 0..t {
assert!(T::Registrar::is_parathread(ParaId::from(i)));
}

for i in 200 .. 200 + c {
for i in 200..200 + c {
assert!(T::Registrar::is_parachain(ParaId::from(i)));
}
}: {
Slots::<T>::manage_lease_period_start(period_begin);
} verify {
#[block]
{
let _ = Slots::<T>::manage_lease_period_start(period_begin);
}

// All paras should have switched.
T::Registrar::execute_pending_transitions();
for i in 0 .. t {
for i in 0..t {
assert!(T::Registrar::is_parachain(ParaId::from(i)));
}
for i in 200 .. 200 + c {
for i in 200..200 + c {
assert!(T::Registrar::is_parathread(ParaId::from(i)));
}

Ok(())
}

// Assume that at most 8 people have deposits for leases on a parachain.
// This would cover at least 4 years of leases in the worst case scenario.
clear_all_leases {
#[benchmark]
fn clear_all_leases() -> Result<(), BenchmarkError> {
let max_people = 8;
let (para, _) = register_a_parathread::<T>(1);

// If there is an offset, we need to be on that block to be able to do lease things.
frame_system::Pallet::<T>::set_block_number(T::LeaseOffset::get() + One::one());

for i in 0 .. max_people {
for i in 0..max_people {
let leaser = account("lease_deposit", i, 0);
let amount = T::Currency::minimum_balance();
T::Currency::make_free_balance_be(&leaser, BalanceOf::<T>::max_value());
Expand All @@ -1102,31 +1119,45 @@ mod benchmarking {
Slots::<T>::force_lease(origin, para, leaser, amount, period_begin, period_count)?;
}

for i in 0 .. max_people {
for i in 0..max_people {
let leaser = account("lease_deposit", i, 0);
assert_eq!(T::Currency::reserved_balance(&leaser), T::Currency::minimum_balance());
}

let origin =
T::ForceOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
}: _<T::RuntimeOrigin>(origin, para)
verify {
for i in 0 .. max_people {

#[extrinsic_call]
_(origin as T::RuntimeOrigin, para);

for i in 0..max_people {
let leaser = account("lease_deposit", i, 0);
assert_eq!(T::Currency::reserved_balance(&leaser), 0u32.into());
}

Ok(())
}

trigger_onboard {
#[benchmark]
fn trigger_onboard() -> Result<(), BenchmarkError> {
// get a parachain into a bad state where they did not onboard
let (para, _) = register_a_parathread::<T>(1);
Leases::<T>::insert(para, vec![Some((account::<T::AccountId>("lease_insert", 0, 0), BalanceOf::<T>::default()))]);
Leases::<T>::insert(
para,
vec![Some((
account::<T::AccountId>("lease_insert", 0, 0),
BalanceOf::<T>::default(),
))],
);
assert!(T::Registrar::is_parathread(para));
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), para)
verify {

#[extrinsic_call]
_(RawOrigin::Signed(caller), para);

T::Registrar::execute_pending_transitions();
assert!(T::Registrar::is_parachain(para));
Ok(())
}

impl_benchmark_test_suite!(
Expand Down
9 changes: 9 additions & 0 deletions prdoc/pr_7042.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
title: `networking::TransactionPool` should accept `Arc`
doc:
- audience: Node Dev
description: The `sc_network_transactions::config::TransactionPool` trait now returns an `Arc` for transactions.
crates:
- name: sc-network-transactions
bump: minor
- name: sc-service
bump: minor
14 changes: 14 additions & 0 deletions prdoc/pr_7344.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
title: '[sync] Let new subscribers know about already connected peers (backward-compatible)'
doc:
- audience: Node Dev
description: Revert https://github.com/paritytech/polkadot-sdk/pull/7011 and replace
it with a backward-compatible solution suitable for backporting to a release branch.
crates:
- name: sc-network-gossip
bump: patch
- name: sc-network-statement
bump: patch
- name: sc-network-sync
bump: patch
- name: sc-network-transactions
bump: patch
10 changes: 4 additions & 6 deletions substrate/client/network-gossip/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,10 @@ impl<B: BlockT> Future for GossipEngine<B> {

match sync_event_stream {
Poll::Ready(Some(event)) => match event {
SyncEvent::InitialPeers(peer_ids) =>
this.network.add_set_reserved(peer_ids, this.protocol.clone()),
SyncEvent::PeerConnected(peer_id) =>
this.network.add_set_reserved(vec![peer_id], this.protocol.clone()),
SyncEvent::PeerDisconnected(peer_id) =>
this.network.remove_set_reserved(peer_id, this.protocol.clone()),
SyncEvent::PeerConnected(remote) =>
this.network.add_set_reserved(remote, this.protocol.clone()),
SyncEvent::PeerDisconnected(remote) =>
this.network.remove_set_reserved(remote, this.protocol.clone()),
},
// The sync event stream closed. Do the same for [`GossipValidator`].
Poll::Ready(None) => {
Expand Down
13 changes: 5 additions & 8 deletions substrate/client/network-gossip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,15 @@ mod validator;

/// Abstraction over a network.
pub trait Network<B: BlockT>: NetworkPeers + NetworkEventStream {
fn add_set_reserved(&self, peer_ids: Vec<PeerId>, protocol: ProtocolName) {
let addrs = peer_ids
.into_iter()
.map(|peer_id| Multiaddr::empty().with(Protocol::P2p(peer_id.into())))
.collect();
let result = self.add_peers_to_reserved_set(protocol, addrs);
fn add_set_reserved(&self, who: PeerId, protocol: ProtocolName) {
let addr = Multiaddr::empty().with(Protocol::P2p(*who.as_ref()));
let result = self.add_peers_to_reserved_set(protocol, iter::once(addr).collect());
if let Err(err) = result {
log::error!(target: "gossip", "add_set_reserved failed: {}", err);
}
}
fn remove_set_reserved(&self, peer_id: PeerId, protocol: ProtocolName) {
let result = self.remove_peers_from_reserved_set(protocol, iter::once(peer_id).collect());
fn remove_set_reserved(&self, who: PeerId, protocol: ProtocolName) {
let result = self.remove_peers_from_reserved_set(protocol, iter::once(who).collect());
if let Err(err) = result {
log::error!(target: "gossip", "remove_set_reserved failed: {}", err);
}
Expand Down
Loading

0 comments on commit 90153db

Please sign in to comment.