diff --git a/bin/node/src/aleph_cli.rs b/bin/node/src/aleph_cli.rs new file mode 100644 index 0000000000..aae854b6c0 --- /dev/null +++ b/bin/node/src/aleph_cli.rs @@ -0,0 +1,18 @@ +use aleph_primitives::DEFAULT_UNIT_CREATION_DELAY; +use finality_aleph::UnitCreationDelay; +use structopt::StructOpt; + +#[derive(Debug, StructOpt, Clone)] +pub struct AlephCli { + #[structopt(long)] + pub unit_creation_delay: Option, +} + +impl AlephCli { + pub fn unit_creation_delay(&self) -> UnitCreationDelay { + UnitCreationDelay( + self.unit_creation_delay + .unwrap_or(DEFAULT_UNIT_CREATION_DELAY), + ) + } +} diff --git a/bin/node/src/chain_spec.rs b/bin/node/src/chain_spec.rs index 7177689da9..f341c7b109 100644 --- a/bin/node/src/chain_spec.rs +++ b/bin/node/src/chain_spec.rs @@ -1,12 +1,12 @@ use aleph_primitives::{ AuthorityId as AlephId, ADDRESSES_ENCODING, DEFAULT_MILLISECS_PER_BLOCK, - DEFAULT_SESSION_PERIOD, DEFAULT_UNIT_CREATION_DELAY, TOKEN_DECIMALS, + DEFAULT_SESSION_PERIOD, TOKEN_DECIMALS, }; -use aleph_primitives::{MillisecsPerBlock, SessionPeriod, UnitCreationDelay}; use aleph_runtime::{ AccountId, AlephConfig, AuraConfig, BalancesConfig, GenesisConfig, SessionConfig, SessionKeys, Signature, SudoConfig, SystemConfig, VestingConfig, WASM_BINARY, }; +use finality_aleph::{MillisecsPerBlock, SessionPeriod}; use hex_literal::hex; use libp2p::PeerId; use sc_service::config::BasePath; @@ -125,9 +125,6 @@ pub struct ChainParams { #[structopt(long)] pub millisecs_per_block: Option, - #[structopt(long)] - pub unit_creation_delay: Option, - #[structopt(long)] pub chain_name: Option, @@ -164,13 +161,6 @@ impl ChainParams { ) } - pub fn unit_creation_delay(&self) -> UnitCreationDelay { - UnitCreationDelay( - self.unit_creation_delay - .unwrap_or(DEFAULT_UNIT_CREATION_DELAY), - ) - } - pub fn session_period(&self) -> SessionPeriod { SessionPeriod(self.session_period.unwrap_or(DEFAULT_SESSION_PERIOD)) } @@ -312,7 +302,6 @@ fn genesis( ) -> GenesisConfig { let session_period = chain_params.session_period(); let millisecs_per_block = chain_params.millisecs_per_block(); - let unit_creation_delay = chain_params.unit_creation_delay(); // NOTE: some combinations of bootstrap chain arguments can potentially // lead to duplicated rich accounts, e.g. if a root account is also an authority @@ -345,13 +334,12 @@ fn genesis( }, aleph: AlephConfig { authorities: vec![], - session_period, - millisecs_per_block, + session_period: session_period.0, + millisecs_per_block: millisecs_per_block.0, validators: authorities .iter() .map(|auth| auth.account_id.clone()) .collect(), - unit_creation_delay, }, session: SessionConfig { keys: authorities diff --git a/bin/node/src/cli.rs b/bin/node/src/cli.rs index 70d829e219..d1bf8ae326 100644 --- a/bin/node/src/cli.rs +++ b/bin/node/src/cli.rs @@ -1,5 +1,5 @@ use crate::commands::BootstrapNodeCmd; -use crate::{chain_spec, commands::BootstrapChainCmd}; +use crate::{aleph_cli::AlephCli, chain_spec, commands::BootstrapChainCmd}; use sc_cli::{ChainSpec, RunCmd, RuntimeVersion, SubstrateCli}; use structopt::StructOpt; @@ -8,6 +8,9 @@ pub struct Cli { #[structopt(subcommand)] pub subcommand: Option, + #[structopt(flatten)] + pub aleph: AlephCli, + #[structopt(flatten)] pub run: RunCmd, } diff --git a/bin/node/src/command.rs b/bin/node/src/command.rs index 5597b1b2d4..36c5fbd469 100644 --- a/bin/node/src/command.rs +++ b/bin/node/src/command.rs @@ -90,8 +90,9 @@ pub fn run() -> sc_cli::Result<()> { } None => { let runner = cli.create_runner(&cli.run)?; + let aleph_cli_config = cli.aleph; runner.run_node_until_exit(|config| async move { - service::new_full(config).map_err(sc_cli::Error::Service) + service::new_full(config, aleph_cli_config).map_err(sc_cli::Error::Service) }) } } diff --git a/bin/node/src/lib.rs b/bin/node/src/lib.rs index f117b8aae6..c260c565e6 100644 --- a/bin/node/src/lib.rs +++ b/bin/node/src/lib.rs @@ -1,3 +1,4 @@ +mod aleph_cli; pub mod chain_spec; pub mod rpc; pub mod service; diff --git a/bin/node/src/main.rs b/bin/node/src/main.rs index c590b78f1d..3b4355712f 100644 --- a/bin/node/src/main.rs +++ b/bin/node/src/main.rs @@ -1,6 +1,7 @@ mod chain_spec; #[macro_use] mod service; +mod aleph_cli; mod cli; mod command; mod commands; diff --git a/bin/node/src/service.rs b/bin/node/src/service.rs index 64707f7500..9d05b62d13 100644 --- a/bin/node/src/service.rs +++ b/bin/node/src/service.rs @@ -1,9 +1,11 @@ //! Service and ServiceFactory implementation. Specialized wrapper over substrate service. +use crate::aleph_cli::AlephCli; use aleph_primitives::AlephSessionApi; use aleph_runtime::{self, opaque::Block, RuntimeApi, MAX_BLOCK_SIZE}; use finality_aleph::{ run_aleph_consensus, AlephBlockImport, AlephConfig, JustificationNotification, Metrics, + MillisecsPerBlock, SessionPeriod, }; use futures::channel::mpsc; use log::warn; @@ -138,7 +140,10 @@ pub fn new_partial( } /// Builds a new service for a full client. -pub fn new_full(mut config: Configuration) -> Result { +pub fn new_full( + mut config: Configuration, + aleph_config: AlephCli, +) -> Result { let sc_service::PartialComponents { client, backend, @@ -167,20 +172,21 @@ pub fn new_full(mut config: Configuration) -> Result warp_sync: None, })?; - let session_period = client - .runtime_api() - .session_period(&BlockId::Number(Zero::zero())) - .unwrap(); + let session_period = SessionPeriod( + client + .runtime_api() + .session_period(&BlockId::Number(Zero::zero())) + .unwrap(), + ); - let millisecs_per_block = client - .runtime_api() - .millisecs_per_block(&BlockId::Number(Zero::zero())) - .unwrap(); + let millisecs_per_block = MillisecsPerBlock( + client + .runtime_api() + .millisecs_per_block(&BlockId::Number(Zero::zero())) + .unwrap(), + ); - let unit_creation_delay = client - .runtime_api() - .unit_creation_delay(&BlockId::Number(Zero::zero())) - .unwrap(); + let unit_creation_delay = aleph_config.unit_creation_delay(); let role = config.role.clone(); let force_authoring = config.force_authoring; diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 573d5b757a..c8dbb8f175 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -6,10 +6,6 @@ #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); -use primitives::{ - MillisecsPerBlock as MillisecsPerBlockPrimitive, SessionPeriod as SessionPeriodPrimitive, - UnitCreationDelay as UnitCreationDelayPrimitive, -}; use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; @@ -110,10 +106,10 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("aleph-node"), impl_name: create_runtime_str!("aleph-node"), authoring_version: 1, - spec_version: 2, + spec_version: 4, impl_version: 1, apis: RUNTIME_API_VERSIONS, - transaction_version: 1, + transaction_version: 2, }; /// This determines the average expected block time that we are targetting. @@ -219,7 +215,7 @@ impl pallet_aura::Config for Runtime { pub struct MinimumPeriod; impl MinimumPeriod { pub fn get() -> u64 { - Aleph::millisecs_per_block().0 / 2 + Aleph::millisecs_per_block() / 2 } } impl> ::frame_support::traits::Get for MinimumPeriod { @@ -321,26 +317,40 @@ impl_opaque_keys! { } } -pub struct SessionPeriod; +parameter_types! { + pub const Offset: u32 = 0; +} -impl SessionPeriod { - pub fn get() -> u32 { - Aleph::session_period().0 +parameter_types! { + pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(30); +} + +pub struct MillisecsPerBlock; + +impl MillisecsPerBlock { + pub fn get() -> u64 { + Aleph::millisecs_per_block() } } -impl> ::frame_support::traits::Get for SessionPeriod { +impl> ::frame_support::traits::Get for MillisecsPerBlock { fn get() -> I { I::from(Self::get()) } } -parameter_types! { - pub const Offset: u32 = 0; +pub struct SessionPeriod; + +impl SessionPeriod { + pub fn get() -> u32 { + Aleph::session_period() + } } -parameter_types! { - pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(30); +impl> ::frame_support::traits::Get for SessionPeriod { + fn get() -> I { + I::from(Self::get()) + } } impl pallet_session::Config for Runtime { @@ -570,16 +580,13 @@ impl_runtime_apis! { Aleph::authorities() } - fn session_period() -> SessionPeriodPrimitive { + fn session_period() -> u32 { Aleph::session_period() } - fn millisecs_per_block() -> MillisecsPerBlockPrimitive { + fn millisecs_per_block() -> u64 { Aleph::millisecs_per_block() } - fn unit_creation_delay() -> UnitCreationDelayPrimitive { - Aleph::unit_creation_delay() - } } } diff --git a/finality-aleph/src/config.rs b/finality-aleph/src/config.rs deleted file mode 100644 index bb8b8f9e77..0000000000 --- a/finality-aleph/src/config.rs +++ /dev/null @@ -1,19 +0,0 @@ -use std::time::Duration; - -/// Configuration for the Aleph protocol service. -pub struct Config { - /// The duration of a message to be sent across the network. - pub gossip_duration: Duration, - /// If the node is running as an authority. - pub is_authority: bool, - /// The name of this particular node. - pub name: Option, - /// The keystore which stores the keys. - pub keystore: Option, -} - -impl Config { - pub(crate) fn _name(&self) -> &str { - self.name.as_deref().unwrap_or("") - } -} diff --git a/finality-aleph/src/justification.rs b/finality-aleph/src/justification.rs index f6d0397d39..9fcee4ef63 100644 --- a/finality-aleph/src/justification.rs +++ b/finality-aleph/src/justification.rs @@ -3,10 +3,10 @@ use crate::{ finalization::finalize_block, last_block_of_session, metrics::Checkpoint, - network, session_id_from_block_num, Metrics, SessionId, SessionMap, + network, session_id_from_block_num, Metrics, SessionId, SessionMap, SessionPeriod, }; use aleph_bft::SignatureSet; -use aleph_primitives::{SessionPeriod, ALEPH_ENGINE_ID}; +use aleph_primitives::ALEPH_ENGINE_ID; use codec::{Decode, Encode}; use futures::{channel::mpsc, StreamExt}; use futures_timer::Delay; diff --git a/finality-aleph/src/lib.rs b/finality-aleph/src/lib.rs index 2b7a93dad0..7c182e5ecc 100644 --- a/finality-aleph/src/lib.rs +++ b/finality-aleph/src/lib.rs @@ -16,7 +16,6 @@ use sp_runtime::{ }; use std::{collections::HashMap, fmt::Debug, sync::Arc}; mod aggregator; -pub mod config; mod crypto; mod data_io; mod finalization; @@ -53,10 +52,18 @@ pub fn peers_set_config() -> sc_network::config::NonDefaultSetConfig { #[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd, Encode, Decode)] pub struct SessionId(pub u32); +#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd, Encode, Decode)] +pub struct SessionPeriod(pub u32); + +#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd, Encode, Decode)] +pub struct MillisecsPerBlock(pub u64); + +#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd, Encode, Decode)] +pub struct UnitCreationDelay(pub u64); + pub use crate::metrics::Metrics; use crate::party::{run_consensus_party, AlephParams}; pub use aleph_primitives::{AuthorityId, AuthorityPair, AuthoritySignature}; -use aleph_primitives::{MillisecsPerBlock, SessionPeriod, UnitCreationDelay}; use futures::channel::mpsc; pub trait ClientForAleph: diff --git a/finality-aleph/src/party.rs b/finality-aleph/src/party.rs index 854255354a..6a0d1f105a 100644 --- a/finality-aleph/src/party.rs +++ b/finality-aleph/src/party.rs @@ -16,11 +16,12 @@ use crate::{ split_network, AlephNetworkData, ConsensusNetwork, DataNetwork, NetworkData, SessionManager, }, session_id_from_block_num, AuthorityId, Future, Metrics, NodeIndex, SessionId, SessionMap, + SessionPeriod, UnitCreationDelay, }; use sp_keystore::CryptoStore; use aleph_bft::{DelayConfig, OrderedBatch, SpawnHandle}; -use aleph_primitives::{AlephSessionApi, SessionPeriod, UnitCreationDelay, KEY_TYPE}; +use aleph_primitives::{AlephSessionApi, KEY_TYPE}; use futures_timer::Delay; use futures::{ diff --git a/pallet/src/lib.rs b/pallet/src/lib.rs index d3c307d78c..1cfce5c79f 100644 --- a/pallet/src/lib.rs +++ b/pallet/src/lib.rs @@ -6,10 +6,6 @@ mod mock; mod tests; use frame_support::Parameter; -use primitives::{ - MillisecsPerBlock as MillisecsPerBlockPrimitive, SessionPeriod as SessionPeriodPrimitive, - UnitCreationDelay as UnitCreationDelayPrimitive, -}; use sp_std::prelude::*; use frame_support::{sp_runtime::BoundToRuntimeAppPublic, traits::OneSessionHandler}; @@ -27,7 +23,6 @@ pub mod pallet { use pallet_session::{Pallet as Session, SessionManager}; use primitives::{ ApiError as AlephApiError, DEFAULT_MILLISECS_PER_BLOCK, DEFAULT_SESSION_PERIOD, - DEFAULT_UNIT_CREATION_DELAY, }; #[pallet::type_value] @@ -98,49 +93,31 @@ pub mod pallet { #[pallet::getter(fn authorities)] pub(super) type Authorities = StorageValue<_, Vec, ValueQuery>; - const DEFAULT_SESSION_PERIOD_PRIMITIVE: SessionPeriodPrimitive = - SessionPeriodPrimitive(DEFAULT_SESSION_PERIOD); - const DEFAULT_MILLISECS_PER_BLOCK_PRIMITIVE: MillisecsPerBlockPrimitive = - MillisecsPerBlockPrimitive(DEFAULT_MILLISECS_PER_BLOCK); - const DEFAULT_UNIT_CREATION_DELAY_PRIMITIVE: UnitCreationDelayPrimitive = - UnitCreationDelayPrimitive(DEFAULT_UNIT_CREATION_DELAY); - #[pallet::type_value] - pub(super) fn DefaultForSessionPeriod() -> SessionPeriodPrimitive { - DEFAULT_SESSION_PERIOD_PRIMITIVE + pub(super) fn DefaultForSessionPeriod() -> u32 { + DEFAULT_SESSION_PERIOD } #[pallet::storage] #[pallet::getter(fn session_period)] pub(super) type SessionPeriod = - StorageValue<_, SessionPeriodPrimitive, ValueQuery, DefaultForSessionPeriod>; + StorageValue<_, u32, ValueQuery, DefaultForSessionPeriod>; #[pallet::type_value] - pub(super) fn DefaultForMillisecsPerBlock() -> MillisecsPerBlockPrimitive { - DEFAULT_MILLISECS_PER_BLOCK_PRIMITIVE + pub(super) fn DefaultForMillisecsPerBlock() -> u64 { + DEFAULT_MILLISECS_PER_BLOCK } #[pallet::storage] #[pallet::getter(fn millisecs_per_block)] pub(super) type MillisecsPerBlock = - StorageValue<_, MillisecsPerBlockPrimitive, ValueQuery, DefaultForMillisecsPerBlock>; - - #[pallet::type_value] - pub(super) fn DefaultForUnitCreationDelay() -> UnitCreationDelayPrimitive { - DEFAULT_UNIT_CREATION_DELAY_PRIMITIVE - } - - #[pallet::storage] - #[pallet::getter(fn unit_creation_delay)] - pub(super) type UnitCreationDelay = - StorageValue<_, UnitCreationDelayPrimitive, ValueQuery, DefaultForUnitCreationDelay>; + StorageValue<_, u64, ValueQuery, DefaultForMillisecsPerBlock>; #[pallet::genesis_config] pub struct GenesisConfig { pub authorities: Vec, - pub session_period: SessionPeriodPrimitive, - pub millisecs_per_block: MillisecsPerBlockPrimitive, - pub unit_creation_delay: UnitCreationDelayPrimitive, + pub session_period: u32, + pub millisecs_per_block: u64, pub validators: Vec, } @@ -149,9 +126,8 @@ pub mod pallet { fn default() -> Self { Self { authorities: Vec::new(), - session_period: DEFAULT_SESSION_PERIOD_PRIMITIVE, - millisecs_per_block: DEFAULT_MILLISECS_PER_BLOCK_PRIMITIVE, - unit_creation_delay: DEFAULT_UNIT_CREATION_DELAY_PRIMITIVE, + session_period: DEFAULT_SESSION_PERIOD, + millisecs_per_block: DEFAULT_MILLISECS_PER_BLOCK, validators: Vec::new(), } } @@ -162,7 +138,6 @@ pub mod pallet { fn build(&self) { >::put(&self.session_period); >::put(&self.millisecs_per_block); - >::put(&self.unit_creation_delay); >::put(Some(&self.validators)); >::put(Some(0)); } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 75debca413..87e5c83831 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -1,8 +1,6 @@ #![allow(clippy::too_many_arguments, clippy::unnecessary_mut_passed)] #![cfg_attr(not(feature = "std"), no_std)] use codec::{Decode, Encode}; -#[cfg(feature = "std")] -use serde::{Deserialize, Serialize}; use sp_core::crypto::KeyTypeId; use sp_runtime::ConsensusEngineId; use sp_std::vec::Vec; @@ -26,21 +24,9 @@ sp_application_crypto::with_pair! { pub type AuthoritySignature = app::Signature; pub type AuthorityId = app::Public; -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd, Encode, Decode)] -pub struct SessionPeriod(pub u32); - -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd, Encode, Decode)] -pub struct MillisecsPerBlock(pub u64); - -/// A delay for unit creation in milliseconds. -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd, Encode, Decode)] -pub struct UnitCreationDelay(pub u64); - pub const DEFAULT_SESSION_PERIOD: u32 = 900; pub const DEFAULT_MILLISECS_PER_BLOCK: u64 = 1000; + pub const TOKEN_DECIMALS: u32 = 12; pub const ADDRESSES_ENCODING: u32 = 42; pub const DEFAULT_UNIT_CREATION_DELAY: u64 = 300; @@ -55,8 +41,7 @@ sp_api::decl_runtime_apis! { { fn next_session_authorities() -> Result, ApiError>; fn authorities() -> Vec; - fn session_period() -> SessionPeriod; - fn millisecs_per_block() -> MillisecsPerBlock; - fn unit_creation_delay() -> UnitCreationDelay; + fn session_period() -> u32; + fn millisecs_per_block() -> u64; } } diff --git a/run_nodes.sh b/run_nodes.sh index 14640fc04c..81d2463883 100755 --- a/run_nodes.sh +++ b/run_nodes.sh @@ -85,6 +85,7 @@ for i in $(seq 0 "$(( N_VALIDATORS + N_NON_VALIDATORS - 1 ))"); do --port $((30334 + i)) \ --bootnodes $bootnodes \ --node-key-file $BASE_PATH/$account_id/p2p_secret \ + --unit-creation-delay 500 \ --execution Native \ --no-mdns \ -lafa=debug \