This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Light friendly storage tracking: track last-modification-time entry in storage #425
Closed
Closed
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
12f3404
Prefix storage values
svyatonik 8e2d736
Merge branch 'master' into storage_value_prefix4
svyatonik 805b609
fixed stalled entries in the :deleted set
svyatonik 44b1f95
Merge branch 'storage_value_prefix4' of https://github.com/paritytech…
svyatonik 6728d96
fixed tests
svyatonik 7c6b525
fix typo
svyatonik f3c6270
Merge branch 'master' into storage_value_prefix4
svyatonik a49c0a9
do not call schedule_purge() for temporary values
svyatonik 88ca94e
fixed chainspec load
svyatonik 596eb90
cache storage prefix
svyatonik 962e568
more tests
svyatonik da1030b
Add .gitlab-ci.yml to allow for CI build.
ddorgan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file modified
BIN
+54.1 KB
(110%)
demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm
Binary file not shown.
Binary file modified
BIN
+54.1 KB
(110%)
demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file modified
BIN
+61.8 KB
(110%)
polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.compact.wasm
Binary file not shown.
Binary file modified
BIN
+61.8 KB
(110%)
polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.wasm
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+42.1 KB
(180%)
substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm
Binary file not shown.
Binary file modified
BIN
+42.1 KB
(180%)
substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.wasm
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2018 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate Demo. | ||
|
||
// Substrate Demo is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Substrate Demo is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Substrate Demo. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/// 2nd level prefix for the length of list of set items. | ||
const LIST_LEN_KEY_PREFIX: &'static [u8] = b":list:len"; | ||
/// 2nd level prefix for the items of list of set items. | ||
const LIST_ITEM_KEY_PREFIX: &'static [u8] = b":list:item"; | ||
/// 2nd level prefix for the map of set items. | ||
const MAP_ITEM_PREFIX: &'static [u8] = b":map:"; | ||
|
||
/// Keys set storage. Should write and read raw values (without adding/stripping the prefix). | ||
pub trait Storage { | ||
/// Read `key` from the storage. | ||
fn read(&self, key: &[u8]) -> Option<Vec<u8>>; | ||
/// Set value under `key` to the storage. | ||
fn set(&mut self, key: &[u8], value: &[u8]); | ||
/// Clear value under `key`. | ||
fn clear(&mut self, key: &[u8]); | ||
} | ||
|
||
/// A set of keys. Holds unique set of storage keys. | ||
/// Allows to enumerate + optionally remove filtered keys (Vec::retain sematics). | ||
pub struct Set<'a, S: 'a> { | ||
/// Prefix for this map. | ||
prefix: &'a [u8], | ||
/// Set storage | ||
storage: &'a mut S, | ||
} | ||
|
||
impl<'a, S: 'a + Storage> Set<'a, S> { | ||
/// Initialize with given prefix. | ||
pub fn new(prefix: &'a [u8], storage: &'a mut S) -> Self { | ||
Set { prefix, storage } | ||
} | ||
|
||
/// Insert item into the set. | ||
pub fn insert(&mut self, item: &[u8]) { | ||
// check if value is already in the set | ||
let map_item_key = compose_key3(self.prefix, MAP_ITEM_PREFIX, item); | ||
match self.storage.read(&map_item_key) { | ||
Some(_) => return, | ||
None => self.storage.set(&map_item_key, &[1]), | ||
} | ||
|
||
// increase list length | ||
let list_len_key = compose_key2(self.prefix, LIST_LEN_KEY_PREFIX); | ||
let list_len: u32 = self.storage.read(&list_len_key) | ||
.and_then(|len| Decode::decode(&mut &len[..])) | ||
.unwrap_or(0); | ||
self.storage.set(&list_len_key, &(list_len + 1).encode()); | ||
|
||
// set item at list length | ||
let list_item_key = compose_key3(self.prefix, LIST_ITEM_KEY_PREFIX, &list_len.encode()); | ||
self.storage.set(&list_item_key, item); | ||
} | ||
|
||
/// Retains only the elements specified by the predicate, which takes key, prefix and value. | ||
#[allow(dead_code)] // part of the Set, but used only by runtime => allow dead code to keep it together | ||
pub fn retain<F: FnMut(&mut S, &[u8]) -> bool>(&mut self, mut f: F) { | ||
// read list length | ||
let list_len_key = compose_key2(self.prefix, LIST_LEN_KEY_PREFIX); | ||
let mut list_len: u32 = self.storage.read(&list_len_key) | ||
.and_then(|len| Decode::decode(&mut &len[..])) | ||
.unwrap_or(0); | ||
|
||
// for each list item: run f and remove item if required | ||
let mut idx = 0u32; | ||
while idx < list_len { | ||
let list_item_key = compose_key3(self.prefix, LIST_ITEM_KEY_PREFIX, &idx.encode()); | ||
let list_item = self.storage.read(&list_item_key) | ||
.expect("idx < list_len; list items from 0 to list_len should exist; qed"); | ||
if f(self.storage, &list_item) { | ||
idx += 1; | ||
continue; | ||
} | ||
|
||
// swap_remove item from the list | ||
if idx != list_len - 1 { | ||
let last_list_item_key = compose_key3(self.prefix, LIST_ITEM_KEY_PREFIX, &(list_len - 1).encode()); | ||
let last_list_item = self.storage.read(&last_list_item_key) | ||
.expect("list_len - 1 < list_len; list items from 0 to list_len should exist; qed"); | ||
self.storage.set(&list_item_key, &last_list_item) | ||
} | ||
|
||
// decrease list length | ||
list_len -= 1; | ||
|
||
// clear entry for item in the map | ||
let map_item_key = compose_key3(self.prefix, MAP_ITEM_PREFIX, &list_item); | ||
self.storage.clear(&map_item_key); | ||
} | ||
|
||
self.storage.set(&list_len_key, &list_len.encode()); | ||
} | ||
} | ||
|
||
/// Compose key of the prefix and the key itself. | ||
fn compose_key2(prefix: &[u8], key: &[u8]) -> Vec<u8> { | ||
let mut prefixed_key = prefix.to_vec(); | ||
prefixed_key.extend(key); | ||
prefixed_key | ||
} | ||
|
||
/// Get composite key. | ||
fn compose_key3(prefix1: &[u8], prefix2: &[u8], key: &[u8]) -> Vec<u8> { | ||
let mut prefixed_key = prefix1.to_vec(); | ||
prefixed_key.extend(prefix2); | ||
prefixed_key.extend(key); | ||
prefixed_key | ||
} | ||
|
||
// This file is compiled by both susbtrate && runtime => to avoid duplicate tests execution: | ||
// see tests in prefix.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think there's an
r
missing:ext_save_prefix_keys