Skip to content

Commit

Permalink
introduce collator selection (#293)
Browse files Browse the repository at this point in the history
* add collator selection

* desired candidates = 5

* taplo fmt
  • Loading branch information
brenzi authored Jun 4, 2024
1 parent 7e9a1c2 commit 0d9569c
Show file tree
Hide file tree
Showing 7 changed files with 552 additions and 3 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ assets-common = { version = "0.11.0", default-features = false }
cumulus-pallet-aura-ext = { version = "0.11.0", default-features = false }
cumulus-pallet-dmp-queue = { version = "0.11.0", default-features = false }
cumulus-pallet-parachain-system = { version = "0.11.0", default-features = false }
cumulus-pallet-session-benchmarking = { version = "13.0.0", default-features = false }
cumulus-pallet-xcm = { version = "0.11.0", default-features = false }
cumulus-pallet-xcmp-queue = { version = "0.11.0", default-features = false }
cumulus-primitives-core = { version = "0.11.0", default-features = false }
Expand All @@ -64,12 +65,14 @@ pallet-authorship = { version = "32.0.0", default-features = false }
pallet-balances = { version = "33.0.0", default-features = false }
pallet-bounties = { version = "31.0.0", default-features = false }
pallet-child-bounties = { version = "31.0.0", default-features = false }
pallet-collator-selection = { version = "13.0.1", default-features = false }
pallet-collective = { version = "32.0.0", default-features = false }
pallet-democracy = { version = "32.0.0", default-features = false }
pallet-message-queue = { version = "35.0.0", default-features = false }
pallet-multisig = { version = "32.0.0", default-features = false }
pallet-preimage = { version = "32.0.0", default-features = false }
pallet-proxy = { version = "32.0.0", default-features = false }
pallet-session = { version = "32.0.0", default-features = false }
pallet-scheduler = { version = "33.0.0", default-features = false }
pallet-sudo = { version = "32.0.0", default-features = false }
pallet-timestamp = { version = "31.0.0", default-features = false }
Expand Down
18 changes: 15 additions & 3 deletions polkadot-parachains/integritee-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/integritee-network/parachain"
edition = "2021"

[dependencies]
hex-literal = { workspace = true }
integritee-parachains-common = { path = "../common", default-features = false }
log = { workspace = true }
parity-scale-codec = { workspace = true }
Expand All @@ -22,6 +23,7 @@ assets-common = { workspace = true }
cumulus-pallet-aura-ext = { workspace = true }
cumulus-pallet-dmp-queue = { workspace = true }
cumulus-pallet-parachain-system = { workspace = true, features = ["parameterized-consensus-hook"] }
cumulus-pallet-session-benchmarking = { optional = true, workspace = true }
cumulus-pallet-xcm = { workspace = true }
cumulus-pallet-xcmp-queue = { workspace = true }
cumulus-primitives-core = { workspace = true }
Expand All @@ -40,10 +42,12 @@ pallet-asset-conversion = { workspace = true }
pallet-asset-registry = { workspace = true }
pallet-assets = { workspace = true }
pallet-aura = { workspace = true }
pallet-authorship = { workspace = true }
pallet-balances = { workspace = true }
pallet-bounties = { workspace = true }
pallet-child-bounties = { workspace = true }
pallet-claims = { workspace = true }
pallet-collator-selection = { workspace = true }
pallet-collective = { workspace = true }
pallet-democracy = { workspace = true }
pallet-enclave-bridge = { workspace = true }
Expand All @@ -52,6 +56,7 @@ pallet-multisig = { workspace = true }
pallet-preimage = { workspace = true }
pallet-proxy = { workspace = true }
pallet-scheduler = { workspace = true }
pallet-session = { workspace = true }
pallet-sidechain = { workspace = true }
pallet-teeracle = { workspace = true }
pallet-teerex = { workspace = true }
Expand Down Expand Up @@ -90,12 +95,11 @@ xcm-transactor-primitives = { workspace = true }
# Benchmarking
frame-benchmarking = { workspace = true, optional = true }
frame-system-benchmarking = { workspace = true, optional = true }
hex-literal = { workspace = true, optional = true }

[dev-dependencies]
cumulus-primitives-parachain-inherent = { workspace = true, features = ["std"] }
hex = { workspace = true }
hex-literal = { workspace = true }

polkadot-primitives = { workspace = true, features = ["std"] }
polkadot-runtime-parachains = { workspace = true, features = ["std"] }

Expand All @@ -109,6 +113,7 @@ std = [
"cumulus-pallet-aura-ext/std",
"cumulus-pallet-dmp-queue/std",
"cumulus-pallet-parachain-system/std",
"cumulus-pallet-session-benchmarking?/std",
"cumulus-pallet-xcm/std",
"cumulus-pallet-xcmp-queue/std",
"cumulus-primitives-core/std",
Expand All @@ -131,10 +136,12 @@ std = [
"pallet-asset-conversion/std",
"pallet-asset-registry/std",
"pallet-aura/std",
"pallet-authorship/std",
"pallet-balances/std",
"pallet-bounties/std",
"pallet-child-bounties/std",
"pallet-claims/std",
"pallet-collator-selection/std",
"pallet-collective/std",
"pallet-democracy/std",
"pallet-enclave-bridge/std",
Expand All @@ -143,6 +150,7 @@ std = [
"pallet-preimage/std",
"pallet-proxy/std",
"pallet-scheduler/std",
"pallet-session/std",
"pallet-sidechain/std",
"pallet-teeracle/std",
"pallet-teerex/std",
Expand Down Expand Up @@ -184,13 +192,13 @@ std = [
runtime-benchmarks = [
"assets-common/runtime-benchmarks",
"cumulus-pallet-parachain-system/runtime-benchmarks",
"cumulus-pallet-session-benchmarking/runtime-benchmarks",
"cumulus-pallet-xcmp-queue/runtime-benchmarks",
"cumulus-primitives-utility/runtime-benchmarks",
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system-benchmarking/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"hex-literal",
"orml-xtokens/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-asset-conversion/runtime-benchmarks",
Expand All @@ -199,6 +207,7 @@ runtime-benchmarks = [
"pallet-bounties/runtime-benchmarks",
"pallet-child-bounties/runtime-benchmarks",
"pallet-claims/runtime-benchmarks",
"pallet-collator-selection/runtime-benchmarks",
"pallet-collective/runtime-benchmarks",
"pallet-democracy/runtime-benchmarks",
"pallet-enclave-bridge/runtime-benchmarks",
Expand Down Expand Up @@ -239,10 +248,12 @@ try-runtime = [
"pallet-asset-conversion/try-runtime",
"pallet-asset-registry/try-runtime",
"pallet-aura/try-runtime",
"pallet-authorship/try-runtime",
"pallet-balances/try-runtime",
"pallet-bounties/try-runtime",
"pallet-child-bounties/try-runtime",
"pallet-claims/try-runtime",
"pallet-collator-selection/try-runtime",
"pallet-collective/try-runtime",
"pallet-democracy/try-runtime",
"pallet-enclave-bridge/try-runtime",
Expand All @@ -251,6 +262,7 @@ try-runtime = [
"pallet-preimage/try-runtime",
"pallet-proxy/try-runtime",
"pallet-scheduler/try-runtime",
"pallet-session/try-runtime",
"pallet-sidechain/try-runtime",
"pallet-teeracle/try-runtime",
"pallet-teerex/try-runtime",
Expand Down
155 changes: 155 additions & 0 deletions polkadot-parachains/integritee-runtime/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,158 @@ pub mod scheduler {
}
}
}

pub mod collator_selection_init {
use frame_support::traits::OnRuntimeUpgrade;
#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;

/// The log target.
const TARGET: &str = "runtime::fix::collator_selection_init";
pub mod v0 {
use super::*;
use crate::{SessionKeys, TEER};
use frame_support::{pallet_prelude::*, traits::Currency};
use hex_literal::hex;
use log::info;
use parachains_common::impls::BalanceOf;
use parity_scale_codec::EncodeLike;
use sp_core::{crypto::key_types::AURA, sr25519};
use sp_std::{vec, vec::Vec};

const INVULNERABLE_AURA_A: [u8; 32] =
hex!("c6c1370a5b6656f3816b2a6c32444ec18d5ac6d33103c4e5c3f359623a19dc47");
const INVULNERABLE_AURA_B: [u8; 32] =
hex!("183490bfadaacbb875537599dfc936cb0159eadf9a4cc8a16a584f170b503509");
const INVULNERABLE_AURA_C: [u8; 32] =
hex!("a8302634ae0c688c7c3447e4e683279ec9384c1758ec78a7a2def44064cf046c");
const INVULNERABLE_AURA_D: [u8; 32] =
hex!("e2750081920a4a704fae7f04069df3018dd259ca2c6af51a9764df226072f75a");
const INVULNERABLE_AURA_E: [u8; 32] =
hex!("ba492297546f3c34602551582069b03c0e13000d5de928e9b5db9d18bbd2e435");

const INVULNERABLE_ACCOUNT_A: [u8; 32] =
hex!("8e9f7d54e1d9bdbac609f444c9e920a87af41216ee6c9ba0c62032fb1ade0464");
const INVULNERABLE_ACCOUNT_B: [u8; 32] =
hex!("b0a7c84862ec760d8817ab01482e955332c9abf11e4568991def6837c1e1ac7b");
const INVULNERABLE_ACCOUNT_C: [u8; 32] =
hex!("1a65141f43b54ed8c0f76201ef374a607bd74484741243212a2accd76e315c09");
const INVULNERABLE_ACCOUNT_D: [u8; 32] =
hex!("645553b30ea9a250dd0f38f472fdeb04e418b8204b96a2bd30ae151ef660053e");
const INVULNERABLE_ACCOUNT_E: [u8; 32] =
hex!("e089d5404fb036a8ac795377213445f9ccebfc6773a183769503974369db7f46");

pub struct InitInvulnerables<T>(sp_std::marker::PhantomData<T>);
impl<T> OnRuntimeUpgrade for InitInvulnerables<T>
where
T: frame_system::Config
+ pallet_collator_selection::Config
+ pallet_session::Config
+ pallet_balances::Config,
<T as frame_system::Config>::AccountId: From<[u8; 32]>,
<T as pallet_session::Config>::ValidatorId: From<[u8; 32]>,
<T as pallet_session::Config>::Keys: From<SessionKeys>,
<T as pallet_balances::Config>::Balance: From<u128>,
<T as pallet_balances::Config>::Balance: EncodeLike<
<<T as pallet_collator_selection::Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::Balance,
>,
{
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<sp_std::vec::Vec<u8>, TryRuntimeError> {
let invulnerables_len = pallet_collator_selection::Invulnerables::<T>::get().len();
Ok((invulnerables_len as u32).encode())
}

fn on_runtime_upgrade() -> Weight {
let invulnerables_len = pallet_collator_selection::Invulnerables::<T>::get().len();
if invulnerables_len > 0 {
info!(target: TARGET, "no need to initialize invulnerables");
return T::DbWeight::get().reads_writes(1, 0)
}
info!(target: TARGET, "initializing the set of invulnerables");

let raw_aura_keys: Vec<[u8; 32]> = vec![
INVULNERABLE_AURA_A,
INVULNERABLE_AURA_B,
INVULNERABLE_AURA_C,
INVULNERABLE_AURA_D,
INVULNERABLE_AURA_E,
];
let raw_account_keys: Vec<[u8; 32]> = vec![
INVULNERABLE_ACCOUNT_A,
INVULNERABLE_ACCOUNT_B,
INVULNERABLE_ACCOUNT_C,
INVULNERABLE_ACCOUNT_D,
INVULNERABLE_ACCOUNT_E,
];

let validatorids: Vec<<T as pallet_session::Config>::ValidatorId> =
raw_account_keys.iter().map(|&pk| pk.into()).collect();

pallet_session::Validators::<T>::put(validatorids);

let queued_keys: Vec<(
<T as pallet_session::Config>::ValidatorId,
<T as pallet_session::Config>::Keys,
)> = raw_account_keys
.iter()
.zip(raw_aura_keys.iter())
.map(|(&account, &aura)| {
(
account.into(),
SessionKeys { aura: sr25519::Public::from_raw(aura).into() }.into(),
)
})
.collect();

pallet_session::QueuedKeys::<T>::put(queued_keys);

for (&account, &aura) in raw_account_keys.iter().zip(raw_aura_keys.iter()) {
pallet_session::NextKeys::<T>::insert::<
<T as pallet_session::Config>::ValidatorId,
<T as pallet_session::Config>::Keys,
>(
account.into(),
SessionKeys { aura: sr25519::Public::from_raw(aura).into() }.into(),
);
pallet_session::KeyOwner::<T>::insert::<
_,
<T as pallet_session::Config>::ValidatorId,
>((AURA, aura.encode()), account.into());
}

let mut invulnerables: Vec<<T as frame_system::Config>::AccountId> =
raw_account_keys.iter().map(|&pk| pk.into()).collect();
invulnerables.sort();
let invulnerables: BoundedVec<_, T::MaxInvulnerables> =
invulnerables.try_into().unwrap();
pallet_collator_selection::Invulnerables::<T>::put(invulnerables);

pallet_collator_selection::CandidacyBond::<T>::put::<BalanceOf<T>>(
(500 * TEER).into(),
);

pallet_collator_selection::DesiredCandidates::<T>::put::<u32>(5);

T::DbWeight::get().reads_writes(0, 5 + 5 * 2)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: sp_std::vec::Vec<u8>) -> Result<(), TryRuntimeError> {
let invulnerables_len =
pallet_collator_selection::Invulnerables::<T>::get().len() as u32;
let apriori_invulnerables_len: u32 = Decode::decode(&mut state.as_slice()).expect(
"the state parameter should be something that was generated by pre_upgrade",
);
ensure!(
invulnerables_len > 0,
"invulnerables are empty after initialization. that should not happen"
);
info!(target: TARGET, "apriori invulnerables: {}, aposteriori: {}", apriori_invulnerables_len, invulnerables_len);
Ok(())
}
}
}
}
2 changes: 2 additions & 0 deletions polkadot-parachains/integritee-runtime/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod pallet_balances;
pub mod pallet_bounties;
pub mod pallet_child_bounties;
pub mod pallet_claims;
pub mod pallet_collator_selection;
pub mod pallet_collective;
pub mod pallet_democracy;
pub mod pallet_enclave_bridge;
Expand All @@ -20,6 +21,7 @@ pub mod pallet_multisig;
pub mod pallet_preimage;
pub mod pallet_proxy;
pub mod pallet_scheduler;
pub mod pallet_session;
pub mod pallet_sidechain;
pub mod pallet_teeracle;
pub mod pallet_teerex;
Expand Down
Loading

0 comments on commit 0d9569c

Please sign in to comment.