Client provide uncles (#1609)

* 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
This commit is contained in:
Marcio Diaz
2019-02-25 11:21:36 +01:00
committed by GitHub
parent 63ec8b335b
commit 43e4544884
10 changed files with 392 additions and 8 deletions
+16
View File
@@ -52,6 +52,7 @@ use executor::RuntimeInfo;
use state_machine::{CodeExecutor, DBValue};
use crate::utils::{Meta, db_err, meta_keys, open_database, read_db, block_id_to_lookup_key, read_meta};
use client::LeafSet;
use client::children;
use state_db::StateDb;
use crate::storage_cache::{CachingState, SharedCache, new_shared_cache};
use log::{trace, debug, warn};
@@ -249,6 +250,10 @@ impl<Block: BlockT> client::blockchain::Backend<Block> for BlockchainDb<Block> {
fn leaves(&self) -> Result<Vec<Block::Hash>, client::error::Error> {
Ok(self.leaves.read().hashes())
}
fn children(&self, parent_hash: Block::Hash) -> Result<Vec<Block::Hash>, client::error::Error> {
children::read_children(&*self.db, columns::META, meta_keys::CHILDREN_PREFIX, parent_hash)
}
}
/// Database transaction
@@ -857,6 +862,10 @@ impl<Block: BlockT<Hash=H256>> Backend<Block> {
displaced_leaf
};
let mut children = children::read_children(&*self.storage.db, columns::META, meta_keys::CHILDREN_PREFIX, parent_hash)?;
children.push(hash);
children::write_children(&mut transaction, columns::META, meta_keys::CHILDREN_PREFIX, parent_hash, children);
meta_updates.push((hash, number, pending_block.leaf_state.is_best(), finalized));
Some((number, hash, enacted, retracted, displaced_leaf, is_best))
@@ -1080,6 +1089,7 @@ impl<Block> client::backend::Backend<Block, Blake2Hasher> for Backend<Block> whe
let key = utils::number_and_hash_to_lookup_key(best.clone(), &hash);
transaction.put(columns::META, meta_keys::BEST_BLOCK, &key);
transaction.delete(columns::KEY_LOOKUP, removed.hash().as_ref());
children::remove_children(&mut transaction, columns::META, meta_keys::CHILDREN_PREFIX, hash);
self.storage.db.write(transaction).map_err(db_err)?;
self.blockchain.update_meta(hash, best, true, false);
self.blockchain.leaves.write().revert(removed.hash().clone(), removed.number().clone(), removed.parent_hash().clone());
@@ -1793,6 +1803,12 @@ mod tests {
test_client::trait_tests::test_leaves_for_backend(backend);
}
#[test]
fn test_children_with_complex_block_tree() {
let backend: Arc<Backend<test_client::runtime::Block>> = Arc::new(Backend::new_test(20, 20));
test_client::trait_tests::test_children_for_backend(backend);
}
#[test]
fn test_blockchain_query_by_number_gets_canonical() {
let backend: Arc<Backend<test_client::runtime::Block>> = Arc::new(Backend::new_test(20, 20));
+2
View File
@@ -51,6 +51,8 @@ pub mod meta_keys {
pub const GENESIS_HASH: &[u8; 3] = b"gen";
/// Leaves prefix list key.
pub const LEAF_PREFIX: &[u8; 4] = b"leaf";
/// Children prefix list key.
pub const CHILDREN_PREFIX: &[u8; 8] = b"children";
}
/// Database metadata.