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

Fix/fix vtoken voting unlock #1267

Merged
merged 2 commits into from
Jun 13, 2024
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
3 changes: 2 additions & 1 deletion pallets/vtoken-voting/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
153 changes: 126 additions & 27 deletions pallets/vtoken-voting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand All @@ -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);

Expand Down Expand Up @@ -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::<Runtime>::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::<Runtime>::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::<Runtime>::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::<Runtime>::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::<Runtime>::get(&ALICE).locked_balance(), 10);
});
}

#[test]
fn successful_conviction_vote_balance_stays_locked_for_correct_time() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -614,41 +713,41 @@ fn notify_vote_success_works() {
ReferendumInfoFor::<Runtime>::get(vtoken, poll_index),
Some(ReferendumInfo::Ongoing(ReferendumStatus {
submitted: None,
tally: TallyOf::<Runtime>::from_parts(10, 0, 2),
tally: TallyOf::<Runtime>::from_parts(20, 0, 4),
}))
);
assert_eq!(
PendingDelegatorVotes::<Runtime>::get(vtoken, poll_index),
BoundedVec::<(DerivativeIndex, AccountVote<Balance>), ConstU32<100>>::try_from(vec![(
derivative_index,
aye(100, 0)
aye(200, 0)
)])
.unwrap()
);
assert_eq!(DelegatorVotes::<Runtime>::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()));
assert_eq!(
ReferendumInfoFor::<Runtime>::get(vtoken, poll_index),
Some(ReferendumInfo::Ongoing(ReferendumStatus {
submitted: Some(1),
tally: TallyOf::<Runtime>::from_parts(10, 0, 2),
tally: TallyOf::<Runtime>::from_parts(20, 0, 4),
}))
);
assert_eq!(PendingDelegatorVotes::<Runtime>::get(vtoken, poll_index).len(), 0);
assert_eq!(
DelegatorVotes::<Runtime>::get(vtoken, poll_index),
BoundedVec::<(DerivativeIndex, AccountVote<Balance>), ConstU32<100>>::try_from(vec![(
derivative_index,
aye(100, 0)
aye(200, 0)
)])
.unwrap()
);
Expand All @@ -662,7 +761,7 @@ fn notify_vote_success_works() {
poll_index,
info: ReferendumInfo::Ongoing(ReferendumStatus {
submitted: Some(1),
tally: TallyOf::<Runtime>::from_parts(10, 0, 2),
tally: TallyOf::<Runtime>::from_parts(20, 0, 4),
}),
}));
System::assert_last_event(RuntimeEvent::VtokenVoting(Event::ResponseReceived {
Expand Down Expand Up @@ -742,25 +841,25 @@ fn notify_vote_fail_works() {
ReferendumInfoFor::<Runtime>::get(vtoken, poll_index),
Some(ReferendumInfo::Ongoing(ReferendumStatus {
submitted: None,
tally: TallyOf::<Runtime>::from_parts(10, 0, 2),
tally: TallyOf::<Runtime>::from_parts(20, 0, 4),
}))
);
assert_eq!(DelegatorVotes::<Runtime>::get(vtoken, poll_index).len(), 0);
assert_eq!(
PendingDelegatorVotes::<Runtime>::get(vtoken, poll_index),
BoundedVec::<(DerivativeIndex, AccountVote<Balance>), 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()));
Expand Down Expand Up @@ -805,24 +904,24 @@ fn notify_remove_delegator_vote_success_works() {
PendingDelegatorVotes::<Runtime>::get(vtoken, poll_index),
BoundedVec::<(DerivativeIndex, AccountVote<Balance>), 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::<Runtime>::get(vtoken, poll_index),
BoundedVec::<(DerivativeIndex, AccountVote<Balance>), ConstU32<100>>::try_from(vec![(
derivative_index,
aye(100, 0)
aye(200, 0)
)])
.unwrap()
);
Expand Down Expand Up @@ -880,24 +979,24 @@ fn notify_remove_delegator_vote_fail_works() {
PendingDelegatorVotes::<Runtime>::get(vtoken, poll_index),
BoundedVec::<(DerivativeIndex, AccountVote<Balance>), 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::<Runtime>::get(vtoken, poll_index),
BoundedVec::<(DerivativeIndex, AccountVote<Balance>), ConstU32<100>>::try_from(vec![(
derivative_index,
aye(100, 0)
aye(200, 0)
)])
.unwrap()
);
Expand Down
2 changes: 1 addition & 1 deletion pallets/vtoken-voting/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}
Expand Down