From a1f2c2e8df6e22ff6a8f8116b8651d0006b58283 Mon Sep 17 00:00:00 2001 From: SunTiebing <1045060705@qq.com> Date: Thu, 6 Feb 2025 15:37:16 +0800 Subject: [PATCH] adjust fast unlock logic --- pallets/vtoken-voting/src/lib.rs | 4 +- .../src/tests/kusama_common_test.rs | 46 +++++++++++++++++-- .../src/tests/polkadot_common_test.rs | 46 +++++++++++++++++-- pallets/vtoken-voting/src/vote.rs | 10 ++-- 4 files changed, 94 insertions(+), 12 deletions(-) diff --git a/pallets/vtoken-voting/src/lib.rs b/pallets/vtoken-voting/src/lib.rs index 9ec508df31..5358aac99c 100644 --- a/pallets/vtoken-voting/src/lib.rs +++ b/pallets/vtoken-voting/src/lib.rs @@ -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 }; } } diff --git a/pallets/vtoken-voting/src/tests/kusama_common_test.rs b/pallets/vtoken-voting/src/tests/kusama_common_test.rs index 6b86d21064..1a31f22a45 100644 --- a/pallets/vtoken-voting/src/tests/kusama_common_test.rs +++ b/pallets/vtoken-voting/src/tests/kusama_common_test.rs @@ -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 { @@ -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), @@ -1978,7 +2018,7 @@ fn update_referendum_vote_status_without_vote_should_fail() { RuntimeOrigin::root(), WETH, poll_index, - ReferendumVoteStatus::NoResult, + ReferendumVoteStatus::Ongoing, ), Error::::VTokenNotSupport ); @@ -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 ); diff --git a/pallets/vtoken-voting/src/tests/polkadot_common_test.rs b/pallets/vtoken-voting/src/tests/polkadot_common_test.rs index eb33b4b7b1..5a40298a54 100644 --- a/pallets/vtoken-voting/src/tests/polkadot_common_test.rs +++ b/pallets/vtoken-voting/src/tests/polkadot_common_test.rs @@ -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 { @@ -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), @@ -1978,7 +2018,7 @@ fn update_referendum_vote_status_without_vote_should_fail() { RuntimeOrigin::root(), WETH, poll_index, - ReferendumVoteStatus::NoResult, + ReferendumVoteStatus::Ongoing, ), Error::::VTokenNotSupport ); @@ -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 ); diff --git a/pallets/vtoken-voting/src/vote.rs b/pallets/vtoken-voting/src/vote.rs index 69c47147b4..839c120bf5 100644 --- a/pallets/vtoken-voting/src/vote.rs +++ b/pallets/vtoken-voting/src/vote.rs @@ -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 } }