Skip to content

Commit 8053e9a

Browse files
authored
Optimize vtokenvoting early unlock (#1875)
* Refactor `update_lock` function to remove unnecessary poll index parameter * Remove unused `poll_indexes` vector in `lib.rs`
1 parent 71e8e54 commit 8053e9a

File tree

4 files changed

+46
-48
lines changed

4 files changed

+46
-48
lines changed

pallets/vtoken-voting/src/lib.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ pub mod pallet {
648648
}
649649

650650
Self::try_remove_vote(&who, vtoken, maybe_poll_index, UnvoteScope::OnlyExpired)?;
651-
Self::update_lock(&who, vtoken, maybe_poll_index.into_iter().collect())?;
651+
Self::update_lock(&who, vtoken)?;
652652

653653
Self::deposit_event(Event::<T>::Unlocked {
654654
who,
@@ -1008,16 +1008,14 @@ pub mod pallet {
10081008
// Only delete the data of a specific chain, without affecting other chains.
10091009
let _ = PendingDelegatorVotes::<T>::clear_prefix(vtoken, u32::MAX, None);
10101010

1011-
let mut poll_indexes: Vec<PollIndex> = Vec::new();
10121011
for (poll_index, maybe_vote) in maybe_old_vote.iter() {
10131012
Self::try_remove_vote(&who, vtoken, Some(*poll_index), UnvoteScope::Any)?;
10141013
if let Some((old_vote, vtoken_balance)) = maybe_vote {
10151014
Self::try_vote(&who, vtoken, *poll_index, *old_vote, *vtoken_balance)?;
10161015
}
10171016
ReferendumInfoFor::<T>::remove(vtoken, poll_index);
1018-
poll_indexes.push(*poll_index);
10191017
}
1020-
Self::update_lock(&who, vtoken, poll_indexes)?;
1018+
Self::update_lock(&who, vtoken)?;
10211019
} else {
10221020
for (poll_index, _) in maybe_old_vote.iter() {
10231021
DelegatorVotes::<T>::remove(vtoken, poll_index);
@@ -1381,36 +1379,14 @@ pub mod pallet {
13811379

13821380
/// Rejig the lock on an account. It will never get more stringent (since that would
13831381
/// indicate a security hole) but may be reduced from what they are currently.
1384-
pub(crate) fn update_lock(
1385-
who: &AccountIdOf<T>,
1386-
vtoken: CurrencyIdOf<T>,
1387-
poll_indexes: Vec<PollIndex>,
1388-
) -> DispatchResult {
1382+
pub(crate) fn update_lock(who: &AccountIdOf<T>, vtoken: CurrencyIdOf<T>) -> DispatchResult {
13891383
let current_block = Self::get_agent_block_number(&vtoken)?;
13901384
let lock_needed = VotingForV2::<T>::mutate(vtoken, who, |voting| {
13911385
voting.rejig(current_block);
13921386
voting.locked_balance()
13931387
});
13941388

1395-
// TODO 当用户已经有锁定时(vtoken vote),再去对新提案投票,新提案结果符合提前解锁条件时,那么用户余额将全部解锁,包含之前的锁定
1396-
// If any return is false, the overall return is false; if it is empty, it returns false.
1397-
let can_unlock_early = {
1398-
if poll_indexes.is_empty() {
1399-
false
1400-
} else {
1401-
let mut ok = true;
1402-
for &poll_index in &poll_indexes {
1403-
let allowed = Self::ensure_early_unlock(who, vtoken, poll_index)?;
1404-
if !allowed {
1405-
ok = false;
1406-
break;
1407-
}
1408-
}
1409-
ok
1410-
}
1411-
};
1412-
1413-
if lock_needed.is_zero() || can_unlock_early {
1389+
if lock_needed.is_zero() {
14141390
ClassLocksFor::<T>::mutate(who, |locks| {
14151391
locks.retain(|x| x.0 != vtoken);
14161392
});

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn basic_voting_works() {
131131
));
132132
assert_eq!(tally(vtoken, poll_index), Tally::from_parts(0, 0, 0));
133133

134-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
134+
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken));
135135
assert_eq!(usable_balance(vtoken, &ALICE), 10);
136136
});
137137
}
@@ -165,7 +165,7 @@ fn voting_balance_gets_locked() {
165165
));
166166
assert_eq!(tally(vtoken, poll_index), Tally::from_parts(0, 0, 0));
167167

168-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
168+
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken));
169169
assert_eq!(usable_balance(vtoken, &ALICE), 10);
170170
});
171171
}
@@ -293,7 +293,7 @@ fn unsuccessful_conviction_vote_balance_can_be_unlocked() {
293293
Some(poll_index),
294294
UnvoteScope::Any
295295
));
296-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
296+
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken));
297297
assert_eq!(usable_balance(vtoken, &ALICE), 10);
298298
});
299299
}
@@ -486,7 +486,7 @@ fn successful_conviction_vote_balance_stays_locked_for_correct_time() {
486486
));
487487
}
488488
for i in 1..=5 {
489-
assert_ok!(VtokenVoting::update_lock(&i, vtoken, vec!(poll_index)));
489+
assert_ok!(VtokenVoting::update_lock(&i, vtoken));
490490
assert_eq!(usable_balance(vtoken, &i), 10 * i as u128);
491491
}
492492
});
@@ -1953,7 +1953,11 @@ fn early_unlock_works() {
19531953
));
19541954

