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

chore: move genesis block builder to chain-spec crate. #13427

Merged
merged 9 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions client/block-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ codec = { package = "parity-scale-codec", version = "3.2.2", features = [
"derive",
] }
sc-client-api = { version = "4.0.0-dev", path = "../api" }
sc-executor = { version = "0.10.0-dev", path = "../executor" }
sp-api = { version = "4.0.0-dev", path = "../../primitives/api" }
sp-block-builder = { version = "4.0.0-dev", path = "../../primitives/block-builder" }
sp-blockchain = { version = "4.0.0-dev", path = "../../primitives/blockchain" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

//! Tool for creating the genesis block.

use std::{marker::PhantomData, sync::Arc};

use sc_client_api::{backend::Backend, BlockImportOperation};
use sc_executor::RuntimeVersionOf;
use sp_core::storage::Storage;
use sp_runtime::{
traits::{Block as BlockT, Hash as HashT, Header as HeaderT, Zero},
BuildStorage,
};
use std::{marker::PhantomData, sync::Arc};

/// Create a genesis block, given the initial storage.
pub fn construct_genesis_block<Block: BlockT>(state_root: Block::Hash) -> Block {
Expand Down
40 changes: 39 additions & 1 deletion client/block-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@

#![warn(missing_docs)]

mod genesis;

pub use genesis::{construct_genesis_block, BuildGenesisBlock, GenesisBlockBuilder};
use std::collections::hash_map::DefaultHasher;

use codec::Encode;

use sp_api::{
Expand All @@ -40,9 +45,42 @@ use sp_runtime::{
Digest,
};

use sc_client_api::backend;
use sc_executor::RuntimeVersionOf;
pub use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_core::storage::{well_known_keys, StateVersion, Storage};

use sc_client_api::backend;
/// Return the state version given the genesis storage and executor.
pub fn resolve_state_version_from_wasm<E>(
yjhmelody marked this conversation as resolved.
Show resolved Hide resolved
storage: &Storage,
executor: &E,
) -> sp_blockchain::Result<StateVersion>
where
E: RuntimeVersionOf,
{
if let Some(wasm) = storage.top.get(well_known_keys::CODE) {
let mut ext = sp_state_machine::BasicExternalities::new_empty(); // just to read runtime version.

let code_fetcher = sp_core::traits::WrappedRuntimeCode(wasm.as_slice().into());
let runtime_code = sp_core::traits::RuntimeCode {
code_fetcher: &code_fetcher,
heap_pages: None,
hash: {
use std::hash::{Hash, Hasher};
let mut state = DefaultHasher::new();
wasm.hash(&mut state);
state.finish().to_le_bytes().to_vec()
},
};
let runtime_version = RuntimeVersionOf::runtime_version(executor, &mut ext, &runtime_code)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered using ReadRuntimeVersion trait + a manual decode instead? I think that could probably work? And it wouldn't require all of the fiddling with WrappedRuntimeCode and RuntimeCode.

.map_err(|e| sp_blockchain::Error::VersionInvalid(e.to_string()))?;
Ok(runtime_version.state_version())
} else {
Err(sp_blockchain::Error::VersionInvalid(
"Runtime missing from initial storage, could not read state version.".to_string(),
))
}
}

/// Used as parameter to [`BlockBuilderProvider`] to express if proof recording should be enabled.
///
Expand Down
7 changes: 2 additions & 5 deletions client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@

//! Substrate Client

use super::{
block_rules::{BlockRules, LookupResult as BlockLookupResult},
genesis::BuildGenesisBlock,
};
use super::block_rules::{BlockRules, LookupResult as BlockLookupResult};
use futures::{FutureExt, StreamExt};
use log::{error, info, trace, warn};
use parking_lot::{Mutex, RwLock};
use prometheus_endpoint::Registry;
use rand::Rng;
use sc_block_builder::{BlockBuilderApi, BlockBuilderProvider, RecordProof};
use sc_block_builder::{BlockBuilderApi, BlockBuilderProvider, BuildGenesisBlock, RecordProof};
use sc_client_api::{
backend::{
self, apply_aux, BlockImportOperation, ClientImportOperation, FinalizeSummary, Finalizer,
Expand Down
1 change: 0 additions & 1 deletion client/service/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
mod block_rules;
mod call_executor;
mod client;
pub mod genesis;
mod wasm_override;
mod wasm_substitutes;

Expand Down
10 changes: 6 additions & 4 deletions client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ pub use self::{
new_full_parts, spawn_tasks, BuildNetworkParams, KeystoreContainer, NetworkStarter,
SpawnTasksParams, TFullBackend, TFullCallExecutor, TFullClient,
},
client::{
genesis::{BuildGenesisBlock, GenesisBlockBuilder},
resolve_state_version_from_wasm, ClientConfig, LocalCallExecutor,
},
client::{ClientConfig, LocalCallExecutor},
error::Error,
};

pub use sc_block_builder::{
resolve_state_version_from_wasm, BuildGenesisBlock, GenesisBlockBuilder,
};

pub use config::{
BasePath, BlocksPruning, Configuration, DatabaseSource, PruningMode, Role, RpcMethods, TaskType,
};
Expand Down