Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
11 changes: 4 additions & 7 deletions Cargo.lock

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

6 changes: 2 additions & 4 deletions node-template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ substrate-service = { path = "../core/service" }
inherents = { package = "substrate-inherents", path = "../core/inherents" }
transaction-pool = { package = "substrate-transaction-pool", path = "../core/transaction-pool" }
network = { package = "substrate-network", path = "../core/network" }
babe = { package = "substrate-consensus-babe", path = "../core/consensus/babe" }
babe-primitives = { package = "substrate-consensus-babe-primitives", path = "../core/consensus/babe/primitives" }
grandpa = { package = "substrate-finality-grandpa", path = "../core/finality-grandpa" }
grandpa-primitives = { package = "substrate-finality-grandpa-primitives", path = "../core/finality-grandpa/primitives" }
aura = { package = "substrate-consensus-aura", path = "../core/consensus/aura" }
aura-primitives = { package = "substrate-consensus-aura-primitives", path = "../core/consensus/aura/primitives" }
substrate-client = { path = "../core/client" }
basic-authorship = { package = "substrate-basic-authorship", path = "../core/basic-authorship" }
node-template-runtime = { path = "runtime" }
Expand Down
14 changes: 10 additions & 4 deletions node-template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Install Rust:
curl https://sh.rustup.rs -sSf | sh
```

Install required tools:
Initialize your Wasm Build environment:

```bash
./scripts/init.sh
Expand All @@ -19,17 +19,23 @@ Install required tools:
Build Wasm and native code:

```bash
cargo build
cargo build --release
```

## Run

### Single node development chain

You can start a development chain with:
Purge any existing developer chain state:

```bash
cargo run -- --dev
./target/release/node-template purge-chain --dev
```

Start a development chain with:

```bash
./target/release/node-template --dev
```

