Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(trie): combining state parts memory usage #3986

Merged
merged 3 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 8 additions & 5 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1741,15 +1741,18 @@ impl Chain {
let shard_state_header = self.get_state_header(shard_id, sync_hash)?;
let mut height = shard_state_header.chunk_height_included();
let state_root = shard_state_header.chunk_prev_state_root();
let mut parts = vec![];
for part_id in 0..num_parts {
let key = StatePartKey(sync_hash, shard_id, part_id).try_to_vec()?;
parts.push(self.store.owned_store().get(ColStateParts, &key)?.unwrap());
let part = self.store.owned_store().get(ColStateParts, &key)?.unwrap();
self.runtime_adapter.apply_state_part(
shard_id,
&state_root,
part_id,
num_parts,
&part,
)?;
}

// Confirm that state matches the parts we received
self.runtime_adapter.confirm_state(shard_id, &state_root, &parts)?;

// Applying the chunk starts here
let mut chain_update = self.chain_update();
chain_update.set_state_finalize(shard_id, sync_hash, shard_state_header)?;
Expand Down
25 changes: 11 additions & 14 deletions chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,15 +816,12 @@ impl RuntimeAdapter for KeyValueRuntime {
num_parts: u64,
) -> Result<Vec<u8>, Error> {
assert!(part_id < num_parts);
if part_id != 0 {
return Ok(vec![]);
}
let state = self.state.read().unwrap().get(&state_root).unwrap().clone();
let data = state.try_to_vec().expect("should never fall");
let state_size = data.len() as u64;
let begin = state_size / num_parts * part_id;
let mut end = state_size / num_parts * (part_id + 1);
if part_id + 1 == num_parts {
end = state_size;
}
Ok(data[begin as usize..end as usize].to_vec())
Ok(data)
}

fn validate_state_part(
Expand All @@ -839,18 +836,18 @@ impl RuntimeAdapter for KeyValueRuntime {
true
}

fn confirm_state(
fn apply_state_part(
&self,
_shard_id: ShardId,
state_root: &StateRoot,
parts: &Vec<Vec<u8>>,
part_id: u64,
_num_parts: u64,
data: &Vec<u8>,
) -> Result<(), Error> {
let mut data = vec![];
for part in parts {
data.push(part.clone());
if part_id != 0 {
return Ok(());
}
let data_flatten: Vec<u8> = data.iter().flatten().cloned().collect();
let state = KVState::try_from_slice(&data_flatten).unwrap();
let state = KVState::try_from_slice(data).unwrap();
self.state.write().unwrap().insert(state_root.clone(), state.clone());
let data = state.try_to_vec()?;
let state_size = data.len() as u64;
Expand Down
6 changes: 4 additions & 2 deletions chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,11 +566,13 @@ pub trait RuntimeAdapter: Send + Sync {
) -> bool;

/// Should be executed after accepting all the parts to set up a new state.
fn confirm_state(
fn apply_state_part(
&self,
shard_id: ShardId,
state_root: &StateRoot,
parts: &Vec<Vec<u8>>,
part_id: u64,
num_parts: u64,
part: &Vec<u8>,
mikhailOK marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<(), Error>;

/// Returns StateRootNode of a state.
Expand Down
2 changes: 1 addition & 1 deletion chain/client/tests/process_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ fn test_process_block_after_state_sync() {
env.clients[0].chain.reset_data_pre_state_sync(sync_hash).unwrap();
env.clients[0]
.runtime_adapter
.confirm_state(0, &chunk_extra.state_root, &vec![state_part])
.apply_state_part(0, &chunk_extra.state_root, 0, 1, &state_part)
.unwrap();
let block = env.clients[0].produce_block(sync_height + 1).unwrap().unwrap();
let (_, res) = env.clients[0].process_block(block, Provenance::PRODUCED);
Expand Down
Loading