Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request paritytech#262 from subspace/extract-subspace-serv…
Browse files Browse the repository at this point in the history
…ice-tweaks

Extract subspace service (tweaks)
  • Loading branch information
liuchengxu authored Feb 18, 2022
2 parents 7b6d0a4 + 02a3da3 commit 551d923
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 250 deletions.
11 changes: 7 additions & 4 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions crates/cirrus-node-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ pub type CollatorSignature = collator_app::Signature;

/// Configuration for the collation generator
pub struct CollationGenerationConfig {
/// Collator's authentication key, so it can sign things.
pub key: CollatorPair,
/// Transaction bundle function. See [`BundlerFn`] for more details.
pub bundler: BundlerFn,
/// State processor function. See [`ProcessorFn`] for more details.
Expand Down
13 changes: 11 additions & 2 deletions crates/subspace-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,28 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
clap = { version = "3.0.13", features = ["derive"] }
frame-benchmarking-cli = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
frame-support = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
futures = "0.3.19"
sc-cli = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927", features = ["wasmtime"] }
sc-executor = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927", features = ["wasmtime"] }
sc-service = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927", features = ["wasmtime"] }
sc-telemetry = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
sp-core = { version = "5.0.0", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
sp-runtime = { version = "5.0.0", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
subspace-runtime = { version = "0.1.0", path = "../subspace-runtime" }
subspace-runtime-primitives = { version = "0.1.0", path = "../subspace-runtime-primitives" }
subspace-service = { version = "0.1.0", path = "../subspace-service" }
thiserror = "1.0.30"

[build-dependencies]
substrate-build-script-utils = { version = "3.0.0", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }

[features]
default = []
default = ["do-not-enforce-cost-of-storage"]
do-not-enforce-cost-of-storage = [
"subspace-runtime/do-not-enforce-cost-of-storage"
]
runtime-benchmarks = [
"subspace-service/runtime-benchmarks",
"subspace-runtime/runtime-benchmarks",
]
json-chain-spec = ["subspace-service/json-chain-spec"]
75 changes: 59 additions & 16 deletions crates/subspace-node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::chain_spec;
use crate::cli::{Cli, Subcommand};
use futures::future::TryFutureExt;
use sc_cli::{ChainSpec, RuntimeVersion, SubstrateCli};
use sc_executor::NativeExecutionDispatch;
use sp_core::crypto::Ss58AddressFormat;
use subspace_service::{chain_spec, subspace_runtime, SubspaceExecutorDispatch};
use subspace_runtime::RuntimeApi;

/// Subspace node error.
#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -46,6 +48,25 @@ impl From<String> for Error {
}
}

struct ExecutorDispatch;

impl NativeExecutionDispatch for ExecutorDispatch {
/// Only enable the benchmarking host functions when we actually want to benchmark.
#[cfg(feature = "runtime-benchmarks")]
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;
/// Otherwise we only use the default Substrate host functions.
#[cfg(not(feature = "runtime-benchmarks"))]
type ExtendHostFunctions = ();

fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
subspace_runtime::api::dispatch(method, data)
}

fn native_version() -> sc_executor::NativeVersion {
subspace_runtime::native_version()
}
}

