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

Commit

Permalink
move check backend out of state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
cheme committed Mar 7, 2022
1 parent 40d7f8e commit 3c84337
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 110 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions bin/node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ where
finality_provider,
)));

io.extend_with(pallet_state_trie_migration_rpc::StateMigrationApi::to_delegate(
pallet_state_trie_migration_rpc::MigrationRpc::new(client.clone(), deny_unsafe),
io.extend_with(sc_state_trie_migration_rpc::StateMigrationApi::to_delegate(
sc_state_trie_migration_rpc::MigrationRpc::new(client.clone(), deny_unsafe),
));

io.extend_with(sc_sync_state_rpc::SyncStateRpcApi::to_delegate(
Expand Down
4 changes: 0 additions & 4 deletions client/db/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,6 @@ impl<B: BlockT> StateBackend<HashFor<B>> for BenchmarkingState<B> {
}
})
}

fn migration_status(&self) -> Result<(u64, u64), Self::Error> {
unimplemented!()
}
}

impl<Block: BlockT> std::fmt::Debug for BenchmarkingState<Block> {
Expand Down
4 changes: 0 additions & 4 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,6 @@ impl<B: BlockT> StateBackend<HashFor<B>> for RefTrackingState<B> {
fn usage_info(&self) -> StateUsageInfo {
self.state.usage_info()
}

fn migration_status(&self) -> Result<(u64, u64), Self::Error> {
self.state.migration_status()
}
}

/// Database settings.
Expand Down
8 changes: 0 additions & 8 deletions client/db/src/storage_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,6 @@ impl<S: StateBackend<HashFor<B>>, B: BlockT> StateBackend<HashFor<B>> for Cachin
info.include_state_machine_states(&self.overlay_stats);
info
}

fn migration_status(&self) -> Result<(u64, u64), Self::Error> {
self.state.migration_status()
}
}

/// Extended [`CachingState`] that will sync the caches on drop.
Expand Down Expand Up @@ -926,10 +922,6 @@ impl<S: StateBackend<HashFor<B>>, B: BlockT> StateBackend<HashFor<B>>
fn usage_info(&self) -> sp_state_machine::UsageInfo {
self.caching_state().usage_info()
}

fn migration_status(&self) -> Result<(u64, u64), Self::Error> {
self.caching_state().migration_status()
}
}

impl<S, B: BlockT> Drop for SyncingCachingState<S, B> {
Expand Down
1 change: 1 addition & 0 deletions client/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ sp-block-builder = { version = "4.0.0-dev", path = "../../primitives/block-build
sc-informant = { version = "0.10.0-dev", path = "../informant" }
sc-telemetry = { version = "4.0.0-dev", path = "../telemetry" }
sc-offchain = { version = "4.0.0-dev", path = "../offchain" }
pallet-state-trie-migration = { version = "4.0.0-dev", path = "../../frame/state-trie-migration" }
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.10.0-dev" }
sc-tracing = { version = "4.0.0-dev", path = "../tracing" }
sp-tracing = { version = "5.0.0", path = "../../primitives/tracing" }
Expand Down
5 changes: 2 additions & 3 deletions client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1524,9 +1524,8 @@ where
}

fn state_migration_status(&self, id: &BlockId<Block>) -> sp_blockchain::Result<(u64, u64)> {
self.state_at(id)?
.migration_status()
.map_err(|e| sp_blockchain::Error::from_state(Box::new(e)))
pallet_state_trie_migration::utils::migration_status(&self.state_at(id)?)
.map_err(|e| Error::Backend(e))
}
}

Expand Down
8 changes: 7 additions & 1 deletion frame/state-trie-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ sp-std = { default-features = false, path = "../../primitives/std" }
sp-io = { default-features = false, path = "../../primitives/io" }
sp-core = { default-features = false, path = "../../primitives/core" }
sp-runtime = { default-features = false, path = "../../primitives/runtime" }
sp-state-machine = { optional = true, path = "../../primitives/state-machine" }
sp-trie = { optional = true, path = "../../primitives/trie" }
trie-db = { version = "0.23.1", optional = true }

frame-support = { default-features = false, path = "../support" }
frame-system = { default-features = false, path = "../system" }
Expand Down Expand Up @@ -49,7 +52,10 @@ std = [
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std"
"sp-state-machine",
"sp-std/std",
"sp-trie",
"trie-db"
]
runtime-benchmarks = ["frame-benchmarking"]
try-runtime = ["frame-support/try-runtime"]
Expand Down
95 changes: 93 additions & 2 deletions frame/state-trie-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,97 @@ mod mock {
}
}

