Rename ExportGenesisStateCommand to ExportGenesisHeadCommand and make it respect custom genesis block builders (#2331)

Closes #2326.

This PR both fixes a logic bug and replaces an incorrect name.

## Bug Fix: Respecting custom genesis builder

Prior to this PR the standard logic for creating a genesis block was
repeated inside of cumulus. This PR removes that duplicated logic, and
calls into the proper `BuildGenesisBlock` implementation.

One consequence is that if the genesis block has already been
initialized, it will not be re-created, but rather read from the
database like it is for other node invocations. So you need to watch out
for old unpurged data during the development process. Offchain tools may
need to be updated accordingly. I've already filed
https://github.com/paritytech/zombienet/issues/1519

## Rename: It doesn't export state. It exports head data.

The name export-genesis-state was always wrong, nad it's never too late
to right a wrong. I've changed the name of the struct to
`ExportGenesisHeadCommand`.

There is still the question of what to do with individual nodes' public
CLIs. I have updated the parachain template to a reasonable default that
preserves compatibility with tools that will expect
`export-genesis-state` to still work. And I've chosen not to modify the
public CLIs of any other nodes in the repo. I'll leave it up to their
individual owners/maintains to decide whether that is appropriate.

---------

Co-authored-by: Joshy Orndorff <git-user-email.h0ly5@simplelogin.com>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Bastian Köcher <info@kchr.de>
This commit is contained in:
Joshy Orndorff
2023-12-13 04:18:33 -05:00
committed by GitHub
parent a84dd0dba5
commit 8683bbeefb
14 changed files with 118 additions and 142 deletions
+25 -58
View File
@@ -23,20 +23,18 @@ use std::{
io::{self, Write},
net::SocketAddr,
path::PathBuf,
sync::Arc,
};
use codec::Encode;
use sc_chain_spec::ChainSpec;
use sc_client_api::ExecutorProvider;
use sc_client_api::HeaderBackend;
use sc_service::{
config::{PrometheusConfig, TelemetryEndpoints},
BasePath, TransactionPoolOptions,
};
use sp_core::hexdisplay::HexDisplay;
use sp_runtime::{
traits::{Block as BlockT, Hash as HashT, Header as HeaderT, Zero},
StateVersion,
};
use sp_runtime::traits::{Block as BlockT, Zero};
use url::Url;
/// The `purge-chain` command used to remove the whole chain: the parachain and the relay chain.
@@ -129,9 +127,9 @@ impl sc_cli::CliConfiguration for PurgeChainCmd {
}
}
/// Command for exporting the genesis state of the parachain
/// Command for exporting the genesis head data of the parachain
#[derive(Debug, clap::Parser)]
pub struct ExportGenesisStateCommand {
pub struct ExportGenesisHeadCommand {
/// Output file name or stdout if unspecified.
#[arg()]
pub output: Option<PathBuf>,
@@ -145,24 +143,29 @@ pub struct ExportGenesisStateCommand {
pub shared_params: sc_cli::SharedParams,
}
impl ExportGenesisStateCommand {
/// Run the export-genesis-state command
pub fn run<Block: BlockT>(
&self,
chain_spec: &dyn ChainSpec,
client: &impl ExecutorProvider<Block>,
) -> sc_cli::Result<()> {
let state_version = sc_chain_spec::resolve_state_version_from_wasm(
&chain_spec.build_storage()?,
client.executor(),
)?;
impl ExportGenesisHeadCommand {
/// Run the export-genesis-head command
pub fn run<B, C>(&self, client: Arc<C>) -> sc_cli::Result<()>
where
B: BlockT,
C: HeaderBackend<B> + 'static,
{
let genesis_hash = client.hash(Zero::zero())?.ok_or(sc_cli::Error::Client(
sp_blockchain::Error::Backend(
"Failed to lookup genesis block hash when exporting genesis head data.".into(),
),
))?;
let genesis_header = client.header(genesis_hash)?.ok_or(sc_cli::Error::Client(
sp_blockchain::Error::Backend(
"Failed to lookup genesis header by hash when exporting genesis head data.".into(),
),
))?;
let block: Block = generate_genesis_block(chain_spec, state_version)?;
let raw_header = block.header().encode();
let raw_header = genesis_header.encode();
let output_buf = if self.raw {
raw_header
} else {
format!("0x{:?}", HexDisplay::from(&block.header().encode())).into_bytes()
format!("0x{:?}", HexDisplay::from(&genesis_header.encode())).into_bytes()
};
if let Some(output) = &self.output {
@@ -175,43 +178,7 @@ impl ExportGenesisStateCommand {
}
}
/// Generate the genesis block from a given ChainSpec.
pub fn generate_genesis_block<Block: BlockT>(
chain_spec: &dyn ChainSpec,
genesis_state_version: StateVersion,
) -> Result<Block, String> {
let storage = chain_spec.build_storage()?;
let child_roots = storage.children_default.iter().map(|(sk, child_content)| {
let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
child_content.data.clone().into_iter().collect(),
genesis_state_version,
);
(sk.clone(), state_root.encode())
});
let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
storage.top.clone().into_iter().chain(child_roots).collect(),
genesis_state_version,
);
let extrinsics_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
Vec::new(),
genesis_state_version,
);
Ok(Block::new(
<<Block as BlockT>::Header as HeaderT>::new(
Zero::zero(),
extrinsics_root,
state_root,
Default::default(),
Default::default(),
),
Default::default(),
))
}
impl sc_cli::CliConfiguration for ExportGenesisStateCommand {
impl sc_cli::CliConfiguration for ExportGenesisHeadCommand {
fn shared_params(&self) -> &sc_cli::SharedParams {
&self.shared_params
}