Skip to content

Commit

Permalink
#12067 wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hirschenberger committed Sep 28, 2022
1 parent 2ee4cb4 commit 4f077b0
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 180 deletions.
24 changes: 12 additions & 12 deletions primitives/externalities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,24 @@ pub trait Externalities: ExtensionStore {
fn set_offchain_storage(&mut self, key: &[u8], value: Option<&[u8]>);

/// Read runtime storage.
fn storage(&self, key: &[u8]) -> Option<Vec<u8>>;
fn storage(&mut self, key: &[u8]) -> Option<Vec<u8>>;

/// Get storage value hash.
///
/// This may be optimized for large values.
fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>>;
fn storage_hash(&mut self, key: &[u8]) -> Option<Vec<u8>>;

/// Get child storage value hash.
///
/// This may be optimized for large values.
///
/// Returns an `Option` that holds the SCALE encoded hash.
fn child_storage_hash(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;
fn child_storage_hash(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;

/// Read child runtime storage.
///
/// Returns an `Option` that holds the SCALE encoded hash.
fn child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;
fn child_storage(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;

/// Set storage entry `key` of current contract being called (effective immediately).
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
Expand All @@ -125,20 +125,20 @@ pub trait Externalities: ExtensionStore {
}

/// Whether a storage entry exists.
fn exists_storage(&self, key: &[u8]) -> bool {
fn exists_storage(&mut self, key: &[u8]) -> bool {
self.storage(key).is_some()
}

/// Whether a child storage entry exists.
fn exists_child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> bool {
fn exists_child_storage(&mut self, child_info: &ChildInfo, key: &[u8]) -> bool {
self.child_storage(child_info, key).is_some()
}

/// Returns the key immediately following the given key, if it exists.
fn next_storage_key(&self, key: &[u8]) -> Option<Vec<u8>>;
fn next_storage_key(&mut self, key: &[u8]) -> Option<Vec<u8>>;

/// Returns the key immediately following the given key, if it exists, in child storage.
fn next_child_storage_key(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;
fn next_child_storage_key(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;

/// Clear an entire child storage.
///
Expand Down Expand Up @@ -270,7 +270,7 @@ pub trait Externalities: ExtensionStore {
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
///
/// Gets the current read/write count for the benchmarking process.
fn read_write_count(&self) -> (u32, u32, u32, u32);
fn read_write_count(&mut self) -> (u32, u32, u32, u32);

/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/// Benchmarking related functionality and shouldn't be used anywhere else!
Expand All @@ -284,7 +284,7 @@ pub trait Externalities: ExtensionStore {
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
///
/// Gets the current DB tracking whitelist.
fn get_whitelist(&self) -> Vec<TrackedStorageKey>;
fn get_whitelist(&mut self) -> Vec<TrackedStorageKey>;

/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/// Benchmarking related functionality and shouldn't be used anywhere else!
Expand All @@ -299,7 +299,7 @@ pub trait Externalities: ExtensionStore {
///
/// Returns estimated proof size for the state queries so far.
/// Proof is reset on commit and wipe.
fn proof_size(&self) -> Option<u32> {
fn proof_size(&mut self) -> Option<u32> {
None
}

Expand All @@ -308,7 +308,7 @@ pub trait Externalities: ExtensionStore {
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
///
/// Get all the keys that have been read or written to during the benchmark.
fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)>;
fn get_read_and_written_keys(&mut self) -> Vec<(Vec<u8>, u32, u32, bool)>;
}

/// Extension for the [`Externalities`] trait.
Expand Down
1 change: 1 addition & 0 deletions primitives/state-machine/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ pub trait AsTrieBackend<H: Hasher, C = sp_trie::cache::LocalTrieCache<H>> {

/// Return the type as [`TrieBackend`].
fn as_trie_backend(&self) -> &TrieBackend<Self::TrieBackendStorage, H, C>;
fn as_trie_backend_mut(&mut self) -> &mut TrieBackend<Self::TrieBackendStorage, H, C>;
}

/// Trait that allows consolidate two transactions together.
Expand Down
33 changes: 17 additions & 16 deletions primitives/state-machine/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ impl BasicExternalities {

impl PartialEq for BasicExternalities {
fn eq(&self, other: &BasicExternalities) -> bool {
self.overlay.changes().map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>() ==
other.overlay.changes().map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>() &&
self.overlay
self.overlay.changes().map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>()
== other.overlay.changes().map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>()
&& self
.overlay
.children()
.map(|(iter, i)| (i, iter.map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>()))
.collect::<BTreeMap<_, _>>() ==
other
.collect::<BTreeMap<_, _>>()
== other
.overlay
.children()
.map(|(iter, i)| {
Expand Down Expand Up @@ -160,27 +161,27 @@ impl From<BTreeMap<StorageKey, StorageValue>> for BasicExternalities {
impl Externalities for BasicExternalities {
fn set_offchain_storage(&mut self, _key: &[u8], _value: Option<&[u8]>) {}

fn storage(&self, key: &[u8]) -> Option<StorageValue> {
fn storage(&mut self, key: &[u8]) -> Option<StorageValue> {
self.overlay.storage(key).and_then(|v| v.map(|v| v.to_vec()))
}

fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>> {
fn storage_hash(&mut self, key: &[u8]) -> Option<Vec<u8>> {
self.storage(key).map(|v| Blake2Hasher::hash(&v).encode())
}

fn child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageValue> {
fn child_storage(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageValue> {
self.overlay.child_storage(child_info, key).and_then(|v| v.map(|v| v.to_vec()))
}

fn child_storage_hash(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
fn child_storage_hash(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
self.child_storage(child_info, key).map(|v| Blake2Hasher::hash(&v).encode())
}

fn next_storage_key(&self, key: &[u8]) -> Option<StorageKey> {
fn next_storage_key(&mut self, key: &[u8]) -> Option<StorageKey> {
self.overlay.iter_after(key).find_map(|(k, v)| v.value().map(|_| k.to_vec()))
}

fn next_child_storage_key(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageKey> {
fn next_child_storage_key(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageKey> {
self.overlay
.child_iter_after(child_info.storage_key(), key)
.find_map(|(k, v)| v.value().map(|_| k.to_vec()))
Expand All @@ -189,7 +190,7 @@ impl Externalities for BasicExternalities {
fn place_storage(&mut self, key: StorageKey, maybe_value: Option<StorageValue>) {
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to set child storage key via main storage");
return
return;
}

self.overlay.set_storage(key, maybe_value)
Expand Down Expand Up @@ -226,7 +227,7 @@ impl Externalities for BasicExternalities {
"Refuse to clear prefix that is part of child storage key via main storage"
);
let maybe_cursor = Some(prefix.to_vec());
return MultiRemovalResults { maybe_cursor, backend: 0, unique: 0, loops: 0 }
return MultiRemovalResults { maybe_cursor, backend: 0, unique: 0, loops: 0 };
}

let count = self.overlay.clear_prefix(prefix);
Expand Down Expand Up @@ -307,23 +308,23 @@ impl Externalities for BasicExternalities {

fn commit(&mut self) {}

fn read_write_count(&self) -> (u32, u32, u32, u32) {
fn read_write_count(&mut self) -> (u32, u32, u32, u32) {
unimplemented!("read_write_count is not supported in Basic")
}

fn reset_read_write_count(&mut self) {
unimplemented!("reset_read_write_count is not supported in Basic")
}

fn get_whitelist(&self) -> Vec<TrackedStorageKey> {
fn get_whitelist(&mut self) -> Vec<TrackedStorageKey> {
unimplemented!("get_whitelist is not supported in Basic")
}

fn set_whitelist(&mut self, _: Vec<TrackedStorageKey>) {
unimplemented!("set_whitelist is not supported in Basic")
}

fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)> {
fn get_read_and_written_keys(&mut self) -> Vec<(Vec<u8>, u32, u32, bool)> {
unimplemented!("get_read_and_written_keys is not supported in Basic")
}
}
Expand Down
40 changes: 20 additions & 20 deletions primitives/state-machine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ where
self.overlay.set_offchain_storage(key, value)
}

fn storage(&self, key: &[u8]) -> Option<StorageValue> {
fn storage(&mut self, key: &[u8]) -> Option<StorageValue> {
let _guard = guard();
let result = self
.overlay
Expand All @@ -206,7 +206,7 @@ where
result
}

fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>> {
fn storage_hash(&mut self, key: &[u8]) -> Option<Vec<u8>> {
let _guard = guard();
let result = self
.overlay
Expand All @@ -224,7 +224,7 @@ where
result.map(|r| r.encode())
}

fn child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageValue> {
fn child_storage(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageValue> {
let _guard = guard();
let result = self
.overlay
Expand All @@ -246,7 +246,7 @@ where
result
}

fn child_storage_hash(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
fn child_storage_hash(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
let _guard = guard();
let result = self
.overlay
Expand All @@ -268,7 +268,7 @@ where
result.map(|r| r.encode())
}

fn exists_storage(&self, key: &[u8]) -> bool {
fn exists_storage(&mut self, key: &[u8]) -> bool {
let _guard = guard();
let result = match self.overlay.storage(key) {
Some(x) => x.is_some(),
Expand All @@ -286,7 +286,7 @@ where
result
}

fn exists_child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> bool {
fn exists_child_storage(&mut self, child_info: &ChildInfo, key: &[u8]) -> bool {
let _guard = guard();

let result = match self.overlay.child_storage(child_info, key) {
Expand All @@ -308,7 +308,7 @@ where
result
}

fn next_storage_key(&self, key: &[u8]) -> Option<StorageKey> {
fn next_storage_key(&mut self, key: &[u8]) -> Option<StorageKey> {
let mut next_backend_key =
self.backend.next_storage_key(key).expect(EXT_NOT_ALLOWED_TO_FAIL);
let mut overlay_changes = self.overlay.iter_after(key).peekable();
Expand All @@ -321,11 +321,11 @@ where

// If `backend_key` is less than the `overlay_key`, we found out next key.
if cmp == Some(Ordering::Less) {
return next_backend_key
return next_backend_key;
} else if overlay_key.1.value().is_some() {
// If there exists a value for the `overlay_key` in the overlay
// (aka the key is still valid), it means we have found our next key.
return Some(overlay_key.0.to_vec())
return Some(overlay_key.0.to_vec());
} else if cmp == Some(Ordering::Equal) {
// If the `backend_key` and `overlay_key` are equal, it means that we need
// to search for the next backend key, because the overlay has overwritten
Expand All @@ -346,7 +346,7 @@ where
}
}

fn next_child_storage_key(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageKey> {
fn next_child_storage_key(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageKey> {
let mut next_backend_key = self
.backend
.next_child_storage_key(child_info, key)
Expand All @@ -362,11 +362,11 @@ where

// If `backend_key` is less than the `overlay_key`, we found out next key.
if cmp == Some(Ordering::Less) {
return next_backend_key
return next_backend_key;
} else if overlay_key.1.value().is_some() {
// If there exists a value for the `overlay_key` in the overlay
// (aka the key is still valid), it means we have found our next key.
return Some(overlay_key.0.to_vec())
return Some(overlay_key.0.to_vec());
} else if cmp == Some(Ordering::Equal) {
// If the `backend_key` and `overlay_key` are equal, it means that we need
// to search for the next backend key, because the overlay has overwritten
Expand All @@ -391,7 +391,7 @@ where
let _guard = guard();
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to directly set child storage key");
return
return;
}

// NOTE: be careful about touching the key names – used outside substrate!
Expand Down Expand Up @@ -472,7 +472,7 @@ where
target: "trie",
"Refuse to directly clear prefix that is part or contains of child storage key",
);
return MultiRemovalResults { maybe_cursor: None, backend: 0, unique: 0, loops: 0 }
return MultiRemovalResults { maybe_cursor: None, backend: 0, unique: 0, loops: 0 };
}

self.mark_dirty();
Expand Down Expand Up @@ -538,7 +538,7 @@ where
storage_root = %HexDisplay::from(&root.as_ref()),
cached = true,
);
return root.encode()
return root.encode();
}

let root =
Expand Down Expand Up @@ -716,27 +716,27 @@ where
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}

fn read_write_count(&self) -> (u32, u32, u32, u32) {
fn read_write_count(&mut self) -> (u32, u32, u32, u32) {
self.backend.read_write_count()
}

fn reset_read_write_count(&mut self) {
self.backend.reset_read_write_count()
}

fn get_whitelist(&self) -> Vec<TrackedStorageKey> {
fn get_whitelist(&mut self) -> Vec<TrackedStorageKey> {
self.backend.get_whitelist()
}

fn set_whitelist(&mut self, new: Vec<TrackedStorageKey>) {
self.backend.set_whitelist(new)
}

fn proof_size(&self) -> Option<u32> {
fn proof_size(&mut self) -> Option<u32> {
self.backend.proof_size()
}

fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)> {
fn get_read_and_written_keys(&mut self) -> Vec<(Vec<u8>, u32, u32, bool)> {
self.backend.get_read_and_written_keys()
}
}
Expand All @@ -761,7 +761,7 @@ where
.apply_to_keys_while(maybe_child, maybe_prefix, maybe_cursor, |key| {
if maybe_limit.map_or(false, |limit| loop_count == limit) {
maybe_next_key = Some(key.to_vec());
return false
return false;
}
let overlay = match maybe_child {
Some(child_info) => self.overlay.child_storage(child_info, key),
Expand Down
Loading

0 comments on commit 4f077b0

Please sign in to comment.