-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Client provide uncles #1609
Client provide uncles #1609
Conversation
core/client/db/src/lib.rs
Outdated
| fn commit_operation(&self, mut operation: Self::BlockImportOperation) | ||
| -> Result<(), client::error::Error> | ||
| { | ||
| println!("db inside commit_operation ----------------------------"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug output.
core/client/db/src/lib.rs
Outdated
| { | ||
| let mut leaves = self.blockchain.leaves.write(); | ||
| let displaced_leaf = leaves.import(hash, number, parent_hash); | ||
| println!("db prepare_transaction --------------- {:?}->{:?}", parent_hash, hash); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug output.
core/client/src/children.rs
Outdated
| @@ -0,0 +1,161 @@ | |||
| // Copyright 2018 Parity Technologies (UK) Ltd. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
core/client/src/children.rs
Outdated
| K: Ord + Eq + Hash + Clone + Encode + Decode + Debug, | ||
| V: Ord + Eq + Hash + Clone + Encode + Decode + Debug, | ||
| { | ||
| storage: BTreeMap<K, Vec<V>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces all over the file.
core/client/src/children.rs
Outdated
| for (parent, child) in self.pending_added.drain(..) { | ||
| parent.using_encoded(|s| buf.extend(s)); | ||
| tx.put_vec(column, &buf[..], child.encode()); | ||
| buf.truncate(prefix.len()); // reuse allocation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you truncate here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because prefix is the same but parent and child change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also it looks like some spaces got into the indentation for L98 -- please make sure that indentation is done with tabs
core/client/src/children.rs
Outdated
| } | ||
| } | ||
|
|
||
| pub fn hashes(&self, parent_hash: K) -> Vec<V> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we not return &[V]?
core/client/src/client.rs
Outdated
| pub fn lock_import_and_run<R, F: FnOnce(&mut ClientImportOperation<Block, Blake2Hasher, B>) -> error::Result<R>>( | ||
| &self, f: F | ||
| ) -> error::Result<R> { | ||
| println!("in lock_import_and_run ------------------------------------------------"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug.
core/client/src/client.rs
Outdated
|
|
||
| fn get_descendants(&self, target: Block::Hash) -> Vec<Block::Hash> { | ||
| let children = self.backend.blockchain().children(target); | ||
| let mut descendants = vec![]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| let mut descendants = vec![]; | |
| let mut descendants = Vec::new(); |
core/client/src/client.rs
Outdated
| if ancestors.contains(&uncles[i]) { | ||
| uncles.remove(i); | ||
| } else { | ||
| i += 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it only increased in the else branch?
388f553 to
bdcde73
Compare
core/client/src/children.rs
Outdated
| K: Ord + Eq + Hash + Clone + Encode + Decode + Debug, | ||
| V: Ord + Eq + Hash + Clone + Encode + Decode + Debug, | ||
| { | ||
| storage: BTreeMap<K, Vec<V>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...is this storing all children in memory? that doesn't seem like it will scale very well.
This mapping should be on-disk only -- I don't think emulating the LeavesSet is a good idea for this instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the part where it reads all children from db :). I'm adding children everywhere where leaves are being added. Are there other parts of the code where I should add them?
core/client/src/client.rs
Outdated
|
|
||
| /// Gets the uncles of the block with `target_hash` going back `max_generation` ancestors. | ||
| pub fn uncles(&self, target_hash: Block::Hash, max_generation: NumberFor<Block>) | ||
| -> error::Result<Option<Vec<Block::Hash>>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not return any Ok(None), so we probably can remove the Option here.
core/client/src/client.rs
Outdated
| -> error::Result<Option<Vec<Block::Hash>>> | ||
| { | ||
| let load_header = |id: Block::Hash| { | ||
| match self.backend.blockchain().header(BlockId::Hash(id)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| match self.backend.blockchain().header(BlockId::Hash(id)) { | |
| match self.backend.blockchain().header(BlockId::Hash(id))? { |
And the you just need to match for Some(_) and None.
core/client/src/client.rs
Outdated
| let genesis_hash = self.backend.blockchain().info().unwrap().genesis_hash; | ||
| let genesis = load_header(genesis_hash)?; | ||
| let mut current = load_header(target_hash)?; | ||
| let mut uncles = vec![]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| let mut uncles = vec![]; | |
| let mut uncles = Vec::new(); |
377c11c to
079c506
Compare
079c506 to
3f9dab7
Compare
core/client/src/children.rs
Outdated
| match self.storage.get_mut(&parent_hash) { | ||
| Some(children) => children.push(child_hash), | ||
| None => { | ||
| self.storage.insert(parent_hash, vec![child_hash]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use entry API. self.storage.entry(parent_hash).or_insert_with(Vec::new).push(child_hash)
core/client/src/children.rs
Outdated
| /// Returns the hashes of the children blocks of the block with `parent_hash`. | ||
| /// It doesn't read the database. | ||
| pub fn hashes_from_mem(&self, parent_hash: K) -> Vec<V> { | ||
| match self.storage.get(&parent_hash) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use unwrap_or_else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*cloned().unwrap_or_default()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤷♂️ mine is shorter :)
core/client/src/children.rs
Outdated
|
|
||
| let mut buf = prefix.to_vec(); | ||
| parent_hash.using_encoded(|s| buf.extend(s)); | ||
| let raw_val = match db.get(column, &buf[..]).unwrap() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prove or remove unwrap.
core/sr-primitives/src/traits.rs
Outdated
|
|
||
| /// Abstraction around hashing | ||
| pub trait Hash: 'static + MaybeSerializeDebug + Clone + Eq + PartialEq { // Stupid bug in the Rust compiler believes derived | ||
| pub trait Hash: 'static + MaybeSerializeDebug + Clone + Eq + PartialEq + PartialOrd + Ord { // Stupid bug in the Rust compiler believes derived |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a bit too strong IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why should hash types always be orderable?
core/client/src/client.rs
Outdated
| } | ||
| }; | ||
|
|
||
| let genesis_hash = self.backend.blockchain().info().unwrap().genesis_hash; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prove or remove unwrap
core/client/src/client.rs
Outdated
| let mut descendants = Vec::new(); | ||
| for child in children { | ||
| descendants.push(child); | ||
| let d = self.get_descendants(child, None)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't this recurse all the way to the head of all descendant chains? could be expensive.
core/client/src/client.rs
Outdated
| for _generation in 0..max_generation.as_() { | ||
| uncles.extend(self.get_descendants(ancestor.hash(), Some(current.hash()))?); | ||
| current = ancestor; | ||
| if genesis == current { break; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just check if current hash == genesis hash and don't load the genesis header. and you can avoid doing the hash calculations in all cases but the first by book-keeping the last parent_hash.
rphmeier
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that uncles are a direct descendant of an ancestor, not any descendant of an ancestor.
core/client/src/children.rs
Outdated
| } | ||
|
|
||
| /// Returns the hashes of the children blocks of the block with `parent_hash`. | ||
| pub fn hashes(&self, db: &KeyValueDB, column: Option<u32>, prefix: &[u8], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either all parameters on one line or one parameter per line, please.
core/client/src/children.rs
Outdated
| let mut buf = prefix.to_vec(); | ||
| parent_hash.using_encoded(|s| buf.extend(s)); | ||
|
|
||
| if let Ok(raw_val_opt) = db.get(column, &buf[..]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is way to complicated. Either split it up across multiple lines with ? or use match.
core/client/src/children.rs
Outdated
| /// Returns the hashes of the children blocks of the block with `parent_hash`. | ||
| /// It doesn't read the database. | ||
| pub fn hashes_from_mem(&self, parent_hash: K) -> Vec<V> { | ||
| match self.storage.get(&parent_hash) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*cloned().unwrap_or_default()
ad07fbf to
f29f611
Compare
core/client/src/client.rs
Outdated
|
|
||
| for _generation in 0..max_generation.as_() { | ||
| let children = self.get_children(ancestor_hash, Some(current_hash))?; | ||
| uncles.extend(children); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think the get_children function could be elided in favor of
let children = self.backend.blockchain().children(ancestor_hash)?;
uncles.extend(children.into_iter().filter(|h| h != ¤t_hash));this saves a sweep and some potential memmoves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.
b4c0d72 to
f587d69
Compare
|
Ready for review again? |
Yes, I think I handled all the issues in the reviews. |
|
what has changed since the last review? |
core/client/src/children.rs
Outdated
|
|
||
| impl ChildrenMap { | ||
| /// Returns the hashes of the children blocks of the block with `parent_hash`. | ||
| pub fn children_hashes< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could kill the ChildrenMap struct and make these free functions.
bkchr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, some last minor issues and then we are good to go :)
core/test-client/src/trait_tests.rs
Outdated
| vec![a5.hash(), b4.hash(), c3.hash(), d2.hash()]); | ||
| } | ||
|
|
||
| /// helper to test the `leaves` implementation for various backends |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// helper to test the `leaves` implementation for various backends | |
| /// helper to test the `children` implementation for various backends |
node/runtime/src/lib.rs
Outdated
| authoring_version: 10, | ||
| spec_version: 30, | ||
| impl_version: 30, | ||
| spec_version: 31, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't touch the runtime, so you don't need to change the spec_version and impl_version.
cc5b1d0 to
b6fc5bf
Compare
* feat: add children function to backend * feat: add test for children hashes * feat: add uncles function to client * fix: improve uncles function adds few more tests * fix: remove children when reverting * fix: typo and spec version
PR for issue #1448