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]);
+2 -1
View File
@@ -23,6 +23,7 @@ use runtime_primitives::{generic::BlockId, Justification, StorageOverlay, Childr
use runtime_primitives::traits::{Block as BlockT, NumberFor};
use state_machine::backend::Backend as StateBackend;
use state_machine::ChangesTrieStorage as StateChangesTrieStorage;
use consensus::well_known_cache_keys;
use hash_db::Hasher;
use trie::MemoryDB;
@@ -75,7 +76,7 @@ pub trait BlockImportOperation<Block, H> where
) -> error::Result<()>;
/// Update cached data.
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>>);
/// Inject storage data into the database.
fn update_db_storage(&mut self, update: <Self::State as StateBackend<H>>::Transaction) -> error::Result<()>;
/// Inject storage data into the database replacing any existing data.
+2 -1
View File
@@ -21,6 +21,7 @@ use std::sync::Arc;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor};
use runtime_primitives::generic::BlockId;
use runtime_primitives::Justification;
use consensus::well_known_cache_keys;
use crate::error::{ErrorKind, Result};
@@ -100,7 +101,7 @@ pub trait ProvideCache<Block: BlockT> {
/// Blockchain optional data cache.
pub trait Cache<Block: BlockT>: Send + Sync {
/// Returns cached value by the given key.
fn get_at(&self, key: &[u8], block: &BlockId<Block>) -> Option<Vec<u8>>;
fn get_at(&self, key: &well_known_cache_keys::Id, block: &BlockId<Block>) -> Option<Vec<u8>>;
}
/// Blockchain info
+4 -4
View File
@@ -27,7 +27,7 @@ use runtime_primitives::{
};
use consensus::{
Error as ConsensusError, ErrorKind as ConsensusErrorKind, ImportBlock, ImportResult,
BlockOrigin, ForkChoiceStrategy,
BlockOrigin, ForkChoiceStrategy, well_known_cache_keys::Id as CacheKeyId,
};
use runtime_primitives::traits::{
Block as BlockT, Header as HeaderT, Zero, As, NumberFor, CurrentHeight, BlockNumberToHash,
@@ -672,7 +672,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
&self,
operation: &mut ClientImportOperation<Block, Blake2Hasher, B>,
import_block: ImportBlock<Block>,
new_cache: HashMap<Vec<u8>, Vec<u8>>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> error::Result<ImportResult> where
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + Clone,
{
@@ -743,7 +743,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
import_headers: PrePostHeader<Block::Header>,
justification: Option<Justification>,
body: Option<Vec<Block::Extrinsic>>,
new_cache: HashMap<Vec<u8>, Vec<u8>>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
finalized: bool,
aux: Vec<(Vec<u8>, Option<Vec<u8>>)>,
fork_choice: ForkChoiceStrategy,
@@ -1396,7 +1396,7 @@ impl<B, E, Block, RA> consensus::BlockImport<Block> for Client<B, E, Block, RA>
fn import_block(
&self,
import_block: ImportBlock<Block>,
new_cache: HashMap<Vec<u8>, Vec<u8>>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
self.lock_import_and_run(|operation| {
self.apply_block(operation, import_block, new_cache)
+4 -3
View File
@@ -29,6 +29,7 @@ use state_machine::{self, InMemoryChangesTrieStorage, ChangesTrieAnchorBlockId};
use hash_db::Hasher;
use heapsize::HeapSizeOf;
use trie::MemoryDB;
use consensus::well_known_cache_keys::Id as CacheKeyId;
use crate::error;
use crate::backend::{self, NewBlockState};
@@ -390,7 +391,7 @@ impl<Block: BlockT> light::blockchain::Storage<Block> for Blockchain<Block>
fn import_header(
&self,
header: Block::Header,
_cache: HashMap<Vec<u8>, Vec<u8>>,
_cache: HashMap<CacheKeyId, Vec<u8>>,
state: NewBlockState,
aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
) -> error::Result<()> {
@@ -431,7 +432,7 @@ impl<Block: BlockT> light::blockchain::Storage<Block> for Blockchain<Block>
/// In-memory operation.
pub struct BlockImportOperation<Block: BlockT, H: Hasher> {
pending_block: Option<PendingBlock<Block>>,
pending_cache: HashMap<Vec<u8>, Vec<u8>>,
pending_cache: HashMap<CacheKeyId, Vec<u8>>,
old_state: InMemory<H>,
new_state: Option<InMemory<H>>,
changes_trie_update: Option<MemoryDB<H>>,
@@ -468,7 +469,7 @@ where
Ok(())
}
fn update_cache(&mut self, cache: HashMap<Vec<u8>, Vec<u8>>) {
fn update_cache(&mut self, cache: HashMap<CacheKeyId, Vec<u8>>) {
self.pending_cache = cache;
}
+3 -2
View File
@@ -34,6 +34,7 @@ use crate::light::fetcher::{Fetcher, RemoteReadRequest};
use hash_db::Hasher;
use trie::MemoryDB;
use heapsize::HeapSizeOf;
use consensus::well_known_cache_keys;
const IN_MEMORY_EXPECT_PROOF: &str = "InMemory state backend has Void error type and always suceeds; qed";
@@ -46,7 +47,7 @@ pub struct Backend<S, F, H> {
/// Light block (header and justification) import operation.
pub struct ImportOperation<Block: BlockT, S, F, H> {
header: Option<Block::Header>,
cache: HashMap<Vec<u8>, Vec<u8>>,
cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
leaf_state: NewBlockState,
aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
finalized_blocks: Vec<BlockId<Block>>,
@@ -254,7 +255,7 @@ where
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>>) {
self.cache = cache;
}
@@ -23,6 +23,7 @@ use parking_lot::Mutex;
use runtime_primitives::{Justification, generic::BlockId};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor, Zero};
use consensus::well_known_cache_keys;
use crate::backend::{AuxStore, NewBlockState};
use crate::blockchain::{Backend as BlockchainBackend, BlockStatus, Cache as BlockchainCache,
@@ -40,7 +41,7 @@ pub trait Storage<Block: BlockT>: AuxStore + BlockchainHeaderBackend<Block> {
fn import_header(
&self,
header: Block::Header,
cache: HashMap<Vec<u8>, Vec<u8>>,
cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
state: NewBlockState,
aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
) -> ClientResult<()>;
@@ -252,7 +253,7 @@ pub mod tests {
fn import_header(
&self,
_header: Header,
_cache: HashMap<Vec<u8>, Vec<u8>>,
_cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
_state: NewBlockState,
_aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
) -> ClientResult<()> {
+1 -1
View File
@@ -694,7 +694,7 @@ fn authorities<B, C>(client: &C, at: &BlockId<B>) -> Result<Vec<AuthorityIdFor<B
{
client
.cache()
.and_then(|cache| cache.get_at(well_known_cache_keys::AUTHORITIES, at)
.and_then(|cache| cache.get_at(&well_known_cache_keys::AUTHORITIES, at)
.and_then(|v| Decode::decode(&mut &v[..])))
.or_else(|| client.runtime_api().authorities(at).ok())
.ok_or_else(|| consensus_common::ErrorKind::InvalidAuthoritiesSet.into())
@@ -20,6 +20,7 @@ use runtime_primitives::traits::{Block as BlockT, DigestItemFor, Header as Heade
use runtime_primitives::Justification;
use std::borrow::Cow;
use std::collections::HashMap;
use crate::well_known_cache_keys;
/// Block import result.
#[derive(Debug, PartialEq, Eq)]
@@ -182,7 +183,7 @@ pub trait BlockImport<B: BlockT> {
fn import_block(
&self,
block: ImportBlock<B>,
cache: HashMap<Vec<u8>, Vec<u8>>,
cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
) -> Result<ImportResult, Self::Error>;
}
@@ -570,7 +570,7 @@ pub fn import_single_block<B: BlockT, V: Verifier<B>>(
let mut cache = HashMap::new();
if let Some(authorities) = new_authorities {
cache.insert(crate::well_known_cache_keys::AUTHORITIES.to_vec(), authorities.encode());
cache.insert(crate::well_known_cache_keys::AUTHORITIES, authorities.encode());
}
import_error(import_handle.import_block(import_block, cache))
+4 -1
View File
@@ -120,6 +120,9 @@ impl<T: SyncOracle> SyncOracle for Arc<T> {
/// A list of all well known keys in the cache.
pub mod well_known_cache_keys {
/// The type representing cache keys.
pub type Id = [u8; 4];
/// A list of authorities.
pub const AUTHORITIES: &'static [u8] = b"auth";
pub const AUTHORITIES: Id = *b"auth";
}
@@ -27,7 +27,7 @@ use client::backend::Backend;
use client::runtime_api::ApiExt;
use consensus_common::{
BlockImport, Error as ConsensusError, ErrorKind as ConsensusErrorKind,
ImportBlock, ImportResult, JustificationImport,
ImportBlock, ImportResult, JustificationImport, well_known_cache_keys,
};
use fg_primitives::GrandpaApi;
use runtime_primitives::Justification;
@@ -388,7 +388,7 @@ impl<B, E, Block: BlockT<Hash=H256>, RA, PRA> BlockImport<Block>
{
type Error = ConsensusError;
fn import_block(&self, mut block: ImportBlock<Block>, new_cache: HashMap<Vec<u8>, Vec<u8>>)
fn import_block(&self, mut block: ImportBlock<Block>, new_cache: HashMap<well_known_cache_keys::Id, Vec<u8>>)
-> Result<ImportResult, Self::Error>
{
let hash = block.post_header().hash();