Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/jeremy-pooled-staking' into gira…
Browse files Browse the repository at this point in the history
…zoki-bring-benchmarks-to-pallet-staking
  • Loading branch information
girazoki committed Sep 1, 2023
2 parents 10f2a0f + 1f07ac4 commit 50fab7b
Show file tree
Hide file tree
Showing 26 changed files with 409 additions and 300 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

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

11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ sc-tracing = { git = "https://github.com/moondance-labs/substrate", branch = "ta
sc-transaction-pool = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43" }
sc-transaction-pool-api = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43" }
sp-blockchain = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43" }
sp-externalities = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43", default-features = false }
sp-keystore = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43", default-features = false }
sp-storage = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43", default-features = false }
sp-timestamp = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43", default-features = false }
sp-externalities = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43", default-features = false}
sp-keystore = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43", default-features = false}
sp-staking = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43", default-features = false}
sp-storage = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43", default-features = false}
sp-timestamp = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43", default-features = false}
substrate-build-script-utils = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43" }
substrate-frame-rpc-system = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43" }
substrate-prometheus-endpoint = { git = "https://github.com/moondance-labs/substrate", branch = "tanssi-polkadot-v0.9.43" }
Expand Down Expand Up @@ -168,7 +169,6 @@ cumulus-pallet-xcmp-queue = { git = "https://github.com/moondance-labs/cumulus",
cumulus-primitives-core = { git = "https://github.com/moondance-labs/cumulus", branch = "tanssi-polkadot-v0.9.43", default-features = false }
cumulus-primitives-timestamp = { git = "https://github.com/moondance-labs/cumulus", branch = "tanssi-polkadot-v0.9.43", default-features = false }
cumulus-primitives-utility = { git = "https://github.com/moondance-labs/cumulus", branch = "tanssi-polkadot-v0.9.43", default-features = false }
pallet-collator-selection = { git = "https://github.com/moondance-labs/cumulus", branch = "tanssi-polkadot-v0.9.43", default-features = false }
parachain-info = { git = "https://github.com/moondance-labs/cumulus", branch = "tanssi-polkadot-v0.9.43", default-features = false }

# Cumulus (client)
Expand Down Expand Up @@ -227,6 +227,7 @@ hex = { version = "0.4.3", default-features = false }
jsonrpsee = { version = "0.16.2", features = [ "server" ] }
num-traits = "0.2.8"
parking_lot = "0.12.1"
rand = { version = "0.8.5", features = ["std_rng"], default-features = false }
serde_json = { version = "1.0.96", default-features = false }
similar-asserts = "1.1.0"
tempfile = "3.1.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ From substrate:
- _Timestamp_: On-Chain notion of time
- _Transaction Payment_: Transaction payment (fee) management
- _Authorship_: A pallet where authorship information for orchestrator is stored
- _CollatorSelection_: A pallet that selects collators to be assigned to author in container-chains and orchestrator
- _Invulnerables_: A pallet that selects invulnerable collators to be assigned to author in container-chains and orchestrator
- _Session_: A pallet that handles session-changes and keys
- _AuthorityMapping_: A pallet that handles a mapping between collator accounts and authority keys

Expand Down
7 changes: 1 addition & 6 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use {
cumulus_primitives_core::ParaId,
dancebox_runtime::{
AccountId, MaintenanceModeConfig, MigrationsConfig, PolkadotXcmConfig, RegistrarConfig,
Signature, SudoConfig, EXISTENTIAL_DEPOSIT,
Signature, SudoConfig,
},
nimbus_primitives::NimbusId,
pallet_configuration::HostConfiguration,
Expand Down Expand Up @@ -302,11 +302,6 @@ fn testnet_genesis(
.collect(),
},
parachain_info: dancebox_runtime::ParachainInfoConfig { parachain_id: id },
collator_selection: dancebox_runtime::CollatorSelectionConfig {
invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect(),
candidacy_bond: EXISTENTIAL_DEPOSIT * 16,
..Default::default()
},
invulnerables: dancebox_runtime::InvulnerablesConfig {
invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect(),
},
Expand Down
9 changes: 9 additions & 0 deletions pallets/invulnerables/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ targets = [ "x86_64-unknown-linux-gnu" ]
[dependencies]
log = { workspace = true }
parity-scale-codec = { workspace = true }
rand = { workspace = true, optional = true }
scale-info = { workspace = true, features = [ "derive" ] }

frame-support = { workspace = true }
frame-system = { workspace = true }
sp-runtime = { workspace = true }
sp-staking = { workspace = true }
sp-std = { workspace = true }

frame-benchmarking = { workspace = true }

pallet-session = { workspace = true }
pallet-balances = { workspace = true, optional = true }

[dev-dependencies]
sp-core = { workspace = true }
sp-io = { workspace = true }
Expand All @@ -32,6 +37,8 @@ std = [
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-balances/std",
"pallet-session/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-runtime/std",
Expand All @@ -41,6 +48,8 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"rand",
]

try-runtime = [ "frame-support/try-runtime" ]
88 changes: 76 additions & 12 deletions pallets/invulnerables/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
use super::*;

#[allow(unused)]
use crate::Pallet as CollatorSelection;
use crate::Pallet as InvulnerablesPallet;
use {
frame_benchmarking::{account, impl_benchmark_test_suite, v2::*, BenchmarkError},
frame_support::traits::{EnsureOrigin, Get},
frame_system::EventRecord,
frame_support::{
pallet_prelude::*,
traits::{Currency, EnsureOrigin, Get},
},
frame_system::{EventRecord, RawOrigin},
pallet_session::{self as session, SessionManager},
sp_std::prelude::*,
};

Expand All @@ -39,19 +43,58 @@ fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
assert_eq!(event, &system_event);
}

fn create_user<T: Config>(string: &'static str, n: u32) -> T::AccountId {
account(string, n, SEED)
fn create_funded_user<T: Config + pallet_balances::Config>(
string: &'static str,
n: u32,
balance_factor: u32,
) -> T::AccountId {
let user = account(string, n, SEED);
let balance = <pallet_balances::Pallet<T> as Currency<T::AccountId>>::minimum_balance()
* balance_factor.into();
let _ = <pallet_balances::Pallet<T> as Currency<T::AccountId>>::make_free_balance_be(
&user, balance,
);
user
}

fn invulnerable<T: Config + frame_system::Config>(c: u32) -> T::AccountId {
create_user::<T>("candidate", c)
fn keys<T: Config + session::Config>(c: u32) -> <T as session::Config>::Keys {
use rand::{RngCore, SeedableRng};

let keys = {
let mut keys = [0u8; 128];

if c > 0 {
let mut rng = rand::rngs::StdRng::seed_from_u64(c as u64);
rng.fill_bytes(&mut keys);
}

keys
};

Decode::decode(&mut &keys[..]).unwrap()
}

fn invulnerables<T: Config + frame_system::Config>(count: u32) -> Vec<T::AccountId> {
(0..count).map(|c| invulnerable::<T>(c)).collect::<Vec<_>>()
fn invulnerable<T: Config + session::Config + pallet_balances::Config>(
c: u32,
) -> (T::AccountId, <T as session::Config>::Keys) {
(create_funded_user::<T>("candidate", c, 100), keys::<T>(c))
}

#[benchmarks]
fn invulnerables<
T: Config + frame_system::Config + pallet_session::Config + pallet_balances::Config,
>(
count: u32,
) -> Vec<T::AccountId> {
let invulnerables = (0..count).map(|c| invulnerable::<T>(c)).collect::<Vec<_>>();

for (who, keys) in invulnerables.clone() {
<session::Pallet<T>>::set_keys(RawOrigin::Signed(who).into(), keys, Vec::new()).unwrap();
}

invulnerables.into_iter().map(|(who, _)| who).collect()
}

#[benchmarks(where T: session::Config + pallet_balances::Config)]
mod benchmarks {
use super::*;

Expand Down Expand Up @@ -93,7 +136,7 @@ mod benchmarks {
frame_support::BoundedVec::try_from(invulnerables).unwrap();
<Invulnerables<T>>::put(invulnerables);

let new_invulnerable = invulnerable::<T>(b + 1);
let new_invulnerable = invulnerable::<T>(b + 1).0;

#[extrinsic_call]
_(origin as T::RuntimeOrigin, new_invulnerable.clone());
Expand Down Expand Up @@ -132,8 +175,29 @@ mod benchmarks {
Ok(())
}

// worst case for new session.
#[benchmark]
fn new_session(r: Linear<1, { T::MaxInvulnerables::get() }>) -> Result<(), BenchmarkError> {
let origin =
T::UpdateOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

frame_system::Pallet::<T>::set_block_number(0u32.into());
// now we need to fill up invulnerables
let mut invulnerables = invulnerables::<T>(r);
invulnerables.sort();
<InvulnerablesPallet<T>>::set_invulnerables(origin, invulnerables)
.expect("set invulnerables failed");

#[block]
{
<InvulnerablesPallet<T> as SessionManager<_>>::new_session(0);
}

Ok(())
}

impl_benchmark_test_suite!(
CollatorSelection,
InvulnerablesPallet,
crate::mock::new_test_ext(),
crate::mock::Test,
);
Expand Down
Loading

0 comments on commit 50fab7b

Please sign in to comment.