Skip to content

Commit

Permalink
Adjust vtoken voting storage (#1547)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
SunTiebing authored Dec 9, 2024
1 parent 0fc4928 commit 37c87f0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions pallets/vtoken-voting/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod benchmarks {
response.clone(),
)?;
}
let votes = match VotingFor::<T>::get(&caller, vtoken) {
let votes = match VotingFor::<T>::get(vtoken, &caller) {
Voting::Casting(Casting { votes, .. }) => votes,
_ => return Err("Votes are not direct".into()),
};
Expand All @@ -97,7 +97,7 @@ mod benchmarks {
Pallet::<T>::vote(RawOrigin::Signed(caller.clone()), vtoken, poll_index, vote);

assert_matches!(
VotingFor::<T>::get(&caller, vtoken),
VotingFor::<T>::get(vtoken, &caller),
Voting::Casting(Casting { votes, .. }) if votes.len() == (r + 1) as usize
);

Expand All @@ -124,7 +124,7 @@ mod benchmarks {
response.clone(),
)?;
}
let votes = match VotingFor::<T>::get(&caller, vtoken) {
let votes = match VotingFor::<T>::get(vtoken, &caller) {
Voting::Casting(Casting { votes, .. }) => votes,
_ => return Err("Votes are not direct".into()),
};
Expand All @@ -136,7 +136,7 @@ mod benchmarks {
Pallet::<T>::vote(RawOrigin::Signed(caller.clone()), vtoken, poll_index, new_vote);

assert_matches!(
VotingFor::<T>::get(&caller, vtoken),
VotingFor::<T>::get(vtoken, &caller),
Voting::Casting(Casting { votes, .. }) if votes.len() == r as usize
);

Expand Down
10 changes: 5 additions & 5 deletions pallets/vtoken-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,9 @@ pub mod pallet {
pub type VotingFor<T: Config> = StorageDoubleMap<
_,
Twox64Concat,
AccountIdOf<T>,
Twox64Concat,
CurrencyIdOf<T>,
Twox64Concat,
AccountIdOf<T>,
VotingOf<T>,
ValueQuery,
>;
Expand Down Expand Up @@ -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::<T>::NotOngoing)?;
VotingFor::<T>::try_mutate(who, vtoken, |voting| {
VotingFor::<T>::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) => {
Expand Down Expand Up @@ -1131,7 +1131,7 @@ pub mod pallet {
poll_index: PollIndex,
scope: UnvoteScope,
) -> DispatchResult {
VotingFor::<T>::try_mutate(who, vtoken, |voting| {
VotingFor::<T>::try_mutate(vtoken, who, |voting| {
if let Voting::Casting(Casting { ref mut votes, delegations, ref mut prior }) =
voting
{
Expand Down Expand Up @@ -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<T>, vtoken: CurrencyIdOf<T>) -> DispatchResult {
let current_block = Self::get_agent_block_number(&vtoken)?;
let lock_needed = VotingFor::<T>::mutate(who, vtoken, |voting| {
let lock_needed = VotingFor::<T>::mutate(vtoken, who, |voting| {
voting.rejig(current_block);
voting.locked_balance()
});
Expand Down
2 changes: 1 addition & 1 deletion pallets/vtoken-voting/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub fn migrate_to_v4<T: Config, C: Get<CurrencyIdOf<T>>>() -> Weight {

// Iterate over all items in the old storage
v3::VotingFor::<T>::iter().for_each(|(account, voting)| {
VotingFor::<T>::insert(account, vtoken, voting);
VotingFor::<T>::insert(vtoken, account, voting);
weight += T::DbWeight::get().reads_writes(0, 1);
});

Expand Down
10 changes: 5 additions & 5 deletions pallets/vtoken-voting/src/tests/kusama_common_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
});
}
}
Expand Down Expand Up @@ -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::<Runtime>::get(&ALICE, vtoken).locked_balance(), 2);
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 2);

assert_ok!(VtokenVoting::vote(
RuntimeOrigin::signed(ALICE),
Expand All @@ -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::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
});
}
}
Expand Down Expand Up @@ -431,12 +431,12 @@ fn lock_amalgamation_valid_with_multiple_removed_votes() {
VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0),
Error::<Runtime>::NoPermissionYet
);
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
assert_eq!(VotingFor::<Runtime>::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::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
assert_eq!(usable_balance(vtoken, &ALICE), 0);
assert_eq!(
ClassLocksFor::<Runtime>::get(&ALICE),
Expand Down
10 changes: 5 additions & 5 deletions pallets/vtoken-voting/src/tests/polkadot_common_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
});
}
}
Expand Down Expand Up @@ -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::<Runtime>::get(&ALICE, vtoken).locked_balance(), 2);
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 2);

assert_ok!(VtokenVoting::vote(
RuntimeOrigin::signed(ALICE),
Expand All @@ -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::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
});
}
}
Expand Down Expand Up @@ -431,12 +431,12 @@ fn lock_amalgamation_valid_with_multiple_removed_votes() {
VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0),
Error::<Runtime>::NoPermissionYet
);
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
assert_eq!(VotingFor::<Runtime>::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::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
assert_eq!(usable_balance(vtoken, &ALICE), 0);
assert_eq!(
ClassLocksFor::<Runtime>::get(&ALICE),
Expand Down
10 changes: 5 additions & 5 deletions pallets/vtoken-voting/src/tests/vbnc_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
});
}
}
Expand Down Expand Up @@ -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::<Runtime>::get(&ALICE, vtoken).locked_balance(), 2);
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 2);

assert_ok!(VtokenVoting::vote(
RuntimeOrigin::signed(ALICE),
Expand All @@ -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::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
});
}
}
Expand Down Expand Up @@ -397,12 +397,12 @@ fn lock_amalgamation_valid_with_multiple_removed_votes() {
VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0),
Error::<Runtime>::NoPermissionYet
);
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
assert_eq!(VotingFor::<Runtime>::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::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
assert_eq!(usable_balance(vtoken, &ALICE), 0);
assert_eq!(
ClassLocksFor::<Runtime>::get(&ALICE),
Expand Down

0 comments on commit 37c87f0

Please sign in to comment.