Skip to content

Commit

Permalink
Hotfix/ensure name is empty (paritytech#129)
Browse files Browse the repository at this point in the history
* Ensure name of intention is empty

* Adjust PCX precision

* Don't use expect()

* Change PCX precision from 8 to 3

* Tweak ensures
  • Loading branch information
liuchengxu committed Nov 30, 2018
1 parent 6d96c6a commit eaff0be
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 63 deletions.
83 changes: 31 additions & 52 deletions cxrml/mining/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ impl<T: Trait> Module<T> {
tokenbalances::is_valid_symbol(&url)?;

ensure!(
!<IntentionProfiles<T>>::get(&intention).name.is_empty(),
<IntentionProfiles<T>>::get(&intention).name.is_empty(),
"Cannot register if already registered."
);

Expand Down Expand Up @@ -536,12 +536,13 @@ impl<T: Trait> Module<T> {
cxsupport::Module::<T>::handle_fee_before(&who, Self::activate_fee(), true, || Ok(()))?;

ensure!(
Self::intentions().iter().find(|&t| t == &who).is_some(),
"Cannot activate if transactor is not an intention."
!<IntentionProfiles<T>>::get(&who).is_active,
"Cannot activate if already active."
);

ensure!(
!<IntentionProfiles<T>>::get(&who).is_active,
"Cannot activate if already activated."
Self::intentions().iter().find(|&t| t == &who).is_some(),
"Cannot activate if transactor is not an intention."
);

let mut iprof = <IntentionProfiles<T>>::get(&who);
Expand All @@ -558,20 +559,21 @@ impl<T: Trait> Module<T> {
let who = ensure_signed(origin)?;
cxsupport::Module::<T>::handle_fee_before(&who, Self::deactivate_fee(), true, || Ok(()))?;

ensure!(
<IntentionProfiles<T>>::get(&who).is_active,
"Cannot deactivate if already inactive."
);

ensure!(
Self::intentions().iter().find(|&t| t == &who).is_some(),
"Cannot deactivate if transactor is not an intention."
);

// deactivate fails in degenerate case of having too few existing staked parties
if Self::intentions().len() <= Self::minimum_validator_count() as usize {
return Err("cannot deactivate when there are too few staked participants");
}

ensure!(
<IntentionProfiles<T>>::get(&who).is_active,
"Cannot deactivate if already inactive."
);

let mut iprof = <IntentionProfiles<T>>::get(&who);
iprof.is_active = false;
<IntentionProfiles<T>>::insert(who, iprof);
Expand All @@ -585,21 +587,17 @@ impl<T: Trait> Module<T> {
cxsupport::Module::<T>::handle_fee_before(&who, Self::stake_fee(), true, || Ok(()))?;

ensure!(value.as_() > 0, "Cannot stake zero.");
// TODO Take care of precision later
// ensure!(
// value.as_() % 10_u64.pow(<tokenbalances::Module<T>>::chainx_precision() as u32) == 0,
// "Cannot stake decimal."
// );

// let value = T::Balance::sa(
// value.as_() / 10_u64.pow(<tokenbalances::Module<T>>::chainx_precision() as u32),
// );

ensure!(
value <= <balances::Module<T>>::free_balance(&who),
"Cannot stake if amount greater than your free balance."
);

ensure!(
Self::intentions().iter().find(|&t| t == &who).is_some(),
"Cannot stake if transactor is not an intention."
);

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

Ok(())
Expand All @@ -610,25 +608,18 @@ impl<T: Trait> Module<T> {
let who = ensure_signed(origin)?;
cxsupport::Module::<T>::handle_fee_before(&who, Self::unstake_fee(), true, || Ok(()))?;

ensure!(
Self::intentions().iter().find(|&t| t == &who).is_some(),
"Cannot unstake if transactor is not an intention."
);

ensure!(value.as_() > 0, "Cannot unstake zero.");
// ensure!(
// value.as_() % 10_u64.pow(<tokenbalances::Module<T>>::chainx_precision() as u32) == 0,
// "Cannot stake decimal."
// );

// let value = T::Balance::sa(
// value.as_() / 10_u64.pow(<tokenbalances::Module<T>>::chainx_precision() as u32),
// );

let current_nomination = Self::nomination_record_of(&who, &who).nomination;

ensure!(
value <= current_nomination,
"Cannot unstake if amount greater than your free balance."
"Cannot unstake if amount greater than your current nomination."
);

ensure!(
Self::intentions().iter().find(|&t| t == &who).is_some(),
"Cannot unstake if transactor is not an intention."
);

let mut nprof = <NominatorProfiles<T>>::get(&who);
Expand Down Expand Up @@ -656,19 +647,15 @@ impl<T: Trait> Module<T> {

ensure!(value.as_() > 0, "Cannot stake zero.");

ensure!(
Self::intentions().iter().find(|&t| t == &target).is_some(),
"cannot nominate if target is not an intention."
);

if Self::intentions().iter().find(|&t| t == &who).is_some() {
ensure!(who != target, "cannot nominate per se as an intention.");
}

// ensure!(
// value.as_() % 10_u64.pow(<tokenbalances::Module<T>>::chainx_precision() as u32) == 0,
// "Cannot stake decimal."
// );

// let value = T::Balance::sa(
// value.as_() / 10_u64.pow(<tokenbalances::Module<T>>::chainx_precision() as u32),
// );

ensure!(
value <= <balances::Module<T>>::free_balance(&who),
"Cannot nominate if greater than your avaliable free balance."
Expand Down Expand Up @@ -744,6 +731,8 @@ impl<T: Trait> Module<T> {
|| Ok(()),
)?;

ensure!(value.as_() > 0, "Cannot unnominate zero.");

let target = <balances::Module<T>>::lookup(target)?;

let nprof = <NominatorProfiles<T>>::get(&source);
Expand All @@ -753,16 +742,6 @@ impl<T: Trait> Module<T> {
"Cannot claim if target is not your nominee."
);

ensure!(value.as_() > 0, "Cannot unnominate zero.");
// ensure!(
// value.as_() % 10_u64.pow(<tokenbalances::Module<T>>::chainx_precision() as u32) == 0,
// "Cannot stake decimal."
// );

// let value = T::Balance::sa(
// value.as_() / 10_u64.pow(<tokenbalances::Module<T>>::chainx_precision() as u32),
// );

let mut record = Self::nomination_record_of(&source, &target);

let current_nomination = record.nomination;
Expand Down
9 changes: 4 additions & 5 deletions pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,10 @@ where
Ok(xt) => Some(*xt.hash()),
Err(e) => match e.into_pool_error() {
Ok(e) => match e.kind() {
extrinsic_pool::ErrorKind::AlreadyImported(hash) => Some(
::std::str::FromStr::from_str(&hash)
.map_err(|_| {})
.expect("Hash string is always valid"),
),
extrinsic_pool::ErrorKind::AlreadyImported(hash) => {
warn!("{:?} already imported", hash);
None
}
_ => {
debug!("Error adding transaction to the pool: {:?}", e);
None
Expand Down
20 changes: 14 additions & 6 deletions src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub fn testnet_genesis(chainspec: ChainSpec) -> GenesisConfig {
const HOURS: u64 = MINUTES * 60;
const DAYS: u64 = HOURS * 24;

let pcx_precision = 3_u16;
let normalize = |n: u128| n * 10_u128.pow(pcx_precision as u32);
let balances_config = BalancesConfig {
transaction_base_fee: 1,
transaction_byte_fee: 0,
Expand All @@ -61,11 +63,17 @@ pub fn testnet_genesis(chainspec: ChainSpec) -> GenesisConfig {
creation_fee: 0,
reclaim_rebate: 0,
balances: vec![
(Keyring::Alice.to_raw_public().into(), 1_000_000),
(Keyring::Bob.to_raw_public().into(), 1_000_000),
(Keyring::Charlie.to_raw_public().into(), 1_000_000),
(Keyring::Dave.to_raw_public().into(), 1_000_000),
(Keyring::Ferdie.to_raw_public().into(), 996_000_000),
(Keyring::Alice.to_raw_public().into(), normalize(1_000_000)),
(Keyring::Bob.to_raw_public().into(), normalize(1_000_000)),
(
Keyring::Charlie.to_raw_public().into(),
normalize(1_000_000),
),
(Keyring::Dave.to_raw_public().into(), normalize(1_000_000)),
(
Keyring::Ferdie.to_raw_public().into(),
normalize(996_000_000),
),
],
};
let balances_config_copy = BalancesConfigCopy::create_from_src(&balances_config).src();
Expand Down Expand Up @@ -118,7 +126,7 @@ pub fn testnet_genesis(chainspec: ChainSpec) -> GenesisConfig {
fee_buy_account: substrate_primitives::H256([1; 32]),
}),
tokenbalances: Some(TokenBalancesConfig {
chainx_precision: 8,
chainx_precision: pcx_precision,
// token_list: Vec<(Token, Vec<(T::AccountId, T::TokenBalance)>)>
// e.g. [("btc", [(account1, value), (account2, value)].to_vec()), ("eth", [(account1, value), (account2, value)].to_vec())]
token_list: vec![
Expand Down

0 comments on commit eaff0be

Please sign in to comment.