From ff9c6f950e9d04fba4cc7b292190e00421336d31 Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Mon, 22 Nov 2021 19:08:29 +0800 Subject: [PATCH] Add some modifications for cirrus development (#143) * Separate out subspace-primitives from subspace-runtime * Introduce `NewFull` struct in subspace-node Also expose the `service` module in public. * Make subspace-node a library --- .github/workflows/rust.yaml | 47 ++++++-------- .github/workflows/rustdoc.yml | 4 +- Cargo.lock | 10 +++ crates/subspace-node/Cargo.toml | 5 ++ crates/subspace-node/{src => bin}/main.rs | 9 +-- crates/subspace-node/src/cli.rs | 34 +++++++--- crates/subspace-node/src/command.rs | 6 +- crates/subspace-node/src/lib.rs | 28 +++++++++ crates/subspace-node/src/service.rs | 39 +++++++++--- crates/subspace-runtime-primitives/Cargo.toml | 27 ++++++++ crates/subspace-runtime-primitives/src/lib.rs | 62 +++++++++++++++++++ crates/subspace-runtime/Cargo.toml | 2 + crates/subspace-runtime/src/lib.rs | 55 +++------------- rust-toolchain.toml | 2 + 14 files changed, 224 insertions(+), 106 deletions(-) rename crates/subspace-node/{src => bin}/main.rs (89%) create mode 100644 crates/subspace-node/src/lib.rs create mode 100644 crates/subspace-runtime-primitives/Cargo.toml create mode 100644 crates/subspace-runtime-primitives/src/lib.rs diff --git a/.github/workflows/rust.yaml b/.github/workflows/rust.yaml index b482436a54fa8..68df504050cd3 100644 --- a/.github/workflows/rust.yaml +++ b/.github/workflows/rust.yaml @@ -10,10 +10,6 @@ env: jobs: cargo-fmt: runs-on: ubuntu-20.04 - strategy: - matrix: - rust: - - nightly steps: - name: Checkout @@ -21,8 +17,9 @@ jobs: - name: Rust toolchain uses: actions-rs/toolchain@v1 + # TODO: Below can be removed when https://github.com/actions-rs/toolchain/issues/126 is resolved with: - toolchain: ${{ matrix.rust }} + toolchain: nightly target: wasm32-unknown-unknown override: true components: rustfmt, clippy @@ -33,9 +30,7 @@ jobs: path: | ~/.cargo/registry ~/.cargo/git - key: ${{ runner.os }}-cargo-${{ matrix.rust }}-${{ hashFiles('**/Cargo.toml') }} - restore-keys: | - ${{ runner.os }}-cargo- + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }} - name: cargo fmt uses: actions-rs/cargo@v1 @@ -50,8 +45,6 @@ jobs: - ubuntu-20.04 - macos-11 - windows-2019 - rust: - - nightly runs-on: ${{ matrix.os }} @@ -61,8 +54,9 @@ jobs: - name: Rust toolchain uses: actions-rs/toolchain@v1 + # TODO: Below can be removed when https://github.com/actions-rs/toolchain/issues/126 is resolved with: - toolchain: ${{ matrix.rust }} + toolchain: nightly target: wasm32-unknown-unknown override: true components: rustfmt, clippy @@ -85,21 +79,19 @@ jobs: path: | ~/.cargo/registry ~/.cargo/git - key: ${{ matrix.os }}-cargo-${{ matrix.rust }}-${{ hashFiles('**/Cargo.toml') }} - restore-keys: | - ${{ matrix.os }}-cargo- + key: ${{ matrix.os }}-cargo-${{ hashFiles('**/Cargo.toml') }} - name: cargo clippy (Linux or Windows with CUDA) - uses: actions-rs/cargo@v1 + uses: actions-rs/clippy-check@v1 with: - command: clippy + token: ${{ secrets.GITHUB_TOKEN }} args: --features=subspace-farmer/cuda -- -D warnings if: runner.os != 'macOS' - name: cargo clippy (macOS) - uses: actions-rs/cargo@v1 + uses: actions-rs/clippy-check@v1 with: - command: clippy + token: ${{ secrets.GITHUB_TOKEN }} args: -- -D warnings if: runner.os == 'macOS' @@ -110,8 +102,6 @@ jobs: - ubuntu-20.04 - macos-11 - windows-2019 - rust: - - nightly runs-on: ${{ matrix.os }} @@ -121,8 +111,9 @@ jobs: - name: Rust toolchain uses: actions-rs/toolchain@v1 + # TODO: Below can be removed when https://github.com/actions-rs/toolchain/issues/126 is resolved with: - toolchain: ${{ matrix.rust }} + toolchain: nightly target: wasm32-unknown-unknown override: true components: rustfmt, clippy @@ -137,7 +128,7 @@ jobs: id: cuda-toolkit with: cuda: '11.2.2' - if: runner.os != 'macOS' + if: runner.os == 'Linux' - name: Configure cache uses: actions/cache@v2 @@ -145,19 +136,17 @@ jobs: path: | ~/.cargo/registry ~/.cargo/git - key: ${{ matrix.os }}-cargo-${{ matrix.rust }}-${{ hashFiles('**/Cargo.toml') }} - restore-keys: | - ${{ matrix.os }}-cargo- + key: ${{ matrix.os }}-cargo-${{ hashFiles('**/Cargo.toml') }} - - name: cargo test (Linux or Windows with CUDA) + - name: cargo test (Linux with CUDA) uses: actions-rs/cargo@v1 with: command: test args: --features=subspace-farmer/cuda - if: runner.os != 'macOS' + if: runner.os == 'Linux' - - name: cargo test (macOS) + - name: cargo test (macOS or Windows) uses: actions-rs/cargo@v1 with: command: test - if: runner.os == 'macOS' + if: runner.os == 'macOS' || runner.os == 'Windows' diff --git a/.github/workflows/rustdoc.yml b/.github/workflows/rustdoc.yml index b6eadf762c68a..5107e3854602c 100644 --- a/.github/workflows/rustdoc.yml +++ b/.github/workflows/rustdoc.yml @@ -33,8 +33,8 @@ jobs: - name: Build Documentation run: cargo doc --all --no-deps --lib env: - RUSTC_BOOTSTRAP: 1 - RUSTDOCFLAGS: "-Z unstable-options --enable-index-page" + RUSTC_BOOTSTRAP: 1 + RUSTDOCFLAGS: "-Z unstable-options --enable-index-page" - name: Deploy Docs uses: JamesIves/github-pages-deploy-action@releases/v3 diff --git a/Cargo.lock b/Cargo.lock index 36aa27034c9fc..05994631c4b16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7339,6 +7339,7 @@ dependencies = [ "sc-consensus-subspace-rpc", "sc-consensus-uncles", "sc-executor", + "sc-network", "sc-rpc", "sc-rpc-api", "sc-service", @@ -7408,9 +7409,18 @@ dependencies = [ "sp-transaction-pool", "sp-version", "subspace-core-primitives", + "subspace-runtime-primitives", "substrate-wasm-builder", ] +[[package]] +name = "subspace-runtime-primitives" +version = "0.1.0" +dependencies = [ + "sp-core", + "sp-runtime", +] + [[package]] name = "subspace-solving" version = "0.1.0" diff --git a/crates/subspace-node/Cargo.toml b/crates/subspace-node/Cargo.toml index 6297bfc2418d8..d5ae678286e49 100644 --- a/crates/subspace-node/Cargo.toml +++ b/crates/subspace-node/Cargo.toml @@ -15,6 +15,10 @@ include = [ "/README.md" ] +[[bin]] +name = "subspace-node" +path = "bin/main.rs" + [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] @@ -32,6 +36,7 @@ sc-consensus-subspace-rpc = { version = "0.1.0", path = "../sc-consensus-subspac sc-consensus-slots = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" } sc-consensus-uncles = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" } sc-executor = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131", features = ["wasmtime"] } +sc-network = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" } sc-rpc = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" } sc-rpc-api = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" } sc-service = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131", features = ["wasmtime"] } diff --git a/crates/subspace-node/src/main.rs b/crates/subspace-node/bin/main.rs similarity index 89% rename from crates/subspace-node/src/main.rs rename to crates/subspace-node/bin/main.rs index 3042cfac3abd1..b0f43f94546a1 100644 --- a/crates/subspace-node/src/main.rs +++ b/crates/subspace-node/bin/main.rs @@ -18,13 +18,6 @@ #![warn(missing_docs)] -mod chain_spec; -#[macro_use] -mod service; -mod cli; -mod command; -mod rpc; - fn main() -> sc_cli::Result<()> { - command::run() + subspace_node::run() } diff --git a/crates/subspace-node/src/cli.rs b/crates/subspace-node/src/cli.rs index 913ee6fc2b8ce..be869b7185049 100644 --- a/crates/subspace-node/src/cli.rs +++ b/crates/subspace-node/src/cli.rs @@ -14,22 +14,16 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use sc_cli::RunCmd; -use structopt::StructOpt; - -#[derive(Debug, StructOpt)] -pub struct Cli { - #[structopt(subcommand)] - pub subcommand: Option, +//! Subspace CLI library. - #[structopt(flatten)] - pub run: RunCmd, -} +use structopt::StructOpt; +/// Utilities for working with a node. #[derive(Debug, StructOpt)] pub enum Subcommand { /// Key management cli utilities Key(sc_cli::KeySubcommand), + /// Build a chain specification. BuildSpec(sc_cli::BuildSpecCmd), @@ -55,3 +49,23 @@ pub enum Subcommand { #[structopt(name = "benchmark", about = "Benchmark runtime pallets.")] Benchmark(frame_benchmarking_cli::BenchmarkCmd), } + +/// Command used to run a Subspace node. +#[derive(Debug, StructOpt)] +pub struct RunCmd { + /// Base command to run a node. + #[structopt(flatten)] + pub base: sc_cli::RunCmd, +} + +/// Subspace Cli. +#[derive(Debug, StructOpt)] +pub struct Cli { + /// Various utility commands. + #[structopt(subcommand)] + pub subcommand: Option, + + /// Run a node. + #[structopt(flatten)] + pub run: RunCmd, +} diff --git a/crates/subspace-node/src/command.rs b/crates/subspace-node/src/command.rs index 09501ada622d0..b496ebf940f90 100644 --- a/crates/subspace-node/src/command.rs +++ b/crates/subspace-node/src/command.rs @@ -147,9 +147,11 @@ pub fn run() -> sc_cli::Result<()> { } } None => { - let runner = cli.create_runner(&cli.run)?; + let runner = cli.create_runner(&cli.run.base)?; runner.run_node_until_exit(|config| async move { - service::new_full(config).map_err(sc_cli::Error::Service) + service::new_full(config) + .map(|full| full.task_manager) + .map_err(sc_cli::Error::Service) }) } } diff --git a/crates/subspace-node/src/lib.rs b/crates/subspace-node/src/lib.rs new file mode 100644 index 0000000000000..8e74affb792fa --- /dev/null +++ b/crates/subspace-node/src/lib.rs @@ -0,0 +1,28 @@ +// Copyright (C) 2021 Subspace Labs, Inc. +// SPDX-License-Identifier: GPL-3.0-or-later + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +//! Subspace Node library. + +#![warn(missing_docs)] + +mod chain_spec; +#[macro_use] +pub mod service; +pub mod cli; +mod command; +mod rpc; + +pub use self::command::run; diff --git a/crates/subspace-node/src/service.rs b/crates/subspace-node/src/service.rs index c2d47dc5541b3..6e2723fa481da 100644 --- a/crates/subspace-node/src/service.rs +++ b/crates/subspace-node/src/service.rs @@ -26,7 +26,7 @@ use std::sync::Arc; use subspace_runtime::opaque::BlockId; use subspace_runtime::{self, opaque::Block, RuntimeApi}; -// Our native executor instance. +/// Subspace native executor instance. pub struct ExecutorDispatch; impl sc_executor::NativeExecutionDispatch for ExecutorDispatch { @@ -46,11 +46,13 @@ impl sc_executor::NativeExecutionDispatch for ExecutorDispatch { } } -type FullClient = +/// Subspace full client. +pub type FullClient = sc_service::TFullClient>; type FullBackend = sc_service::TFullBackend; type FullSelectChain = sc_consensus::LongestChain; +/// Creates `PartialComponents` for Subspace client. #[allow(clippy::type_complexity)] pub fn new_partial( config: &Configuration, @@ -161,8 +163,22 @@ pub fn new_partial( }) } +/// Full client along with some other components. +pub struct NewFull { + /// Task manager. + pub task_manager: TaskManager, + /// Full client. + pub client: C, + /// Network. + pub network: Arc::Hash>>, + /// RPC handlers. + pub rpc_handlers: sc_service::RpcHandlers, + /// Full client backend. + pub backend: Arc, +} + /// Builds a new service for a full client. -pub fn new_full(config: Configuration) -> Result { +pub fn new_full(config: Configuration) -> Result>, ServiceError> { let sc_service::PartialComponents { client, backend, @@ -291,21 +307,28 @@ pub fn new_full(config: Configuration) -> Result { }) }; - let _rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams { - network, - client, + let rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams { + network: network.clone(), + client: client.clone(), keystore: keystore_container.sync_keystore(), task_manager: &mut task_manager, transaction_pool, rpc_extensions_builder, on_demand: None, remote_blockchain: None, - backend, + backend: backend.clone(), system_rpc_tx, config, telemetry: telemetry.as_mut(), })?; network_starter.start_network(); - Ok(task_manager) + + Ok(NewFull { + task_manager, + client, + network, + rpc_handlers, + backend, + }) } diff --git a/crates/subspace-runtime-primitives/Cargo.toml b/crates/subspace-runtime-primitives/Cargo.toml new file mode 100644 index 0000000000000..7d7305fca11d1 --- /dev/null +++ b/crates/subspace-runtime-primitives/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "subspace-runtime-primitives" +description = "Runtime primitives for Subspace Network" +license = "GPL-3.0-or-later" +version = "0.1.0" +authors = ["Subspace Labs "] +edition = "2021" +homepage = "https://subspace.network" +repository = "https://github.com/subspace/subspace" +include = [ + "/src", + "/Cargo.toml", +] + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +sp-core = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" } +sp-runtime = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" } + +[features] +default = ["std"] +std = [ + "sp-core/std", + "sp-runtime/std", +] diff --git a/crates/subspace-runtime-primitives/src/lib.rs b/crates/subspace-runtime-primitives/src/lib.rs new file mode 100644 index 0000000000000..00ec7255b459f --- /dev/null +++ b/crates/subspace-runtime-primitives/src/lib.rs @@ -0,0 +1,62 @@ +// Copyright (C) 2021 Subspace Labs, Inc. +// SPDX-License-Identifier: GPL-3.0-or-later + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +//! Runtime primitives for Subspace Network. + +#![cfg_attr(not(feature = "std"), no_std)] + +use sp_runtime::traits::{IdentifyAccount, Verify}; +use sp_runtime::MultiSignature; + +/// An index to a block. +pub type BlockNumber = u32; + +/// Alias to 512-bit hash when used in the context of a transaction signature on the chain. +pub type Signature = MultiSignature; + +/// Some way of identifying an account on the chain. We intentionally make it equivalent +/// to the public key of our transaction signing scheme. +pub type AccountId = <::Signer as IdentifyAccount>::AccountId; + +/// Balance of an account. +pub type Balance = u128; + +/// Index of a transaction in the chain. +pub type Index = u32; + +/// A hash of some data used by the chain. +pub type Hash = sp_core::H256; + +/// Type used for expressing timestamp. +pub type Moment = u64; + +/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know +/// the specifics of the runtime. They can then be made to be agnostic over specific formats +/// of data like extrinsics, allowing for them to continue syncing the network through upgrades +/// to even the core data structures. +pub mod opaque { + use super::BlockNumber; + use sp_runtime::generic; + use sp_runtime::traits::BlakeTwo256; + pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic; + + /// Opaque block header type. + pub type Header = generic::Header; + /// Opaque block type. + pub type Block = generic::Block; + /// Opaque block identifier type. + pub type BlockId = generic::BlockId; +} diff --git a/crates/subspace-runtime/Cargo.toml b/crates/subspace-runtime/Cargo.toml index bffc53cbf8047..5e550d999442b 100644 --- a/crates/subspace-runtime/Cargo.toml +++ b/crates/subspace-runtime/Cargo.toml @@ -46,6 +46,7 @@ sp-std = { version = "4.0.0-dev", default-features = false, git = "https://githu sp-transaction-pool = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" } sp-version = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" } subspace-core-primitives = { version = "0.1.0", default-features = false, path = "../subspace-core-primitives" } +subspace-runtime-primitives = { version = "0.1.0", default-features = false, path = "../subspace-runtime-primitives" } # Used for the node template's RPCs frame-system-rpc-runtime-api = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" } @@ -92,6 +93,7 @@ std = [ "sp-transaction-pool/std", "sp-version/std", "subspace-core-primitives/std", + "subspace-runtime-primitives/std", ] runtime-benchmarks = [ "frame-benchmarking", diff --git a/crates/subspace-runtime/src/lib.rs b/crates/subspace-runtime/src/lib.rs index 87c83ef9126c6..673f6f3fc4af4 100644 --- a/crates/subspace-runtime/src/lib.rs +++ b/crates/subspace-runtime/src/lib.rs @@ -36,11 +36,11 @@ use frame_system::EnsureNever; use pallet_transaction_payment::CurrencyAdapter; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; -use sp_runtime::traits::{AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, Verify}; +use sp_runtime::traits::{AccountIdLookup, BlakeTwo256, Block as BlockT}; use sp_runtime::{ create_runtime_str, generic, transaction_validity::{TransactionSource, TransactionValidity}, - ApplyExtrinsicResult, MultiSignature, Perbill, + ApplyExtrinsicResult, Perbill, }; use sp_std::prelude::*; #[cfg(feature = "std")] @@ -48,50 +48,11 @@ use sp_version::NativeVersion; use sp_version::RuntimeVersion; use subspace_core_primitives::objects::{BlockObject, BlockObjectMapping}; use subspace_core_primitives::{RootBlock, Sha256Hash, PIECE_SIZE, SHA256_HASH_SIZE}; +pub use subspace_runtime_primitives::*; -/// An index to a block. -pub type BlockNumber = u32; - -/// Alias to 512-bit hash when used in the context of a transaction signature on the chain. -pub type Signature = MultiSignature; - -/// Some way of identifying an account on the chain. We intentionally make it equivalent -/// to the public key of our transaction signing scheme. -pub type AccountId = <::Signer as IdentifyAccount>::AccountId; - -/// Balance of an account. -pub type Balance = u128; - -/// Index of a transaction in the chain. -pub type Index = u32; - -/// A hash of some data used by the chain. -pub type Hash = sp_core::H256; - -/// Type used for expressing timestamp. -pub type Moment = u64; - -/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know -/// the specifics of the runtime. They can then be made to be agnostic over specific formats -/// of data like extrinsics, allowing for them to continue syncing the network through upgrades -/// to even the core data structures. -pub mod opaque { - use super::{BlockNumber, Subspace}; - use sp_runtime::traits::BlakeTwo256; - use sp_runtime::{generic, impl_opaque_keys, OpaqueExtrinsic}; - use sp_std::vec::Vec; - - /// Opaque block header type. - pub type Header = generic::Header; - /// Opaque block type. - pub type Block = generic::Block; - /// Opaque block identifier type. - pub type BlockId = generic::BlockId; - - impl_opaque_keys! { - pub struct SessionKeys { - pub subspace: Subspace, - } +sp_runtime::impl_opaque_keys! { + pub struct SessionKeys { + pub subspace: Subspace, } } @@ -767,13 +728,13 @@ impl_runtime_apis! { impl sp_session::SessionKeys for Runtime { fn generate_session_keys(seed: Option>) -> Vec { - opaque::SessionKeys::generate(seed) + SessionKeys::generate(seed) } fn decode_session_keys( encoded: Vec, ) -> Option, KeyTypeId)>> { - opaque::SessionKeys::decode_into_raw_public_keys(&encoded) + SessionKeys::decode_into_raw_public_keys(&encoded) } } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 5d56faf9ae08c..17ce332ff707e 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,2 +1,4 @@ [toolchain] channel = "nightly" +targets = ["wasm32-unknown-unknown"] +profile = "default"