Skip to content

Commit 37c87f0

Browse files
authored
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.
1 parent 0fc4928 commit 37c87f0

File tree

6 files changed

+25
-25
lines changed

6 files changed

+25
-25
lines changed

pallets/vtoken-voting/src/benchmarking.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ mod benchmarks {
8787
response.clone(),
8888
)?;
8989
}
90-
let votes = match VotingFor::<T>::get(&caller, vtoken) {
90+
let votes = match VotingFor::<T>::get(vtoken, &caller) {
9191
Voting::Casting(Casting { votes, .. }) => votes,
9292
_ => return Err("Votes are not direct".into()),
9393
};
@@ -97,7 +97,7 @@ mod benchmarks {
9797
Pallet::<T>::vote(RawOrigin::Signed(caller.clone()), vtoken, poll_index, vote);
9898

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

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

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

pallets/vtoken-voting/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ pub mod pallet {
340340
pub type VotingFor<T: Config> = StorageDoubleMap<
341341
_,
342342
Twox64Concat,
343-
AccountIdOf<T>,
344-
Twox64Concat,
345343
CurrencyIdOf<T>,
344+
Twox64Concat,
345+
AccountIdOf<T>,
346346
VotingOf<T>,
347347
ValueQuery,
348348
>;
@@ -1078,7 +1078,7 @@ pub mod pallet {
10781078
let mut total_vote = None;
10791079
Self::try_access_poll(vtoken, poll_index, |poll_status| {
10801080
let tally = poll_status.ensure_ongoing().ok_or(Error::<T>::NotOngoing)?;
1081-
VotingFor::<T>::try_mutate(who, vtoken, |voting| {
1081+
VotingFor::<T>::try_mutate(vtoken, who, |voting| {
10821082
if let Voting::Casting(Casting { ref mut votes, delegations, .. }) = voting {
10831083
match votes.binary_search_by_key(&poll_index, |i| i.0) {
10841084
Ok(i) => {
@@ -1131,7 +1131,7 @@ pub mod pallet {
11311131
poll_index: PollIndex,
11321132
scope: UnvoteScope,
11331133
) -> DispatchResult {
1134-
VotingFor::<T>::try_mutate(who, vtoken, |voting| {
1134+
VotingFor::<T>::try_mutate(vtoken, who, |voting| {
11351135
if let Voting::Casting(Casting { ref mut votes, delegations, ref mut prior }) =
11361136
voting
11371137
{
@@ -1183,7 +1183,7 @@ pub mod pallet {
11831183
/// indicate a security hole) but may be reduced from what they are currently.
11841184
pub(crate) fn update_lock(who: &AccountIdOf<T>, vtoken: CurrencyIdOf<T>) -> DispatchResult {
11851185
let current_block = Self::get_agent_block_number(&vtoken)?;
1186-
let lock_needed = VotingFor::<T>::mutate(who, vtoken, |voting| {
1186+
let lock_needed = VotingFor::<T>::mutate(vtoken, who, |voting| {
11871187
voting.rejig(current_block);
11881188
voting.locked_balance()
11891189
});

pallets/vtoken-voting/src/migration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ pub fn migrate_to_v4<T: Config, C: Get<CurrencyIdOf<T>>>() -> Weight {
290290

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

pallets/vtoken-voting/src/tests/kusama_common_test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn ensure_balance_after_unlock() {
248248
assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, poll_index));
249249
assert_eq!(usable_balance(vtoken, &ALICE), 0);
250250
assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10);
251-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
251+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
252252
});
253253
}
254254
}
@@ -299,7 +299,7 @@ fn ensure_comprehensive_balance_after_unlock() {
299299
assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, poll_index));
300300
assert_eq!(usable_balance(vtoken, &ALICE), 8);
301301
assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 2);
302-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 2);
302+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 2);
303303

304304
assert_ok!(VtokenVoting::vote(
305305
RuntimeOrigin::signed(ALICE),
@@ -311,7 +311,7 @@ fn ensure_comprehensive_balance_after_unlock() {
311311

312312
assert_eq!(usable_balance(vtoken, &ALICE), 0);
313313
assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10);
314-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
314+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
315315
});
316316
}
317317
}
@@ -431,12 +431,12 @@ fn lock_amalgamation_valid_with_multiple_removed_votes() {
431431
VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0),
432432
Error::<Runtime>::NoPermissionYet
433433
);
434-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
434+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
435435
assert_eq!(usable_balance(vtoken, &ALICE), 0);
436436

437437
RelaychainDataProvider::set_block_number(11);
438438
assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0));
439-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
439+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
440440
assert_eq!(usable_balance(vtoken, &ALICE), 0);
441441
assert_eq!(
442442
ClassLocksFor::<Runtime>::get(&ALICE),

pallets/vtoken-voting/src/tests/polkadot_common_test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn ensure_balance_after_unlock() {
248248
assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, poll_index));
249249
assert_eq!(usable_balance(vtoken, &ALICE), 0);
250250
assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10);
251-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
251+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
252252
});
253253
}
254254
}
@@ -299,7 +299,7 @@ fn ensure_comprehensive_balance_after_unlock() {
299299
assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, poll_index));
300300
assert_eq!(usable_balance(vtoken, &ALICE), 8);
301301
assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 2);
302-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 2);
302+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 2);
303303

