Skip to content

Commit

Permalink
Rename ValidatorLedger.total to total_nomination (paritytech#280)
Browse files Browse the repository at this point in the history
* Rename ValidatorLedger.total to total_nomination

* Move DECIMALS to xpallet-protocol

* .

* .

* Remove duplicate consts in runtime

* .
  • Loading branch information
liuchengxu authored Oct 3, 2020
1 parent 5e48bf2 commit 35d1fdf
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 103 deletions.
87 changes: 41 additions & 46 deletions cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,25 @@ use hex_literal::hex;
use serde::{Deserialize, Serialize};
use serde_json::json;

use sc_chain_spec::ChainSpecExtension;
use sc_service::{config::TelemetryEndpoints, ChainType, Properties};

use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{crypto::UncheckedInto, sr25519, Pair, Public};
use sp_finality_grandpa::AuthorityId as GrandpaId;
use sp_runtime::traits::{IdentifyAccount, Verify};

use pallet_im_online::sr25519::AuthorityId as ImOnlineId;

use xpallet_protocol::{BTC_DECIMALS, PCX, PCX_DECIMALS, X_BTC};

use chainx_runtime::{
constants::currency::DOLLARS, AssetInfo, AssetRestriction, AssetRestrictions, BtcParams,
BtcTxVerifier, Chain, NetworkType, TrusteeInfoConfig,
constants::{currency::DOLLARS, time::DAYS},
AccountId, AssetId, AssetInfo, AssetRestriction, AssetRestrictions, Balance, BtcParams,
BtcTxVerifier, Chain, NetworkType, ReferralId, Runtime, Signature, TrusteeInfoConfig,
WASM_BINARY,
};
use chainx_runtime::{AccountId, AssetId, Balance, ReferralId, Runtime, Signature, WASM_BINARY};
use chainx_runtime::{
AuraConfig, AuthorityDiscoveryConfig, BalancesConfig, CouncilConfig, DemocracyConfig,
ElectionsConfig, GenesisConfig, GrandpaConfig, ImOnlineConfig, IndicesConfig, SessionConfig,
Expand All @@ -20,18 +34,8 @@ use chainx_runtime::{
XSpotConfig, XStakingConfig, XSystemConfig,
};

use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use sc_chain_spec::ChainSpecExtension;
use sc_service::{ChainType, Properties};
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{crypto::UncheckedInto, sr25519, Pair, Public};
use sp_finality_grandpa::AuthorityId as GrandpaId;
use sp_runtime::traits::{IdentifyAccount, Verify};

use crate::genesis::trustees::TrusteeParams;
use crate::res::BitcoinParams;
use sc_service::config::TelemetryEndpoints;

// Note this is the URL for the telemetry server
//const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/";
Expand Down Expand Up @@ -144,7 +148,7 @@ pub fn development_config() -> Result<ChainSpec, String> {
("Alice//stash", endowed_balance),
("Bob//stash", endowed_balance),
],
crate::res::load_testnet_btc_genesis_header_info,
crate::res::testnet_btc_genesis_header,
crate::genesis::trustees::local_testnet_trustees(),
)
};
Expand Down Expand Up @@ -225,7 +229,7 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
("Eve//stash", endowed_balance),
("Ferdie//stash", endowed_balance),
],
crate::res::load_testnet_btc_genesis_header_info,
crate::res::testnet_btc_genesis_header,
crate::genesis::trustees::local_testnet_trustees(),
)
};
Expand Down Expand Up @@ -365,7 +369,7 @@ pub fn staging_testnet_config() -> Result<ChainSpec, String> {
root_key.clone(), // use root key as vesting_account
assets.clone(),
endowed.clone(),
crate::res::load_testnet_btc_genesis_header_info,
crate::res::testnet_btc_genesis_header,
crate::genesis::trustees::staging_testnet_trustees(),
)
};
Expand Down Expand Up @@ -508,7 +512,7 @@ pub fn testnet_config() -> Result<ChainSpec, String> {
root_key.clone(), // use root key as vesting_account
assets.clone(),
endowed.clone(),
crate::res::load_testnet_btc_genesis_header_info,
crate::res::testnet_btc_genesis_header,
crate::genesis::trustees::staging_testnet_trustees(),
)
};
Expand All @@ -529,11 +533,9 @@ pub fn testnet_config() -> Result<ChainSpec, String> {
))
}

