Skip to content

Commit ff5c446

Browse files
committed
working towards benchmarking
1 parent 4902199 commit ff5c446

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

substrate/frame/indices/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ frame-benchmarking = { optional = true, workspace = true }
2121
frame-support = { workspace = true }
2222
frame-system = { workspace = true }
2323
log = { workspace = true }
24+
pallet-balances = { optional = true, workspace = true }
2425
scale-info = { features = ["derive"], workspace = true }
2526
sp-core = { workspace = true }
2627
sp-io = { workspace = true }

substrate/frame/indices/src/benchmarking.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@ use crate::*;
2323
use frame_benchmarking::v2::*;
2424
use frame_support::traits::Get;
2525
use frame_system::RawOrigin;
26+
use frame_support::traits::{Currency, ReservableCurrency};
2627

2728
const SEED: u32 = 0;
2829

29-
#[benchmarks]
30+
#[benchmarks(
31+
where
32+
T::NativeBalance: frame_support::traits::fungible::Inspect<T::AccountId>,
33+
T: Config + pallet_balances::Config,
34+
pallet_balances::Pallet<T>: Currency<T::AccountId, Balance = BalanceOf<T>> + ReservableCurrency<T::AccountId>,
35+
)]
3036
mod benchmarks {
3137
use super::*;
38+
use crate::migration::v1::MigrateCurrencyToFungibles;
39+
use frame_support::weights::WeightMeter;
40+
use frame_support::migrations::SteppedMigration;
3241

3342
#[benchmark]
3443
fn claim() {
@@ -204,7 +213,7 @@ mod benchmarks {
204213
let old_deposit = T::Deposit::get();
205214
#[block]
206215
{
207-
MigrateCurrencyToFungibles::<T>::migrate_account(account, index, frozen, old_deposit);
216+
let _ = MigrateCurrencyToFungibles::<T, pallet_balances::Pallet<T>>::step(None, &mut WeightMeter::new());
208217
}
209218
assert_eq!(Accounts::<T>::get(index).unwrap().0, account);
210219
assert_eq!(Accounts::<T>::get(index).unwrap().1, old_deposit);

substrate/frame/indices/src/migration/v1/mod.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,28 @@ use crate::{
8080
pallet::{Accounts, Config, HoldReason},
8181
BalanceOf,
8282
};
83-
use alloc::collections::BTreeMap;
83+
8484
use frame_support::{
8585
migrations::{MigrationId, SteppedMigration, SteppedMigrationError},
8686
pallet_prelude::PhantomData,
8787
traits::{
88-
fungible::{Inspect, InspectHold, MutateHold},
88+
fungible::{Inspect, MutateHold},
8989
tokens::{Fortitude, Preservation},
9090
Currency, Get, ReservableCurrency,
9191
},
9292
weights::WeightMeter,
9393
};
94-
use sp_runtime::{traits::Zero, TryRuntimeError};
94+
use sp_runtime::traits::Zero;
9595
use std::cmp::min;
9696

9797
#[cfg(feature = "try-runtime")]
9898
use alloc::vec::Vec;
99+
#[cfg(any(test, feature = "try-runtime"))]
100+
use alloc::collections::BTreeMap;
101+
#[cfg(any(test, feature = "try-runtime"))]
102+
use sp_runtime::TryRuntimeError;
103+
#[cfg(any(test, feature = "try-runtime"))]
104+
use frame_support::traits::fungible::InspectHold;
99105

100106
// Module containing the OLD (v0) storage items that used Currency trait.
101107
pub mod v0 {
@@ -150,12 +156,6 @@ where
150156
return Err(SteppedMigrationError::InsufficientWeight { required: min_required });
151157
}
152158

153-
loop {
154-
// Process one account per step
155-
if meter.try_consume(min_required).is_err() {
156-
break;
157-
}
158-
159159
// Get the iterator for the OLD accounts to migrate
160160
let mut iter = if let Some(Some(last_key)) = cursor {
161161
v0::OldAccounts::<T>::iter_from(v0::OldAccounts::<T>::hashed_key_for(last_key))
@@ -172,7 +172,8 @@ where
172172
println!("Migration completed - no more accounts to migrate");
173173
return Ok(None);
174174
}
175-
}
175+
176+
meter.consume(min_required);
176177

177178
Ok(cursor)
178179
}
@@ -254,7 +255,12 @@ where
254255

255256
Ok(())
256257
}
258+
}
257259

260+
impl<T: Config, OldCurrency> MigrateCurrencyToFungibles<T, OldCurrency>
261+
where
262+
OldCurrency: Currency<T::AccountId, Balance = BalanceOf<T>> + ReservableCurrency<T::AccountId>,
263+
{
258264
fn migrate_account(
259265
account: T::AccountId,
260266
index: T::AccountIndex,

0 commit comments

Comments
 (0)