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

Corner cases about (child) storage #241

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
31 changes: 31 additions & 0 deletions adapters/substrate/src/host_api/child_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@ pub fn ext_default_child_storage_get_version_1(rtm: Runtime, input: ParsedInput)
ext_default_child_storage_set_version_1(rtm, input)
}

pub fn ext_default_child_storage_set_version_1_invalid_child_key(mut rtm: Runtime, input: ParsedInput) {
// Parse inputs
let child_key = input.get(0);
// Skipping index `1`
let key = input.get(2);
let value = input.get(3);

// Set NON-child key/value
let _ = rtm.call("rtm_ext_storage_set_version_1", &(key, value).encode());

// Get NON-child value
let res = rtm
.call_and_decode::<Option<Vec<u8>>>("rtm_ext_storage_get_version_1", &key.encode())
.unwrap();
assert_eq!(res, value);

// Try to get child value from NON-child key
let res = rtm.call_and_decode::<Option<Vec<u8>>>(
"rtm_ext_default_child_storage_get_version_1",
&(key, key).encode(),
);
assert!(res.is_none());

// Try to get child value from NON-child key (note that `child_key` does not exist)
let res = rtm.call_and_decode::<Option<Vec<u8>>>(
"rtm_ext_default_child_storage_get_version_1",
&(child_key, key).encode(),
);
assert!(res.is_none());
}

pub fn ext_default_child_storage_read_version_1(mut rtm: Runtime, input: ParsedInput) {
// Parse inputs
let child_key1 = input.get(0);
Expand Down
5 changes: 5 additions & 0 deletions adapters/substrate/src/host_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub fn process_subcommand(matches: &ArgMatches) {
"ext_storage_append_version_1" => storage::ext_storage_append_version_1(rtm, input),
"ext_storage_root_version_1" => storage::ext_storage_root_version_1(rtm, input),
"ext_storage_next_key_version_1" => storage::ext_storage_next_key_version_1(rtm, input),
"ext_storage_set_version_1_try_fetch_child_key" => storage::ext_storage_set_version_1_try_fetch_child_key(rtm, input),
"ext_storage_set_version_1_try_set_child_key" => storage::ext_storage_set_version_1_try_set_child_key(rtm, input),

// child storage api
"ext_default_child_storage_set_version_1" => {
Expand All @@ -87,6 +89,9 @@ pub fn process_subcommand(matches: &ArgMatches) {
"ext_default_child_storage_get_version_1" => {
child_storage::ext_default_child_storage_get_version_1(rtm, input)
}
"ext_default_child_storage_set_version_1_invalid_child_key" => {
child_storage::ext_default_child_storage_set_version_1_invalid_child_key(rtm, input)
}
"ext_default_child_storage_read_version_1" => {
child_storage::ext_default_child_storage_read_version_1(rtm, input)
}
Expand Down
67 changes: 67 additions & 0 deletions adapters/substrate/src/host_api/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,70 @@ pub fn ext_storage_next_key_version_1(mut rtm: Runtime, input: ParsedInput) {
assert!(res.is_none());
}
}

pub fn ext_storage_set_version_1_try_fetch_child_key(mut rtm: Runtime, input: ParsedInput) {
// Parse inputs
let child_key = input.get(0);
// Skipping index `1`
let key = input.get(2);
let value = input.get(3);

// Set child key/value
let _ = rtm.call(
"rtm_ext_default_child_storage_set_version_1",
&(child_key, key, value).encode(),
);

// Get child value
let res = rtm
.call_and_decode::<Option<Vec<u8>>>(
"rtm_ext_default_child_storage_get_version_1",
&(child_key, key).encode(),
)
.unwrap();
assert_eq!(res, value);

// Try to get value of child storage key itself (must fail).
let res = rtm
.call_and_decode::<Option<Vec<u8>>>("rtm_ext_storage_get_version_1", &child_key.encode());
assert!(res.is_none());

// Try to read value of child storage key itself (must fail).
let res = rtm.call_and_decode::<Option<Vec<u8>>>(
"rtm_ext_storage_read_version_1",
&(child_key, 0, value.len() as u32).encode(),
);
assert!(res.is_none());
}

pub fn ext_storage_set_version_1_try_set_child_key(mut rtm: Runtime, input: ParsedInput) {
// Parse inputs
let child_key = input.get(0);
// Skipping index `1`
let key = input.get(2);
let value = input.get(3);

// Set child key/value
let _ = rtm.call(
"rtm_ext_default_child_storage_set_version_1",
&(child_key, key, value).encode(),
);

// Set NON-child key/value with identitcal key (DROPPED)
let _ = rtm.call("rtm_ext_storage_set_version_1", &(child_key, vec![1]).encode());

// Try to get (dropped) value.
let res = rtm
.call_and_decode::<Option<Vec<u8>>>("rtm_ext_storage_get_version_1", &child_key.encode());
assert!(res.is_none());

// Get child value (must not be destroyed)
let res = rtm
.call_and_decode::<Option<Vec<u8>>>(
"rtm_ext_default_child_storage_get_version_1",
&(child_key, key).encode(),
)
.unwrap();

assert_eq!(res, value);
}
5 changes: 4 additions & 1 deletion fixtures/host-api/HostApiFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ module HostApiFunctions
const child_key_value = [
"ext_default_child_storage_set_version_1",
"ext_default_child_storage_get_version_1",
"ext_default_child_storage_set_version_1_invalid_child_key",
"ext_default_child_storage_clear_version_1",
"ext_default_child_storage_exists_version_1"
"ext_default_child_storage_exists_version_1",
"ext_storage_set_version_1_try_fetch_child_key",
"ext_storage_set_version_1_try_set_child_key"
]

const child_prefix_key_value_key_value = [
Expand Down
33 changes: 33 additions & 0 deletions fixtures/host-api/HostApiOutputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ module HostApiOutputs
"value-added\n",
"Reactive\n",
"secondary\n",
# ext_default_child_storage_set_version_1_invalid_child_key
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
# ext_default_child_storage_clear_version_1
nothing,
nothing,
Expand All @@ -211,6 +222,28 @@ module HostApiOutputs
"true\n",
"true\n",
"true\n",
# ext_storage_set_version_1_try_fetch_child_key
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
# ext_storage_set_version_1_try_set_child_key
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
nothing,
]

const child_prefix_key_value_key_value = [
Expand Down
2 changes: 1 addition & 1 deletion hosts/gossamer
Submodule gossamer updated 409 files
2 changes: 1 addition & 1 deletion hosts/kagome
2 changes: 1 addition & 1 deletion hosts/substrate
Submodule substrate updated 1131 files