19551955
assert_eq!(usable_balance(vtoken, &ALICE), 8);
1956-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
1956+
assert_ok!(VtokenVoting::unlock(
1957+
RuntimeOrigin::signed(ALICE),
1958+
vtoken,
1959+
Some(poll_index)
1960+
));
19571961
assert_eq!(usable_balance(vtoken, &ALICE), 10);
19581962
});
19591963
}
@@ -1966,6 +1970,12 @@ fn early_unlock_works_with_none_status() {
19661970
let poll_index = 3;
19671971
let derivative_index: DerivativeIndex = 0;
19681972

1973+
assert_ok!(VtokenVoting::set_vote_locking_period(
1974+
RuntimeOrigin::root(),
1975+
vtoken,
1976+
100,
1977+
));
1978+
19691979
assert_ok!(VtokenVoting::vote(
19701980
RuntimeOrigin::signed(ALICE),
19711981
vtoken,
@@ -1997,7 +2007,11 @@ fn early_unlock_works_with_none_status() {
19972007
));
19982008

19992009
assert_eq!(usable_balance(vtoken, &ALICE), 8);
2000-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
2010+
assert_ok!(VtokenVoting::unlock(
2011+
RuntimeOrigin::signed(ALICE),
2012+
vtoken,
2013+
Some(poll_index)
2014+
));
20012015
assert_eq!(usable_balance(vtoken, &ALICE), 10);
20022016
});
20032017
}
@@ -2041,7 +2055,7 @@ fn early_unlock_fails() {
20412055
));
20422056

20432057
assert_eq!(usable_balance(vtoken, &ALICE), 8);
2044-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
2058+
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken));
20452059
assert_eq!(usable_balance(vtoken, &ALICE), 8);
20462060
});
20472061
}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn basic_voting_works() {
131131
));
132132
assert_eq!(tally(vtoken, poll_index), Tally::from_parts(0, 0, 0));
133133

134-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
134+
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken));
135135
assert_eq!(usable_balance(vtoken, &ALICE), 10);
136136
});
137137
}
@@ -165,7 +165,7 @@ fn voting_balance_gets_locked() {
165165
));
166166
assert_eq!(tally(vtoken, poll_index), Tally::from_parts(0, 0, 0));
167167

168-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
168+
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken));
169169
assert_eq!(usable_balance(vtoken, &ALICE), 10);
170170
});
171171
}
@@ -293,7 +293,7 @@ fn unsuccessful_conviction_vote_balance_can_be_unlocked() {
293293
Some(poll_index),
294294
UnvoteScope::Any
295295
));
296-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
296+
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken));
297297
assert_eq!(usable_balance(vtoken, &ALICE), 10);
298298
});
299299
}
@@ -486,7 +486,7 @@ fn successful_conviction_vote_balance_stays_locked_for_correct_time() {
486486
));
487487
}
488488
for i in 1..=5 {
489-
assert_ok!(VtokenVoting::update_lock(&i, vtoken, vec!(poll_index)));
489+
assert_ok!(VtokenVoting::update_lock(&i, vtoken));
490490
assert_eq!(usable_balance(vtoken, &i), 10 * i as u128);
491491
}
492492
});
@@ -1952,7 +1952,11 @@ fn early_unlock_works() {
19521952
));
19531953

