Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Support custom genesis block #12291

Merged
merged 18 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
22429e0
Set genesis block data using the built genesis block
liuchengxu Sep 18, 2022
217770e
Make resolve_state_version_from_wasm a separate function and some sma…
liuchengxu Sep 18, 2022
2f2ae4a
Introduce trait BuildGenesisBlock
liuchengxu Sep 18, 2022
db96310
Make call_executor test compile
liuchengxu Sep 18, 2022
2d538e8
cargo +nightly fmt --all
liuchengxu Sep 18, 2022
66ffc2c
Merge branch 'master' of https://github.com/paritytech/substrate into…
liuchengxu Sep 20, 2022
7a2bc59
Merge branch 'master' of https://github.com/paritytech/substrate into…
liuchengxu Sep 22, 2022
96361d0
Fix test
liuchengxu Sep 25, 2022
9578867
Merge branch 'master' of https://github.com/paritytech/substrate into…
liuchengxu Sep 25, 2022
61861be
Remove unnecessary clone
liuchengxu Sep 25, 2022
8190b56
Merge branch 'master' of https://github.com/paritytech/substrate into…
liuchengxu Oct 26, 2022
3d18673
FMT
liuchengxu Oct 27, 2022
c437b55
Merge branch 'master' of https://github.com/paritytech/substrate into…
liuchengxu Nov 26, 2022
9691b25
Apply review suggestions
liuchengxu Dec 18, 2022
16b5cea
Merge branch 'master' of https://github.com/paritytech/substrate into…
liuchengxu Dec 18, 2022
27cfafd
Revert changes to new_full_client() and new_full_parts() signature
liuchengxu Dec 18, 2022
15c7561
Remove needless `Block` type in `resolve_state_version_from_wasm`
liuchengxu Dec 19, 2022
695bd41
Merge branch 'master' of https://github.com/paritytech/substrate into…
liuchengxu Dec 19, 2022
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
13 changes: 12 additions & 1 deletion bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,22 @@ pub fn new_partial(
config.runtime_cache_size,
);

let backend = sc_service::new_db_backend(config.db_config())?;

let genesis_block_builder = sc_service::GenesisBlockBuilder::new(
config.chain_spec.as_storage_builder(),
!config.no_genesis(),
backend.clone(),
executor.clone(),
)?;

let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, _>(
sc_service::new_full_parts::<Block, RuntimeApi, _, _>(
config,
telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),
executor,
backend,
genesis_block_builder,
)?;
let client = Arc::new(client);

Expand Down
13 changes: 12 additions & 1 deletion bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,22 @@ pub fn new_partial(
config.runtime_cache_size,
);

let backend = sc_service::new_db_backend(config.db_config())?;

let genesis_block_builder = sc_service::GenesisBlockBuilder::new(
config.chain_spec.as_storage_builder(),
!config.no_genesis(),
backend.clone(),
executor.clone(),
)?;

let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, _>(
sc_service::new_full_parts::<Block, RuntimeApi, _, _>(
config,
telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),
executor,
backend,
genesis_block_builder,
)?;
let client = Arc::new(client);

Expand Down
17 changes: 16 additions & 1 deletion bin/node/inspect/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,22 @@ impl InspectCmd {
config.runtime_cache_size,
);

let client = new_full_client::<B, RA, _>(&config, None, executor)?;
let backend = sc_service::new_db_backend(config.db_config())?;

let genesis_block_builder = sc_service::GenesisBlockBuilder::new(
config.chain_spec.as_storage_builder(),
!config.no_genesis(),
backend.clone(),
executor.clone(),
)?;

let client = new_full_client::<B, RA, _, _>(
&config,
None,
executor,
backend,
genesis_block_builder,
)?;
let inspect = Inspector::<B>::new(client);

match &self.command {
Expand Down
30 changes: 20 additions & 10 deletions bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,24 +397,34 @@ impl BenchDb {
let task_executor = TaskExecutor::new();

let backend = sc_service::new_db_backend(db_config).expect("Should not fail");
let executor = NativeElseWasmExecutor::new(
WasmExecutionMethod::Compiled {
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
},
None,
8,
2,
);
let client_config = sc_service::ClientConfig::default();
let genesis_block_builder = sc_service::GenesisBlockBuilder::new(
&keyring.generate_genesis(),
!client_config.no_genesis,
backend.clone(),
executor.clone(),
)
.expect("Failed to create genesis block builder");

let client = sc_service::new_client(
backend.clone(),
NativeElseWasmExecutor::new(
WasmExecutionMethod::Compiled {
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
},
None,
8,
2,
),
&keyring.generate_genesis(),
executor,
genesis_block_builder,
None,
None,
ExecutionExtensions::new(profile.into_execution_strategies(), None, None),
Box::new(task_executor.clone()),
None,
None,
Default::default(),
client_config,
)
.expect("Should not fail");

Expand Down
47 changes: 27 additions & 20 deletions client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use crate::{
config::{Configuration, KeystoreConfig, PrometheusConfig},
error::Error,
metrics::MetricsService,
start_rpc_servers, RpcHandlers, SpawnTaskHandle, TaskManager, TransactionPoolAdapter,
start_rpc_servers, BuildGenesisBlock, RpcHandlers, SpawnTaskHandle, TaskManager,
TransactionPoolAdapter,
};
use futures::{channel::oneshot, future::ready, FutureExt, StreamExt};
use jsonrpsee::RpcModule;
Expand Down Expand Up @@ -72,7 +73,6 @@ use sp_keystore::{CryptoStore, SyncCryptoStore, SyncCryptoStorePtr};
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, BlockIdTo, NumberFor, Zero},
BuildStorage,
};
use std::{str::FromStr, sync::Arc, time::SystemTime};

