Skip to content

Commit

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

* Add tests for renominate

* Build wasm

* Add renominate in fee

* Nit
  • Loading branch information
liuchengxu authored Mar 28, 2019
1 parent ee8b1bd commit ce128bd
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 40 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified cli/src/chainx_runtime.compact.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions runtime/src/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl CheckFee for Call {
XStakingCall::refresh(_, _, _, _) => Some(1000),
XStakingCall::nominate(_, _, _) => Some(5),
XStakingCall::unnominate(_, _, _) => Some(3),
XStakingCall::renominate(_, _, _, _) => Some(8),
XStakingCall::unfreeze(_, _) => Some(2),
XStakingCall::claim(_) => Some(3),
XStakingCall::setup_trustee(_, _, _, _) => Some(1000),
Expand Down
Binary file not shown.
3 changes: 3 additions & 0 deletions xrml/xmining/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ xbitcoin = { package = "xrml-bridge-bitcoin", path = "../../xbridge/bitcoin", de
xrecords = { package = "xrml-xassets-records", path = "../../xassets/records", default-features = false }
session = { package = "xrml-session", path = "../../xsession", default_features = false }

[dev-dependencies]
fee_manager = { package = "xrml-fee-manager", path = "../../xfee/manager", default-features = false }

[features]
default = ["std"]
std = [
Expand Down
42 changes: 40 additions & 2 deletions xrml/xmining/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,33 @@ decl_module! {
Self::apply_nominate(&who, &target, value)?;
}

/// Renominate from one to another intention.
fn renominate(
origin,
from: <T::Lookup as StaticLookup>::Source,
to: <T::Lookup as StaticLookup>::Source,
value: T::Balance,
memo: Memo
) {
let who = ensure_signed(origin)?;
let context = system::ChainContext::<T>::default();
let from = context.lookup(from)?;
let to = context.lookup(to)?;

xassets::is_valid_memo::<T>(&memo)?;
ensure!(!value.is_zero(), "Cannot renominate zero.");
ensure!(
<NominationRecords<T>>::get((who.clone(), from.clone())).is_some(),
"Cannot renominate if the from party is not your nominee."
);
ensure!(
value <= Self::revokable_of(&who, &from),
"Cannot renominate if greater than your current nomination."
);

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

fn unnominate(
origin,
target: <T::Lookup as StaticLookup>::Source,
Expand All @@ -126,6 +153,7 @@ decl_module! {
Self::apply_unnominate(&who, &target, value)?;
}

/// Claim the reward for your nomination.
fn claim(origin, target: <T::Lookup as StaticLookup>::Source) {
let who = ensure_signed(origin)?;
let target = system::ChainContext::<T>::default().lookup(target)?;
Expand Down Expand Up @@ -212,7 +240,7 @@ decl_module! {
Self::apply_refresh(&who, url, desire_to_run, next_key, about);
}

/// Register intention
/// Register to be an intention.
fn register(origin, name: Name) {
let who = ensure_signed(origin)?;

Expand All @@ -231,7 +259,6 @@ decl_module! {

xaccounts::is_valid_about::<T>(&about)?;

// TODO validate addr
Self::validate_trustee_entity(&chain, &hot_entity)?;
Self::validate_trustee_entity(&chain, &cold_entity)?;

Expand Down Expand Up @@ -450,6 +477,17 @@ impl<T: Trait> Module<T> {
Ok(())
}

fn apply_renominate(
who: &T::AccountId,
from: &T::AccountId,
to: &T::AccountId,
value: T::Balance,
) -> Result {
Self::apply_update_vote_weight(who, from, value, false);
Self::apply_update_vote_weight(who, to, value, true);
Ok(())
}

fn apply_unnominate(source: &T::AccountId, target: &T::AccountId, value: T::Balance) -> Result {
let freeze_until = if Self::is_intention(source) && *source == *target {
<system::Module<T>>::block_number() + Self::intention_bonding_duration()
Expand Down
53 changes: 15 additions & 38 deletions xrml/xmining/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ impl balances::Trait for Test {
type OnNewAccount = Indices;
type OnFreeBalanceZero = ();
type Event = ();
type TransactionPayment = ();
type DustRemoval = ();
type TransferPayment = ();
}
impl xaccounts::Trait for Test {
type Event = ();
Expand All @@ -68,6 +71,8 @@ impl xassets::Trait for Test {
type OnAssetChanged = ();
type OnAssetRegisterOrRevoke = ();
}
impl fee_manager::Trait for Test {}

pub struct DummyDetermineValidatorList;
impl ValidatorList<u64> for DummyDetermineValidatorList {
fn validator_list() -> Vec<u64> {
Expand Down Expand Up @@ -111,6 +116,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
.build_storage()
.unwrap()
.0;
let pcx_precision = 8;
let apply_prec = |x| x * 10_u64.pow(pcx_precision as u32);
t.extend(
consensus::GenesisConfig::<Test> {
code: vec![],
Expand All @@ -130,6 +137,12 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
session::GenesisConfig::<Test> {
session_length: 1,
validators: vec![(10, 10), (20, 20), (30, 30), (40, 40)],
keys: vec![
(10, UintAuthorityId(10)),
(20, UintAuthorityId(20)),
(30, UintAuthorityId(30)),
(40, UintAuthorityId(40)),
],
}
.build_storage()
.unwrap()
Expand All @@ -139,6 +152,8 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
balances::GenesisConfig::<Test> {
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40)],
existential_deposit: 0,
transaction_base_fee: 0,
transaction_byte_fee: 0,
transfer_fee: 0,
creation_fee: 0,
vesting: vec![],
Expand All @@ -157,50 +172,12 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
.0,
);

let pcx_precision = 8;
let apply_prec = |x| x * 10_u64.pow(pcx_precision as u32);
let full_endowed = vec![
(
10u64,
apply_prec(10),
b"10".to_vec(),
b"10.com".to_vec(),
b"03f72c448a0e59f48d4adef86cba7b278214cece8e56ef32ba1d179e0a8129bdba".to_vec(), // hot_entity
b"02a79800dfed17ad4c78c52797aa3449925692bc8c83de469421080f42d27790ee".to_vec(),
), // cold_entity
(
20u64,
apply_prec(20),
b"20".to_vec(),
b"Bob.com".to_vec(),
b"0306117a360e5dbe10e1938a047949c25a86c0b0e08a0a7c1e611b97de6b2917dd".to_vec(),
b"03ece1a20b5468b12fd7beda3e62ef6b2f6ad9774489e9aff1c8bc684d87d70780".to_vec(),
),
(
30u64,
apply_prec(30),
b"30".to_vec(),
b"30".to_vec(),
b"0311252930af8ba766b9c7a6580d8dc4bbf9b0befd17a8ef7fabac275bba77ae40".to_vec(),
b"02e34d10113f2dd162e8d8614a4afbb8e2eb14eddf4036042b35d12cf5529056a2".to_vec(),
),
(
40u64,
apply_prec(30),
b"40".to_vec(),
b"40".to_vec(),
b"0227e54b65612152485a812b8856e92f41f64788858466cc4d8df674939a5538c3".to_vec(),
b"020699bf931859cafdacd8ac4d3e055eae7551427487e281e3efba618bdd395f2f".to_vec(),
),
];
t.extend(
GenesisConfig::<Test> {
initial_reward: apply_prec(50),
current_era: 0,
validator_count: 8,
minimum_validator_count: 4,
trustee_count: 8,
minimum_trustee_count: 4,
bonding_duration: 1,
intention_bonding_duration: 10,
sessions_per_era: 1,
Expand Down
83 changes: 83 additions & 0 deletions xrml/xmining/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,89 @@ fn nominate_should_work() {
});
}

#[test]
fn renominate_should_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
Session::check_rotate_session(System::block_number());

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

System::set_block_number(2);
Session::check_rotate_session(System::block_number());
assert_ok!(Staking::nominate(Origin::signed(2), 1.into(), 15, vec![]));

assert_eq!(XAssets::pcx_free_balance(&2), 20 - 15);
assert_eq!(
Staking::nomination_record_of(&2, &1),
NominationRecord {
nomination: 15,
last_vote_weight: 0,
last_vote_weight_update: 2,
revocations: vec![],
}
);

System::set_block_number(3);
Session::check_rotate_session(System::block_number());
assert_ok!(Staking::renominate(
Origin::signed(2),
1.into(),
3.into(),
10,
b"memo".to_vec()
));
assert_eq!(
Staking::nomination_record_of(&2, &1),
NominationRecord {
nomination: 5,
last_vote_weight: 15,
last_vote_weight_update: 3,
revocations: vec![],
}
);
assert_eq!(
Staking::nomination_record_of(&2, &3),
NominationRecord {
nomination: 10,
last_vote_weight: 0,
last_vote_weight_update: 3,
revocations: vec![],
}
);

System::set_block_number(4);
Session::check_rotate_session(System::block_number());

assert_ok!(Staking::renominate(
Origin::signed(2),
1.into(),
3.into(),
5,
b"memo".to_vec()
));
assert_eq!(
Staking::nomination_record_of(&2, &1),
NominationRecord {
nomination: 0,
last_vote_weight: 20,
last_vote_weight_update: 4,
revocations: vec![],
}
);
assert_eq!(
Staking::nomination_record_of(&2, &3),
NominationRecord {
nomination: 15,
last_vote_weight: 10,
last_vote_weight_update: 4,
revocations: vec![],
}
);
});
}

#[test]
fn unnominate_should_work() {
with_externalities(&mut new_test_ext(), || {
Expand Down

0 comments on commit ce128bd

Please sign in to comment.