304304
assert_ok!(VtokenVoting::vote(
305305
RuntimeOrigin::signed(ALICE),
@@ -311,7 +311,7 @@ fn ensure_comprehensive_balance_after_unlock() {
311311

312312
assert_eq!(usable_balance(vtoken, &ALICE), 0);
313313
assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10);
314-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
314+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
315315
});
316316
}
317317
}
@@ -431,12 +431,12 @@ fn lock_amalgamation_valid_with_multiple_removed_votes() {
431431
VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0),
432432
Error::<Runtime>::NoPermissionYet
433433
);
434-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
434+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
435435
assert_eq!(usable_balance(vtoken, &ALICE), 0);
436436

437437
RelaychainDataProvider::set_block_number(11);
438438
assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0));
439-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
439+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
440440
assert_eq!(usable_balance(vtoken, &ALICE), 0);
441441
assert_eq!(
442442
ClassLocksFor::<Runtime>::get(&ALICE),

pallets/vtoken-voting/src/tests/vbnc_test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn ensure_balance_after_unlock() {
225225
assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, poll_index));
226226
assert_eq!(usable_balance(vtoken, &ALICE), 0);
227227
assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10);
228-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
228+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
229229
});
230230
}
231231
}
@@ -273,7 +273,7 @@ fn ensure_comprehensive_balance_after_unlock() {
273273
assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, poll_index));
274274
assert_eq!(usable_balance(vtoken, &ALICE), 8);
275275
assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 2);
276-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 2);
276+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 2);
277277

278278
assert_ok!(VtokenVoting::vote(
279279
RuntimeOrigin::signed(ALICE),
@@ -284,7 +284,7 @@ fn ensure_comprehensive_balance_after_unlock() {
284284

285285
assert_eq!(usable_balance(vtoken, &ALICE), 0);
286286
assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10);
287-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
287+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
288288
});
289289
}
290290
}
@@ -397,12 +397,12 @@ fn lock_amalgamation_valid_with_multiple_removed_votes() {
397397
VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0),
398398
Error::<Runtime>::NoPermissionYet
399399
);
400-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
400+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
401401
assert_eq!(usable_balance(vtoken, &ALICE), 0);
402402

403403
System::set_block_number(11);
404404
assert_ok!(VtokenVoting::unlock(RuntimeOrigin::signed(ALICE), vtoken, 0));
405-
assert_eq!(VotingFor::<Runtime>::get(&ALICE, vtoken).locked_balance(), 10);
405+
assert_eq!(VotingFor::<Runtime>::get(vtoken, &ALICE).locked_balance(), 10);
406406
assert_eq!(usable_balance(vtoken, &ALICE), 0);
407407
assert_eq!(
408408
ClassLocksFor::<Runtime>::get(&ALICE),

0 commit comments

Comments
 (0)