Skip to content

Commit

Permalink
Extract process_state_key_values() out of process_state_verified()
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Oct 26, 2024
1 parent f40e937 commit 7b67beb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
4 changes: 3 additions & 1 deletion substrate/client/api/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub use sp_trie::MerkleValue;

use crate::{blockchain::Backend as BlockchainBackend, UsageInfo};

pub use sp_state_machine::{Backend as StateBackend, BackendTransaction, KeyValueStates};
pub use sp_state_machine::{
Backend as StateBackend, BackendTransaction, KeyValueStates, KeyValueStorageLevel,
};

/// Extracts the state backend type for the given backend.
pub type StateBackendFor<B, Block> = <B as Backend<Block>>::State;
Expand Down
51 changes: 30 additions & 21 deletions substrate/client/network/sync/src/strategy/state_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
};
use codec::{Decode, Encode};
use log::debug;
use sc_client_api::{CompactProof, KeyValueStates, ProofProvider};
use sc_client_api::{CompactProof, KeyValueStates, KeyValueStorageLevel, ProofProvider};
use sc_consensus::ImportedState;
use smallvec::SmallVec;
use sp_core::storage::well_known_keys;
Expand Down Expand Up @@ -138,31 +138,40 @@ where
self.state.entry(key_value.1).or_default().1.push(key_value.0);
}

fn process_state_verified(&mut self, values: KeyValueStates) {
for values in values.0 {
let is_top = values.state_root.is_empty();
fn process_state_key_values(
&mut self,
state_root: Vec<u8>,
key_values: impl IntoIterator<Item = (Vec<u8>, Vec<u8>)>,
) {
let is_top = state_root.is_empty();

let (child_key_values, top_key_values): (Vec<_>, Vec<_>) =
values.key_values.into_iter().partition(|key_value| {
is_top && well_known_keys::is_child_storage_key(key_value.0.as_slice())
});
let (child_key_values, top_key_values): (Vec<_>, Vec<_>) =
key_values.into_iter().partition(|key_value| {
is_top && well_known_keys::is_child_storage_key(key_value.0.as_slice())
});

for key_value in child_key_values {
self.insert_child_trie_roots(key_value);
}

let entry = self.state.entry(values.state_root).or_default();
for key_value in child_key_values {
self.insert_child_trie_roots(key_value);
}

if entry.0.len() > 0 && entry.1.len() > 1 {
// Already imported child_trie with same root.
// Warning this will not work with parallel download.
} else {
for (key, _value) in &top_key_values {
self.imported_bytes += key.len() as u64;
}
let entry = self.state.entry(state_root).or_default();

entry.0.extend(top_key_values);
if entry.0.len() > 0 && entry.1.len() > 1 {
// Already imported child_trie with same root.
// Warning this will not work with parallel download.
} else {
for (key, _value) in &top_key_values {
self.imported_bytes += key.len() as u64;
}

entry.0.extend(top_key_values);
}
}

fn process_state_verified(&mut self, values: KeyValueStates) {
for values in values.0 {
let KeyValueStorageLevel { state_root, parent_storage_keys: _, key_values } = values;
self.process_state_key_values(state_root, key_values);
}
}

Expand Down

0 comments on commit 7b67beb

Please sign in to comment.