const PCX_DECIMALS: u8 = 8;
const BTC_DECIMALS: u8 = 8;
fn pcx() -> (AssetId, AssetInfo, AssetRestrictions) {
(
xpallet_protocol::PCX,
PCX,
AssetInfo::new::<Runtime>(
b"PCX".to_vec(),
b"Polkadot ChainX".to_vec(),
Expand All @@ -551,7 +553,7 @@ fn pcx() -> (AssetId, AssetInfo, AssetRestrictions) {

fn xbtc() -> (AssetId, AssetInfo, AssetRestrictions) {
(
xpallet_protocol::X_BTC,
X_BTC,
AssetInfo::new::<Runtime>(
b"XBTC".to_vec(),
b"ChainX Bitcoin".to_vec(),
Expand Down Expand Up @@ -624,7 +626,7 @@ where
let (assets, assets_restrictions) = init_assets(assets);

let endowed_accounts = endowed
.get(&xpallet_protocol::PCX)
.get(&PCX)
.expect("PCX endowed; qed")
.iter()
.cloned()
Expand All @@ -634,7 +636,7 @@ where
let num_endowed_accounts = endowed_accounts.len();

let balances = endowed
.get(&xpallet_protocol::PCX)
.get(&PCX)
.expect("PCX endowed; qed")
.iter()
.cloned()
Expand All @@ -649,10 +651,18 @@ where
.map(|member| (member, STASH))
.collect();

let tech_comm_members = endowed_accounts
.iter()
.take((num_endowed_accounts + 1) / 2)
.cloned()
.collect::<Vec<_>>();

let society_members = tech_comm_members.clone();

// PCX only reserves the native asset id in assets module,
// the actual native fund management is handled by pallet_balances.
let mut assets_endowed = endowed;
assets_endowed.remove(&xpallet_protocol::PCX);
assets_endowed.remove(&PCX);

let validators = initial_authorities
.clone()
Expand Down Expand Up @@ -688,11 +698,7 @@ where
}),
pallet_collective_Instance1: Some(CouncilConfig::default()),
pallet_collective_Instance2: Some(TechnicalCommitteeConfig {
members: endowed_accounts
.iter()
.take((num_endowed_accounts + 1) / 2)
.cloned()
.collect(),
members: tech_comm_members,
phantom: Default::default(),
}),
pallet_membership_Instance1: Some(Default::default()),
Expand All @@ -718,11 +724,7 @@ where
pallet_balances: Some(BalancesConfig { balances }),
pallet_indices: Some(IndicesConfig { indices: vec![] }),
pallet_society: Some(SocietyConfig {
members: endowed_accounts
.iter()
.take((num_endowed_accounts + 1) / 2)
.cloned()
.collect(),
members: society_members,
pot: 0,
max_members: 999,
}),
Expand All @@ -742,7 +744,7 @@ where
genesis_hash,
network,
confirmed_count,
} = bitcoin_info(); // crate::res::load_mainnet_btc_genesis_header_info();
} = bitcoin_info(); // crate::res::mainnet_btc_genesis_header();
Some(XGatewayBitcoinConfig {
genesis_trustees: btc_genesis_trustees,
genesis_info,
Expand All @@ -754,7 +756,7 @@ where
2 * 7 * 24 * 60 * 60, // target_timespan_seconds
10 * 60, // target_spacing_seconds
4, // retargeting_factor
), // retargeting_factor
),
verifier: BtcTxVerifier::Recover,
confirmation_number: confirmed_count,
reserved_block: 2100,
Expand All @@ -774,18 +776,11 @@ where
..Default::default()
}),
xpallet_mining_asset: Some(XMiningAssetConfig {
claim_restrictions: vec![(xpallet_protocol::X_BTC, (10, chainx_runtime::DAYS * 7))],
mining_power_map: vec![(xpallet_protocol::X_BTC, 400)],
claim_restrictions: vec![(X_BTC, (10, DAYS * 7))],
mining_power_map: vec![(X_BTC, 400)],
}),
xpallet_dex_spot: Some(XSpotConfig {
trading_pairs: vec![(
xpallet_protocol::PCX,
xpallet_protocol::X_BTC,
9,
2,
100000,
true,
)],
trading_pairs: vec![(PCX, X_BTC, 9, 2, 100000, true)],
}),
}
}
4 changes: 2 additions & 2 deletions cli/src/res.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ fn build_bitcoin_params(raw: BitcoinGenesisHeader, confirmed_count: u32) -> Bitc
}

