From 37c87f0cebd3875c2fab77cd095615da14824876 Mon Sep 17 00:00:00 2001 From: SunTiebing <87381708+SunTiebing@users.noreply.github.com> Date: Mon, 9 Dec 2024 21:37:17 +0800 Subject: [PATCH] Adjust vtoken voting storage (#1547) * change VotingFor and ReferendumTimeout storage type to StorageDoubleMap * fix clippy * fix clippy * adjust test code * Remove conditional compilation. * Set CurrencyIdOf in the VotingFor storage as mandatory and AccountIdOf as optional. --- pallets/vtoken-voting/src/benchmarking.rs | 8 ++++---- pallets/vtoken-voting/src/lib.rs | 10 +++++----- pallets/vtoken-voting/src/migration.rs | 2 +- pallets/vtoken-voting/src/tests/kusama_common_test.rs | 10 +++++----- .../vtoken-voting/src/tests/polkadot_common_test.rs | 10 +++++----- pallets/vtoken-voting/src/tests/vbnc_test.rs | 10 +++++----- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/pallets/vtoken-voting/src/benchmarking.rs b/pallets/vtoken-voting/src/benchmarking.rs index 79ff2e7fb0..a8eda54e37 100644 --- a/pallets/vtoken-voting/src/benchmarking.rs +++ b/pallets/vtoken-voting/src/benchmarking.rs @@ -87,7 +87,7 @@ mod benchmarks { response.clone(), )?; } - let votes = match VotingFor::::get(&caller, vtoken) { + let votes = match VotingFor::::get(vtoken, &caller) { Voting::Casting(Casting { votes, .. }) => votes, _ => return Err("Votes are not direct".into()), }; @@ -97,7 +97,7 @@ mod benchmarks { Pallet::::vote(RawOrigin::Signed(caller.clone()), vtoken, poll_index, vote); assert_matches!( - VotingFor::::get(&caller, vtoken), + VotingFor::::get(vtoken, &caller), Voting::Casting(Casting { votes, .. }) if votes.len() == (r + 1) as usize ); @@ -124,7 +124,7 @@ mod benchmarks { response.clone(), )?; } - let votes = match VotingFor::::get(&caller, vtoken) { + let votes = match VotingFor::::get(vtoken, &caller) { Voting::Casting(Casting { votes, .. }) => votes, _ => return Err("Votes are not direct".into()), }; @@ -136,7 +136,7 @@ mod benchmarks { Pallet::::vote(RawOrigin::Signed(caller.clone()), vtoken, poll_index, new_vote); assert_matches!( - VotingFor::::get(&caller, vtoken), + VotingFor::::get(vtoken, &caller), Voting::Casting(Casting { votes, .. }) if votes.len() == r as usize ); diff --git a/pallets/vtoken-voting/src/lib.rs b/pallets/vtoken-voting/src/lib.rs index 1360544b39..953a8a1153 100644 --- a/pallets/vtoken-voting/src/lib.rs +++ b/pallets/vtoken-voting/src/lib.rs @@ -340,9 +340,9 @@ pub mod pallet { pub type VotingFor = StorageDoubleMap< _, Twox64Concat, - AccountIdOf, - Twox64Concat, CurrencyIdOf, + Twox64Concat, + AccountIdOf, VotingOf, ValueQuery, >; @@ -1078,7 +1078,7 @@ pub mod pallet { let mut total_vote = None; Self::try_access_poll(vtoken, poll_index, |poll_status| { let tally = poll_status.ensure_ongoing().ok_or(Error::::NotOngoing)?; - VotingFor::::try_mutate(who, vtoken, |voting| { + VotingFor::::try_mutate(vtoken, who, |voting| { if let Voting::Casting(Casting { ref mut votes, delegations, .. }) = voting { match votes.binary_search_by_key(&poll_index, |i| i.0) { Ok(i) => { @@ -1131,7 +1131,7 @@ pub mod pallet { poll_index: PollIndex, scope: UnvoteScope, ) -> DispatchResult { - VotingFor::::try_mutate(who, vtoken, |voting| { + VotingFor::::try_mutate(vtoken, who, |voting| { if let Voting::Casting(Casting { ref mut votes, delegations, ref mut prior }) = voting { @@ -1183,7 +1183,7 @@ pub mod pallet { /// indicate a security hole) but may be reduced from what they are currently. pub(crate) fn update_lock(who: &AccountIdOf, vtoken: CurrencyIdOf) -> DispatchResult { let current_block = Self::get_agent_block_number(&vtoken)?; - let lock_needed = VotingFor::::mutate(who, vtoken, |voting| { + let lock_needed = VotingFor::::mutate(vtoken, who, |voting| { voting.rejig(current_block); voting.locked_balance() }); diff --git a/pallets/vtoken-voting/src/migration.rs b/pallets/vtoken-voting/src/migration.rs index e3a94e5a5f..c302464e34 100644 --- a/pallets/vtoken-voting/src/migration.rs +++ b/pallets/vtoken-voting/src/migration.rs @@ -290,7 +290,7 @@ pub fn migrate_to_v4>>() -> Weight { // Iterate over all items in the old storage v3::VotingFor::::iter().for_each(|(account, voting)| { - VotingFor::::insert(account, vtoken, voting); + VotingFor::::insert(vtoken, account, voting); weight += T::DbWeight::get().reads_writes(0, 1); }); diff --git a/pallets/vtoken-voting/src/tests/kusama_common_test.rs b/pallets/vtoken-voting/src/tests/kusama_common_test.rs index 2fb6de88ab..9c8e7c5714 100644 --- a/pallets/vtoken-voting/src/tests/kusama_common_test.rs +++ b/pallets/vtoken-voting/src/tests/kusama_common_test.rs @@ -248,7 +248,7 @@ fn ensure_balance_after_unlock() { assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, poll_index)); assert_eq!(usable_balance(vtoken, &ALICE), 0); assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 10); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 10); }); } } @@ -299,7 +299,7 @@ fn ensure_comprehensive_balance_after_unlock() { assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, poll_index)); assert_eq!(usable_balance(vtoken, &ALICE), 8); assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 2); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 2); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 2); assert_ok!(VtokenVoting::vote( RuntimeOrigin::signed(ALICE), @@ -311,7 +311,7 @@ fn ensure_comprehensive_balance_after_unlock() { assert_eq!(usable_balance(vtoken, &ALICE), 0); assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 10); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 10); }); } } @@ -431,12 +431,12 @@ fn lock_amalgamation_valid_with_multiple_removed_votes() { VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0), Error::::NoPermissionYet ); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 10); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 10); assert_eq!(usable_balance(vtoken, &ALICE), 0); RelaychainDataProvider::set_block_number(11); assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0)); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 10); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 10); assert_eq!(usable_balance(vtoken, &ALICE), 0); assert_eq!( ClassLocksFor::::get(&ALICE), diff --git a/pallets/vtoken-voting/src/tests/polkadot_common_test.rs b/pallets/vtoken-voting/src/tests/polkadot_common_test.rs index 0b09ae10ba..f82cb3e90f 100644 --- a/pallets/vtoken-voting/src/tests/polkadot_common_test.rs +++ b/pallets/vtoken-voting/src/tests/polkadot_common_test.rs @@ -248,7 +248,7 @@ fn ensure_balance_after_unlock() { assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, poll_index)); assert_eq!(usable_balance(vtoken, &ALICE), 0); assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 10); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 10); }); } } @@ -299,7 +299,7 @@ fn ensure_comprehensive_balance_after_unlock() { assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, poll_index)); assert_eq!(usable_balance(vtoken, &ALICE), 8); assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 2); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 2); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 2); assert_ok!(VtokenVoting::vote( RuntimeOrigin::signed(ALICE), @@ -311,7 +311,7 @@ fn ensure_comprehensive_balance_after_unlock() { assert_eq!(usable_balance(vtoken, &ALICE), 0); assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 10); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 10); }); } } @@ -431,12 +431,12 @@ fn lock_amalgamation_valid_with_multiple_removed_votes() { VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0), Error::::NoPermissionYet ); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 10); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 10); assert_eq!(usable_balance(vtoken, &ALICE), 0); RelaychainDataProvider::set_block_number(11); assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0)); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 10); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 10); assert_eq!(usable_balance(vtoken, &ALICE), 0); assert_eq!( ClassLocksFor::::get(&ALICE), diff --git a/pallets/vtoken-voting/src/tests/vbnc_test.rs b/pallets/vtoken-voting/src/tests/vbnc_test.rs index a868ae7627..7e29662427 100644 --- a/pallets/vtoken-voting/src/tests/vbnc_test.rs +++ b/pallets/vtoken-voting/src/tests/vbnc_test.rs @@ -225,7 +225,7 @@ fn ensure_balance_after_unlock() { assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, poll_index)); assert_eq!(usable_balance(vtoken, &ALICE), 0); assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 10); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 10); }); } } @@ -273,7 +273,7 @@ fn ensure_comprehensive_balance_after_unlock() { assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, poll_index)); assert_eq!(usable_balance(vtoken, &ALICE), 8); assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 2); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 2); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 2); assert_ok!(VtokenVoting::vote( RuntimeOrigin::signed(ALICE), @@ -284,7 +284,7 @@ fn ensure_comprehensive_balance_after_unlock() { assert_eq!(usable_balance(vtoken, &ALICE), 0); assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 10); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 10); }); } } @@ -397,12 +397,12 @@ fn lock_amalgamation_valid_with_multiple_removed_votes() { VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0), Error::::NoPermissionYet ); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 10); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 10); assert_eq!(usable_balance(vtoken, &ALICE), 0); System::set_block_number(11); assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0)); - assert_eq!(VotingFor::::get(&ALICE, vtoken).locked_balance(), 10); + assert_eq!(VotingFor::::get(vtoken, &ALICE).locked_balance(), 10); assert_eq!(usable_balance(vtoken, &ALICE), 0); assert_eq!( ClassLocksFor::::get(&ALICE),