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

Adds first version of validate_block #3

Merged
merged 13 commits into from
Apr 17, 2019
Merged

Conversation

bkchr
Copy link
Member

@bkchr bkchr commented Mar 7, 2019

@bkchr bkchr requested a review from rphmeier March 7, 2019 13:12
@paritytech paritytech deleted a comment from parity-cla-bot Mar 7, 2019
runtime/src/lib.rs Outdated Show resolved Hide resolved
runtime/src/lib.rs Outdated Show resolved Hide resolved
type HashOf<B> = <B as BlockT>::Hash;

/// Abstract the storage into a trait without `Block` generic.
trait StorageT {
Copy link
Contributor

Choose a reason for hiding this comment

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

a bit weird to call it StorageT. why not just Storage?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because I have a struct that is called Storage in the same module 😅

self.overlay.get(key).cloned().or_else(|| {
read_trie_value(
&self.witness_data,
&self.storage_root, key
Copy link
Contributor

Choose a reason for hiding this comment

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

key should go on new line.

}
}

unsafe fn ext_set_storage(
Copy link
Contributor

Choose a reason for hiding this comment

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

i would prefer to extract these to a submodule where we can be a bit more sure about invariants

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, this is already its own module and you are sure about the invariants. This function is only usable by validate_block. I would not put this into another file.


use hash_db::HashDB;

static mut STORAGE: Option<Box<StorageT>> = None;
Copy link
Contributor

Choose a reason for hiding this comment

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

eep. still not a fan of the static mut. Why not at least a RefCell?

Copy link
Contributor

Choose a reason for hiding this comment

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

TBH I see no reason why, with native-runtime optimization, this is guaranteed not to be used from multiple threads

Copy link
Member Author

Choose a reason for hiding this comment

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

This will never be callable by the native side. First we don't support replacing the functions on native, second register_validate_block! resolves to nothing on native.

struct Storage<B: BlockT> {
witness_data: MemoryDB<<HashingOf<B> as HashT>::Hasher>,
overlay: hashbrown::HashMap<Vec<u8>, Option<Vec<u8>>>,
storage_root: HashOf<B>,
Copy link
Contributor

Choose a reason for hiding this comment

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

B::Hash is easier

storage_root: HashOf<B>
) -> Result<Self, &'static str> {
let mut db = MemoryDB::default();
data.into_iter().for_each(|i| { db.insert(&[], &i); });
Copy link
Contributor

Choose a reason for hiding this comment

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

what does the &[] mean here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't really know. I copied this code from substrate. I think that &[] is the prefix. Maybe @cheme can help us here?

Copy link
Contributor

Choose a reason for hiding this comment

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

That is the prefix for the MemoryDB, I believe here if MemoryDB is used from substrate trie crate, there is no key transformation with the prefix so it is safe to put nothing (a variant called PrefixedMemoryDB does insert the prefix before the key when inserting/querying the keyvaluedb to avoid collision in trie with unhashed value).

Copy link
Contributor

Choose a reason for hiding this comment

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

hm, maybe we need this when using a chain with child tries

Copy link
Contributor

@cheme cheme Apr 15, 2019

Choose a reason for hiding this comment

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

there is the key conflict with child trie, but this way of doing things cannot solve it directly (for two identical child trie there is the sametrie related prefix). I was also wondering about exposing this key transformation trait to insert a child trie specific prefix with a specialized implementation (paritytech/trie#12 last comment), but using a hashdb specific implementation would achieve the same thing.

Copy link
Member Author

Choose a reason for hiding this comment

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

I assume that we then need to adapt this? This is basically the functionality I use to generate the proof. I assume we would need to extend the proof with the prefix somehow?

Copy link
Contributor

Choose a reason for hiding this comment

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

If you use child trie I think you need paritytech/substrate#2209 (I should split it in two has I believe the api change would not be merged or at least requires some consensus but the proof side of the pr seems ok).
But otherwhise the prefix thing should only apply to avoid reference count in db, so the Recorder of trie does not use the prefix so it should not matter (that is the good thing about the way Arkadiy solves the issue).

Some(mut value) => {
*written_out = value.len() as u32;
let ptr = value.as_mut_ptr();
mem::forget(ptr);
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the purpose of forgetting ptr (which is copy) and then using it right after?

Copy link
Member Author

Choose a reason for hiding this comment

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

Was incorrect and is fixed now.

"substrate-trie/std",
]
wasm = [
"hashbrown/nightly",
Copy link
Contributor

Choose a reason for hiding this comment

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

is this wasm feature something native or something that require some feature declaration?
(I remember that I wanted to use hashbrown but use hasmap_core in trie because it felt easier to manage with 'std' substrate feature, just thinking let's wait for hashbrown to be std and see if at some point it makes it to alloc).

Copy link
Member Author

Choose a reason for hiding this comment

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

This requires a feature declaration.

@rphmeier rphmeier merged commit 2700e3b into master Apr 17, 2019
@rphmeier rphmeier deleted the bkchr-validate_block branch April 17, 2019 12:26
coriolinus pushed a commit that referenced this pull request Mar 24, 2020
imstar15 pushed a commit to imstar15/cumulus that referenced this pull request Nov 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants