Skip to content

Commit

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

Close paritytech#681

* Add test

* Nits
  • Loading branch information
atenjin committed Jul 29, 2019
1 parent cdaf089 commit 90c80e0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use transaction_pool::txpool::Pool as TransactionPool;
use chainx_primitives::{AccountId, Block};
use chainx_runtime::{GenesisConfig, RuntimeApi};
use runtime_api::xsession_api::XSessionApi;
use substrate_service::TelemetryOnConnect;

type XSystemInherentDataProvider = xsystem::InherentDataProvider;

Expand Down Expand Up @@ -246,6 +245,7 @@ construct_service_factory! {
}},
LightImportQueue = AuraImportQueue<Self::Block>
{ |config: &FactoryFullConfiguration<Self>, client: Arc<LightClient<Self>>| {
#[allow(deprecated)]
let fetch_checker = client.backend().blockchain().fetcher()
.upgrade()
.map(|fetcher| fetcher.checker().clone())
Expand All @@ -270,6 +270,7 @@ construct_service_factory! {
}},
SelectChain = LongestChain<FullBackend<Self>, Self::Block>
{ |config: &FactoryFullConfiguration<Self>, client: Arc<FullClient<Self>>| {
#[allow(deprecated)]
Ok(LongestChain::new(
client.backend().clone(),
client.import_lock()
Expand Down
Binary file not shown.
15 changes: 14 additions & 1 deletion xrml/xmining/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ decl_module! {
Self::apply_renominate(&who, &from, &to, value, current_block)?;
}

/// Unbond the nomination.
fn unnominate(
origin,
target: <T::Lookup as StaticLookup>::Source,
Expand All @@ -143,6 +144,10 @@ decl_module! {
value <= Self::revokable_of(&who, &target),
"Cannot unnominate if greater than your revokable nomination."
);
ensure!(
Self::current_revocations_count(&who, &target) < Self::max_unbond_entries_per_intention() as usize,
"Cannot unnomiate if the limit of max unbond entries is reached."
);

Self::apply_unnominate(&who, &target, value)?;
}
Expand All @@ -160,6 +165,7 @@ decl_module! {
Self::apply_claim(&who, &target)?;
}

/// Free the locked unnomination.
fn unfreeze(
origin,
target: <T::Lookup as StaticLookup>::Source,
Expand Down Expand Up @@ -191,7 +197,6 @@ decl_module! {
}

Self::staking_unreserve(&who, value)?;

revocations.swap_remove(revocation_index as usize);
if let Some(mut record) = <NominationRecords<T>>::get(&nominate_pair) {
record.revocations = revocations;
Expand Down Expand Up @@ -383,8 +388,12 @@ decl_storage! {
/// Reported validators that did evil, reset per session.
pub EvilValidatorsPerSession get(evil_validators): Vec<T::AccountId>;

/// The height of user's last nomination.
pub LastRenominationOf get(last_renomination_of): map T::AccountId => Option<T::BlockNumber>;

/// The maximum ongoing unbond entries simultaneously against per intention.
pub MaxUnbondEntriesPerIntention get(max_unbond_entries_per_intention): u32 = 10u32;

/// 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 @@ -660,6 +669,10 @@ impl<T: Trait> Module<T> {
Self::is_intention(nominator) && *nominator == *nominee
}

fn current_revocations_count(who: &T::AccountId, target: &T::AccountId) -> usize {
Self::nomination_record_of(who, target).revocations.len()
}

#[cfg(feature = "std")]
pub fn bootstrap_register(intention: &T::AccountId, name: Name) -> Result {
Self::apply_register(intention, name)
Expand Down
30 changes: 30 additions & 0 deletions xrml/xmining/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ fn renominate_limitation_should_work() {
));
});
}

#[test]
fn upper_bound_of_total_nomination_should_work() {
with_externalities(&mut new_test_ext(), || {
Expand Down Expand Up @@ -589,3 +590,32 @@ fn upper_bound_of_total_nomination_should_work() {
));
});
}

#[test]
fn max_unbond_entries_limit_should_work() {
with_externalities(&mut new_test_ext(), || {
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::nominate(Origin::signed(1), 1.into(), 10, vec![]));

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

for i in 2..12 {
System::set_block_number(i);
XSession::check_rotate_session(System::block_number());
assert_ok!(XStaking::unnominate(Origin::signed(3), 1.into(), 1, vec![]));
}

System::set_block_number(12);
XSession::check_rotate_session(System::block_number());
assert_noop!(
XStaking::unnominate(Origin::signed(3), 1.into(), 1, vec![]),
"Cannot unnomiate if the limit of max unbond entries is reached."
);

assert_ok!(XStaking::unfreeze(Origin::signed(3), 1.into(), 1));

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

0 comments on commit 90c80e0

Please sign in to comment.