19541954
assert_eq!(usable_balance(vtoken, &ALICE), 8);
1955-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
1955+
assert_ok!(VtokenVoting::unlock(
1956+
RuntimeOrigin::signed(ALICE),
1957+
vtoken,
1958+
Some(poll_index)
1959+
));
19561960
assert_eq!(usable_balance(vtoken, &ALICE), 10);
19571961
});
19581962
}
@@ -1996,7 +2000,11 @@ fn early_unlock_works_with_none_status() {
19962000
));
19972001

19982002
assert_eq!(usable_balance(vtoken, &ALICE), 8);
1999-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
2003+
assert_ok!(VtokenVoting::unlock(
2004+
RuntimeOrigin::signed(ALICE),
2005+
vtoken,
2006+
Some(poll_index)
2007+
));
20002008
assert_eq!(usable_balance(vtoken, &ALICE), 10);
20012009
});
20022010
}
@@ -2040,7 +2048,7 @@ fn early_unlock_fails() {
20402048
));
20412049

20422050
assert_eq!(usable_balance(vtoken, &ALICE), 8);
2043-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
2051+
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken));
20442052
assert_eq!(usable_balance(vtoken, &ALICE), 8);
20452053
});
20462054
}
@@ -2171,7 +2179,7 @@ fn voting_with_delegate() {
21712179
));
21722180
assert_eq!(tally(vtoken, poll_index), Tally::from_parts(0, 0, 0));
21732181

2174-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
2182+
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken));
21752183
assert_eq!(usable_balance(vtoken, &ALICE), 10);
21762184

21772185
assert_ok!(VtokenVoting::undelegate(RuntimeOrigin::signed(BOB), vtoken));

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn basic_voting_works() {
123123
));
124124
assert_eq!(tally(vtoken, poll_index), Tally::from_parts(0, 0, 0));
125125

126-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
126+
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken));
127127
assert_eq!(usable_balance(vtoken, &ALICE), 10);
128128
});
129129
}
@@ -152,7 +152,7 @@ fn voting_balance_gets_locked() {
152152
));
153153
assert_eq!(tally(vtoken, poll_index), Tally::from_parts(0, 0, 0));
154154

155-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
155+
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken));
156156
assert_eq!(usable_balance(vtoken, &ALICE), 10);
157157
});
158158
}
@@ -255,7 +255,7 @@ fn unsuccessful_conviction_vote_balance_can_be_unlocked() {
255255
Some(poll_index),
256256
UnvoteScope::Any
257257
));
258-
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, vec!(poll_index)));
258+
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken));
259259
assert_eq!(usable_balance(vtoken, &ALICE), 10);
260260
});
261261
}
@@ -413,7 +413,7 @@ fn successful_conviction_vote_balance_stays_locked_for_correct_time() {
413413
));
414414
}
415415
for i in 1..=5 {
416-
assert_ok!(VtokenVoting::update_lock(&i, vtoken, vec!(poll_index)));
416+
assert_ok!(VtokenVoting::update_lock(&i, vtoken));
417417
assert_eq!(usable_balance(vtoken, &i), 10 * i as u128);
418418
}
419419
});
@@ -1440,7 +1440,7 @@ fn batch_voting_works() {
14401440
));
14411441
assert_eq!(tally(vtoken, poll_index), Tally::from_parts(0, 0, 0));
14421442

1443-
assert_ok!(VtokenVoting::update_lock(&FERDIE, vtoken, vec!(poll_index)));
1443+
assert_ok!(VtokenVoting::update_lock(&FERDIE, vtoken));
14441444
assert_eq!(usable_balance(vtoken, &FERDIE), 3000);
14451445
});
14461446
}

0 commit comments

Comments
 (0)