Skip to content

Commit 4866b08

Browse files
paritytech-release-backport-bot[bot]Doordashconbkchrggwpezkianenigma
authored
[unstable2507] Backport #9731 (#9739)
Backport #9731 into `unstable2507` from Doordashcon. See the [documentation](https://github.com/paritytech/polkadot-sdk/blob/master/docs/BACKPORT.md) on how to use this bot. <!-- # To be used by other automation, do not modify: original-pr-number: #${pull_number} --> Co-authored-by: Doordashcon <[email protected]> Co-authored-by: Bastian Köcher <[email protected]> Co-authored-by: Oliver Tale-Yazdi <[email protected]> Co-authored-by: Kian Paimani <[email protected]>
1 parent bc4eade commit 4866b08

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

prdoc/pr_9731.prdoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
title: simulate `rank_to_votes` conversion in vote benchmark
2+
3+
doc:
4+
- audience: Runtime Dev
5+
description: |
6+
Fixes benchmarking failures for the vote extrinsic in pallet-ranked-collective by properly
7+
simulating the `rank_to_votes` conversion process. Previously used hardcoded vote values
8+
which caused assertion failures when using custom VoteWeight converters (e.g., in
9+
[Ambassador Collective configuration](https://github.com/polkadot-fellows/runtimes/pull/736)). The fix calculates vote weight based on member's
10+
actual rank and minimum required rank for the class.
11+
12+
crates:
13+
- name: pallet-ranked-collective
14+
bump: patch

substrate/frame/ranked-collective/src/benchmarking.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ fn make_member<T: Config<I>, I: 'static>(rank: Rank) -> T::AccountId {
5858
}
5959

6060
#[instance_benchmarks(
61-
where <<T as pallet::Config<I>>::Polls as frame_support::traits::Polling<Tally<T, I, pallet::Pallet<T, I>>>>::Index: From<u8>
61+
where
62+
<<T as pallet::Config<I>>::Polls as frame_support::traits::Polling<Tally<T, I, pallet::Pallet<T, I>>>>::Index: From<u8>,
63+
<T as frame_system::Config>::RuntimeEvent: TryInto<pallet::Event<T, I>>,
6264
)]
6365
mod benchmarks {
6466
use super::*;
@@ -227,9 +229,22 @@ mod benchmarks {
227229

228230
// If the class exists, verify the vote event and tally.
229231
if let Some(_) = class {
230-
let tally = Tally::from_parts(0, 0, 1);
231-
let vote_event = Event::Voted { who: caller, poll, vote: VoteRecord::Nay(1), tally };
232-
assert_last_event::<T, I>(vote_event.into());
232+
// Get the actual vote weight from the latest event's VoteRecord::Nay
233+
let mut events = frame_system::Pallet::<T>::events();
234+
let last_event = events.pop().expect("At least one event should exist");
235+
let event: Event<T, I> = last_event
236+
.event
237+
.try_into()
238+
.unwrap_or_else(|_| panic!("Event conversion failed"));
239+
240+
match event {
241+
Event::Voted { vote: VoteRecord::Nay(vote_weight), who, poll: poll2, tally } => {
242+
assert_eq!(tally, Tally::from_parts(0, 0, vote_weight));
243+
assert_eq!(caller, who);
244+
assert_eq!(poll, poll2);
245+
},
246+
_ => panic!("Invalid event"),
247+
};
233248
}
234249

235250
Ok(())

0 commit comments

Comments
 (0)