Skip to content

Commit 7ab3713

Browse files
Krayt78shawntabrizire-gius
committed
Migrating pallet-state-trie-migration to benchmarking V2 (paritytech#6617)
# Description Migrated pallet-state-trie-migration benchmarking to the new benchmarking syntax v2. This is part of paritytech#6202 Co-authored-by: Shawn Tabrizi <[email protected]> Co-authored-by: Giuseppe Re <[email protected]>
1 parent 5153ede commit 7ab3713

File tree

1 file changed

+108
-68
lines changed
  • substrate/frame/state-trie-migration/src

1 file changed

+108
-68
lines changed

substrate/frame/state-trie-migration/src/lib.rs

Lines changed: 108 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,13 @@ pub mod pallet {
249249
if limits.item.is_zero() || limits.size.is_zero() {
250250
// handle this minor edge case, else we would call `migrate_tick` at least once.
251251
log!(warn, "limits are zero. stopping");
252-
return Ok(())
252+
return Ok(());
253253
}
254254

255255
while !self.exhausted(limits) && !self.finished() {
256256
if let Err(e) = self.migrate_tick() {
257257
log!(error, "migrate_until_exhaustion failed: {:?}", e);
258-
return Err(e)
258+
return Err(e);
259259
}
260260
}
261261

@@ -332,7 +332,7 @@ pub mod pallet {
332332
_ => {
333333
// defensive: there must be an ongoing top migration.
334334
frame_support::defensive!("cannot migrate child key.");
335-
return Ok(())
335+
return Ok(());
336336
},
337337
};
338338

@@ -374,7 +374,7 @@ pub mod pallet {
374374
Progress::Complete => {
375375
// defensive: there must be an ongoing top migration.
376376
frame_support::defensive!("cannot migrate top key.");
377-
return Ok(())
377+
return Ok(());
378378
},
379379
};
380380

@@ -669,7 +669,7 @@ pub mod pallet {
669669
// ensure that the migration witness data was correct.
670670
if real_size_upper < task.dyn_size {
671671
Self::slash(who, deposit)?;
672-
return Ok(().into())
672+
return Ok(().into());
673673
}
674674

675675
Self::deposit_event(Event::<T>::Migrated {
@@ -957,6 +957,7 @@ pub mod pallet {
957957
mod benchmarks {
958958
use super::{pallet::Pallet as StateTrieMigration, *};
959959
use alloc::vec;
960+
use frame_benchmarking::v2::*;
960961
use frame_support::traits::fungible::{Inspect, Mutate};
961962

962963
// The size of the key seemingly makes no difference in the read/write time, so we make it
@@ -970,8 +971,12 @@ mod benchmarks {
970971
stash
971972
}
972973

973-
frame_benchmarking::benchmarks! {
974-
continue_migrate {
974+
#[benchmarks]
975+
mod inner_benchmarks {
976+
use super::*;
977+
978+
#[benchmark]
979+
fn continue_migrate() -> Result<(), BenchmarkError> {
975980
// note that this benchmark should migrate nothing, as we only want the overhead weight
976981
// of the bookkeeping, and the migration cost itself is noted via the `dynamic_weight`
977982
// function.
@@ -980,116 +985,151 @@ mod benchmarks {
980985
let stash = set_balance_for_deposit::<T>(&caller, null.item);
981986
// Allow signed migrations.
982987
SignedMigrationMaxLimits::<T>::put(MigrationLimits { size: 1024, item: 5 });
983-
}: _(frame_system::RawOrigin::Signed(caller.clone()), null, 0, StateTrieMigration::<T>::migration_process())
984-
verify {
988+
989+
#[extrinsic_call]
990+
_(
991+
frame_system::RawOrigin::Signed(caller.clone()),
992+
null,
993+
0,
994+
StateTrieMigration::<T>::migration_process(),
995+
);
996+
985997
assert_eq!(StateTrieMigration::<T>::migration_process(), Default::default());
986-
assert_eq!(T::Currency::balance(&caller), stash)
998+
assert_eq!(T::Currency::balance(&caller), stash);
999+
1000+
Ok(())
9871001
}
9881002

989-
continue_migrate_wrong_witness {
1003+
#[benchmark]
1004+
fn continue_migrate_wrong_witness() -> Result<(), BenchmarkError> {
9901005
let null = MigrationLimits::default();
9911006
let caller = frame_benchmarking::whitelisted_caller();
992-
let bad_witness = MigrationTask { progress_top: Progress::LastKey(vec![1u8].try_into().unwrap()), ..Default::default() };
993-
}: {
994-
assert!(
995-
StateTrieMigration::<T>::continue_migrate(
1007+
let bad_witness = MigrationTask {
1008+
progress_top: Progress::LastKey(vec![1u8].try_into().unwrap()),
1009+
..Default::default()
1010+
};
1011+
#[block]
1012+
{
1013+
assert!(StateTrieMigration::<T>::continue_migrate(
9961014
frame_system::RawOrigin::Signed(caller).into(),
9971015
null,
9981016
0,
9991017
bad_witness,
10001018
)
1001-
.is_err()
1002-
)
1003-
}
1004-
verify {
1005-
assert_eq!(StateTrieMigration::<T>::migration_process(), Default::default())
1019+
.is_err());
1020+
}
1021+
1022+
assert_eq!(StateTrieMigration::<T>::migration_process(), Default::default());
1023+
1024+
Ok(())
10061025
}
10071026

1008-
migrate_custom_top_success {
1027+
#[benchmark]
1028+
fn migrate_custom_top_success() -> Result<(), BenchmarkError> {
10091029
let null = MigrationLimits::default();
10101030
let caller: T::AccountId = frame_benchmarking::whitelisted_caller();
10111031
let stash = set_balance_for_deposit::<T>(&caller, null.item);
1012-
}: migrate_custom_top(frame_system::RawOrigin::Signed(caller.clone()), Default::default(), 0)
1013-
verify {
1032+
#[extrinsic_call]
1033+
migrate_custom_top(
1034+
frame_system::RawOrigin::Signed(caller.clone()),
1035+
Default::default(),
1036+
0,
1037+
);
1038+
10141039
assert_eq!(StateTrieMigration::<T>::migration_process(), Default::default());
1015-
assert_eq!(T::Currency::balance(&caller), stash)
1040+
assert_eq!(T::Currency::balance(&caller), stash);
1041+
Ok(())
10161042
}
10171043

1018-
migrate_custom_top_fail {
1044+
#[benchmark]
1045+
fn migrate_custom_top_fail() -> Result<(), BenchmarkError> {
10191046
let null = MigrationLimits::default();
10201047
let caller: T::AccountId = frame_benchmarking::whitelisted_caller();
10211048
let stash = set_balance_for_deposit::<T>(&caller, null.item);
10221049
// for tests, we need to make sure there is _something_ in storage that is being
10231050
// migrated.
1024-
sp_io::storage::set(b"foo", vec![1u8;33].as_ref());
1025-
}: {
1026-
assert!(
1027-
StateTrieMigration::<T>::migrate_custom_top(
1051+
sp_io::storage::set(b"foo", vec![1u8; 33].as_ref());
1052+
#[block]
1053+
{
1054+
assert!(StateTrieMigration::<T>::migrate_custom_top(
10281055
frame_system::RawOrigin::Signed(caller.clone()).into(),
10291056
vec![b"foo".to_vec()],
10301057
1,
1031-
).is_ok()
1032-
);
1058+
)
1059+
.is_ok());
1060+
1061+
frame_system::Pallet::<T>::assert_last_event(
1062+
<T as Config>::RuntimeEvent::from(crate::Event::Slashed {
1063+
who: caller.clone(),
1064+
amount: StateTrieMigration::<T>::calculate_deposit_for(1u32),
1065+
})
1066+
.into(),
1067+
);
1068+
}
10331069

1034-
frame_system::Pallet::<T>::assert_last_event(
1035-
<T as Config>::RuntimeEvent::from(crate::Event::Slashed {
1036-
who: caller.clone(),
1037-
amount: StateTrieMigration::<T>::calculate_deposit_for(1u32),
1038-
}).into(),
1039-
);
1040-
}
1041-
verify {
10421070
assert_eq!(StateTrieMigration::<T>::migration_process(), Default::default());
10431071
// must have gotten slashed
1044-
assert!(T::Currency::balance(&caller) < stash)
1072+
assert!(T::Currency::balance(&caller) < stash);
1073+
1074+
Ok(())
10451075
}
10461076

1047-
migrate_custom_child_success {
1077+
#[benchmark]
1078+
fn migrate_custom_child_success() -> Result<(), BenchmarkError> {
10481079
let caller: T::AccountId = frame_benchmarking::whitelisted_caller();
10491080
let stash = set_balance_for_deposit::<T>(&caller, 0);
1050-
}: migrate_custom_child(
1051-
frame_system::RawOrigin::Signed(caller.clone()),
1052-
StateTrieMigration::<T>::childify(Default::default()),
1053-
Default::default(),
1054-
0
1055-
)
1056-
verify {
1081+
1082+
#[extrinsic_call]
1083+
migrate_custom_child(
1084+
frame_system::RawOrigin::Signed(caller.clone()),
1085+
StateTrieMigration::<T>::childify(Default::default()),
1086+
Default::default(),
1087+
0,
1088+
);
1089+
10571090
assert_eq!(StateTrieMigration::<T>::migration_process(), Default::default());
10581091
assert_eq!(T::Currency::balance(&caller), stash);
1092+
1093+
Ok(())
10591094
}
10601095

1061-
migrate_custom_child_fail {
1096+
#[benchmark]
1097+
fn migrate_custom_child_fail() -> Result<(), BenchmarkError> {
10621098
let caller: T::AccountId = frame_benchmarking::whitelisted_caller();
10631099
let stash = set_balance_for_deposit::<T>(&caller, 1);
10641100
// for tests, we need to make sure there is _something_ in storage that is being
10651101
// migrated.
1066-
sp_io::default_child_storage::set(b"top", b"foo", vec![1u8;33].as_ref());
1067-
}: {
1068-
assert!(
1069-
StateTrieMigration::<T>::migrate_custom_child(
1102+
sp_io::default_child_storage::set(b"top", b"foo", vec![1u8; 33].as_ref());
1103+
1104+
#[block]
1105+
{
1106+
assert!(StateTrieMigration::<T>::migrate_custom_child(
10701107
frame_system::RawOrigin::Signed(caller.clone()).into(),
10711108
StateTrieMigration::<T>::childify("top"),
10721109
vec![b"foo".to_vec()],
10731110
1,
1074-
).is_ok()
1075-
)
1076-
}
1077-
verify {
1111+
)
1112+
.is_ok());
1113+
}
10781114
assert_eq!(StateTrieMigration::<T>::migration_process(), Default::default());
10791115
// must have gotten slashed
1080-
assert!(T::Currency::balance(&caller) < stash)
1116+
assert!(T::Currency::balance(&caller) < stash);
1117+
Ok(())
10811118
}
10821119

1083-
process_top_key {
1084-
let v in 1 .. (4 * 1024 * 1024);
1085-
1120+
#[benchmark]
1121+
fn process_top_key(v: Linear<1, { 4 * 1024 * 1024 }>) -> Result<(), BenchmarkError> {
10861122
let value = alloc::vec![1u8; v as usize];
10871123
sp_io::storage::set(KEY, &value);
1088-
}: {
1089-
let data = sp_io::storage::get(KEY).unwrap();
1090-
sp_io::storage::set(KEY, &data);
1091-
let _next = sp_io::storage::next_key(KEY);
1092-
assert_eq!(data, value);
1124+
#[block]
1125+
{
1126+
let data = sp_io::storage::get(KEY).unwrap();
1127+
sp_io::storage::set(KEY, &data);
1128+
let _next = sp_io::storage::next_key(KEY);
1129+
assert_eq!(data, value);
1130+
}
1131+
1132+
Ok(())
10931133
}
10941134

10951135
impl_benchmark_test_suite!(
@@ -1741,7 +1781,7 @@ pub(crate) mod remote_tests {
17411781
let ((finished, weight), proof) = ext.execute_and_prove(|| {
17421782
let weight = run_to_block::<Runtime>(now + One::one()).1;
17431783
if StateTrieMigration::<Runtime>::migration_process().finished() {
1744-
return (true, weight)
1784+
return (true, weight);
17451785
}
17461786
duration += One::one();
17471787
now += One::one();
@@ -1768,7 +1808,7 @@ pub(crate) mod remote_tests {
17681808
ext.commit_all().unwrap();
17691809

17701810
if finished {
1771-
break
1811+
break;
17721812
}
17731813
}
17741814

0 commit comments

Comments
 (0)