Skip to content

Commit

Permalink
Removed unit-creation-delay from runtime. Removed type wrappings in t…
Browse files Browse the repository at this point in the history
…he runtime. (#193)
  • Loading branch information
DamianStraszak authored Nov 15, 2021
1 parent 5ba6d0e commit c664456
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 129 deletions.
18 changes: 18 additions & 0 deletions bin/node/src/aleph_cli.rs
Original file line number Diff line number Diff line change
@@ -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<u64>,
}

impl AlephCli {
pub fn unit_creation_delay(&self) -> UnitCreationDelay {
UnitCreationDelay(
self.unit_creation_delay
.unwrap_or(DEFAULT_UNIT_CREATION_DELAY),
)
}
}
20 changes: 4 additions & 16 deletions bin/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -125,9 +125,6 @@ pub struct ChainParams {
#[structopt(long)]
pub millisecs_per_block: Option<u64>,

#[structopt(long)]
pub unit_creation_delay: Option<u64>,

#[structopt(long)]
pub chain_name: Option<String>,

Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion bin/node/src/cli.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -8,6 +8,9 @@ pub struct Cli {
#[structopt(subcommand)]
pub subcommand: Option<Subcommand>,

#[structopt(flatten)]
pub aleph: AlephCli,

#[structopt(flatten)]
pub run: RunCmd,
}
Expand Down
3 changes: 2 additions & 1 deletion bin/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down
1 change: 1 addition & 0 deletions bin/node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod aleph_cli;
pub mod chain_spec;
pub mod rpc;
pub mod service;
1 change: 1 addition & 0 deletions bin/node/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod chain_spec;
#[macro_use]
mod service;
mod aleph_cli;
mod cli;
mod command;
mod commands;
Expand Down
32 changes: 19 additions & 13 deletions bin/node/src/service.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -138,7 +140,10 @@ pub fn new_partial(
}

/// Builds a new service for a full client.
pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError> {
pub fn new_full(
mut config: Configuration,
aleph_config: AlephCli,
) -> Result<TaskManager, ServiceError> {
let sc_service::PartialComponents {
client,
backend,
Expand Down Expand Up @@ -167,20 +172,21 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
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;
Expand Down
49 changes: 28 additions & 21 deletions bin/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<I: From<u64>> ::frame_support::traits::Get<I> for MinimumPeriod {
Expand Down Expand Up @@ -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<I: From<u32>> ::frame_support::traits::Get<I> for SessionPeriod {
impl<I: From<u64>> ::frame_support::traits::Get<I> 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<I: From<u32>> ::frame_support::traits::Get<I> for SessionPeriod {
fn get() -> I {
I::from(Self::get())
}
}

impl pallet_session::Config for Runtime {
Expand Down Expand Up @@ -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()
}
}
}
19 changes: 0 additions & 19 deletions finality-aleph/src/config.rs

This file was deleted.

4 changes: 2 additions & 2 deletions finality-aleph/src/justification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 9 additions & 2 deletions finality-aleph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<B, BE>:
Expand Down
3 changes: 2 additions & 1 deletion finality-aleph/src/party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
Loading

0 comments on commit c664456

Please sign in to comment.