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

introducing staking, ready for hacking #2

Merged
merged 1 commit into from
Jun 3, 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
28 changes: 28 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ members = [
"srml/token/kton",
"srml/contract",
"srml/try",
"srml/staking",
]
exclude = ["runtime/wasm"]

Expand Down
12 changes: 12 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ default_features = false
git = 'https://github.com/paritytech/substrate.git'
package = 'srml-timestamp'

[dependencies.session]
default_features = false
git = 'https://github.com/paritytech/substrate.git'
package = 'srml-session'

[dependencies.staking]
default_features = false
path = "../srml/staking"
package = 'evo-staking'

[dependencies.version]
default_features = false
git = 'https://github.com/paritytech/substrate.git'
Expand Down Expand Up @@ -143,5 +153,7 @@ std = [
'serde/std',
'safe-mix/std',
'consensus-aura/std',
"session/std",
"staking/std",
'offchain-primitives/std',
]
35 changes: 34 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use primitives::{ed25519, sr25519, OpaqueMetadata};

use runtime_primitives::{
ApplyResult, transaction_validity::TransactionValidity, generic, create_runtime_str,
traits::{self, NumberFor, BlakeTwo256, Block as BlockT, StaticLookup, Verify, AuthorityIdFor}
traits::{self, NumberFor, BlakeTwo256, Block as BlockT, StaticLookup, Verify, AuthorityIdFor, Convert}
};
use client::{
block_builder::api::{CheckInherentsResult, InherentData, self as block_builder_api},
Expand All @@ -33,6 +33,7 @@ use client::{
use version::RuntimeVersion;
#[cfg(feature = "std")]
use version::NativeVersion;
pub use staking::StakerStatus;

// A few exports that help ease life for downstream crates.
#[cfg(any(feature = "std", test))]
Expand Down Expand Up @@ -116,6 +117,20 @@ pub fn native_version() -> NativeVersion {
}
}

pub struct CurrencyToVoteHandler;

impl CurrencyToVoteHandler {
fn factor() -> u128 { (Balances::total_issuance() / u64::max_value() as u128).max(1) }
}

impl Convert<u128, u64> for CurrencyToVoteHandler {
fn convert(x: u128) -> u64 { (x / Self::factor()) as u64 }
}

impl Convert<u128, u128> for CurrencyToVoteHandler {
fn convert(x: u128) -> u128 { x * Self::factor() }
}

impl system::Trait for Runtime {
/// The identifier used to distinguish between accounts.
type AccountId = AccountId;
Expand Down Expand Up @@ -173,6 +188,13 @@ impl timestamp::Trait for Runtime {
type OnTimestampSet = Aura;
}

impl session::Trait for Runtime {
type ConvertAccountIdToSessionKey = ();
type OnSessionChange = Staking;
type Event = Event;
}


impl balances::Trait for Runtime {
/// The type for recording an account's balance.
type Balance = u128;
Expand Down Expand Up @@ -226,6 +248,15 @@ impl kton::Trait for Runtime {
type SystemRefund = ();
}

impl staking::Trait for Runtime {
type Currency = Kton;
type CurrencyToVote = CurrencyToVoteHandler;
type OnRewardMinted = ();
type Event = Event;
type Slash = ();
type Reward = ();
}

impl contract::Trait for Runtime {
type SystemCurrency = Kton;
type Event = Event;
Expand All @@ -249,6 +280,8 @@ construct_runtime!(
Ring: ring,
Kton: kton::{Module, Call, Config<T>, Storage, Event<T>},
Contract: contract,
Session: session,
Staking: staking,
}
);

Expand Down
25 changes: 25 additions & 0 deletions runtime/wasm/Cargo.lock

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

82 changes: 68 additions & 14 deletions src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use primitives::{ed25519, sr25519, Pair, crypto::UncheckedInto};
use node_template_runtime::{
AccountId, GenesisConfig, ConsensusConfig, TimestampConfig, BalancesConfig,
SudoConfig, IndicesConfig, RingConfig, KtonConfig, ContractConfig,
SudoConfig, IndicesConfig, RingConfig, KtonConfig, ContractConfig, SessionConfig, StakingConfig,
StakerStatus, Permill, Perbill
};
use substrate_service;
use hex_literal::hex;
Expand Down Expand Up @@ -44,11 +45,11 @@ impl Alternative {
"Development",
"dev",
|| testnet_genesis(vec![
authority_key("Alice")
get_authority_keys_from_seed("Alice"),
], vec![
account_key("Alice")
],
account_key("Alice")
get_account_id_from_seed("Alice"),
),
vec![],
None,
Expand All @@ -60,17 +61,17 @@ impl Alternative {
"Local Testnet",
"local_testnet",
|| testnet_genesis(vec![
authority_key("Alice"),
authority_key("Bob"),
get_authority_keys_from_seed("Alice"),
get_authority_keys_from_seed("Bob"),
], vec![
account_key("Alice"),
account_key("Bob"),
account_key("Charlie"),
account_key("Dave"),
account_key("Eve"),
account_key("Ferdie"),
get_account_id_from_seed("Alice"),
get_account_id_from_seed("Bob"),
get_account_id_from_seed("Charlie"),
get_account_id_from_seed("Dave"),
get_account_id_from_seed("Eve"),
get_account_id_from_seed("Ferdie"),
],
account_key("Alice"),
get_account_id_from_seed("Alice"),
),
vec![],
None,
Expand All @@ -89,12 +90,23 @@ impl Alternative {
}
}
}
const MILLICENTS: u128 = 1_000_000_000;
const CENTS: u128 = 1_000 * MILLICENTS; // assume this is worth about a cent.
const DOLLARS: u128 = 100 * CENTS;

fn testnet_genesis(initial_authorities: Vec<AuthorityId>, endowed_accounts: Vec<AccountId>, root_key: AccountId) -> GenesisConfig {
const SECS_PER_BLOCK: u64 = 6;
const MINUTES: u64 = 60 / SECS_PER_BLOCK;
const HOURS: u64 = MINUTES * 60;
const DAYS: u64 = HOURS * 24;

const ENDOWMENT: u128 = 10_000_000 * DOLLARS;
const STASH: u128 = 100 * DOLLARS;

fn testnet_genesis(initial_authorities: Vec<(AccountId, AccountId, AuthorityId)>, endowed_accounts: Vec<AccountId>, root_key: AccountId) -> GenesisConfig {
GenesisConfig {
consensus: Some(ConsensusConfig {
code: include_bytes!("../runtime/wasm/target/wasm32-unknown-unknown/release/node_template_runtime_wasm.compact.wasm").to_vec(),
authorities: initial_authorities.clone(),
authorities: initial_authorities.iter().map(|x| x.2.clone()).collect(),
}),
system: None,
timestamp: Some(TimestampConfig {
Expand All @@ -115,6 +127,24 @@ fn testnet_genesis(initial_authorities: Vec<AuthorityId>, endowed_accounts: Vec<
sudo: Some(SudoConfig {
key: root_key,
}),
session: Some(SessionConfig {
validators: initial_authorities.iter().map(|x| x.1.clone()).collect(),
session_length: 10,
keys: initial_authorities.iter().map(|x| (x.1.clone(), x.2.clone())).collect::<Vec<_>>(),
}),
staking: Some(StakingConfig {
current_era: 0,
minimum_validator_count: 1,
validator_count: 2,
sessions_per_era: 5,
bonding_duration: 12,
offline_slash: Perbill::zero(),
session_reward: Perbill::zero(),
current_session_reward: 0,
offline_slash_grace: 0,
stakers: initial_authorities.iter().map(|x| (x.0.clone(), x.1.clone(), STASH, StakerStatus::Validator)).collect(),
invulnerables: initial_authorities.iter().map(|x| x.1.clone()).collect(),
}),
ring: Some(RingConfig {
transaction_base_fee: 1,
transaction_byte_fee: 0,
Expand All @@ -136,3 +166,27 @@ fn testnet_genesis(initial_authorities: Vec<AuthorityId>, endowed_accounts: Vec<
}),
}
}

/// Helper function to generate AccountId from seed
pub fn get_account_id_from_seed(seed: &str) -> AccountId {
sr25519::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}

/// Helper function to generate AuthorityId from seed
pub fn get_session_key_from_seed(seed: &str) -> AuthorityId {
ed25519::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}


/// Helper function to generate stash, controller and session key from seed
pub fn get_authority_keys_from_seed(seed: &str) -> (AccountId, AccountId, AuthorityId) {
(
get_account_id_from_seed(&format!("{}//stash", seed)),
get_account_id_from_seed(seed),
get_session_key_from_seed(seed)
)
}
48 changes: 48 additions & 0 deletions srml/staking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[package]
name = "evo-staking"
version = "0.1.0"
authors = ["hammeWang <[email protected]>"]
edition = "2018"

[dependencies]
hex-literal = "0.1.0"
serde = { version = "1.0", default-features = false }
serde_derive = { version = "1.0", optional = true }
safe-mix = { version = "1.0", default-features = false}
parity-codec = { version = "3.0", default-features = false }
parity-codec-derive = { version = "3.0", default-features = false }
substrate-keyring = { git = "https://github.com/paritytech/substrate", optional = true }
substrate-primitives = { git = "https://github.com/paritytech/substrate", default-features = false }
rstd = { package ="sr-std", git = "https://github.com/paritytech/substrate", default-features = false }
primitives = { package ="sr-primitives", git = "https://github.com/paritytech/substrate", default-features = false }
srml-support = { git = "https://github.com/paritytech/substrate", default-features = false }
system = { package ="srml-system", git = "https://github.com/paritytech/substrate", default-features = false }
balances = { package ="srml-balances", git = "https://github.com/paritytech/substrate", default_features = false }
timestamp = { package ="srml-timestamp", git = "https://github.com/paritytech/substrate", default_features = false }
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 }

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

[features]
default = ["std"]
std = [
"serde/std",
"serde_derive",
"safe-mix/std",
"parity-codec/std",
"parity-codec-derive/std",
"substrate-primitives/std",
"rstd/std",
"srml-support/std",
"balances/std",
"primitives/std",
"system/std",
"timestamp/std",
"session/std",
"consensus/std",
]
Loading