Skip to content

Commit

Permalink
New distribution model (paritytech#753)
Browse files Browse the repository at this point in the history
* Split out reward.rs

* Deposit Reward event earlier (paritytech#731)

* Deposit Reward event earlier

Close paritytech#730

* Nit

* .

* Build wasm

* Refactoring for new distribution model (paritytech#735)

* Split out reward.rs

* Move reward related to reward.rs

* Extract try_funding_team

* Build wasm

* Fix/opreturn parse (paritytech#732)

* fix bug for transition_trustee_session

* provide test for opreturn

* provide new opreturn parser and tests

* Add is_xss_proof (paritytech#737)

* Add is_xss_proof

* Apply is_xss_proof on token name and desc

* Fix accounts tests

* Cursory impl for new distribution model

* Add multiply_by_rational

* Clean up

* Pub multiply_by_rational()

* Move VoteWeight definition to traits.rs

* Fix rls warnings in staking/src/lib.rs

* Refactor reward.rs a bit

* Refactor slash a bit

* Nit

* Add event when cross-chain assets grows too fast

* Nits

* Nits

* Add default value

* Test multiple_by_rational won't panic

* Unify Reward event by introducing SessionReward

* Nit
  • Loading branch information
liuchengxu authored Jul 5, 2019
1 parent 5812b9e commit 3fc62c4
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 115 deletions.
Binary file modified cli/src/chainx_runtime.compact.wasm
Binary file not shown.
Binary file not shown.
48 changes: 21 additions & 27 deletions xrml/xmining/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod reward;
mod shifter;
pub mod slash;
mod tests;
pub mod traits;
pub mod types;
pub mod vote_weight;

Expand All @@ -24,15 +25,14 @@ use system::ensure_signed;

// ChainX
use xaccounts::IntentionJackpotAccountIdFor;
use xassets::{Memo, Token};
use xassets::{AssetErr, Memo, Token};
use xr_primitives::{Name, XString, URL};
use xsupport::debug;
#[cfg(feature = "std")]
use xsupport::who;

pub use self::reward::{OnReward, OnRewardCalculation};
pub use self::traits::*;
pub use self::types::*;
pub use self::vote_weight::VoteWeight;

const DEFAULT_MINIMUM_VALIDATOR_COUNT: u32 = 4;
const SESSIONS_PER_ROUND: u64 = 210_000;
Expand Down Expand Up @@ -162,7 +162,7 @@ decl_module! {
let record = Self::nomination_record_of(&who, &target);
let mut revocations = record.revocations;

ensure!(revocations.len() > 0, "Revocation list is empty");
ensure!(!revocations.is_empty(), "Revocation list is empty");
ensure!(
revocation_index < revocations.len() as u32,
"Revocation index out of range."
Expand Down Expand Up @@ -263,17 +263,17 @@ decl_module! {
<MaximumIntentionCount<T>>::put(new);
}

/// Force there to be a new era. This also forces a new session immediately after.
/// `apply_rewards` should be true for validators to get the session reward.
fn force_new_era(apply_rewards: bool) -> Result {
Self::apply_force_new_era(apply_rewards)
}

/// Set the offline slash grace period.
fn set_minimum_penalty(new: T::Balance) {
<MinimumPenalty<T>>::put(new);
}

/// Set the distribution ratio between cross-chain assets and native assets.
pub fn set_distribution_ratio(new: (u32, u32)) {
ensure!(new.0 > 0 && new.1 > 0, "DistributionRatio can not be zero.");
<DistributionRatio<T>>::put(new);
}

/// Set the minimum validator candidate threshold.
fn set_minimum_candidate_threshold(new: (T::Balance, T::Balance)) {
<MinimumCandidateThreshold<T>>::put(new);
Expand All @@ -292,6 +292,8 @@ decl_event!(
{
/// All validators have been rewarded by the given balance.
Reward(Balance, Balance),
/// All rewards issued to all (psedu-)intentions.
SessionReward(Balance, Balance, Balance, Balance),
/// Missed blocks by each offline validator per session.
MissedBlocksOfOfflineValidatorPerSession(Vec<(AccountId, u32)>),
EnforceValidatorsInactive(Vec<AccountId>),
Expand Down Expand Up @@ -329,6 +331,9 @@ decl_storage! {
/// The current era index.
pub CurrentEra get(current_era) config(): T::BlockNumber;

/// Allocation ratio of native asset and cross-chain assets.
pub DistributionRatio get(distribution_ratio): (u32, u32) = (1u32, 1u32);

/// The next value of sessions per era.
pub NextSessionsPerEra get(next_sessions_per_era): Option<T::BlockNumber>;
/// The session index at which the era length last changed.
Expand Down Expand Up @@ -428,7 +433,7 @@ impl<T: Trait> Module<T> {
xassets::AssetType::ReservedStaking,
value,
)
.map_err(|e| e.info())
.map_err(AssetErr::info)
}

fn unnominate_reserve(who: &T::AccountId, value: T::Balance) -> Result {
Expand All @@ -439,7 +444,7 @@ impl<T: Trait> Module<T> {
xassets::AssetType::ReservedStakingRevocation,
value,
)
.map_err(|e| e.info())
.map_err(AssetErr::info)
}

fn staking_unreserve(who: &T::AccountId, value: T::Balance) -> Result {
Expand All @@ -450,24 +455,13 @@ impl<T: Trait> Module<T> {
xassets::AssetType::Free,
value,
)
.map_err(|e| e.info())
}

// Just force_new_era without origin check.
fn apply_force_new_era(apply_rewards: bool) -> Result {
<ForcingNewEra<T>>::put(());
<xsession::Module<T>>::apply_force_new_session(apply_rewards)
.map_err(AssetErr::info)
}

fn apply_nominate(source: &T::AccountId, target: &T::AccountId, value: T::Balance) -> Result {
Self::staking_reserve(source, value)?;
Self::apply_update_vote_weight(source, target, value, true);
Self::deposit_event(RawEvent::Nominate(
source.clone(),
target.clone(),
value.clone(),
));

Self::deposit_event(RawEvent::Nominate(source.clone(), target.clone(), value));
Ok(())
}

Expand Down Expand Up @@ -655,8 +649,8 @@ impl<T: Trait> Module<T> {
T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(who)
}

pub fn multi_jackpot_accountid_for_unsafe(whos: &Vec<T::AccountId>) -> Vec<T::AccountId> {
whos.into_iter()
pub fn multi_jackpot_accountid_for_unsafe(whos: &[T::AccountId]) -> Vec<T::AccountId> {
whos.iter()
.map(|who| T::DetermineIntentionJackpotAccountId::accountid_for_unsafe(who))
.collect()
}
Expand Down
22 changes: 22 additions & 0 deletions xrml/xmining/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,28 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
pcx_desc,
)
.unwrap();

let btc = Asset::new(
b"BTC".to_vec(),
b"X-BTC".to_vec(),
Chain::Bitcoin,
8, // bitcoin precision
b"ChainX's Cross-chain Bitcoin".to_vec(),
)
.unwrap();

let sdot = Asset::new(
b"SDOT".to_vec(), // token
b"Shadow DOT".to_vec(),
Chain::Ethereum,
3, // precision
b"ChainX's Shadow Polkadot from Ethereum".to_vec(),
)
.unwrap();

XAssets::bootstrap_register_asset(pcx, true, false).unwrap();
XAssets::bootstrap_register_asset(btc, true, true).unwrap();
XAssets::bootstrap_register_asset(sdot, true, true).unwrap();

let intentions = vec![
(10, 10 * 100_000_000, b"name10".to_vec(), b"".to_vec()),
Expand Down Expand Up @@ -324,6 +345,7 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
XStaking::bootstrap_refresh(&intention, Some(url), Some(true), None, None);
XStaking::bootstrap_update_vote_weight(&intention, &intention, value, true);
}

XAssets::pcx_issue(&1, 10).unwrap();
XAssets::pcx_issue(&2, 20).unwrap();
XAssets::pcx_issue(&3, 30).unwrap();
Expand Down
Loading

0 comments on commit 3fc62c4

Please sign in to comment.