Skip to content

Commit

Permalink
Add LastRenominationOf (paritytech#794)
Browse files Browse the repository at this point in the history
* Add LastRenominationOf

Close paritytech#692

* Fix renominate tests

* Build wasm
  • Loading branch information
atenjin committed Jul 29, 2019
1 parent 5c61543 commit cdaf089
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
Binary file not shown.
12 changes: 11 additions & 1 deletion xrml/xmining/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ decl_module! {
Self::wont_reach_upper_bound(&to, value)?;
}

Self::apply_renominate(&who, &from, &to, value)?;
let bonding_duration = Self::bonding_duration();
let current_block = <system::Module<T>>::block_number();
if let Some(last_renomination) = Self::last_renomination_of(&who) {
ensure!(current_block > last_renomination + bonding_duration, "Cannot renominate if your last renomination is not expired.");
}

Self::apply_renominate(&who, &from, &to, value, current_block)?;
}

fn unnominate(
Expand Down Expand Up @@ -377,6 +383,8 @@ decl_storage! {
/// Reported validators that did evil, reset per session.
pub EvilValidatorsPerSession get(evil_validators): Vec<T::AccountId>;

pub LastRenominationOf get(last_renomination_of): map T::AccountId => Option<T::BlockNumber>;

/// Minimum penalty for each slash.
pub MinimumPenalty get(minimum_penalty) config(): T::Balance;
/// The active validators that have ever been offline per session.
Expand Down Expand Up @@ -519,9 +527,11 @@ impl<T: Trait> Module<T> {
from: &T::AccountId,
to: &T::AccountId,
value: T::Balance,
current_block: T::BlockNumber,
) -> Result {
Self::apply_update_vote_weight(who, from, value, false);
Self::apply_update_vote_weight(who, to, value, true);
<LastRenominationOf<T>>::insert(who, current_block);
Ok(())
}

Expand Down
55 changes: 55 additions & 0 deletions xrml/xmining/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,14 @@ fn renominate_should_work() {
System::set_block_number(1);
XSession::check_rotate_session(System::block_number());

assert_ok!(XStaking::set_bonding_duration(0));

assert_ok!(XStaking::register(Origin::signed(1), b"name".to_vec(),));
assert_ok!(XStaking::register(Origin::signed(3), b"name3".to_vec(),));

assert_ok!(XStaking::nominate(Origin::signed(1), 1.into(), 5, vec![]));
assert_ok!(XStaking::nominate(Origin::signed(3), 3.into(), 5, vec![]));

System::set_block_number(2);
XSession::check_rotate_session(System::block_number());
assert_ok!(XStaking::nominate(Origin::signed(2), 1.into(), 15, vec![]));
Expand Down Expand Up @@ -497,6 +502,56 @@ fn minimum_candidate_threshold_should_work() {
});
}

#[test]
fn renominate_limitation_should_work() {
with_externalities(&mut new_test_ext(), || {
assert_ok!(XStaking::set_bonding_duration(2));

assert_ok!(XStaking::register(Origin::signed(1), b"name1".to_vec(),));
assert_ok!(XStaking::register(Origin::signed(2), b"name2".to_vec(),));
assert_ok!(XStaking::register(Origin::signed(3), b"name3".to_vec(),));

assert_ok!(XStaking::nominate(Origin::signed(1), 1.into(), 5, vec![]));
assert_ok!(XStaking::nominate(Origin::signed(2), 2.into(), 5, vec![]));
assert_ok!(XStaking::nominate(Origin::signed(3), 3.into(), 5, vec![]));

assert_ok!(XStaking::nominate(Origin::signed(4), 1.into(), 10, vec![]));

System::set_block_number(1);
XSession::check_rotate_session(System::block_number());
assert_ok!(XStaking::renominate(
Origin::signed(4),
1.into(),
2.into(),
3,
b"memo".to_vec()
));

System::set_block_number(2);
XSession::check_rotate_session(System::block_number());
assert_noop!(
XStaking::renominate(Origin::signed(4), 1.into(), 3.into(), 3, b"memo".to_vec()),
"Cannot renominate if your last renomination is not expired."
);

System::set_block_number(3);
XSession::check_rotate_session(System::block_number());
assert_noop!(
XStaking::renominate(Origin::signed(4), 1.into(), 3.into(), 3, b"memo".to_vec()),
"Cannot renominate if your last renomination is not expired."
);

System::set_block_number(4);
XSession::check_rotate_session(System::block_number());
assert_ok!(XStaking::renominate(
Origin::signed(4),
1.into(),
3.into(),
3,
b"memo".to_vec()
));
});
}
#[test]
fn upper_bound_of_total_nomination_should_work() {
with_externalities(&mut new_test_ext(), || {
Expand Down

0 comments on commit cdaf089

Please sign in to comment.