Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Sassafras consensus - Prototype 2.1 #11889

Merged
merged 14 commits into from
Aug 23, 2022
4 changes: 2 additions & 2 deletions Cargo.lock

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

12 changes: 10 additions & 2 deletions bin/node-sassafras/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ substrate-build-script-utils = { version = "3.0.0", path = "../../../utils/build

[features]
default = []
runtime-benchmarks = ["node-sassafras-runtime/runtime-benchmarks"]
runtime-benchmarks = [
"node-sassafras-runtime/runtime-benchmarks"
]
# Enable features that allow the runtime to be tried and debugged. Name might be subject to change
# in the near future.
try-runtime = ["node-sassafras-runtime/try-runtime", "try-runtime-cli"]
try-runtime = [
"node-sassafras-runtime/try-runtime",
"try-runtime-cli"
]
use-session-pallet = [
"node-sassafras-runtime/use-session-pallet"
]
75 changes: 40 additions & 35 deletions bin/node-sassafras/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ use node_sassafras_runtime::{
AccountId, BalancesConfig, GenesisConfig, GrandpaConfig, SassafrasConfig, Signature,
SudoConfig, SystemConfig, WASM_BINARY,
};
#[cfg(feature = "use-session-pallet")]
use node_sassafras_runtime::{SessionConfig, SessionKeys};
use sc_service::ChainType;
use sp_consensus_sassafras::AuthorityId as SassafrasId;
use sp_consensus_sassafras::{AuthorityId as SassafrasId, SassafrasEpochConfiguration};
use sp_core::{sr25519, Pair, Public};
use sp_finality_grandpa::AuthorityId as GrandpaId;
use sp_runtime::traits::{IdentifyAccount, Verify};

// The URL for the telemetry server.
// const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/";
// Genesis constants for Sassafras parameters configuration.
const SASSAFRAS_TICKETS_MAX_ATTEMPTS_NUMBER: u32 = 32;
const SASSAFRAS_TICKETS_REDUNDANCY_FACTOR: u32 = 1;

/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.
pub type ChainSpec = sc_service::GenericChainSpec<GenesisConfig>;
Expand All @@ -23,55 +26,48 @@ pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Pu

type AccountPublic = <Signature as Verify>::Signer;

/// Generate an account ID from seed.
/// Generate an account id from seed.
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}

/// Generate authority keys from seed.
pub fn authority_keys_from_seed(s: &str) -> (SassafrasId, GrandpaId) {
(get_from_seed::<SassafrasId>(s), get_from_seed::<GrandpaId>(s))
/// Generate authority account id and keys from seed.
pub fn authority_keys_from_seed(seed: &str) -> (AccountId, SassafrasId, GrandpaId) {
(
get_account_id_from_seed::<sr25519::Public>(seed),
get_from_seed::<SassafrasId>(seed),
get_from_seed::<GrandpaId>(seed),
)
}

pub fn development_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;

Ok(ChainSpec::from_genesis(
// Name
"Development",
// ID
"dev",
ChainType::Development,
move || {
testnet_genesis(
wasm_binary,
// Initial PoA authorities
vec![authority_keys_from_seed("Alice")],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
],
true,
)
},
// Bootnodes
vec![],
// Telemetry
None,
// Protocol ID
None,
None,
// Properties
None,
// Extensions
None,
))
}
Expand All @@ -80,19 +76,14 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;

Ok(ChainSpec::from_genesis(
// Name
"Local Testnet",
// ID
"local_testnet",
ChainType::Local,
move || {
testnet_genesis(
wasm_binary,
// Initial PoA authorities
vec![authority_keys_from_seed("Alice"), authority_keys_from_seed("Bob")],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
Expand All @@ -107,30 +98,23 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
],
true,
)
},
// Bootnodes
vec![],
// Telemetry
None,
// Protocol ID
None,
// Properties
None,
None,
// Extensions
None,
))
}

