diff --git a/substrate/core/client/db/Cargo.toml b/substrate/core/client/db/Cargo.toml index cc6e38d017..2944ab533b 100644 --- a/substrate/core/client/db/Cargo.toml +++ b/substrate/core/client/db/Cargo.toml @@ -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] diff --git a/substrate/core/client/db/src/cache/mod.rs b/substrate/core/client/db/src/cache/mod.rs index 93555f3c15..b5dd45f11d 100644 --- a/substrate/core/client/db/src/cache/mod.rs +++ b/substrate/core/client/db/src/cache/mod.rs @@ -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 CacheItemT for T where T: Clone + Decode + Encode + PartialEq {} /// Database-backed blockchain data cache. pub struct DbCache { - cache_at: HashMap, ListCache, self::list_storage::DbStorage>>, + cache_at: HashMap, self::list_storage::DbStorage>>, db: Arc, key_lookup_column: Option, header_column: Option, @@ -112,7 +113,7 @@ impl DbCache { } /// Creates `ListCache` with the given name or returns a reference to the existing. - fn get_cache(&mut self, name: Vec) -> &mut ListCache, self::list_storage::DbStorage> { + fn get_cache(&mut self, name: CacheKeyId) -> &mut ListCache, self::list_storage::DbStorage> { get_cache_helper( &mut self.cache_at, name, @@ -128,17 +129,17 @@ impl DbCache { // 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, ListCache, self::list_storage::DbStorage>>, - name: Vec, + cache_at: &'a mut HashMap, self::list_storage::DbStorage>>, + name: CacheKeyId, db: &Arc, key_lookup: Option, header: Option, cache: Option, best_finalized_block: &ComplexBlockId, ) -> &'a mut ListCache, 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 { - cache_at_op: HashMap, self::list_cache::CommitOperation>>, + cache_at_op: HashMap>>, best_finalized_block: Option>, } @@ -162,7 +163,7 @@ pub struct DbCacheTransactionOps { pub struct DbCacheTransaction<'a, Block: BlockT> { cache: &'a mut DbCache, tx: &'a mut DBTransaction, - cache_at_op: HashMap, self::list_cache::CommitOperation>>, + cache_at_op: HashMap>>, best_finalized_block: Option>, } @@ -180,7 +181,7 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> { mut self, parent: ComplexBlockId, block: ComplexBlockId, - data_at: HashMap, Vec>, + data_at: HashMap>, is_final: bool, ) -> ClientResult { assert!(self.cache_at_op.is_empty()); @@ -192,8 +193,8 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> { .cloned() .collect::>(); - let mut insert_op = |name: Vec, value: Option>| -> Result<(), client::error::Error> { - let cache = self.cache.get_cache(name.clone()); + let mut insert_op = |name: CacheKeyId, value: Option>| -> 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(pub RwLock>); impl BlockchainCache for DbCacheSync { - fn get_at(&self, key: &[u8], at: &BlockId) -> Option> { + fn get_at(&self, key: &CacheKeyId, at: &BlockId) -> Option> { let cache = self.0.read(); let storage = cache.cache_at.get(key)?.storage(); let db = storage.db(); diff --git a/substrate/core/client/db/src/lib.rs b/substrate/core/client/db/src/lib.rs index dde01bf929..7657bfd396 100644 --- a/substrate/core/client/db/src/lib.rs +++ b/substrate/core/client/db/src/lib.rs @@ -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, Ok(()) } - fn update_cache(&mut self, _cache: HashMap, Vec>) { + fn update_cache(&mut self, _cache: HashMap>) { // Currently cache isn't implemented on full nodes. } diff --git a/substrate/core/client/db/src/light.rs b/substrate/core/client/db/src/light.rs index f064035d51..62b6486f54 100644 --- a/substrate/core/client/db/src/light.rs +++ b/substrate/core/client/db/src/light.rs @@ -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 LightBlockchainStorage for LightStorage fn import_header( &self, header: Block::Header, - cache_at: HashMap, Vec>, + cache_at: HashMap>, leaf_state: NewBlockState, aux_ops: Vec<(Vec, Option>)>, ) -> 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>; @@ -569,7 +569,7 @@ pub(crate) mod tests { pub fn insert_block Header>( db: &LightStorage, - cache: HashMap, Vec>, + cache: HashMap>, header: F, ) -> Hash { let header = header(); @@ -580,7 +580,7 @@ pub(crate) mod tests { fn insert_final_block Header>( db: &LightStorage, - cache: HashMap, Vec>, + cache: HashMap>, header: F, ) -> Hash { let header = header(); @@ -591,7 +591,7 @@ pub(crate) mod tests { fn insert_non_best_block Header>( db: &LightStorage, - cache: HashMap, Vec>, + cache: HashMap>, header: F, ) -> Hash { let header = header(); @@ -828,18 +828,18 @@ pub(crate) mod tests { } } - fn same_authorities() -> HashMap, Vec> { + fn same_authorities() -> HashMap> { HashMap::new() } - fn make_authorities(authorities: Vec) -> HashMap, Vec> { + fn make_authorities(authorities: Vec) -> HashMap> { 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, at: BlockId) -> Option> { - 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]); diff --git a/substrate/core/client/src/backend.rs b/substrate/core/client/src/backend.rs index 864d04ed61..8a6ffe4384 100644 --- a/substrate/core/client/src/backend.rs +++ b/substrate/core/client/src/backend.rs @@ -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 where ) -> error::Result<()>; /// Update cached data. - fn update_cache(&mut self, cache: HashMap, Vec>); + fn update_cache(&mut self, cache: HashMap>); /// Inject storage data into the database. fn update_db_storage(&mut self, update: >::Transaction) -> error::Result<()>; /// Inject storage data into the database replacing any existing data. diff --git a/substrate/core/client/src/blockchain.rs b/substrate/core/client/src/blockchain.rs index 682d686bd5..5d7b2a9c23 100644 --- a/substrate/core/client/src/blockchain.rs +++ b/substrate/core/client/src/blockchain.rs @@ -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 { /// Blockchain optional data cache. pub trait Cache: Send + Sync { /// Returns cached value by the given key. - fn get_at(&self, key: &[u8], block: &BlockId) -> Option>; + fn get_at(&self, key: &well_known_cache_keys::Id, block: &BlockId) -> Option>; } /// Blockchain info diff --git a/substrate/core/client/src/client.rs b/substrate/core/client/src/client.rs index 7ab66cbc09..041933cfff 100644 --- a/substrate/core/client/src/client.rs +++ b/substrate/core/client/src/client.rs @@ -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 Client where &self, operation: &mut ClientImportOperation, import_block: ImportBlock, - new_cache: HashMap, Vec>, + new_cache: HashMap>, ) -> error::Result where E: CallExecutor + Send + Sync + Clone, { @@ -743,7 +743,7 @@ impl Client where import_headers: PrePostHeader, justification: Option, body: Option>, - new_cache: HashMap, Vec>, + new_cache: HashMap>, finalized: bool, aux: Vec<(Vec, Option>)>, fork_choice: ForkChoiceStrategy, @@ -1396,7 +1396,7 @@ impl consensus::BlockImport for Client fn import_block( &self, import_block: ImportBlock, - new_cache: HashMap, Vec>, + new_cache: HashMap>, ) -> Result { self.lock_import_and_run(|operation| { self.apply_block(operation, import_block, new_cache) diff --git a/substrate/core/client/src/in_mem.rs b/substrate/core/client/src/in_mem.rs index d272135628..29256169f9 100644 --- a/substrate/core/client/src/in_mem.rs +++ b/substrate/core/client/src/in_mem.rs @@ -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 light::blockchain::Storage for Blockchain fn import_header( &self, header: Block::Header, - _cache: HashMap, Vec>, + _cache: HashMap>, state: NewBlockState, aux_ops: Vec<(Vec, Option>)>, ) -> error::Result<()> { @@ -431,7 +432,7 @@ impl light::blockchain::Storage for Blockchain /// In-memory operation. pub struct BlockImportOperation { pending_block: Option>, - pending_cache: HashMap, Vec>, + pending_cache: HashMap>, old_state: InMemory, new_state: Option>, changes_trie_update: Option>, @@ -468,7 +469,7 @@ where Ok(()) } - fn update_cache(&mut self, cache: HashMap, Vec>) { + fn update_cache(&mut self, cache: HashMap>) { self.pending_cache = cache; } diff --git a/substrate/core/client/src/light/backend.rs b/substrate/core/client/src/light/backend.rs index c62a89ce9d..b4b805f3c6 100644 --- a/substrate/core/client/src/light/backend.rs +++ b/substrate/core/client/src/light/backend.rs @@ -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 { /// Light block (header and justification) import operation. pub struct ImportOperation { header: Option, - cache: HashMap, Vec>, + cache: HashMap>, leaf_state: NewBlockState, aux_ops: Vec<(Vec, Option>)>, finalized_blocks: Vec>, @@ -254,7 +255,7 @@ where Ok(()) } - fn update_cache(&mut self, cache: HashMap, Vec>) { + fn update_cache(&mut self, cache: HashMap>) { self.cache = cache; } diff --git a/substrate/core/client/src/light/blockchain.rs b/substrate/core/client/src/light/blockchain.rs index cf6304253a..e081c14d1a 100644 --- a/substrate/core/client/src/light/blockchain.rs +++ b/substrate/core/client/src/light/blockchain.rs @@ -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: AuxStore + BlockchainHeaderBackend { fn import_header( &self, header: Block::Header, - cache: HashMap, Vec>, + cache: HashMap>, state: NewBlockState, aux_ops: Vec<(Vec, Option>)>, ) -> ClientResult<()>; @@ -252,7 +253,7 @@ pub mod tests { fn import_header( &self, _header: Header, - _cache: HashMap, Vec>, + _cache: HashMap>, _state: NewBlockState, _aux_ops: Vec<(Vec, Option>)>, ) -> ClientResult<()> { diff --git a/substrate/core/consensus/aura/src/lib.rs b/substrate/core/consensus/aura/src/lib.rs index d3ea731795..fd33228b66 100644 --- a/substrate/core/consensus/aura/src/lib.rs +++ b/substrate/core/consensus/aura/src/lib.rs @@ -694,7 +694,7 @@ fn authorities(client: &C, at: &BlockId) -> Result { fn import_block( &self, block: ImportBlock, - cache: HashMap, Vec>, + cache: HashMap>, ) -> Result; } diff --git a/substrate/core/consensus/common/src/import_queue.rs b/substrate/core/consensus/common/src/import_queue.rs index 1d0a52d324..7a418ae9f4 100644 --- a/substrate/core/consensus/common/src/import_queue.rs +++ b/substrate/core/consensus/common/src/import_queue.rs @@ -570,7 +570,7 @@ pub fn import_single_block>( 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)) diff --git a/substrate/core/consensus/common/src/lib.rs b/substrate/core/consensus/common/src/lib.rs index c1308f2da9..134a34454e 100644 --- a/substrate/core/consensus/common/src/lib.rs +++ b/substrate/core/consensus/common/src/lib.rs @@ -120,6 +120,9 @@ impl SyncOracle for Arc { /// 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"; } diff --git a/substrate/core/finality-grandpa/src/import.rs b/substrate/core/finality-grandpa/src/import.rs index 7914660e29..5fa20fc2f6 100644 --- a/substrate/core/finality-grandpa/src/import.rs +++ b/substrate/core/finality-grandpa/src/import.rs @@ -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, RA, PRA> BlockImport { type Error = ConsensusError; - fn import_block(&self, mut block: ImportBlock, new_cache: HashMap, Vec>) + fn import_block(&self, mut block: ImportBlock, new_cache: HashMap>) -> Result { let hash = block.post_header().hash();