Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust fast unlock logic #1648

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pallets/vtoken-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,9 @@ pub mod pallet {
return match (is_aye, vote_status) {
(true, ReferendumVoteStatus::Rejected) => Ok(true), // Voted Aye but the referendum was rejected
(false, ReferendumVoteStatus::Approved) => Ok(true), // Voted Nay but the referendum was approved
_ => Ok(false), // No early unlock allowed in other cases
(_, ReferendumVoteStatus::Ongoing) => Ok(false), // No early unlock allowed in Ongoing cases
(_, ReferendumVoteStatus::None) => Ok(true), // Early unlock allowed in None cases
_ => Ok(false), // No early unlock allowed in other cases
};
}
}
Expand Down
46 changes: 43 additions & 3 deletions pallets/vtoken-voting/src/tests/kusama_common_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,46 @@ fn early_unlock_works() {
}
}

#[test]
fn early_unlock_works_with_none_status() {
for &vtoken in TOKENS {
new_test_ext().execute_with(|| {
let poll_index = 3;

assert_ok!(VtokenVoting::vote(
RuntimeOrigin::signed(ALICE),
vtoken,
poll_index,
aye(2, 5)
));
assert_eq!(tally(vtoken, poll_index), Tally::from_parts(20, 0, 4));
System::assert_last_event(RuntimeEvent::VtokenVoting(Event::Voted {
who: ALICE,
vtoken,
poll_index,
token_vote: aye(4, 5),
delegator_vote: aye(200, 0),
}));
assert_ok!(VtokenVoting::notify_vote(
origin_response(),
0,
response_success()
));

assert_ok!(VtokenVoting::update_referendum_vote_status(
RuntimeOrigin::root(),
vtoken,
poll_index,
ReferendumVoteStatus::None,
));

assert_eq!(usable_balance(vtoken, &ALICE), 8);
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, poll_index));
assert_eq!(usable_balance(vtoken, &ALICE), 10);
});
}
}

#[test]
fn early_unlock_fails() {
for &vtoken in TOKENS {
Expand Down Expand Up @@ -1939,7 +1979,7 @@ fn update_referendum_vote_status_works() {
RuntimeOrigin::root(),
vtoken,
poll_index,
ReferendumVoteStatus::NoResult,
ReferendumVoteStatus::Ongoing,
));
assert_ok!(VtokenVoting::vote(
RuntimeOrigin::signed(ALICE),
Expand Down Expand Up @@ -1978,7 +2018,7 @@ fn update_referendum_vote_status_without_vote_should_fail() {
RuntimeOrigin::root(),
WETH,
poll_index,
ReferendumVoteStatus::NoResult,
ReferendumVoteStatus::Ongoing,
),
Error::<Runtime>::VTokenNotSupport
);
Expand All @@ -1996,7 +2036,7 @@ fn update_referendum_vote_status_with_origin_signed_should_fail() {
RuntimeOrigin::signed(ALICE),
vtoken,
poll_index,
ReferendumVoteStatus::NoResult,
ReferendumVoteStatus::Ongoing,
),
DispatchError::BadOrigin
);
Expand Down
46 changes: 43 additions & 3 deletions pallets/vtoken-voting/src/tests/polkadot_common_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,46 @@ fn early_unlock_works() {
}
}

#[test]
fn early_unlock_works_with_none_status() {
for &vtoken in TOKENS {
new_test_ext().execute_with(|| {
let poll_index = 3;

assert_ok!(VtokenVoting::vote(
RuntimeOrigin::signed(ALICE),
vtoken,
poll_index,
aye(2, 5)
));
assert_eq!(tally(vtoken, poll_index), Tally::from_parts(20, 0, 4));
System::assert_last_event(RuntimeEvent::VtokenVoting(Event::Voted {
who: ALICE,
vtoken,
poll_index,
token_vote: aye(4, 5),
delegator_vote: aye(200, 0),
}));
assert_ok!(VtokenVoting::notify_vote(
origin_response(),
0,
response_success()
));

assert_ok!(VtokenVoting::update_referendum_vote_status(
RuntimeOrigin::root(),
vtoken,
poll_index,
ReferendumVoteStatus::None,
));

assert_eq!(usable_balance(vtoken, &ALICE), 8);
assert_ok!(VtokenVoting::update_lock(&ALICE, vtoken, poll_index));
assert_eq!(usable_balance(vtoken, &ALICE), 10);
});
}
}

#[test]
fn early_unlock_fails() {
for &vtoken in TOKENS {
Expand Down Expand Up @@ -1939,7 +1979,7 @@ fn update_referendum_vote_status_works() {
RuntimeOrigin::root(),
vtoken,
poll_index,
ReferendumVoteStatus::NoResult,
ReferendumVoteStatus::Ongoing,
));
assert_ok!(VtokenVoting::vote(
RuntimeOrigin::signed(ALICE),
Expand Down Expand Up @@ -1978,7 +2018,7 @@ fn update_referendum_vote_status_without_vote_should_fail() {
RuntimeOrigin::root(),
WETH,
poll_index,
ReferendumVoteStatus::NoResult,
ReferendumVoteStatus::Ongoing,
),
Error::<Runtime>::VTokenNotSupport
);
Expand All @@ -1996,7 +2036,7 @@ fn update_referendum_vote_status_with_origin_signed_should_fail() {
RuntimeOrigin::signed(ALICE),
vtoken,
poll_index,
ReferendumVoteStatus::NoResult,
ReferendumVoteStatus::Ongoing,
),
DispatchError::BadOrigin
);
Expand Down
10 changes: 5 additions & 5 deletions pallets/vtoken-voting/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,14 +736,14 @@ pub enum ReferendumVoteStatus {
Approved,
/// The referendum proposal was rejected.
Rejected,
/// The referendum proposal timed out without a decision.
TimedOut,
/// No result is currently available for the referendum proposal.
NoResult,
/// The referendum proposal was Cancelled/TimeOut/Killed.
None,
/// Ongoing is currently available for the referendum proposal.
Ongoing,
}

impl Default for ReferendumVoteStatus {
fn default() -> Self {
ReferendumVoteStatus::NoResult
ReferendumVoteStatus::Ongoing
}
}