mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 11:07:56 +00:00
Fix key collision for child trie (#4162)
* In progress, runtime io must switch to future proof root + child_specific (unique id) + u32 type. * Switch interface, sr-io seems ok, rpc could use similar interface to sr-io, genesis json broken if there is child trie in existing encoding genesis. * test from previous implementation. * fix proving test. * Restore Keyspacedb from other branch, only apply to child trie. * Removing unneeded child_info from child root (child info are stored if things changed, otherwhise the root does not change). * Switch rpc to use same format as ext: more future proof. * use root from child info for trie backend essence. * Breaking long lines. * Update doc and clean pr a bit. * fix error type * Restore removed doc on merge and update sr-io doc. * Switch child storage api to use directly unique id, if managed id where to be put in place, the api will change at this time. * Clean deprecated host interface from child. * Removing assertion on child info (can fail depending on root memoization). * merging child info in the overlay when possible. * child iteration by prefix using child_info. * Using ChainInfo in frame support. ChainInfo gets redesign to avoid buffers allocation on every calls. * Add length of root to the data of child info. * comments * Encode compact. * Remove child info with root. * Fix try_update condition. * Comment Ext child root caching. * Replace tuples by struct with field * remove StorageTuple alias. * Fix doc tests, and remove StorageOverlay and ChildStorageOverlay aliases.
This commit is contained in:
@@ -217,38 +217,6 @@ impl_wasm_host_interface! {
|
||||
Ok(sp_io::storage::set(&key, &value))
|
||||
}
|
||||
|
||||
ext_set_child_storage(
|
||||
storage_key_data: Pointer<u8>,
|
||||
storage_key_len: WordSize,
|
||||
key_data: Pointer<u8>,
|
||||
key_len: WordSize,
|
||||
value_data: Pointer<u8>,
|
||||
value_len: WordSize,
|
||||
) {
|
||||
let storage_key = context.read_memory(storage_key_data, storage_key_len)
|
||||
.map_err(|_| "Invalid attempt to determine storage_key in ext_set_child_storage")?;
|
||||
let key = context.read_memory(key_data, key_len)
|
||||
.map_err(|_| "Invalid attempt to determine key in ext_set_child_storage")?;
|
||||
let value = context.read_memory(value_data, value_len)
|
||||
.map_err(|_| "Invalid attempt to determine value in ext_set_child_storage")?;
|
||||
|
||||
Ok(sp_io::storage::child_set(&storage_key, &key, &value))
|
||||
}
|
||||
|
||||
ext_clear_child_storage(
|
||||
storage_key_data: Pointer<u8>,
|
||||
storage_key_len: WordSize,
|
||||
key_data: Pointer<u8>,
|
||||
key_len: WordSize,
|
||||
) {
|
||||
let storage_key = context.read_memory(storage_key_data, storage_key_len)
|
||||
.map_err(|_| "Invalid attempt to determine storage_key in ext_clear_child_storage")?;
|
||||
let key = context.read_memory(key_data, key_len)
|
||||
.map_err(|_| "Invalid attempt to determine key in ext_clear_child_storage")?;
|
||||
|
||||
Ok(sp_io::storage::child_clear(&storage_key, &key))
|
||||
}
|
||||
|
||||
ext_clear_storage(key_data: Pointer<u8>, key_len: WordSize) {
|
||||
let key = context.read_memory(key_data, key_len)
|
||||
.map_err(|_| "Invalid attempt to determine key in ext_clear_storage")?;
|
||||
@@ -261,45 +229,12 @@ impl_wasm_host_interface! {
|
||||
Ok(if sp_io::storage::exists(&key) { 1 } else { 0 })
|
||||
}
|
||||
|
||||
ext_exists_child_storage(
|
||||
storage_key_data: Pointer<u8>,
|
||||
storage_key_len: WordSize,
|
||||
key_data: Pointer<u8>,
|
||||
key_len: WordSize,
|
||||
) -> u32 {
|
||||
let storage_key = context.read_memory(storage_key_data, storage_key_len)
|
||||
.map_err(|_| "Invalid attempt to determine storage_key in ext_exists_child_storage")?;
|
||||
let key = context.read_memory(key_data, key_len)
|
||||
.map_err(|_| "Invalid attempt to determine key in ext_exists_child_storage")?;
|
||||
|
||||
Ok(if sp_io::storage::child_exists(&storage_key, &key) { 1 } else { 0 })
|
||||
}
|
||||
|
||||
ext_clear_prefix(prefix_data: Pointer<u8>, prefix_len: WordSize) {
|
||||
let prefix = context.read_memory(prefix_data, prefix_len)
|
||||
.map_err(|_| "Invalid attempt to determine prefix in ext_clear_prefix")?;
|
||||
Ok(sp_io::storage::clear_prefix(&prefix))
|
||||
}
|
||||
|
||||
ext_clear_child_prefix(
|
||||
storage_key_data: Pointer<u8>,
|
||||
storage_key_len: WordSize,
|
||||
prefix_data: Pointer<u8>,
|
||||
prefix_len: WordSize,
|
||||
) {
|
||||
let storage_key = context.read_memory(storage_key_data, storage_key_len)
|
||||
.map_err(|_| "Invalid attempt to determine storage_key in ext_clear_child_prefix")?;
|
||||
let prefix = context.read_memory(prefix_data, prefix_len)
|
||||
.map_err(|_| "Invalid attempt to determine prefix in ext_clear_child_prefix")?;
|
||||
Ok(sp_io::storage::child_clear_prefix(&storage_key, &prefix))
|
||||
}
|
||||
|
||||
ext_kill_child_storage(storage_key_data: Pointer<u8>, storage_key_len: WordSize) {
|
||||
let storage_key = context.read_memory(storage_key_data, storage_key_len)
|
||||
.map_err(|_| "Invalid attempt to determine storage_key in ext_kill_child_storage")?;
|
||||
Ok(sp_io::storage::child_storage_kill(&storage_key))
|
||||
}
|
||||
|
||||
ext_get_allocated_storage(
|
||||
key_data: Pointer<u8>,
|
||||
key_len: WordSize,
|
||||
@@ -322,32 +257,6 @@ impl_wasm_host_interface! {
|
||||
}
|
||||
}
|
||||
|
||||
ext_get_allocated_child_storage(
|
||||
storage_key_data: Pointer<u8>,
|
||||
storage_key_len: WordSize,
|
||||
key_data: Pointer<u8>,
|
||||
key_len: WordSize,
|
||||
written_out: Pointer<u32>,
|
||||
) -> Pointer<u8> {
|
||||
let storage_key = context.read_memory(storage_key_data, storage_key_len)
|
||||
.map_err(|_| "Invalid attempt to determine storage_key in ext_get_allocated_child_storage")?;
|
||||
let key = context.read_memory(key_data, key_len)
|
||||
.map_err(|_| "Invalid attempt to determine key in ext_get_allocated_child_storage")?;
|
||||
|
||||
if let Some(value) = sp_io::storage::child_get(&storage_key, &key) {
|
||||
let offset = context.allocate_memory(value.len() as u32)?;
|
||||
context.write_memory(offset, &value)
|
||||
.map_err(|_| "Invalid attempt to set memory in ext_get_allocated_child_storage")?;
|
||||
context.write_primitive(written_out, value.len() as u32)
|
||||
.map_err(|_| "Invalid attempt to write written_out in ext_get_allocated_child_storage")?;
|
||||
Ok(offset)
|
||||
} else {
|
||||
context.write_primitive(written_out, u32::max_value())
|
||||
.map_err(|_| "Invalid attempt to write failed written_out in ext_get_allocated_child_storage")?;
|
||||
Ok(Pointer::null())
|
||||
}
|
||||
}
|
||||
|
||||
ext_get_storage_into(
|
||||
key_data: Pointer<u8>,
|
||||
key_len: WordSize,
|
||||
@@ -369,53 +278,11 @@ impl_wasm_host_interface! {
|
||||
}
|
||||
}
|
||||
|
||||
ext_get_child_storage_into(
|
||||
storage_key_data: Pointer<u8>,
|
||||
storage_key_len: WordSize,
|
||||
key_data: Pointer<u8>,
|
||||
key_len: WordSize,
|
||||
value_data: Pointer<u8>,
|
||||
value_len: WordSize,
|
||||
value_offset: WordSize,
|
||||
) -> WordSize {
|
||||
let storage_key = context.read_memory(storage_key_data, storage_key_len)
|
||||
.map_err(|_| "Invalid attempt to determine storage_key in ext_get_child_storage_into")?;
|
||||
let key = context.read_memory(key_data, key_len)
|
||||
.map_err(|_| "Invalid attempt to get key in ext_get_child_storage_into")?;
|
||||
|
||||
if let Some(value) = sp_io::storage::child_get(&storage_key, &key) {
|
||||
let data = &value[value.len().min(value_offset as usize)..];
|
||||
let written = std::cmp::min(value_len as usize, data.len());
|
||||
context.write_memory(value_data, &data[..written])
|
||||
.map_err(|_| "Invalid attempt to get value in ext_get_child_storage_into")?;
|
||||
Ok(value.len() as u32)
|
||||
} else {
|
||||
Ok(u32::max_value())
|
||||
}
|
||||
}
|
||||
|
||||
ext_storage_root(result: Pointer<u8>) {
|
||||
context.write_memory(result, sp_io::storage::root().as_ref())
|
||||
.map_err(|_| "Invalid attempt to set memory in ext_storage_root".into())
|
||||
}
|
||||
|
||||
ext_child_storage_root(
|
||||
storage_key_data: Pointer<u8>,
|
||||
storage_key_len: WordSize,
|
||||
written_out: Pointer<u32>,
|
||||
) -> Pointer<u8> {
|
||||
let storage_key = context.read_memory(storage_key_data, storage_key_len)
|
||||
.map_err(|_| "Invalid attempt to determine storage_key in ext_child_storage_root")?;
|
||||
let value = sp_io::storage::child_root(&storage_key);
|
||||
|
||||
let offset = context.allocate_memory(value.len() as u32)?;
|
||||
context.write_memory(offset, &value)
|
||||
.map_err(|_| "Invalid attempt to set memory in ext_child_storage_root")?;
|
||||
context.write_primitive(written_out, value.len() as u32)
|
||||
.map_err(|_| "Invalid attempt to write written_out in ext_child_storage_root")?;
|
||||
Ok(offset)
|
||||
}
|
||||
|
||||
ext_storage_changes_root(
|
||||
parent_hash_data: Pointer<u8>,
|
||||
_len: WordSize,
|
||||
|
||||
@@ -128,11 +128,14 @@ fn storage_should_work(wasm_method: WasmExecutionMethod) {
|
||||
assert_eq!(output, b"all ok!".to_vec().encode());
|
||||
}
|
||||
|
||||
let expected = TestExternalities::new((map![
|
||||
let expected = TestExternalities::new(primitives::storage::Storage {
|
||||
top: map![
|
||||
b"input".to_vec() => b"Hello world".to_vec(),
|
||||
b"foo".to_vec() => b"bar".to_vec(),
|
||||
b"baz".to_vec() => b"bar".to_vec()
|
||||
], map![]));
|
||||
],
|
||||
children: map![],
|
||||
});
|
||||
assert_eq!(ext, expected);
|
||||
}
|
||||
|
||||
@@ -162,11 +165,14 @@ fn clear_prefix_should_work(wasm_method: WasmExecutionMethod) {
|
||||
assert_eq!(output, b"all ok!".to_vec().encode());
|
||||
}
|
||||
|
||||
let expected = TestExternalities::new((map![
|
||||
let expected = TestExternalities::new(primitives::storage::Storage {
|
||||
top: map![
|
||||
b"aaa".to_vec() => b"1".to_vec(),
|
||||
b"aab".to_vec() => b"2".to_vec(),
|
||||
b"bbb".to_vec() => b"5".to_vec()
|
||||
], map![]));
|
||||
],
|
||||
children: map![],
|
||||
});
|
||||
assert_eq!(expected, ext);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user