Use [u8; 4] for well known cache keys (#2152)

* Use [u8; 4] for well known cache keys

* Use type alias
This commit is contained in:
Stanislav Tkach
2019-04-01 02:56:49 +03:00
committed by DemiMarie-parity
parent 3dfda381d5
commit fbbd79e778
15 changed files with 53 additions and 42 deletions
+1 -1
View File
@@ -21,12 +21,12 @@ parity-codec = { version = "3.2", features = ["derive"] }
executor = { package = "substrate-executor", path = "../../executor" }
state_db = { package = "substrate-state-db", path = "../../state-db" }
trie = { package = "substrate-trie", path = "../../trie" }
consensus_common = { package = "substrate-consensus-common", path = "../../consensus/common" }
[dev-dependencies]
kvdb-memorydb = { git = "https://github.com/paritytech/parity-common", rev="b0317f649ab2c665b7987b8475878fc4d2e1f81d" }
substrate-keyring = { path = "../../keyring" }
test-client = { package = "substrate-test-client", path = "../../test-client" }
consensus_common = { package = "substrate-consensus-common", path = "../../consensus/common" }
env_logger = { version = "0.6" }
[features]
+13 -12
View File
@@ -26,6 +26,7 @@ use client::error::Result as ClientResult;
use parity_codec::{Encode, Decode};
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor, As};
use consensus_common::well_known_cache_keys::Id as CacheKeyId;
use crate::utils::{self, COLUMN_META};
use self::list_cache::ListCache;
@@ -64,7 +65,7 @@ impl<T> CacheItemT for T where T: Clone + Decode + Encode + PartialEq {}
/// Database-backed blockchain data cache.
pub struct DbCache<Block: BlockT> {
cache_at: HashMap<Vec<u8>, ListCache<Block, Vec<u8>, self::list_storage::DbStorage>>,
cache_at: HashMap<CacheKeyId, ListCache<Block, Vec<u8>, self::list_storage::DbStorage>>,
db: Arc<KeyValueDB>,
key_lookup_column: Option<u32>,
header_column: Option<u32>,
@@ -112,7 +113,7 @@ impl<Block: BlockT> DbCache<Block> {
}
/// Creates `ListCache` with the given name or returns a reference to the existing.
fn get_cache(&mut self, name: Vec<u8>) -> &mut ListCache<Block, Vec<u8>, self::list_storage::DbStorage> {
fn get_cache(&mut self, name: CacheKeyId) -> &mut ListCache<Block, Vec<u8>, self::list_storage::DbStorage> {
get_cache_helper(
&mut self.cache_at,
name,
@@ -128,17 +129,17 @@ impl<Block: BlockT> DbCache<Block> {
// This helper is needed because otherwise the borrow checker will require to
// clone all parameters outside of the closure.
fn get_cache_helper<'a, Block: BlockT>(
cache_at: &'a mut HashMap<Vec<u8>, ListCache<Block, Vec<u8>, self::list_storage::DbStorage>>,
name: Vec<u8>,
cache_at: &'a mut HashMap<CacheKeyId, ListCache<Block, Vec<u8>, self::list_storage::DbStorage>>,
name: CacheKeyId,
db: &Arc<KeyValueDB>,
key_lookup: Option<u32>,
header: Option<u32>,
cache: Option<u32>,
best_finalized_block: &ComplexBlockId<Block>,
) -> &'a mut ListCache<Block, Vec<u8>, self::list_storage::DbStorage> {
cache_at.entry(name.clone()).or_insert_with(|| {
cache_at.entry(name).or_insert_with(|| {
ListCache::new(
self::list_storage::DbStorage::new(name, db.clone(),
self::list_storage::DbStorage::new(name.to_vec(), db.clone(),
self::list_storage::DbColumns {
meta: COLUMN_META,
key_lookup,
@@ -154,7 +155,7 @@ fn get_cache_helper<'a, Block: BlockT>(
/// Cache operations that are to be committed after database transaction is committed.
pub struct DbCacheTransactionOps<Block: BlockT> {
cache_at_op: HashMap<Vec<u8>, self::list_cache::CommitOperation<Block, Vec<u8>>>,
cache_at_op: HashMap<CacheKeyId, self::list_cache::CommitOperation<Block, Vec<u8>>>,
best_finalized_block: Option<ComplexBlockId<Block>>,
}
@@ -162,7 +163,7 @@ pub struct DbCacheTransactionOps<Block: BlockT> {
pub struct DbCacheTransaction<'a, Block: BlockT> {
cache: &'a mut DbCache<Block>,
tx: &'a mut DBTransaction,
cache_at_op: HashMap<Vec<u8>, self::list_cache::CommitOperation<Block, Vec<u8>>>,
cache_at_op: HashMap<CacheKeyId, self::list_cache::CommitOperation<Block, Vec<u8>>>,
best_finalized_block: Option<ComplexBlockId<Block>>,
}
@@ -180,7 +181,7 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> {
mut self,
parent: ComplexBlockId<Block>,
block: ComplexBlockId<Block>,
data_at: HashMap<Vec<u8>, Vec<u8>>,
data_at: HashMap<CacheKeyId, Vec<u8>>,
is_final: bool,
) -> ClientResult<Self> {
assert!(self.cache_at_op.is_empty());
@@ -192,8 +193,8 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> {
.cloned()
.collect::<Vec<_>>();
let mut insert_op = |name: Vec<u8>, value: Option<Vec<u8>>| -> Result<(), client::error::Error> {
let cache = self.cache.get_cache(name.clone());
let mut insert_op = |name: CacheKeyId, value: Option<Vec<u8>>| -> Result<(), client::error::Error> {
let cache = self.cache.get_cache(name);
let op = cache.on_block_insert(
&mut self::list_storage::DbStorageTransaction::new(
cache.storage(),
@@ -253,7 +254,7 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> {
pub struct DbCacheSync<Block: BlockT>(pub RwLock<DbCache<Block>>);
impl<Block: BlockT> BlockchainCache<Block> for DbCacheSync<Block> {
fn get_at(&self, key: &[u8], at: &BlockId<Block>) -> Option<Vec<u8>> {
fn get_at(&self, key: &CacheKeyId, at: &BlockId<Block>) -> Option<Vec<u8>> {
let cache = self.0.read();
let storage = cache.cache_at.get(key)?.storage();
let db = storage.db();
+2 -1
View File
@@ -55,6 +55,7 @@ use crate::utils::{Meta, db_err, meta_keys, open_database, read_db, block_id_to_
use client::leaves::{LeafSet, FinalizationDisplaced};
use client::children;
use state_db::StateDb;
use consensus_common::well_known_cache_keys;
use crate::storage_cache::{CachingState, SharedCache, new_shared_cache};
use log::{trace, debug, warn};
pub use state_db::PruningMode;
@@ -313,7 +314,7 @@ where Block: BlockT<Hash=H256>,
Ok(())
}
fn update_cache(&mut self, _cache: HashMap<Vec<u8>, Vec<u8>>) {
fn update_cache(&mut self, _cache: HashMap<well_known_cache_keys::Id, Vec<u8>>) {
// Currently cache isn't implemented on full nodes.
}
+9 -9
View File
@@ -33,6 +33,7 @@ use primitives::Blake2Hasher;
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT,
Zero, One, As, NumberFor, Digest, DigestItem};
use consensus_common::well_known_cache_keys;
use crate::cache::{DbCacheSync, DbCache, ComplexBlockId};
use crate::utils::{self, meta_keys, Meta, db_err, open_database,
read_db, block_id_to_lookup_key, read_meta};
@@ -370,7 +371,7 @@ impl<Block> LightBlockchainStorage<Block> for LightStorage<Block>
fn import_header(
&self,
header: Block::Header,
cache_at: HashMap<Vec<u8>, Vec<u8>>,
cache_at: HashMap<well_known_cache_keys::Id, Vec<u8>>,
leaf_state: NewBlockState,
aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
) -> ClientResult<()> {
@@ -539,7 +540,6 @@ pub(crate) mod tests {
use runtime_primitives::generic::DigestItem;
use runtime_primitives::testing::{H256 as Hash, Header, Block as RawBlock, ExtrinsicWrapper};
use runtime_primitives::traits::AuthorityIdFor;
use consensus_common::well_known_cache_keys;
use super::*;
type Block = RawBlock<ExtrinsicWrapper<u32>>;
@@ -569,7 +569,7 @@ pub(crate) mod tests {
pub fn insert_block<F: Fn() -> Header>(
db: &LightStorage<Block>,
cache: HashMap<Vec<u8>, Vec<u8>>,
cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
header: F,
) -> Hash {
let header = header();
@@ -580,7 +580,7 @@ pub(crate) mod tests {
fn insert_final_block<F: Fn() -> Header>(
db: &LightStorage<Block>,
cache: HashMap<Vec<u8>, Vec<u8>>,
cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
header: F,
) -> Hash {
let header = header();
@@ -591,7 +591,7 @@ pub(crate) mod tests {
fn insert_non_best_block<F: Fn() -> Header>(
db: &LightStorage<Block>,
cache: HashMap<Vec<u8>, Vec<u8>>,
cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
header: F,
) -> Hash {
let header = header();
@@ -828,18 +828,18 @@ pub(crate) mod tests {
}
}
fn same_authorities() -> HashMap<Vec<u8>, Vec<u8>> {
fn same_authorities() -> HashMap<well_known_cache_keys::Id, Vec<u8>> {
HashMap::new()
}
fn make_authorities(authorities: Vec<AuthorityId>) -> HashMap<Vec<u8>, Vec<u8>> {
fn make_authorities(authorities: Vec<AuthorityId>) -> HashMap<well_known_cache_keys::Id, Vec<u8>> {
let mut map = HashMap::new();
map.insert(well_known_cache_keys::AUTHORITIES.to_vec(), authorities.encode());
map.insert(well_known_cache_keys::AUTHORITIES, authorities.encode());
map
}
fn get_authorities(cache: &BlockchainCache<Block>, at: BlockId<Block>) -> Option<Vec<AuthorityId>> {
cache.get_at(well_known_cache_keys::AUTHORITIES, &at).and_then(|val| Decode::decode(&mut &val[..]))
cache.get_at(&well_known_cache_keys::AUTHORITIES, &at).and_then(|val| Decode::decode(&mut &val[..]))
}
let auth1 = || AuthorityId::from_raw([1u8; 32]);