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

Staking implementation #114

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions Cargo.lock

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

13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ slices = "0.2.0"
smallvec = "1.10.0"
serde = { version = "1.0.152", default-features = false }
serde_json = "1.0.92"
itertools = { version = "0.11", default-features = false, features = [
"use_alloc",
] }
rustc-hex = { version = "2.0.1", default-features = false }

# Substrate
frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.11.0" }
Expand Down Expand Up @@ -80,9 +84,11 @@ sp-genesis-builder = { git = "https://github.com/paritytech/polkadot-sdk", defau
sp-inherents = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.11.0" }
sp-io = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.11.0", default-features = false }
sp-keystore = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.11.0" }
sp-keyring = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.11.0", default-features = false }
sp-offchain = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.11.0" }
sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.11.0" }
sp-session = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.11.0" }
sp-staking = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.11.0" }
sp-std = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.11.0" }
sp-timestamp = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.11.0" }
sp-transaction-pool = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.11.0" }
Expand Down Expand Up @@ -177,6 +183,13 @@ xcm = { package = "staging-xcm", git = "https://github.com/paritytech/polkadot-s
xcm-builder = { package = "staging-xcm-builder", git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.11.0" }
xcm-executor = { package = "staging-xcm-executor", git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.11.0" }

# Staking
substrate-fixed = { git = "https://github.com/encointer/substrate-fixed", default-features = false }
pallet-parachain-staking = { path = "pallets/parachain-staking", default-features = false }
#pallet-evm-precompile-parachain-staking = { path = "precompiles/parachain-staking", default-features = false }
# num-integer = { version = "0.1", default-features = false }
# sp-consensus-slots = { git = "https://github.com/paritytech/polkadot-sdk", default-features = false, branch = "release-polkadot-v1.11.0" }

# Local
neuroweb-runtime = { path = "./runtime" }
precompile-utils = { path = "./precompiles/utils", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ codec = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["arbitrary_precision"] }
jsonrpsee = { workspace = true }
itertools = { workspace = true }

# Local
neuroweb-runtime = { workspace = true }
Expand Down
51 changes: 41 additions & 10 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
use cumulus_primitives_core::ParaId;
use neuroweb_runtime::{AccountId, AuraId,
EVMConfig, Signature, EXISTENTIAL_DEPOSIT};
use neuroweb_runtime::{pallet_parachain_staking::{
inflation::{perbill_annual_to_perbill_round, BLOCKS_PER_YEAR},
InflationInfo, Range,
}, AccountId, AuraId, Balance, EVMConfig, MinCandidateStk, Signature, OTP};
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
use sc_service::ChainType;
use serde::{Deserialize, Serialize};
use sp_core::{sr25519, Pair, Public, H160, U256};
use sp_runtime::traits::{IdentifyAccount, Verify};
use sp_runtime::{Perbill, Percent, traits::{IdentifyAccount, Verify}};
use std::{collections::BTreeMap, str::FromStr};
use itertools::Itertools;

/// Specialized `ChainSpec` for the normal parachain runtime.
pub type ChainSpec =
sc_service::GenericChainSpec<neuroweb_runtime::RuntimeGenesisConfig, Extensions>;

/// The default XCM version to set in genesis config.
const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION;
pub const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION;
pub const COLLATOR_COMMISSION: Perbill = Perbill::from_percent(10);
pub const PARACHAIN_BOND_RESERVE_PERCENT: Percent = Percent::from_percent(0);
pub const BLOCKS_PER_ROUND: u32 = 3600; // 6 hours of blocks
pub const NUM_SELECTED_CANDIDATES: u32 = 1; // For start

pub fn neuroweb_inflation_config() -> InflationInfo<Balance> {
fn to_round_inflation(annual: Range<Perbill>) -> Range<Perbill> {
perbill_annual_to_perbill_round(
annual,
// rounds per year
BLOCKS_PER_YEAR / BLOCKS_PER_ROUND,
)
}
let annual =
Range { min: Perbill::from_percent(2), ideal: Perbill::from_percent(3), max: Perbill::from_percent(3) };

InflationInfo {
// staking expectations
expect: Range { min: 100_000 * OTP, ideal: 200_000 * OTP, max: 500_000 * OTP },
// annual inflation
annual,
round: to_round_inflation(annual),
}
}

/// Helper function to generate a crypto pair from seed
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
Expand Down Expand Up @@ -174,13 +201,8 @@ fn testnet_genesis(
parachain_id: id,
..Default::default()
},
collator_selection: neuroweb_runtime::CollatorSelectionConfig {
invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect(),
candidacy_bond: EXISTENTIAL_DEPOSIT * 16,
..Default::default()
},
session: neuroweb_runtime::SessionConfig {
keys: invulnerables
keys: invulnerables.clone()
.into_iter()
.map(|(acc, aura)| {
(
Expand Down Expand Up @@ -242,6 +264,15 @@ fn testnet_genesis(
council: Default::default(),
democracy: Default::default(),
transaction_payment: Default::default(),
parachain_staking: neuroweb_runtime::ParachainStakingConfig {
candidates: invulnerables.clone().into_iter().map(|account| (account.0, MinCandidateStk::get())).collect_vec(),
delegations: vec![],
blocks_per_round: BLOCKS_PER_ROUND,
num_selected_candidates: NUM_SELECTED_CANDIDATES,
parachain_bond_reserve_percent: PARACHAIN_BOND_RESERVE_PERCENT,
collator_commission: COLLATOR_COMMISSION,
inflation_config: neuroweb_inflation_config()
}
};

serde_json::to_value(&config).expect("Could not build genesis config.")
Expand Down
1 change: 1 addition & 0 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ pub fn run() -> Result<()> {
match cmd {
BenchmarkCmd::Pallet(cmd) => {
if cfg!(feature = "runtime-benchmarks") {
#[allow(deprecated)]
runner.sync_run(|config| cmd.run::<HashingFor<Block>, ()>(config))
} else {
Err("Benchmarking wasn't enabled when building the node. \
Expand Down
79 changes: 79 additions & 0 deletions pallets/parachain-staking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
[package]
name = "pallet-parachain-staking"
description = "Parachain staking pallet for collator selection and reward distribution"
authors = { workspace = true }
edition = "2021"
version = "1.0.0"

[dependencies]
serde = { workspace = true }
log = { workspace = true }

codec = { workspace = true, features = [
"derive",
] }
scale-info = { workspace = true, features = ["derive"] }

# FRAME
frame-benchmarking = { workspace = true, optional = true }
frame-support.workspace = true
frame-system.workspace = true
sp-runtime.workspace = true
sp-std.workspace = true
sp-core.workspace = true
substrate-fixed.workspace = true
sp-staking.workspace = true
pallet-authorship.workspace = true
pallet-session.workspace = true

[dev-dependencies]
pallet-balances.workspace = true
pallet-aura.workspace = true
pallet-timestamp.workspace = true
similar-asserts = "1.1.0"
sp-io.workspace = true
sp-consensus-aura.workspace = true

[features]
default = [ "std" ]
std = [
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-aura/std",
"pallet-authorship/std",
"pallet-balances/std",
"pallet-session/std",
"pallet-timestamp/std",
"codec/std",
"scale-info/std",
"serde/std",
"sp-consensus-aura/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-staking/std",
"sp-std/std",
"substrate-fixed/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-benchmarking?/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"sp-staking/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-aura/try-runtime",
"pallet-authorship/try-runtime",
"pallet-balances/try-runtime",
"pallet-session/try-runtime",
"pallet-timestamp/try-runtime",
"sp-runtime/try-runtime",
]
Loading
Loading