From 8dd442d08cc9dc4bac400b257ab64b49f971b4fd Mon Sep 17 00:00:00 2001 From: SunTiebing <1045060705@qq.com> Date: Thu, 13 Jun 2024 11:30:51 +0800 Subject: [PATCH 1/2] update the exchange rate of vtokenminting of vtokenvoting test --- pallets/vtoken-voting/src/mock.rs | 3 +- pallets/vtoken-voting/src/tests.rs | 54 +++++++++++++++--------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/pallets/vtoken-voting/src/mock.rs b/pallets/vtoken-voting/src/mock.rs index f7d01b235..686900e18 100644 --- a/pallets/vtoken-voting/src/mock.rs +++ b/pallets/vtoken-voting/src/mock.rs @@ -290,7 +290,8 @@ impl BlockNumberProvider for RelaychainDataProvider { } parameter_types! { - pub static VTokenSupply: Balance = u64::MAX.into(); + // modify TokenSupply to be twice that of VTokenSupply, making the exchange rate for vtokenming 1:2 + pub static VTokenSupply: Balance = u64::MAX.checked_div(2u64).unwrap().into(); pub static TokenSupply: Balance = u64::MAX.into(); } diff --git a/pallets/vtoken-voting/src/tests.rs b/pallets/vtoken-voting/src/tests.rs index 8f0fac3cb..98511710c 100644 --- a/pallets/vtoken-voting/src/tests.rs +++ b/pallets/vtoken-voting/src/tests.rs @@ -71,13 +71,13 @@ fn basic_voting_works() { let vtoken = VKSM; assert_ok!(VtokenVoting::vote(RuntimeOrigin::signed(ALICE), vtoken, poll_index, aye(2, 5))); - assert_eq!(tally(vtoken, poll_index), Tally::from_parts(10, 0, 2)); + 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(2, 5), - delegator_vote: aye(100, 0), + token_vote: aye(4, 5), + delegator_vote: aye(200, 0), })); assert_ok!(VtokenVoting::notify_vote(origin_response(), 0, response_success())); @@ -101,7 +101,7 @@ fn voting_balance_gets_locked() { poll_index, nay(10, 0) )); - assert_eq!(tally(vtoken, poll_index), Tally::from_parts(0, 1, 0)); + assert_eq!(tally(vtoken, poll_index), Tally::from_parts(0, 2, 0)); assert_ok!(VtokenVoting::notify_vote(origin_response(), 0, response_success())); assert_eq!(usable_balance(vtoken, &ALICE), 0); @@ -614,25 +614,25 @@ fn notify_vote_success_works() { ReferendumInfoFor::::get(vtoken, poll_index), Some(ReferendumInfo::Ongoing(ReferendumStatus { submitted: None, - tally: TallyOf::::from_parts(10, 0, 2), + tally: TallyOf::::from_parts(20, 0, 4), })) ); assert_eq!( PendingDelegatorVotes::::get(vtoken, poll_index), BoundedVec::<(DerivativeIndex, AccountVote), ConstU32<100>>::try_from(vec![( derivative_index, - aye(100, 0) + aye(200, 0) )]) .unwrap() ); assert_eq!(DelegatorVotes::::get(vtoken, poll_index).len(), 0); - assert_eq!(tally(vtoken, poll_index), Tally::from_parts(10, 0, 2)); + 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(2, 5), - delegator_vote: aye(100, 0), + token_vote: aye(4, 5), + delegator_vote: aye(200, 0), })); assert_ok!(VtokenVoting::notify_vote(origin_response(), query_id, response.clone())); @@ -640,7 +640,7 @@ fn notify_vote_success_works() { ReferendumInfoFor::::get(vtoken, poll_index), Some(ReferendumInfo::Ongoing(ReferendumStatus { submitted: Some(1), - tally: TallyOf::::from_parts(10, 0, 2), + tally: TallyOf::::from_parts(20, 0, 4), })) ); assert_eq!(PendingDelegatorVotes::::get(vtoken, poll_index).len(), 0); @@ -648,7 +648,7 @@ fn notify_vote_success_works() { DelegatorVotes::::get(vtoken, poll_index), BoundedVec::<(DerivativeIndex, AccountVote), ConstU32<100>>::try_from(vec![( derivative_index, - aye(100, 0) + aye(200, 0) )]) .unwrap() ); @@ -662,7 +662,7 @@ fn notify_vote_success_works() { poll_index, info: ReferendumInfo::Ongoing(ReferendumStatus { submitted: Some(1), - tally: TallyOf::::from_parts(10, 0, 2), + tally: TallyOf::::from_parts(20, 0, 4), }), })); System::assert_last_event(RuntimeEvent::VtokenVoting(Event::ResponseReceived { @@ -742,7 +742,7 @@ fn notify_vote_fail_works() { ReferendumInfoFor::::get(vtoken, poll_index), Some(ReferendumInfo::Ongoing(ReferendumStatus { submitted: None, - tally: TallyOf::::from_parts(10, 0, 2), + tally: TallyOf::::from_parts(20, 0, 4), })) ); assert_eq!(DelegatorVotes::::get(vtoken, poll_index).len(), 0); @@ -750,17 +750,17 @@ fn notify_vote_fail_works() { PendingDelegatorVotes::::get(vtoken, poll_index), BoundedVec::<(DerivativeIndex, AccountVote), ConstU32<100>>::try_from(vec![( derivative_index, - aye(100, 0) + aye(200, 0) )]) .unwrap() ); - assert_eq!(tally(vtoken, poll_index), Tally::from_parts(10, 0, 2)); + 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(2, 5), - delegator_vote: aye(100, 0), + token_vote: aye(4, 5), + delegator_vote: aye(200, 0), })); assert_ok!(VtokenVoting::notify_vote(origin_response(), query_id, response.clone())); @@ -805,24 +805,24 @@ fn notify_remove_delegator_vote_success_works() { PendingDelegatorVotes::::get(vtoken, poll_index), BoundedVec::<(DerivativeIndex, AccountVote), ConstU32<100>>::try_from(vec![( derivative_index, - aye(100, 0) + aye(200, 0) )]) .unwrap() ); - assert_eq!(tally(vtoken, poll_index), Tally::from_parts(10, 0, 2)); + 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(2, 5), - delegator_vote: aye(100, 0), + token_vote: aye(4, 5), + delegator_vote: aye(200, 0), })); assert_ok!(VtokenVoting::notify_vote(origin_response(), query_id, response.clone())); assert_eq!( DelegatorVotes::::get(vtoken, poll_index), BoundedVec::<(DerivativeIndex, AccountVote), ConstU32<100>>::try_from(vec![( derivative_index, - aye(100, 0) + aye(200, 0) )]) .unwrap() ); @@ -880,24 +880,24 @@ fn notify_remove_delegator_vote_fail_works() { PendingDelegatorVotes::::get(vtoken, poll_index), BoundedVec::<(DerivativeIndex, AccountVote), ConstU32<100>>::try_from(vec![( derivative_index, - aye(100, 0) + aye(200, 0) )]) .unwrap() ); - assert_eq!(tally(vtoken, poll_index), Tally::from_parts(10, 0, 2)); + 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(2, 5), - delegator_vote: aye(100, 0), + token_vote: aye(4, 5), + delegator_vote: aye(200, 0), })); assert_ok!(VtokenVoting::notify_vote(origin_response(), query_id, response_success())); assert_eq!( DelegatorVotes::::get(vtoken, poll_index), BoundedVec::<(DerivativeIndex, AccountVote), ConstU32<100>>::try_from(vec![( derivative_index, - aye(100, 0) + aye(200, 0) )]) .unwrap() ); From f5169c94e68ae5c016d2952cc678c2aa6686a367 Mon Sep 17 00:00:00 2001 From: SunTiebing <1045060705@qq.com> Date: Thu, 13 Jun 2024 11:32:52 +0800 Subject: [PATCH 2/2] Resolve the frozen anomaly when unlocking --- pallets/vtoken-voting/src/tests.rs | 99 ++++++++++++++++++++++++++++++ pallets/vtoken-voting/src/vote.rs | 2 +- 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/pallets/vtoken-voting/src/tests.rs b/pallets/vtoken-voting/src/tests.rs index 98511710c..48f204fa2 100644 --- a/pallets/vtoken-voting/src/tests.rs +++ b/pallets/vtoken-voting/src/tests.rs @@ -177,6 +177,105 @@ fn unsuccessful_conviction_vote_balance_can_be_unlocked() { }); } +#[test] +fn ensure_balance_after_unlock() { + new_test_ext().execute_with(|| { + let poll_index = 3; + let poll_index_2 = 4; + let vtoken = VKSM; + let locking_period = 10; + assert_ok!(VtokenVoting::set_vote_locking_period( + RuntimeOrigin::root(), + vtoken, + locking_period, + )); + + assert_ok!(VtokenVoting::vote( + RuntimeOrigin::signed(ALICE), + vtoken, + poll_index, + aye(10, 1) + )); + assert_ok!(VtokenVoting::notify_vote(origin_response(), 0, response_success())); + assert_ok!(VtokenVoting::vote( + RuntimeOrigin::signed(ALICE), + vtoken, + poll_index_2, + aye(10, 5) + )); + assert_ok!(VtokenVoting::notify_vote(origin_response(), 1, response_success())); + + assert_ok!(VtokenVoting::set_referendum_status( + RuntimeOrigin::root(), + vtoken, + poll_index, + ReferendumInfoOf::::Completed(3), + )); + RelaychainDataProvider::set_block_number(13); + 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::::get(&ALICE).locked_balance(), 10); + }); +} + +#[test] +fn ensure_comprehensive_balance_after_unlock() { + new_test_ext().execute_with(|| { + let poll_index = 3; + let poll_index_2 = 4; + let poll_index_3 = 5; + let vtoken = VKSM; + let locking_period = 10; + assert_ok!(VtokenVoting::set_vote_locking_period( + RuntimeOrigin::root(), + vtoken, + locking_period, + )); + + assert_ok!(VtokenVoting::vote(RuntimeOrigin::signed(ALICE), vtoken, poll_index, aye(2, 1))); + assert_ok!(VtokenVoting::notify_vote(origin_response(), 0, response_success())); + assert_ok!(VtokenVoting::vote( + RuntimeOrigin::signed(ALICE), + vtoken, + poll_index_2, + aye(1, 5) + )); + assert_ok!(VtokenVoting::notify_vote(origin_response(), 1, response_success())); + assert_ok!(VtokenVoting::vote( + RuntimeOrigin::signed(ALICE), + vtoken, + poll_index_3, + aye(2, 5) + )); + assert_ok!(VtokenVoting::notify_vote(origin_response(), 2, response_success())); + + assert_ok!(VtokenVoting::set_referendum_status( + RuntimeOrigin::root(), + vtoken, + poll_index, + ReferendumInfoOf::::Completed(3), + )); + RelaychainDataProvider::set_block_number(13); + 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::::get(&ALICE).locked_balance(), 2); + + assert_ok!(VtokenVoting::vote( + RuntimeOrigin::signed(ALICE), + vtoken, + poll_index_2, + aye(10, 5) + )); + assert_ok!(VtokenVoting::notify_vote(origin_response(), 3, response_success())); + + assert_eq!(usable_balance(vtoken, &ALICE), 0); + assert_eq!(Tokens::accounts(&ALICE, vtoken).frozen, 10); + assert_eq!(VotingFor::::get(&ALICE).locked_balance(), 10); + }); +} + #[test] fn successful_conviction_vote_balance_stays_locked_for_correct_time() { new_test_ext().execute_with(|| { diff --git a/pallets/vtoken-voting/src/vote.rs b/pallets/vtoken-voting/src/vote.rs index 2797fe136..a9a9b1b86 100644 --- a/pallets/vtoken-voting/src/vote.rs +++ b/pallets/vtoken-voting/src/vote.rs @@ -415,7 +415,7 @@ where pub fn locked_balance(&self) -> Balance { match self { Voting::Casting(Casting { votes, prior, .. }) => - votes.iter().map(|i| i.1.balance()).fold(prior.locked(), |a, i| a.max(i)), + votes.iter().map(|i| i.3).fold(prior.locked(), |a, i| a.max(i)), Voting::Delegating(Delegating { balance, prior, .. }) => *balance.max(&prior.locked()), } }