impl SubstrateCli for Cli {
fn impl_name() -> String {
"Subspace".into()
Expand Down Expand Up @@ -120,9 +141,13 @@ pub fn run() -> std::result::Result<(), Error> {
Some(Subcommand::CheckBlock(cmd)) => {
let runner = cli.create_runner(cmd)?;
set_default_ss58_version(&runner.config().chain_spec);
runner.async_run(|mut config| {
let (client, _, import_queue, task_manager) =
subspace_service::new_chain_ops(&mut config)?;
runner.async_run(|config| {
let sc_service::PartialComponents {
client,
import_queue,
task_manager,
..
} = subspace_service::new_partial::<RuntimeApi, ExecutorDispatch>(&config)?;
Ok((
cmd.run(client, import_queue).map_err(Error::SubstrateCli),
task_manager,
Expand All @@ -132,8 +157,12 @@ pub fn run() -> std::result::Result<(), Error> {
Some(Subcommand::ExportBlocks(cmd)) => {
let runner = cli.create_runner(cmd)?;
set_default_ss58_version(&runner.config().chain_spec);
runner.async_run(|mut config| {
let (client, _, _, task_manager) = subspace_service::new_chain_ops(&mut config)?;
runner.async_run(|config| {
let sc_service::PartialComponents {
client,
task_manager,
..
} = subspace_service::new_partial::<RuntimeApi, ExecutorDispatch>(&config)?;
Ok((
cmd.run(client, config.database)
.map_err(Error::SubstrateCli),
Expand All @@ -144,8 +173,12 @@ pub fn run() -> std::result::Result<(), Error> {
Some(Subcommand::ExportState(cmd)) => {
let runner = cli.create_runner(cmd)?;
set_default_ss58_version(&runner.config().chain_spec);
runner.async_run(|mut config| {
let (client, _, _, task_manager) = subspace_service::new_chain_ops(&mut config)?;
runner.async_run(|config| {
let sc_service::PartialComponents {
client,
task_manager,
..
} = subspace_service::new_partial::<RuntimeApi, ExecutorDispatch>(&config)?;
Ok((
cmd.run(client, config.chain_spec)
.map_err(Error::SubstrateCli),
Expand All @@ -156,9 +189,13 @@ pub fn run() -> std::result::Result<(), Error> {
Some(Subcommand::ImportBlocks(cmd)) => {
let runner = cli.create_runner(cmd)?;
set_default_ss58_version(&runner.config().chain_spec);
runner.async_run(|mut config| {
let (client, _, import_queue, task_manager) =
subspace_service::new_chain_ops(&mut config)?;
runner.async_run(|config| {
let sc_service::PartialComponents {
client,
import_queue,
task_manager,
..
} = subspace_service::new_partial::<RuntimeApi, ExecutorDispatch>(&config)?;
Ok((
cmd.run(client, import_queue).map_err(Error::SubstrateCli),
task_manager,
Expand All @@ -172,9 +209,13 @@ pub fn run() -> std::result::Result<(), Error> {
Some(Subcommand::Revert(cmd)) => {
let runner = cli.create_runner(cmd)?;
set_default_ss58_version(&runner.config().chain_spec);
runner.async_run(|mut config| {
let (client, backend, _, task_manager) =
subspace_service::new_chain_ops(&mut config)?;
runner.async_run(|config| {
let sc_service::PartialComponents {
client,
backend,
task_manager,
..
} = subspace_service::new_partial::<RuntimeApi, ExecutorDispatch>(&config)?;
Ok((
cmd.run(client, backend).map_err(Error::SubstrateCli),
task_manager,
Expand All @@ -186,7 +227,7 @@ pub fn run() -> std::result::Result<(), Error> {
let runner = cli.create_runner(cmd)?;
set_default_ss58_version(&runner.config().chain_spec);
runner.sync_run(|config| {
cmd.run::<subspace_runtime::Block, SubspaceExecutorDispatch>(config)
cmd.run::<subspace_runtime::Block, ExecutorDispatch>(config)
})?;
} else {
return Err(Error::Other(
Expand All @@ -200,7 +241,9 @@ pub fn run() -> std::result::Result<(), Error> {
let runner = cli.create_runner(&cli.run.base)?;
set_default_ss58_version(&runner.config().chain_spec);
runner.run_node_until_exit(|config| async move {
subspace_service::new_full::<subspace_runtime::RuntimeApi, SubspaceExecutorDispatch>(config, true)
subspace_service::new_full::<subspace_runtime::RuntimeApi, ExecutorDispatch>(
config, true,
)
.await
.map(|full| full.task_manager)
})?;
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#![warn(missing_docs)]

mod chain_spec;
pub mod cli;
mod command;

Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use subspace_core_primitives::objects::{BlockObject, BlockObjectMapping};
use subspace_core_primitives::{Randomness, RootBlock, Sha256Hash, PIECE_SIZE};
pub use subspace_runtime_primitives::{
use subspace_runtime_primitives::{
opaque, AccountId, Balance, BlockNumber, Hash, Index, Moment, Signature, CONFIRMATION_DEPTH_K,
MIN_REPLICATION_FACTOR, RECORDED_HISTORY_SEGMENT_SIZE, RECORD_SIZE,
STORAGE_FEES_ESCROW_BLOCK_REWARD, STORAGE_FEES_ESCROW_BLOCK_TAX,
Expand Down
4 changes: 0 additions & 4 deletions crates/subspace-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ sp-runtime = { version = "5.0.0", git = "https://github.com/paritytech/substrate
sp-timestamp = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
sp-transaction-pool = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
sp-trie = { version = "5.0.0", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
subspace-runtime = { version = "0.1.0", features = ["do-not-enforce-cost-of-storage"], path = "../subspace-runtime" }
subspace-runtime-primitives = { version = "0.1.0", path = "../subspace-runtime-primitives" }
substrate-frame-rpc-system = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
substrate-prometheus-endpoint = { git = "https://github.com/paritytech/substrate", rev = "e6def65920d30029e42d498cb07cec5dd433b927" }
Expand All @@ -67,9 +66,6 @@ pallet-transaction-payment-rpc-runtime-api = { version = "4.0.0-dev", git = "htt

[features]
default = []
runtime-benchmarks = [
"subspace-runtime/runtime-benchmarks",
]
# This feature makes `testnet` chain spec to use `chain-spec.json` file in the root of the repo instead of compiled
# version
json-chain-spec = []
51 changes: 0 additions & 51 deletions crates/subspace-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.
pub mod chain_spec;
pub mod rpc;

use lru::LruCache;
Expand All @@ -41,7 +40,6 @@ use sp_blockchain::HeaderBackend;
use sp_consensus::SelectChain;
use sp_runtime::traits::{BlakeTwo256, Block as BlockT, Header as HeaderT};
use std::sync::Arc;
pub use subspace_runtime;
use subspace_runtime_primitives::{
opaque::{Block, BlockId},
AccountId, Balance, Index as Nonce,
Expand Down Expand Up @@ -116,33 +114,10 @@ pub enum Error {
Prometheus(#[from] substrate_prometheus_endpoint::PrometheusError),
}

/// Subspace native executor instance.
pub struct SubspaceExecutorDispatch;

impl sc_executor::NativeExecutionDispatch for SubspaceExecutorDispatch {
/// Only enable the benchmarking host functions when we actually want to benchmark.
#[cfg(feature = "runtime-benchmarks")]
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;
/// Otherwise we only use the default Substrate host functions.
#[cfg(not(feature = "runtime-benchmarks"))]
type ExtendHostFunctions = ();

fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
subspace_runtime::api::dispatch(method, data)
}

fn native_version() -> sc_executor::NativeVersion {
subspace_runtime::native_version()
}
}

/// Subspace-like full client.
pub type FullClient<RuntimeApi, ExecutorDispatch> =
sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;

/// Full client using subspace-runtime.
pub type SubspaceClient = FullClient<subspace_runtime::RuntimeApi, SubspaceExecutorDispatch>;

type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;

Expand Down Expand Up @@ -605,29 +580,3 @@ where
block_signing_notification_stream,
})
}

/// Builds a new object suitable for chain operations.
#[allow(clippy::type_complexity)]
pub fn new_chain_ops(
mut config: &mut Configuration,
) -> Result<
(
Arc<FullClient<subspace_runtime::RuntimeApi, SubspaceExecutorDispatch>>,
Arc<FullBackend>,
sc_consensus::BasicQueue<Block, sp_trie::PrefixedMemoryDB<BlakeTwo256>>,
TaskManager,
),
Error,
> {
config.keystore = sc_service::config::KeystoreConfig::InMemory;

let sc_service::PartialComponents {
client,
backend,
import_queue,
task_manager,
..
} = new_partial::<subspace_runtime::RuntimeApi, SubspaceExecutorDispatch>(config)?;

Ok((client, backend, import_queue, task_manager))
}
3 changes: 2 additions & 1 deletion crates/subspace-service/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use sp_api::ProvideRuntimeApi;
use sp_block_builder::BlockBuilder;
use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
use std::sync::Arc;
use subspace_runtime::{opaque::Block, AccountId, Balance, Index};
use subspace_runtime_primitives::opaque::Block;
use subspace_runtime_primitives::{AccountId, Balance, Index};

/// Full client dependencies.
pub struct FullDeps<C, P> {
Expand Down
5 changes: 1 addition & 4 deletions cumulus/client/cirrus-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use polkadot_overseer::Handle as OverseerHandle;

use cirrus_client_executor_gossip::{Action, GossipMessageHandler};
use cirrus_node_primitives::{
BundleResult, CollationGenerationConfig, CollatorPair, ExecutorSlotInfo, ProcessorResult,
BundleResult, CollationGenerationConfig, ExecutorSlotInfo, ProcessorResult,
};
use cirrus_primitives::{AccountId, Hash, SecondaryApi};
use sp_executor::{
Expand Down Expand Up @@ -528,7 +528,6 @@ pub struct StartExecutorParams<Block: BlockT, Spawner, Client, TransactionPool,
pub announce_block: Arc<dyn Fn(Block::Hash, Option<Vec<u8>>) + Send + Sync>,
pub overseer_handle: OverseerHandle,
pub spawner: Spawner,
pub key: CollatorPair,
pub parachain_consensus: Box<dyn ParachainConsensus<Block>>,
pub transaction_pool: Arc<TransactionPool>,
pub bundle_sender: TracingUnboundedSender<Bundle<Block::Extrinsic>>,
Expand All @@ -545,7 +544,6 @@ pub async fn start_executor<Block, Spawner, Client, TransactionPool, Backend, CI
announce_block: _,
mut overseer_handle,
spawner,
key,
parachain_consensus,
transaction_pool,
bundle_sender,
Expand Down Expand Up @@ -596,7 +594,6 @@ where

let span = tracing::Span::current();
let config = CollationGenerationConfig {
key,
bundler: {
let executor = executor.clone();
let span = span.clone();
Expand Down
Loading

0 comments on commit 551d923

Please sign in to comment.