Detailed logs may be shown by running the node with the following environment variables set: `RUST_LOG=debug RUST_BACKTRACE=1 cargo run -- --dev`.
Expand Down
10 changes: 4 additions & 6 deletions node-template/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ support = { package = "srml-support", path = "../../srml/support", default_featu
primitives = { package = "substrate-primitives", path = "../../core/primitives", default_features = false }
substrate-session = { path = "../../core/session", default-features = false }
balances = { package = "srml-balances", path = "../../srml/balances", default_features = false }
babe = { package = "srml-babe", path = "../../srml/babe", default-features = false }
babe-primitives = { package = "substrate-consensus-babe-primitives", path = "../../core/consensus/babe/primitives", default-features = false }
aura = { package = "srml-aura", path = "../../srml/aura", default_features = false }
consensus-aura = { package = "substrate-consensus-aura-primitives", path = "../../core/consensus/aura/primitives", default_features = false }
executive = { package = "srml-executive", path = "../../srml/executive", default_features = false }
indices = { package = "srml-indices", path = "../../srml/indices", default_features = false }
grandpa = { package = "srml-grandpa", path = "../../srml/grandpa", default-features = false }
randomness-collective-flip = { package = "srml-randomness-collective-flip", path = "../../srml/randomness-collective-flip", default_features = false }
system = { package = "srml-system", path = "../../srml/system", default_features = false }
timestamp = { package = "srml-timestamp", path = "../../srml/timestamp", default_features = false }
Expand All @@ -40,11 +39,10 @@ std = [
"runtime-io/std",
"support/std",
"balances/std",
"babe/std",
"babe-primitives/std",
"aura/std",
"consensus-aura/std",
"executive/std",
"indices/std",
"grandpa/std",
"primitives/std",
"sr-primitives/std",
"randomness-collective-flip/std",
Expand Down
73 changes: 14 additions & 59 deletions node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ use sr_primitives::{
};
use sr_primitives::traits::{NumberFor, BlakeTwo256, Block as BlockT, StaticLookup, Verify, ConvertInto};
use sr_primitives::weights::Weight;
use babe::{AuthorityId as BabeId};
use grandpa::{AuthorityId as GrandpaId, AuthorityWeight as GrandpaWeight};
use grandpa::fg_primitives;
use client::{
block_builder::api::{CheckInherentsResult, InherentData, self as block_builder_api},
runtime_api as client_api, impl_runtime_apis
Expand Down Expand Up @@ -61,6 +58,9 @@ pub type Hash = primitives::H256;
/// Digest item type.
pub type DigestItem = generic::DigestItem<Hash>;

/// The SR25519 pub key of an session that belongs to an Aura authority of the chain.
pub type AuraId = consensus_aura::sr25519::AuthorityId;

/// Used for the module template in `./template.rs`
mod template;

Expand All @@ -80,14 +80,10 @@ pub mod opaque {
/// Opaque block identifier type.
pub type BlockId = generic::BlockId<Block>;

pub type SessionHandlers = (Grandpa, Babe);

impl_opaque_keys! {
pub struct SessionKeys {
#[id(key_types::GRANDPA)]
pub grandpa: GrandpaId,
#[id(key_types::BABE)]
pub babe: BabeId,
#[id(key_types::AURA)]
pub aura: AuraId,
}
}
}
Expand All @@ -102,20 +98,6 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
apis: RUNTIME_API_VERSIONS,
};

/// Constants for Babe.

/// Since BABE is probabilistic this is the average expected block time that
/// we are targetting. Blocks will be produced at a minimum duration defined
/// by `SLOT_DURATION`, but some slots will not be allocated to any
/// authority and hence no block will be produced. We expect to have this
/// block time on average following the defined slot duration and the value
/// of `c` configured for BABE (where `1 - c` represents the probability of
/// a slot being empty).
/// This value is only used indirectly to define the unit constants below
/// that are expressed in blocks. The rest of the code should use
/// `SLOT_DURATION` instead (like the timestamp module for calculating the
/// minimum period).
/// <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results>
pub const MILLISECS_PER_BLOCK: u64 = 6000;

pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;
Expand Down Expand Up @@ -181,19 +163,8 @@ impl system::Trait for Runtime {
type Version = Version;
}

parameter_types! {
pub const EpochDuration: u64 = EPOCH_DURATION_IN_BLOCKS as u64;
pub const ExpectedBlockTime: u64 = MILLISECS_PER_BLOCK;
}

impl babe::Trait for Runtime {
type EpochDuration = EpochDuration;
type ExpectedBlockTime = ExpectedBlockTime;
type EpochChangeTrigger = babe::SameAuthoritiesForever;
}

impl grandpa::Trait for Runtime {
type Event = Event;
impl aura::Trait for Runtime {
type AuthorityId = AuraId;
}

impl indices::Trait for Runtime {
Expand All @@ -215,7 +186,7 @@ parameter_types! {
impl timestamp::Trait for Runtime {
/// A timestamp: milliseconds since the unix epoch.
type Moment = u64;
type OnTimestampSet = Babe;
type OnTimestampSet = Aura;
type MinimumPeriod = MinimumPeriod;
}

Expand Down Expand Up @@ -265,8 +236,7 @@ construct_runtime!(
{
System: system::{Module, Call, Storage, Config, Event},
Timestamp: timestamp::{Module, Call, Storage, Inherent},
Babe: babe::{Module, Call, Storage, Config, Inherent(Timestamp)},
Grandpa: grandpa::{Module, Call, Storage, Config, Event},
Aura: aura::{Module, Config<T>, Inherent(Timestamp)},
Indices: indices::{default, Config<T>},
Balances: balances::{default, Error},
Sudo: sudo,
Expand Down Expand Up @@ -357,27 +327,12 @@ impl_runtime_apis! {
}
}

impl fg_primitives::GrandpaApi<Block> for Runtime {
fn grandpa_authorities() -> Vec<(GrandpaId, GrandpaWeight)> {
Grandpa::grandpa_authorities()
impl consensus_aura::AuraApi<Block, AuraId> for Runtime {
fn slot_duration() -> u64 {
Aura::slot_duration()
}
}

impl babe_primitives::BabeApi<Block> for Runtime {
fn configuration() -> babe_primitives::BabeConfiguration {
// The choice of `c` parameter (where `1 - c` represents the
// probability of a slot being empty), is done in accordance to the
// slot duration and expected target block time, for safely
// resisting network delays of maximum two seconds.
// <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results>
babe_primitives::BabeConfiguration {
slot_duration: Babe::slot_duration(),
epoch_length: EpochDuration::get(),
c: PRIMARY_PROBABILITY,
genesis_authorities: Babe::authorities(),
randomness: Babe::randomness(),
secondary_slots: true,
}
fn authorities() -> Vec<AuraId> {
Aura::authorities()
}
}

Expand Down
35 changes: 14 additions & 21 deletions node-template/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use primitives::{Pair, Public};
use node_template_runtime::{
AccountId, BabeConfig, BalancesConfig, GenesisConfig, GrandpaConfig,
AuraId, AccountId, AuraConfig, BalancesConfig, GenesisConfig,
SudoConfig, IndicesConfig, SystemConfig, WASM_BINARY,
};
use babe_primitives::{AuthorityId as BabeId};
use grandpa_primitives::{AuthorityId as GrandpaId};
use aura_primitives::sr25519::AuthorityPair as AuraPair;
use substrate_service;

// Note this is the URL for the telemetry server
Expand All @@ -31,14 +30,11 @@ pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Pu
.public()
}

/// Helper function to generate stash, controller and session key from seed
pub fn get_authority_keys_from_seed(seed: &str) -> (AccountId, AccountId, GrandpaId, BabeId) {
(
get_from_seed::<AccountId>(&format!("{}//stash", seed)),
get_from_seed::<AccountId>(seed),
get_from_seed::<GrandpaId>(seed),
get_from_seed::<BabeId>(seed),
)
/// Helper function to generate an authority key for Aura
pub fn get_authority_key_from_seed(s: &str) -> AuraId {
AuraPair::from_string(&format!("//{}", s), None)
.expect("static values are valid; qed")
.public()
}

impl Alternative {
Expand All @@ -49,7 +45,7 @@ impl Alternative {
"Development",
"dev",
|| testnet_genesis(vec![
get_authority_keys_from_seed("Alice"),
get_authority_key_from_seed("Alice"),
],
get_from_seed::<AccountId>("Alice"),
vec![
Expand All @@ -69,8 +65,8 @@ impl Alternative {
"Local Testnet",
"local_testnet",
|| testnet_genesis(vec![
get_authority_keys_from_seed("Alice"),
get_authority_keys_from_seed("Bob"),
get_authority_key_from_seed("Alice"),
get_authority_key_from_seed("Bob"),
],
get_from_seed::<AccountId>("Alice"),
vec![
Expand Down Expand Up @@ -106,7 +102,7 @@ impl Alternative {
}
}

fn testnet_genesis(initial_authorities: Vec<(AccountId, AccountId, GrandpaId, BabeId)>,
fn testnet_genesis(initial_authorities: Vec<AuraId>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool) -> GenesisConfig {
Expand All @@ -125,11 +121,8 @@ fn testnet_genesis(initial_authorities: Vec<(AccountId, AccountId, GrandpaId, Ba
sudo: Some(SudoConfig {
key: root_key,
}),
babe: Some(BabeConfig {
authorities: initial_authorities.iter().map(|x| (x.3.clone(), 1)).collect(),
}),
grandpa: Some(GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.2.clone(), 1)).collect(),
}),
aura: Some(AuraConfig {
authorities: initial_authorities.clone(),
})
}
}
Loading