Companion for substrate#9732 (#678)

* state-update4 branch

* new ref

* Update to latest.

* update deps

* switch to host state version

* update

* fmt

* up

* remove trie patch

* remove patch

* fmt

* update

* set state_versions in runtimes

* state version from storage

* state version from storage

* seedling compat

* restore lock

* update lockfile for substrate

* update lockfile for polkadot

Co-authored-by: parity-processbot <>
This commit is contained in:
cheme
2021-12-24 12:28:43 +01:00
committed by GitHub
parent 876e594f59
commit 0256fe73c0
18 changed files with 297 additions and 269 deletions
+251 -250
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -430,8 +430,8 @@ mod tests {
assert_eq!(1, *block.header().number()); assert_eq!(1, *block.header().number());
// Ensure that we did not include `:code` in the proof. // Ensure that we did not include `:code` in the proof.
let db = block let proof = block.storage_proof();
.storage_proof() let db = proof
.to_storage_proof::<BlakeTwo256>(Some(header.state_root())) .to_storage_proof::<BlakeTwo256>(Some(header.state_root()))
.unwrap() .unwrap()
.0 .0
+7 -2
View File
@@ -21,21 +21,26 @@ use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT, Zero
/// Generate the genesis block from a given ChainSpec. /// Generate the genesis block from a given ChainSpec.
pub fn generate_genesis_block<Block: BlockT>( pub fn generate_genesis_block<Block: BlockT>(
chain_spec: &Box<dyn ChainSpec>, chain_spec: &Box<dyn ChainSpec>,
genesis_state_version: sp_runtime::StateVersion,
) -> Result<Block, String> { ) -> Result<Block, String> {
let storage = chain_spec.build_storage()?; let storage = chain_spec.build_storage()?;
let child_roots = storage.children_default.iter().map(|(sk, child_content)| { 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( let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
child_content.data.clone().into_iter().collect(), child_content.data.clone().into_iter().collect(),
genesis_state_version,
); );
(sk.clone(), state_root.encode()) (sk.clone(), state_root.encode())
}); });
let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root( let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
storage.top.clone().into_iter().chain(child_roots).collect(), storage.top.clone().into_iter().chain(child_roots).collect(),
genesis_state_version,
); );
let extrinsics_root = let extrinsics_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
<<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(Vec::new()); Vec::new(),
sp_runtime::StateVersion::V0,
);
Ok(Block::new( Ok(Block::new(
<<Block as BlockT>::Header as HeaderT>::new( <<Block as BlockT>::Header as HeaderT>::new(
+1
View File
@@ -371,6 +371,7 @@ mod tests {
impl_version: 1, impl_version: 1,
apis: sp_version::create_apis_vec!([]), apis: sp_version::create_apis_vec!([]),
transaction_version: 1, transaction_version: 1,
state_version: 1,
}; };
pub const ParachainId: ParaId = ParaId::new(200); pub const ParachainId: ParaId = ParaId::new(200);
pub const ReservedXcmpWeight: Weight = 0; pub const ReservedXcmpWeight: Weight = 0;
@@ -63,6 +63,7 @@ parameter_types! {
impl_version: 1, impl_version: 1,
apis: sp_version::create_apis_vec!([]), apis: sp_version::create_apis_vec!([]),
transaction_version: 1, transaction_version: 1,
state_version: 1,
}; };
pub const ParachainId: ParaId = ParaId::new(200); pub const ParachainId: ParaId = ParaId::new(200);
pub const ReservedXcmpWeight: Weight = 0; pub const ReservedXcmpWeight: Weight = 0;
@@ -26,7 +26,7 @@ use polkadot_parachain::primitives::{HeadData, ValidationParams, ValidationResul
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use sp_core::storage::ChildInfo; use sp_core::storage::{ChildInfo, StateVersion};
use sp_externalities::{set_and_run_with_externalities, Externalities}; use sp_externalities::{set_and_run_with_externalities, Externalities};
use sp_trie::MemoryDB; use sp_trie::MemoryDB;
@@ -68,7 +68,7 @@ where
// Uncompress // Uncompress
let mut db = MemoryDB::default(); let mut db = MemoryDB::default();
let root = match sp_trie::decode_compact::<sp_trie::Layout<HashFor<B>>, _, _>( let root = match sp_trie::decode_compact::<sp_trie::LayoutV1<HashFor<B>>, _, _>(
&mut db, &mut db,
storage_proof.iter_compact_encoded_nodes(), storage_proof.iter_compact_encoded_nodes(),
Some(parent_head.state_root()), Some(parent_head.state_root()),
@@ -221,8 +221,8 @@ fn host_storage_clear(key: &[u8]) {
with_externalities(|ext| ext.place_storage(key.to_vec(), None)) with_externalities(|ext| ext.place_storage(key.to_vec(), None))
} }
fn host_storage_root() -> Vec<u8> { fn host_storage_root(version: StateVersion) -> Vec<u8> {
with_externalities(|ext| ext.storage_root()) with_externalities(|ext| ext.storage_root(version))
} }
fn host_storage_clear_prefix(prefix: &[u8], limit: Option<u32>) -> KillStorageResult { fn host_storage_clear_prefix(prefix: &[u8], limit: Option<u32>) -> KillStorageResult {
@@ -327,9 +327,9 @@ fn host_default_child_storage_clear_prefix(
}) })
} }
fn host_default_child_storage_root(storage_key: &[u8]) -> Vec<u8> { fn host_default_child_storage_root(storage_key: &[u8], version: StateVersion) -> Vec<u8> {
let child_info = ChildInfo::new_default(storage_key); let child_info = ChildInfo::new_default(storage_key);
with_externalities(|ext| ext.child_storage_root(&child_info)) with_externalities(|ext| ext.child_storage_root(&child_info, version))
} }
fn host_default_child_storage_next_key(storage_key: &[u8], key: &[u8]) -> Option<Vec<u8>> { fn host_default_child_storage_next_key(storage_key: &[u8], key: &[u8]) -> Option<Vec<u8>> {
@@ -192,8 +192,9 @@ pub fn run() -> Result<()> {
builder.with_profiling(sc_tracing::TracingReceiver::Log, ""); builder.with_profiling(sc_tracing::TracingReceiver::Log, "");
let _ = builder.init(); let _ = builder.init();
let block: Block = let spec = load_spec(&params.chain.clone().unwrap_or_default())?;
generate_genesis_block(&load_spec(&params.chain.clone().unwrap_or_default())?)?; let state_version = Cli::native_runtime_version(&spec).state_version();
let block: Block = generate_genesis_block(&spec, state_version)?;
let raw_header = block.header().encode(); let raw_header = block.header().encode();
let output_buf = if params.raw { let output_buf = if params.raw {
raw_header raw_header
@@ -274,8 +275,10 @@ pub fn run() -> Result<()> {
let parachain_account = let parachain_account =
AccountIdConversion::<polkadot_primitives::v0::AccountId>::into_account(&id); AccountIdConversion::<polkadot_primitives::v0::AccountId>::into_account(&id);
let block: Block = let state_version =
generate_genesis_block(&config.chain_spec).map_err(|e| format!("{:?}", e))?; RelayChainCli::native_runtime_version(&config.chain_spec).state_version();
let block: Block = generate_genesis_block(&config.chain_spec, state_version)
.map_err(|e| format!("{:?}", e))?;
let genesis_state = format!("0x{:?}", HexDisplay::from(&block.header().encode())); let genesis_state = format!("0x{:?}", HexDisplay::from(&block.header().encode()));
let tokio_handle = config.tokio_handle.clone(); let tokio_handle = config.tokio_handle.clone();
@@ -180,6 +180,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 0, impl_version: 0,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 1, transaction_version: 1,
state_version: 1,
}; };
/// This determines the average expected block time that we are targeting. /// This determines the average expected block time that we are targeting.
@@ -97,6 +97,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 0, impl_version: 0,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 1, transaction_version: 1,
state_version: 0,
}; };
pub const MILLISECS_PER_BLOCK: u64 = 12000; pub const MILLISECS_PER_BLOCK: u64 = 12000;
@@ -60,6 +60,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 0, impl_version: 0,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 1, transaction_version: 1,
state_version: 0,
}; };
/// The version information used to identify this runtime when compiled natively. /// The version information used to identify this runtime when compiled natively.
@@ -71,6 +71,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 0, impl_version: 0,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 1, transaction_version: 1,
state_version: 0,
}; };
/// The version information used to identify this runtime when compiled natively. /// The version information used to identify this runtime when compiled natively.
+9 -3
View File
@@ -361,8 +361,10 @@ pub fn run() -> Result<()> {
builder.with_profiling(sc_tracing::TracingReceiver::Log, ""); builder.with_profiling(sc_tracing::TracingReceiver::Log, "");
let _ = builder.init(); let _ = builder.init();
let block: crate::service::Block = let spec = load_spec(&params.chain.clone().unwrap_or_default())?;
generate_genesis_block(&load_spec(&params.chain.clone().unwrap_or_default())?)?; let state_version = Cli::native_runtime_version(&spec).state_version();
let block: crate::service::Block = generate_genesis_block(&spec, state_version)?;
let raw_header = block.header().encode(); let raw_header = block.header().encode();
let output_buf = if params.raw { let output_buf = if params.raw {
raw_header raw_header
@@ -468,8 +470,12 @@ pub fn run() -> Result<()> {
let parachain_account = let parachain_account =
AccountIdConversion::<polkadot_primitives::v0::AccountId>::into_account(&id); AccountIdConversion::<polkadot_primitives::v0::AccountId>::into_account(&id);
let state_version =
RelayChainCli::native_runtime_version(&config.chain_spec).state_version();
let block: crate::service::Block = let block: crate::service::Block =
generate_genesis_block(&config.chain_spec).map_err(|e| format!("{:?}", e))?; generate_genesis_block(&config.chain_spec, state_version)
.map_err(|e| format!("{:?}", e))?;
let genesis_state = format!("0x{:?}", HexDisplay::from(&block.header().encode())); let genesis_state = format!("0x{:?}", HexDisplay::from(&block.header().encode()));
let tokio_handle = config.tokio_handle.clone(); let tokio_handle = config.tokio_handle.clone();
@@ -94,6 +94,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 0, impl_version: 0,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 3, transaction_version: 3,
state_version: 0,
}; };
/// The version information used to identify this runtime when compiled natively. /// The version information used to identify this runtime when compiled natively.
@@ -94,6 +94,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 0, impl_version: 0,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 3, transaction_version: 3,
state_version: 0,
}; };
/// The version information used to identify this runtime when compiled natively. /// The version information used to identify this runtime when compiled natively.
@@ -95,6 +95,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 0, impl_version: 0,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 4, transaction_version: 4,
state_version: 0,
}; };
/// The version information used to identify this runtime when compiled natively. /// The version information used to identify this runtime when compiled natively.
+2 -1
View File
@@ -118,6 +118,7 @@ impl RelayStateSproofBuilder {
self, self,
) -> (polkadot_primitives::v1::Hash, sp_state_machine::StorageProof) { ) -> (polkadot_primitives::v1::Hash, sp_state_machine::StorageProof) {
let (db, root) = MemoryDB::<HashFor<polkadot_primitives::v1::Block>>::default_with_root(); let (db, root) = MemoryDB::<HashFor<polkadot_primitives::v1::Block>>::default_with_root();
let state_version = Default::default(); // for test using default.
let mut backend = sp_state_machine::TrieBackend::new(db, root); let mut backend = sp_state_machine::TrieBackend::new(db, root);
let mut relevant_keys = Vec::new(); let mut relevant_keys = Vec::new();
@@ -126,7 +127,7 @@ impl RelayStateSproofBuilder {
let mut insert = |key: Vec<u8>, value: Vec<u8>| { let mut insert = |key: Vec<u8>, value: Vec<u8>| {
relevant_keys.push(key.clone()); relevant_keys.push(key.clone());
backend.insert(vec![(None, vec![(key, Some(value))])]); backend.insert(vec![(None, vec![(key, Some(value))])], state_version);
}; };
insert(relay_chain::well_known_keys::ACTIVE_CONFIG.to_vec(), self.host_config.encode()); insert(relay_chain::well_known_keys::ACTIVE_CONFIG.to_vec(), self.host_config.encode());
+2
View File
@@ -94,6 +94,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 1, impl_version: 1,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 1, transaction_version: 1,
state_version: 1,
}; };
#[cfg(feature = "increment-spec-version")] #[cfg(feature = "increment-spec-version")]
@@ -107,6 +108,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 1, impl_version: 1,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
transaction_version: 1, transaction_version: 1,
state_version: 1,
}; };
pub const MILLISECS_PER_BLOCK: u64 = 12000; pub const MILLISECS_PER_BLOCK: u64 = 12000;
+2 -1
View File
@@ -24,7 +24,8 @@ use sp_runtime::traits::Block as BlockT;
/// Returns the initial head data for a parachain ID. /// Returns the initial head data for a parachain ID.
pub fn initial_head_data(para_id: ParaId) -> HeadData { pub fn initial_head_data(para_id: ParaId) -> HeadData {
let spec = Box::new(crate::chain_spec::get_chain_spec(para_id)); let spec = Box::new(crate::chain_spec::get_chain_spec(para_id));
let block: Block = generate_genesis_block(&(spec as Box<_>)).unwrap(); let block: Block =
generate_genesis_block(&(spec as Box<_>), sp_runtime::StateVersion::V1).unwrap();
let genesis_state = block.header().encode(); let genesis_state = block.header().encode();
genesis_state.into() genesis_state.into()
} }