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

avoid key collision on child trie and proof on child trie #2209

Closed
wants to merge 142 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
142 commits
Select commit Hold shift + click to select a range
f6c4bb2
Straight forward move of trie related only change from child-trie-soft
cheme Mar 26, 2019
878a7ae
merge backend stuff from old branch, add insertion of child trie root in
cheme Mar 26, 2019
a464936
commit before wasm_executor change (we will keep old interface for now:
cheme Mar 26, 2019
4de73d7
tabify
cheme Mar 26, 2019
be0e340
Things compile (account change is unoptimized), not for wasm.
cheme Mar 26, 2019
7f3a282
Make SutrieField non public
cheme Mar 27, 2019
4d208db
Compile no std child trie with subtrie query (super costy: do we trust
cheme Mar 27, 2019
6d7f355
Make the single child trie test pass.
cheme Mar 27, 2019
25bcb4c
Proving synch child trie content (TODO it requires deletion tests).
cheme Mar 28, 2019
9eaef35
restore test
cheme Apr 1, 2019
37d9536
Merge branch 'master' into child-trie-soft-min
cheme Apr 1, 2019
67c03a2
Fix test error (wrong vec alloc).
cheme Apr 2, 2019
99dbb5a
tests for no key collision
cheme Apr 3, 2019
3be1802
Merge branch 'master' into child-trie-soft-min
cheme Apr 3, 2019
fec73d0
Revert `storage_root` using child, create a variant for it: see
cheme Apr 4, 2019
20d168d
indentation and remove comment
cheme Apr 4, 2019
d797bd0
Remove some TODOs, fix compile error
cheme Apr 4, 2019
27096aa
plaindb does not need to be keyspaceddb
cheme Apr 4, 2019
3560acc
Merge branch 'master' into child-trie-soft-min
cheme Apr 5, 2019
c3fc432
bump impl_version
cheme Apr 8, 2019
3e417eb
Merge branch 'master' into child-trie-soft-min
cheme Apr 16, 2019
5af490f
fix and 2299
cheme Apr 16, 2019
5a8dddd
Merge branch 'master' into child-trie-soft-min
cheme Apr 16, 2019
8f04f00
Merge branch 'master' into child-trie-soft-min
cheme Apr 26, 2019
8051179
fix keyspace insert and start proof (missing in higher level test case a
cheme Apr 29, 2019
8eac118
remove todo of test
cheme Apr 30, 2019
a65f9f2
Merge branch 'master' into child-trie-soft-min
cheme Apr 30, 2019
d495ef5
Merge branch 'master' into child-trie-soft-min &
cheme May 14, 2019
197d77a
- refactor api, use noderef of read only query (require and additional
cheme May 15, 2019
2ea3c89
Remove SubTrieNode struct
cheme May 16, 2019
99c45ea
put extension in encoding/decoding properly
cheme May 16, 2019
3553ab2
SubTrie struct renaming (and single encode point)
cheme May 16, 2019
47984fe
remove contract prefix, and make use of a child trie prefix.
cheme May 16, 2019
2bf2d7b
fix for overlay before reverting
cheme May 16, 2019
bc7165c
fix2 for prefix in overlay
cheme May 16, 2019
21c3acf
revert (will probably need two variant, but for rpc pr)
cheme May 16, 2019
98b2fc3
break some lines
cheme May 16, 2019
432cb10
Merge branch 'master' into child-trie-soft-min
cheme May 16, 2019
e266dfe
Add module specific seed to generator (accountid is undefined)
cheme May 16, 2019
7f64652
Update core/primitives/src/subtrie.rs
cheme May 17, 2019
999a26e
Merge branch 'master' into child-trie-soft-min and adapt rpc test
cheme May 21, 2019
7bbd681
Merge branch 'child-trie-soft-min' of github.com:cheme/substrate into…
cheme May 21, 2019
87f03b7
child trie additional prefix is bad design for primitive, removing it.
cheme May 21, 2019
f18e002
safer encoding of version.
cheme May 24, 2019
0eaeca0
Merge branch 'master' into child-trie-soft-min (untested)
cheme May 24, 2019
b57319d
missing import
cheme May 24, 2019
1f848d2
bump impl version
cheme May 24, 2019
403df51
Merge branch 'master' into child-trie-soft-min
cheme May 24, 2019
305b60a
Merge branch 'master' into child-trie-soft-min
cheme May 28, 2019
423cfb1
correct version
cheme May 28, 2019
a0ffa31
fix typing
cheme May 28, 2019
6e3bed7
Merge branch 'master' into child-trie-soft-min
cheme May 29, 2019
7411146
misnamed storage key variables.
cheme May 29, 2019
331be51
fix error messages.
cheme May 29, 2019
bc2935c
remove convenience constructor, remove unneeded pub on field.
cheme May 29, 2019
5a87b6a
Merge branch 'master' into child-trie-soft-min
cheme May 29, 2019
f40400b
minor tweaks before reworking docs
cheme May 30, 2019
9bc1ab7
First pass of documentation improvement.
cheme May 30, 2019
c073b21
do not refer to parent_key when it is ParentTrie:
cheme May 30, 2019
d15ca49
child trie renaming did exceed 100 character line.
cheme May 30, 2019
0736b96
Link github issue for TODOs
cheme May 31, 2019
9e0485d
Merge branch 'master' into child-trie-soft-min
cheme May 31, 2019
586b50e
comment incorrect variable
cheme May 31, 2019
65d7485
Merge branch 'master' into child-trie-soft-min
cheme Jun 6, 2019
95a69b2
Makes child trie instantiation a bisschen more explicit.
cheme Jun 6, 2019
d089693
Merge branch 'master' into child-trie-soft-min
cheme Jun 12, 2019
ec69ae0
rename fetch and init trie function.
cheme Jun 12, 2019
76ea14d
moved_child is a bad idea, just delete in top, if it is reinserted this
cheme Jun 14, 2019
b2050c8
moved map is correct (fix usage of child trie to return option<option<)
cheme Jun 14, 2019
5a0cbe1
Add missing wasm boilerplate.
cheme Jun 14, 2019
6e84810
remove moved_ct from overlay in favor to optional in pending.
cheme Jun 14, 2019
7f5694f
Merge branch 'master' into child-trie-soft-min
cheme Jun 14, 2019
5006d73
Use keyspace for storage_cache.
cheme Jun 17, 2019
b85508e
Comment of unsafety of `set_child` if directly exposed.
cheme Jun 17, 2019
0c14777
Merge branch 'master' into child-trie-soft-min
cheme Jun 17, 2019
10c4f58
use named fields.
cheme Jun 17, 2019
453927b
Merge branch 'master' into child-trie-soft-min
cheme Jun 21, 2019
349f9a5
Merge branch 'master' into child-trie-soft-min
cheme Jun 25, 2019
fc034fb
Refacto of keyspace to use static creation unique info.
cheme Jun 27, 2019
45cfbd6
put child trie content under their own prefix (with common root with
cheme Jun 28, 2019
4f9717e
Revert to storing keyspace in state approach.
cheme Jun 28, 2019
7cb2d84
invalid asumption for `child_delta_trie_root`.
cheme Jun 28, 2019
c821a08
clean unused child storage key relative methods.
cheme Jun 28, 2019
c3ba830
Merge branch 'master' into child-trie-soft-min and
cheme Jun 28, 2019
acf9641
invalid comment
cheme Jun 28, 2019
0050457
indent and remove deprecated comments/struct
cheme Jun 28, 2019
c36b91b
Make top trie write through keyspace db, this is not going fine because
cheme Jul 2, 2019
9d3d9e2
Fix issue: prefixed_key already got the keyspace in prefix.
cheme Jul 2, 2019
3db4da8
Merge branch 'master' into child-trie-soft-min
cheme Jul 2, 2019
4348d70
Address review comment:
cheme Jul 4, 2019
134a4bf
Merge branch 'master' into child-trie-soft-min
cheme Jul 4, 2019
5325621
Restore auth version.
cheme Jul 4, 2019
97118e8
Avoid confusing Option<Option< in result return of overlay.
cheme Jul 4, 2019
0ed7f80
Missing fix from review on testing ext for set child trie (code from
cheme Jul 4, 2019
8a6986a
Use fields for change trie storage.
cheme Jul 4, 2019
7652de9
switch MapTransaction to a struct.
cheme Jul 4, 2019
94c629e
Merge branch 'master' into child-trie-soft-min
cheme Jul 4, 2019
7675740
Apply same encoding for parent root (compatible with implementation of a
cheme Jul 1, 2019
90fba8a
Fix previous cherry pick
cheme Jul 4, 2019
d8c58c6
Addressing nits.
cheme Jul 4, 2019
661ba2e
Avoid unused creation of child trie.
cheme Jul 4, 2019
833e9ff
Create child trie id from additional storage state.
cheme Jul 5, 2019
71bda12
update default encoding.
cheme Jul 5, 2019
08b3062
Handle possible switch to multiple trie layer.
cheme Jul 5, 2019
89f3cd5
Remove a redundancy on keyspaceddb instantiation.
cheme Jul 5, 2019
834f52a
actually remove redundancy correctly
cheme Jul 5, 2019
0445228
comment
cheme Jul 5, 2019
75add99
Merge branch 'child-trie-soft-min-old' into child-trie-soft-min
cheme Jul 5, 2019
e70edab
Merge branch 'master' into child-trie-soft-min and apply review
cheme Jul 9, 2019
b8a0cd4
Merge branch 'master' into child-trie-soft-min
cheme Jul 10, 2019
1514171
Merge branch 'master' into child-trie-soft-min
cheme Jul 10, 2019
a7da811
Fix new test cases.
cheme Jul 10, 2019
b96c523
Merge branch 'master' into child-trie-soft-min
cheme Jul 11, 2019
865672d
Merge branch 'master' into child-trie-soft-min
cheme Jul 12, 2019
b6d7705
bump runtime version
cheme Jul 12, 2019
b45344c
Merge branch 'master' into child-trie-soft-min
cheme Jul 18, 2019
517f95c
Merge branch 'master' into child-trie-soft-min
cheme Jul 31, 2019
79a07de
Merge branch 'master' into child-trie-soft-min
cheme Aug 2, 2019
a027fb0
Some doc enhancement.
cheme Aug 2, 2019
b92655c
ChildRead correctly defined (previous definition was wrong).
cheme Aug 2, 2019
8a875f4
child_trie 'is_updatable_with' to avoid already broken redundancy.
cheme Aug 2, 2019
fcd8bdb
fix system bench error.
cheme Aug 2, 2019
3241fc4
Fix client doc rust code.
cheme Aug 2, 2019
ecac03b
Merge branch 'master' into child-trie-soft-min
cheme Aug 2, 2019
10369b9
Merge branch 'master' into child-trie-soft-min
cheme Aug 7, 2019
15a2967
Merge branch 'master' into child-trie-soft-min
cheme Aug 8, 2019
acc94e4
Merge branch 'master' into child-trie-soft-min
cheme Aug 8, 2019
7d96338
Remove unused method (in favor of assimilate).
cheme Aug 8, 2019
ba7bdcb
Merge branch 'child-trie-soft-min' of github.com:cheme/polkadot into …
cheme Aug 14, 2019
aacea85
Merge branch 'master' into child-trie-soft-min
cheme Aug 14, 2019
6789641
merge fix.
cheme Aug 14, 2019
2b37160
Rename MapTransaction to StorageContent.
cheme Aug 14, 2019
b1183bd
Get subscription stuff from child-deletion branch:
cheme Aug 14, 2019
bc5653c
Merge branch 'master' into child-trie-soft-min
cheme Aug 22, 2019
5ecfd19
update to master.
cheme Aug 22, 2019
baf89c8
Cast explicitelly for readability.
cheme Aug 26, 2019
0f8bff9
bump spec version.
cheme Aug 26, 2019
5a8576b
Merge branch 'master' into child-trie-soft-min
cheme Aug 29, 2019
045ee32
Merge branch 'master' into child-trie-soft-min
cheme Aug 29, 2019
58e6e41
Fix compilation.
cheme Aug 29, 2019
70555c5
Merge branch 'master' into child-trie-soft-min with conflicts.
cheme Sep 5, 2019
130e5e4
build passing, regression on extrinsics for a set_child (see new TODO).
cheme Sep 6, 2019
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
Prev Previous commit
Next Next commit
moved_child is a bad idea, just delete in top, if it is reinserted this
will be handle by full_storage order.
  • Loading branch information
cheme committed Jun 14, 2019
commit 76ea14dae740263027cb94f8b6e9a096ead02923
82 changes: 78 additions & 4 deletions core/primitives/src/child_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use parity_codec::{Encode, Decode};
use rstd::prelude::*;
use rstd::ptr;
use crate::storage::well_known_keys::CHILD_STORAGE_KEY_PREFIX;
#[cfg(feature = "std")]
pub use impl_serde::serialize as bytes;
Expand Down Expand Up @@ -190,19 +191,23 @@ impl ChildTrie {
///
/// This can be quite unsafe for user, so use with care (write new trie information
/// as soon as possible).
Copy link
Contributor

Choose a reason for hiding this comment

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

in which context ? because you can create multiple ones in case you give wrong entry ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

context is mainly the window of time where you create the child trie at a parent key but it is not updated (calling this function again will create another child trie instance at the same parent key which is not suitable).

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes ok I feel it is better that old new function as old new function would have been worse:

like you could do a new without fetching the current trie and then write on it and you would have two child trie with the same parent.

pub fn fetch_or_new_pending(
pub fn fetch_or_new(
keyspace_builder: &mut impl KeySpaceGenerator,
parent_fetcher: impl FnOnce(&[u8]) -> Option<Self>,
Copy link
Contributor

Choose a reason for hiding this comment

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

this is for incoming multiple level trie ? because otherwise the only function to put here is runtime_io::child_trie no ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really, it is just to keep things generic (but yes it should be runtime_io::child_trie that is call for general srml module).

Copy link
Contributor

Choose a reason for hiding this comment

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

Then maybe I would put a new method in externalities to have fetch_or_new without this argument ?
and maybe rename to something fetch_or_new_pending because it doesn't create a new child trie until you actually write on it.

Copy link
Contributor

Choose a reason for hiding this comment

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

if empty child trie can't exist then we can't do better than that I think

child_trie_update: impl FnOnce(ChildTrie),
parent: &[u8],
) -> Self {
parent_fetcher(parent).unwrap_or_else(|| {
parent_fetcher(parent)
.unwrap_or_else(|| {
let parent = Self::prefix_parent_key(parent);
ChildTrie {
let ct = ChildTrie {
keyspace: keyspace_builder.generate_keyspace(),
root: Default::default(),
parent,
extension: Default::default(),
}
};
child_trie_update(ct.clone());
ct
})
}
/// Get a reference to the child trie information
Expand Down Expand Up @@ -262,8 +267,77 @@ impl ChildTrie {
enc.extend_from_slice(&self.extension[..]);
enc
}

/// Function to send child trie without relying on
/// contiguous memory.
pub fn unsafe_ptr_child_trie(&self) -> PtrChildTrie {
(
self.keyspace.as_ptr(),
self.keyspace.len() as u32,
self.root.as_ref().map(|r| r.as_ptr()).unwrap_or(ptr::null()),
self.root.as_ref().map(|r| r.len() as u32).unwrap_or(u32::max_value()),
self.parent.as_ptr(),
self.parent.len() as u32,
self.extension.as_ptr(),
self.extension.len() as u32,
)
}
/// Function to rebuild child trie accessed from
pub fn unsafe_from_ptr_child_trie(pct: PtrChildTrieMut) -> Self {
let (
keyspace,
kl,
root,
rl,
parent,
pl,
extension,
el,
) = pct;
unsafe {
let keyspace = from_raw_parts(keyspace, kl).expect("non optional; qed");
let root = from_raw_parts(root, rl);
let parent = from_raw_parts(parent, pl).expect("non optional; qed");
let extension = from_raw_parts(extension, el).expect("non optional; qed");
ChildTrie { keyspace, root, parent, extension }
}
}
}

// this is redundant with runtime io without_std TODO EMCH move to some util crate
unsafe fn from_raw_parts(ptr: *mut u8, len: u32) -> Option<Vec<u8>> {
if len == u32::max_value() {
None
} else {
Some(<Vec<u8>>::from_raw_parts(ptr, len as usize, len as usize))
}
}

/// Pointers repersentation of ChildTrie
type PtrChildTrie = (
*const u8,
u32,
*const u8,
u32,
*const u8,
u32,
*const u8,
u32,
);

/// Mut Pointers repersentation of ChildTrie
type PtrChildTrieMut = (
*mut u8,
u32,
*mut u8,
u32,
*mut u8,
u32,
*mut u8,
u32,
);


impl AsRef<ChildTrie> for ChildTrie {
fn as_ref(&self) -> &ChildTrie {
self
Expand Down
5 changes: 5 additions & 0 deletions core/sr-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ export_api! {
/// Get child trie for a given `storage_key` location, or `None` if undefined.
fn child_trie(storage_key: &[u8]) -> Option<ChildTrie>;

/// Update or create an existing child trie.
/// Return false if it could not be updated (eg direct change
/// of root is not allowed).
fn set_child_trie(ct: ChildTrie) -> bool;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be replaced with a safer semantics? Like init_child_trie(storage_key: &[u8]) which returns a new unique keyspace (instead of it being passed in) and rejects if the parent trie is not empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't it the same thing as what we do with fetch_or_new method?
I need at some point a method to update child trie extension content, this was reducing the number of methods over the wasm boundary, but yes it can be split between creation and update case.

Copy link
Contributor

Choose a reason for hiding this comment

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

The main suggestion here is letting this call generate the keyspace for newly initialized child tries.

The function could just then be set_child_trie(storage_key: &[u8], extension: &[u8]) -> ChildTrie, which fetches or initializes a child trie at the storage key (parent path) and sets its extension data. It might be able to replace fetch_or_new entirely.


/// Get `key` from child storage, placing the value into `value_out` (as much of it as possible) and return
/// the number of bytes that the entry in storage had beyond the offset or None if the storage entry
/// doesn't exist at all. Note that if the buffer is smaller than the storage entry length, the returned
Expand Down
5 changes: 5 additions & 0 deletions core/sr-io/with_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ impl StorageApi for () {
.expect("storage cannot be called outside of an Externalities-provided environment.")
}

fn set_child_trie(ct: ChildTrie) -> bool {
ext::with(|ext| ext.set_child_trie(ct))
.expect("storage cannot be called outside of an Externalities-provided environment.")
}

fn child_storage(child_trie: ChildTrieReadRef, key: &[u8]) -> Option<Vec<u8>> {
ext::with(|ext| {
ext.child_storage(child_trie, key).map(|s| s.to_vec())
Expand Down
68 changes: 62 additions & 6 deletions core/sr-io/without_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ pub use rstd;
pub use rstd::{mem, slice};

use core::{intrinsics, panic::PanicInfo};
use rstd::{vec::Vec, cell::Cell, convert::TryInto};
use primitives::{offchain, Blake2Hasher, child_trie::{ChildTrie, ChildTrieReadRef}};
use rstd::{vec::Vec, cell::Cell, convert::TryInto, ptr};
use primitives::{offchain, Blake2Hasher,
child_trie::{ChildTrie, ChildTrieReadRef}};

#[cfg(not(feature = "no_panic_handler"))]
#[panic_handler]
Expand Down Expand Up @@ -201,6 +202,30 @@ pub mod ext {

/// Set value for key in storage.
fn ext_set_storage(key_data: *const u8, key_len: u32, value_data: *const u8, value_len: u32);
/// Get child trie at a storage location.
fn ext_get_child_trie(
storage_key_data: *const u8,
storage_key_len: u32,
a: *mut *mut u8,
b: *mut u32,
c: *mut *mut u8,
d: *mut u32,
e: *mut *mut u8,
f: *mut u32,
g: *mut *mut u8,
h: *mut u32
) -> bool;
/// Set child trie return false if there is an attempt to change non empty root.
fn ext_set_child_trie(
a: *const u8,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Use descriptive variable names.

b: u32,
c: *const u8,
d: u32,
e: *const u8,
f: u32,
g: *const u8,
h: u32
) -> bool;
/// Remove key and value from storage.
fn ext_clear_storage(key_data: *const u8, key_len: u32);
/// Checks if the given key exists in the storage.
Expand Down Expand Up @@ -584,12 +609,43 @@ impl StorageApi for () {

/// Get child trie at storage key location.
fn child_trie(storage_key: &[u8]) -> Option<ChildTrie> {
let prefixed_key = ChildTrie::prefix_parent_key(storage_key);
let prefixed_key_cat = ChildTrie::parent_key_slice(&prefixed_key);
storage(prefixed_key_cat)
.and_then(|enc_node| ChildTrie::decode_node_with_parent(&enc_node, prefixed_key))
let mut a = ptr::null_mut();
let mut b = 0u32;
let mut c = ptr::null_mut();
let mut d = 0u32;
let mut e = ptr::null_mut();
let mut f = 0u32;
let mut g = ptr::null_mut();
let mut h = 0u32;
unsafe {
if ext_get_child_trie.get()(
storage_key.as_ptr(),
storage_key.len() as u32,
&mut a as *mut _,
&mut b,
&mut c as *mut _,
&mut d,
&mut e as *mut _,
&mut f,
&mut g as *mut _,
&mut h,
) {
Some(ChildTrie::unsafe_from_ptr_child_trie((a, b, c, d, e, f, g, h)))
} else {
None
}
}
}

/// Set child trie. Can fail and return false (eg change of root).
fn set_child_trie(ct: ChildTrie) -> bool {
unsafe {
let p = ct.unsafe_ptr_child_trie();
ext_set_child_trie.get()(p.0, p.1, p.2, p.3, p.4, p.5, p.6, p.7)
}
}


fn child_storage(child_trie: ChildTrieReadRef, key: &[u8]) -> Option<Vec<u8>> {
let mut length: u32 = 0;
let empty_byte: [u8;0] = [];
Expand Down
4 changes: 4 additions & 0 deletions core/state-machine/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ impl<H: Hasher> Externalities<H> for BasicExternalities where H::Out: Ord {
unreachable!("basic not used for child trie");
}

fn set_child_trie(&mut self, _ct: ChildTrie) -> bool {
unreachable!("basic not used for child trie");
}

fn clear_prefix(&mut self, prefix: &[u8]) {
self.changes.clear_prefix(prefix);
self.inner.retain(|key, _| !key.starts_with(prefix));
Expand Down
21 changes: 21 additions & 0 deletions core/state-machine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,27 @@ where
self.overlay.set_child_storage(child_trie, key, value);
}

fn set_child_trie(&mut self, ct: ChildTrie) -> bool {
let _guard = panic_handler::AbortGuard::new(true);
// do check for backend
let ct = match self.child_trie(ct.parent_trie().as_ref()) {
Some(ct_old) => if
(ct_old.root_initial_value() != ct.root_initial_value()
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this. If the ChildTrie is new, then it can overwrite an existing one with a different root, but if it is not new (presumably an update) then the root cannot have changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Root should never be change directly like this. 👍

&& !ct.is_new()) ||
ct_old.keyspace() != ct.keyspace() {
return false;
} else {
ct
},
None => if ct.is_new() {
ct
} else {
return false;
},
};
self.overlay.set_child_trie(ct)
}

fn kill_child_storage(&mut self, child_trie: &ChildTrie) {
let _guard = panic_handler::AbortGuard::new(true);

Expand Down
13 changes: 9 additions & 4 deletions core/state-machine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,18 @@ pub trait Externalities<H: Hasher> {
/// Read child runtime storage.
fn child_storage(&self, child_trie: ChildTrieReadRef, key: &[u8]) -> Option<Vec<u8>>;

/// get child trie infos at storage_key
/// Get child trie infos at 'storage_key'.
fn child_trie(&self, storage_key: &[u8]) -> Option<ChildTrie>;

/// Set storage entry `key` of current contract being called (effective immediately).
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
self.place_storage(key, Some(value));
}

/// Set child trie infos, can fail if there is an attempt to change a
/// non empty child root directly.
fn set_child_trie(&mut self, ct: ChildTrie) -> bool;

/// Set child storage entry `key` of current contract being called (effective immediately).
fn set_child_storage(&mut self, child_trie: &ChildTrie, key: Vec<u8>, value: Vec<u8>) {
self.place_child_storage(child_trie, key, Some(value))
Expand Down Expand Up @@ -1179,9 +1183,10 @@ mod tests {
);

assert_eq!(ext.child_trie(&b"testchild"[..]), None);
let child_trie = ChildTrie::fetch_or_new_pending(
let child_trie = ChildTrie::fetch_or_new(
&mut TestKeySpaceGenerator::new(),
|_| None,
|_| (),
b"testchild",
);
ext.set_child_storage(&child_trie, b"abc".to_vec(), b"def".to_vec());
Expand Down Expand Up @@ -1293,8 +1298,8 @@ mod tests {
use std::collections::HashSet;

let mut ks_gen = TestKeySpaceGenerator::new();
let child_trie1 = ChildTrie::fetch_or_new_pending(&mut ks_gen, |_| None, &[0x01]);
let child_trie2 = ChildTrie::fetch_or_new_pending(&mut ks_gen, |_| None, &[0x23]);
let child_trie1 = ChildTrie::fetch_or_new(&mut ks_gen, |_| None, |_| (), &[0x01]);
let child_trie2 = ChildTrie::fetch_or_new(&mut ks_gen, |_| None, |_| (), &[0x23]);
let mut tr1 = {
let mut ttrie = test_trie();
let backend = ttrie.as_trie_backend().unwrap();
Expand Down
Loading