-
Notifications
You must be signed in to change notification settings - Fork 49
Relay header and check proof logic #127
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2b45066
minor changes, commit before merge
hackfisher 1fff089
Merge from develop and fix conflicts
hackfisher 76ea95f
add merkle patricia trie
hackfisher cba6644
add expected receipt in the extrisinct, actrully can get from last no…
hackfisher 2747ed8
Merge branch 'develop' into denny_fixing_ethereum_bridge
hackfisher 9d0ee99
add test call for extrinsics
hackfisher c0631cd
minor changes
hackfisher 3f566a9
update types
hackfisher 09b5e4a
add total difficulty related codes
hackfisher 6c6222c
Set corrct ethash params, Fixed #112
hackfisher 2ab88ee
cargo fmt
hackfisher e3892d8
fix compile issues
hackfisher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| [package] | ||
| name = "merkle-patricia-trie" | ||
| version = "0.1.0" | ||
| authors = ["Darwinia Network <hello@darwinia.network>"] | ||
| edition = "2018" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
|
||
| [dependencies] | ||
| rlp = { git = "https://github.com/darwinia-network/parity-common.git", default-features = false } | ||
| hash = { package = "keccak-hash", git = "https://github.com/darwinia-network/parity-common.git", default-features = false } | ||
| hashbrown = { version = "0.6.0" } | ||
|
|
||
| [dev-dependencies] | ||
| rand = "0.6.3" | ||
| hex = "0.3.2" | ||
| criterion = "0.2.10" | ||
| ethereum-types = "0.5.2" | ||
| uuid = { version = "0.7", features = ["serde", "v4"] } | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "rlp/std", | ||
| "hash/std" | ||
| ] | ||
|
|
||
| [[bench]] | ||
| name = "trie" | ||
| harness = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| use std::rc::Rc; | ||
|
|
||
| use criterion::{criterion_group, criterion_main, Criterion}; | ||
| use merkle_patricia_trie::{MemoryDB, MerklePatriciaTrie, Trie}; | ||
| use uuid::Uuid; | ||
|
|
||
| fn insert_worse_case_benchmark(c: &mut Criterion) { | ||
| c.bench_function("insert one", |b| { | ||
| let mut trie = MerklePatriciaTrie::new(Rc::new(MemoryDB::new())); | ||
|
|
||
| b.iter(|| { | ||
| let key = Uuid::new_v4().as_bytes().to_vec(); | ||
| let value = Uuid::new_v4().as_bytes().to_vec(); | ||
| trie.insert(key, value).unwrap() | ||
| }) | ||
| }); | ||
|
|
||
| c.bench_function("insert 1k", |b| { | ||
| let mut trie = MerklePatriciaTrie::new(Rc::new(MemoryDB::new())); | ||
|
|
||
| let (keys, values) = random_data(1000); | ||
| b.iter(|| { | ||
| for i in 0..keys.len() { | ||
| trie.insert(keys[i].clone(), values[i].clone()).unwrap() | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| c.bench_function("insert 10k", |b| { | ||
| let mut trie = MerklePatriciaTrie::new(Rc::new(MemoryDB::new())); | ||
|
|
||
| let (keys, values) = random_data(10000); | ||
| b.iter(|| { | ||
| for i in 0..keys.len() { | ||
| trie.insert(keys[i].clone(), values[i].clone()).unwrap() | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| c.bench_function("get based 10k", |b| { | ||
| let mut trie = MerklePatriciaTrie::new(Rc::new(MemoryDB::new())); | ||
|
|
||
| let (keys, values) = random_data(10000); | ||
| for i in 0..keys.len() { | ||
| trie.insert(keys[i].clone(), values[i].clone()).unwrap() | ||
| } | ||
|
|
||
| b.iter(|| { | ||
| let key = trie.get(&keys[7777]).unwrap(); | ||
| assert_ne!(key, None); | ||
| }); | ||
| }); | ||
|
|
||
| c.bench_function("remove 1k", |b| { | ||
| let mut trie = MerklePatriciaTrie::new(Rc::new(MemoryDB::new())); | ||
|
|
||
| let (keys, values) = random_data(1000); | ||
| for i in 0..keys.len() { | ||
| trie.insert(keys[i].clone(), values[i].clone()).unwrap() | ||
| } | ||
|
|
||
| b.iter(|| { | ||
| for key in keys.iter() { | ||
| trie.remove(key).unwrap(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| c.bench_function("remove 10k", |b| { | ||
| let mut trie = MerklePatriciaTrie::new(Rc::new(MemoryDB::new())); | ||
|
|
||
| let (keys, values) = random_data(10000); | ||
| for i in 0..keys.len() { | ||
| trie.insert(keys[i].clone(), values[i].clone()).unwrap() | ||
| } | ||
|
|
||
| b.iter(|| { | ||
| for key in keys.iter() { | ||
| trie.remove(key).unwrap(); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn random_data(n: usize) -> (Vec<Vec<u8>>, Vec<Vec<u8>>) { | ||
| let mut keys = Vec::with_capacity(n); | ||
| let mut values = Vec::with_capacity(n); | ||
| for _ in 0..n { | ||
| let key = Uuid::new_v4().as_bytes().to_vec(); | ||
| let value = Uuid::new_v4().as_bytes().to_vec(); | ||
| keys.push(key); | ||
| values.push(value); | ||
| } | ||
|
|
||
| (keys, values) | ||
| } | ||
|
|
||
| criterion_group!(benches, insert_worse_case_benchmark); | ||
| criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| use crate::std::*; | ||
| use hashbrown::HashMap; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct MemoryDB { | ||
| data: RefCell<HashMap<Vec<u8>, Vec<u8>>>, | ||
| } | ||
|
|
||
| impl MemoryDB { | ||
| pub fn new() -> Self { | ||
| MemoryDB { | ||
| data: RefCell::new(HashMap::new()), | ||
| } | ||
| } | ||
|
|
||
| pub fn get(&self, key: &[u8]) -> Option<Vec<u8>> { | ||
| let data = self.data.borrow(); | ||
| if let Some(d) = data.get(key) { | ||
| Some(d.clone()) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| pub fn insert(&self, key: Vec<u8>, value: Vec<u8>) -> Option<Vec<u8>> { | ||
| self.data.borrow_mut().insert(key, value) | ||
| } | ||
|
|
||
| pub fn contains(&self, key: &[u8]) -> bool { | ||
| self.data.borrow().contains_key(key) | ||
| } | ||
|
|
||
| pub fn remove(&self, key: &[u8]) -> Option<Vec<u8>> { | ||
| self.data.borrow_mut().remove(key) | ||
| } | ||
|
|
||
| /// Insert a batch of data into the cache. | ||
| pub fn insert_batch(&self, keys: Vec<Vec<u8>>, values: Vec<Vec<u8>>) { | ||
| for i in 0..keys.len() { | ||
| let key = keys[i].clone(); | ||
| let value = values[i].clone(); | ||
| self.insert(key, value); | ||
| } | ||
| } | ||
|
|
||
| /// Remove a batch of data into the cache. | ||
| pub fn remove_batch(&self, keys: &[Vec<u8>]) { | ||
| for key in keys { | ||
| self.remove(key); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| use crate::std::*; | ||
| use rlp::DecoderError; | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum TrieError { | ||
| DB(String), | ||
| Decoder(DecoderError), | ||
| InvalidData, | ||
| InvalidStateRoot, | ||
| InvalidProof, | ||
| } | ||
|
|
||
| impl fmt::Display for TrieError { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| let printable = match *self { | ||
| TrieError::DB(ref err) => format!("trie error: {:?}", err), | ||
| TrieError::Decoder(ref err) => format!("trie error: {:?}", err), | ||
| TrieError::InvalidData => "trie error: invalid data".to_owned(), | ||
| TrieError::InvalidStateRoot => "trie error: invalid state root".to_owned(), | ||
| TrieError::InvalidProof => "trie error: invalid proof".to_owned(), | ||
| }; | ||
| write!(f, "{}", printable) | ||
| } | ||
| } | ||
|
|
||
| impl From<DecoderError> for TrieError { | ||
| fn from(error: DecoderError) -> Self { | ||
| TrieError::Decoder(error) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Ensure we're `no_std` when compiling for Wasm. | ||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| #[cfg(not(feature = "std"))] | ||
| extern crate alloc; | ||
| #[cfg(not(feature = "std"))] | ||
| extern crate core; | ||
|
|
||
| #[cfg(not(feature = "std"))] | ||
| mod std { | ||
| pub use alloc::borrow::ToOwned; | ||
| pub use alloc::format; | ||
| pub use alloc::rc::Rc; | ||
| pub use alloc::string::String; | ||
| pub use alloc::vec; | ||
| pub use alloc::vec::Vec; | ||
|
|
||
| pub use core::cell::RefCell; | ||
| pub use core::fmt; | ||
| } | ||
|
|
||
| #[cfg(feature = "std")] | ||
| mod std { | ||
| pub use std::cell::RefCell; | ||
| pub use std::fmt; | ||
| pub use std::rc::Rc; | ||
| } | ||
|
|
||
| mod db; | ||
| mod error; | ||
| mod nibbles; | ||
| mod node; | ||
| mod proof; | ||
| mod tests; | ||
| pub mod trie; | ||
|
|
||
| pub use db::MemoryDB; | ||
| pub use error::TrieError; | ||
| pub use proof::Proof; | ||
| pub use trie::{MerklePatriciaTrie, Trie, TrieResult}; | ||
|
|
||
| /// Generates a trie for a vector of key-value tuples | ||
| /// | ||
| /// ```rust | ||
| /// extern crate merkle_patricia_trie as trie; | ||
| /// extern crate hex; | ||
| /// | ||
| /// use trie::{Trie, build_trie}; | ||
| /// use hex::FromHex; | ||
| /// | ||
| /// fn main() { | ||
| /// let v = vec![ | ||
| /// ("doe", "reindeer"), | ||
| /// ("dog", "puppy"), | ||
| /// ("dogglesworth", "cat"), | ||
| /// ]; | ||
| /// | ||
| /// let root:Vec<u8> = Vec::from_hex("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3").unwrap(); | ||
| /// assert_eq!(build_trie(v).unwrap().root().unwrap(), root); | ||
| /// } | ||
| /// ``` | ||
| pub fn build_trie<I, A, B>(data: I) -> TrieResult<MerklePatriciaTrie> | ||
| where | ||
| I: IntoIterator<Item = (A, B)>, | ||
| A: AsRef<[u8]> + Ord, | ||
| B: AsRef<[u8]>, | ||
| { | ||
| let memdb = std::Rc::new(MemoryDB::new()); | ||
| let mut trie = MerklePatriciaTrie::new(memdb.clone()); | ||
| data.into_iter().for_each(|(key, value)| { | ||
| // TODO the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `core::ops::Try`) | ||
| trie.insert(key.as_ref().to_vec(), value.as_ref().to_vec()); | ||
| }); | ||
| trie.root()?; | ||
| Ok(trie) | ||
| } | ||
|
|
||
| /// Generates a trie for a vector of values | ||
| /// | ||
| /// ```rust | ||
| /// extern crate merkle_patricia_trie as trie; | ||
| /// extern crate hex; | ||
| /// | ||
| /// use trie::{Trie, build_order_trie}; | ||
| /// use hex::FromHex; | ||
| /// | ||
| /// fn main() { | ||
| /// let v = &["doe", "reindeer"]; | ||
| /// let root:Vec<u8> = Vec::from_hex("e766d5d51b89dc39d981b41bda63248d7abce4f0225eefd023792a540bcffee3").unwrap(); | ||
| /// assert_eq!(build_order_trie(v).unwrap().root().unwrap(), root); | ||
| /// } | ||
| /// ``` | ||
| pub fn build_order_trie<I>(data: I) -> TrieResult<MerklePatriciaTrie> | ||
| where | ||
| I: IntoIterator, | ||
| I::Item: AsRef<[u8]>, | ||
| { | ||
| build_trie(data.into_iter().enumerate().map(|(i, v)| (rlp::encode(&i), v))) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.