Expand Down Expand Up @@ -170,27 +170,39 @@ impl KeystoreContainer {
}

/// Creates a new full client for the given config.
pub fn new_full_client<TBl, TRtApi, TExec>(
pub fn new_full_client<TBl, TRtApi, TExec, TBuildGenesisBlock>(
config: &Configuration,
telemetry: Option<TelemetryHandle>,
executor: TExec,
backend: Arc<TFullBackend<TBl>>,
genesis_block_builder: TBuildGenesisBlock,
) -> Result<TFullClient<TBl, TRtApi, TExec>, Error>
where
TBl: BlockT,
TExec: CodeExecutor + RuntimeVersionOf + Clone,
TBuildGenesisBlock: BuildGenesisBlock<
TBl,
BlockImportOperation = <Backend<TBl> as sc_client_api::backend::Backend<TBl>>::BlockImportOperation
>,
{
new_full_parts(config, telemetry, executor).map(|parts| parts.0)
new_full_parts(config, telemetry, executor, backend, genesis_block_builder).map(|parts| parts.0)
}

/// Create the initial parts of a full node.
pub fn new_full_parts<TBl, TRtApi, TExec>(
pub fn new_full_parts<TBl, TRtApi, TExec, TBuildGenesisBlock>(
liuchengxu marked this conversation as resolved.
Show resolved Hide resolved
config: &Configuration,
telemetry: Option<TelemetryHandle>,
executor: TExec,
backend: Arc<TFullBackend<TBl>>,
genesis_block_builder: TBuildGenesisBlock,
) -> Result<TFullParts<TBl, TRtApi, TExec>, Error>
where
TBl: BlockT,
TExec: CodeExecutor + RuntimeVersionOf + Clone,
TBuildGenesisBlock: BuildGenesisBlock<
TBl,
BlockImportOperation = <Backend<TBl> as sc_client_api::backend::Backend<TBl>>::BlockImportOperation
>,
{
let keystore_container = KeystoreContainer::new(&config.keystore)?;

Expand All @@ -208,16 +220,7 @@ where
.cloned()
.unwrap_or_default();

let (client, backend) = {
let db_config = sc_client_db::DatabaseSettings {
trie_cache_maximum_size: config.trie_cache_maximum_size,
state_pruning: config.state_pruning.clone(),
source: config.database.clone(),
blocks_pruning: config.blocks_pruning,
};

let backend = new_db_backend(db_config)?;

let client = {
let extensions = sc_client_api::execution_extensions::ExecutionExtensions::new(
config.execution_strategies.clone(),
Some(keystore_container.sync_keystore()),
Expand All @@ -244,7 +247,7 @@ where
let client = new_client(
backend.clone(),
executor,
chain_spec.as_storage_builder(),
genesis_block_builder,
fork_blocks,
bad_blocks,
extensions,
Expand All @@ -263,7 +266,7 @@ where
},
)?;

(client, backend)
client
};

Ok((client, backend, keystore_container, task_manager))
Expand All @@ -282,10 +285,10 @@ where
}

/// Create an instance of client backed by given backend.
pub fn new_client<E, Block, RA>(
pub fn new_client<E, Block, RA, G>(
backend: Arc<Backend<Block>>,
executor: E,
genesis_storage: &dyn BuildStorage,
genesis_block_builder: G,
fork_blocks: ForkBlocks<Block>,
bad_blocks: BadBlocks<Block>,
execution_extensions: ExecutionExtensions<Block>,
Expand All @@ -305,6 +308,10 @@ pub fn new_client<E, Block, RA>(
where
Block: BlockT,
E: CodeExecutor + RuntimeVersionOf,
G: BuildGenesisBlock<
Block,
BlockImportOperation = <Backend<Block> as sc_client_api::backend::Backend<Block>>::BlockImportOperation
>,
{
let executor = crate::client::LocalCallExecutor::new(
backend.clone(),
Expand All @@ -315,7 +322,7 @@ where
crate::client::Client::new(
backend,
executor,
genesis_storage,
genesis_block_builder,
fork_blocks,
bad_blocks,
execution_extensions,
Expand Down
33 changes: 18 additions & 15 deletions client/service/src/client/call_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,24 +361,27 @@ mod tests {
// LocalCallExecutor directly later on
let client_config = ClientConfig::default();

// client is used for the convenience of creating and inserting the genesis block.
let _client = substrate_test_runtime_client::client::new_with_backend::<
_,
_,
runtime::Block,
_,
runtime::RuntimeApi,
>(
let genesis_block_builder = crate::GenesisBlockBuilder::new(
&substrate_test_runtime_client::GenesisParameters::default().genesis_storage(),
!client_config.no_genesis,
backend.clone(),
executor.clone(),
&substrate_test_runtime_client::GenesisParameters::default().genesis_storage(),
None,
Box::new(TaskExecutor::new()),
None,
None,
Default::default(),
)
.expect("Creates a client");
.expect("Creates genesis block builder");

// client is used for the convenience of creating and inserting the genesis block.
let _client =
crate::client::new_with_backend::<_, _, runtime::Block, _, runtime::RuntimeApi>(
backend.clone(),
executor.clone(),
genesis_block_builder,
None,
Box::new(TaskExecutor::new()),
None,
None,
Default::default(),
)
.expect("Creates a client");

let call_executor = LocalCallExecutor {
backend: backend.clone(),
Expand Down
Loading