This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Client provide uncles #1609
Merged
Merged
Client provide uncles #1609
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d5cd4ca
fix: wasm file
seerscode a19a704
feat: add Ord to Hash
seerscode 427ac59
feat: add children function to backend
seerscode 79ae994
feat: add test for children hashes
seerscode a89f657
feat: add uncles function to client
seerscode a5dcc25
fix: improve uncles function adds few more tests
seerscode a2e00c2
chore: address review
seerscode dbcf786
fix: wasm file, typo, unused import
seerscode 88b8745
fix: children datastructure to not keep children on memory
seerscode ef925d7
fix: types
seerscode b94a380
fix tests
seerscode aab6e23
fix: it should use the map already created
seerscode afa09cc
chore: add documentation
seerscode 6e14788
chore: remove unused imports
seerscode b5c151a
fix tests
seerscode 107984e
fix: Order of Hash
seerscode 3fae6ca
fix: remove get_children function
seerscode 6809938
fix: separate HashMap from db
seerscode f587d69
fix: remove hashmap
seerscode 6b55176
feat: add tests for backend
seerscode 3a0a58b
Merge branch 'master' into mar-client-provide-uncles
gavofyork adfb706
chore: free functions
seerscode bf5a3e8
feat: add remove children
seerscode 96ce77f
fix: remove children when reverting
seerscode 334fc5a
Merge branch 'mar-client-provide-uncles' of github.com:paritytech/sub…
seerscode b6fc5bf
fix: typo and spec version
seerscode 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
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
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,100 @@ | ||
| // Copyright 2019 Parity Technologies (UK) Ltd. | ||
| // This file is part of Substrate. | ||
|
|
||
| // Substrate is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Substrate is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| use kvdb::{KeyValueDB, DBTransaction}; | ||
| use parity_codec::{Encode, Decode}; | ||
| use crate::error; | ||
| use std::hash::Hash; | ||
|
|
||
|
|
||
| /// Used to access children blocks hashes from db. | ||
| pub struct ChildrenMap; | ||
|
|
||
| impl ChildrenMap { | ||
| /// Returns the hashes of the children blocks of the block with `parent_hash`. | ||
| pub fn children_hashes< | ||
| K: Eq + Hash + Clone + Encode + Decode, | ||
| V: Eq + Hash + Clone + Encode + Decode, | ||
| >(db: &KeyValueDB, column: Option<u32>, prefix: &[u8], parent_hash: K) -> error::Result<Vec<V>> { | ||
| let mut buf = prefix.to_vec(); | ||
| parent_hash.using_encoded(|s| buf.extend(s)); | ||
|
|
||
| let raw_val_opt = match db.get(column, &buf[..]) { | ||
| Ok(raw_val_opt) => raw_val_opt, | ||
| Err(_) => return Err(error::ErrorKind::Backend("Error reading value from database".into()).into()), | ||
| }; | ||
|
|
||
| let raw_val = match raw_val_opt { | ||
| Some(val) => val, | ||
| None => return Ok(Vec::new()), | ||
| }; | ||
|
|
||
| let children: Vec<V> = match Decode::decode(&mut &raw_val[..]) { | ||
| Some(children) => children, | ||
| None => return Err(error::ErrorKind::Backend("Error decoding children".into()).into()), | ||
| }; | ||
|
|
||
| Ok(children) | ||
| } | ||
|
|
||
| /// Insert the key-value pair (`parent_hash`, `children_hashes`) in the transaction. | ||
| /// Any existing value is overwritten upon write. | ||
| pub fn prepare_transaction< | ||
| K: Eq + Hash + Clone + Encode + Decode, | ||
| V: Eq + Hash + Clone + Encode + Decode, | ||
| >( | ||
| tx: &mut DBTransaction, | ||
| column: Option<u32>, | ||
| prefix: &[u8], | ||
| parent_hash: K, | ||
| children_hashes: V, | ||
| ) { | ||
| let mut key = prefix.to_vec(); | ||
| parent_hash.using_encoded(|s| key.extend(s)); | ||
| tx.put_vec(column, &key[..], children_hashes.encode()); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn children_write_read() { | ||
| const PREFIX: &[u8] = b"children"; | ||
| let db = ::kvdb_memorydb::create(0); | ||
|
|
||
| let mut tx = DBTransaction::new(); | ||
|
|
||
| let mut children1 = Vec::new(); | ||
| children1.push(1_3); | ||
| children1.push(1_5); | ||
| ChildrenMap::prepare_transaction(&mut tx, None, PREFIX, 1_1, children1); | ||
|
|
||
| let mut children2 = Vec::new(); | ||
| children2.push(1_4); | ||
| children2.push(1_6); | ||
| ChildrenMap::prepare_transaction(&mut tx, None, PREFIX, 1_2, children2); | ||
|
|
||
| db.write(tx).unwrap(); | ||
|
|
||
| let r1: Vec<u32> = ChildrenMap::children_hashes(&db, None, PREFIX, 1_1).unwrap(); | ||
| let r2: Vec<u32> = ChildrenMap::children_hashes(&db, None, PREFIX, 1_2).unwrap(); | ||
|
|
||
| assert_eq!(r1, vec![1_3, 1_5]); | ||
| assert_eq!(r2, vec![1_4, 1_6]); | ||
| } | ||
| } | ||
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
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
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.
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
ChildrenMapstruct and make these free functions.