Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pallet-invulnerables (#163) #221

Merged
merged 18 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 47 additions & 33 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pallet-collator-assignment = { path = "pallets/collator-assignment", default-fea
pallet-collator-assignment-runtime-api = { path = "pallets/collator-assignment/rpc/runtime-api", default-features = false }
pallet-configuration = { path = "pallets/configuration", default-features = false }
pallet-initializer = { path = "pallets/initializer", default-features = false }
pallet-invulnerables = { path = "pallets/invulnerables", default-features = false }
pallet-registrar = { path = "pallets/registrar", default-features = false }
pallet-registrar-runtime-api = { path = "pallets/registrar/rpc/runtime-api", default-features = false }

Expand Down
3 changes: 3 additions & 0 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ fn testnet_genesis(
candidacy_bond: EXISTENTIAL_DEPOSIT * 16,
..Default::default()
},
invulnerables: dancebox_runtime::InvulnerablesConfig {
invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect(),
},
session: dancebox_runtime::SessionConfig {
keys: invulnerables
.into_iter()
Expand Down
46 changes: 46 additions & 0 deletions pallets/invulnerables/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
authors = { workspace = true }
description = "Simple pallet to store invulnarable collators."
name = "pallet-invulnerables"
edition = "2021"
license = "GPL-3.0-only"
version = "0.1.0"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
log = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true, features = ["derive"] }

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

frame-benchmarking = { workspace = true }

[dev-dependencies]
sp-core = { workspace = true }
sp-io = { workspace = true }

[features]
default = ["std"]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
std = [
"parity-scale-codec/std",
"log/std",
"scale-info/std",
"sp-runtime/std",
"sp-std/std",
"frame-support/std",
"frame-system/std",
"frame-benchmarking/std",
]

try-runtime = [ "frame-support/try-runtime" ]
138 changes: 138 additions & 0 deletions pallets/invulnerables/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright (C) Moondance Labs Ltd.
// This file is part of Tanssi.

// Tanssi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Tanssi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>

//! Benchmarking setup for pallet-invulnerables

#![cfg(feature = "runtime-benchmarks")]

use super::*;

#[allow(unused)]
use crate::Pallet as CollatorSelection;
use frame_benchmarking::{account, impl_benchmark_test_suite, v2::*, BenchmarkError};
use frame_support::traits::{EnsureOrigin, Get};
use frame_system::EventRecord;
use sp_std::prelude::*;

const SEED: u32 = 0;

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
// compare to the last event record
let EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}

fn create_user<T: Config>(string: &'static str, n: u32) -> T::AccountId {
account(string, n, SEED)
}

fn invulnerable<T: Config + frame_system::Config>(c: u32) -> T::AccountId {
create_user::<T>("candidate", c)
}

fn invulnerables<T: Config + frame_system::Config>(count: u32) -> Vec<T::AccountId> {
(0..count).map(|c| invulnerable::<T>(c)).collect::<Vec<_>>()
}

#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn set_invulnerables(
b: Linear<1, { T::MaxInvulnerables::get() }>,
) -> Result<(), BenchmarkError> {
let origin =
T::UpdateOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

let new_invulnerables = invulnerables::<T>(b);
let mut sorted_new_invulnerables = new_invulnerables.clone();
sorted_new_invulnerables.sort();

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

// assert that it comes out sorted
assert_last_event::<T>(
Event::NewInvulnerables {
invulnerables: sorted_new_invulnerables,
}
.into(),
);
Ok(())
}

#[benchmark]
fn add_invulnerable(
b: Linear<1, { T::MaxInvulnerables::get() - 1 }>,
) -> Result<(), BenchmarkError> {
let origin =
T::UpdateOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

// now we need to fill up invulnerables
let mut invulnerables = invulnerables::<T>(b);
invulnerables.sort();
let invulnerables: frame_support::BoundedVec<_, T::MaxInvulnerables> =
frame_support::BoundedVec::try_from(invulnerables).unwrap();
<Invulnerables<T>>::put(invulnerables);

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

#[extrinsic_call]
_(origin as T::RuntimeOrigin, new_invulnerable.clone());

assert_last_event::<T>(
Event::InvulnerableAdded {
account_id: new_invulnerable,
}
.into(),
);
Ok(())
}

#[benchmark]
fn remove_invulnerable(
b: Linear<{ 1 }, { T::MaxInvulnerables::get() }>,
) -> Result<(), BenchmarkError> {
let origin =
T::UpdateOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let mut invulnerables = invulnerables::<T>(b);
invulnerables.sort();
let invulnerables: frame_support::BoundedVec<_, T::MaxInvulnerables> =
frame_support::BoundedVec::try_from(invulnerables).unwrap();
<Invulnerables<T>>::put(invulnerables);
let to_remove = <Invulnerables<T>>::get().first().unwrap().clone();

#[extrinsic_call]
_(origin as T::RuntimeOrigin, to_remove.clone());

assert_last_event::<T>(
Event::InvulnerableRemoved {
account_id: to_remove,
}
.into(),
);
Ok(())
}

impl_benchmark_test_suite!(
CollatorSelection,
crate::mock::new_test_ext(),
crate::mock::Test,
);
}
Loading
Loading