diff --git a/substrate/core/client/db/src/cache/list_cache.rs b/substrate/core/client/db/src/cache/list_cache.rs index 188012e78f..9095b80fb6 100644 --- a/substrate/core/client/db/src/cache/list_cache.rs +++ b/substrate/core/client/db/src/cache/list_cache.rs @@ -52,12 +52,21 @@ use crate::cache::{CacheItemT, ComplexBlockId, EntryType}; use crate::cache::list_entry::{Entry, StorageEntry}; use crate::cache::list_storage::{Storage, StorageTransaction, Metadata}; +/// Pruning strategy. +#[derive(Debug, Clone, Copy)] +pub enum PruningStrategy { + /// Prune entries when they're too far behind best finalized block. + ByDepth(N), + /// Do not prune old entries at all. + NeverPrune, +} + /// List-based cache. pub struct ListCache> { /// Cache storage. storage: S, - /// Prune depth. - prune_depth: NumberFor, + /// Pruning strategy. + pruning_strategy: PruningStrategy>, /// Best finalized block. best_finalized_block: ComplexBlockId, /// Best finalized entry (if exists). @@ -107,7 +116,11 @@ pub enum ForkAppendResult { impl> ListCache { /// Create new db list cache entry. - pub fn new(storage: S, prune_depth: NumberFor, best_finalized_block: ComplexBlockId) -> Self { + pub fn new( + storage: S, + pruning_strategy: PruningStrategy>, + best_finalized_block: ComplexBlockId, + ) -> Self { let (best_finalized_entry, unfinalized) = storage.read_meta() .and_then(|meta| read_forks(&storage, meta)) .unwrap_or_else(|error| { @@ -117,7 +130,7 @@ impl> ListCache ListCache { storage, - prune_depth, + pruning_strategy, best_finalized_block, best_finalized_entry, unfinalized, @@ -362,9 +375,14 @@ impl> ListCache tx: &mut Tx, block: &ComplexBlockId ) { + let prune_depth = match self.pruning_strategy { + PruningStrategy::ByDepth(prune_depth) => prune_depth, + PruningStrategy::NeverPrune => return, + }; + let mut do_pruning = || -> ClientResult<()> { // calculate last ancient block number - let ancient_block = match block.number.checked_sub(&self.prune_depth) { + let ancient_block = match block.number.checked_sub(&prune_depth) { Some(number) => match self.storage.read_id(number)? { Some(hash) => ComplexBlockId::new(hash, number), None => return Ok(()), @@ -669,7 +687,7 @@ pub mod tests { // when block is earlier than best finalized block AND it is not finalized // --- 50 --- // ----------> [100] - assert_eq!(ListCache::<_, u64, _>::new(DummyStorage::new(), 1024, test_id(100)) + assert_eq!(ListCache::<_, u64, _>::new(DummyStorage::new(), PruningStrategy::ByDepth(1024), test_id(100)) .value_at_block(&test_id(50)).unwrap(), None); // when block is earlier than best finalized block AND it is finalized AND value is some // [30] ---- 50 ---> [100] @@ -679,7 +697,7 @@ pub mod tests { .with_id(50, H256::from_low_u64_be(50)) .with_entry(test_id(100), StorageEntry { prev_valid_from: Some(test_id(30)), value: 100 }) .with_entry(test_id(30), StorageEntry { prev_valid_from: None, value: 30 }), - 1024, test_id(100) + PruningStrategy::ByDepth(1024), test_id(100) ).value_at_block(&test_id(50)).unwrap(), Some((test_id(30), Some(test_id(100)), 30))); // when block is the best finalized block AND value is some // ---> [100] @@ -689,7 +707,7 @@ pub mod tests { .with_id(100, H256::from_low_u64_be(100)) .with_entry(test_id(100), StorageEntry { prev_valid_from: Some(test_id(30)), value: 100 }) .with_entry(test_id(30), StorageEntry { prev_valid_from: None, value: 30 }), - 1024, test_id(100) + PruningStrategy::ByDepth(1024), test_id(100) ).value_at_block(&test_id(100)).unwrap(), Some((test_id(100), None, 100))); // when block is parallel to the best finalized block // ---- 100 @@ -700,7 +718,7 @@ pub mod tests { .with_id(50, H256::from_low_u64_be(50)) .with_entry(test_id(100), StorageEntry { prev_valid_from: Some(test_id(30)), value: 100 }) .with_entry(test_id(30), StorageEntry { prev_valid_from: None, value: 30 }), - 1024, test_id(100) + PruningStrategy::ByDepth(1024), test_id(100) ).value_at_block(&ComplexBlockId::new(H256::from_low_u64_be(2), 100)).unwrap(), None); // when block is later than last finalized block AND there are no forks AND finalized value is Some @@ -710,7 +728,7 @@ pub mod tests { .with_meta(Some(test_id(100)), Vec::new()) .with_id(50, H256::from_low_u64_be(50)) .with_entry(test_id(100), StorageEntry { prev_valid_from: Some(test_id(30)), value: 100 }), - 1024, test_id(100) + PruningStrategy::ByDepth(1024), test_id(100) ).value_at_block(&test_id(200)).unwrap(), Some((test_id(100), None, 100))); // when block is later than last finalized block AND there are no matching forks @@ -726,7 +744,7 @@ pub mod tests { .with_header(test_header(3)) .with_header(test_header(4)) .with_header(fork_header(0, 2, 3)), - 1024, test_id(2) + PruningStrategy::ByDepth(1024), test_id(2) ).value_at_block(&fork_id(0, 2, 3)).unwrap(), Some((correct_id(2), None, 2))); // when block is later than last finalized block AND there are no matching forks // AND block is not connected to finalized block @@ -743,7 +761,7 @@ pub mod tests { .with_header(test_header(4)) .with_header(fork_header(0, 1, 3)) .with_header(fork_header(0, 1, 2)), - 1024, test_id(2) + PruningStrategy::ByDepth(1024), test_id(2) ).value_at_block(&fork_id(0, 1, 3)).unwrap(), None); // when block is later than last finalized block AND it appends to unfinalized fork from the end @@ -756,7 +774,7 @@ pub mod tests { .with_entry(correct_id(4), StorageEntry { prev_valid_from: Some(correct_id(2)), value: 4 }) .with_header(test_header(4)) .with_header(test_header(5)), - 1024, test_id(2) + PruningStrategy::ByDepth(1024), test_id(2) ).value_at_block(&correct_id(5)).unwrap(), Some((correct_id(4), None, 4))); // when block is later than last finalized block AND it does not fits unfinalized fork // AND it is connected to the finalized block AND finalized value is Some @@ -771,7 +789,7 @@ pub mod tests { .with_header(test_header(3)) .with_header(test_header(4)) .with_header(fork_header(0, 2, 3)), - 1024, test_id(2) + PruningStrategy::ByDepth(1024), test_id(2) ).value_at_block(&fork_id(0, 2, 3)).unwrap(), Some((correct_id(2), None, 2))); } @@ -781,7 +799,7 @@ pub mod tests { let fin = EntryType::Final; // when trying to insert block < finalized number - assert!(ListCache::new(DummyStorage::new(), 1024, test_id(100)) + assert!(ListCache::new(DummyStorage::new(), PruningStrategy::ByDepth(1024), test_id(100)) .on_block_insert( &mut DummyTransaction::new(), test_id(49), @@ -790,7 +808,7 @@ pub mod tests { nfin, ).unwrap().is_none()); // when trying to insert block @ finalized number - assert!(ListCache::new(DummyStorage::new(), 1024, test_id(100)) + assert!(ListCache::new(DummyStorage::new(), PruningStrategy::ByDepth(1024), test_id(100)) .on_block_insert( &mut DummyTransaction::new(), test_id(99), @@ -805,7 +823,7 @@ pub mod tests { DummyStorage::new() .with_meta(None, vec![test_id(4)]) .with_entry(test_id(4), StorageEntry { prev_valid_from: None, value: 4 }), - 1024, test_id(2) + PruningStrategy::ByDepth(1024), test_id(2) ); cache.unfinalized[0].best_block = Some(test_id(4)); let mut tx = DummyTransaction::new(); @@ -830,7 +848,7 @@ pub mod tests { .with_meta(None, vec![correct_id(4)]) .with_entry(correct_id(4), StorageEntry { prev_valid_from: None, value: 4 }) .with_header(test_header(4)), - 1024, test_id(2) + PruningStrategy::ByDepth(1024), test_id(2) ); let mut tx = DummyTransaction::new(); assert_eq!(cache.on_block_insert(&mut tx, correct_id(4), correct_id(5), Some(4), nfin).unwrap(), @@ -856,7 +874,7 @@ pub mod tests { .with_header(test_header(2)) .with_header(test_header(3)) .with_header(test_header(4)), - 1024, correct_id(2) + PruningStrategy::ByDepth(1024), correct_id(2) ); let mut tx = DummyTransaction::new(); assert_eq!(cache.on_block_insert(&mut tx, correct_id(3), fork_id(0, 3, 4), Some(14), nfin).unwrap(), @@ -871,7 +889,7 @@ pub mod tests { DummyStorage::new() .with_meta(Some(correct_id(2)), vec![]) .with_entry(correct_id(2), StorageEntry { prev_valid_from: None, value: 2 }), - 1024, correct_id(2) + PruningStrategy::ByDepth(1024), correct_id(2) ); let mut tx = DummyTransaction::new(); assert_eq!(cache.on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(2), nfin).unwrap(), None); @@ -884,7 +902,7 @@ pub mod tests { DummyStorage::new() .with_meta(Some(correct_id(2)), vec![]) .with_entry(correct_id(2), StorageEntry { prev_valid_from: None, value: 2 }), - 1024, correct_id(2) + PruningStrategy::ByDepth(1024), correct_id(2) ); let mut tx = DummyTransaction::new(); assert_eq!(cache.on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(3), nfin).unwrap(), @@ -894,7 +912,7 @@ pub mod tests { assert_eq!(*tx.updated_meta(), Some(Metadata { finalized: Some(correct_id(2)), unfinalized: vec![correct_id(3)] })); // when inserting finalized entry AND there are no previous finalized entries - let cache = ListCache::new(DummyStorage::new(), 1024, correct_id(2)); + let cache = ListCache::new(DummyStorage::new(), PruningStrategy::ByDepth(1024), correct_id(2)); let mut tx = DummyTransaction::new(); assert_eq!( cache.on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(3), fin).unwrap(), @@ -912,7 +930,7 @@ pub mod tests { DummyStorage::new() .with_meta(Some(correct_id(2)), vec![]) .with_entry(correct_id(2), StorageEntry { prev_valid_from: None, value: 2 }), - 1024, correct_id(2) + PruningStrategy::ByDepth(1024), correct_id(2) ); let mut tx = DummyTransaction::new(); assert_eq!(cache.on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(2), fin).unwrap(), @@ -940,7 +958,7 @@ pub mod tests { .with_meta(Some(correct_id(2)), vec![fork_id(0, 1, 3)]) .with_entry(correct_id(2), StorageEntry { prev_valid_from: None, value: 2 }) .with_entry(fork_id(0, 1, 3), StorageEntry { prev_valid_from: None, value: 13 }), - 1024, correct_id(2) + PruningStrategy::ByDepth(1024), correct_id(2) ); let mut tx = DummyTransaction::new(); assert_eq!(cache.on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(2), fin).unwrap(), @@ -955,7 +973,7 @@ pub mod tests { .with_meta(Some(correct_id(2)), vec![correct_id(5)]) .with_entry(correct_id(2), StorageEntry { prev_valid_from: None, value: 2 }) .with_entry(correct_id(5), StorageEntry { prev_valid_from: Some(correct_id(2)), value: 5 }), - 1024, correct_id(2) + PruningStrategy::ByDepth(1024), correct_id(2) ); let mut tx = DummyTransaction::new(); assert_eq!(cache.on_block_finalize(&mut tx, correct_id(2), correct_id(3)).unwrap(), @@ -969,7 +987,7 @@ pub mod tests { .with_meta(Some(correct_id(2)), vec![correct_id(5)]) .with_entry(correct_id(2), StorageEntry { prev_valid_from: None, value: 2 }) .with_entry(correct_id(5), StorageEntry { prev_valid_from: Some(correct_id(2)), value: 5 }), - 1024, correct_id(4) + PruningStrategy::ByDepth(1024), correct_id(4) ); let mut tx = DummyTransaction::new(); assert_eq!( @@ -989,7 +1007,7 @@ pub mod tests { .with_meta(Some(correct_id(2)), vec![fork_id(0, 1, 3)]) .with_entry(correct_id(2), StorageEntry { prev_valid_from: None, value: 2 }) .with_entry(fork_id(0, 1, 3), StorageEntry { prev_valid_from: None, value: 13 }), - 1024, correct_id(2) + PruningStrategy::ByDepth(1024), correct_id(2) ); let mut tx = DummyTransaction::new(); assert_eq!(cache.on_block_finalize(&mut tx, correct_id(2), correct_id(3)).unwrap(), @@ -1004,7 +1022,7 @@ pub mod tests { .with_entry(correct_id(2), StorageEntry { prev_valid_from: None, value: 2 }) .with_entry(correct_id(5), StorageEntry { prev_valid_from: Some(correct_id(2)), value: 5 }) .with_entry(correct_id(6), StorageEntry { prev_valid_from: Some(correct_id(5)), value: 6 }), - 1024, correct_id(2) + PruningStrategy::ByDepth(1024), correct_id(2) ); // when new block is appended to unfinalized fork @@ -1043,7 +1061,7 @@ pub mod tests { .with_header(test_header(3)) .with_header(test_header(4)) .with_header(test_header(5)), - 1024, correct_id(0) + PruningStrategy::ByDepth(1024), correct_id(0) ).find_unfinalized_fork(&correct_id(4)).unwrap().unwrap().head.valid_from, correct_id(5)); // --- [2] ---------------> [5] // ----------> [3] ---> 4 @@ -1060,7 +1078,7 @@ pub mod tests { .with_header(fork_header(0, 1, 2)) .with_header(fork_header(0, 1, 3)) .with_header(fork_header(0, 1, 4)), - 1024, correct_id(0) + PruningStrategy::ByDepth(1024), correct_id(0) ).find_unfinalized_fork(&fork_id(0, 1, 4)).unwrap().unwrap().head.valid_from, fork_id(0, 1, 3)); // --- [2] ---------------> [5] // ----------> [3] @@ -1080,7 +1098,7 @@ pub mod tests { .with_header(fork_header(1, 1, 2)) .with_header(fork_header(1, 1, 3)) .with_header(fork_header(1, 1, 4)), - 1024, correct_id(0) + PruningStrategy::ByDepth(1024), correct_id(0) ).find_unfinalized_fork(&fork_id(1, 1, 4)).unwrap().is_none()); } @@ -1341,32 +1359,45 @@ pub mod tests { } #[test] - fn ancient_entries_are_pruned() { - let cache = ListCache::new(DummyStorage::new() - .with_id(10, H256::from_low_u64_be(10)) - .with_id(20, H256::from_low_u64_be(20)) - .with_id(30, H256::from_low_u64_be(30)) - .with_entry(test_id(10), StorageEntry { prev_valid_from: None, value: 10 }) - .with_entry(test_id(20), StorageEntry { prev_valid_from: Some(test_id(10)), value: 20 }) - .with_entry(test_id(30), StorageEntry { prev_valid_from: Some(test_id(20)), value: 30 }), - 10, test_id(9)); - let mut tx = DummyTransaction::new(); + fn ancient_entries_are_pruned_when_pruning_enabled() { + fn do_test(strategy: PruningStrategy) { + let cache = ListCache::new(DummyStorage::new() + .with_id(10, H256::from_low_u64_be(10)) + .with_id(20, H256::from_low_u64_be(20)) + .with_id(30, H256::from_low_u64_be(30)) + .with_entry(test_id(10), StorageEntry { prev_valid_from: None, value: 10 }) + .with_entry(test_id(20), StorageEntry { prev_valid_from: Some(test_id(10)), value: 20 }) + .with_entry(test_id(30), StorageEntry { prev_valid_from: Some(test_id(20)), value: 30 }), + strategy, test_id(9)); + let mut tx = DummyTransaction::new(); - // when finalizing entry #10: no entries pruned - cache.prune_finalized_entries(&mut tx, &test_id(10)); - assert!(tx.removed_entries().is_empty()); - assert!(tx.inserted_entries().is_empty()); - // when finalizing entry #19: no entries pruned - cache.prune_finalized_entries(&mut tx, &test_id(19)); - assert!(tx.removed_entries().is_empty()); - assert!(tx.inserted_entries().is_empty()); - // when finalizing entry #20: no entries pruned - cache.prune_finalized_entries(&mut tx, &test_id(20)); - assert!(tx.removed_entries().is_empty()); - assert!(tx.inserted_entries().is_empty()); - // when finalizing entry #30: entry 10 pruned + entry 20 is truncated - cache.prune_finalized_entries(&mut tx, &test_id(30)); - assert_eq!(*tx.removed_entries(), vec![test_id(10).hash].into_iter().collect()); - assert_eq!(*tx.inserted_entries(), vec![test_id(20).hash].into_iter().collect()); + // when finalizing entry #10: no entries pruned + cache.prune_finalized_entries(&mut tx, &test_id(10)); + assert!(tx.removed_entries().is_empty()); + assert!(tx.inserted_entries().is_empty()); + // when finalizing entry #19: no entries pruned + cache.prune_finalized_entries(&mut tx, &test_id(19)); + assert!(tx.removed_entries().is_empty()); + assert!(tx.inserted_entries().is_empty()); + // when finalizing entry #20: no entries pruned + cache.prune_finalized_entries(&mut tx, &test_id(20)); + assert!(tx.removed_entries().is_empty()); + assert!(tx.inserted_entries().is_empty()); + // when finalizing entry #30: entry 10 pruned + entry 20 is truncated (if pruning is enabled) + cache.prune_finalized_entries(&mut tx, &test_id(30)); + match strategy { + PruningStrategy::NeverPrune => { + assert!(tx.removed_entries().is_empty()); + assert!(tx.inserted_entries().is_empty()); + }, + PruningStrategy::ByDepth(_) => { + assert_eq!(*tx.removed_entries(), vec![test_id(10).hash].into_iter().collect()); + assert_eq!(*tx.inserted_entries(), vec![test_id(20).hash].into_iter().collect()); + }, + } + } + + do_test(PruningStrategy::ByDepth(10)); + do_test(PruningStrategy::NeverPrune) } } diff --git a/substrate/core/client/db/src/cache/mod.rs b/substrate/core/client/db/src/cache/mod.rs index f53eb54ca0..6f7d1bf47d 100644 --- a/substrate/core/client/db/src/cache/mod.rs +++ b/substrate/core/client/db/src/cache/mod.rs @@ -21,15 +21,14 @@ use parking_lot::RwLock; use kvdb::{KeyValueDB, DBTransaction}; -use client::blockchain::Cache as BlockchainCache; +use client::blockchain::{well_known_cache_keys::{self, Id as CacheKeyId}, Cache as BlockchainCache}; use client::error::Result as ClientResult; use codec::{Encode, Decode}; use sr_primitives::generic::BlockId; use sr_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor, Zero}; -use consensus_common::well_known_cache_keys::Id as CacheKeyId; use crate::utils::{self, COLUMN_META, db_err}; -use self::list_cache::ListCache; +use self::list_cache::{ListCache, PruningStrategy}; mod list_cache; mod list_entry; @@ -166,7 +165,7 @@ fn get_cache_helper<'a, Block: BlockT>( cache, }, ), - PRUNE_DEPTH.into(), + cache_pruning_strategy(name), best_finalized_block.clone(), ) }) @@ -335,3 +334,17 @@ impl BlockchainCache for DbCacheSync { } } +/// Get pruning strategy for given cache. +fn cache_pruning_strategy>(cache: CacheKeyId) -> PruningStrategy { + // the cache is mostly used to store data from consensus engines + // this kind of data is only required for non-finalized blocks + // => by default we prune finalized cached entries + + match cache { + // we need to keep changes tries configurations forever (or at least until changes tries, + // that were built using this configuration, are pruned) to make it possible to refer + // to old changes tries + well_known_cache_keys::CHANGES_TRIE_CONFIG => PruningStrategy::NeverPrune, + _ => PruningStrategy::ByDepth(PRUNE_DEPTH.into()), + } +} diff --git a/substrate/core/client/db/src/lib.rs b/substrate/core/client/db/src/lib.rs index 1a0b2331d3..62cc8027c2 100644 --- a/substrate/core/client/db/src/lib.rs +++ b/substrate/core/client/db/src/lib.rs @@ -39,7 +39,7 @@ use std::io; use std::collections::{HashMap, HashSet}; use client::backend::NewBlockState; -use client::blockchain::HeaderBackend; +use client::blockchain::{well_known_cache_keys, HeaderBackend}; use client::ExecutionStrategies; use client::backend::{StorageCollection, ChildStorageCollection}; use codec::{Decode, Encode}; @@ -65,7 +65,6 @@ use crate::utils::{Meta, db_err, meta_keys, read_db, block_id_to_lookup_key, rea 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; diff --git a/substrate/core/client/db/src/light.rs b/substrate/core/client/db/src/light.rs index 2d100dad29..07f805495c 100644 --- a/substrate/core/client/db/src/light.rs +++ b/substrate/core/client/db/src/light.rs @@ -23,8 +23,11 @@ use parking_lot::RwLock; use kvdb::{KeyValueDB, DBTransaction}; use client::backend::{AuxStore, NewBlockState}; -use client::blockchain::{BlockStatus, Cache as BlockchainCache, - HeaderBackend as BlockchainHeaderBackend, Info as BlockchainInfo}; +use client::blockchain::{ + BlockStatus, Cache as BlockchainCache, + HeaderBackend as BlockchainHeaderBackend, Info as BlockchainInfo, + well_known_cache_keys, +}; use client::cht; use client::error::{Error as ClientError, Result as ClientResult}; use client::light::blockchain::Storage as LightBlockchainStorage; @@ -32,7 +35,6 @@ use codec::{Decode, Encode}; use primitives::Blake2Hasher; use sr_primitives::generic::{DigestItem, BlockId}; use sr_primitives::traits::{Block as BlockT, Header as HeaderT, Zero, One, NumberFor}; -use consensus_common::well_known_cache_keys; use crate::cache::{DbCacheSync, DbCache, ComplexBlockId, EntryType as CacheEntryType}; use crate::utils::{self, meta_keys, Meta, db_err, read_db, block_id_to_lookup_key, read_meta}; use crate::DatabaseSettings; diff --git a/substrate/core/client/src/backend.rs b/substrate/core/client/src/backend.rs index e7b5045336..9b6d9ce58f 100644 --- a/substrate/core/client/src/backend.rs +++ b/substrate/core/client/src/backend.rs @@ -25,7 +25,8 @@ use sr_primitives::{generic::BlockId, Justification, StorageOverlay, ChildrenSto use sr_primitives::traits::{Block as BlockT, NumberFor}; use state_machine::backend::Backend as StateBackend; use state_machine::{ChangesTrieStorage as StateChangesTrieStorage, ChangesTrieTransaction}; -use consensus::{well_known_cache_keys, BlockOrigin}; +use crate::blockchain::well_known_cache_keys; +use consensus::BlockOrigin; use hash_db::Hasher; use parking_lot::Mutex; diff --git a/substrate/core/client/src/blockchain.rs b/substrate/core/client/src/blockchain.rs index 9c9c915388..a6edb8505a 100644 --- a/substrate/core/client/src/blockchain.rs +++ b/substrate/core/client/src/blockchain.rs @@ -21,7 +21,6 @@ use std::sync::Arc; use sr_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor}; use sr_primitives::generic::BlockId; use sr_primitives::Justification; -use consensus::well_known_cache_keys; use crate::error::{Error, Result}; @@ -254,3 +253,18 @@ pub fn tree_route) -> Result< { block: StoredBlock, diff --git a/substrate/core/client/src/lib.rs b/substrate/core/client/src/lib.rs index 3919b3970f..636a1e4df6 100644 --- a/substrate/core/client/src/lib.rs +++ b/substrate/core/client/src/lib.rs @@ -124,6 +124,8 @@ pub use crate::notifications::{StorageEventStream, StorageChangeSet}; pub use state_machine::ExecutionStrategy; #[cfg(feature = "std")] pub use crate::leaves::LeafSet; +#[cfg(feature = "std")] +pub use crate::blockchain::well_known_cache_keys; #[doc(inline)] pub use sr_api_macros::{decl_runtime_apis, impl_runtime_apis}; diff --git a/substrate/core/client/src/light/backend.rs b/substrate/core/client/src/light/backend.rs index 5f44c03e21..300d140630 100644 --- a/substrate/core/client/src/light/backend.rs +++ b/substrate/core/client/src/light/backend.rs @@ -29,12 +29,11 @@ use crate::backend::{ AuxStore, Backend as ClientBackend, BlockImportOperation, RemoteBackend, NewBlockState, StorageCollection, ChildStorageCollection, }; -use crate::blockchain::HeaderBackend as BlockchainHeaderBackend; +use crate::blockchain::{HeaderBackend as BlockchainHeaderBackend, well_known_cache_keys}; use crate::error::{Error as ClientError, Result as ClientResult}; use crate::light::blockchain::{Blockchain, Storage as BlockchainStorage}; use hash_db::Hasher; use trie::MemoryDB; -use consensus::well_known_cache_keys; const IN_MEMORY_EXPECT_PROOF: &str = "InMemory state backend has Void error type and always succeeds; qed"; diff --git a/substrate/core/client/src/light/blockchain.rs b/substrate/core/client/src/light/blockchain.rs index 1e1a7669a0..8fef351b7c 100644 --- a/substrate/core/client/src/light/blockchain.rs +++ b/substrate/core/client/src/light/blockchain.rs @@ -22,11 +22,13 @@ use std::{sync::Arc, collections::HashMap}; use sr_primitives::{Justification, generic::BlockId}; use sr_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, - HeaderBackend as BlockchainHeaderBackend, Info as BlockchainInfo, ProvideCache}; +use crate::blockchain::{ + Backend as BlockchainBackend, BlockStatus, Cache as BlockchainCache, + HeaderBackend as BlockchainHeaderBackend, Info as BlockchainInfo, ProvideCache, + well_known_cache_keys, +}; use crate::cht; use crate::error::{Error as ClientError, Result as ClientResult}; use crate::light::fetcher::{Fetcher, RemoteHeaderRequest}; diff --git a/substrate/core/consensus/aura/src/lib.rs b/substrate/core/consensus/aura/src/lib.rs index 585fbc522f..3c60128dd3 100644 --- a/substrate/core/consensus/aura/src/lib.rs +++ b/substrate/core/consensus/aura/src/lib.rs @@ -33,7 +33,7 @@ use std::{sync::Arc, time::Duration, thread, marker::PhantomData, hash::Hash, fm use codec::{Encode, Decode, Codec}; use consensus_common::{self, BlockImport, Environment, Proposer, ForkChoiceStrategy, BlockImportParams, BlockOrigin, Error as ConsensusError, - SelectChain, well_known_cache_keys::{self, Id as CacheKeyId} + SelectChain, }; use consensus_common::import_queue::{ Verifier, BasicQueue, BoxBlockImport, BoxJustificationImport, BoxFinalityProofImport, @@ -41,6 +41,7 @@ use consensus_common::import_queue::{ use client::{ block_builder::api::BlockBuilder as BlockBuilderApi, blockchain::ProvideCache, runtime_api::ApiExt, error::Result as CResult, backend::AuxStore, BlockOf, + well_known_cache_keys::{self, Id as CacheKeyId}, }; use sr_primitives::{generic::{BlockId, OpaqueDigestItemId}, Justification}; diff --git a/substrate/core/consensus/babe/src/lib.rs b/substrate/core/consensus/babe/src/lib.rs index acb66038de..7816f81d47 100644 --- a/substrate/core/consensus/babe/src/lib.rs +++ b/substrate/core/consensus/babe/src/lib.rs @@ -65,7 +65,6 @@ use consensus_common::ImportResult; use consensus_common::import_queue::{ BoxJustificationImport, BoxFinalityProofImport, }; -use consensus_common::well_known_cache_keys::Id as CacheKeyId; use sr_primitives::{generic::{BlockId, OpaqueDigestItemId}, Justification}; use sr_primitives::traits::{ Block as BlockT, Header, DigestItemFor, NumberFor, ProvideRuntimeApi, @@ -96,7 +95,7 @@ use srml_babe::{ BabeInherentData, timestamp::{TimestampInherentData, InherentType as TimestampInherent} }; -use consensus_common::{SelectChain, well_known_cache_keys}; +use consensus_common::SelectChain; use consensus_common::import_queue::{Verifier, BasicQueue}; use client::{ block_builder::api::BlockBuilder as BlockBuilderApi, @@ -104,6 +103,7 @@ use client::{ runtime_api::ApiExt, error::Result as ClientResult, backend::{AuxStore, Backend}, ProvideUncles, utils::is_descendent_of, + well_known_cache_keys::{self, Id as CacheKeyId}, }; use fork_tree::ForkTree; use slots::{CheckedHeader, check_equivocation}; diff --git a/substrate/core/consensus/common/src/block_import.rs b/substrate/core/consensus/common/src/block_import.rs index 02f7979e9c..bcafb352cd 100644 --- a/substrate/core/consensus/common/src/block_import.rs +++ b/substrate/core/consensus/common/src/block_import.rs @@ -21,9 +21,8 @@ use sr_primitives::Justification; use std::borrow::Cow; use std::collections::HashMap; use std::sync::Arc; -use crate::well_known_cache_keys; -use crate::import_queue::Verifier; +use crate::import_queue::{Verifier, CacheKeyId}; /// Block import result. #[derive(Debug, PartialEq, Eq)] @@ -182,7 +181,7 @@ pub trait BlockImport { fn import_block( &mut self, block: BlockImportParams, - cache: HashMap>, + cache: HashMap>, ) -> Result; } @@ -202,7 +201,7 @@ where for<'r> &'r T: BlockImport fn import_block( &mut self, block: BlockImportParams, - cache: HashMap>, + cache: HashMap>, ) -> Result { (&**self).import_block(block, cache) } diff --git a/substrate/core/consensus/common/src/import_queue.rs b/substrate/core/consensus/common/src/import_queue.rs index f4febb5e23..533df2b179 100644 --- a/substrate/core/consensus/common/src/import_queue.rs +++ b/substrate/core/consensus/common/src/import_queue.rs @@ -27,7 +27,7 @@ use std::collections::HashMap; use sr_primitives::{Justification, traits::{Block as BlockT, Header as _, NumberFor}}; -use crate::{error::Error as ConsensusError, well_known_cache_keys::Id as CacheKeyId}; +use crate::error::Error as ConsensusError; use crate::block_import::{ BlockImport, BlockOrigin, BlockImportParams, ImportedAux, JustificationImport, ImportResult, FinalityProofImport, @@ -65,6 +65,9 @@ pub struct IncomingBlock { pub origin: Option, } +/// Type of keys in the blockchain cache that consensus module could use for its needs. +pub type CacheKeyId = [u8; 4]; + /// Verify a justification of a block pub trait Verifier: Send + Sync { /// Verify the given data and return the BlockImportParams and an optional diff --git a/substrate/core/consensus/common/src/lib.rs b/substrate/core/consensus/common/src/lib.rs index 3fd0a0c694..a79bd157e2 100644 --- a/substrate/core/consensus/common/src/lib.rs +++ b/substrate/core/consensus/common/src/lib.rs @@ -117,15 +117,3 @@ where T: ?Sized, for<'r> &'r T: SyncOracle <&T>::is_offline(&mut &**self) } } - -/// 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: Id = *b"auth"; - - /// Current Epoch data. - pub const EPOCH: Id = *b"epch"; -} diff --git a/substrate/core/consensus/pow/src/lib.rs b/substrate/core/consensus/pow/src/lib.rs index 343c7b14c6..8bc0d3593a 100644 --- a/substrate/core/consensus/pow/src/lib.rs +++ b/substrate/core/consensus/pow/src/lib.rs @@ -35,6 +35,7 @@ use std::collections::HashMap; use client::{ BlockOf, blockchain::{HeaderBackend, ProvideCache}, block_builder::api::BlockBuilder as BlockBuilderApi, backend::AuxStore, + well_known_cache_keys::Id as CacheKeyId, }; use sr_primitives::Justification; use sr_primitives::generic::{BlockId, Digest, DigestItem}; @@ -45,7 +46,7 @@ use primitives::H256; use inherents::{InherentDataProviders, InherentData}; use consensus_common::{ BlockImportParams, BlockOrigin, ForkChoiceStrategy, - well_known_cache_keys::Id as CacheKeyId, Environment, Proposer, + Environment, Proposer, }; use consensus_common::import_queue::{BoxBlockImport, BasicQueue, Verifier}; use codec::{Encode, Decode}; diff --git a/substrate/core/finality-grandpa/src/import.rs b/substrate/core/finality-grandpa/src/import.rs index e72914b7c2..8f7124b3d3 100644 --- a/substrate/core/finality-grandpa/src/import.rs +++ b/substrate/core/finality-grandpa/src/import.rs @@ -21,14 +21,14 @@ use codec::Encode; use futures::sync::mpsc; use parking_lot::RwLockWriteGuard; -use client::{blockchain, CallExecutor, Client}; +use client::{blockchain, CallExecutor, Client, well_known_cache_keys}; use client::blockchain::HeaderBackend; use client::backend::Backend; use client::runtime_api::ApiExt; use client::utils::is_descendent_of; use consensus_common::{ BlockImport, Error as ConsensusError, - BlockImportParams, ImportResult, JustificationImport, well_known_cache_keys, + BlockImportParams, ImportResult, JustificationImport, SelectChain, }; use fg_primitives::GrandpaApi; diff --git a/substrate/core/finality-grandpa/src/light_import.rs b/substrate/core/finality-grandpa/src/light_import.rs index f57bcaebed..053daa81a8 100644 --- a/substrate/core/finality-grandpa/src/light_import.rs +++ b/substrate/core/finality-grandpa/src/light_import.rs @@ -24,10 +24,11 @@ use client::{ backend::{AuxStore, Backend, Finalizer}, blockchain::HeaderBackend, error::Error as ClientError, + well_known_cache_keys, }; use codec::{Encode, Decode}; use consensus_common::{ - import_queue::Verifier, well_known_cache_keys, + import_queue::Verifier, BlockOrigin, BlockImport, FinalityProofImport, BlockImportParams, ImportResult, ImportedAux, Error as ConsensusError, }; diff --git a/substrate/core/network/src/test/mod.rs b/substrate/core/network/src/test/mod.rs index 5fd67acd1b..8cceeeaae6 100644 --- a/substrate/core/network/src/test/mod.rs +++ b/substrate/core/network/src/test/mod.rs @@ -28,10 +28,12 @@ use crate::config::build_multiaddr; use log::trace; use crate::chain::FinalityProofProvider; use client::{ - self, ClientInfo, BlockchainEvents, BlockImportNotification, FinalityNotifications, ImportNotifications, - FinalityNotification, LongestChain + self, ClientInfo, BlockchainEvents, BlockImportNotification, + FinalityNotifications, ImportNotifications, + FinalityNotification, LongestChain, + error::Result as ClientResult, + well_known_cache_keys::{self, Id as CacheKeyId}, }; -use client::error::Result as ClientResult; use client::block_builder::BlockBuilder; use client::backend::{AuxStore, Backend, Finalizer}; use crate::config::Roles; @@ -40,7 +42,7 @@ use consensus::import_queue::{ BoxBlockImport, BoxJustificationImport, Verifier, BoxFinalityProofImport, }; use consensus::block_import::{BlockImport, ImportResult}; -use consensus::{Error as ConsensusError, well_known_cache_keys::{self, Id as CacheKeyId}}; +use consensus::Error as ConsensusError; use consensus::{BlockOrigin, ForkChoiceStrategy, BlockImportParams, JustificationImport}; use futures::prelude::*; use futures03::{StreamExt as _, TryStreamExt as _};