/// Migration utility module.
#[cfg(feature = "std")]
pub mod utils {
use sp_core::{
storage::{ChildInfo, ChildType, PrefixedStorageKey},
Hasher,
};
use sp_state_machine::Backend;
use sp_trie::{KeySpacedDB, Trie};
use trie_db::{
node::{NodePlan, ValuePlan},
TrieDBNodeIterator,
};

/// Check trie migration status.
pub fn migration_status<H, B>(backend: &B) -> Result<(u64, u64), String>
where
H: Hasher,
H::Out: codec::Codec,
B: Backend<H>,
{
let trie_backend = if let Some(backend) = backend.as_trie_backend() {
backend
} else {
return Err("No access to trie from backend.".to_string())
};
let essence = trie_backend.essence();

let threshold: u32 = sp_core::storage::TRIE_VALUE_NODE_THRESHOLD;
let mut nb_to_migrate = 0;
let mut nb_to_migrate_child = 0;

let trie = sp_trie::trie_types::TrieDB::new(essence, &essence.root())
.map_err(|e| format!("TrieDB creation error: {}", e))?;
let iter_node = TrieDBNodeIterator::new(&trie)
.map_err(|e| format!("TrieDB node iterator error: {}", e))?;
for node in iter_node {
let node = node.map_err(|e| format!("TrieDB node iterator error: {}", e))?;
match node.2.node_plan() {
NodePlan::Leaf { value, .. } |
NodePlan::NibbledBranch { value: Some(value), .. } =>
if let ValuePlan::Inline(range) = value {
if (range.end - range.start) as u32 >= threshold {
nb_to_migrate += 1;
}
},
_ => (),
}
}

let mut child_roots: Vec<(ChildInfo, Vec<u8>)> = Vec::new();
// get all child trie roots
for key_value in trie.iter().map_err(|e| format!("TrieDB node iterator error: {}", e))? {
let (key, value) =
key_value.map_err(|e| format!("TrieDB node iterator error: {}", e))?;
if key[..]
.starts_with(sp_core::storage::well_known_keys::DEFAULT_CHILD_STORAGE_KEY_PREFIX)
{
let prefixed_key = PrefixedStorageKey::new(key);
let (_type, unprefixed) = ChildType::from_prefixed_key(&prefixed_key).unwrap();
child_roots.push((ChildInfo::new_default(unprefixed), value));
}
}
for (child_info, root) in child_roots {
let mut child_root = H::Out::default();
let storage = KeySpacedDB::new(essence, child_info.keyspace());

child_root.as_mut()[..].copy_from_slice(&root[..]);
let trie = sp_trie::trie_types::TrieDB::new(&storage, &child_root)
.map_err(|e| format!("New child TrieDB error: {}", e))?;
let iter_node = TrieDBNodeIterator::new(&trie)
.map_err(|e| format!("TrieDB node iterator error: {}", e))?;
for node in iter_node {
let node = node.map_err(|e| format!("Child TrieDB node iterator error: {}", e))?;
match node.2.node_plan() {
NodePlan::Leaf { value, .. } |
NodePlan::NibbledBranch { value: Some(value), .. } =>
if let ValuePlan::Inline(range) = value {
if (range.end - range.start) as u32 >= threshold {
nb_to_migrate_child += 1;
}
},
_ => (),
}
}
}

Ok((nb_to_migrate, nb_to_migrate_child))
}
}

#[cfg(test)]
mod test {
use super::{mock::*, *};
Expand Down Expand Up @@ -1453,7 +1544,7 @@ pub(crate) mod remote_tests {
// set the version to 1, as if the upgrade happened.
ext.state_version = sp_core::storage::StateVersion::V1;

let (top_left, child_left) = ext.as_backend().essence().check_migration_state().unwrap();
let (top_left, child_left) = crate::utils::migration_status(&ext.as_backend()).unwrap();
assert!(
top_left > 0,
"no node needs migrating, this probably means that state was initialized with `StateVersion::V1`",
Expand Down Expand Up @@ -1511,7 +1602,7 @@ pub(crate) mod remote_tests {
)
});

let (top_left, child_left) = ext.as_backend().essence().check_migration_state().unwrap();
let (top_left, child_left) = crate::utils::migration_status(&ext.as_backend()).unwrap();
assert_eq!(top_left, 0);
assert_eq!(child_left, 0);
}
Expand Down
2 changes: 0 additions & 2 deletions primitives/state-machine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ log = { version = "0.4.11", optional = true }
thiserror = { version = "1.0.30", optional = true }
parking_lot = { version = "0.12.0", optional = true }
hash-db = { version = "0.15.2", default-features = false }
trie-db = { version = "0.23.1", default-features = false }
trie-root = { version = "0.17.0", default-features = false }
sp-trie = { version = "6.0.0", path = "../trie", default-features = false }
sp-core = { version = "6.0.0", path = "../core", default-features = false }
Expand Down Expand Up @@ -47,7 +46,6 @@ std = [
"sp-externalities/std",
"sp-std/std",
"sp-trie/std",
"trie-db/std",
"trie-root/std",
"log",
"thiserror",
Expand Down
3 changes: 0 additions & 3 deletions primitives/state-machine/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,6 @@ pub trait Backend<H: Hasher>: sp_std::fmt::Debug {
fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)> {
unimplemented!()
}

/// Check trie migration status.
fn migration_status(&self) -> Result<(u64, u64), Self::Error>;
}

