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 da402d48d0419..34f4205ee67ce 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(())