// testnet
pub fn load_testnet_btc_genesis_header_info() -> BitcoinParams {
pub fn testnet_btc_genesis_header() -> BitcoinParams {
let raw: BitcoinGenesisHeader =
serde_json::from_str(include_str!("./res/btc_genesis_header_testnet.json"))
.expect("JSON was not well-formatted");
build_bitcoin_params(raw, 6u32)
}

// mainnet
pub fn load_mainnet_btc_genesis_header_info() -> BitcoinParams {
pub fn mainnet_btc_genesis_header() -> BitcoinParams {
let raw: BitcoinGenesisHeader =
serde_json::from_str(include_str!("./res/btc_genesis_header_mainnet.json"))
.expect("JSON was not well-formatted");
Expand Down
13 changes: 0 additions & 13 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,6 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
transaction_version: 1,
};

pub const MILLISECS_PER_BLOCK: u64 = 6000;

pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;

// These time units are defined in number of blocks.
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
pub const HOURS: BlockNumber = MINUTES * 60;
pub const DAYS: BlockNumber = HOURS * 24;

/// The version information used to identify this runtime when compiled natively.
#[cfg(feature = "std")]
pub fn native_version() -> NativeVersion {
Expand Down Expand Up @@ -313,17 +304,13 @@ impl pallet_aura::Trait for Runtime {
impl pallet_grandpa::Trait for Runtime {
type Event = Event;
type Call = Call;

type KeyOwnerProof =
<Self::KeyOwnerProofSystem as KeyOwnerProofSystem<(KeyTypeId, GrandpaId)>>::Proof;

type KeyOwnerIdentification = <Self::KeyOwnerProofSystem as KeyOwnerProofSystem<(
KeyTypeId,
GrandpaId,
)>>::IdentificationTuple;

type KeyOwnerProofSystem = Historical;

type HandleEquivocation = ();
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/chainx-js/res/chainx_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"lockedUntil": "BlockNumber"
},
"ValidatorLedger": {
"total": "Balance",
"totalNomination": "Balance",
"lastTotalVoteWeight": "WeightType",
"lastTotalVoteWeightUpdate": "BlockNumber"
},
Expand Down
3 changes: 1 addition & 2 deletions xpallets/dex/spot/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use sp_runtime::{

use chainx_primitives::{AssetId, BlockNumber};
use xpallet_assets::{AssetInfo, AssetRestriction, AssetRestrictions, Chain};
use xpallet_protocol::PCX_DECIMALS;

/// The AccountId alias in this test module.
pub(crate) type AccountId = u64;
Expand Down Expand Up @@ -124,8 +125,6 @@ thread_local! {
#[derive(Default)]
pub struct ExtBuilder;

const PCX_DECIMALS: u8 = 8;

fn pcx() -> (AssetId, AssetInfo, AssetRestrictions) {
(
xpallet_protocol::PCX,
Expand Down
22 changes: 11 additions & 11 deletions xpallets/mining/staking/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ where
BlockNumber: Default + BaseArithmetic + Copy,
{
fn amount(&self) -> Balance {
self.total
self.total_nomination
}

fn set_amount(&mut self, new: Balance) {
self.total = new;
self.total_nomination = new;
}

fn last_acum_weight(&self) -> WeightType {
Expand Down Expand Up @@ -151,24 +151,24 @@ impl<T: Trait> Module<T> {
current_block: T::BlockNumber,
delta: Delta<BalanceOf<T>>,
) {
Nominations::<T>::mutate(nominator, validator, |claimer_ledger| {
claimer_ledger.nomination = delta.calculate(claimer_ledger.nomination);
claimer_ledger.last_vote_weight = new_weight;
claimer_ledger.last_vote_weight_update = current_block;
Nominations::<T>::mutate(nominator, validator, |claimer| {
claimer.nomination = delta.calculate(claimer.nomination);
claimer.last_vote_weight = new_weight;
claimer.last_vote_weight_update = current_block;
});
}

///
pub(crate) fn set_validator_vote_weight(
validator: &T::AccountId,
who: &T::AccountId,
new_weight: WeightType,
current_block: T::BlockNumber,
delta: Delta<BalanceOf<T>>,
) {
ValidatorLedgers::<T>::mutate(validator, |validator_ledger| {
validator_ledger.total = delta.calculate(validator_ledger.total);
validator_ledger.last_total_vote_weight = new_weight;
validator_ledger.last_total_vote_weight_update = current_block;
ValidatorLedgers::<T>::mutate(who, |validator| {
validator.total_nomination = delta.calculate(validator.total_nomination);
validator.last_total_vote_weight = new_weight;
validator.last_total_vote_weight_update = current_block;
});
}

Expand Down
Loading

0 comments on commit 35d1fdf

Please sign in to comment.