/// Trait that allows consolidate two transactions together.
Expand Down
5 changes: 0 additions & 5 deletions primitives/state-machine/src/proving_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,6 @@ where
fn usage_info(&self) -> crate::stats::UsageInfo {
self.0.usage_info()
}

fn migration_status(&self) -> Result<(u64, u64), Self::Error> {
// TODO consider error (proof over the whole state could be exclude from use case).
self.0.migration_status()
}
}

/// Create a backend used for checking the proof., using `H` as hasher.
Expand Down
4 changes: 0 additions & 4 deletions primitives/state-machine/src/trie_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ where
fn wipe(&self) -> Result<(), Self::Error> {
Ok(())
}

fn migration_status(&self) -> Result<(u64, u64), Self::Error> {
self.essence().check_migration_state()
}
}

#[cfg(test)]
Expand Down
72 changes: 1 addition & 71 deletions primitives/state-machine/src/trie_backend_essence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use codec::Encode;
use hash_db::{self, AsHashDB, HashDB, HashDBRef, Hasher, Prefix};
#[cfg(feature = "std")]
use parking_lot::RwLock;
use sp_core::storage::{ChildInfo, ChildType, PrefixedStorageKey, StateVersion};
use sp_core::storage::{ChildInfo, ChildType, StateVersion};
use sp_std::{boxed::Box, vec::Vec};
use sp_trie::{
child_delta_trie_root, delta_trie_root, empty_child_trie_root, read_child_trie_value,
Expand All @@ -36,10 +36,6 @@ use sp_trie::{
use std::collections::HashMap;
#[cfg(feature = "std")]
use std::sync::Arc;
use trie_db::{
node::{NodePlan, ValuePlan},
TrieDBNodeIterator,
};

#[cfg(not(feature = "std"))]
macro_rules! format {
Expand Down Expand Up @@ -433,72 +429,6 @@ where
);
}

/// Check remaining state item to migrate. Note this function should be remove when all state
/// migration did finished as it is only an utility.
// original author: @cheme
pub fn check_migration_state(&self) -> Result<(u64, u64)> {
let threshold: u32 = sp_core::storage::TRIE_VALUE_NODE_THRESHOLD;
let mut nb_to_migrate = 0;
let mut nb_to_migrate_child = 0;

let trie = sp_trie::trie_types::TrieDB::new(self, &self.root)
.map_err(|e| format!("TrieDB creation error: {}", e))?;
let iter_node = TrieDBNodeIterator::new(&trie)
.map_err(|e| format!("TrieDB node iterator error: {}", e))?;
for node in iter_node {
let node = node.map_err(|e| format!("TrieDB node iterator error: {}", e))?;
match node.2.node_plan() {
NodePlan::Leaf { value, .. } |
NodePlan::NibbledBranch { value: Some(value), .. } =>
if let ValuePlan::Inline(range) = value {
if (range.end - range.start) as u32 >= threshold {
nb_to_migrate += 1;
}
},
_ => (),
}
}

let mut child_roots: Vec<(ChildInfo, Vec<u8>)> = Vec::new();
// get all child trie roots
for key_value in trie.iter().map_err(|e| format!("TrieDB node iterator error: {}", e))? {
let (key, value) =
key_value.map_err(|e| format!("TrieDB node iterator error: {}", e))?;
if key[..]
.starts_with(sp_core::storage::well_known_keys::DEFAULT_CHILD_STORAGE_KEY_PREFIX)
{
let prefixed_key = PrefixedStorageKey::new(key);
let (_type, unprefixed) = ChildType::from_prefixed_key(&prefixed_key).unwrap();
child_roots.push((ChildInfo::new_default(unprefixed), value));
}
}
for (child_info, root) in child_roots {
let mut child_root = H::Out::default();
let storage = KeySpacedDB::new(self, child_info.keyspace());

child_root.as_mut()[..].copy_from_slice(&root[..]);
let trie = sp_trie::trie_types::TrieDB::new(&storage, &child_root)
.map_err(|e| format!("New child TrieDB error: {}", e))?;
let iter_node = TrieDBNodeIterator::new(&trie)
.map_err(|e| format!("TrieDB node iterator error: {}", e))?;
for node in iter_node {
let node = node.map_err(|e| format!("Child TrieDB node iterator error: {}", e))?;
match node.2.node_plan() {
NodePlan::Leaf { value, .. } |
NodePlan::NibbledBranch { value: Some(value), .. } =>
if let ValuePlan::Inline(range) = value {
if (range.end - range.start) as u32 >= threshold {
nb_to_migrate_child += 1;
}
},
_ => (),
}
}
}

Ok((nb_to_migrate, nb_to_migrate_child))
}

/// Returns all `(key, value)` pairs in the trie.
pub fn pairs(&self) -> Vec<(StorageKey, StorageValue)> {
let collect_all = || -> sp_std::result::Result<_, Box<TrieError<H::Out>>> {
Expand Down

0 comments on commit 3c84337

Please sign in to comment.