Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add epoch in staking #6

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions runtime/wasm/Cargo.lock

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

4 changes: 3 additions & 1 deletion srml/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ evo-support = {path = "../support", default_features = false}
runtime_io = { package = "sr-io", git = "https://github.com/paritytech/substrate", default-features = false }
session = { package ="srml-session", git = "https://github.com/paritytech/substrate", default-features = false }
consensus = { package ="srml-consensus", git = "https://github.com/paritytech/substrate", default-features = false }
kton = { package = "evo-kton", path = "../token/kton", default-features = false}

[dev-dependencies]
ring = { package = "evo-ring", path = "../token/ring"}
kton = { package = "evo-kton", path = "../token/kton"}


[features]
default = ["std"]
Expand All @@ -45,4 +46,5 @@ std = [
"timestamp/std",
"session/std",
"consensus/std",
"kton/std",
]
25 changes: 14 additions & 11 deletions srml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ decl_storage! {
/// The length of a staking era in sessions.
pub SessionsPerEra get(sessions_per_era) config(): T::BlockNumber = T::BlockNumber::sa(1000);

/// Changed: maximum reward, part of the whole session, which will reward validators & nominators
/// for now 40% percent
/// Changed: maximum reward, part of the per validator within a session,
/// which will reward validators & nominators, for now 40% percent
pub SessionReward get(session_reward) config(): Perbill = Perbill::from_billionths(400000000);
/// Slash, per validator that is taken for the first time they are found to be offline.
pub OfflineSlash get(offline_slash) config(): Perbill = Perbill::from_millionths(1000); // Perbill::from_fraction() is only for std, so use from_millionths().
Expand Down Expand Up @@ -517,6 +517,11 @@ decl_storage! {
pub CurrentEpoch get(current_epoch): T::BlockNumber;

pub LastEpochChanged get(last_epoch_changed): T::BlockNumber;

/// ktoner cut for block reward, Perbill for calculating portion
pub ShareHolderReward get(share_holder_reward): Perbill = Perbill::from_billionths(400000000);


}

add_extra_genesis {
Expand Down Expand Up @@ -904,19 +909,19 @@ impl<T: Trait> Module<T> {
fn new_era() {
// Payout
let reward = <CurrentEraReward<T>>::take();
// TODO: for now block_reward == ktoner_rewardå
// TODO: for now block_reward is equal to ktoner_reward
let block_reward = Self::session_reward() * reward;
let validator_count = <RewardBalanceOf<T>>::sa(<CurrentElected<T>>::get().len() as u64);
let reward_per_validator = reward * block_reward.clone() / validator_count;
let holder_reward: u64 = <RewardBalanceOf<T>>::as_(Self::share_holder_reward() * reward);
let holder_reward = <T::Currency as SystemCurrency<T::AccountId>>::CurrencyOf::sa(holder_reward);
if !reward.is_zero() {
let validators = Self::current_elected();
for v in validators.iter() {
Self::reward_validator(v, reward);
Self::reward_validator(v, block_reward);
}
Self::deposit_event(RawEvent::Reward(reward));

// reward ktoner
<T::Currency as SystemCurrency<T::AccountId>>::reward_ktoner(block_reward.clone());
<T::Currency as SystemCurrency<T::AccountId>>::reward_ktoner(holder_reward);

// TODO: ready for hacking
// deprecated for now
Expand All @@ -940,9 +945,6 @@ impl<T: Trait> Module<T> {
let slot_stake: u64 = <BalanceOf<T>>::as_(Self::select_validators());

// Update the balances for rewarding according to the stakes.
// <CurrentSessionReward<T>>::put(Self::session_reward() * <RewardBalanceOf<T>>::sa(slot_stake));


let era_index = Self::current_era();
if ((era_index - Self::last_epoch_changed()) % Self::era_per_epoch()).is_zero() {
Self::new_epoch();
Expand All @@ -951,7 +953,8 @@ impl<T: Trait> Module<T> {

// change session reward
fn new_epoch() {
if let Ok(session_reward) = minting::compute_next_session_reward::<T>() {
let validator_count = <CurrentElected<T>>::get().len() as u64;
if let Ok(session_reward) = minting::compute_next_session_reward::<T>(validator_count) {
<CurrentSessionReward<T>>::put(session_reward);
}
}
Expand Down
13 changes: 6 additions & 7 deletions srml/staking/src/minting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ use crate::{Trait, RewardBalanceOf, Module};
use rstd::result;
use srml_support::traits::{Currency};
use primitives::traits::{Convert, Zero, One, As, StaticLookup, CheckedSub, Saturating, Bounded};

pub const YEAR: u64 = 31536000;

use kton::DECIMALS;

// change when new era
pub fn compute_next_session_reward<T: Trait>(
pub fn compute_next_session_reward<T: Trait>(validator_count: u64
) -> Result<RewardBalanceOf<T>, &'static str> {
//TODO: add decimal
//TODO: add collection of eras as a minimum set for changing session_reward
let epoch_length = <Module<T>>::era_per_epoch();
let era_length = <Module<T>>::sessions_per_era();
let session_number = era_length * epoch_length;
let cap : RewardBalanceOf<T> = As::sa(10000000000_u64);
let sessions_per_era = era_length * epoch_length;
let cap = <RewardBalanceOf<T>>::sa(10000000000 * DECIMALS);
let total_issuance_now = T::RewardCurrency::total_issuance();
if let Some(surplus) = cap.checked_sub(&total_issuance_now) {
Ok(surplus / (<RewardBalanceOf<T> as As<u64>>::sa(5_u64) * <RewardBalanceOf<T>>::sa(session_number.as_())))
// mint 20% of the rest
Ok(surplus / (<RewardBalanceOf<T> as As<u64>>::sa(5 * validator_count) * <RewardBalanceOf<T>>::sa(sessions_per_era.as_())))
} else {
return Err("too large.");
}
Expand Down
5 changes: 3 additions & 2 deletions srml/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use primitives::testing::{ UintAuthorityId, ConvertUintAuthorityId};
use substrate_primitives::{H256, Blake2Hasher};
use srml_support::impl_outer_origin;
use crate::{GenesisConfig, Module, Trait, StakerStatus};
use kton::DECIMALS;

impl_outer_origin!{
pub enum Origin for Test {}
Expand Down Expand Up @@ -127,9 +128,9 @@ impl ExtBuilder {

let (mut t, mut c) = system::GenesisConfig::<Test>::default().build_storage().unwrap();
let balance_factor = if self.existential_deposit > 0 {
1000
1000 * DECIMALS
} else {
1
1 * DECIMALS
};

let _ = timestamp::GenesisConfig::<Test> {
Expand Down
4 changes: 2 additions & 2 deletions srml/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate sr_std as rstd;

use codec::{Codec, Encode, Decode};
use srml_support::dispatch::Result;
use srml_support::traits::Imbalance;
use srml_support::traits::{Imbalance, Currency};
use sr_primitives::traits::{
Zero, SimpleArithmetic, As, StaticLookup, Member, CheckedAdd, CheckedSub,
MaybeSerializeDebug, Saturating
Expand All @@ -14,7 +14,7 @@ use rstd::{prelude::*, result};
// general interface
pub trait SystemCurrency<AccountId> {
// ring
type CurrencyOf: SimpleArithmetic + Codec + Copy + MaybeSerializeDebug + Default;
type CurrencyOf: SimpleArithmetic + Codec + Copy + MaybeSerializeDebug + Default + As<u64>;
type PositiveImbalance: Imbalance<Self::CurrencyOf, Opposite=Self::NegativeImbalance>;
type NegativeImbalance: Imbalance<Self::CurrencyOf, Opposite=Self::PositiveImbalance>;

Expand Down
8 changes: 8 additions & 0 deletions srml/token/kton/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub use imbalances::{PositiveImbalance, NegativeImbalance};
const DEPOSIT_ID: LockIdentifier = *b"lockkton";
const MONTH: u64 = 2592000;

pub const DECIMALS: u64 = 1000000000;

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default)]
#[cfg_attr(feature = "std", derive(Debug))]
Expand Down Expand Up @@ -208,6 +209,13 @@ decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default;

// root
pub fn set_total_issuance(total_issuance: T::Balance) -> Result {
<TotalIssuance<T>>::put(total_issuance);

Ok(())
}

pub fn transfer(
origin,
dest: <T::Lookup as StaticLookup>::Source,
Expand Down
4 changes: 2 additions & 2 deletions srml/token/kton/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ impl ExtBuilder {
pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> {
let (mut t, mut c) = system::GenesisConfig::<Test>::default().build_storage().unwrap();
let balance_factor = if self.existential_deposit > 0 {
1000
1000 * DECIMALS
} else {
1
1 * DECIMALS
};

let _ = timestamp::GenesisConfig::<Test> {
Expand Down
Loading