Skip to content

Commit

Permalink
Merge pull request #133 from AurevoirXavier/fix-bug
Browse files Browse the repository at this point in the history
Fix bug
  • Loading branch information
hackfisher authored Dec 3, 2019
2 parents bfb901e + b39f937 commit 1dfa6f1
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 180 deletions.
172 changes: 86 additions & 86 deletions node/cli/res/crayfish-fir.json

Large diffs are not rendered by default.

26 changes: 14 additions & 12 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("node"),
impl_name: create_runtime_str!("darwinia-node"),
authoring_version: 2,
spec_version: 79,
impl_version: 79,
authoring_version: 3,
spec_version: 80,
impl_version: 80,
apis: RUNTIME_API_VERSIONS,
};

Expand Down Expand Up @@ -145,7 +145,7 @@ parameter_types! {
}
impl balances::Trait for Runtime {
type Balance = Balance;
type OnFreeBalanceZero = (Staking, Session);
type OnFreeBalanceZero = ((Staking, Contracts), Session);
type OnNewAccount = Indices;
type TransferPayment = ();
type DustRemoval = ();
Expand Down Expand Up @@ -186,7 +186,7 @@ parameter_types! {
pub const MinimumPeriod: Moment = SLOT_DURATION / 2;
}
impl timestamp::Trait for Runtime {
type Moment = u64;
type Moment = Moment;
type OnTimestampSet = Babe;
type MinimumPeriod = MinimumPeriod;
}
Expand All @@ -206,7 +206,7 @@ impl authorship::Trait for Runtime {
type FindAuthor = session::FindAccountFromAuthorIndex<Self, Babe>;
type UncleGenerations = UncleGenerations;
type FilterUncle = ();
type EventHandler = Staking;
type EventHandler = (Staking, ImOnline);
}

// NOTE: `SessionHandler` and `SessionKeys` are co-dependent: One key will be used for each handler.
Expand Down Expand Up @@ -243,7 +243,7 @@ impl sudo::Trait for Runtime {
impl offences::Trait for Runtime {
type Event = Event;
type IdentificationTuple = session::historical::IdentificationTuple<Self>;
type OnOffenceHandler = ();
type OnOffenceHandler = Staking;
}

type SubmitTransaction = TransactionSubmitter<ImOnlineId, Runtime, UncheckedExtrinsic>;
Expand All @@ -264,11 +264,11 @@ impl grandpa::Trait for Runtime {
}

parameter_types! {
pub const WindowSize: BlockNumber = finality_tracker::DEFAULT_WINDOW_SIZE.into();
pub const ReportLatency: BlockNumber = finality_tracker::DEFAULT_REPORT_LATENCY.into();
pub const WindowSize: BlockNumber = 101;
pub const ReportLatency: BlockNumber = 1000;
}
impl finality_tracker::Trait for Runtime {
type OnFinalizationStalled = ();
type OnFinalizationStalled = Grandpa;
type WindowSize = WindowSize;
type ReportLatency = ReportLatency;
}
Expand Down Expand Up @@ -352,14 +352,17 @@ impl kton::Trait for Runtime {

parameter_types! {
pub const Period: BlockNumber = 1 * MINUTES;
// pub const Offset: BlockNumber = 0;
pub const SessionsPerEra: sr_staking_primitives::SessionIndex = 5;
// about 14 days = 14 * 24 * 60 * 60
pub const BondingDuration: TimeStamp = 1209600;
// 365 days * 24 hours * 60 minutes / 5 minutes
pub const ErasPerEpoch: EraIndex = 105120;
// decimal 9
pub const HardCap: Balance = 10_000_000_000 * COIN;
// date in Los Angeles*: 11/19/2019, 2:33:20 AM
// date in Berlin* :11/19/2019, 5:33:20 PM
// date in Beijing*: 11/19/2019, 4:33:20 PM
// date in New York* :11/19/2019, 4:33:20 AM
pub const GenesisTime: Moment = 1_574_156_000_000;
}
impl staking::Trait for Runtime {
Expand Down Expand Up @@ -430,7 +433,6 @@ pub type Block = generic::Block<Header, UncheckedExtrinsic>;
/// A Block signed with a Justification
pub type SignedBlock = generic::SignedBlock<Block>;
/// BlockId type as expected by this runtime.
/// BlockId type as expected by this runtime.
pub type BlockId = generic::BlockId<Block>;
/// The SignedExtension to the basic transaction logic.
pub type SignedExtra = (
Expand Down
32 changes: 20 additions & 12 deletions srml/staking/src/inflation.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use sr_primitives::Perbill;

use sr_primitives::traits::{IntegerSquareRoot, SaturatedConversion};

/// utility in staking
use crate::{KtonBalanceOf, RingBalanceOf, Trait};
use rstd::convert::TryInto;
use sr_primitives::{
traits::{IntegerSquareRoot, SaturatedConversion},
Perbill,
};
use substrate_primitives::U256;

// 1 - (99 /100)^sqrt(year)
use super::{KtonBalanceOf, RingBalanceOf, TimeStamp, Trait};

// 1 - (99 / 100) ^ sqrt(year)
// <T: Trait + 'static>() -> RingBalanceOf<T>
pub fn compute_total_payout<T: Trait>(
era_duration: u64,
living_time: u64,
era_duration: TimeStamp,
living_time: TimeStamp,
total_left: u128,
) -> (RingBalanceOf<T>, RingBalanceOf<T>) {
// Milliseconds per year for the Julian year (365.25 days).
const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;
const MILLISECONDS_PER_YEAR: TimeStamp = 1000 * 3600 * 24 * 36525 / 100;

let year: u32 = (living_time / MILLISECONDS_PER_YEAR + 1).saturated_into::<u32>();

let portion = Perbill::from_rational_approximation(era_duration as u64, MILLISECONDS_PER_YEAR);
let portion = Perbill::from_rational_approximation(era_duration, MILLISECONDS_PER_YEAR);

let maximum = portion * total_left;

// let maximum = maximum - maximum * 99_u128.pow(year.integer_sqrt()) / 100_u128.pow(year.integer_sqrt());
let maximum = maximum - maximum * 99_u128.pow(year.integer_sqrt()) / 100_u128.pow(year.integer_sqrt());

let payout = maximum * 1; // TODO: add treasury ratio: Perbill::from_rational_approximation(npos_token_staked, total_tokens);

Expand Down Expand Up @@ -59,3 +59,11 @@ pub fn compute_kton_return<T: Trait>(value: RingBalanceOf<T>, months: u32) -> Kt
/ U256::from(1_970_000);
res.as_u128().try_into().unwrap_or_default()
}

#[cfg(any(feature = "std", test))]
mod test {
// use super::*;

#[test]
fn _test() {}
}
16 changes: 6 additions & 10 deletions srml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,11 @@ use darwinia_support::{
};
use phragmen::{build_support_map, elect, equalize, ExtendedBalance, PhragmenStakedAssignment};

#[allow(unused)]
#[cfg(any(feature = "bench", test))]
#[cfg(test)]
mod mock;
//
#[cfg(test)]
mod tests;

//#[cfg(all(feature = "bench", test))]
//mod benches;

const DEFAULT_MINIMUM_VALIDATOR_COUNT: u32 = 4;
const MAX_NOMINATIONS: usize = 16;
const MAX_UNSTAKE_THRESHOLD: u32 = 10;
Expand Down Expand Up @@ -999,8 +994,9 @@ impl<T: Trait> Module<T> {
// from the nearest expire time
if !value_left.is_zero() {
// sorted by expire_time from far to near
deposit_items
.sort_unstable_by_key(|item| u64::max_value() - item.expire_time.saturated_into::<u64>());
deposit_items.sort_unstable_by_key(|item| {
TimeStamp::max_value() - item.expire_time.saturated_into::<TimeStamp>()
});
deposit_items.drain_filter(|item| {
if value_left.is_zero() {
return false;
Expand Down Expand Up @@ -1084,8 +1080,8 @@ impl<T: Trait> Module<T> {

let total_left: u128 = (T::Cap::get() - T::Ring::total_issuance()).saturated_into::<u128>();
let (total_payout, max_payout) = inflation::compute_total_payout::<T>(
era_duration.saturated_into::<u64>(),
(T::GenesisTime::get() - T::Time::now()).saturated_into::<u64>(),
era_duration.saturated_into::<TimeStamp>(),
(T::Time::now() - T::GenesisTime::get()).saturated_into::<TimeStamp>(),
total_left,
);

Expand Down
141 changes: 81 additions & 60 deletions types.json
Original file line number Diff line number Diff line change
@@ -1,57 +1,11 @@
{
"EpochDuration": "u64",
"EraIndex": "u32",
"TimeStamp": "u64",
"RingBalanceOf": "u128",
"KtonBalanceOf": "u128",
"ExtendedBalance": "u128",
"Address": "H160",
"EthBlockNumber": "u64",
"StakingBalance": {
"_enum": {
"Ring": "RingBalanceOf",
"Kton": "KtonBalanceOf"
}
},
"IndividualExposure": {
"who": "AccountId",
"value": "ExtendedBalance"
},
"Exposure": {
"total": "ExtendedBalance",
"own": "ExtendedBalance",
"others": "Vec<IndividualExposure>"
},
"ValidatorPrefs": {
"node_name": "Vec<u8>",
"unstake_threshold": "Compact<u32>",
"validator_payment_ratio": "Compact<u32>"
},
"StakingLedger": {
"stash": "AccountId",
"active_ring": "Compact<RingBalanceOf>",
"active_deposit_ring": "Compact<RingBalanceOf>",
"active_kton": "Compact<KtonBalanceOf>",
"deposit_items": "Vec<TimeDepositItem>",
"ring_staking_lock": "StakingLock",
"kton_staking_lock": "StakingLock"
},
"TimeDepositItem": {
"value": "Compact<RingBalanceOf>",
"start_time": "Compact<Moment>",
"expire_time": "Compact<Moment>"
},
"BalanceLock": {
"id": "LockIdentifier",
"withdraw_lock": "WithdrawLock",
"reasons": "WithdrawReasons"
},
"WithdrawLock": {
"_enum": {
"Normal": "NormalLock",
"WithStaking": "StakingLock"
}
},
"NormalLock": {
"amount": "u128",
"until": "Moment"
Expand All @@ -60,6 +14,32 @@
"staking_amount": "u128",
"unbondings": "Vec<NormalLock>"
},
"WithdrawLock": {
"_enum": {
"Normal": "NormalLock",
"WithStaking": "StakingLock"
}
},
"ActionRecord": {
"index": "u64",
"proof": "Vec<u8>",
"header_hash": "H256"
},
"Address": "H160",
"BestBlock": {
"height": "EthBlockNumber",
"hash": "H256",
"total_difficulty": "U256"
},
"BlockDetails": {
"height": "EthBlockNumber",
"hash": "H256",
"total_difficulty": "U256"
},
"Bloom": {
"_struct": "[u8; 256]"
},
"EthBlockNumber": "u64",
"EthHeader": {
"parent_hash": "H256",
"timestamp": "u64",
Expand All @@ -77,22 +57,63 @@
"seal": "Vec<Bytes>",
"hash": "Option<H256>"
},
"ActionRecord": {
"index": "u64",
"proof": "Vec<u8>",
"header_hash": "H256"
},
"BestBlock": {
"height": "EthBlockNumber",
"hash": "H256",
"total_difficulty": "U256"
},
"H64": {
"_struct": "[u8; 8]"
},
"BlockDetails": {
"height": "EthBlockNumber",
"hash": "H256",
"total_difficulty": "U256"
"LogEntry": {
"address": "Address",
"topics": "Vec<H256>",
"data": "Bytes"
},
"Receipt": {
"gas_used": "U256",
"log_bloom": "Bloom",
"logs": "Vec<LogEntry>",
"outcome": "TransactionOutcome"
},
"TransactionOutcome": {
"_enum": {
"Unknown": null,
"StateRoot": "H256",
"StatusCode": "u8"
}
},
"EraIndex": "u32",
"Exposure": {
"total": "ExtendedBalance",
"own": "ExtendedBalance",
"others": "Vec<IndividualExposure>"
},
"ExtendedBalance": "u128",
"IndividualExposure": {
"who": "AccountId",
"value": "ExtendedBalance"
},
"KtonBalanceOf": "u128",
"RingBalanceOf": "u128",
"StakingBalance": {
"_enum": {
"Ring": "RingBalanceOf",
"Kton": "KtonBalanceOf"
}
},
"StakingLedger": {
"stash": "AccountId",
"active_ring": "Compact<RingBalanceOf>",
"active_deposit_ring": "Compact<RingBalanceOf>",
"active_kton": "Compact<KtonBalanceOf>",
"deposit_items": "Vec<TimeDepositItem>",
"ring_staking_lock": "StakingLock",
"kton_staking_lock": "StakingLock"
},
"TimeDepositItem": {
"value": "Compact<RingBalanceOf>",
"start_time": "Compact<Moment>",
"expire_time": "Compact<Moment>"
},
"ValidatorPrefs": {
"node_name": "Vec<u8>",
"unstake_threshold": "Compact<u32>",
"validator_payment_ratio": "Compact<u32>"
}
}

0 comments on commit 1dfa6f1

Please sign in to comment.