Skip to content

Commit

Permalink
Integrate Asset Mining into Staking (paritytech#144)
Browse files Browse the repository at this point in the history
* Impl asset mining can_claim()

* Impl AssetMining

* Test claim_restriction

Fix a bug of Staking bond validator_acceptable_votes_check

* total issuance should work

* Test staking reward

* chain_spec: StakingConfig

* Use cargo build instead of cargo test --no-run

* Fix asset mining mock

* Try stable

* Test use stable

* Fix claim restriction test

* Put the most time consuming job at the last

.

.

* Pin to 0514 nightly
  • Loading branch information
liuchengxu authored Jul 18, 2020
1 parent c2e7f83 commit 42b5407
Show file tree
Hide file tree
Showing 14 changed files with 686 additions and 205 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
toolchain: nightly-2020-05-14
target: wasm32-unknown-unknown
profile: minimal
override: true
Expand Down Expand Up @@ -56,8 +56,8 @@ jobs:
- name: Format
run: cargo fmt -- --check

- name: Compile
run: cargo test --no-run
- if: matrix.os == 'ubuntu-latest'
run: cargo clippy

- name: Test
# FIXME: test all
Expand All @@ -71,5 +71,5 @@ jobs:
cd xpallets/dex/spot
cargo test -- --nocapture
- if: matrix.os == 'ubuntu-latest'
run: cargo clippy
- name: Compile
run: cargo test --no-run
48 changes: 0 additions & 48 deletions .travis.yml

This file was deleted.

2 changes: 2 additions & 0 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,11 @@ fn testnet_genesis(
.collect()
},
validator_count: 100,
minimum_validator_count: initial_authorities.len() as u32,
minimum_validator_count: 4,
sessions_per_era: 3,
vesting_account: get_account_id_from_seed::<sr25519::Public>("vesting"),
glob_dist_ratio: (12, 88),
mining_ratio: (10, 90),
..Default::default()
}),
xpallet_dex_spot: Some(XSpotConfig {
Expand Down
16 changes: 16 additions & 0 deletions primitives/mining/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@ pub fn generic_weight_factors<
)
}

/// Computes the dividend according to the ratio of source_vote_weight/target_vote_weight.
///
/// dividend = source_vote_weight/target_vote_weight * balance_of(claimee_reward_pot)
pub fn compute_dividend<AccountId, Balance: BaseArithmetic, F: FnOnce(&AccountId) -> Balance>(
source_vote_weight: WeightType,
target_vote_weight: WeightType,
claimee_reward_pot: &AccountId,
reward_pot_balance: F,
) -> Balance {
let total_reward_pot = reward_pot_balance(&claimee_reward_pot);
match source_vote_weight.checked_mul(total_reward_pot.saturated_into()) {
Some(x) => (x / target_vote_weight).saturated_into(),
None => panic!("source_vote_weight * total_reward_pot overflow, this should not happen"),
}
}

/// Claims the reward for participating in the mining.
pub trait Claim<AccountId> {
/// Entity of holder of individual miners.
Expand Down
2 changes: 2 additions & 0 deletions xpallets/mining/asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ xpallet-mining-staking = { path = "../../mining/staking", default-features = fal
xpallet-protocol = { path = "../../protocol" }
sp-core = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4" }
sp-io = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4" }
pallet-session = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4" }
pallet-timestamp = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4" }
env_logger = "0.7.1"

[features]
Expand Down
51 changes: 45 additions & 6 deletions xpallets/mining/asset/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use super::*;
use codec::Encode;
use sp_core::crypto::UncheckedFrom;
use sp_runtime::traits::Hash;
use sp_runtime::traits::Saturating;
use xp_mining_common::{
generic_weight_factors, BaseMiningWeight, Claim, ComputeMiningWeight, WeightFactors, WeightType,
compute_dividend, generic_weight_factors, BaseMiningWeight, Claim, ComputeMiningWeight,
WeightFactors, WeightType,
};
use xp_mining_staking::MiningPower;

impl<'a, T: Trait> BaseMiningWeight<T::Balance, T::BlockNumber> for AssetLedgerWrapper<'a, T> {
fn amount(&self) -> T::Balance {
Expand Down Expand Up @@ -82,7 +85,11 @@ impl<T: Trait> xpallet_assets::OnAssetChanged<T::AccountId, T::Balance> for Modu
Self::update_mining_weights(source, target, current_block);
}

fn on_issue_post(target: &AssetId, source: &T::AccountId, value: T::Balance) -> DispatchResult {
fn on_issue_post(
target: &AssetId,
source: &T::AccountId,
_value: T::Balance,
) -> DispatchResult {
Self::issue_reward(source, target);
Ok(())
}
Expand Down Expand Up @@ -110,12 +117,14 @@ impl<T: Trait> xpallet_assets::OnAssetChanged<T::AccountId, T::Balance> for Modu

impl<T: Trait> Module<T> {
fn allocate_dividend(
_claimer: &T::AccountId,
claimer: &T::AccountId,
_claimee: &AssetId,
_claimee_reward_pot: &T::AccountId,
_dividend: T::Balance,
dividend: T::Balance,
) -> Result<(), Error<T>> {
todo!("referral_or_treasury 10%, claimer 90%")
// todo!("referral_or_treasury 10%, claimer 90%")
let _ = xpallet_assets::Module::<T>::pcx_issue(claimer, dividend);
Ok(())
}
}

Expand All @@ -134,7 +143,12 @@ impl<T: Trait> Claim<T::AccountId> for Module<T> {
)?;

let claimee_reward_pot = T::DetermineRewardPotAccount::reward_pot_account_for(claimee);
let dividend = Self::compute_dividend(source_weight, target_weight, &claimee_reward_pot);
let dividend = compute_dividend::<T::AccountId, T::Balance, _>(
source_weight,
target_weight,
&claimee_reward_pot,
xpallet_assets::Module::<T>::pcx_free_balance,
);

Self::can_claim(claimer, claimee, dividend, current_block)?;

Expand Down Expand Up @@ -205,3 +219,28 @@ where
UncheckedFrom::unchecked_from(T::Hashing::hash(&buf[..]))
}
}

impl<T: Trait> xp_mining_staking::AssetMining<T::Balance> for Module<T> {
/// Collects the mining power of all mining assets.
fn asset_mining_power() -> Vec<(AssetId, MiningPower)> {
// Currently only X-BTC asset.
XTypeAssetPowerMap::iter()
.map(|(asset_id, fixed_power)| {
let total_balance =
<xpallet_assets::Module<T>>::all_type_total_asset_balance(&asset_id);
(
asset_id,
total_balance
.saturating_mul(fixed_power.saturated_into())
.saturated_into::<MiningPower>(),
)
})
.collect()
}

/// Issues reward to the reward pot of an Asset.
fn reward(asset_id: AssetId, value: T::Balance) {
let reward_pot = T::DetermineRewardPotAccount::reward_pot_account_for(&asset_id);
let _ = xpallet_assets::Module::<T>::pcx_issue(&reward_pot, value);
}
}
Loading

0 comments on commit 42b5407

Please sign in to comment.