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

Implement pallet_registrar hooks #325

Merged
merged 20 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
151 changes: 133 additions & 18 deletions pallets/registrar/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ mod benchmarks {
}

#[benchmark]
fn deregister(x: Linear<5, 3_000_000>, y: Linear<1, 50>) {
fn deregister_immediate(x: Linear<5, 3_000_000>, y: Linear<1, 50>) {
let storage = vec![(b"code".to_vec(), vec![1; x as usize]).into()];
let storage = new_genesis_data(storage);

Expand All @@ -112,10 +112,12 @@ mod benchmarks {
storage.clone(),
)
.unwrap();
// Do not call mark_valid_for_collating, to ensure that the deregister call also executes the cleanup hooks
}

// We should have registered y
assert_eq!(Pallet::<T>::pending_verification().len(), y as usize);
assert!(Pallet::<T>::registrar_deposit(ParaId::from(y - 1)).is_some());

#[extrinsic_call]
Pallet::<T>::deregister(RawOrigin::Root, (y - 1).into());
Expand All @@ -125,6 +127,57 @@ mod benchmarks {
assert!(Pallet::<T>::registrar_deposit(ParaId::from(y - 1)).is_none());
}

#[benchmark]
fn deregister_scheduled(x: Linear<5, 3_000_000>, y: Linear<1, 50>) {
let storage = vec![(b"code".to_vec(), vec![1; x as usize]).into()];
let storage = new_genesis_data(storage);
let genesis_para_id_len = Pallet::<T>::registered_para_ids().len();

for i in 0..y {
// Twice the deposit just in case
let (caller, _deposit_amount) =
create_funded_user::<T>("caller", i, T::DepositAmount::get());
Pallet::<T>::register(
RawOrigin::Signed(caller.clone()).into(),
i.into(),
storage.clone(),
)
.unwrap();
// Call mark_valid_for_collating to ensure that the deregister call
// does not execute the cleanup hooks immediately
Pallet::<T>::mark_valid_for_collating(RawOrigin::Root.into(), i.into()).unwrap();
}

// Start a new session
Pallet::<T>::initializer_on_new_session(&T::SessionDelay::get());
// We should have registered y
assert_eq!(
Pallet::<T>::registered_para_ids().len(),
genesis_para_id_len + y as usize
);
assert!(Pallet::<T>::registrar_deposit(ParaId::from(y - 1)).is_some());

#[extrinsic_call]
Pallet::<T>::deregister(RawOrigin::Root, (y - 1).into());

// We now have y - 1 but the deposit has not been removed yet
assert_eq!(
Pallet::<T>::pending_registered_para_ids()[0].1.len(),
genesis_para_id_len + (y - 1) as usize
);
assert!(Pallet::<T>::registrar_deposit(ParaId::from(y - 1)).is_some());

// Start a new session
Pallet::<T>::initializer_on_new_session(&T::SessionDelay::get());

// Now it has been removed
assert_eq!(
Pallet::<T>::registered_para_ids().len(),
genesis_para_id_len + (y - 1) as usize
);
assert!(Pallet::<T>::registrar_deposit(ParaId::from(y - 1)).is_none());
}

#[benchmark]
fn mark_valid_for_collating(y: Linear<1, 50>) {
let storage = vec![(vec![1; 4], vec![1; 3_000_000usize]).into()];
Expand Down Expand Up @@ -208,7 +261,7 @@ mod benchmarks {
Pallet::<T>::deregister(RawOrigin::Root.into(), para_id).unwrap();
}

// Worst case: when RegisteredParaIds and PendingVerification are both full
// Worst case: when RegisteredParaIds and Paused are both full
// First loop to fill RegisteredParaIds to its maximum
for i in 0..y {
// Twice the deposit just in case
Expand All @@ -223,7 +276,7 @@ mod benchmarks {
Pallet::<T>::mark_valid_for_collating(RawOrigin::Root.into(), i.into()).unwrap();
}

// Second loop to fill PendingVerification to its maximum
// Second loop to fill Paused to its maximum
for k in 1000..(1000 + y) {
let (caller, _deposit_amount) =
create_funded_user::<T>("caller", k, T::DepositAmount::get());
Expand All @@ -233,31 +286,93 @@ mod benchmarks {
storage.clone(),
)
.unwrap();
Pallet::<T>::mark_valid_for_collating(RawOrigin::Root.into(), k.into()).unwrap();
Pallet::<T>::pause_container_chain(RawOrigin::Root.into(), k.into()).unwrap();
}

// Check PendingParaIds has a length of y
assert_eq!(
Pallet::<T>::pending_registered_para_ids()[0].1.len(),
y as usize
);
// Check PendingPaused has a length of y
assert_eq!(Pallet::<T>::pending_paused()[0].1.len(), y as usize);
// Check y-1 is not in PendingPaused
assert!(!Pallet::<T>::pending_paused()[0]
.1
.contains(&ParaId::from(y - 1)));
// Check y-1 is in pending_registered_para_ids
assert!(Pallet::<T>::pending_registered_para_ids()[0]
.1
.contains(&ParaId::from(y - 1)));

#[extrinsic_call]
Pallet::<T>::pause_container_chain(RawOrigin::Root, (y - 1).into());

// Start a new session
Pallet::<T>::initializer_on_new_session(&T::SessionDelay::get());

// Check y-1 is not in PendingVerification
assert!(!Pallet::<T>::pending_verification().contains(&ParaId::from(y - 1)));
// Check y-1 is in Paused
assert!(Pallet::<T>::paused().contains(&ParaId::from(y - 1)));
// Check y-1 is not in registered_para_ids
assert!(!Pallet::<T>::registered_para_ids().contains(&ParaId::from(y - 1)));
}

#[benchmark]
fn unpause_container_chain(y: Linear<1, 50>) {
let storage = vec![(vec![1; 4], vec![1; 3_000_000usize]).into()];
let storage = new_genesis_data(storage);

// Deregister all the existing chains to avoid conflicts with the new ones
for para_id in Pallet::<T>::registered_para_ids() {
Pallet::<T>::deregister(RawOrigin::Root.into(), para_id).unwrap();
}

// Worst case: when RegisteredParaIds and Paused are both full
// First loop to fill RegisteredParaIds to its maximum
for i in 0..y {
// Twice the deposit just in case
let (caller, _deposit_amount) =
create_funded_user::<T>("caller", i, T::DepositAmount::get());
Pallet::<T>::register(
RawOrigin::Signed(caller.clone()).into(),
i.into(),
storage.clone(),
)
.unwrap();
Pallet::<T>::mark_valid_for_collating(RawOrigin::Root.into(), i.into()).unwrap();
}

// Second loop to fill Paused to its maximum
for k in 1000..(1000 + y) {
let (caller, _deposit_amount) =
create_funded_user::<T>("caller", k, T::DepositAmount::get());
Pallet::<T>::register(
RawOrigin::Signed(caller.clone()).into(),
k.into(),
storage.clone(),
)
.unwrap();
Pallet::<T>::mark_valid_for_collating(RawOrigin::Root.into(), k.into()).unwrap();
Pallet::<T>::pause_container_chain(RawOrigin::Root.into(), k.into()).unwrap();
}

// Check PendingPaused has a length of y
assert_eq!(Pallet::<T>::pending_paused()[0].1.len(), y as usize);
// Check 1000 is in PendingPaused
assert!(Pallet::<T>::pending_paused()[0]
.1
.contains(&ParaId::from(1000)));
// Check 1000 is not in pending_registered_para_ids
assert!(!Pallet::<T>::pending_registered_para_ids()[0]
.1
.contains(&ParaId::from(1000)));

#[extrinsic_call]
Pallet::<T>::pause_container_chain(RawOrigin::Root, (y - 1).into());
Pallet::<T>::unpause_container_chain(RawOrigin::Root, 1000u32.into());

// y-1 should be included again in PendingVerification
assert!(Pallet::<T>::pending_verification().contains(&ParaId::from(y - 1)));
// Start a new session
Pallet::<T>::initializer_on_new_session(&T::SessionDelay::get());

// y-1 should not be in PendingParaIds
assert_eq!(
Pallet::<T>::pending_registered_para_ids()[0].1.len(),
(y - 1) as usize
);
// Check 1000 is not in Paused
assert!(!Pallet::<T>::paused().contains(&ParaId::from(1000)));
// Check 1000 is in registered_para_ids
assert!(Pallet::<T>::registered_para_ids().contains(&ParaId::from(1000)));
}

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