Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make triehash generic over encoding scheme #61

Closed
wants to merge 56 commits into from

Conversation

dvdplm
Copy link
Contributor

@dvdplm dvdplm commented Sep 17, 2018

Part of paritytech/substrate#664

Introduces a TrieStream trait (in a triestream crate) that lets us compute trie roots without depending on RLP. Refactors the trie building using the new trait.

I'm quite unhappy with how the new trait is essentially modeled on RlpStream. In particular having to have an encode method seems awkward and out of place. Ideas for improving this are most welcome.

…oding

* master:
  Simple test case for the pr
  Update README.md
  Fix for removal of a fatdb value.
…oding

* master: (22 commits)
  fix rebase and bump rlp in trie-standardmap
  remove elastic-array in triehash
  fix indentations
  fix retab bug with pretty-print
  use Vec in memorydb benches
  rebased and retab
  update patricia-trie to use generic hashdb elements
  bump hasdb and memorydb v0.3.0 and fix tests
  have memorydb be generic over the value type
  have hashdb be generic over DBValue
  have NodeCodec using Vec
  bump rlp v0.3.0 and make all workspace test pass
  fix doc test
  remove elastic-array and use Vec instead
  publish kvdb-rocksdb v0.1.4
  Update fs-swap version
  Patricia tree version 0.2.2 with fatdb changes
  Better error messages
  Use debug instead of warn for atomic swap failure log
  document public functions and use expect instead of unwrap
  ...
@dvdplm dvdplm self-assigned this Sep 17, 2018
fn out(self) -> Vec<u8>;
fn as_raw(&self) -> &[u8];

fn encode(k: &usize) -> Vec<u8>; // Arrgh – `ordered_trie_root` enumerates and rlp-encodes items with `rlp::encode()` so need something similar here. :/
Copy link
Member

Choose a reason for hiding this comment

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

errant spaces

@dvdplm dvdplm requested a review from jacogr September 21, 2018 08:01
/// assert_eq!(ordered_trie_root::<KeccakHasher, _>(v), root.into());
/// }
/// ```
pub fn ordered_trie_root<H, I>(input: I) -> H::Out
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one is moved to triehash-ethereum to avoid depending on rlp::encode. Need to implement enumerated_trie_root in substrate.

Copy link
Member

Choose a reason for hiding this comment

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

that's fair enough - it's pretty trivial.

H::hash(&stream.out())
}

#[cfg(test)]
pub fn unhashed_trie<H, S, I, A, B>(input: I) -> Vec<u8>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the same as trie_root but skips the final hashing. Useful for debugging (probably should be removed before merging).

fn append_extension(&mut self, key: &[u8]);
fn append_substream<H: Hasher>(&mut self, other: Self);
fn out(self) -> Vec<u8>;
fn as_raw(&self) -> &[u8];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one is useful for debugging but not needed otherwise.

println!("[begin_branch] pushing BRANCH_NODE: {}, {:#x?}, {:#010b}", BRANCH_NODE, BRANCH_NODE, BRANCH_NODE);
self.buffer.push(BRANCH_NODE);
println!("[begin_branch] buffer so far: {:#x?}", self.buffer);
// TODO: I think this is wrong. I need to know how long the full branch node is I think and
Copy link
Member

Choose a reason for hiding this comment

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

why would you? it's always 17-length tuple.


fn append_leaf(&mut self, key: &[u8], value: &[u8]) {
let mut hpe = hex_prefix_encode_substrate(key, true);
self.buffer.push(hpe.next().expect("key is not empty; qed"));
Copy link
Member

@gavofyork gavofyork Sep 21, 2018

Choose a reason for hiding this comment

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

this shouldn't be here.

}
fn append_extension(&mut self, key: &[u8]) {
let mut hpe = hex_prefix_encode_substrate(key, false);
self.buffer.push(hpe.next().expect("key is not empty; qed"));
Copy link
Member

@gavofyork gavofyork Sep 21, 2018

Choose a reason for hiding this comment

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

shouldn't be here.

@@ -99,6 +100,19 @@ fn take(input: &mut &[u8], count: usize) -> Option<&[u8]> {
Some(r)
}

fn partial_to_key(partial: &[u8], offset: u8, big: u8) -> Vec<u8> {
let nibble_count = partial.len() * 2 + if data[0] & 16 == 16 { 1 } else { 0 };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is data? Missing param?

@gavofyork gavofyork closed this Sep 23, 2018
@gavofyork gavofyork mentioned this pull request Sep 23, 2018
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants