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

Commit

Permalink
Move child changeset creation to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
athei committed Jun 11, 2020
1 parent 4cf20c9 commit 10022f6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
39 changes: 23 additions & 16 deletions primitives/state-machine/src/overlayed_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,16 @@ impl OverlayedChanges {
let size_write = val.as_ref().map(|x| x.len() as u64).unwrap_or(0);
self.stats.tally_write_overlay(size_write);
let storage_key = child_info.storage_key().to_vec();
let tx_depth = self.transaction_depth();
let num_client_tx = self.top.num_client_transactions();
let exec_mode = self.top.execution_mode();
let (overlay, info) = self.children.entry(storage_key)
.or_insert_with(|| (
// This changeset might be created when there are already open transactions.
// We need to catch up here so it is at the same transaction depth.
OverlayedChangeSet::new(tx_depth, num_client_tx, exec_mode),
let changeset = self.top.spawn_child();

This comment has been minimized.

Copy link
@cheme

cheme Jun 11, 2020

Contributor

why not putting the call to spawn_child into or_insert_with fn parameter (we already use a ref to child_info)?

This comment has been minimized.

Copy link
@athei

athei Jun 18, 2020

Author Member

It is just based on an old version of my PR. It will be rebased on master once storage tx is merged. I would recommend to have my branch as PR target for now. So we do not have those commits in here.

let (changeset, info) = self.children.entry(storage_key).or_insert_with(||
(
changeset,
child_info.to_owned()
));
)
);
let updatable = info.try_update(child_info);
debug_assert!(updatable);
overlay.set(key, val, extrinsic_index);
changeset.set(key, val, extrinsic_index);
}

/// Clear child storage of given storage key.
Expand All @@ -268,9 +265,14 @@ impl OverlayedChanges {
child_info: &ChildInfo,
) {
let extrinsic_index = self.extrinsic_index();
let storage_key = child_info.storage_key();
let (changeset, info) = self.children.entry(storage_key.to_vec())
.or_insert_with(|| (Default::default(), child_info.to_owned()));
let storage_key = child_info.storage_key().to_vec();
let changeset = self.top.spawn_child();
let (changeset, info) = self.children.entry(storage_key).or_insert_with(||
(
changeset,
child_info.to_owned()
)
);
let updatable = info.try_update(child_info);
debug_assert!(updatable);
changeset.clear_where(|_, _| true, extrinsic_index);
Expand All @@ -292,9 +294,14 @@ impl OverlayedChanges {
prefix: &[u8],
) {
let extrinsic_index = self.extrinsic_index();
let storage_key = child_info.storage_key();
let (changeset, info) = self.children.entry(storage_key.to_vec())
.or_insert_with(|| (Default::default(), child_info.to_owned()));
let storage_key = child_info.storage_key().to_vec();
let changeset = self.top.spawn_child();
let (changeset, info) = self.children.entry(storage_key).or_insert_with(||
(
changeset,
child_info.to_owned()
)
);
let updatable = info.try_update(child_info);
debug_assert!(updatable);
changeset.clear_where(|key, _| key.starts_with(prefix), extrinsic_index);
Expand Down
25 changes: 8 additions & 17 deletions primitives/state-machine/src/overlayed_changes/changeset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,16 @@ fn insert_dirty(set: &mut DirtyKeysSets, key: StorageKey) -> bool {
}

impl OverlayedChangeSet {
/// Create a new changeset.
pub fn new(depth: usize, num_client_transactions: usize, mode: ExecutionMode) -> Self {
/// Create a new changeset at the same transaction state but without any contents.
///
/// This changeset might be created when there are already open transactions.
/// We need to catch up here so that the child is at the same transaction depth.
pub fn spawn_child(&self) -> Self {
use std::iter::repeat;
Self {
dirty_keys: repeat(HashSet::new()).take(depth).collect(),
num_client_transactions,
execution_mode: mode,
dirty_keys: repeat(HashSet::new()).take(self.transaction_depth()).collect(),
num_client_transactions: self.num_client_transactions,
execution_mode: self.execution_mode,
.. Default::default()
}
}
Expand Down Expand Up @@ -264,18 +267,6 @@ impl OverlayedChangeSet {
self.dirty_keys.len()
}

/// Returns how many of the open transactions are opened by the client.
///
/// These are always the first transaction to open and the last to close.
pub fn num_client_transactions(&self) -> usize {
self.num_client_transactions
}

/// Returns whether we are executing in client or runtime mode.
pub fn execution_mode(&self) -> ExecutionMode {
self.execution_mode
}

/// Call this before transfering control to the runtime.
///
/// This protects all existing transactions from being removed by the runtime.
Expand Down

0 comments on commit 10022f6

Please sign in to comment.