/// Configure initial storage state for FRAME modules.
fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(SassafrasId, GrandpaId)>,
initial_authorities: Vec<(AccountId, SassafrasId, GrandpaId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool,
) -> GenesisConfig {
GenesisConfig {
system: SystemConfig {
Expand All @@ -141,18 +125,39 @@ fn testnet_genesis(
// Configure endowed accounts with initial balance of 1 << 60.
balances: endowed_accounts.iter().cloned().map(|k| (k, 1 << 60)).collect(),
},

sassafras: SassafrasConfig {
authorities: initial_authorities.iter().map(|x| (x.0.clone(), 0)).collect(),
epoch_config: Some(node_sassafras_runtime::SASSAFRAS_GENESIS_EPOCH_CONFIG),
#[cfg(feature = "use-session-pallet")]
authorities: vec![],
#[cfg(not(feature = "use-session-pallet"))]
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 0)).collect(),
epoch_config: SassafrasEpochConfiguration {
attempts_number: SASSAFRAS_TICKETS_MAX_ATTEMPTS_NUMBER,
redundancy_factor: SASSAFRAS_TICKETS_REDUNDANCY_FACTOR,
},
},
grandpa: GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
#[cfg(feature = "use-session-pallet")]
authorities: vec![],
#[cfg(not(feature = "use-session-pallet"))]
authorities: initial_authorities.iter().map(|x| (x.2.clone(), 1)).collect(),
},
sudo: SudoConfig {
// Assign network admin rights.
key: Some(root_key),
},
transaction_payment: Default::default(),
#[cfg(feature = "use-session-pallet")]
session: SessionConfig {
keys: initial_authorities
.iter()
.map(|x| {
(
x.0.clone(),
x.0.clone(),
SessionKeys { sassafras: x.1.clone(), grandpa: x.2.clone() },
)
})
.collect::<Vec<_>>(),
},
}
}
4 changes: 4 additions & 0 deletions bin/node-sassafras/node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub enum Subcommand {
/// Revert the chain to a previous state.
Revert(sc_cli::RevertCmd),

/// Sub-commands concerned with benchmarking.
#[clap(subcommand)]
Benchmark(frame_benchmarking_cli::BenchmarkCmd),

/// Try some command against runtime state.
#[cfg(feature = "try-runtime")]
TryRuntime(try_runtime_cli::TryRuntimeCmd),
Expand Down
26 changes: 26 additions & 0 deletions bin/node-sassafras/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
cli::{Cli, Subcommand},
service,
};
use frame_benchmarking_cli::BenchmarkCmd;
use node_sassafras_runtime::Block;
use sc_cli::{ChainSpec, RuntimeVersion, SubstrateCli};
use sc_service::PartialComponents;
Expand Down Expand Up @@ -102,6 +103,31 @@ pub fn run() -> sc_cli::Result<()> {
Ok((cmd.run(client, backend, Some(aux_revert)), task_manager))
})
},
Some(Subcommand::Benchmark(cmd)) => {
let runner = cli.create_runner(cmd)?;

runner.sync_run(|config| {
// This switch needs to be in the client, since the client decides
// which sub-commands it wants to support.
match cmd {
BenchmarkCmd::Pallet(cmd) => {
if !cfg!(feature = "runtime-benchmarks") {
return Err(
"Runtime benchmarking wasn't enabled when building the node. \
You can enable it with `--features runtime-benchmarks`."
.into(),
)
}

cmd.run::<Block, service::ExecutorDispatch>(config)
},
_ => {
println!("Not implemented...");
Ok(())
},
}
})
},
#[cfg(feature = "try-runtime")]
Some(Subcommand::TryRuntime(cmd)) => {
let runner = cli.create_runner(cmd)?;
Expand Down
6 changes: 3 additions & 3 deletions bin/node-sassafras/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ pub fn new_partial(
let justification_import = grandpa_block_import.clone();

let (sassafras_block_import, sassafras_link) = sc_consensus_sassafras::block_import(
sc_consensus_sassafras::Config::get(&*client)?,
sc_consensus_sassafras::configuration(&*client)?,
grandpa_block_import,
client.clone(),
)?;

let slot_duration = sassafras_link.config().slot_duration();
let slot_duration = sassafras_link.genesis_config().slot_duration();

let import_queue = sc_consensus_sassafras::import_queue(
sassafras_link.clone(),
Expand Down Expand Up @@ -269,7 +269,7 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
let can_author_with =
sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone());

let slot_duration = sassafras_link.config().slot_duration();
let slot_duration = sassafras_link.genesis_config().slot_duration();

let sassafras_config = sc_consensus_sassafras::SassafrasParams {
client: client.clone(),
Expand Down
7 changes: 4 additions & 3 deletions bin/node-sassafras/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ scale-info = { version = "2.1.1", default-features = false, features = ["derive"

pallet-sassafras = { version = "0.1.0", default-features = false, path = "../../../frame/sassafras" }
pallet-balances = { version = "4.0.0-dev", default-features = false, path = "../../../frame/balances" }
pallet-session = { version = "4.0.0-dev", default-features = false, path = "../../../frame/session" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../../../frame/support" }
pallet-grandpa = { version = "4.0.0-dev", default-features = false, path = "../../../frame/grandpa" }
pallet-randomness-collective-flip = { version = "4.0.0-dev", default-features = false, path = "../../../frame/randomness-collective-flip" }
pallet-sudo = { version = "4.0.0-dev", default-features = false, path = "../../../frame/sudo" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../../frame/system" }
frame-try-runtime = { version = "0.10.0-dev", default-features = false, path = "../../../frame/try-runtime", optional = true }
Expand Down Expand Up @@ -62,8 +62,8 @@ std = [
"pallet-sassafras/std",
"pallet-balances/std",
"pallet-grandpa/std",
"pallet-randomness-collective-flip/std",
"pallet-sudo/std",
"pallet-session/std",
"pallet-timestamp/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-transaction-payment/std",
Expand All @@ -87,6 +87,7 @@ runtime-benchmarks = [
"hex-literal",
"pallet-balances/runtime-benchmarks",
"pallet-grandpa/runtime-benchmarks",
"pallet-sassafras/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
Expand All @@ -96,8 +97,8 @@ try-runtime = [
"frame-system/try-runtime",
"pallet-balances/try-runtime",
"pallet-grandpa/try-runtime",
"pallet-randomness-collective-flip/try-runtime",
"pallet-sudo/try-runtime",
"pallet-timestamp/try-runtime",
"pallet-transaction-payment/try-runtime",
]
use-session-pallet = []
Loading