From f4e7cb7fa9bd7cf4c1faafc8645d346a5e768699 Mon Sep 17 00:00:00 2001 From: Doordashcon Date: Mon, 15 Sep 2025 09:33:06 +0100 Subject: [PATCH] Simulate `rank_to_votes` in `pallet-ranked-collective` benchmark. (#9731) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit resolves #9730 --------- Co-authored-by: Bastian Köcher Co-authored-by: Oliver Tale-Yazdi (cherry picked from commit 1cbf4eed97a87ae4c1aef6176c80761c49f60e6f) --- prdoc/pr_9731.prdoc | 14 +++++++++++ .../ranked-collective/src/benchmarking.rs | 23 +++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 prdoc/pr_9731.prdoc diff --git a/prdoc/pr_9731.prdoc b/prdoc/pr_9731.prdoc new file mode 100644 index 0000000000000..5ce5576d95fcc --- /dev/null +++ b/prdoc/pr_9731.prdoc @@ -0,0 +1,14 @@ +title: simulate `rank_to_votes` conversion in vote benchmark + +doc: +- audience: Runtime Dev + description: | + Fixes benchmarking failures for the vote extrinsic in pallet-ranked-collective by properly + simulating the `rank_to_votes` conversion process. Previously used hardcoded vote values + which caused assertion failures when using custom VoteWeight converters (e.g., in + [Ambassador Collective configuration](https://github.com/polkadot-fellows/runtimes/pull/736)). The fix calculates vote weight based on member's + actual rank and minimum required rank for the class. + +crates: +- name: pallet-ranked-collective + bump: patch \ No newline at end of file diff --git a/substrate/frame/ranked-collective/src/benchmarking.rs b/substrate/frame/ranked-collective/src/benchmarking.rs index 978489fb8485e..68f0623ac910f 100644 --- a/substrate/frame/ranked-collective/src/benchmarking.rs +++ b/substrate/frame/ranked-collective/src/benchmarking.rs @@ -58,7 +58,9 @@ fn make_member, I: 'static>(rank: Rank) -> T::AccountId { } #[instance_benchmarks( -where <>::Polls as frame_support::traits::Polling>>>::Index: From +where + <>::Polls as frame_support::traits::Polling>>>::Index: From, + ::RuntimeEvent: TryInto>, )] mod benchmarks { use super::*; @@ -227,9 +229,22 @@ mod benchmarks { // If the class exists, verify the vote event and tally. if let Some(_) = class { - let tally = Tally::from_parts(0, 0, 1); - let vote_event = Event::Voted { who: caller, poll, vote: VoteRecord::Nay(1), tally }; - assert_last_event::(vote_event.into()); + // Get the actual vote weight from the latest event's VoteRecord::Nay + let mut events = frame_system::Pallet::::events(); + let last_event = events.pop().expect("At least one event should exist"); + let event: Event = last_event + .event + .try_into() + .unwrap_or_else(|_| panic!("Event conversion failed")); + + match event { + Event::Voted { vote: VoteRecord::Nay(vote_weight), who, poll: poll2, tally } => { + assert_eq!(tally, Tally::from_parts(0, 0, vote_weight)); + assert_eq!(caller, who); + assert_eq!(poll, poll2); + }, + _ => panic!("Invalid event"), + }; } Ok(())