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

Fix clear prefix check to avoid erasing child trie roots. #7848

Merged
4 commits merged into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
23 changes: 21 additions & 2 deletions primitives/state-machine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,9 @@ where
HexDisplay::from(&prefix),
);
let _guard = guard();
if is_child_storage_key(prefix) {
warn!(target: "trie", "Refuse to directly clear prefix that is part of child storage key");

if sp_core::storage::well_known_keys::can_contain_child_storage_key(prefix) {
warn!(target: "trie", "Refuse to directly clear prefix that is part or contains of child storage key");
return;
}

Expand Down Expand Up @@ -1022,6 +1023,24 @@ mod tests {
ext.child_storage_hash(child_info, &[30]),
Some(Blake2Hasher::hash(&[31]).as_ref().to_vec()),
);

// check clear prefix is ignored on conflict
use sp_core::storage::well_known_keys;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to make this an independent test.

let mut ext = ext;
let mut not_under_prefix = well_known_keys::CHILD_STORAGE_KEY_PREFIX.to_vec();
not_under_prefix[4] = 88;
not_under_prefix.extend(b"path");
ext.set_storage(not_under_prefix.clone(), vec![10]);

ext.clear_prefix(&[]);
ext.clear_prefix(&well_known_keys::CHILD_STORAGE_KEY_PREFIX[..4]);
let mut under_prefix = well_known_keys::CHILD_STORAGE_KEY_PREFIX.to_vec();
under_prefix.extend(b"path");
ext.clear_prefix(&well_known_keys::CHILD_STORAGE_KEY_PREFIX[..4]);
assert_eq!(ext.child_storage(child_info, &[30]), Some(vec![31]));
assert_eq!(ext.storage(not_under_prefix.as_slice()), Some(vec![10]));
ext.clear_prefix(&not_under_prefix[..5]);
assert_eq!(ext.storage(not_under_prefix.as_slice()), None);
}

#[test]
Expand Down
10 changes: 10 additions & 0 deletions primitives/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ pub mod well_known_keys {
// Other code might depend on this, so be careful changing this.
key.starts_with(CHILD_STORAGE_KEY_PREFIX)
}

/// Whether a prefix potentially contains child storage keys.
pub fn can_contain_child_storage_key(prefix: &[u8]) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Whether a prefix potentially contains child storage keys.
pub fn can_contain_child_storage_key(prefix: &[u8]) -> bool {
/// Returns if the given `key` starts with [`CHILD_STORAGE_KEY_PREFIX`] or collides with it.
pub fn starts_with_child_storage_key(key: &[u8]) -> bool {

if prefix.len() > CHILD_STORAGE_KEY_PREFIX.len() {
prefix.starts_with(CHILD_STORAGE_KEY_PREFIX)
} else {
CHILD_STORAGE_KEY_PREFIX.starts_with(prefix)
}
}

}

/// Information related to a child state.
Expand Down