diff --git a/Cargo.lock b/Cargo.lock index 2888776ab6144..8963f0a19d94e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5231,6 +5231,7 @@ dependencies = [ "sp-state-machine 0.8.0", "sp-trie 2.0.0", "substrate-test-runtime-client 2.0.0", + "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -7074,6 +7075,15 @@ name = "target_info" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "tempdir" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tempfile" version = "3.1.0" @@ -8697,6 +8707,7 @@ dependencies = [ "checksum take_mut 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60" "checksum target-lexicon 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f4c118a7a38378f305a9e111fcb2f7f838c0be324bfb31a77ea04f7f6e684b4" "checksum target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c63f48baada5c52e65a29eef93ab4f8982681b67f9e8d29c7b05abcfec2b9ffe" +"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" "checksum termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "96d6098003bde162e4277c70665bd87c326f5a0c3f3fbfb285787fa482d54e6e" "checksum test-case 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a605baa797821796a751f4a959e1206079b24a4b7e1ed302b7d785d81a9276c9" diff --git a/client/api/src/backend.rs b/client/api/src/backend.rs index 292031a4cb9de..a1bed27a271bb 100644 --- a/client/api/src/backend.rs +++ b/client/api/src/backend.rs @@ -18,11 +18,11 @@ use std::sync::Arc; use std::collections::HashMap; -use sp_core::ChangesTrieConfiguration; +use sp_core::ChangesTrieConfigurationRange; use sp_core::offchain::OffchainStorage; use sp_runtime::{generic::BlockId, Justification, Storage}; use sp_runtime::traits::{Block as BlockT, NumberFor, HasherFor}; -use sp_state_machine::{ChangesTrieStorage as StateChangesTrieStorage, ChangesTrieTransaction}; +use sp_state_machine::{ChangesTrieState, ChangesTrieStorage as StateChangesTrieStorage, ChangesTrieTransaction}; use crate::{ blockchain::{ Backend as BlockchainBackend, well_known_cache_keys @@ -248,8 +248,6 @@ pub trait Backend: AuxStore + Send + Sync { type Blockchain: BlockchainBackend; /// Associated state backend type. type State: StateBackend> + Send; - /// Changes trie storage. - type ChangesTrieStorage: PrunableStateChangesTrieStorage; /// Offchain workers local storage. type OffchainStorage: OffchainStorage; @@ -284,7 +282,7 @@ pub trait Backend: AuxStore + Send + Sync { fn usage_info(&self) -> Option; /// Returns reference to changes trie storage. - fn changes_trie_storage(&self) -> Option<&Self::ChangesTrieStorage>; + fn changes_trie_storage(&self) -> Option<&dyn PrunableStateChangesTrieStorage>; /// Returns a handle to offchain storage. fn offchain_storage(&self) -> Option; @@ -342,12 +340,16 @@ pub trait Backend: AuxStore + Send + Sync { pub trait PrunableStateChangesTrieStorage: StateChangesTrieStorage, NumberFor> { - /// Get number block of oldest, non-pruned changes trie. - fn oldest_changes_trie_block( - &self, - config: &ChangesTrieConfiguration, - best_finalized: NumberFor, - ) -> NumberFor; + /// Get reference to StateChangesTrieStorage. + fn storage(&self) -> &dyn StateChangesTrieStorage, NumberFor>; + /// Get configuration at given block. + fn configuration_at(&self, at: &BlockId) -> sp_blockchain::Result< + ChangesTrieConfigurationRange, Block::Hash> + >; + /// Get end block (inclusive) of oldest pruned max-level (or skewed) digest trie blocks range. + /// It is guaranteed that we have no any changes tries before (and including) this block. + /// It is guaranteed that all existing changes tries after this block are not yet pruned (if created). + fn oldest_pruned_digest_range_end(&self) -> NumberFor; } /// Mark for all Backend implementations, that are making use of state data, stored locally. @@ -364,3 +366,20 @@ pub trait RemoteBackend: Backend { /// locally, or prepares request to fetch that data from remote node. fn remote_blockchain(&self) -> Arc>; } + +/// Return changes tries state at given block. +pub fn changes_tries_state_at_block<'a, Block: BlockT>( + block: &BlockId, + maybe_storage: Option<&'a dyn PrunableStateChangesTrieStorage>, +) -> sp_blockchain::Result, NumberFor>>> { + let storage = match maybe_storage { + Some(storage) => storage, + None => return Ok(None), + }; + + let config_range = storage.configuration_at(block)?; + match config_range.config { + Some(config) => Ok(Some(ChangesTrieState::new(config, config_range.zero.0, storage.storage()))), + None => Ok(None), + } +} diff --git a/client/api/src/light.rs b/client/api/src/light.rs index fb3aeeab7c733..fc9b6dc6fd28c 100644 --- a/client/api/src/light.rs +++ b/client/api/src/light.rs @@ -26,7 +26,7 @@ use sp_runtime::{ }, generic::BlockId }; -use sp_core::ChangesTrieConfiguration; +use sp_core::ChangesTrieConfigurationRange; use sp_state_machine::StorageProof; use sp_blockchain::{ HeaderMetadata, well_known_cache_keys, HeaderBackend, Cache as BlockchainCache, @@ -96,8 +96,8 @@ pub struct RemoteReadChildRequest { /// Remote key changes read request. #[derive(Clone, Debug, PartialEq, Eq)] pub struct RemoteChangesRequest { - /// Changes trie configuration. - pub changes_trie_config: ChangesTrieConfiguration, + /// All changes trie configurations that are valid within [first_block; last_block]. + pub changes_trie_configs: Vec>, /// Query changes from range of blocks, starting (and including) with this hash... pub first_block: (Header::Number, Header::Hash), /// ...ending (and including) with this hash. Should come after first_block and diff --git a/client/authority-discovery/src/tests.rs b/client/authority-discovery/src/tests.rs index 07e1c943be638..77ed6a1d948e2 100644 --- a/client/authority-discovery/src/tests.rs +++ b/client/authority-discovery/src/tests.rs @@ -212,12 +212,10 @@ impl ApiExt for RuntimeApi { unimplemented!("Not required for testing!") } - fn into_storage_changes< - T: sp_api::ChangesTrieStorage, sp_api::NumberFor> - >( + fn into_storage_changes( &self, _: &Self::StateBackend, - _: Option<&T>, + _: Option<&sp_api::ChangesTrieState, sp_api::NumberFor>>, _: ::Hash, ) -> std::result::Result, String> where Self: Sized diff --git a/client/basic-authorship/src/basic_authorship.rs b/client/basic-authorship/src/basic_authorship.rs index 576009ef27487..d13000051a6e4 100644 --- a/client/basic-authorship/src/basic_authorship.rs +++ b/client/basic-authorship/src/basic_authorship.rs @@ -374,9 +374,12 @@ mod tests { api.execute_block(&block_id, proposal.block).unwrap(); let state = backend.state_at(block_id).unwrap(); - let changes_trie_storage = backend.changes_trie_storage(); + let changes_trie_state = backend::changes_tries_state_at_block( + &block_id, + backend.changes_trie_storage(), + ).unwrap(); - let storage_changes = api.into_storage_changes(&state, changes_trie_storage, genesis_hash) + let storage_changes = api.into_storage_changes(&state, changes_trie_state.as_ref(), genesis_hash) .unwrap(); assert_eq!( diff --git a/client/block-builder/src/lib.rs b/client/block-builder/src/lib.rs index f59f88f5ba237..9b403dead4415 100644 --- a/client/block-builder/src/lib.rs +++ b/client/block-builder/src/lib.rs @@ -191,14 +191,17 @@ where let proof = self.api.extract_proof(); let state = self.backend.state_at(self.block_id)?; - let changes_trie_storage = self.backend.changes_trie_storage(); + let changes_trie_state = backend::changes_tries_state_at_block( + &self.block_id, + self.backend.changes_trie_storage(), + )?; let parent_hash = self.parent_hash; // The unsafe is required because the consume requires that we drop/consume the inner api // (what we do here). let storage_changes = self.api.into_storage_changes( &state, - changes_trie_storage, + changes_trie_state.as_ref(), parent_hash, ); diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 61568cea8b660..95dc08afaf5ef 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -675,19 +675,21 @@ fn initialize_authorities_cache(client: &C) -> Result<(), ConsensusErro }; // check if we already have initialized the cache + let map_err = |error| sp_consensus::Error::from(sp_consensus::Error::ClientImport( + format!( + "Error initializing authorities cache: {}", + error, + ))); + let genesis_id = BlockId::Number(Zero::zero()); let genesis_authorities: Option> = cache .get_at(&well_known_cache_keys::AUTHORITIES, &genesis_id) + .unwrap_or(None) .and_then(|(_, _, v)| Decode::decode(&mut &v[..]).ok()); if genesis_authorities.is_some() { return Ok(()); } - let map_err = |error| sp_consensus::Error::from(sp_consensus::Error::ClientImport( - format!( - "Error initializing authorities cache: {}", - error, - ))); let genesis_authorities = authorities(client, &genesis_id)?; cache.initialize(&well_known_cache_keys::AUTHORITIES, genesis_authorities.encode()) .map_err(map_err)?; @@ -706,6 +708,7 @@ fn authorities(client: &C, at: &BlockId) -> Result, Consensus .cache() .and_then(|cache| cache .get_at(&well_known_cache_keys::AUTHORITIES, at) + .unwrap_or(None) .and_then(|(_, _, v)| Decode::decode(&mut &v[..]).ok()) ) .or_else(|| AuraApi::authorities(&*client.runtime_api(), at).ok()) diff --git a/client/db/Cargo.toml b/client/db/Cargo.toml index 6b08d92db91c4..dcc7df29ca11c 100644 --- a/client/db/Cargo.toml +++ b/client/db/Cargo.toml @@ -31,6 +31,8 @@ sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } env_logger = "0.7.0" quickcheck = "0.9" +kvdb-rocksdb = "0.4" +tempdir = "0.3" [features] default = [] diff --git a/client/db/src/cache/list_cache.rs b/client/db/src/cache/list_cache.rs index 6345486f17f94..72278a1e85e6d 100644 --- a/client/db/src/cache/list_cache.rs +++ b/client/db/src/cache/list_cache.rs @@ -94,6 +94,12 @@ pub enum CommitOperation { BlockReverted(BTreeMap>>), } +/// A set of commit operations. +#[derive(Debug)] +pub struct CommitOperations { + operations: Vec>, +} + /// Single fork of list-based cache. #[derive(Debug)] #[cfg_attr(test, derive(PartialEq))] @@ -123,21 +129,17 @@ impl> ListCache storage: S, pruning_strategy: PruningStrategy>, best_finalized_block: ComplexBlockId, - ) -> Self { + ) -> ClientResult { let (best_finalized_entry, unfinalized) = storage.read_meta() - .and_then(|meta| read_forks(&storage, meta)) - .unwrap_or_else(|error| { - warn!(target: "db", "Unable to initialize list cache: {}. Restarting", error); - (None, Vec::new()) - }); + .and_then(|meta| read_forks(&storage, meta))?; - ListCache { + Ok(ListCache { storage, pruning_strategy, best_finalized_block, best_finalized_entry, unfinalized, - } + }) } /// Get reference to the storage. @@ -145,6 +147,12 @@ impl> ListCache &self.storage } + /// Get unfinalized forks reference. + #[cfg(test)] + pub fn unfinalized(&self) -> &[Fork] { + &self.unfinalized + } + /// Get value valid at block. pub fn value_at_block( &self, @@ -156,8 +164,8 @@ impl> ListCache // BUT since we're not guaranteeing to provide correct values for forks // behind the finalized block, check if the block is finalized first - if !chain::is_finalized_block(&self.storage, at, Bounded::max_value())? { - return Ok(None); + if !chain::is_finalized_block(&self.storage, &at, Bounded::max_value())? { + return Err(ClientError::NotInFinalizedChain); } self.best_finalized_entry.as_ref() @@ -171,11 +179,14 @@ impl> ListCache // IF there's no matching fork, ensure that this isn't a block from a fork that has forked // behind the best finalized block and search at finalized fork - match self.find_unfinalized_fork(at)? { + match self.find_unfinalized_fork(&at)? { Some(fork) => Some(&fork.head), None => match self.best_finalized_entry.as_ref() { - Some(best_finalized_entry) if chain::is_connected_to_block(&self.storage, &best_finalized_entry.valid_from, at)? => - Some(best_finalized_entry), + Some(best_finalized_entry) if chain::is_connected_to_block( + &self.storage, + &at, + &best_finalized_entry.valid_from, + )? => Some(best_finalized_entry), _ => None, }, } @@ -198,9 +209,98 @@ impl> ListCache block: ComplexBlockId, value: Option, entry_type: EntryType, + operations: &mut CommitOperations, + ) -> ClientResult<()> { + Ok(operations.append(self.do_on_block_insert(tx, parent, block, value, entry_type, operations)?)) + } + + /// When previously inserted block is finalized. + pub fn on_block_finalize>( + &self, + tx: &mut Tx, + parent: ComplexBlockId, + block: ComplexBlockId, + operations: &mut CommitOperations, + ) -> ClientResult<()> { + Ok(operations.append(self.do_on_block_finalize(tx, parent, block, operations)?)) + } + + /// When block is reverted. + pub fn on_block_revert>( + &self, + tx: &mut Tx, + reverted_block: &ComplexBlockId, + operations: &mut CommitOperations, + ) -> ClientResult<()> { + Ok(operations.append(Some(self.do_on_block_revert(tx, reverted_block)?))) + } + + /// When transaction is committed. + pub fn on_transaction_commit(&mut self, ops: CommitOperations) { + for op in ops.operations { + match op { + CommitOperation::AppendNewBlock(index, best_block) => { + let mut fork = self.unfinalized.get_mut(index) + .expect("ListCache is a crate-private type; + internal clients of ListCache are committing transaction while cache is locked; + CommitOperation holds valid references while cache is locked; qed"); + fork.best_block = Some(best_block); + }, + CommitOperation::AppendNewEntry(index, entry) => { + let mut fork = self.unfinalized.get_mut(index) + .expect("ListCache is a crate-private type; + internal clients of ListCache are committing transaction while cache is locked; + CommitOperation holds valid references while cache is locked; qed"); + fork.best_block = Some(entry.valid_from.clone()); + fork.head = entry; + }, + CommitOperation::AddNewFork(entry) => { + self.unfinalized.push(Fork { + best_block: Some(entry.valid_from.clone()), + head: entry, + }); + }, + CommitOperation::BlockFinalized(block, finalizing_entry, forks) => { + self.best_finalized_block = block; + if let Some(finalizing_entry) = finalizing_entry { + self.best_finalized_entry = Some(finalizing_entry); + } + for fork_index in forks.iter().rev() { + self.unfinalized.remove(*fork_index); + } + }, + CommitOperation::BlockReverted(forks) => { + for (fork_index, updated_fork) in forks.into_iter().rev() { + match updated_fork { + Some(updated_fork) => self.unfinalized[fork_index] = updated_fork, + None => { self.unfinalized.remove(fork_index); }, + } + } + }, + } + } + } + + fn do_on_block_insert>( + &self, + tx: &mut Tx, + parent: ComplexBlockId, + block: ComplexBlockId, + value: Option, + entry_type: EntryType, + operations: &CommitOperations, ) -> ClientResult>> { // this guarantee is currently provided by LightStorage && we're relying on it here - debug_assert!(entry_type != EntryType::Final || self.best_finalized_block.hash == parent.hash); + let prev_operation = operations.operations.last(); + debug_assert!( + entry_type != EntryType::Final || + self.best_finalized_block.hash == parent.hash || + match prev_operation { + Some(&CommitOperation::BlockFinalized(ref best_finalized_block, _, _)) + => best_finalized_block.hash == parent.hash, + _ => false, + } + ); // we do not store any values behind finalized if block.number != Zero::zero() && self.best_finalized_block.number >= block.number { @@ -296,7 +396,7 @@ impl> ListCache } // cleanup database from abandoned unfinalized forks and obsolete finalized entries - let abandoned_forks = self.destroy_abandoned_forks(tx, &block); + let abandoned_forks = self.destroy_abandoned_forks(tx, &block, prev_operation); self.prune_finalized_entries(tx, &block); match new_storage_entry { @@ -310,34 +410,39 @@ impl> ListCache } } - /// When previously inserted block is finalized. - pub fn on_block_finalize>( + fn do_on_block_finalize>( &self, tx: &mut Tx, parent: ComplexBlockId, block: ComplexBlockId, + operations: &CommitOperations, ) -> ClientResult>> { - // this guarantee is currently provided by LightStorage && we're relying on it here - debug_assert_eq!(self.best_finalized_block.hash, parent.hash); + // this guarantee is currently provided by db backend && we're relying on it here + let prev_operation = operations.operations.last(); + debug_assert!( + self.best_finalized_block.hash == parent.hash || + match prev_operation { + Some(&CommitOperation::BlockFinalized(ref best_finalized_block, _, _)) + => best_finalized_block.hash == parent.hash, + _ => false, + } + ); // there could be at most one entry that is finalizing let finalizing_entry = self.storage.read_entry(&block)? .map(|entry| entry.into_entry(block.clone())); // cleanup database from abandoned unfinalized forks and obsolete finalized entries - let abandoned_forks = self.destroy_abandoned_forks(tx, &block); + let abandoned_forks = self.destroy_abandoned_forks(tx, &block, prev_operation); self.prune_finalized_entries(tx, &block); - let update_meta = finalizing_entry.is_some(); let operation = CommitOperation::BlockFinalized(block, finalizing_entry, abandoned_forks); - if update_meta { - tx.update_meta(self.best_finalized_entry.as_ref(), &self.unfinalized, &operation); - } + tx.update_meta(self.best_finalized_entry.as_ref(), &self.unfinalized, &operation); + Ok(Some(operation)) } - /// When block is reverted. - pub fn on_block_revert>( + fn do_on_block_revert>( &self, tx: &mut Tx, reverted_block: &ComplexBlockId, @@ -374,50 +479,6 @@ impl> ListCache Ok(operation) } - /// When transaction is committed. - pub fn on_transaction_commit(&mut self, op: CommitOperation) { - match op { - CommitOperation::AppendNewBlock(index, best_block) => { - let mut fork = self.unfinalized.get_mut(index) - .expect("ListCache is a crate-private type; - internal clients of ListCache are committing transaction while cache is locked; - CommitOperation holds valid references while cache is locked; qed"); - fork.best_block = Some(best_block); - }, - CommitOperation::AppendNewEntry(index, entry) => { - let mut fork = self.unfinalized.get_mut(index) - .expect("ListCache is a crate-private type; - internal clients of ListCache are committing transaction while cache is locked; - CommitOperation holds valid references while cache is locked; qed"); - fork.best_block = Some(entry.valid_from.clone()); - fork.head = entry; - }, - CommitOperation::AddNewFork(entry) => { - self.unfinalized.push(Fork { - best_block: Some(entry.valid_from.clone()), - head: entry, - }); - }, - CommitOperation::BlockFinalized(block, finalizing_entry, forks) => { - self.best_finalized_block = block; - if let Some(finalizing_entry) = finalizing_entry { - self.best_finalized_entry = Some(finalizing_entry); - } - for fork_index in forks.iter().rev() { - self.unfinalized.remove(*fork_index); - } - }, - CommitOperation::BlockReverted(forks) => { - for (fork_index, updated_fork) in forks.into_iter().rev() { - match updated_fork { - Some(updated_fork) => self.unfinalized[fork_index] = updated_fork, - None => { self.unfinalized.remove(fork_index); }, - } - } - }, - } - } - /// Prune old finalized entries. fn prune_finalized_entries>( &self, @@ -475,10 +536,22 @@ impl> ListCache fn destroy_abandoned_forks>( &self, tx: &mut Tx, - block: &ComplexBlockId + block: &ComplexBlockId, + prev_operation: Option<&CommitOperation>, ) -> BTreeSet { - let mut destroyed = BTreeSet::new(); - for (index, fork) in self.unfinalized.iter().enumerate() { + // if some block has been finalized already => take it into account + let prev_abandoned_forks = match prev_operation { + Some(&CommitOperation::BlockFinalized(_, _, ref abandoned_forks)) => Some(abandoned_forks), + _ => None, + }; + + let mut destroyed = prev_abandoned_forks.cloned().unwrap_or_else(|| BTreeSet::new()); + let live_unfinalized = self.unfinalized.iter() + .enumerate() + .filter(|(idx, _)| prev_abandoned_forks + .map(|prev_abandoned_forks| !prev_abandoned_forks.contains(idx)) + .unwrap_or(true)); + for (index, fork) in live_unfinalized { if fork.head.valid_from.number == block.number { destroyed.insert(index); if fork.head.valid_from.hash != block.hash { @@ -493,7 +566,10 @@ impl> ListCache } /// Search unfinalized fork where given block belongs. - fn find_unfinalized_fork(&self, block: &ComplexBlockId) -> ClientResult>> { + fn find_unfinalized_fork( + &self, + block: &ComplexBlockId, + ) -> ClientResult>> { for unfinalized in &self.unfinalized { if unfinalized.matches(&self.storage, block)? { return Ok(Some(&unfinalized)); @@ -549,7 +625,7 @@ impl Fork { }; // check if the parent is connected to the beginning of the range - if !chain::is_connected_to_block(storage, &parent, &begin)? { + if !chain::is_connected_to_block(storage, parent, &begin)? { return Ok(None); } @@ -621,6 +697,65 @@ impl Fork { } } +impl Default for CommitOperations { + fn default() -> Self { + CommitOperations { operations: Vec::new() } + } +} + +// This should never be allowed for non-test code to avoid revealing its internals. +#[cfg(test)] +impl From>> for CommitOperations { + fn from(operations: Vec>) -> Self { + CommitOperations { operations } + } +} + +impl CommitOperations { + /// Append operation to the set. + fn append(&mut self, new_operation: Option>) { + let new_operation = match new_operation { + Some(new_operation) => new_operation, + None => return, + }; + + let last_operation = match self.operations.pop() { + Some(last_operation) => last_operation, + None => { + self.operations.push(new_operation); + return; + }, + }; + + // we are able (and obliged to) to merge two consequent block finalization operations + match last_operation { + CommitOperation::BlockFinalized(old_finalized_block, old_finalized_entry, old_abandoned_forks) => { + match new_operation { + CommitOperation::BlockFinalized(new_finalized_block, new_finalized_entry, new_abandoned_forks) => { + self.operations.push(CommitOperation::BlockFinalized( + new_finalized_block, + new_finalized_entry, + new_abandoned_forks, + )); + }, + _ => { + self.operations.push(CommitOperation::BlockFinalized( + old_finalized_block, + old_finalized_entry, + old_abandoned_forks, + )); + self.operations.push(new_operation); + }, + } + }, + _ => { + self.operations.push(last_operation); + self.operations.push(new_operation); + }, + } + } +} + /// Destroy fork by deleting all unfinalized entries. pub fn destroy_fork, Tx: StorageTransaction>( head_valid_from: ComplexBlockId, @@ -674,7 +809,7 @@ mod chain { block1: &ComplexBlockId, block2: &ComplexBlockId, ) -> ClientResult { - let (begin, end) = if block1 > block2 { (block2, block1) } else { (block1, block2) }; + let (begin, end) = if *block1 > *block2 { (block2, block1) } else { (block1, block2) }; let mut current = storage.read_header(&end.hash)? .ok_or_else(|| ClientError::UnknownBlock(format!("{}", end.hash)))?; while *current.number() > begin.number { @@ -773,8 +908,8 @@ 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(), PruningStrategy::ByDepth(1024), test_id(100)) - .value_at_block(&test_id(50)).unwrap(), None); + assert!(ListCache::<_, u64, _>::new(DummyStorage::new(), PruningStrategy::ByDepth(1024), test_id(100)) + .unwrap().value_at_block(&test_id(50)).is_err()); // when block is earlier than best finalized block AND it is finalized AND value is some // [30] ---- 50 ---> [100] assert_eq!(ListCache::new( @@ -784,7 +919,7 @@ pub mod tests { .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 }), PruningStrategy::ByDepth(1024), test_id(100) - ).value_at_block(&test_id(50)).unwrap(), Some((test_id(30), Some(test_id(100)), 30))); + ).unwrap().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] assert_eq!(ListCache::new( @@ -794,18 +929,18 @@ pub mod tests { .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 }), PruningStrategy::ByDepth(1024), test_id(100) - ).value_at_block(&test_id(100)).unwrap(), Some((test_id(100), None, 100))); + ).unwrap().value_at_block(&test_id(100)).unwrap(), Some((test_id(100), None, 100))); // when block is parallel to the best finalized block // ---- 100 // ---> [100] - assert_eq!(ListCache::new( + assert!(ListCache::new( DummyStorage::new() .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 }) .with_entry(test_id(30), StorageEntry { prev_valid_from: None, value: 30 }), PruningStrategy::ByDepth(1024), test_id(100) - ).value_at_block(&ComplexBlockId::new(H256::from_low_u64_be(2), 100)).unwrap(), None); + ).unwrap().value_at_block(&ComplexBlockId::new(H256::from_low_u64_be(2), 100)).is_err()); // when block is later than last finalized block AND there are no forks AND finalized value is Some // ---> [100] --- 200 @@ -815,7 +950,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 }), PruningStrategy::ByDepth(1024), test_id(100) - ).value_at_block(&test_id(200)).unwrap(), Some((test_id(100), None, 100))); + ).unwrap().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 // AND block is connected to finalized block AND finalized value is Some @@ -831,7 +966,7 @@ pub mod tests { .with_header(test_header(4)) .with_header(fork_header(0, 2, 3)), PruningStrategy::ByDepth(1024), test_id(2) - ).value_at_block(&fork_id(0, 2, 3)).unwrap(), Some((correct_id(2), None, 2))); + ).unwrap().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 // --- 2 --- 3 @@ -848,7 +983,7 @@ pub mod tests { .with_header(fork_header(0, 1, 3)) .with_header(fork_header(0, 1, 2)), PruningStrategy::ByDepth(1024), test_id(2) - ).value_at_block(&fork_id(0, 1, 3)).unwrap(), None); + ).unwrap().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 // AND unfinalized value is Some @@ -861,7 +996,7 @@ pub mod tests { .with_header(test_header(4)) .with_header(test_header(5)), PruningStrategy::ByDepth(1024), test_id(2) - ).value_at_block(&correct_id(5)).unwrap(), Some((correct_id(4), None, 4))); + ).unwrap().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 // ---> [2] ----------> [4] @@ -876,7 +1011,7 @@ pub mod tests { .with_header(test_header(4)) .with_header(fork_header(0, 2, 3)), PruningStrategy::ByDepth(1024), test_id(2) - ).value_at_block(&fork_id(0, 2, 3)).unwrap(), Some((correct_id(2), None, 2))); + ).unwrap().value_at_block(&fork_id(0, 2, 3)).unwrap(), Some((correct_id(2), None, 2))); } #[test] @@ -885,22 +1020,25 @@ pub mod tests { let fin = EntryType::Final; // when trying to insert block < finalized number - assert!(ListCache::new(DummyStorage::new(), PruningStrategy::ByDepth(1024), test_id(100)) - .on_block_insert( + let mut ops = Default::default(); + assert!(ListCache::new(DummyStorage::new(), PruningStrategy::ByDepth(1024), test_id(100)).unwrap() + .do_on_block_insert( &mut DummyTransaction::new(), test_id(49), test_id(50), Some(50), nfin, + &mut ops, ).unwrap().is_none()); // when trying to insert block @ finalized number - assert!(ListCache::new(DummyStorage::new(), PruningStrategy::ByDepth(1024), test_id(100)) - .on_block_insert( + assert!(ListCache::new(DummyStorage::new(), PruningStrategy::ByDepth(1024), test_id(100)).unwrap() + .do_on_block_insert( &mut DummyTransaction::new(), test_id(99), test_id(100), Some(100), nfin, + &Default::default(), ).unwrap().is_none()); // when trying to insert non-final block AND it appends to the best block of unfinalized fork @@ -910,19 +1048,23 @@ pub mod tests { .with_meta(None, vec![test_id(4)]) .with_entry(test_id(4), StorageEntry { prev_valid_from: None, value: 4 }), PruningStrategy::ByDepth(1024), test_id(2) - ); + ).unwrap(); cache.unfinalized[0].best_block = Some(test_id(4)); let mut tx = DummyTransaction::new(); - assert_eq!(cache.on_block_insert(&mut tx, test_id(4), test_id(5), Some(4), nfin).unwrap(), - Some(CommitOperation::AppendNewBlock(0, test_id(5)))); + assert_eq!( + cache.do_on_block_insert(&mut tx, test_id(4), test_id(5), Some(4), nfin, &Default::default()).unwrap(), + Some(CommitOperation::AppendNewBlock(0, test_id(5))), + ); assert!(tx.inserted_entries().is_empty()); assert!(tx.removed_entries().is_empty()); assert!(tx.updated_meta().is_none()); // when trying to insert non-final block AND it appends to the best block of unfinalized fork // AND new value is the same as in the fork' best block let mut tx = DummyTransaction::new(); - assert_eq!(cache.on_block_insert(&mut tx, test_id(4), test_id(5), Some(5), nfin).unwrap(), - Some(CommitOperation::AppendNewEntry(0, Entry { valid_from: test_id(5), value: 5 }))); + assert_eq!( + cache.do_on_block_insert(&mut tx, test_id(4), test_id(5), Some(5), nfin, &Default::default()).unwrap(), + Some(CommitOperation::AppendNewEntry(0, Entry { valid_from: test_id(5), value: 5 })), + ); assert_eq!(*tx.inserted_entries(), vec![test_id(5).hash].into_iter().collect()); assert!(tx.removed_entries().is_empty()); assert_eq!(*tx.updated_meta(), Some(Metadata { finalized: None, unfinalized: vec![test_id(5)] })); @@ -935,18 +1077,36 @@ pub mod tests { .with_entry(correct_id(4), StorageEntry { prev_valid_from: None, value: 4 }) .with_header(test_header(4)), PruningStrategy::ByDepth(1024), test_id(2) - ); + ).unwrap(); let mut tx = DummyTransaction::new(); - assert_eq!(cache.on_block_insert(&mut tx, correct_id(4), correct_id(5), Some(4), nfin).unwrap(), - Some(CommitOperation::AppendNewBlock(0, correct_id(5)))); + assert_eq!( + cache.do_on_block_insert( + &mut tx, + correct_id(4), + correct_id(5), + Some(4), + nfin, + &Default::default(), + ).unwrap(), + Some(CommitOperation::AppendNewBlock(0, correct_id(5))), + ); assert!(tx.inserted_entries().is_empty()); assert!(tx.removed_entries().is_empty()); assert!(tx.updated_meta().is_none()); // when trying to insert non-final block AND it is the first block that appends to the best block of unfinalized fork // AND new value is the same as in the fork' best block let mut tx = DummyTransaction::new(); - assert_eq!(cache.on_block_insert(&mut tx, correct_id(4), correct_id(5), Some(5), nfin).unwrap(), - Some(CommitOperation::AppendNewEntry(0, Entry { valid_from: correct_id(5), value: 5 }))); + assert_eq!( + cache.do_on_block_insert( + &mut tx, + correct_id(4), + correct_id(5), + Some(5), + nfin, + &Default::default(), + ).unwrap(), + Some(CommitOperation::AppendNewEntry(0, Entry { valid_from: correct_id(5), value: 5 })), + ); assert_eq!(*tx.inserted_entries(), vec![correct_id(5).hash].into_iter().collect()); assert!(tx.removed_entries().is_empty()); assert_eq!(*tx.updated_meta(), Some(Metadata { finalized: None, unfinalized: vec![correct_id(5)] })); @@ -961,10 +1121,13 @@ pub mod tests { .with_header(test_header(3)) .with_header(test_header(4)), PruningStrategy::ByDepth(1024), correct_id(2) - ); + ).unwrap(); 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(), - Some(CommitOperation::AddNewFork(Entry { valid_from: fork_id(0, 3, 4), value: 14 }))); + assert_eq!( + cache.do_on_block_insert(&mut tx, correct_id(3), fork_id(0, 3, 4), Some(14), nfin, &Default::default()) + .unwrap(), + Some(CommitOperation::AddNewFork(Entry { valid_from: fork_id(0, 3, 4), value: 14 })), + ); assert_eq!(*tx.inserted_entries(), vec![fork_id(0, 3, 4).hash].into_iter().collect()); assert!(tx.removed_entries().is_empty()); assert_eq!(*tx.updated_meta(), Some(Metadata { finalized: Some(correct_id(2)), unfinalized: vec![correct_id(4), fork_id(0, 3, 4)] })); @@ -976,9 +1139,13 @@ pub mod tests { .with_meta(Some(correct_id(2)), vec![]) .with_entry(correct_id(2), StorageEntry { prev_valid_from: None, value: 2 }), PruningStrategy::ByDepth(1024), correct_id(2) - ); + ).unwrap(); let mut tx = DummyTransaction::new(); - assert_eq!(cache.on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(2), nfin).unwrap(), None); + assert_eq!( + cache.do_on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(2), nfin, &Default::default()) + .unwrap(), + None, + ); assert!(tx.inserted_entries().is_empty()); assert!(tx.removed_entries().is_empty()); assert!(tx.updated_meta().is_none()); @@ -989,19 +1156,23 @@ pub mod tests { .with_meta(Some(correct_id(2)), vec![]) .with_entry(correct_id(2), StorageEntry { prev_valid_from: None, value: 2 }), PruningStrategy::ByDepth(1024), correct_id(2) - ); + ).unwrap(); let mut tx = DummyTransaction::new(); - assert_eq!(cache.on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(3), nfin).unwrap(), - Some(CommitOperation::AddNewFork(Entry { valid_from: correct_id(3), value: 3 }))); + assert_eq!( + cache.do_on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(3), nfin, &Default::default()) + .unwrap(), + Some(CommitOperation::AddNewFork(Entry { valid_from: correct_id(3), value: 3 })), + ); assert_eq!(*tx.inserted_entries(), vec![correct_id(3).hash].into_iter().collect()); assert!(tx.removed_entries().is_empty()); 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(), PruningStrategy::ByDepth(1024), correct_id(2)); + let cache = ListCache::new(DummyStorage::new(), PruningStrategy::ByDepth(1024), correct_id(2)).unwrap(); let mut tx = DummyTransaction::new(); assert_eq!( - cache.on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(3), fin).unwrap(), + cache.do_on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(3), fin, &Default::default()) + .unwrap(), Some(CommitOperation::BlockFinalized( correct_id(3), Some(Entry { valid_from: correct_id(3), value: 3 }), @@ -1017,17 +1188,19 @@ pub mod tests { .with_meta(Some(correct_id(2)), vec![]) .with_entry(correct_id(2), StorageEntry { prev_valid_from: None, value: 2 }), PruningStrategy::ByDepth(1024), correct_id(2) - ); + ).unwrap(); let mut tx = DummyTransaction::new(); - assert_eq!(cache.on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(2), fin).unwrap(), - Some(CommitOperation::BlockFinalized(correct_id(3), None, Default::default()))); + assert_eq!( + cache.do_on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(2), fin, &Default::default()).unwrap(), + Some(CommitOperation::BlockFinalized(correct_id(3), None, Default::default())), + ); assert!(tx.inserted_entries().is_empty()); assert!(tx.removed_entries().is_empty()); assert!(tx.updated_meta().is_none()); // when inserting finalized entry AND value differs from previous finalized let mut tx = DummyTransaction::new(); assert_eq!( - cache.on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(3), fin).unwrap(), + cache.do_on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(3), fin, &Default::default()).unwrap(), Some(CommitOperation::BlockFinalized( correct_id(3), Some(Entry { valid_from: correct_id(3), value: 3 }), @@ -1045,10 +1218,12 @@ pub mod tests { .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 }), PruningStrategy::ByDepth(1024), correct_id(2) - ); + ).unwrap(); let mut tx = DummyTransaction::new(); - assert_eq!(cache.on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(2), fin).unwrap(), - Some(CommitOperation::BlockFinalized(correct_id(3), None, vec![0].into_iter().collect()))); + assert_eq!( + cache.do_on_block_insert(&mut tx, correct_id(2), correct_id(3), Some(2), fin, &Default::default()).unwrap(), + Some(CommitOperation::BlockFinalized(correct_id(3), None, vec![0].into_iter().collect())), + ); } #[test] @@ -1060,13 +1235,18 @@ 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 }), PruningStrategy::ByDepth(1024), correct_id(2) - ); + ).unwrap(); let mut tx = DummyTransaction::new(); - assert_eq!(cache.on_block_finalize(&mut tx, correct_id(2), correct_id(3)).unwrap(), - Some(CommitOperation::BlockFinalized(correct_id(3), None, Default::default()))); + assert_eq!( + cache.do_on_block_finalize(&mut tx, correct_id(2), correct_id(3), &Default::default()).unwrap(), + Some(CommitOperation::BlockFinalized(correct_id(3), None, Default::default())), + ); assert!(tx.inserted_entries().is_empty()); assert!(tx.removed_entries().is_empty()); - assert!(tx.updated_meta().is_none()); + assert_eq!( + *tx.updated_meta(), + Some(Metadata { finalized: Some(correct_id(2)), unfinalized: vec![correct_id(5)] }), + ); // finalization finalizes entry let cache = ListCache::new( DummyStorage::new() @@ -1074,10 +1254,10 @@ 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 }), PruningStrategy::ByDepth(1024), correct_id(4) - ); + ).unwrap(); let mut tx = DummyTransaction::new(); assert_eq!( - cache.on_block_finalize(&mut tx, correct_id(4), correct_id(5)).unwrap(), + cache.do_on_block_finalize(&mut tx, correct_id(4), correct_id(5), &Default::default()).unwrap(), Some(CommitOperation::BlockFinalized( correct_id(5), Some(Entry { valid_from: correct_id(5), value: 5 }), @@ -1094,10 +1274,12 @@ pub mod tests { .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 }), PruningStrategy::ByDepth(1024), correct_id(2) - ); + ).unwrap(); let mut tx = DummyTransaction::new(); - assert_eq!(cache.on_block_finalize(&mut tx, correct_id(2), correct_id(3)).unwrap(), - Some(CommitOperation::BlockFinalized(correct_id(3), None, vec![0].into_iter().collect()))); + assert_eq!( + cache.do_on_block_finalize(&mut tx, correct_id(2), correct_id(3), &Default::default()).unwrap(), + Some(CommitOperation::BlockFinalized(correct_id(3), None, vec![0].into_iter().collect())), + ); } #[test] @@ -1109,25 +1291,29 @@ pub mod tests { .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 }), PruningStrategy::ByDepth(1024), correct_id(2) - ); + ).unwrap(); // when new block is appended to unfinalized fork - cache.on_transaction_commit(CommitOperation::AppendNewBlock(0, correct_id(6))); + cache.on_transaction_commit(vec![CommitOperation::AppendNewBlock(0, correct_id(6))].into()); assert_eq!(cache.unfinalized[0].best_block, Some(correct_id(6))); // when new entry is appended to unfinalized fork - cache.on_transaction_commit(CommitOperation::AppendNewEntry(0, Entry { valid_from: correct_id(7), value: 7 })); + cache.on_transaction_commit(vec![ + CommitOperation::AppendNewEntry(0, Entry { valid_from: correct_id(7), value: 7 }), + ].into()); assert_eq!(cache.unfinalized[0].best_block, Some(correct_id(7))); assert_eq!(cache.unfinalized[0].head, Entry { valid_from: correct_id(7), value: 7 }); // when new fork is added - cache.on_transaction_commit(CommitOperation::AddNewFork(Entry { valid_from: correct_id(10), value: 10 })); + cache.on_transaction_commit(vec![ + CommitOperation::AddNewFork(Entry { valid_from: correct_id(10), value: 10 }), + ].into()); assert_eq!(cache.unfinalized[2].best_block, Some(correct_id(10))); assert_eq!(cache.unfinalized[2].head, Entry { valid_from: correct_id(10), value: 10 }); // when block is finalized + entry is finalized + unfinalized forks are deleted - cache.on_transaction_commit(CommitOperation::BlockFinalized( + cache.on_transaction_commit(vec![CommitOperation::BlockFinalized( correct_id(20), Some(Entry { valid_from: correct_id(20), value: 20 }), vec![0, 1, 2].into_iter().collect(), - )); + )].into()); assert_eq!(cache.best_finalized_block, correct_id(20)); assert_eq!(cache.best_finalized_entry, Some(Entry { valid_from: correct_id(20), value: 20 })); assert!(cache.unfinalized.is_empty()); @@ -1148,7 +1334,7 @@ pub mod tests { .with_header(test_header(4)) .with_header(test_header(5)), PruningStrategy::ByDepth(1024), correct_id(0) - ).find_unfinalized_fork(&correct_id(4)).unwrap().unwrap().head.valid_from, correct_id(5)); + ).unwrap().find_unfinalized_fork((&correct_id(4)).into()).unwrap().unwrap().head.valid_from, correct_id(5)); // --- [2] ---------------> [5] // ----------> [3] ---> 4 assert_eq!(ListCache::new( @@ -1165,7 +1351,8 @@ pub mod tests { .with_header(fork_header(0, 1, 3)) .with_header(fork_header(0, 1, 4)), PruningStrategy::ByDepth(1024), correct_id(0) - ).find_unfinalized_fork(&fork_id(0, 1, 4)).unwrap().unwrap().head.valid_from, fork_id(0, 1, 3)); + ).unwrap() + .find_unfinalized_fork((&fork_id(0, 1, 4)).into()).unwrap().unwrap().head.valid_from, fork_id(0, 1, 3)); // --- [2] ---------------> [5] // ----------> [3] // -----------------> 4 @@ -1185,7 +1372,7 @@ pub mod tests { .with_header(fork_header(1, 1, 3)) .with_header(fork_header(1, 1, 4)), PruningStrategy::ByDepth(1024), correct_id(0) - ).find_unfinalized_fork(&fork_id(1, 1, 4)).unwrap().is_none()); + ).unwrap().find_unfinalized_fork((&fork_id(1, 1, 4)).into()).unwrap().is_none()); } #[test] @@ -1195,7 +1382,7 @@ pub mod tests { .with_entry(test_id(100), StorageEntry { prev_valid_from: Some(test_id(50)), value: 100 }) .with_entry(test_id(50), StorageEntry { prev_valid_from: None, value: 50 }); assert_eq!(Fork::<_, u64> { best_block: None, head: Entry { valid_from: test_id(100), value: 0 } } - .matches(&storage, &test_id(20)).unwrap(), false); + .matches(&storage, (&test_id(20)).into()).unwrap(), false); // when block is not connected to the begin block let storage = DummyStorage::new() .with_entry(correct_id(5), StorageEntry { prev_valid_from: Some(correct_id(3)), value: 100 }) @@ -1206,7 +1393,7 @@ pub mod tests { .with_header(fork_header(0, 2, 4)) .with_header(fork_header(0, 2, 3)); assert_eq!(Fork::<_, u64> { best_block: None, head: Entry { valid_from: correct_id(5), value: 100 } } - .matches(&storage, &fork_id(0, 2, 4)).unwrap(), false); + .matches(&storage, (&fork_id(0, 2, 4)).into()).unwrap(), false); // when block is not connected to the end block let storage = DummyStorage::new() .with_entry(correct_id(5), StorageEntry { prev_valid_from: Some(correct_id(3)), value: 100 }) @@ -1216,14 +1403,14 @@ pub mod tests { .with_header(test_header(3)) .with_header(fork_header(0, 3, 4)); assert_eq!(Fork::<_, u64> { best_block: None, head: Entry { valid_from: correct_id(5), value: 100 } } - .matches(&storage, &fork_id(0, 3, 4)).unwrap(), false); + .matches(&storage, (&fork_id(0, 3, 4)).into()).unwrap(), false); // when block is connected to the begin block AND end is open let storage = DummyStorage::new() .with_entry(correct_id(5), StorageEntry { prev_valid_from: None, value: 100 }) .with_header(test_header(5)) .with_header(test_header(6)); assert_eq!(Fork::<_, u64> { best_block: None, head: Entry { valid_from: correct_id(5), value: 100 } } - .matches(&storage, &correct_id(6)).unwrap(), true); + .matches(&storage, (&correct_id(6)).into()).unwrap(), true); // when block is connected to the begin block AND to the end block let storage = DummyStorage::new() .with_entry(correct_id(5), StorageEntry { prev_valid_from: Some(correct_id(3)), value: 100 }) @@ -1232,7 +1419,7 @@ pub mod tests { .with_header(test_header(4)) .with_header(test_header(3)); assert_eq!(Fork::<_, u64> { best_block: None, head: Entry { valid_from: correct_id(5), value: 100 } } - .matches(&storage, &correct_id(4)).unwrap(), true); + .matches(&storage, (&correct_id(4)).into()).unwrap(), true); } #[test] @@ -1338,9 +1525,21 @@ pub mod tests { #[test] fn is_connected_to_block_fails() { // when storage returns error - assert!(chain::is_connected_to_block::<_, u64, _>(&FaultyStorage, &test_id(1), &test_id(100)).is_err()); + assert!( + chain::is_connected_to_block::<_, u64, _>( + &FaultyStorage, + (&test_id(1)).into(), + &test_id(100), + ).is_err(), + ); // when there's no header in the storage - assert!(chain::is_connected_to_block::<_, u64, _>(&DummyStorage::new(), &test_id(1), &test_id(100)).is_err()); + assert!( + chain::is_connected_to_block::<_, u64, _>( + &DummyStorage::new(), + (&test_id(1)).into(), + &test_id(100), + ).is_err(), + ); } #[test] @@ -1348,35 +1547,35 @@ pub mod tests { // when without iterations we end up with different block assert_eq!(chain::is_connected_to_block::<_, u64, _>(&DummyStorage::new() .with_header(test_header(1)), - &test_id(1), &correct_id(1)).unwrap(), false); + (&test_id(1)).into(), &correct_id(1)).unwrap(), false); // when with ASC iterations we end up with different block assert_eq!(chain::is_connected_to_block::<_, u64, _>(&DummyStorage::new() .with_header(test_header(0)) .with_header(test_header(1)) .with_header(test_header(2)), - &test_id(0), &correct_id(2)).unwrap(), false); + (&test_id(0)).into(), &correct_id(2)).unwrap(), false); // when with DESC iterations we end up with different block assert_eq!(chain::is_connected_to_block::<_, u64, _>(&DummyStorage::new() .with_header(test_header(0)) .with_header(test_header(1)) .with_header(test_header(2)), - &correct_id(2), &test_id(0)).unwrap(), false); + (&correct_id(2)).into(), &test_id(0)).unwrap(), false); // when without iterations we end up with the same block assert_eq!(chain::is_connected_to_block::<_, u64, _>(&DummyStorage::new() .with_header(test_header(1)), - &correct_id(1), &correct_id(1)).unwrap(), true); + (&correct_id(1)).into(), &correct_id(1)).unwrap(), true); // when with ASC iterations we end up with the same block assert_eq!(chain::is_connected_to_block::<_, u64, _>(&DummyStorage::new() .with_header(test_header(0)) .with_header(test_header(1)) .with_header(test_header(2)), - &correct_id(0), &correct_id(2)).unwrap(), true); + (&correct_id(0)).into(), &correct_id(2)).unwrap(), true); // when with DESC iterations we end up with the same block assert_eq!(chain::is_connected_to_block::<_, u64, _>(&DummyStorage::new() .with_header(test_header(0)) .with_header(test_header(1)) .with_header(test_header(2)), - &correct_id(2), &correct_id(0)).unwrap(), true); + (&correct_id(2)).into(), &correct_id(0)).unwrap(), true); } #[test] @@ -1454,7 +1653,7 @@ pub mod tests { .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)); + strategy, test_id(9)).unwrap(); let mut tx = DummyTransaction::new(); // when finalizing entry #10: no entries pruned @@ -1515,28 +1714,64 @@ pub mod tests { .with_header(fork_header(1, 2, 5)) .with_header(fork_header(2, 4, 5)), PruningStrategy::ByDepth(1024), correct_id(1) - ); + ).unwrap(); // when 5 is reverted: entry 5 is truncated - let op = cache.on_block_revert(&mut DummyTransaction::new(), &correct_id(5)).unwrap(); + let op = cache.do_on_block_revert(&mut DummyTransaction::new(), &correct_id(5)).unwrap(); assert_eq!(op, CommitOperation::BlockReverted(vec![ (0, Some(Fork { best_block: None, head: Entry { valid_from: correct_id(4), value: 4 } })), ].into_iter().collect())); - cache.on_transaction_commit(op); + cache.on_transaction_commit(vec![op].into()); // when 3 is reverted: entries 4+5' are truncated - let op = cache.on_block_revert(&mut DummyTransaction::new(), &correct_id(3)).unwrap(); + let op = cache.do_on_block_revert(&mut DummyTransaction::new(), &correct_id(3)).unwrap(); assert_eq!(op, CommitOperation::BlockReverted(vec![ (0, None), (2, None), ].into_iter().collect())); - cache.on_transaction_commit(op); + cache.on_transaction_commit(vec![op].into()); // when 2 is reverted: entries 4'+5' are truncated - let op = cache.on_block_revert(&mut DummyTransaction::new(), &correct_id(2)).unwrap(); + let op = cache.do_on_block_revert(&mut DummyTransaction::new(), &correct_id(2)).unwrap(); assert_eq!(op, CommitOperation::BlockReverted(vec![ (0, None), ].into_iter().collect())); - cache.on_transaction_commit(op); + cache.on_transaction_commit(vec![op].into()); + } + + #[test] + fn append_commit_operation_works() { + let mut ops = CommitOperations::default(); + ops.append(None); + assert_eq!(ops.operations, Vec::new()); + + ops.append(Some(CommitOperation::BlockFinalized( + test_id(10), + Some(Entry { valid_from: test_id(10), value: 10 }), + vec![5].into_iter().collect(), + ))); + assert_eq!( + ops.operations, + vec![CommitOperation::BlockFinalized( + test_id(10), + Some(Entry { valid_from: test_id(10), value: 10 }), + vec![5].into_iter().collect(), + )], + ); + + ops.append(Some(CommitOperation::BlockFinalized( + test_id(20), + Some(Entry { valid_from: test_id(20), value: 20 }), + vec![5, 6].into_iter().collect(), + ))); + + assert_eq!( + ops.operations, + vec![CommitOperation::BlockFinalized( + test_id(20), + Some(Entry { valid_from: test_id(20), value: 20 }), + vec![5, 6].into_iter().collect(), + )], + ); } } diff --git a/client/db/src/cache/list_storage.rs b/client/db/src/cache/list_storage.rs index 9cd3b1049a4f8..606090ee1401d 100644 --- a/client/db/src/cache/list_storage.rs +++ b/client/db/src/cache/list_storage.rs @@ -222,7 +222,9 @@ mod meta { unfinalized.push(&entry.valid_from); }, CommitOperation::BlockFinalized(_, ref finalizing_entry, ref forks) => { - finalized = finalizing_entry.as_ref().map(|entry| &entry.valid_from); + if let Some(finalizing_entry) = finalizing_entry.as_ref() { + finalized = Some(&finalizing_entry.valid_from); + } for fork_index in forks.iter().rev() { unfinalized.remove(*fork_index); } diff --git a/client/db/src/cache/mod.rs b/client/db/src/cache/mod.rs index bef8d9a7919d3..8fd1adc094ae4 100644 --- a/client/db/src/cache/mod.rs +++ b/client/db/src/cache/mod.rs @@ -16,7 +16,7 @@ //! DB-backed cache of blockchain data. -use std::{sync::Arc, collections::HashMap}; +use std::{sync::Arc, collections::{HashMap, hash_map::Entry}}; use parking_lot::RwLock; use kvdb::{KeyValueDB, DBTransaction}; @@ -51,8 +51,10 @@ pub enum EntryType { /// Block identifier that holds both hash and number. #[derive(Clone, Debug, Encode, Decode, PartialEq)] pub struct ComplexBlockId { - hash: Block::Hash, - number: NumberFor, + /// Hash of the block. + pub(crate) hash: Block::Hash, + /// Number of the block. + pub(crate) number: NumberFor, } impl ComplexBlockId { @@ -79,7 +81,7 @@ pub struct DbCache { db: Arc, key_lookup_column: u32, header_column: u32, - authorities_column: u32, + cache_column: u32, genesis_hash: Block::Hash, best_finalized_block: ComplexBlockId, } @@ -90,7 +92,7 @@ impl DbCache { db: Arc, key_lookup_column: u32, header_column: u32, - authorities_column: u32, + cache_column: u32, genesis_hash: Block::Hash, best_finalized_block: ComplexBlockId, ) -> Self { @@ -99,7 +101,7 @@ impl DbCache { db, key_lookup_column, header_column, - authorities_column, + cache_column, genesis_hash, best_finalized_block, } @@ -115,30 +117,48 @@ impl DbCache { DbCacheTransaction { cache: self, tx, - cache_at_op: HashMap::new(), + cache_at_ops: HashMap::new(), best_finalized_block: None, } } + /// Begin cache transaction with given ops. + pub fn transaction_with_ops<'a>( + &'a mut self, + tx: &'a mut DBTransaction, + ops: DbCacheTransactionOps, + ) -> DbCacheTransaction<'a, Block> { + DbCacheTransaction { + cache: self, + tx, + cache_at_ops: ops.cache_at_ops, + best_finalized_block: ops.best_finalized_block, + } + } + /// Run post-commit cache operations. - pub fn commit(&mut self, ops: DbCacheTransactionOps) { - for (name, op) in ops.cache_at_op.into_iter() { - self.get_cache(name).on_transaction_commit(op); + pub fn commit(&mut self, ops: DbCacheTransactionOps) -> ClientResult<()> { + for (name, ops) in ops.cache_at_ops.into_iter() { + self.get_cache(name)?.on_transaction_commit(ops); } if let Some(best_finalized_block) = ops.best_finalized_block { self.best_finalized_block = best_finalized_block; } + Ok(()) } /// Creates `ListCache` with the given name or returns a reference to the existing. - fn get_cache(&mut self, name: CacheKeyId) -> &mut ListCache, self::list_storage::DbStorage> { + pub(crate) fn get_cache( + &mut self, + name: CacheKeyId, + ) -> ClientResult<&mut ListCache, self::list_storage::DbStorage>> { get_cache_helper( &mut self.cache_at, name, &self.db, self.key_lookup_column, self.header_column, - self.authorities_column, + self.cache_column, &self.best_finalized_block ) } @@ -154,34 +174,49 @@ fn get_cache_helper<'a, Block: BlockT>( header: u32, cache: u32, best_finalized_block: &ComplexBlockId, -) -> &'a mut ListCache, self::list_storage::DbStorage> { - cache_at.entry(name).or_insert_with(|| { - ListCache::new( - self::list_storage::DbStorage::new(name.to_vec(), db.clone(), - self::list_storage::DbColumns { - meta: COLUMN_META, - key_lookup, - header, - cache, - }, - ), - cache_pruning_strategy(name), - best_finalized_block.clone(), - ) - }) +) -> ClientResult<&'a mut ListCache, self::list_storage::DbStorage>> { + match cache_at.entry(name) { + Entry::Occupied(entry) => Ok(entry.into_mut()), + Entry::Vacant(entry) => { + let cache = ListCache::new( + self::list_storage::DbStorage::new(name.to_vec(), db.clone(), + self::list_storage::DbColumns { + meta: COLUMN_META, + key_lookup, + header, + cache, + }, + ), + cache_pruning_strategy(name), + best_finalized_block.clone(), + )?; + Ok(entry.insert(cache)) + } + } } /// Cache operations that are to be committed after database transaction is committed. +#[derive(Default)] pub struct DbCacheTransactionOps { - cache_at_op: HashMap>>, + cache_at_ops: HashMap>>, best_finalized_block: Option>, } +impl DbCacheTransactionOps { + /// Empty transaction ops. + pub fn empty() -> DbCacheTransactionOps { + DbCacheTransactionOps { + cache_at_ops: HashMap::new(), + best_finalized_block: None, + } + } +} + /// Database-backed blockchain data cache transaction valid for single block import. pub struct DbCacheTransaction<'a, Block: BlockT> { cache: &'a mut DbCache, tx: &'a mut DBTransaction, - cache_at_op: HashMap>>, + cache_at_ops: HashMap>>, best_finalized_block: Option>, } @@ -189,7 +224,7 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> { /// Convert transaction into post-commit operations set. pub fn into_ops(self) -> DbCacheTransactionOps { DbCacheTransactionOps { - cache_at_op: self.cache_at_op, + cache_at_ops: self.cache_at_ops, best_finalized_block: self.best_finalized_block, } } @@ -202,8 +237,6 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> { data_at: HashMap>, entry_type: EntryType, ) -> ClientResult { - assert!(self.cache_at_op.is_empty()); - // prepare list of caches that are not update // (we might still need to do some cache maintenance in this case) let missed_caches = self.cache.cache_at.keys() @@ -212,8 +245,9 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> { .collect::>(); let mut insert_op = |name: CacheKeyId, value: Option>| -> Result<(), sp_blockchain::Error> { - let cache = self.cache.get_cache(name); - let op = cache.on_block_insert( + let cache = self.cache.get_cache(name)?; + let cache_ops = self.cache_at_ops.entry(name).or_default(); + cache.on_block_insert( &mut self::list_storage::DbStorageTransaction::new( cache.storage(), &mut self.tx, @@ -222,10 +256,9 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> { block.clone(), value, entry_type, + cache_ops, )?; - if let Some(op) = op { - self.cache_at_op.insert(name, op); - } + Ok(()) }; @@ -245,23 +278,19 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> { pub fn on_block_finalize( mut self, parent: ComplexBlockId, - block: ComplexBlockId + block: ComplexBlockId, ) -> ClientResult { - assert!(self.cache_at_op.is_empty()); - - for (name, cache_at) in self.cache.cache_at.iter() { - let op = cache_at.on_block_finalize( + for (name, cache) in self.cache.cache_at.iter() { + let cache_ops = self.cache_at_ops.entry(*name).or_default(); + cache.on_block_finalize( &mut self::list_storage::DbStorageTransaction::new( - cache_at.storage(), + cache.storage(), &mut self.tx ), parent.clone(), block.clone(), + cache_ops, )?; - - if let Some(op) = op { - self.cache_at_op.insert(name.to_owned(), op); - } } self.best_finalized_block = Some(block); @@ -275,16 +304,15 @@ impl<'a, Block: BlockT> DbCacheTransaction<'a, Block> { reverted_block: &ComplexBlockId, ) -> ClientResult { for (name, cache) in self.cache.cache_at.iter() { - let op = cache.on_block_revert( + let cache_ops = self.cache_at_ops.entry(*name).or_default(); + cache.on_block_revert( &mut self::list_storage::DbStorageTransaction::new( cache.storage(), &mut self.tx ), reverted_block, + cache_ops, )?; - - assert!(!self.cache_at_op.contains_key(name)); - self.cache_at_op.insert(name.to_owned(), op); } Ok(self) @@ -310,7 +338,7 @@ impl BlockchainCache for DbCacheSync { )?; let tx_ops = tx.into_ops(); db.write(dbtx).map_err(db_err)?; - cache.commit(tx_ops); + cache.commit(tx_ops)?; Ok(()) } @@ -318,40 +346,38 @@ impl BlockchainCache for DbCacheSync { &self, key: &CacheKeyId, at: &BlockId, - ) -> Option<((NumberFor, Block::Hash), Option<(NumberFor, Block::Hash)>, Vec)> { + ) -> ClientResult, Block::Hash), Option<(NumberFor, Block::Hash)>, Vec)>> { let mut cache = self.0.write(); - let storage = cache.get_cache(*key).storage(); + let cache = cache.get_cache(*key)?; + let storage = cache.storage(); let db = storage.db(); let columns = storage.columns(); let at = match *at { BlockId::Hash(hash) => { - let header = utils::read_header::( + let header = utils::require_header::( &**db, columns.key_lookup, columns.header, - BlockId::Hash(hash.clone())).ok()??; + BlockId::Hash(hash.clone()))?; ComplexBlockId::new(hash, *header.number()) }, BlockId::Number(number) => { - let hash = utils::read_header::( + let hash = utils::require_header::( &**db, columns.key_lookup, columns.header, - BlockId::Number(number.clone())).ok()??.hash(); + BlockId::Number(number.clone()))?.hash(); ComplexBlockId::new(hash, number) }, }; - cache.cache_at - .get(key)? - .value_at_block(&at) + cache.value_at_block(&at) .map(|block_and_value| block_and_value.map(|(begin_block, end_block, value)| ( (begin_block.number, begin_block.hash), end_block.map(|end_block| (end_block.number, end_block.hash)), value, ))) - .ok()? } } diff --git a/client/db/src/changes_tries_storage.rs b/client/db/src/changes_tries_storage.rs new file mode 100644 index 0000000000000..72163a5694213 --- /dev/null +++ b/client/db/src/changes_tries_storage.rs @@ -0,0 +1,1015 @@ +// Copyright 2019 Parity Technologies (UK) Ltd. +// This file is part of Substrate. + +// Substrate is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Substrate is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Substrate. If not, see . + +//! DB-backed changes tries storage. + +use std::collections::{HashMap, HashSet}; +use std::sync::Arc; +use hash_db::Prefix; +use kvdb::{KeyValueDB, DBTransaction}; +use codec::{Decode, Encode}; +use parking_lot::RwLock; +use sp_blockchain::{Error as ClientError, Result as ClientResult}; +use sp_trie::MemoryDB; +use sc_client_api::backend::PrunableStateChangesTrieStorage; +use sp_blockchain::{well_known_cache_keys, Cache as BlockchainCache}; +use sp_core::{ChangesTrieConfiguration, ChangesTrieConfigurationRange, convert_hash}; +use sp_runtime::traits::{ + Block as BlockT, Header as HeaderT, HasherFor, NumberFor, One, Zero, CheckedSub, +}; +use sp_runtime::generic::{BlockId, DigestItem, ChangesTrieSignal}; +use sp_state_machine::{DBValue, ChangesTrieBuildCache, ChangesTrieCacheAction}; +use crate::utils::{self, Meta, meta_keys, db_err}; +use crate::cache::{ + DbCacheSync, DbCache, DbCacheTransactionOps, + ComplexBlockId, EntryType as CacheEntryType, +}; + +/// Extract new changes trie configuration (if available) from the header. +pub fn extract_new_configuration(header: &Header) -> Option<&Option> { + header.digest() + .log(DigestItem::as_changes_trie_signal) + .and_then(ChangesTrieSignal::as_new_configuration) +} + +/// Opaque configuration cache transaction. During its lifetime, no-one should modify cache. This is currently +/// guaranteed because import lock is held during block import/finalization. +pub struct DbChangesTrieStorageTransaction { + /// Cache operations that must be performed after db transaction is comitted. + cache_ops: DbCacheTransactionOps, + /// New configuration (if changed at current block). + new_config: Option>, +} + +impl DbChangesTrieStorageTransaction { + /// Consume self and return transaction with given new configuration. + pub fn with_new_config(mut self, new_config: Option>) -> Self { + self.new_config = new_config; + self + } +} + +impl From> for DbChangesTrieStorageTransaction { + fn from(cache_ops: DbCacheTransactionOps) -> Self { + DbChangesTrieStorageTransaction { + cache_ops, + new_config: None, + } + } +} + +/// Changes tries storage. +/// +/// Stores all tries in separate DB column. +/// Lock order: meta, tries_meta, cache, build_cache. +pub struct DbChangesTrieStorage { + db: Arc, + meta_column: u32, + changes_tries_column: u32, + key_lookup_column: u32, + header_column: u32, + meta: Arc, Block::Hash>>>, + tries_meta: RwLock>, + min_blocks_to_keep: Option, + /// The cache stores all ever existing changes tries configurations. + cache: DbCacheSync, + /// Build cache is a map of block => set of storage keys changed at this block. + /// They're used to build digest blocks - instead of reading+parsing tries from db + /// we just use keys sets from the cache. + build_cache: RwLock>>, +} + +/// Persistent struct that contains all the changes tries metadata. +#[derive(Decode, Encode, Debug)] +struct ChangesTriesMeta { + /// Oldest unpruned max-level (or skewed) digest trie blocks range. + /// The range is inclusive from both sides. + /// Is None only if: + /// 1) we haven't yet finalized any blocks (except genesis) + /// 2) if best_finalized_block - min_blocks_to_keep points to the range where changes tries are disabled + /// 3) changes tries pruning is disabled + pub oldest_digest_range: Option<(NumberFor, NumberFor)>, + /// End block (inclusive) of oldest pruned max-level (or skewed) digest trie blocks range. + /// It is guaranteed that we have no any changes tries before (and including) this block. + /// It is guaranteed that all existing changes tries after this block are not yet pruned (if created). + pub oldest_pruned_digest_range_end: NumberFor, +} + +impl DbChangesTrieStorage { + /// Create new changes trie storage. + pub fn new( + db: Arc, + meta_column: u32, + changes_tries_column: u32, + key_lookup_column: u32, + header_column: u32, + cache_column: u32, + meta: Arc, Block::Hash>>>, + min_blocks_to_keep: Option, + ) -> ClientResult { + let (finalized_hash, finalized_number, genesis_hash) = { + let meta = meta.read(); + (meta.finalized_hash, meta.finalized_number, meta.genesis_hash) + }; + let tries_meta = read_tries_meta(&*db, meta_column)?; + Ok(Self { + db: db.clone(), + meta_column, + changes_tries_column, + key_lookup_column, + header_column, + meta, + min_blocks_to_keep, + cache: DbCacheSync(RwLock::new(DbCache::new( + db.clone(), + key_lookup_column, + header_column, + cache_column, + genesis_hash, + ComplexBlockId::new(finalized_hash, finalized_number), + ))), + build_cache: RwLock::new(ChangesTrieBuildCache::new()), + tries_meta: RwLock::new(tries_meta), + }) + } + + /// Commit new changes trie. + pub fn commit( + &self, + tx: &mut DBTransaction, + mut changes_trie: MemoryDB>, + parent_block: ComplexBlockId, + block: ComplexBlockId, + new_header: &Block::Header, + finalized: bool, + new_configuration: Option>, + cache_tx: Option>, + ) -> ClientResult> { + // insert changes trie, associated with block, into DB + for (key, (val, _)) in changes_trie.drain() { + tx.put(self.changes_tries_column, key.as_ref(), &val); + } + + // if configuration has not been changed AND block is not finalized => nothing to do here + let new_configuration = match new_configuration { + Some(new_configuration) => new_configuration, + None if !finalized => return Ok(DbCacheTransactionOps::empty().into()), + None => return self.finalize( + tx, + parent_block.hash, + block.hash, + block.number, + Some(new_header), + cache_tx, + ), + }; + + // update configuration cache + let mut cache_at = HashMap::new(); + cache_at.insert(well_known_cache_keys::CHANGES_TRIE_CONFIG, new_configuration.encode()); + Ok(DbChangesTrieStorageTransaction::from(match cache_tx { + Some(cache_tx) => self.cache.0.write() + .transaction_with_ops(tx, cache_tx.cache_ops) + .on_block_insert( + parent_block, + block, + cache_at, + if finalized { CacheEntryType::Final } else { CacheEntryType::NonFinal }, + )? + .into_ops(), + None => self.cache.0.write() + .transaction(tx) + .on_block_insert( + parent_block, + block, + cache_at, + if finalized { CacheEntryType::Final } else { CacheEntryType::NonFinal }, + )? + .into_ops(), + }).with_new_config(Some(new_configuration))) + } + + /// Called when block is finalized. + pub fn finalize( + &self, + tx: &mut DBTransaction, + parent_block_hash: Block::Hash, + block_hash: Block::Hash, + block_num: NumberFor, + new_header: Option<&Block::Header>, + cache_tx: Option>, + ) -> ClientResult> { + // prune obsolete changes tries + self.prune(tx, block_hash, block_num, new_header.clone(), cache_tx.as_ref())?; + + // if we have inserted the block that we're finalizing in the same transaction + // => then we have already finalized it from the commit() call + if cache_tx.is_some() { + if let Some(new_header) = new_header { + if new_header.hash() == block_hash { + return Ok(cache_tx.expect("guarded by cache_tx.is_some(); qed")); + } + } + } + + // and finalize configuration cache entries + let block = ComplexBlockId::new(block_hash, block_num); + let parent_block_num = block_num.checked_sub(&One::one()).unwrap_or_else(|| Zero::zero()); + let parent_block = ComplexBlockId::new(parent_block_hash, parent_block_num); + Ok(match cache_tx { + Some(cache_tx) => DbChangesTrieStorageTransaction::from( + self.cache.0.write() + .transaction_with_ops(tx, cache_tx.cache_ops) + .on_block_finalize( + parent_block, + block, + )? + .into_ops() + ).with_new_config(cache_tx.new_config), + None => DbChangesTrieStorageTransaction::from( + self.cache.0.write() + .transaction(tx) + .on_block_finalize( + parent_block, + block, + )? + .into_ops() + ), + }) + } + + /// When block is reverted. + pub fn revert( + &self, + tx: &mut DBTransaction, + block: &ComplexBlockId, + ) -> ClientResult> { + Ok(self.cache.0.write().transaction(tx) + .on_block_revert(block)? + .into_ops() + .into()) + } + + /// When transaction has been committed. + pub fn post_commit(&self, tx: Option>) { + if let Some(tx) = tx { + self.cache.0.write().commit(tx.cache_ops) + .expect("only fails if cache with given name isn't loaded yet;\ + cache is already loaded because there is tx; qed"); + } + } + + /// Commit changes into changes trie build cache. + pub fn commit_build_cache(&self, cache_update: ChangesTrieCacheAction>) { + self.build_cache.write().perform(cache_update); + } + + /// Prune obsolete changes tries. + fn prune( + &self, + tx: &mut DBTransaction, + block_hash: Block::Hash, + block_num: NumberFor, + new_header: Option<&Block::Header>, + cache_tx: Option<&DbChangesTrieStorageTransaction>, + ) -> ClientResult<()> { + // never prune on archive nodes + let min_blocks_to_keep = match self.min_blocks_to_keep { + Some(min_blocks_to_keep) => min_blocks_to_keep, + None => return Ok(()), + }; + + let mut tries_meta = self.tries_meta.write(); + let mut next_digest_range_start = block_num; + loop { + // prune oldest digest if it is known + // it could be unknown if: + // 1) either we're finalizing block#1 + // 2) or we are (or were) in period where changes tries are disabled + if let Some((begin, end)) = tries_meta.oldest_digest_range { + if block_num <= end || block_num - end <= min_blocks_to_keep.into() { + break; + } + + tries_meta.oldest_pruned_digest_range_end = end; + sp_state_machine::prune_changes_tries( + &*self, + begin, + end, + &sp_state_machine::ChangesTrieAnchorBlockId { + hash: convert_hash(&block_hash), + number: block_num, + }, + |node| tx.delete(self.changes_tries_column, node.as_ref()), + ); + + next_digest_range_start = end + One::one(); + } + + // proceed to the next configuration range + let next_digest_range_start_hash = match block_num == next_digest_range_start { + true => block_hash, + false => utils::require_header::( + &*self.db, + self.key_lookup_column, + self.header_column, + BlockId::Number(next_digest_range_start), + )?.hash(), + }; + + let config_for_new_block = new_header + .map(|header| *header.number() == next_digest_range_start) + .unwrap_or(false); + let next_config = match cache_tx { + Some(cache_tx) if config_for_new_block && cache_tx.new_config.is_some() => { + let config = cache_tx + .new_config + .clone() + .expect("guarded by is_some(); qed"); + ChangesTrieConfigurationRange { + zero: (block_num, block_hash), + end: None, + config, + } + }, + _ if config_for_new_block => { + self.configuration_at(&BlockId::Hash(*new_header.expect( + "config_for_new_block is only true when new_header is passed; qed" + ).parent_hash()))? + }, + _ => self.configuration_at(&BlockId::Hash(next_digest_range_start_hash))?, + }; + if let Some(config) = next_config.config { + let mut oldest_digest_range = config + .next_max_level_digest_range(next_config.zero.0, next_digest_range_start) + .unwrap_or_else(|| (next_digest_range_start, next_digest_range_start)); + + if let Some(end) = next_config.end { + if end.0 < oldest_digest_range.1 { + oldest_digest_range.1 = end.0; + } + } + + tries_meta.oldest_digest_range = Some(oldest_digest_range); + continue; + } + + tries_meta.oldest_digest_range = None; + break; + } + + write_tries_meta(tx, self.meta_column, &*tries_meta); + Ok(()) + } +} + +impl PrunableStateChangesTrieStorage for DbChangesTrieStorage { + fn storage(&self) -> &dyn sp_state_machine::ChangesTrieStorage, NumberFor> { + self + } + + fn configuration_at(&self, at: &BlockId) -> ClientResult< + ChangesTrieConfigurationRange, Block::Hash> + > { + self.cache + .get_at(&well_known_cache_keys::CHANGES_TRIE_CONFIG, at)? + .and_then(|(zero, end, encoded)| Decode::decode(&mut &encoded[..]).ok() + .map(|config| ChangesTrieConfigurationRange { zero, end, config })) + .ok_or_else(|| ClientError::ErrorReadingChangesTriesConfig) + } + + fn oldest_pruned_digest_range_end(&self) -> NumberFor { + self.tries_meta.read().oldest_pruned_digest_range_end + } +} + +impl sp_state_machine::ChangesTrieRootsStorage, NumberFor> + for DbChangesTrieStorage +{ + fn build_anchor( + &self, + hash: Block::Hash, + ) -> Result>, String> { + utils::read_header::(&*self.db, self.key_lookup_column, self.header_column, BlockId::Hash(hash)) + .map_err(|e| e.to_string()) + .and_then(|maybe_header| maybe_header.map(|header| + sp_state_machine::ChangesTrieAnchorBlockId { + hash, + number: *header.number(), + } + ).ok_or_else(|| format!("Unknown header: {}", hash))) + } + + fn root( + &self, + anchor: &sp_state_machine::ChangesTrieAnchorBlockId>, + block: NumberFor, + ) -> Result, String> { + // check API requirement: we can't get NEXT block(s) based on anchor + if block > anchor.number { + return Err(format!("Can't get changes trie root at {} using anchor at {}", block, anchor.number)); + } + + // we need to get hash of the block to resolve changes trie root + let block_id = if block <= self.meta.read().finalized_number { + // if block is finalized, we could just read canonical hash + BlockId::Number(block) + } else { + // the block is not finalized + let mut current_num = anchor.number; + let mut current_hash: Block::Hash = convert_hash(&anchor.hash); + let maybe_anchor_header: Block::Header = utils::require_header::( + &*self.db, self.key_lookup_column, self.header_column, BlockId::Number(current_num) + ).map_err(|e| e.to_string())?; + if maybe_anchor_header.hash() == current_hash { + // if anchor is canonicalized, then the block is also canonicalized + BlockId::Number(block) + } else { + // else (block is not finalized + anchor is not canonicalized): + // => we should find the required block hash by traversing + // back from the anchor to the block with given number + while current_num != block { + let current_header: Block::Header = utils::require_header::( + &*self.db, self.key_lookup_column, self.header_column, BlockId::Hash(current_hash) + ).map_err(|e| e.to_string())?; + + current_hash = *current_header.parent_hash(); + current_num = current_num - One::one(); + } + + BlockId::Hash(current_hash) + } + }; + + Ok( + utils::require_header::( + &*self.db, + self.key_lookup_column, + self.header_column, + block_id, + ) + .map_err(|e| e.to_string())? + .digest() + .log(DigestItem::as_changes_trie_root) + .cloned() + ) + } +} + +impl sp_state_machine::ChangesTrieStorage, NumberFor> + for DbChangesTrieStorage +where + Block: BlockT, +{ + fn as_roots_storage(&self) -> &dyn sp_state_machine::ChangesTrieRootsStorage, NumberFor> { + self + } + + fn with_cached_changed_keys( + &self, + root: &Block::Hash, + functor: &mut dyn FnMut(&HashMap>, HashSet>>), + ) -> bool { + self.build_cache.read().with_changed_keys(root, functor) + } + + fn get(&self, key: &Block::Hash, _prefix: Prefix) -> Result, String> { + self.db.get(self.changes_tries_column, key.as_ref()) + .map_err(|err| format!("{}", err)) + } +} + +/// Read changes tries metadata from database. +fn read_tries_meta( + db: &dyn KeyValueDB, + meta_column: u32, +) -> ClientResult> { + match db.get(meta_column, meta_keys::CHANGES_TRIES_META).map_err(db_err)? { + Some(h) => match Decode::decode(&mut &h[..]) { + Ok(h) => Ok(h), + Err(err) => Err(ClientError::Backend(format!("Error decoding changes tries metadata: {}", err))), + }, + None => Ok(ChangesTriesMeta { + oldest_digest_range: None, + oldest_pruned_digest_range_end: Zero::zero(), + }), + } +} + +/// Write changes tries metadata from database. +fn write_tries_meta( + tx: &mut DBTransaction, + meta_column: u32, + meta: &ChangesTriesMeta, +) { + tx.put(meta_column, meta_keys::CHANGES_TRIES_META, &meta.encode()); +} + +#[cfg(test)] +mod tests { + use hash_db::EMPTY_PREFIX; + use sc_client_api::backend::{ + Backend as ClientBackend, NewBlockState, BlockImportOperation, PrunableStateChangesTrieStorage, + }; + use sp_blockchain::HeaderBackend as BlockchainHeaderBackend; + use sp_core::H256; + use sp_runtime::testing::{Digest, Header}; + use sp_runtime::traits::{Hash, BlakeTwo256}; + use sp_state_machine::{ChangesTrieRootsStorage, ChangesTrieStorage}; + use crate::Backend; + use crate::tests::{Block, insert_header, prepare_changes}; + use super::*; + + fn changes(number: u64) -> Option, Vec)>> { + Some(vec![(number.to_le_bytes().to_vec(), number.to_le_bytes().to_vec())]) + } + + fn insert_header_with_configuration_change( + backend: &Backend, + number: u64, + parent_hash: H256, + changes: Option, Vec)>>, + new_configuration: Option, + ) -> H256 { + let mut digest = Digest::default(); + let mut changes_trie_update = Default::default(); + if let Some(changes) = changes { + let (root, update) = prepare_changes(changes); + digest.push(DigestItem::ChangesTrieRoot(root)); + changes_trie_update = update; + } + digest.push(DigestItem::ChangesTrieSignal(ChangesTrieSignal::NewConfiguration(new_configuration))); + + let header = Header { + number, + parent_hash, + state_root: BlakeTwo256::trie_root(Vec::new()), + digest, + extrinsics_root: Default::default(), + }; + let header_hash = header.hash(); + + let block_id = if number == 0 { + BlockId::Hash(Default::default()) + } else { + BlockId::Number(number - 1) + }; + let mut op = backend.begin_operation().unwrap(); + backend.begin_state_operation(&mut op, block_id).unwrap(); + op.set_block_data(header, None, None, NewBlockState::Best).unwrap(); + op.update_changes_trie((changes_trie_update, ChangesTrieCacheAction::Clear)).unwrap(); + backend.commit_operation(op).unwrap(); + + header_hash + } + + #[test] + fn changes_trie_storage_works() { + let backend = Backend::::new_test(1000, 100); + backend.changes_tries_storage.meta.write().finalized_number = 1000; + + let check_changes = |backend: &Backend, block: u64, changes: Vec<(Vec, Vec)>| { + let (changes_root, mut changes_trie_update) = prepare_changes(changes); + let anchor = sp_state_machine::ChangesTrieAnchorBlockId { + hash: backend.blockchain().header(BlockId::Number(block)).unwrap().unwrap().hash(), + number: block + }; + assert_eq!(backend.changes_tries_storage.root(&anchor, block), Ok(Some(changes_root))); + + let storage = backend.changes_tries_storage.storage(); + for (key, (val, _)) in changes_trie_update.drain() { + assert_eq!(storage.get(&key, EMPTY_PREFIX), Ok(Some(val))); + } + }; + + let changes0 = vec![(b"key_at_0".to_vec(), b"val_at_0".to_vec())]; + let changes1 = vec![ + (b"key_at_1".to_vec(), b"val_at_1".to_vec()), + (b"another_key_at_1".to_vec(), b"another_val_at_1".to_vec()), + ]; + let changes2 = vec![(b"key_at_2".to_vec(), b"val_at_2".to_vec())]; + + let block0 = insert_header(&backend, 0, Default::default(), Some(changes0.clone()), Default::default()); + let block1 = insert_header(&backend, 1, block0, Some(changes1.clone()), Default::default()); + let _ = insert_header(&backend, 2, block1, Some(changes2.clone()), Default::default()); + + // check that the storage contains tries for all blocks + check_changes(&backend, 0, changes0); + check_changes(&backend, 1, changes1); + check_changes(&backend, 2, changes2); + } + + #[test] + fn changes_trie_storage_works_with_forks() { + let backend = Backend::::new_test(1000, 100); + + let changes0 = vec![(b"k0".to_vec(), b"v0".to_vec())]; + let changes1 = vec![(b"k1".to_vec(), b"v1".to_vec())]; + let changes2 = vec![(b"k2".to_vec(), b"v2".to_vec())]; + let block0 = insert_header(&backend, 0, Default::default(), Some(changes0.clone()), Default::default()); + let block1 = insert_header(&backend, 1, block0, Some(changes1.clone()), Default::default()); + let block2 = insert_header(&backend, 2, block1, Some(changes2.clone()), Default::default()); + + let changes2_1_0 = vec![(b"k3".to_vec(), b"v3".to_vec())]; + let changes2_1_1 = vec![(b"k4".to_vec(), b"v4".to_vec())]; + let block2_1_0 = insert_header(&backend, 3, block2, Some(changes2_1_0.clone()), Default::default()); + let block2_1_1 = insert_header(&backend, 4, block2_1_0, Some(changes2_1_1.clone()), Default::default()); + + let changes2_2_0 = vec![(b"k5".to_vec(), b"v5".to_vec())]; + let changes2_2_1 = vec![(b"k6".to_vec(), b"v6".to_vec())]; + let block2_2_0 = insert_header(&backend, 3, block2, Some(changes2_2_0.clone()), Default::default()); + let block2_2_1 = insert_header(&backend, 4, block2_2_0, Some(changes2_2_1.clone()), Default::default()); + + // finalize block1 + backend.changes_tries_storage.meta.write().finalized_number = 1; + + // branch1: when asking for finalized block hash + let (changes1_root, _) = prepare_changes(changes1); + let anchor = sp_state_machine::ChangesTrieAnchorBlockId { hash: block2_1_1, number: 4 }; + assert_eq!(backend.changes_tries_storage.root(&anchor, 1), Ok(Some(changes1_root))); + + // branch2: when asking for finalized block hash + let anchor = sp_state_machine::ChangesTrieAnchorBlockId { hash: block2_2_1, number: 4 }; + assert_eq!(backend.changes_tries_storage.root(&anchor, 1), Ok(Some(changes1_root))); + + // branch1: when asking for non-finalized block hash (search by traversal) + let (changes2_1_0_root, _) = prepare_changes(changes2_1_0); + let anchor = sp_state_machine::ChangesTrieAnchorBlockId { hash: block2_1_1, number: 4 }; + assert_eq!(backend.changes_tries_storage.root(&anchor, 3), Ok(Some(changes2_1_0_root))); + + // branch2: when asking for non-finalized block hash (search using canonicalized hint) + let (changes2_2_0_root, _) = prepare_changes(changes2_2_0); + let anchor = sp_state_machine::ChangesTrieAnchorBlockId { hash: block2_2_1, number: 4 }; + assert_eq!(backend.changes_tries_storage.root(&anchor, 3), Ok(Some(changes2_2_0_root))); + + // finalize first block of branch2 (block2_2_0) + backend.changes_tries_storage.meta.write().finalized_number = 3; + + // branch2: when asking for finalized block of this branch + assert_eq!(backend.changes_tries_storage.root(&anchor, 3), Ok(Some(changes2_2_0_root))); + + // branch1: when asking for finalized block of other branch + // => result is incorrect (returned for the block of branch1), but this is expected, + // because the other fork is abandoned (forked before finalized header) + let anchor = sp_state_machine::ChangesTrieAnchorBlockId { hash: block2_1_1, number: 4 }; + assert_eq!(backend.changes_tries_storage.root(&anchor, 3), Ok(Some(changes2_2_0_root))); + } + + #[test] + fn changes_tries_are_pruned_on_finalization() { + let mut backend = Backend::::new_test(1000, 100); + backend.changes_tries_storage.min_blocks_to_keep = Some(8); + + let parent_hash = |number| { + if number == 0 { + Default::default() + } else { + backend.blockchain().header(BlockId::Number(number - 1)).unwrap().unwrap().hash() + } + }; + + let insert_regular_header = |with_changes, number| { + insert_header( + &backend, + number, + parent_hash(number), + if with_changes { changes(number) } else { None }, + Default::default(), + ); + }; + + let is_pruned = |number| { + let trie_root = backend + .blockchain() + .header(BlockId::Number(number)) + .unwrap().unwrap() + .digest() + .log(DigestItem::as_changes_trie_root) + .cloned(); + match trie_root { + Some(trie_root) => backend.changes_tries_storage.get(&trie_root, EMPTY_PREFIX).unwrap().is_none(), + None => true, + } + }; + + let finalize_block = |number| { + let header = backend.blockchain().header(BlockId::Number(number)).unwrap().unwrap(); + let mut tx = DBTransaction::new(); + let cache_ops = backend.changes_tries_storage.finalize( + &mut tx, + *header.parent_hash(), + header.hash(), + number, + None, + None, + ).unwrap(); + backend.storage.db.write(tx).unwrap(); + backend.changes_tries_storage.post_commit(Some(cache_ops)); + }; + + // configuration ranges: + // (0; 6] - None + // [7; 17] - Some(2^2): D2 is built at #10, #14; SD is built at #17 + // [18; 21] - None + // [22; 32] - Some(8^1): D1 is built at #29; SD is built at #32 + // [33; ... - Some(1) + let config_at_6 = Some(ChangesTrieConfiguration::new(2, 2)); + let config_at_17 = None; + let config_at_21 = Some(ChangesTrieConfiguration::new(8, 1)); + let config_at_32 = Some(ChangesTrieConfiguration::new(1, 0)); + + (0..6).for_each(|number| insert_regular_header(false, number)); + insert_header_with_configuration_change(&backend, 6, parent_hash(6), None, config_at_6); + (7..17).for_each(|number| insert_regular_header(true, number)); + insert_header_with_configuration_change(&backend, 17, parent_hash(17), changes(17), config_at_17); + (18..21).for_each(|number| insert_regular_header(false, number)); + insert_header_with_configuration_change(&backend, 21, parent_hash(21), None, config_at_21); + (22..32).for_each(|number| insert_regular_header(true, number)); + insert_header_with_configuration_change(&backend, 32, parent_hash(32), changes(32), config_at_32); + (33..50).for_each(|number| insert_regular_header(true, number)); + + // when only genesis is finalized, nothing is pruned + (0..=6).for_each(|number| assert!(is_pruned(number))); + (7..=17).for_each(|number| assert!(!is_pruned(number))); + (18..=21).for_each(|number| assert!(is_pruned(number))); + (22..50).for_each(|number| assert!(!is_pruned(number))); + + // when blocks [1; 18] are finalized, nothing is pruned + (1..=18).for_each(|number| finalize_block(number)); + (0..=6).for_each(|number| assert!(is_pruned(number))); + (7..=17).for_each(|number| assert!(!is_pruned(number))); + (18..=21).for_each(|number| assert!(is_pruned(number))); + (22..50).for_each(|number| assert!(!is_pruned(number))); + + // when block 19 is finalized, changes tries for blocks [7; 10] are pruned + finalize_block(19); + (0..=10).for_each(|number| assert!(is_pruned(number))); + (11..=17).for_each(|number| assert!(!is_pruned(number))); + (18..=21).for_each(|number| assert!(is_pruned(number))); + (22..50).for_each(|number| assert!(!is_pruned(number))); + + // when blocks [20; 22] are finalized, nothing is pruned + (20..=22).for_each(|number| finalize_block(number)); + (0..=10).for_each(|number| assert!(is_pruned(number))); + (11..=17).for_each(|number| assert!(!is_pruned(number))); + (18..=21).for_each(|number| assert!(is_pruned(number))); + (22..50).for_each(|number| assert!(!is_pruned(number))); + + // when block 23 is finalized, changes tries for blocks [11; 14] are pruned + finalize_block(23); + (0..=14).for_each(|number| assert!(is_pruned(number))); + (15..=17).for_each(|number| assert!(!is_pruned(number))); + (18..=21).for_each(|number| assert!(is_pruned(number))); + (22..50).for_each(|number| assert!(!is_pruned(number))); + + // when blocks [24; 25] are finalized, nothing is pruned + (24..=25).for_each(|number| finalize_block(number)); + (0..=14).for_each(|number| assert!(is_pruned(number))); + (15..=17).for_each(|number| assert!(!is_pruned(number))); + (18..=21).for_each(|number| assert!(is_pruned(number))); + (22..50).for_each(|number| assert!(!is_pruned(number))); + + // when block 26 is finalized, changes tries for blocks [15; 17] are pruned + finalize_block(26); + (0..=21).for_each(|number| assert!(is_pruned(number))); + (22..50).for_each(|number| assert!(!is_pruned(number))); + + // when blocks [27; 37] are finalized, nothing is pruned + (27..=37).for_each(|number| finalize_block(number)); + (0..=21).for_each(|number| assert!(is_pruned(number))); + (22..50).for_each(|number| assert!(!is_pruned(number))); + + // when block 38 is finalized, changes tries for blocks [22; 29] are pruned + finalize_block(38); + (0..=29).for_each(|number| assert!(is_pruned(number))); + (30..50).for_each(|number| assert!(!is_pruned(number))); + + // when blocks [39; 40] are finalized, nothing is pruned + (39..=40).for_each(|number| finalize_block(number)); + (0..=29).for_each(|number| assert!(is_pruned(number))); + (30..50).for_each(|number| assert!(!is_pruned(number))); + + // when block 41 is finalized, changes tries for blocks [30; 32] are pruned + finalize_block(41); + (0..=32).for_each(|number| assert!(is_pruned(number))); + (33..50).for_each(|number| assert!(!is_pruned(number))); + + // when block 42 is finalized, changes trie for block 33 is pruned + finalize_block(42); + (0..=33).for_each(|number| assert!(is_pruned(number))); + (34..50).for_each(|number| assert!(!is_pruned(number))); + + // when block 43 is finalized, changes trie for block 34 is pruned + finalize_block(43); + (0..=34).for_each(|number| assert!(is_pruned(number))); + (35..50).for_each(|number| assert!(!is_pruned(number))); + } + + #[test] + fn changes_tries_configuration_is_updated_on_block_insert() { + let backend = Backend::::new_test(1000, 100); + + // configurations at blocks + let config_at_1 = Some(ChangesTrieConfiguration { + digest_interval: 4, + digest_levels: 2, + }); + let config_at_3 = Some(ChangesTrieConfiguration { + digest_interval: 8, + digest_levels: 1, + }); + let config_at_5 = None; + let config_at_7 = Some(ChangesTrieConfiguration { + digest_interval: 8, + digest_levels: 1, + }); + + // insert some blocks + let block0 = insert_header(&backend, 0, Default::default(), None, Default::default()); + let block1 = insert_header_with_configuration_change(&backend, 1, block0, None, config_at_1.clone()); + let block2 = insert_header(&backend, 2, block1, None, Default::default()); + let block3 = insert_header_with_configuration_change(&backend, 3, block2, None, config_at_3.clone()); + let block4 = insert_header(&backend, 4, block3, None, Default::default()); + let block5 = insert_header_with_configuration_change(&backend, 5, block4, None, config_at_5.clone()); + let block6 = insert_header(&backend, 6, block5, None, Default::default()); + let block7 = insert_header_with_configuration_change(&backend, 7, block6, None, config_at_7.clone()); + + // test configuration cache + let storage = &backend.changes_tries_storage; + assert_eq!( + storage.configuration_at(&BlockId::Hash(block1)).unwrap().config, + config_at_1.clone(), + ); + assert_eq!( + storage.configuration_at(&BlockId::Hash(block2)).unwrap().config, + config_at_1.clone(), + ); + assert_eq!( + storage.configuration_at(&BlockId::Hash(block3)).unwrap().config, + config_at_3.clone(), + ); + assert_eq!( + storage.configuration_at(&BlockId::Hash(block4)).unwrap().config, + config_at_3.clone(), + ); + assert_eq!( + storage.configuration_at(&BlockId::Hash(block5)).unwrap().config, + config_at_5.clone(), + ); + assert_eq!( + storage.configuration_at(&BlockId::Hash(block6)).unwrap().config, + config_at_5.clone(), + ); + assert_eq!( + storage.configuration_at(&BlockId::Hash(block7)).unwrap().config, + config_at_7.clone(), + ); + } + + #[test] + fn test_finalize_several_configuration_change_blocks_in_single_operation() { + let mut backend = Backend::::new_test(10, 10); + backend.changes_tries_storage.min_blocks_to_keep = Some(8); + + let configs = (0..=7).map(|i| Some(ChangesTrieConfiguration::new(2, i))).collect::>(); + + // insert unfinalized headers + let block0 = insert_header_with_configuration_change(&backend, 0, Default::default(), None, configs[0].clone()); + let block1 = insert_header_with_configuration_change(&backend, 1, block0, changes(1), configs[1].clone()); + let block2 = insert_header_with_configuration_change(&backend, 2, block1, changes(2), configs[2].clone()); + + let side_config2_1 = Some(ChangesTrieConfiguration::new(3, 2)); + let side_config2_2 = Some(ChangesTrieConfiguration::new(3, 3)); + let block2_1 = insert_header_with_configuration_change(&backend, 2, block1, changes(8), side_config2_1.clone()); + let _ = insert_header_with_configuration_change(&backend, 3, block2_1, changes(9), side_config2_2.clone()); + + // insert finalized header => 4 headers are finalized at once + let header3 = Header { + number: 3, + parent_hash: block2, + state_root: Default::default(), + digest: Digest { + logs: vec![ + DigestItem::ChangesTrieSignal(ChangesTrieSignal::NewConfiguration(configs[3].clone())), + ], + }, + extrinsics_root: Default::default(), + }; + let block3 = header3.hash(); + let mut op = backend.begin_operation().unwrap(); + backend.begin_state_operation(&mut op, BlockId::Hash(block2)).unwrap(); + op.mark_finalized(BlockId::Hash(block1), None).unwrap(); + op.mark_finalized(BlockId::Hash(block2), None).unwrap(); + op.set_block_data(header3, None, None, NewBlockState::Final).unwrap(); + backend.commit_operation(op).unwrap(); + + // insert more unfinalized headers + let block4 = insert_header_with_configuration_change(&backend, 4, block3, changes(4), configs[4].clone()); + let block5 = insert_header_with_configuration_change(&backend, 5, block4, changes(5), configs[5].clone()); + let block6 = insert_header_with_configuration_change(&backend, 6, block5, changes(6), configs[6].clone()); + + // insert finalized header => 4 headers are finalized at once + let header7 = Header { + number: 7, + parent_hash: block6, + state_root: Default::default(), + digest: Digest { + logs: vec![ + DigestItem::ChangesTrieSignal(ChangesTrieSignal::NewConfiguration(configs[7].clone())), + ], + }, + extrinsics_root: Default::default(), + }; + let mut op = backend.begin_operation().unwrap(); + backend.begin_state_operation(&mut op, BlockId::Hash(block6)).unwrap(); + op.mark_finalized(BlockId::Hash(block4), None).unwrap(); + op.mark_finalized(BlockId::Hash(block5), None).unwrap(); + op.mark_finalized(BlockId::Hash(block6), None).unwrap(); + op.set_block_data(header7, None, None, NewBlockState::Final).unwrap(); + backend.commit_operation(op).unwrap(); + } + + #[test] + fn changes_tries_configuration_is_reverted() { + let backend = Backend::::new_test(10, 10); + + let config0 = Some(ChangesTrieConfiguration::new(2, 5)); + let block0 = insert_header_with_configuration_change(&backend, 0, Default::default(), None, config0); + let config1 = Some(ChangesTrieConfiguration::new(2, 6)); + let block1 = insert_header_with_configuration_change(&backend, 1, block0, changes(0), config1); + backend.finalize_block(BlockId::Number(1), Some(vec![42])).unwrap(); + let config2 = Some(ChangesTrieConfiguration::new(2, 7)); + let block2 = insert_header_with_configuration_change(&backend, 2, block1, changes(1), config2); + let config2_1 = Some(ChangesTrieConfiguration::new(2, 8)); + let _ = insert_header_with_configuration_change(&backend, 3, block2, changes(10), config2_1); + let config2_2 = Some(ChangesTrieConfiguration::new(2, 9)); + let block2_2 = insert_header_with_configuration_change(&backend, 3, block2, changes(20), config2_2); + let config2_3 = Some(ChangesTrieConfiguration::new(2, 10)); + let _ = insert_header_with_configuration_change(&backend, 4, block2_2, changes(30), config2_3); + + // before truncate there are 2 unfinalized forks - block2_1+block2_3 + assert_eq!( + backend.changes_tries_storage.cache.0.write() + .get_cache(well_known_cache_keys::CHANGES_TRIE_CONFIG) + .unwrap() + .unfinalized() + .iter() + .map(|fork| fork.head().valid_from.number) + .collect::>(), + vec![3, 4], + ); + + // after truncating block2_3 - there are 2 unfinalized forks - block2_1+block2_2 + backend.revert(1, false).unwrap(); + assert_eq!( + backend.changes_tries_storage.cache.0.write() + .get_cache(well_known_cache_keys::CHANGES_TRIE_CONFIG) + .unwrap() + .unfinalized() + .iter() + .map(|fork| fork.head().valid_from.number) + .collect::>(), + vec![3, 3], + ); + + // after truncating block2_1 && block2_2 - there are still two unfinalized forks (cache impl specifics), + // the 1st one points to the block #3 because it isn't truncated + backend.revert(1, false).unwrap(); + assert_eq!( + backend.changes_tries_storage.cache.0.write() + .get_cache(well_known_cache_keys::CHANGES_TRIE_CONFIG) + .unwrap() + .unfinalized() + .iter() + .map(|fork| fork.head().valid_from.number) + .collect::>(), + vec![3, 2], + ); + + // after truncating block2 - there are no unfinalized forks + backend.revert(1, false).unwrap(); + assert!( + backend.changes_tries_storage.cache.0.write() + .get_cache(well_known_cache_keys::CHANGES_TRIE_CONFIG) + .unwrap() + .unfinalized() + .iter() + .map(|fork| fork.head().valid_from.number) + .collect::>() + .is_empty(), + ); + } +} diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index b4eae2db2b49f..4038d917a7df4 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -31,18 +31,21 @@ pub mod offchain; mod children; mod cache; +mod changes_tries_storage; mod storage_cache; +#[cfg(any(feature = "kvdb-rocksdb", test))] +mod upgrade; mod utils; mod stats; use std::sync::Arc; use std::path::PathBuf; use std::io; -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use sc_client_api::{execution_extensions::ExecutionExtensions, ForkBlocks, UsageInfo, MemoryInfo, BadBlocks, IoInfo}; use sc_client_api::backend::NewBlockState; -use sc_client_api::backend::{StorageCollection, ChildStorageCollection}; +use sc_client_api::backend::{PrunableStateChangesTrieStorage, StorageCollection, ChildStorageCollection}; use sp_blockchain::{ Result as ClientResult, Error as ClientError, well_known_cache_keys, HeaderBackend, @@ -51,11 +54,11 @@ use codec::{Decode, Encode}; use hash_db::Prefix; use kvdb::{KeyValueDB, DBTransaction}; use sp_trie::{MemoryDB, PrefixedMemoryDB, prefixed_key}; -use parking_lot::{Mutex, RwLock}; -use sp_core::{ChangesTrieConfiguration, convert_hash, traits::CodeExecutor}; +use parking_lot::RwLock; +use sp_core::{ChangesTrieConfiguration, traits::CodeExecutor}; use sp_core::storage::{well_known_keys, ChildInfo}; use sp_runtime::{ - generic::{BlockId, DigestItem}, Justification, Storage, + generic::BlockId, Justification, Storage, BuildStorage, }; use sp_runtime::traits::{ @@ -63,10 +66,11 @@ use sp_runtime::traits::{ }; use sc_executor::RuntimeInfo; use sp_state_machine::{ - DBValue, ChangesTrieTransaction, ChangesTrieCacheAction, ChangesTrieBuildCache, + DBValue, ChangesTrieTransaction, ChangesTrieCacheAction, backend::Backend as StateBackend, UsageInfo as StateUsageInfo, }; -use crate::utils::{Meta, db_err, meta_keys, read_db, read_meta}; +use crate::utils::{DatabaseType, Meta, db_err, meta_keys, read_db, read_meta}; +use crate::changes_tries_storage::{DbChangesTrieStorage, DbChangesTrieStorageTransaction}; use sc_client::leaves::{LeafSet, FinalizationDisplaced}; use sc_state_db::StateDb; use sp_blockchain::{CachedHeaderMetadata, HeaderMetadata, HeaderMetadataCache}; @@ -322,6 +326,7 @@ pub(crate) mod columns { pub const AUX: u32 = 8; /// Offchain workers local storage pub const OFFCHAIN: u32 = 9; + pub const CACHE: u32 = 10; } struct PendingBlock { @@ -352,7 +357,7 @@ pub struct BlockchainDb { impl BlockchainDb { fn new(db: Arc) -> ClientResult { - let meta = read_meta::(&*db, columns::META, columns::HEADER)?; + let meta = read_meta::(&*db, columns::HEADER)?; let leaves = LeafSet::read_from_db(&*db, columns::META, meta_keys::LEAF_PREFIX)?; Ok(BlockchainDb { db, @@ -511,7 +516,8 @@ pub struct BlockImportOperation { storage_updates: StorageCollection, child_storage_updates: ChildStorageCollection, changes_trie_updates: MemoryDB>, - changes_trie_cache_update: Option>>, + changes_trie_build_cache_update: Option>>, + changes_trie_config_update: Option>, pending_block: Option>, aux_ops: Vec<(Vec, Option>)>, finalized_blocks: Vec<(BlockId, Option)>, @@ -545,6 +551,9 @@ impl sc_client_api::backend::BlockImportOperation for Bloc leaf_state: NewBlockState, ) -> ClientResult<()> { assert!(self.pending_block.is_none(), "Only one block per operation is allowed"); + if let Some(changes_trie_config_update) = changes_tries_storage::extract_new_configuration(&header) { + self.changes_trie_config_update = Some(changes_trie_config_update.clone()); + } self.pending_block = Some(PendingBlock { header, body, @@ -583,12 +592,22 @@ impl sc_client_api::backend::BlockImportOperation for Bloc child_content.data.into_iter().map(|(k, v)| (k, Some(v))), child_content.child_info), ); + let mut changes_trie_config: Option = None; let (root, transaction) = self.old_state.full_storage_root( - storage.top.into_iter().map(|(k, v)| (k, Some(v))), + storage.top.into_iter().map(|(k, v)| { + if k == well_known_keys::CHANGES_TRIE_CONFIG { + changes_trie_config = Some( + Decode::decode(&mut &v[..]) + .expect("changes trie configuration is encoded properly at genesis") + ); + } + (k, Some(v)) + }), child_delta ); self.db_updates = transaction; + self.changes_trie_config_update = Some(changes_trie_config); self.commit_state = true; Ok(root) } @@ -598,7 +617,7 @@ impl sc_client_api::backend::BlockImportOperation for Bloc update: ChangesTrieTransaction, NumberFor>, ) -> ClientResult<()> { self.changes_trie_updates = update.0; - self.changes_trie_cache_update = Some(update.1); + self.changes_trie_build_cache_update = Some(update.1); Ok(()) } @@ -674,174 +693,6 @@ impl sp_state_machine::Storage> for DbGenesisSto } } -/// A database wrapper for changes tries. -pub struct DbChangesTrieStorage { - db: Arc, - meta: Arc, Block::Hash>>>, - min_blocks_to_keep: Option, - cache: RwLock>>, -} - -impl DbChangesTrieStorage { - /// Commit new changes trie. - pub fn commit(&self, tx: &mut DBTransaction, mut changes_trie: MemoryDB>) { - for (key, (val, _)) in changes_trie.drain() { - tx.put(columns::CHANGES_TRIE, key.as_ref(), &val); - } - } - - /// Commit changes into changes trie build cache. - pub fn commit_cache(&self, cache_update: ChangesTrieCacheAction>) { - self.cache.write().perform(cache_update); - } - - /// Prune obsolete changes tries. - pub fn prune( - &self, - config: &ChangesTrieConfiguration, - tx: &mut DBTransaction, - block_hash: Block::Hash, - block_num: NumberFor, - ) { - // never prune on archive nodes - let min_blocks_to_keep = match self.min_blocks_to_keep { - Some(min_blocks_to_keep) => min_blocks_to_keep, - None => return, - }; - - sp_state_machine::prune_changes_tries( - config, - &*self, - min_blocks_to_keep.into(), - &sp_state_machine::ChangesTrieAnchorBlockId { - hash: convert_hash(&block_hash), - number: block_num, - }, - |node| tx.delete(columns::CHANGES_TRIE, node.as_ref())); - } -} - -impl sc_client_api::backend::PrunableStateChangesTrieStorage - for DbChangesTrieStorage -{ - fn oldest_changes_trie_block( - &self, - config: &ChangesTrieConfiguration, - best_finalized_block: NumberFor, - ) -> NumberFor { - match self.min_blocks_to_keep { - Some(min_blocks_to_keep) => sp_state_machine::oldest_non_pruned_changes_trie( - config, - min_blocks_to_keep.into(), - best_finalized_block, - ), - None => One::one(), - } - } -} - -impl sp_state_machine::ChangesTrieRootsStorage, NumberFor> - for DbChangesTrieStorage -{ - fn build_anchor( - &self, - hash: Block::Hash, - ) -> Result>, String> { - utils::read_header::( - &*self.db, - columns::KEY_LOOKUP, - columns::HEADER, - BlockId::Hash(hash), - ) - .map_err(|e| e.to_string()) - .and_then(|maybe_header| maybe_header.map(|header| - sp_state_machine::ChangesTrieAnchorBlockId { - hash, - number: *header.number(), - } - ).ok_or_else(|| format!("Unknown header: {}", hash))) - } - - fn root( - &self, - anchor: &sp_state_machine::ChangesTrieAnchorBlockId>, - block: NumberFor, - ) -> Result, String> { - // check API requirement: we can't get NEXT block(s) based on anchor - if block > anchor.number { - return Err( - format!("Can't get changes trie root at {} using anchor at {}", block, anchor.number) - ) - } - - // we need to get hash of the block to resolve changes trie root - let block_id = if block <= self.meta.read().finalized_number { - // if block is finalized, we could just read canonical hash - BlockId::Number(block) - } else { - // the block is not finalized - let mut current_num = anchor.number; - let mut current_hash: Block::Hash = convert_hash(&anchor.hash); - let maybe_anchor_header: Block::Header = utils::require_header::( - &*self.db, columns::KEY_LOOKUP, columns::HEADER, BlockId::Number(current_num) - ).map_err(|e| e.to_string())?; - if maybe_anchor_header.hash() == current_hash { - // if anchor is canonicalized, then the block is also canonicalized - BlockId::Number(block) - } else { - // else (block is not finalized + anchor is not canonicalized): - // => we should find the required block hash by traversing - // back from the anchor to the block with given number - while current_num != block { - let current_header: Block::Header = utils::require_header::( - &*self.db, columns::KEY_LOOKUP, columns::HEADER, BlockId::Hash(current_hash) - ).map_err(|e| e.to_string())?; - - current_hash = *current_header.parent_hash(); - current_num = current_num - One::one(); - } - - BlockId::Hash(current_hash) - } - }; - - Ok( - utils::require_header::( - &*self.db, - columns::KEY_LOOKUP, - columns::HEADER, - block_id, - ) - .map_err(|e| e.to_string())? - .digest() - .log(DigestItem::as_changes_trie_root) - .cloned() - ) - } -} - -impl sp_state_machine::ChangesTrieStorage, NumberFor> - for DbChangesTrieStorage -{ - fn as_roots_storage(&self) - -> &dyn sp_state_machine::ChangesTrieRootsStorage, NumberFor> - { - self - } - - fn with_cached_changed_keys( - &self, - root: &Block::Hash, - functor: &mut dyn FnMut(&HashMap>, HashSet>>), - ) -> bool { - self.cache.read().with_changed_keys(root, functor) - } - - fn get(&self, key: &Block::Hash, _prefix: Prefix) -> Result, String> { - self.db.get(columns::CHANGES_TRIE, key.as_ref()).map_err(|err| format!("{}", err)) - } -} - /// Frozen `value` at time `at`. /// /// Used as inner structure under lock in `FrozenForDuration`. @@ -889,9 +740,6 @@ pub struct Backend { storage: Arc>, offchain_storage: offchain::LocalStorage, changes_tries_storage: DbChangesTrieStorage, - /// None<*> means that the value hasn't been cached yet. Some(*) means that the value (either None or - /// Some(*)) has been cached and is valid. - changes_trie_config: Mutex>>, blockchain: BlockchainDb, canonicalization_delay: u64, shared_cache: SharedCache, @@ -906,7 +754,7 @@ impl Backend { /// /// The pruning window is how old a block must be before the state is pruned. pub fn new(config: DatabaseSettings, canonicalization_delay: u64) -> ClientResult { - let db = crate::utils::open_database(&config, columns::META, "full")?; + let db = crate::utils::open_database::(&config, DatabaseType::Full)?; Self::from_kvdb(db as Arc<_>, canonicalization_delay, &config) } @@ -942,22 +790,25 @@ impl Backend { state_db, }; let offchain_storage = offchain::LocalStorage::new(db.clone()); - let changes_tries_storage = DbChangesTrieStorage { + let changes_tries_storage = DbChangesTrieStorage::new( db, + columns::META, + columns::CHANGES_TRIE, + columns::KEY_LOOKUP, + columns::HEADER, + columns::CACHE, meta, - min_blocks_to_keep: if is_archive_pruning { + if is_archive_pruning { None } else { Some(MIN_BLOCKS_TO_KEEP_CHANGES_TRIES_FOR) }, - cache: RwLock::new(ChangesTrieBuildCache::new()), - }; + )?; Ok(Backend { storage: Arc::new(storage_db), offchain_storage, changes_tries_storage, - changes_trie_config: Mutex::new(None), blockchain, canonicalization_delay, shared_cache: new_shared_cache( @@ -1026,26 +877,6 @@ impl Backend { self.blockchain.db.iter(columns::HEADER).count() as u64 } - /// Read (from storage or cache) changes trie config. - /// - /// Currently changes tries configuration is set up once (at genesis) and could not - /// be changed. Thus, we'll actually read value once and then just use cached value. - fn changes_trie_config(&self, block: Block::Hash) -> ClientResult> { - let mut cached_changes_trie_config = self.changes_trie_config.lock(); - match cached_changes_trie_config.clone() { - Some(cached_changes_trie_config) => Ok(cached_changes_trie_config), - None => { - use sc_client_api::backend::Backend; - let changes_trie_config = self - .state_at(BlockId::Hash(block))? - .storage(well_known_keys::CHANGES_TRIE_CONFIG)? - .and_then(|v| Decode::decode(&mut &*v).ok()); - *cached_changes_trie_config = Some(changes_trie_config.clone()); - Ok(changes_trie_config) - }, - } - } - /// Handle setting head within a transaction. `route_to` should be the last /// block that existed in the database. `best_to` should be the best block /// to be set. @@ -1137,6 +968,7 @@ impl Backend { header: &Block::Header, last_finalized: Option, justification: Option, + changes_trie_cache_ops: &mut Option>, finalization_displaced: &mut Option>>, ) -> ClientResult<(Block::Hash, ::Number, bool, bool)> { // TODO: ensure best chain contains this block. @@ -1144,8 +976,10 @@ impl Backend { self.ensure_sequential_finalization(header, last_finalized)?; self.note_finalized( transaction, + false, header, *hash, + changes_trie_cache_ops, finalization_displaced, )?; @@ -1204,6 +1038,7 @@ impl Backend { let mut meta_updates = Vec::with_capacity(operation.finalized_blocks.len()); let mut last_finalized_hash = self.blockchain.meta.read().finalized_hash; + let mut changes_trie_cache_ops = None; for (block, justification) in operation.finalized_blocks { let block_hash = self.blockchain.expect_block_hash_from_id(&block)?; let block_header = self.blockchain.expect_header(BlockId::Hash(block_hash))?; @@ -1214,6 +1049,7 @@ impl Backend { &block_header, Some(last_finalized_hash), justification, + &mut changes_trie_cache_ops, &mut finalization_displaced_leaves, )?); last_finalized_hash = block_hash; @@ -1257,6 +1093,11 @@ impl Backend { if number.is_zero() { transaction.put(columns::META, meta_keys::FINALIZED_BLOCK, &lookup_key); transaction.put(columns::META, meta_keys::GENESIS_HASH, hash.as_ref()); + + // for tests, because config is set from within the reset_storage + if operation.changes_trie_config_update.is_none() { + operation.changes_trie_config_update = Some(None); + } } let finalized = if operation.commit_state { @@ -1293,8 +1134,20 @@ impl Backend { let header = &pending_block.header; let is_best = pending_block.leaf_state.is_best(); let changes_trie_updates = operation.changes_trie_updates; - - self.changes_tries_storage.commit(&mut transaction, changes_trie_updates); + let changes_trie_config_update = operation.changes_trie_config_update; + changes_trie_cache_ops = Some(self.changes_tries_storage.commit( + &mut transaction, + changes_trie_updates, + cache::ComplexBlockId::new( + *header.parent_hash(), + if number.is_zero() { Zero::zero() } else { number - One::one() }, + ), + cache::ComplexBlockId::new(hash, number), + header, + finalized, + changes_trie_config_update, + changes_trie_cache_ops, + )?); let cache = operation.old_state.release(); // release state reference so that it can be finalized if finalized { @@ -1302,8 +1155,10 @@ impl Backend { self.ensure_sequential_finalization(header, Some(last_finalized_hash))?; self.note_finalized( &mut transaction, + true, header, hash, + &mut changes_trie_cache_ops, &mut finalization_displaced_leaves, )?; } else { @@ -1353,11 +1208,15 @@ impl Backend { let write_result = self.storage.db.write(transaction).map_err(db_err); - if let Some(changes_trie_cache_update) = operation.changes_trie_cache_update { - self.changes_tries_storage.commit_cache(changes_trie_cache_update); - } - - if let Some((number, hash, enacted, retracted, displaced_leaf, is_best, mut cache)) = imported { + if let Some(( + number, + hash, + enacted, + retracted, + displaced_leaf, + is_best, + mut cache, + )) = imported { if let Err(e) = write_result { let mut leaves = self.blockchain.leaves.write(); let mut undo = leaves.undo(); @@ -1383,6 +1242,11 @@ impl Backend { ); } + if let Some(changes_trie_build_cache_update) = operation.changes_trie_build_cache_update { + self.changes_tries_storage.commit_build_cache(changes_trie_build_cache_update); + } + self.changes_tries_storage.post_commit(changes_trie_cache_ops); + if let Some((enacted, retracted)) = cache_update { self.shared_cache.lock().sync(&enacted, &retracted); } @@ -1401,15 +1265,15 @@ impl Backend { fn note_finalized( &self, transaction: &mut DBTransaction, + is_inserted: bool, f_header: &Block::Header, f_hash: Block::Hash, + changes_trie_cache_ops: &mut Option>, displaced: &mut Option>> ) -> ClientResult<()> { let f_num = f_header.number().clone(); if self.storage.state_db.best_canonical().map(|c| f_num.saturated_into::() > c).unwrap_or(true) { - let parent_hash = f_header.parent_hash().clone(); - let lookup_key = utils::number_and_hash_to_lookup_key(f_num, f_hash.clone())?; transaction.put(columns::META, meta_keys::FINALIZED_BLOCK, &lookup_key); @@ -1417,9 +1281,16 @@ impl Backend { .map_err(|e: sc_state_db::Error| sp_blockchain::Error::from(format!("State database error: {:?}", e)))?; apply_state_commit(transaction, commit); - let changes_trie_config = self.changes_trie_config(parent_hash)?; - if let Some(changes_trie_config) = changes_trie_config { - self.changes_tries_storage.prune(&changes_trie_config, transaction, f_hash, f_num); + if !f_num.is_zero() { + let new_changes_trie_cache_ops = self.changes_tries_storage.finalize( + transaction, + *f_header.parent_hash(), + f_hash, + f_num, + if is_inserted { Some(&f_header) } else { None }, + changes_trie_cache_ops.take(), + )?; + *changes_trie_cache_ops = Some(new_changes_trie_cache_ops); } } @@ -1476,7 +1347,6 @@ impl sc_client_api::backend::Backend for Backend { type BlockImportOperation = BlockImportOperation; type Blockchain = BlockchainDb; type State = CachingState, Block>; - type ChangesTrieStorage = DbChangesTrieStorage; type OffchainStorage = offchain::LocalStorage; fn begin_operation(&self) -> ClientResult { @@ -1487,8 +1357,9 @@ impl sc_client_api::backend::Backend for Backend { db_updates: PrefixedMemoryDB::default(), storage_updates: Default::default(), child_storage_updates: Default::default(), + changes_trie_config_update: None, changes_trie_updates: MemoryDB::default(), - changes_trie_cache_update: None, + changes_trie_build_cache_update: None, aux_ops: Vec::new(), finalized_blocks: Vec::new(), set_head: None, @@ -1532,16 +1403,19 @@ impl sc_client_api::backend::Backend for Backend { let header = self.blockchain.expect_header(block)?; let mut displaced = None; let commit = |displaced| { + let mut changes_trie_cache_ops = None; let (hash, number, is_best, is_finalized) = self.finalize_block_with_transaction( &mut transaction, &hash, &header, None, justification, + &mut changes_trie_cache_ops, displaced, )?; self.storage.db.write(transaction).map_err(db_err)?; self.blockchain.update_meta(hash, number, is_best, is_finalized); + self.changes_tries_storage.post_commit(changes_trie_cache_ops); Ok(()) }; match commit(&mut displaced) { @@ -1557,7 +1431,7 @@ impl sc_client_api::backend::Backend for Backend { Ok(()) } - fn changes_trie_storage(&self) -> Option<&Self::ChangesTrieStorage> { + fn changes_trie_storage(&self) -> Option<&dyn PrunableStateChangesTrieStorage> { Some(&self.changes_tries_storage) } @@ -1616,6 +1490,7 @@ impl sc_client_api::backend::Backend for Backend { match self.storage.state_db.revert_one() { Some(commit) => { apply_state_commit(&mut transaction, commit); + let removed_number = best_number; let removed = self.blockchain.header(BlockId::Number(best_number))?.ok_or_else( || sp_blockchain::Error::UnknownBlock( format!("Error reverting to {}. Block hash not found.", best_number)))?; @@ -1628,6 +1503,13 @@ impl sc_client_api::backend::Backend for Backend { let update_finalized = best_number < finalized; let key = utils::number_and_hash_to_lookup_key(best_number.clone(), &best_hash)?; + let changes_trie_cache_ops = self.changes_tries_storage.revert( + &mut transaction, + &cache::ComplexBlockId::new( + removed.hash(), + removed_number, + ), + )?; transaction.put(columns::META, meta_keys::BEST_BLOCK, &key); if update_finalized { transaction.put(columns::META, meta_keys::FINALIZED_BLOCK, &key); @@ -1635,6 +1517,7 @@ impl sc_client_api::backend::Backend for Backend { transaction.delete(columns::KEY_LOOKUP, removed.hash().as_ref()); children::remove_children(&mut transaction, columns::META, meta_keys::CHILDREN_PREFIX, best_hash); self.storage.db.write(transaction).map_err(db_err)?; + self.changes_tries_storage.post_commit(Some(changes_trie_cache_ops)); self.blockchain.update_meta(best_hash, best_number, true, update_finalized); } None => return Ok(c.saturated_into::>()) @@ -1749,14 +1632,8 @@ impl sc_client_api::backend::Backend for Backend { impl sc_client_api::backend::LocalBackend for Backend {} -/// TODO: remove me in #3201 -pub fn unused_sink(cache_tx: crate::cache::DbCacheTransaction) { - cache_tx.on_block_revert(&crate::cache::ComplexBlockId::new(Default::default(), 0.into())).unwrap(); - unimplemented!() -} - #[cfg(test)] -mod tests { +pub(crate) mod tests { use hash_db::{HashDB, EMPTY_PREFIX}; use super::*; use crate::columns; @@ -1765,12 +1642,13 @@ mod tests { use sc_client::blockchain::Backend as BLBTrait; use sp_runtime::testing::{Header, Block as RawBlock, ExtrinsicWrapper}; use sp_runtime::traits::{Hash, BlakeTwo256}; - use sp_state_machine::{TrieMut, TrieDBMut, ChangesTrieRootsStorage, ChangesTrieStorage}; + use sp_runtime::generic::DigestItem; + use sp_state_machine::{TrieMut, TrieDBMut}; use sp_blockchain::{lowest_common_ancestor, tree_route}; - type Block = RawBlock>; + pub(crate) type Block = RawBlock>; - fn prepare_changes(changes: Vec<(Vec, Vec)>) -> (H256, MemoryDB) { + pub fn prepare_changes(changes: Vec<(Vec, Vec)>) -> (H256, MemoryDB) { let mut changes_root = H256::default(); let mut changes_trie_update = MemoryDB::::default(); { @@ -1786,20 +1664,22 @@ mod tests { (changes_root, changes_trie_update) } - fn insert_header( + pub fn insert_header( backend: &Backend, number: u64, parent_hash: H256, - changes: Vec<(Vec, Vec)>, + changes: Option, Vec)>>, extrinsics_root: H256, ) -> H256 { use sp_runtime::testing::Digest; - let (changes_root, changes_trie_update) = prepare_changes(changes); - let digest = Digest { - logs: vec![ - DigestItem::ChangesTrieRoot(changes_root), - ], - }; + + let mut digest = Digest::default(); + let mut changes_trie_update = Default::default(); + if let Some(changes) = changes { + let (root, update) = prepare_changes(changes); + digest.push(DigestItem::ChangesTrieRoot(root)); + changes_trie_update = update; + } let header = Header { number, parent_hash, @@ -2126,238 +2006,20 @@ mod tests { ).unwrap().is_none()); } - #[test] - fn changes_trie_storage_works() { - let backend = Backend::::new_test(1000, 100); - backend.changes_tries_storage.meta.write().finalized_number = 1000; - - - let check_changes = |backend: &Backend, block: u64, changes: Vec<(Vec, Vec)>| { - let (changes_root, mut changes_trie_update) = prepare_changes(changes); - let anchor = sp_state_machine::ChangesTrieAnchorBlockId { - hash: backend.blockchain().header(BlockId::Number(block)).unwrap().unwrap().hash(), - number: block - }; - assert_eq!(backend.changes_tries_storage.root(&anchor, block), Ok(Some(changes_root))); - - for (key, (val, _)) in changes_trie_update.drain() { - assert_eq!(backend.changes_trie_storage().unwrap().get(&key, EMPTY_PREFIX), Ok(Some(val))); - } - }; - - let changes0 = vec![(b"key_at_0".to_vec(), b"val_at_0".to_vec())]; - let changes1 = vec![ - (b"key_at_1".to_vec(), b"val_at_1".to_vec()), - (b"another_key_at_1".to_vec(), b"another_val_at_1".to_vec()), - ]; - let changes2 = vec![(b"key_at_2".to_vec(), b"val_at_2".to_vec())]; - - let block0 = insert_header(&backend, 0, Default::default(), changes0.clone(), Default::default()); - let block1 = insert_header(&backend, 1, block0, changes1.clone(), Default::default()); - let _ = insert_header(&backend, 2, block1, changes2.clone(), Default::default()); - - // check that the storage contains tries for all blocks - check_changes(&backend, 0, changes0); - check_changes(&backend, 1, changes1); - check_changes(&backend, 2, changes2); - } - - #[test] - fn changes_trie_storage_works_with_forks() { - let backend = Backend::::new_test(1000, 100); - - let changes0 = vec![(b"k0".to_vec(), b"v0".to_vec())]; - let changes1 = vec![(b"k1".to_vec(), b"v1".to_vec())]; - let changes2 = vec![(b"k2".to_vec(), b"v2".to_vec())]; - let block0 = insert_header(&backend, 0, Default::default(), changes0.clone(), Default::default()); - let block1 = insert_header(&backend, 1, block0, changes1.clone(), Default::default()); - let block2 = insert_header(&backend, 2, block1, changes2.clone(), Default::default()); - - let changes2_1_0 = vec![(b"k3".to_vec(), b"v3".to_vec())]; - let changes2_1_1 = vec![(b"k4".to_vec(), b"v4".to_vec())]; - let block2_1_0 = insert_header(&backend, 3, block2, changes2_1_0.clone(), Default::default()); - let block2_1_1 = insert_header(&backend, 4, block2_1_0, changes2_1_1.clone(), Default::default()); - - let changes2_2_0 = vec![(b"k5".to_vec(), b"v5".to_vec())]; - let changes2_2_1 = vec![(b"k6".to_vec(), b"v6".to_vec())]; - let block2_2_0 = insert_header(&backend, 3, block2, changes2_2_0.clone(), Default::default()); - let block2_2_1 = insert_header(&backend, 4, block2_2_0, changes2_2_1.clone(), Default::default()); - - // finalize block1 - backend.changes_tries_storage.meta.write().finalized_number = 1; - - // branch1: when asking for finalized block hash - let (changes1_root, _) = prepare_changes(changes1); - let anchor = sp_state_machine::ChangesTrieAnchorBlockId { hash: block2_1_1, number: 4 }; - assert_eq!(backend.changes_tries_storage.root(&anchor, 1), Ok(Some(changes1_root))); - - // branch2: when asking for finalized block hash - let anchor = sp_state_machine::ChangesTrieAnchorBlockId { hash: block2_2_1, number: 4 }; - assert_eq!(backend.changes_tries_storage.root(&anchor, 1), Ok(Some(changes1_root))); - - // branch1: when asking for non-finalized block hash (search by traversal) - let (changes2_1_0_root, _) = prepare_changes(changes2_1_0); - let anchor = sp_state_machine::ChangesTrieAnchorBlockId { hash: block2_1_1, number: 4 }; - assert_eq!(backend.changes_tries_storage.root(&anchor, 3), Ok(Some(changes2_1_0_root))); - - // branch2: when asking for non-finalized block hash (search using canonicalized hint) - let (changes2_2_0_root, _) = prepare_changes(changes2_2_0); - let anchor = sp_state_machine::ChangesTrieAnchorBlockId { hash: block2_2_1, number: 4 }; - assert_eq!(backend.changes_tries_storage.root(&anchor, 3), Ok(Some(changes2_2_0_root))); - - // finalize first block of branch2 (block2_2_0) - backend.changes_tries_storage.meta.write().finalized_number = 3; - - // branch2: when asking for finalized block of this branch - assert_eq!(backend.changes_tries_storage.root(&anchor, 3), Ok(Some(changes2_2_0_root))); - - // branch1: when asking for finalized block of other branch - // => result is incorrect (returned for the block of branch1), but this is expected, - // because the other fork is abandoned (forked before finalized header) - let anchor = sp_state_machine::ChangesTrieAnchorBlockId { hash: block2_1_1, number: 4 }; - assert_eq!(backend.changes_tries_storage.root(&anchor, 3), Ok(Some(changes2_2_0_root))); - } - - #[test] - fn changes_tries_with_digest_are_pruned_on_finalization() { - let mut backend = Backend::::new_test(1000, 100); - backend.changes_tries_storage.min_blocks_to_keep = Some(8); - let config = ChangesTrieConfiguration { - digest_interval: 2, - digest_levels: 2, - }; - - // insert some blocks - let block0 = insert_header(&backend, 0, Default::default(), vec![(b"key_at_0".to_vec(), b"val_at_0".to_vec())], Default::default()); - let block1 = insert_header(&backend, 1, block0, vec![(b"key_at_1".to_vec(), b"val_at_1".to_vec())], Default::default()); - let block2 = insert_header(&backend, 2, block1, vec![(b"key_at_2".to_vec(), b"val_at_2".to_vec())], Default::default()); - let block3 = insert_header(&backend, 3, block2, vec![(b"key_at_3".to_vec(), b"val_at_3".to_vec())], Default::default()); - let block4 = insert_header(&backend, 4, block3, vec![(b"key_at_4".to_vec(), b"val_at_4".to_vec())], Default::default()); - let block5 = insert_header(&backend, 5, block4, vec![(b"key_at_5".to_vec(), b"val_at_5".to_vec())], Default::default()); - let block6 = insert_header(&backend, 6, block5, vec![(b"key_at_6".to_vec(), b"val_at_6".to_vec())], Default::default()); - let block7 = insert_header(&backend, 7, block6, vec![(b"key_at_7".to_vec(), b"val_at_7".to_vec())], Default::default()); - let block8 = insert_header(&backend, 8, block7, vec![(b"key_at_8".to_vec(), b"val_at_8".to_vec())], Default::default()); - let block9 = insert_header(&backend, 9, block8, vec![(b"key_at_9".to_vec(), b"val_at_9".to_vec())], Default::default()); - let block10 = insert_header(&backend, 10, block9, vec![(b"key_at_10".to_vec(), b"val_at_10".to_vec())], Default::default()); - let block11 = insert_header(&backend, 11, block10, vec![(b"key_at_11".to_vec(), b"val_at_11".to_vec())], Default::default()); - let block12 = insert_header(&backend, 12, block11, vec![(b"key_at_12".to_vec(), b"val_at_12".to_vec())], Default::default()); - let block13 = insert_header(&backend, 13, block12, vec![(b"key_at_13".to_vec(), b"val_at_13".to_vec())], Default::default()); - backend.changes_tries_storage.meta.write().finalized_number = 13; - - // check that roots of all tries are in the columns::CHANGES_TRIE - let anchor = sp_state_machine::ChangesTrieAnchorBlockId { hash: block13, number: 13 }; - fn read_changes_trie_root(backend: &Backend, num: u64) -> H256 { - backend.blockchain().header(BlockId::Number(num)).unwrap().unwrap().digest().logs().iter() - .find(|i| i.as_changes_trie_root().is_some()).unwrap().as_changes_trie_root().unwrap().clone() - } - let root1 = read_changes_trie_root(&backend, 1); assert_eq!(backend.changes_tries_storage.root(&anchor, 1).unwrap(), Some(root1)); - let root2 = read_changes_trie_root(&backend, 2); assert_eq!(backend.changes_tries_storage.root(&anchor, 2).unwrap(), Some(root2)); - let root3 = read_changes_trie_root(&backend, 3); assert_eq!(backend.changes_tries_storage.root(&anchor, 3).unwrap(), Some(root3)); - let root4 = read_changes_trie_root(&backend, 4); assert_eq!(backend.changes_tries_storage.root(&anchor, 4).unwrap(), Some(root4)); - let root5 = read_changes_trie_root(&backend, 5); assert_eq!(backend.changes_tries_storage.root(&anchor, 5).unwrap(), Some(root5)); - let root6 = read_changes_trie_root(&backend, 6); assert_eq!(backend.changes_tries_storage.root(&anchor, 6).unwrap(), Some(root6)); - let root7 = read_changes_trie_root(&backend, 7); assert_eq!(backend.changes_tries_storage.root(&anchor, 7).unwrap(), Some(root7)); - let root8 = read_changes_trie_root(&backend, 8); assert_eq!(backend.changes_tries_storage.root(&anchor, 8).unwrap(), Some(root8)); - let root9 = read_changes_trie_root(&backend, 9); assert_eq!(backend.changes_tries_storage.root(&anchor, 9).unwrap(), Some(root9)); - let root10 = read_changes_trie_root(&backend, 10); assert_eq!(backend.changes_tries_storage.root(&anchor, 10).unwrap(), Some(root10)); - let root11 = read_changes_trie_root(&backend, 11); assert_eq!(backend.changes_tries_storage.root(&anchor, 11).unwrap(), Some(root11)); - let root12 = read_changes_trie_root(&backend, 12); assert_eq!(backend.changes_tries_storage.root(&anchor, 12).unwrap(), Some(root12)); - - // now simulate finalization of block#12, causing prune of tries at #1..#4 - let mut tx = DBTransaction::new(); - backend.changes_tries_storage.prune(&config, &mut tx, Default::default(), 12); - backend.storage.db.write(tx).unwrap(); - assert!(backend.changes_tries_storage.get(&root1, EMPTY_PREFIX).unwrap().is_none()); - assert!(backend.changes_tries_storage.get(&root2, EMPTY_PREFIX).unwrap().is_none()); - assert!(backend.changes_tries_storage.get(&root3, EMPTY_PREFIX).unwrap().is_none()); - assert!(backend.changes_tries_storage.get(&root4, EMPTY_PREFIX).unwrap().is_none()); - assert!(backend.changes_tries_storage.get(&root5, EMPTY_PREFIX).unwrap().is_some()); - assert!(backend.changes_tries_storage.get(&root6, EMPTY_PREFIX).unwrap().is_some()); - assert!(backend.changes_tries_storage.get(&root7, EMPTY_PREFIX).unwrap().is_some()); - assert!(backend.changes_tries_storage.get(&root8, EMPTY_PREFIX).unwrap().is_some()); - - // now simulate finalization of block#16, causing prune of tries at #5..#8 - let mut tx = DBTransaction::new(); - backend.changes_tries_storage.prune(&config, &mut tx, Default::default(), 16); - backend.storage.db.write(tx).unwrap(); - assert!(backend.changes_tries_storage.get(&root5, EMPTY_PREFIX).unwrap().is_none()); - assert!(backend.changes_tries_storage.get(&root6, EMPTY_PREFIX).unwrap().is_none()); - assert!(backend.changes_tries_storage.get(&root7, EMPTY_PREFIX).unwrap().is_none()); - assert!(backend.changes_tries_storage.get(&root8, EMPTY_PREFIX).unwrap().is_none()); - - // now "change" pruning mode to archive && simulate finalization of block#20 - // => no changes tries are pruned, because we never prune in archive mode - backend.changes_tries_storage.min_blocks_to_keep = None; - let mut tx = DBTransaction::new(); - backend.changes_tries_storage.prune(&config, &mut tx, Default::default(), 20); - backend.storage.db.write(tx).unwrap(); - assert!(backend.changes_tries_storage.get(&root9, EMPTY_PREFIX).unwrap().is_some()); - assert!(backend.changes_tries_storage.get(&root10, EMPTY_PREFIX).unwrap().is_some()); - assert!(backend.changes_tries_storage.get(&root11, EMPTY_PREFIX).unwrap().is_some()); - assert!(backend.changes_tries_storage.get(&root12, EMPTY_PREFIX).unwrap().is_some()); - } - - #[test] - fn changes_tries_without_digest_are_pruned_on_finalization() { - let mut backend = Backend::::new_test(1000, 100); - backend.changes_tries_storage.min_blocks_to_keep = Some(4); - let config = ChangesTrieConfiguration { - digest_interval: 0, - digest_levels: 0, - }; - - // insert some blocks - let block0 = insert_header(&backend, 0, Default::default(), vec![(b"key_at_0".to_vec(), b"val_at_0".to_vec())], Default::default()); - let block1 = insert_header(&backend, 1, block0, vec![(b"key_at_1".to_vec(), b"val_at_1".to_vec())], Default::default()); - let block2 = insert_header(&backend, 2, block1, vec![(b"key_at_2".to_vec(), b"val_at_2".to_vec())], Default::default()); - let block3 = insert_header(&backend, 3, block2, vec![(b"key_at_3".to_vec(), b"val_at_3".to_vec())], Default::default()); - let block4 = insert_header(&backend, 4, block3, vec![(b"key_at_4".to_vec(), b"val_at_4".to_vec())], Default::default()); - let block5 = insert_header(&backend, 5, block4, vec![(b"key_at_5".to_vec(), b"val_at_5".to_vec())], Default::default()); - let block6 = insert_header(&backend, 6, block5, vec![(b"key_at_6".to_vec(), b"val_at_6".to_vec())], Default::default()); - - // check that roots of all tries are in the columns::CHANGES_TRIE - let anchor = sp_state_machine::ChangesTrieAnchorBlockId { hash: block6, number: 6 }; - fn read_changes_trie_root(backend: &Backend, num: u64) -> H256 { - backend.blockchain().header(BlockId::Number(num)).unwrap().unwrap().digest().logs().iter() - .find(|i| i.as_changes_trie_root().is_some()).unwrap().as_changes_trie_root().unwrap().clone() - } - - let root1 = read_changes_trie_root(&backend, 1); assert_eq!(backend.changes_tries_storage.root(&anchor, 1).unwrap(), Some(root1)); - let root2 = read_changes_trie_root(&backend, 2); assert_eq!(backend.changes_tries_storage.root(&anchor, 2).unwrap(), Some(root2)); - let root3 = read_changes_trie_root(&backend, 3); assert_eq!(backend.changes_tries_storage.root(&anchor, 3).unwrap(), Some(root3)); - let root4 = read_changes_trie_root(&backend, 4); assert_eq!(backend.changes_tries_storage.root(&anchor, 4).unwrap(), Some(root4)); - let root5 = read_changes_trie_root(&backend, 5); assert_eq!(backend.changes_tries_storage.root(&anchor, 5).unwrap(), Some(root5)); - let root6 = read_changes_trie_root(&backend, 6); assert_eq!(backend.changes_tries_storage.root(&anchor, 6).unwrap(), Some(root6)); - - // now simulate finalization of block#5, causing prune of trie at #1 - let mut tx = DBTransaction::new(); - backend.changes_tries_storage.prune(&config, &mut tx, block5, 5); - backend.storage.db.write(tx).unwrap(); - assert!(backend.changes_tries_storage.get(&root1, EMPTY_PREFIX).unwrap().is_none()); - assert!(backend.changes_tries_storage.get(&root2, EMPTY_PREFIX).unwrap().is_some()); - - // now simulate finalization of block#6, causing prune of tries at #2 - let mut tx = DBTransaction::new(); - backend.changes_tries_storage.prune(&config, &mut tx, block6, 6); - backend.storage.db.write(tx).unwrap(); - assert!(backend.changes_tries_storage.get(&root2, EMPTY_PREFIX).unwrap().is_none()); - assert!(backend.changes_tries_storage.get(&root3, EMPTY_PREFIX).unwrap().is_some()); - } - #[test] fn tree_route_works() { let backend = Backend::::new_test(1000, 100); let blockchain = backend.blockchain(); - let block0 = insert_header(&backend, 0, Default::default(), Vec::new(), Default::default()); + let block0 = insert_header(&backend, 0, Default::default(), None, Default::default()); // fork from genesis: 3 prong. - let a1 = insert_header(&backend, 1, block0, Vec::new(), Default::default()); - let a2 = insert_header(&backend, 2, a1, Vec::new(), Default::default()); - let a3 = insert_header(&backend, 3, a2, Vec::new(), Default::default()); + let a1 = insert_header(&backend, 1, block0, None, Default::default()); + let a2 = insert_header(&backend, 2, a1, None, Default::default()); + let a3 = insert_header(&backend, 3, a2, None, Default::default()); // fork from genesis: 2 prong. - let b1 = insert_header(&backend, 1, block0, Vec::new(), H256::from([1; 32])); - let b2 = insert_header(&backend, 2, b1, Vec::new(), Default::default()); + let b1 = insert_header(&backend, 1, block0, None, H256::from([1; 32])); + let b2 = insert_header(&backend, 2, b1, None, Default::default()); { let tree_route = tree_route(blockchain, a3, b2).unwrap(); @@ -2397,8 +2059,8 @@ mod tests { let backend = Backend::::new_test(1000, 100); let blockchain = backend.blockchain(); - let block0 = insert_header(&backend, 0, Default::default(), Vec::new(), Default::default()); - let block1 = insert_header(&backend, 1, block0, Vec::new(), Default::default()); + let block0 = insert_header(&backend, 0, Default::default(), None, Default::default()); + let block1 = insert_header(&backend, 1, block0, None, Default::default()); { let tree_route = tree_route(blockchain, block0, block1).unwrap(); @@ -2413,16 +2075,16 @@ mod tests { fn lowest_common_ancestor_works() { let backend = Backend::::new_test(1000, 100); let blockchain = backend.blockchain(); - let block0 = insert_header(&backend, 0, Default::default(), Vec::new(), Default::default()); + let block0 = insert_header(&backend, 0, Default::default(), None, Default::default()); // fork from genesis: 3 prong. - let a1 = insert_header(&backend, 1, block0, Vec::new(), Default::default()); - let a2 = insert_header(&backend, 2, a1, Vec::new(), Default::default()); - let a3 = insert_header(&backend, 3, a2, Vec::new(), Default::default()); + let a1 = insert_header(&backend, 1, block0, None, Default::default()); + let a2 = insert_header(&backend, 2, a1, None, Default::default()); + let a3 = insert_header(&backend, 3, a2, None, Default::default()); // fork from genesis: 2 prong. - let b1 = insert_header(&backend, 1, block0, Vec::new(), H256::from([1; 32])); - let b2 = insert_header(&backend, 2, b1, Vec::new(), Default::default()); + let b1 = insert_header(&backend, 1, block0, None, H256::from([1; 32])); + let b2 = insert_header(&backend, 2, b1, None, Default::default()); { let lca = lowest_common_ancestor(blockchain, a3, b2).unwrap(); @@ -2479,14 +2141,14 @@ mod tests { let backend = Backend::::new_test(10000, 10000); let blockchain = backend.blockchain(); - let genesis = insert_header(&backend, 0, Default::default(), Vec::new(), Default::default()); + let genesis = insert_header(&backend, 0, Default::default(), None, Default::default()); let block100 = (1..=100).fold(genesis, |parent, n| { - insert_header(&backend, n, parent, Vec::new(), Default::default()) + insert_header(&backend, n, parent, None, Default::default()) }); let block7000 = (101..=7000).fold(block100, |parent, n| { - insert_header(&backend, n, parent, Vec::new(), Default::default()) + insert_header(&backend, n, parent, None, Default::default()) }); // This will cause the ancestor of `block100` to be set to `genesis` as a side-effect. @@ -2522,17 +2184,17 @@ mod tests { #[test] fn test_leaves_pruned_on_finality() { let backend: Backend = Backend::new_test(10, 10); - let block0 = insert_header(&backend, 0, Default::default(), Default::default(), Default::default()); + let block0 = insert_header(&backend, 0, Default::default(), None, Default::default()); - let block1_a = insert_header(&backend, 1, block0, Default::default(), Default::default()); - let block1_b = insert_header(&backend, 1, block0, Default::default(), [1; 32].into()); - let block1_c = insert_header(&backend, 1, block0, Default::default(), [2; 32].into()); + let block1_a = insert_header(&backend, 1, block0, None, Default::default()); + let block1_b = insert_header(&backend, 1, block0, None, [1; 32].into()); + let block1_c = insert_header(&backend, 1, block0, None, [2; 32].into()); assert_eq!(backend.blockchain().leaves().unwrap(), vec![block1_a, block1_b, block1_c]); - let block2_a = insert_header(&backend, 2, block1_a, Default::default(), Default::default()); - let block2_b = insert_header(&backend, 2, block1_b, Default::default(), Default::default()); - let block2_c = insert_header(&backend, 2, block1_b, Default::default(), [1; 32].into()); + let block2_a = insert_header(&backend, 2, block1_a, None, Default::default()); + let block2_b = insert_header(&backend, 2, block1_b, None, Default::default()); + let block2_c = insert_header(&backend, 2, block1_b, None, [1; 32].into()); assert_eq!(backend.blockchain().leaves().unwrap(), vec![block2_a, block2_b, block2_c, block1_c]); @@ -2559,8 +2221,8 @@ mod tests { let backend = Backend::::new_test(10, 10); - let block0 = insert_header(&backend, 0, Default::default(), Default::default(), Default::default()); - let _ = insert_header(&backend, 1, block0, Default::default(), Default::default()); + let block0 = insert_header(&backend, 0, Default::default(), None, Default::default()); + let _ = insert_header(&backend, 1, block0, None, Default::default()); let justification = Some(vec![1, 2, 3]); backend.finalize_block(BlockId::Number(1), justification.clone()).unwrap(); @@ -2575,9 +2237,11 @@ mod tests { fn test_finalize_multiple_blocks_in_single_op() { let backend = Backend::::new_test(10, 10); - let block0 = insert_header(&backend, 0, Default::default(), Default::default(), Default::default()); - let block1 = insert_header(&backend, 1, block0, Default::default(), Default::default()); - let block2 = insert_header(&backend, 2, block1, Default::default(), Default::default()); + let block0 = insert_header(&backend, 0, Default::default(), None, Default::default()); + let block1 = insert_header(&backend, 1, block0, None, Default::default()); + let block2 = insert_header(&backend, 2, block1, None, Default::default()); + let block3 = insert_header(&backend, 3, block2, None, Default::default()); + let block4 = insert_header(&backend, 4, block3, None, Default::default()); { let mut op = backend.begin_operation().unwrap(); backend.begin_state_operation(&mut op, BlockId::Hash(block0)).unwrap(); @@ -2585,15 +2249,22 @@ mod tests { op.mark_finalized(BlockId::Hash(block2), None).unwrap(); backend.commit_operation(op).unwrap(); } + { + let mut op = backend.begin_operation().unwrap(); + backend.begin_state_operation(&mut op, BlockId::Hash(block2)).unwrap(); + op.mark_finalized(BlockId::Hash(block3), None).unwrap(); + op.mark_finalized(BlockId::Hash(block4), None).unwrap(); + backend.commit_operation(op).unwrap(); + } } #[test] fn test_finalize_non_sequential() { let backend = Backend::::new_test(10, 10); - let block0 = insert_header(&backend, 0, Default::default(), Default::default(), Default::default()); - let block1 = insert_header(&backend, 1, block0, Default::default(), Default::default()); - let block2 = insert_header(&backend, 2, block1, Default::default(), Default::default()); + let block0 = insert_header(&backend, 0, Default::default(), None, Default::default()); + let block1 = insert_header(&backend, 1, block0, None, Default::default()); + let block2 = insert_header(&backend, 2, block1, None, Default::default()); { let mut op = backend.begin_operation().unwrap(); backend.begin_state_operation(&mut op, BlockId::Hash(block0)).unwrap(); diff --git a/client/db/src/light.rs b/client/db/src/light.rs index 8277f20b8d8ea..e663fc5699490 100644 --- a/client/db/src/light.rs +++ b/client/db/src/light.rs @@ -38,7 +38,7 @@ use codec::{Decode, Encode}; use sp_runtime::generic::{DigestItem, BlockId}; use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Zero, One, NumberFor, HasherFor}; 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::utils::{self, meta_keys, DatabaseType, Meta, db_err, read_db, block_id_to_lookup_key, read_meta}; use crate::{DatabaseSettings, FrozenForDuration}; use log::{trace, warn, debug}; @@ -68,13 +68,10 @@ pub struct LightStorage { io_stats: FrozenForDuration, } -impl LightStorage - where - Block: BlockT, -{ +impl LightStorage { /// Create new storage with given settings. pub fn new(config: DatabaseSettings) -> ClientResult { - let db = crate::utils::open_database(&config, columns::META, "light")?; + let db = crate::utils::open_database::(&config, DatabaseType::Light)?; Self::from_kvdb(db as Arc<_>) } @@ -89,7 +86,7 @@ impl LightStorage } fn from_kvdb(db: Arc) -> ClientResult { - let meta = read_meta::(&*db, columns::META, columns::HEADER)?; + let meta = read_meta::(&*db, columns::HEADER)?; let cache = DbCache::new( db.clone(), columns::KEY_LOOKUP, @@ -417,7 +414,7 @@ impl LightBlockchainStorage for LightStorage fn import_header( &self, header: Block::Header, - cache_at: HashMap>, + mut cache_at: HashMap>, leaf_state: NewBlockState, aux_ops: Vec<(Vec, Option>)>, ) -> ClientResult<()> { @@ -475,6 +472,13 @@ impl LightBlockchainStorage for LightStorage )?; } + // update changes trie configuration cache + if !cache_at.contains_key(&well_known_cache_keys::CHANGES_TRIE_CONFIG) { + if let Some(new_configuration) = crate::changes_tries_storage::extract_new_configuration(&header) { + cache_at.insert(well_known_cache_keys::CHANGES_TRIE_CONFIG, new_configuration.encode()); + } + } + { let mut cache = self.cache.0.write(); let cache_ops = cache.transaction(&mut transaction) @@ -487,8 +491,11 @@ impl LightBlockchainStorage for LightStorage .into_ops(); debug!("Light DB Commit {:?} ({})", hash, number); + self.db.write(transaction).map_err(db_err)?; - cache.commit(cache_ops); + cache.commit(cache_ops) + .expect("only fails if cache with given name isn't loaded yet;\ + cache is already loaded because there are cache_ops; qed"); } self.update_meta(hash, number, leaf_state.is_best(), finalized); @@ -543,7 +550,9 @@ impl LightBlockchainStorage for LightStorage .into_ops(); self.db.write(transaction).map_err(db_err)?; - cache.commit(cache_ops); + cache.commit(cache_ops) + .expect("only fails if cache with given name isn't loaded yet;\ + cache is already loaded because there are cache_ops; qed"); } self.update_meta(hash, header.number().clone(), false, true); @@ -603,7 +612,8 @@ fn cht_key>(cht_type: u8, block: N) -> ClientResult<[u8; 5]> { #[cfg(test)] pub(crate) mod tests { use sc_client::cht; - use sp_runtime::generic::DigestItem; + use sp_core::ChangesTrieConfiguration; + use sp_runtime::generic::{DigestItem, ChangesTrieSignal}; use sp_runtime::testing::{H256 as Hash, Header, Block as RawBlock, ExtrinsicWrapper}; use sp_blockchain::{lowest_common_ancestor, tree_route}; use super::*; @@ -964,7 +974,7 @@ pub(crate) mod tests { } fn get_authorities(cache: &dyn BlockchainCache, at: BlockId) -> Option> { - cache.get_at(&well_known_cache_keys::AUTHORITIES, &at) + cache.get_at(&well_known_cache_keys::AUTHORITIES, &at).unwrap_or(None) .and_then(|(_, _, val)| Decode::decode(&mut &val[..]).ok()) } @@ -1148,8 +1158,8 @@ pub(crate) mod tests { let (genesis_hash, storage) = { let db = LightStorage::::new_test(); - // before cache is initialized => None - assert_eq!(db.cache().get_at(b"test", &BlockId::Number(0)), None); + // before cache is initialized => Err + assert!(db.cache().get_at(b"test", &BlockId::Number(0)).is_err()); // insert genesis block (no value for cache is provided) let mut genesis_hash = None; @@ -1160,14 +1170,14 @@ pub(crate) mod tests { }); // after genesis is inserted => None - assert_eq!(db.cache().get_at(b"test", &BlockId::Number(0)), None); + assert_eq!(db.cache().get_at(b"test", &BlockId::Number(0)).unwrap(), None); // initialize cache db.cache().initialize(b"test", vec![42]).unwrap(); // after genesis is inserted + cache is initialized => Some assert_eq!( - db.cache().get_at(b"test", &BlockId::Number(0)), + db.cache().get_at(b"test", &BlockId::Number(0)).unwrap(), Some(((0, genesis_hash.unwrap()), None, vec![42])), ); @@ -1177,8 +1187,37 @@ pub(crate) mod tests { // restart && check that after restart value is read from the cache let db = LightStorage::::from_kvdb(storage as Arc<_>).expect("failed to create test-db"); assert_eq!( - db.cache().get_at(b"test", &BlockId::Number(0)), + db.cache().get_at(b"test", &BlockId::Number(0)).unwrap(), Some(((0, genesis_hash.unwrap()), None, vec![42])), ); } + + #[test] + fn changes_trie_configuration_is_tracked_on_light_client() { + let db = LightStorage::::new_test(); + + let new_config = Some(ChangesTrieConfiguration::new(2, 2)); + + // insert block#0 && block#1 (no value for cache is provided) + let hash0 = insert_block(&db, HashMap::new(), || default_header(&Default::default(), 0)); + assert_eq!( + db.cache().get_at(&well_known_cache_keys::CHANGES_TRIE_CONFIG, &BlockId::Number(0)).unwrap() + .map(|(_, _, v)| ChangesTrieConfiguration::decode(&mut &v[..]).unwrap()), + None, + ); + + // insert configuration at block#1 (starts from block#2) + insert_block(&db, HashMap::new(), || { + let mut header = default_header(&hash0, 1); + header.digest_mut().push( + DigestItem::ChangesTrieSignal(ChangesTrieSignal::NewConfiguration(new_config.clone())) + ); + header + }); + assert_eq!( + db.cache().get_at(&well_known_cache_keys::CHANGES_TRIE_CONFIG, &BlockId::Number(1)).unwrap() + .map(|(_, _, v)| Option::::decode(&mut &v[..]).unwrap()), + Some(new_config), + ); + } } diff --git a/client/db/src/upgrade.rs b/client/db/src/upgrade.rs new file mode 100644 index 0000000000000..ab2d4bbf799b2 --- /dev/null +++ b/client/db/src/upgrade.rs @@ -0,0 +1,198 @@ +// Copyright 2019-2020 Parity Technologies (UK) Ltd. +// This file is part of Substrate. + +// Substrate is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Substrate is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Substrate. If not, see . + +//! Database upgrade logic. + +use std::fs; +use std::io::{Read, Write, ErrorKind}; +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use codec::Encode; +use kvdb_rocksdb::{Database, DatabaseConfig}; +use parking_lot::RwLock; +use sp_blockchain::{well_known_cache_keys, Cache}; +use sp_core::ChangesTrieConfiguration; +use sp_runtime::traits::Block as BlockT; +use crate::{ + cache::{ComplexBlockId, DbCache, DbCacheSync}, + utils::{DatabaseType, check_database_type, db_err, read_genesis_hash}, +}; + +/// Version file name. +const VERSION_FILE_NAME: &'static str = "db_version"; + +/// Current db version. +const CURRENT_VERSION: u32 = 1; + +/// Number of columns in v0. +const V0_NUM_COLUMNS: u32 = 10; + +/// Upgrade database to current version. +pub fn upgrade_db(db_path: &Path, db_type: DatabaseType) -> sp_blockchain::Result<()> { + let db_version = current_version(db_path)?; + match db_version { + 0 => migrate_0_to_1::(db_path, db_type)?, + 1 => (), + _ => Err(sp_blockchain::Error::Backend(format!("Future database version: {}", db_version)))?, + } + + update_version(db_path) +} + +/// Migration from version0 to version1: +/// 1) the number of columns has changed from 10 to 11; +/// 2) changes tries configuration are now cached. +fn migrate_0_to_1(db_path: &Path, db_type: DatabaseType) -> sp_blockchain::Result<()> { + { + let db = open_database(db_path, db_type, V0_NUM_COLUMNS)?; + db.add_column().map_err(db_err)?; + db.flush().map_err(db_err)?; + } + + let db = open_database(db_path, db_type, V0_NUM_COLUMNS + 1)?; + + const V0_FULL_KEY_LOOKUP_COLUMN: u32 = 3; + const V0_FULL_HEADER_COLUMN: u32 = 4; + const V0_FULL_CACHE_COLUMN: u32 = 10; // that's the column we have just added + const V0_LIGHT_KEY_LOOKUP_COLUMN: u32 = 1; + const V0_LIGHT_HEADER_COLUMN: u32 = 2; + const V0_LIGHT_CACHE_COLUMN: u32 = 3; + + let (key_lookup_column, header_column, cache_column) = match db_type { + DatabaseType::Full => ( + V0_FULL_KEY_LOOKUP_COLUMN, + V0_FULL_HEADER_COLUMN, + V0_FULL_CACHE_COLUMN, + ), + DatabaseType::Light => ( + V0_LIGHT_KEY_LOOKUP_COLUMN, + V0_LIGHT_HEADER_COLUMN, + V0_LIGHT_CACHE_COLUMN, + ), + }; + + let genesis_hash: Option = read_genesis_hash(&db)?; + if let Some(genesis_hash) = genesis_hash { + let cache: DbCacheSync = DbCacheSync(RwLock::new(DbCache::new( + Arc::new(db), + key_lookup_column, + header_column, + cache_column, + genesis_hash, + ComplexBlockId::new(genesis_hash, 0.into()), + ))); + let changes_trie_config: Option = None; + cache.initialize(&well_known_cache_keys::CHANGES_TRIE_CONFIG, changes_trie_config.encode())?; + } + + Ok(()) +} + +/// Reads current database version from the file at given path. +/// If the file does not exist returns 0. +fn current_version(path: &Path) -> sp_blockchain::Result { + let unknown_version_err = || sp_blockchain::Error::Backend("Unknown database version".into()); + + match fs::File::open(version_file_path(path)) { + Err(ref err) if err.kind() == ErrorKind::NotFound => Ok(0), + Err(_) => Err(unknown_version_err()), + Ok(mut file) => { + let mut s = String::new(); + file.read_to_string(&mut s).map_err(|_| unknown_version_err())?; + u32::from_str_radix(&s, 10).map_err(|_| unknown_version_err()) + }, + } +} + +/// Opens database of givent type with given number of columns. +fn open_database(db_path: &Path, db_type: DatabaseType, db_columns: u32) -> sp_blockchain::Result { + let db_path = db_path.to_str() + .ok_or_else(|| sp_blockchain::Error::Backend("Invalid database path".into()))?; + let db_cfg = DatabaseConfig::with_columns(db_columns); + let db = Database::open(&db_cfg, db_path).map_err(db_err)?; + check_database_type(&db, db_type)?; + Ok(db) +} + +/// Writes current database version to the file. +/// Creates a new file if the version file does not exist yet. +fn update_version(path: &Path) -> sp_blockchain::Result<()> { + fs::create_dir_all(path).map_err(db_err)?; + let mut file = fs::File::create(version_file_path(path)).map_err(db_err)?; + file.write_all(format!("{}", CURRENT_VERSION).as_bytes()).map_err(db_err)?; + Ok(()) +} + +/// Returns the version file path. +fn version_file_path(path: &Path) -> PathBuf { + let mut file_path = path.to_owned(); + file_path.push(VERSION_FILE_NAME); + file_path +} + +#[cfg(test)] +mod tests { + use sc_state_db::PruningMode; + use crate::{DatabaseSettings, DatabaseSettingsSrc}; + use crate::tests::Block; + use super::*; + + fn create_db(db_path: &Path, version: Option) { + let db_cfg = DatabaseConfig::with_columns(V0_NUM_COLUMNS); + Database::open(&db_cfg, db_path.to_str().unwrap()).unwrap(); + if let Some(version) = version { + fs::create_dir_all(db_path).unwrap(); + let mut file = fs::File::create(version_file_path(db_path)).unwrap(); + file.write_all(format!("{}", version).as_bytes()).unwrap(); + } + } + + fn open_database(db_path: &Path) -> sp_blockchain::Result<()> { + crate::utils::open_database::(&DatabaseSettings { + state_cache_size: 0, + state_cache_child_ratio: None, + pruning: PruningMode::ArchiveAll, + source: DatabaseSettingsSrc::Path { path: db_path.to_owned(), cache_size: None }, + }, DatabaseType::Full).map(|_| ()) + } + + #[test] + fn downgrade_never_happens() { + let db_dir = tempdir::TempDir::new("").unwrap(); + create_db(db_dir.path(), Some(CURRENT_VERSION + 1)); + assert!(open_database(db_dir.path()).is_err()); + } + + #[test] + fn open_empty_database_works() { + let db_dir = tempdir::TempDir::new("").unwrap(); + open_database(db_dir.path()).unwrap(); + open_database(db_dir.path()).unwrap(); + assert_eq!(current_version(db_dir.path()).unwrap(), CURRENT_VERSION); + } + + #[test] + fn upgrade_from_0_to_1_works() { + for version_from_file in &[None, Some(0)] { + let db_dir = tempdir::TempDir::new("").unwrap(); + let db_path = db_dir.path(); + create_db(db_path, *version_from_file); + open_database(db_path).unwrap(); + assert_eq!(current_version(db_path).unwrap(), CURRENT_VERSION); + } + } +} diff --git a/client/db/src/utils.rs b/client/db/src/utils.rs index ed7b2220aacd5..f7f51d7f6de5d 100644 --- a/client/db/src/utils.rs +++ b/client/db/src/utils.rs @@ -21,7 +21,7 @@ use std::sync::Arc; use std::{io, convert::TryInto}; use kvdb::{KeyValueDB, DBTransaction}; -#[cfg(feature = "kvdb-rocksdb")] +#[cfg(any(feature = "kvdb-rocksdb", test))] use kvdb_rocksdb::{Database, DatabaseConfig}; use log::debug; @@ -36,7 +36,7 @@ use crate::{DatabaseSettings, DatabaseSettingsSrc}; /// Number of columns in the db. Must be the same for both full && light dbs. /// Otherwise RocksDb will fail to open database && check its type. -pub const NUM_COLUMNS: u32 = 10; +pub const NUM_COLUMNS: u32 = 11; /// Meta column. The set of keys in the column is shared by full && light storages. pub const COLUMN_META: u32 = 0; @@ -50,6 +50,8 @@ pub mod meta_keys { pub const FINALIZED_BLOCK: &[u8; 5] = b"final"; /// Meta information prefix for list-based caches. pub const CACHE_META_PREFIX: &[u8; 5] = b"cache"; + /// Meta information for changes tries key. + pub const CHANGES_TRIES_META: &[u8; 5] = b"ctrie"; /// Genesis block hash. pub const GENESIS_HASH: &[u8; 3] = b"gen"; /// Leaves prefix list key. @@ -76,6 +78,15 @@ pub struct Meta { /// A block lookup key: used for canonical lookup from block number to hash pub type NumberIndexKey = [u8; 4]; +/// Database type. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum DatabaseType { + /// Full node database. + Full, + /// Light node database. + Light, +} + /// Convert block number into short lookup key (LE representation) for /// blocks that are in the canonical chain. /// @@ -203,14 +214,17 @@ pub fn db_err(err: io::Error) -> sp_blockchain::Error { } /// Open RocksDB database. -pub fn open_database( +pub fn open_database( config: &DatabaseSettings, - col_meta: u32, - db_type: &str + db_type: DatabaseType, ) -> sp_blockchain::Result> { let db: Arc = match &config.source { - #[cfg(feature = "kvdb-rocksdb")] + #[cfg(any(feature = "kvdb-rocksdb", test))] DatabaseSettingsSrc::Path { path, cache_size } => { + // first upgrade database to required version + crate::upgrade::upgrade_db::(&path, db_type)?; + + // and now open database assuming that it has the latest version let mut db_config = DatabaseConfig::with_columns(NUM_COLUMNS); if let Some(cache_size) = cache_size { @@ -232,7 +246,7 @@ pub fn open_database( .ok_or_else(|| sp_blockchain::Error::Backend("Invalid database path".into()))?; Arc::new(Database::open(&db_config, &path).map_err(db_err)?) }, - #[cfg(not(feature = "kvdb-rocksdb"))] + #[cfg(not(any(feature = "kvdb-rocksdb", test)))] DatabaseSettingsSrc::Path { .. } => { let msg = "Try to open RocksDB database with RocksDB disabled".into(); return Err(sp_blockchain::Error::Backend(msg)); @@ -240,22 +254,28 @@ pub fn open_database( DatabaseSettingsSrc::Custom(db) => db.clone(), }; - // check database type - match db.get(col_meta, meta_keys::TYPE).map_err(db_err)? { + check_database_type(&*db, db_type)?; + + Ok(db) +} + +/// Check database type. +pub fn check_database_type(db: &dyn KeyValueDB, db_type: DatabaseType) -> sp_blockchain::Result<()> { + match db.get(COLUMN_META, meta_keys::TYPE).map_err(db_err)? { Some(stored_type) => { - if db_type.as_bytes() != &*stored_type { + if db_type.as_str().as_bytes() != &*stored_type { return Err(sp_blockchain::Error::Backend( - format!("Unexpected database type. Expected: {}", db_type)).into()); + format!("Unexpected database type. Expected: {}", db_type.as_str())).into()); } }, None => { let mut transaction = DBTransaction::new(); - transaction.put(col_meta, meta_keys::TYPE, db_type.as_bytes()); + transaction.put(COLUMN_META, meta_keys::TYPE, db_type.as_str().as_bytes()); db.write(transaction).map_err(db_err)?; }, } - Ok(db) + Ok(()) } /// Read database column entry for the given block. @@ -304,20 +324,15 @@ pub fn require_header( } /// Read meta from the database. -pub fn read_meta(db: &dyn KeyValueDB, col_meta: u32, col_header: u32) -> Result< +pub fn read_meta(db: &dyn KeyValueDB, col_header: u32) -> Result< Meta<<::Header as HeaderT>::Number, Block::Hash>, sp_blockchain::Error, > where Block: BlockT, { - let genesis_hash: Block::Hash = match db.get(col_meta, meta_keys::GENESIS_HASH).map_err(db_err)? { - Some(h) => match Decode::decode(&mut &h[..]) { - Ok(h) => h, - Err(err) => return Err(sp_blockchain::Error::Backend( - format!("Error decoding genesis hash: {}", err) - )), - }, + let genesis_hash: Block::Hash = match read_genesis_hash(db)? { + Some(genesis_hash) => genesis_hash, None => return Ok(Meta { best_hash: Default::default(), best_number: Zero::zero(), @@ -328,7 +343,7 @@ pub fn read_meta(db: &dyn KeyValueDB, col_meta: u32, col_header: u32) -> }; let load_meta_block = |desc, key| -> Result<_, sp_blockchain::Error> { - if let Some(Some(header)) = db.get(col_meta, key).and_then(|id| + if let Some(Some(header)) = db.get(COLUMN_META, key).and_then(|id| match id { Some(id) => db.get(col_header, &id).map(|h| h.map(|b| Block::Header::decode(&mut &b[..]).ok())), None => Ok(None), @@ -354,6 +369,29 @@ pub fn read_meta(db: &dyn KeyValueDB, col_meta: u32, col_header: u32) -> }) } +/// Read genesis hash from database. +pub fn read_genesis_hash(db: &dyn KeyValueDB) -> sp_blockchain::Result> { + match db.get(COLUMN_META, meta_keys::GENESIS_HASH).map_err(db_err)? { + Some(h) => match Decode::decode(&mut &h[..]) { + Ok(h) => Ok(Some(h)), + Err(err) => Err(sp_blockchain::Error::Backend( + format!("Error decoding genesis hash: {}", err) + )), + }, + None => Ok(None), + } +} + +impl DatabaseType { + /// Returns str representation of the type. + pub fn as_str(&self) -> &'static str { + match *self { + DatabaseType::Full => "full", + DatabaseType::Light => "light", + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -368,4 +406,10 @@ mod tests { _ => unreachable!(), }; } + + #[test] + fn database_type_as_str_works() { + assert_eq!(DatabaseType::Full.as_str(), "full"); + assert_eq!(DatabaseType::Light.as_str(), "light"); + } } diff --git a/client/finality-grandpa/src/tests.rs b/client/finality-grandpa/src/tests.rs index 4ad08c8868f27..9e1e15af52336 100644 --- a/client/finality-grandpa/src/tests.rs +++ b/client/finality-grandpa/src/tests.rs @@ -287,12 +287,10 @@ impl ApiExt for RuntimeApi { unimplemented!("Not required for testing!") } - fn into_storage_changes< - T: sp_api::ChangesTrieStorage, sp_api::NumberFor> - >( + fn into_storage_changes( &self, _: &Self::StateBackend, - _: Option<&T>, + _: Option<&sp_api::ChangesTrieState, sp_api::NumberFor>>, _: ::Hash, ) -> std::result::Result, String> where Self: Sized diff --git a/client/network/src/protocol/light_dispatch.rs b/client/network/src/protocol/light_dispatch.rs index dd94abc2a46fd..bfa8daa181ca1 100644 --- a/client/network/src/protocol/light_dispatch.rs +++ b/client/network/src/protocol/light_dispatch.rs @@ -691,7 +691,7 @@ pub mod tests { use crate::message::{self, BlockAttributes, Direction, FromBlock, RequestId}; use libp2p::PeerId; use super::{REQUEST_TIMEOUT, LightDispatch, LightDispatchNetwork, RequestData, StorageProof}; - use sp_test_primitives::{changes_trie_config, Block, Extrinsic, Header}; + use sp_test_primitives::{Block, Extrinsic, Header}; struct DummyFetchChecker { ok: bool } @@ -1095,7 +1095,11 @@ pub mod tests { let (tx, response) = oneshot::channel(); light_dispatch.add_request(&mut network_interface, RequestData::RemoteChanges(RemoteChangesRequest { - changes_trie_config: changes_trie_config(), + changes_trie_configs: vec![sp_core::ChangesTrieConfigurationRange { + zero: (0, Default::default()), + end: None, + config: Some(sp_core::ChangesTrieConfiguration::new(4, 2)), + }], first_block: (1, Default::default()), last_block: (100, Default::default()), max_block: (100, Default::default()), diff --git a/client/rpc/src/state/tests.rs b/client/rpc/src/state/tests.rs index 848e80d4fbd53..a0ab11e977204 100644 --- a/client/rpc/src/state/tests.rs +++ b/client/rpc/src/state/tests.rs @@ -21,7 +21,7 @@ use self::error::Error; use std::sync::Arc; use assert_matches::assert_matches; use futures01::stream::Stream; -use sp_core::storage::{well_known_keys, ChildInfo}; +use sp_core::{storage::{well_known_keys, ChildInfo}, ChangesTrieConfiguration}; use sp_core::hash::H256; use sp_io::hashing::blake2_256; use substrate_test_runtime_client::{ @@ -378,7 +378,9 @@ fn should_query_storage() { } run_tests(Arc::new(substrate_test_runtime_client::new())); - run_tests(Arc::new(TestClientBuilder::new().set_support_changes_trie(true).build())); + run_tests(Arc::new(TestClientBuilder::new() + .changes_trie_config(Some(ChangesTrieConfiguration::new(4, 2))) + .build())); } #[test] diff --git a/client/src/call_executor.rs b/client/src/call_executor.rs index f2095c33bb7dc..6c685fc1b82c7 100644 --- a/client/src/call_executor.rs +++ b/client/src/call_executor.rs @@ -17,7 +17,7 @@ use std::{sync::Arc, panic::UnwindSafe, result, cell::RefCell}; use codec::{Encode, Decode}; use sp_runtime::{ - generic::BlockId, traits::{Block as BlockT, HasherFor}, + generic::BlockId, traits::{Block as BlockT, HasherFor, NumberFor}, }; use sp_state_machine::{ self, OverlayedChanges, Ext, ExecutionManager, StateMachine, ExecutionStrategy, @@ -80,7 +80,7 @@ where let state = self.backend.state_at(*id)?; let return_data = StateMachine::new( &state, - self.backend.changes_trie_storage(), + backend::changes_tries_state_at_block(id, self.backend.changes_trie_storage())?, &mut changes, &self.executor, method, @@ -132,6 +132,7 @@ where } let mut state = self.backend.state_at(*at)?; + let changes_trie_state = backend::changes_tries_state_at_block(at, self.backend.changes_trie_storage())?; let mut storage_transaction_cache = storage_transaction_cache.map(|c| c.borrow_mut()); @@ -150,7 +151,7 @@ where StateMachine::new( &backend, - self.backend.changes_trie_storage(), + changes_trie_state, &mut *changes.borrow_mut(), &self.executor, method, @@ -163,7 +164,7 @@ where } None => StateMachine::new( &state, - self.backend.changes_trie_storage(), + changes_trie_state, &mut *changes.borrow_mut(), &self.executor, method, @@ -183,13 +184,13 @@ where fn runtime_version(&self, id: &BlockId) -> sp_blockchain::Result { let mut overlay = OverlayedChanges::default(); let state = self.backend.state_at(*id)?; + let changes_trie_state = backend::changes_tries_state_at_block(id, self.backend.changes_trie_storage())?; let mut cache = StorageTransactionCache::::default(); - let mut ext = Ext::new( &mut overlay, &mut cache, &state, - self.backend.changes_trie_storage(), + changes_trie_state, None, ); let version = self.executor.runtime_version(&mut ext); @@ -207,7 +208,7 @@ where method: &str, call_data: &[u8] ) -> Result<(Vec, StorageProof), sp_blockchain::Error> { - sp_state_machine::prove_execution_on_trie_backend( + sp_state_machine::prove_execution_on_trie_backend::<_, _, NumberFor, _>( trie_state, overlay, &self.executor, diff --git a/client/src/client.rs b/client/src/client.rs index efcbadb2b32e7..c74a005c6fea6 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -66,6 +66,7 @@ pub use sc_client_api::{ backend::{ self, BlockImportOperation, PrunableStateChangesTrieStorage, ClientImportOperation, Finalizer, ImportSummary, NewBlockState, + changes_tries_state_at_block, }, client::{ ImportNotifications, FinalityNotification, FinalityNotifications, BlockImportNotification, @@ -407,18 +408,26 @@ impl Client where first: NumberFor, last: BlockId, ) -> sp_blockchain::Result, BlockId)>> { - let (config, storage) = match self.require_changes_trie().ok() { - Some((config, storage)) => (config, storage), + let last_number = self.backend.blockchain().expect_block_number_from_id(&last)?; + let last_hash = self.backend.blockchain().expect_block_hash_from_id(&last)?; + if first > last_number { + return Err(sp_blockchain::Error::ChangesTrieAccessFailed("Invalid changes trie range".into())); + } + + let (storage, configs) = match self.require_changes_trie(first, last_hash, false).ok() { + Some((storage, configs)) => (storage, configs), None => return Ok(None), }; - let last_num = self.backend.blockchain().expect_block_number_from_id(&last)?; - if first > last_num { - return Err(sp_blockchain::Error::ChangesTrieAccessFailed("Invalid changes trie range".into())); + + let first_available_changes_trie = configs.last().map(|config| config.0); + match first_available_changes_trie { + Some(first_available_changes_trie) => { + let oldest_unpruned = storage.oldest_pruned_digest_range_end(); + let first = std::cmp::max(first_available_changes_trie, oldest_unpruned); + Ok(Some((first, last))) + }, + None => Ok(None) } - let finalized_number = self.backend.blockchain().info().finalized_number; - let oldest = storage.oldest_changes_trie_block(&config, finalized_number); - let first = ::std::cmp::max(first, oldest); - Ok(Some((first, last))) } /// Get pairs of (block, extrinsic) where key has been changed at given blocks range. @@ -432,30 +441,42 @@ impl Client where storage_key: Option<&StorageKey>, key: &StorageKey ) -> sp_blockchain::Result, u32)>> { - let (config, storage) = self.require_changes_trie()?; let last_number = self.backend.blockchain().expect_block_number_from_id(&last)?; let last_hash = self.backend.blockchain().expect_block_hash_from_id(&last)?; + let (storage, configs) = self.require_changes_trie(first, last_hash, true)?; + + let mut result = Vec::new(); + let best_number = self.backend.blockchain().info().best_number; + for (config_zero, config_end, config) in configs { + let range_first = ::std::cmp::max(first, config_zero + One::one()); + let range_anchor = match config_end { + Some((config_end_number, config_end_hash)) => if last_number > config_end_number { + ChangesTrieAnchorBlockId { hash: config_end_hash, number: config_end_number } + } else { + ChangesTrieAnchorBlockId { hash: convert_hash(&last_hash), number: last_number } + }, + None => ChangesTrieAnchorBlockId { hash: convert_hash(&last_hash), number: last_number }, + }; - // FIXME: remove this in https://github.com/paritytech/substrate/pull/3201 - let config_range = ChangesTrieConfigurationRange { - config: &config, - zero: Zero::zero(), - end: None, - }; + let config_range = ChangesTrieConfigurationRange { + config: &config, + zero: config_zero.clone(), + end: config_end.map(|(config_end_number, _)| config_end_number), + }; + let result_range: Vec<(NumberFor, u32)> = key_changes::, _>( + config_range, + storage.storage(), + range_first, + &range_anchor, + best_number, + storage_key.as_ref().map(|x| &x.0[..]), + &key.0) + .and_then(|r| r.map(|r| r.map(|(block, tx)| (block, tx))).collect::>()) + .map_err(|err| sp_blockchain::Error::ChangesTrieAccessFailed(err))?; + result.extend(result_range); + } - key_changes::, _>( - config_range, - &*storage, - first, - &ChangesTrieAnchorBlockId { - hash: convert_hash(&last_hash), - number: last_number, - }, - self.backend.blockchain().info().best_number, - storage_key.as_ref().map(|sk| sk.0.as_slice()), - &key.0) - .and_then(|r| r.map(|r| r.map(|(block, tx)| (block, tx))).collect::>()) - .map_err(|err| sp_blockchain::Error::ChangesTrieAccessFailed(err)) + Ok(result) } /// Get proof for computation of (block, extrinsic) pairs where key has been changed at given blocks range. @@ -550,11 +571,13 @@ impl Client where } } - let (config, storage) = self.require_changes_trie()?; + let first_number = self.backend.blockchain() + .expect_block_number_from_id(&BlockId::Hash(first))?; + let (storage, configs) = self.require_changes_trie(first_number, last, true)?; let min_number = self.backend.blockchain().expect_block_number_from_id(&BlockId::Hash(min))?; let recording_storage = AccessedRootsRecorder:: { - storage, + storage: storage.storage(), min: min_number, required_roots_proofs: Mutex::new(BTreeMap::new()), }; @@ -564,31 +587,31 @@ impl Client where self.backend.blockchain().expect_block_number_from_id(&BlockId::Hash(max))?, ); - // FIXME: remove this in https://github.com/paritytech/substrate/pull/3201 - let config_range = ChangesTrieConfigurationRange { - config: &config, - zero: Zero::zero(), - end: None, - }; - // fetch key changes proof - let first_number = self.backend.blockchain() - .expect_block_number_from_id(&BlockId::Hash(first))?; - let last_number = self.backend.blockchain() - .expect_block_number_from_id(&BlockId::Hash(last))?; - let key_changes_proof = key_changes_proof::, _>( - config_range, - &recording_storage, - first_number, - &ChangesTrieAnchorBlockId { - hash: convert_hash(&last), - number: last_number, - }, - max_number, - storage_key.as_ref().map(|sk| sk.0.as_slice()), - &key.0, - ) - .map_err(|err| sp_blockchain::Error::from(sp_blockchain::Error::ChangesTrieAccessFailed(err)))?; + let mut proof = Vec::new(); + for (config_zero, config_end, config) in configs { + let last_number = self.backend.blockchain() + .expect_block_number_from_id(&BlockId::Hash(last))?; + let config_range = ChangesTrieConfigurationRange { + config: &config, + zero: config_zero, + end: config_end.map(|(config_end_number, _)| config_end_number), + }; + let proof_range = key_changes_proof::, _>( + config_range, + &recording_storage, + first_number, + &ChangesTrieAnchorBlockId { + hash: convert_hash(&last), + number: last_number, + }, + max_number, + storage_key.as_ref().map(|x| &x.0[..]), + &key.0, + ) + .map_err(|err| sp_blockchain::Error::ChangesTrieAccessFailed(err))?; + proof.extend(proof_range); + } // now gather proofs for all changes tries roots that were touched during key_changes_proof // execution AND are unknown (i.e. replaced with CHT) to the requester @@ -597,7 +620,7 @@ impl Client where Ok(ChangesProof { max_block: max_number, - proof: key_changes_proof, + proof, roots: roots.into_iter().map(|(n, h)| (n, convert_hash(&h))).collect(), roots_proof, }) @@ -650,14 +673,45 @@ impl Client where Ok(proof) } - /// Returns changes trie configuration and storage or an error if it is not supported. - fn require_changes_trie(&self) -> sp_blockchain::Result<(ChangesTrieConfiguration, &B::ChangesTrieStorage)> { - let config = self.changes_trie_config()?; - let storage = self.backend.changes_trie_storage(); - match (config, storage) { - (Some(config), Some(storage)) => Ok((config, storage)), - _ => Err(sp_blockchain::Error::ChangesTriesNotSupported.into()), + /// Returns changes trie storage and all configurations that have been active in the range [first; last]. + /// + /// Configurations are returned in descending order (and obviously never overlap). + /// If fail_if_disabled is false, returns maximal consequent configurations ranges, starting from last and + /// stopping on either first, or when CT have been disabled. + /// If fail_if_disabled is true, fails when there's a subrange where CT have been disabled + /// inside first..last blocks range. + fn require_changes_trie( + &self, + first: NumberFor, + last: Block::Hash, + fail_if_disabled: bool, + ) -> sp_blockchain::Result<( + &dyn PrunableStateChangesTrieStorage, + Vec<(NumberFor, Option<(NumberFor, Block::Hash)>, ChangesTrieConfiguration)>, + )> { + let storage = match self.backend.changes_trie_storage() { + Some(storage) => storage, + None => return Err(sp_blockchain::Error::ChangesTriesNotSupported), + }; + + let mut configs = Vec::with_capacity(1); + let mut current = last; + loop { + let config_range = storage.configuration_at(&BlockId::Hash(current))?; + match config_range.config { + Some(config) => configs.push((config_range.zero.0, config_range.end, config)), + None if !fail_if_disabled => return Ok((storage, configs)), + None => return Err(sp_blockchain::Error::ChangesTriesNotSupported), + } + + if config_range.zero.0 < first { + break; + } + + current = *self.backend.blockchain().expect_header(BlockId::Hash(config_range.zero.1))?.parent_hash(); } + + Ok((storage, configs)) } /// Create a new block, built on the head of the chain. @@ -988,10 +1042,14 @@ impl Client where )?; let state = self.backend.state_at(at)?; + let changes_trie_state = changes_tries_state_at_block( + &at, + self.backend.changes_trie_storage(), + )?; let gen_storage_changes = runtime_api.into_storage_changes( &state, - self.backend.changes_trie_storage(), + changes_trie_state.as_ref(), *parent_hash, ); @@ -1249,13 +1307,6 @@ impl Client where Ok(uncles) } - fn changes_trie_config(&self) -> Result, Error> { - Ok(self.backend.state_at(BlockId::Number(self.backend.blockchain().info().best_number))? - .storage(well_known_keys::CHANGES_TRIE_CONFIG) - .map_err(|e| sp_blockchain::Error::from_state(Box::new(e)))? - .and_then(|c| Decode::decode(&mut &*c).ok())) - } - /// Prepare in-memory header that is used in execution environment. fn prepare_environment_block(&self, parent: &BlockId) -> sp_blockchain::Result { let parent_header = self.backend.blockchain().expect_header(*parent)?; @@ -1871,7 +1922,8 @@ pub(crate) mod tests { // prepare client ang import blocks let mut local_roots = Vec::new(); - let mut remote_client = TestClientBuilder::new().set_support_changes_trie(true).build(); + let config = Some(ChangesTrieConfiguration::new(4, 2)); + let mut remote_client = TestClientBuilder::new().changes_trie_config(config).build(); let mut nonces: HashMap<_, u64> = Default::default(); for (i, block_transfers) in blocks_transfers.into_iter().enumerate() { let mut builder = remote_client.new_block(Default::default()).unwrap(); @@ -2907,13 +2959,15 @@ pub(crate) mod tests { .unwrap().build().unwrap().block; client.import(BlockOrigin::Own, b2.clone()).unwrap(); + // prepare B3 before we finalize A2, because otherwise we won't be able to + // read changes trie configuration after A2 is finalized + let b3 = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default(), false) + .unwrap().build().unwrap().block; + // we will finalize A2 which should make it impossible to import a new // B3 at the same height but that doesnt't include it ClientExt::finalize_block(&client, BlockId::Hash(a2.hash()), None).unwrap(); - let b3 = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default(), false) - .unwrap().build().unwrap().block; - let import_err = client.import(BlockOrigin::Own, b3).err().unwrap(); let expected_err = ConsensusError::ClientImport( sp_blockchain::Error::NotInFinalizedChain.to_string() @@ -3050,4 +3104,104 @@ pub(crate) mod tests { check_block_b1.parent_hash = H256::random(); assert_eq!(client.check_block(check_block_b1.clone()).unwrap(), ImportResult::UnknownParent); } + + #[test] + fn imports_blocks_with_changes_tries_config_change() { + // create client with initial 4^2 configuration + let mut client = TestClientBuilder::with_default_backend() + .changes_trie_config(Some(ChangesTrieConfiguration { + digest_interval: 4, + digest_levels: 2, + })).build(); + + // =================================================================== + // blocks 1,2,3,4,5,6,7,8,9,10 are empty + // block 11 changes the key + // block 12 is the L1 digest that covers this change + // blocks 13,14,15,16,17,18,19,20,21,22 are empty + // block 23 changes the configuration to 5^1 AND is skewed digest + // =================================================================== + // blocks 24,25 are changing the key + // block 26 is empty + // block 27 changes the key + // block 28 is the L1 digest (NOT SKEWED!!!) that covers changes AND changes configuration to 3^1 + // =================================================================== + // block 29 is empty + // block 30 changes the key + // block 31 is L1 digest that covers this change + // =================================================================== + (1..11).for_each(|number| { + let block = client.new_block_at(&BlockId::Number(number - 1), Default::default(), false) + .unwrap().build().unwrap().block; + client.import(BlockOrigin::Own, block).unwrap(); + }); + (11..12).for_each(|number| { + let mut block = client.new_block_at(&BlockId::Number(number - 1), Default::default(), false).unwrap(); + block.push_storage_change(vec![42], Some(number.to_le_bytes().to_vec())).unwrap(); + let block = block.build().unwrap().block; + client.import(BlockOrigin::Own, block).unwrap(); + }); + (12..23).for_each(|number| { + let block = client.new_block_at(&BlockId::Number(number - 1), Default::default(), false) + .unwrap().build().unwrap().block; + client.import(BlockOrigin::Own, block).unwrap(); + }); + (23..24).for_each(|number| { + let mut block = client.new_block_at(&BlockId::Number(number - 1), Default::default(), false).unwrap(); + block.push_changes_trie_configuration_update(Some(ChangesTrieConfiguration { + digest_interval: 5, + digest_levels: 1, + })).unwrap(); + let block = block.build().unwrap().block; + client.import(BlockOrigin::Own, block).unwrap(); + }); + (24..26).for_each(|number| { + let mut block = client.new_block_at(&BlockId::Number(number - 1), Default::default(), false).unwrap(); + block.push_storage_change(vec![42], Some(number.to_le_bytes().to_vec())).unwrap(); + let block = block.build().unwrap().block; + client.import(BlockOrigin::Own, block).unwrap(); + }); + (26..27).for_each(|number| { + let block = client.new_block_at(&BlockId::Number(number - 1), Default::default(), false) + .unwrap().build().unwrap().block; + client.import(BlockOrigin::Own, block).unwrap(); + }); + (27..28).for_each(|number| { + let mut block = client.new_block_at(&BlockId::Number(number - 1), Default::default(), false).unwrap(); + block.push_storage_change(vec![42], Some(number.to_le_bytes().to_vec())).unwrap(); + let block = block.build().unwrap().block; + client.import(BlockOrigin::Own, block).unwrap(); + }); + (28..29).for_each(|number| { + let mut block = client.new_block_at(&BlockId::Number(number - 1), Default::default(), false).unwrap(); + block.push_changes_trie_configuration_update(Some(ChangesTrieConfiguration { + digest_interval: 3, + digest_levels: 1, + })).unwrap(); + let block = block.build().unwrap().block; + client.import(BlockOrigin::Own, block).unwrap(); + }); + (29..30).for_each(|number| { + let block = client.new_block_at(&BlockId::Number(number - 1), Default::default(), false) + .unwrap().build().unwrap().block; + client.import(BlockOrigin::Own, block).unwrap(); + }); + (30..31).for_each(|number| { + let mut block = client.new_block_at(&BlockId::Number(number - 1), Default::default(), false).unwrap(); + block.push_storage_change(vec![42], Some(number.to_le_bytes().to_vec())).unwrap(); + let block = block.build().unwrap().block; + client.import(BlockOrigin::Own, block).unwrap(); + }); + (31..32).for_each(|number| { + let block = client.new_block_at(&BlockId::Number(number - 1), Default::default(), false) + .unwrap().build().unwrap().block; + client.import(BlockOrigin::Own, block).unwrap(); + }); + + // now check that configuration cache works + assert_eq!( + client.key_changes(1, BlockId::Number(31), None, &StorageKey(vec![42])).unwrap(), + vec![(30, 0), (27, 0), (25, 0), (24, 0), (11, 0)] + ); + } } diff --git a/client/src/genesis.rs b/client/src/genesis.rs index 6dd6e2ec43bb5..fccdd71817e1f 100644 --- a/client/src/genesis.rs +++ b/client/src/genesis.rs @@ -45,7 +45,7 @@ mod tests { use codec::{Encode, Decode, Joiner}; use sc_executor::native_executor_instance; use sp_state_machine::{ - StateMachine, OverlayedChanges, ExecutionStrategy, InMemoryChangesTrieStorage, + StateMachine, OverlayedChanges, ExecutionStrategy, InMemoryBackend, }; use substrate_test_runtime_client::{ @@ -92,7 +92,7 @@ mod tests { StateMachine::new( backend, - Some(&InMemoryChangesTrieStorage::<_, u64>::new()), + sp_state_machine::disabled_changes_trie_state::<_, u64>(), &mut overlay, &executor(), "Core_initialize_block", @@ -105,7 +105,7 @@ mod tests { for tx in transactions.iter() { StateMachine::new( backend, - Some(&InMemoryChangesTrieStorage::<_, u64>::new()), + sp_state_machine::disabled_changes_trie_state::<_, u64>(), &mut overlay, &executor(), "BlockBuilder_apply_extrinsic", @@ -118,7 +118,7 @@ mod tests { let ret_data = StateMachine::new( backend, - Some(&InMemoryChangesTrieStorage::<_, u64>::new()), + sp_state_machine::disabled_changes_trie_state::<_, u64>(), &mut overlay, &executor(), "BlockBuilder_finalize_block", @@ -150,7 +150,7 @@ mod tests { #[test] fn construct_genesis_should_work_with_native() { let mut storage = GenesisConfig::new( - false, + None, vec![Sr25519Keyring::One.public().into(), Sr25519Keyring::Two.public().into()], vec![AccountKeyring::One.into(), AccountKeyring::Two.into()], 1000, @@ -165,7 +165,7 @@ mod tests { let mut overlay = OverlayedChanges::default(); let _ = StateMachine::new( &backend, - Some(&InMemoryChangesTrieStorage::<_, u64>::new()), + sp_state_machine::disabled_changes_trie_state::<_, u64>(), &mut overlay, &executor(), "Core_execute_block", @@ -178,7 +178,7 @@ mod tests { #[test] fn construct_genesis_should_work_with_wasm() { - let mut storage = GenesisConfig::new(false, + let mut storage = GenesisConfig::new(None, vec![Sr25519Keyring::One.public().into(), Sr25519Keyring::Two.public().into()], vec![AccountKeyring::One.into(), AccountKeyring::Two.into()], 1000, @@ -193,7 +193,7 @@ mod tests { let mut overlay = OverlayedChanges::default(); let _ = StateMachine::new( &backend, - Some(&InMemoryChangesTrieStorage::<_, u64>::new()), + sp_state_machine::disabled_changes_trie_state::<_, u64>(), &mut overlay, &executor(), "Core_execute_block", @@ -206,7 +206,7 @@ mod tests { #[test] fn construct_genesis_with_bad_transaction_should_panic() { - let mut storage = GenesisConfig::new(false, + let mut storage = GenesisConfig::new(None, vec![Sr25519Keyring::One.public().into(), Sr25519Keyring::Two.public().into()], vec![AccountKeyring::One.into(), AccountKeyring::Two.into()], 68, @@ -221,7 +221,7 @@ mod tests { let mut overlay = OverlayedChanges::default(); let r = StateMachine::new( &backend, - Some(&InMemoryChangesTrieStorage::<_, u64>::new()), + sp_state_machine::disabled_changes_trie_state::<_, u64>(), &mut overlay, &executor(), "Core_execute_block", diff --git a/client/src/in_mem.rs b/client/src/in_mem.rs index 5d0d14c1cb1d7..42a5e90101b97 100644 --- a/client/src/in_mem.rs +++ b/client/src/in_mem.rs @@ -16,22 +16,17 @@ //! In memory client backend -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use std::sync::Arc; use parking_lot::RwLock; -use sp_core::{ChangesTrieConfiguration, storage::well_known_keys}; +use sp_core::storage::well_known_keys; use sp_core::offchain::storage::{ InMemOffchainStorage as OffchainStorage }; -use sp_runtime::generic::{BlockId, DigestItem}; +use sp_runtime::generic::BlockId; use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Zero, NumberFor, HasherFor}; use sp_runtime::{Justification, Storage}; -use sp_state_machine::{ - InMemoryChangesTrieStorage, ChangesTrieAnchorBlockId, ChangesTrieTransaction, - InMemoryBackend, Backend as StateBackend, -}; -use hash_db::Prefix; -use sp_trie::MemoryDB; +use sp_state_machine::{ChangesTrieTransaction, InMemoryBackend, Backend as StateBackend}; use sp_blockchain::{CachedHeaderMetadata, HeaderMetadata}; use sc_client_api::{ @@ -466,7 +461,6 @@ pub struct BlockImportOperation { pending_cache: HashMap>, old_state: InMemoryBackend>, new_state: Option>>, - changes_trie_update: Option>>, aux: Vec<(Vec, Option>)>, finalized_blocks: Vec<(BlockId, Option)>, set_head: Option>, @@ -510,9 +504,8 @@ impl backend::BlockImportOperation for BlockImportOperatio fn update_changes_trie( &mut self, - update: ChangesTrieTransaction, NumberFor>, + _update: ChangesTrieTransaction, NumberFor>, ) -> sp_blockchain::Result<()> { - self.changes_trie_update = Some(update.0); Ok(()) } @@ -569,7 +562,6 @@ impl backend::BlockImportOperation for BlockImportOperatio /// > struct for testing purposes. Do **NOT** use in production. pub struct Backend where Block::Hash: Ord { states: RwLock>>>, - changes_trie_storage: ChangesTrieStorage, blockchain: Blockchain, import_lock: RwLock<()>, } @@ -579,7 +571,6 @@ impl Backend where Block::Hash: Ord { pub fn new() -> Self { Backend { states: RwLock::new(HashMap::new()), - changes_trie_storage: ChangesTrieStorage(InMemoryChangesTrieStorage::new()), blockchain: Blockchain::new(), import_lock: Default::default(), } @@ -606,7 +597,6 @@ impl backend::Backend for Backend where Block::Hash type BlockImportOperation = BlockImportOperation; type Blockchain = Blockchain; type State = InMemoryBackend>; - type ChangesTrieStorage = ChangesTrieStorage; type OffchainStorage = OffchainStorage; fn begin_operation(&self) -> sp_blockchain::Result { @@ -616,7 +606,6 @@ impl backend::Backend for Backend where Block::Hash pending_cache: Default::default(), old_state, new_state: None, - changes_trie_update: None, aux: Default::default(), finalized_blocks: Default::default(), set_head: None, @@ -647,17 +636,6 @@ impl backend::Backend for Backend where Block::Hash self.states.write().insert(hash, operation.new_state.unwrap_or_else(|| old_state.clone())); - let maybe_changes_trie_root = header.digest().log(DigestItem::as_changes_trie_root).cloned(); - if let Some(changes_trie_root) = maybe_changes_trie_root { - if let Some(changes_trie_update) = operation.changes_trie_update { - self.changes_trie_storage.0.insert( - *header.number(), - changes_trie_root, - changes_trie_update - ); - } - } - self.blockchain.insert(hash, header, justification, body, pending_block.state)?; } @@ -688,8 +666,8 @@ impl backend::Backend for Backend where Block::Hash None } - fn changes_trie_storage(&self) -> Option<&Self::ChangesTrieStorage> { - Some(&self.changes_trie_storage) + fn changes_trie_storage(&self) -> Option<&dyn backend::PrunableStateChangesTrieStorage> { + None } fn offchain_storage(&self) -> Option { @@ -737,66 +715,6 @@ impl backend::RemoteBackend for Backend where Block } } -/// Prunable in-memory changes trie storage. -pub struct ChangesTrieStorage( - InMemoryChangesTrieStorage, NumberFor> -); - -impl backend::PrunableStateChangesTrieStorage for ChangesTrieStorage { - fn oldest_changes_trie_block( - &self, - _config: &ChangesTrieConfiguration, - _best_finalized: NumberFor, - ) -> NumberFor { - Zero::zero() - } -} - -impl sp_state_machine::ChangesTrieRootsStorage, NumberFor> for - ChangesTrieStorage -{ - fn build_anchor( - &self, - _hash: Block::Hash, - ) -> Result>, String> { - Err("Dummy implementation".into()) - } - - fn root( - &self, - _anchor: &ChangesTrieAnchorBlockId>, - _block: NumberFor, - ) -> Result, String> { - Err("Dummy implementation".into()) - } -} - -impl sp_state_machine::ChangesTrieStorage, NumberFor> for - ChangesTrieStorage -{ - fn as_roots_storage(&self) - -> &dyn sp_state_machine::ChangesTrieRootsStorage, NumberFor> - { - self - } - - fn with_cached_changed_keys( - &self, - _root: &Block::Hash, - _functor: &mut dyn FnMut(&HashMap>, HashSet>>), - ) -> bool { - false - } - - fn get( - &self, - key: &Block::Hash, - prefix: Prefix, - ) -> Result, String> { - self.0.get(key, prefix) - } -} - /// Check that genesis storage is valid. pub fn check_genesis_storage(storage: &Storage) -> sp_blockchain::Result<()> { if storage.top.iter().any(|(k, _)| well_known_keys::is_child_storage_key(k)) { diff --git a/client/src/light/backend.rs b/client/src/light/backend.rs index 65fd94e32395c..2afb269d48838 100644 --- a/client/src/light/backend.rs +++ b/client/src/light/backend.rs @@ -21,19 +21,22 @@ use std::collections::HashMap; use std::sync::Arc; use parking_lot::RwLock; -use sp_core::storage::{ChildInfo, OwnedChildInfo}; +use codec::{Decode, Encode}; + +use sp_core::ChangesTrieConfiguration; +use sp_core::storage::{well_known_keys, ChildInfo, OwnedChildInfo}; use sp_core::offchain::storage::InMemOffchainStorage; use sp_state_machine::{ Backend as StateBackend, TrieBackend, InMemoryBackend, ChangesTrieTransaction }; use sp_runtime::{generic::BlockId, Justification, Storage}; use sp_runtime::traits::{Block as BlockT, NumberFor, Zero, Header, HasherFor}; -use crate::in_mem::{self, check_genesis_storage}; +use crate::in_mem::check_genesis_storage; use sp_blockchain::{Error as ClientError, Result as ClientResult}; use sc_client_api::{ backend::{ AuxStore, Backend as ClientBackend, BlockImportOperation, RemoteBackend, NewBlockState, - StorageCollection, ChildStorageCollection, + StorageCollection, ChildStorageCollection, PrunableStateChangesTrieStorage, }, blockchain::{ HeaderBackend as BlockchainHeaderBackend, well_known_cache_keys, @@ -62,6 +65,7 @@ pub struct ImportOperation { finalized_blocks: Vec>, set_head: Option>, storage_update: Option>>, + changes_trie_config_update: Option>, _phantom: std::marker::PhantomData, } @@ -115,7 +119,6 @@ impl ClientBackend for Backend> type BlockImportOperation = ImportOperation; type Blockchain = Blockchain; type State = GenesisOrUnavailableState>; - type ChangesTrieStorage = in_mem::ChangesTrieStorage; type OffchainStorage = InMemOffchainStorage; fn begin_operation(&self) -> ClientResult { @@ -127,6 +130,7 @@ impl ClientBackend for Backend> finalized_blocks: Vec::new(), set_head: None, storage_update: None, + changes_trie_config_update: None, _phantom: Default::default(), }) } @@ -148,6 +152,9 @@ impl ClientBackend for Backend> if let Some(header) = operation.header { let is_genesis_import = header.number().is_zero(); + if let Some(new_config) = operation.changes_trie_config_update { + operation.cache.insert(well_known_cache_keys::CHANGES_TRIE_CONFIG, new_config.encode()); + } self.blockchain.storage().import_header( header, operation.cache, @@ -194,7 +201,7 @@ impl ClientBackend for Backend> self.blockchain.storage().usage_info() } - fn changes_trie_storage(&self) -> Option<&Self::ChangesTrieStorage> { + fn changes_trie_storage(&self) -> Option<&dyn PrunableStateChangesTrieStorage> { None } @@ -296,6 +303,13 @@ impl BlockImportOperation for ImportOperation fn reset_storage(&mut self, input: Storage) -> ClientResult { check_genesis_storage(&input)?; + // changes trie configuration + let changes_trie_config = input.top.iter() + .find(|(k, _)| &k[..] == well_known_keys::CHANGES_TRIE_CONFIG) + .map(|(_, v)| Decode::decode(&mut &v[..]) + .expect("changes trie configuration is encoded properly at genesis")); + self.changes_trie_config_update = Some(changes_trie_config); + // this is only called when genesis block is imported => shouldn't be performance bottleneck let mut storage: HashMap, OwnedChildInfo)>, _> = HashMap::new(); storage.insert(None, input.top); diff --git a/client/src/light/call_executor.rs b/client/src/light/call_executor.rs index 9ac88103e8e04..01a93c78219bc 100644 --- a/client/src/light/call_executor.rs +++ b/client/src/light/call_executor.rs @@ -259,7 +259,7 @@ fn check_execution_proof_with_make_header( + execution_proof_check_on_trie_backend::( &trie_backend, &mut changes, executor, @@ -268,7 +268,7 @@ fn check_execution_proof_with_make_header( + execution_proof_check_on_trie_backend::( &trie_backend, &mut changes, executor, diff --git a/client/src/light/fetcher.rs b/client/src/light/fetcher.rs index a35bfdc25cce9..d66108b7f0adb 100644 --- a/client/src/light/fetcher.rs +++ b/client/src/light/fetcher.rs @@ -25,12 +25,12 @@ use codec::{Decode, Encode}; use sp_core::{convert_hash, traits::CodeExecutor}; use sp_runtime::traits::{ Block as BlockT, Header as HeaderT, Hash, HashFor, NumberFor, - SimpleArithmetic, CheckedConversion, Zero, + SimpleArithmetic, CheckedConversion, }; use sp_state_machine::{ ChangesTrieRootsStorage, ChangesTrieAnchorBlockId, ChangesTrieConfigurationRange, - TrieBackend, read_proof_check, key_changes_proof_check, create_proof_check_backend_storage, - read_child_proof_check, + InMemoryChangesTrieStorage, TrieBackend, read_proof_check, key_changes_proof_check_with_db, + create_proof_check_backend_storage, read_child_proof_check, }; pub use sp_state_machine::StorageProof; use sp_blockchain::{Error as ClientError, Result as ClientResult}; @@ -113,30 +113,34 @@ impl> LightDataChecker { )?; } - // FIXME: remove this in https://github.com/paritytech/substrate/pull/3201 - let changes_trie_config_range = ChangesTrieConfigurationRange { - config: &request.changes_trie_config, - zero: Zero::zero(), - end: None, - }; - // and now check the key changes proof + get the changes - key_changes_proof_check::( - changes_trie_config_range, - &RootsStorage { - roots: (request.tries_roots.0, &request.tries_roots.2), - prev_roots: remote_roots, - }, - remote_proof, - request.first_block.0, - &ChangesTrieAnchorBlockId { - hash: convert_hash(&request.last_block.1), - number: request.last_block.0, - }, - remote_max_block, - request.storage_key.as_ref().map(Vec::as_slice), - &request.key) - .map_err(|err| ClientError::ChangesTrieAccessFailed(err)) + let mut result = Vec::new(); + let proof_storage = InMemoryChangesTrieStorage::with_proof(remote_proof); + for config_range in &request.changes_trie_configs { + let result_range = key_changes_proof_check_with_db::( + ChangesTrieConfigurationRange { + config: config_range.config.as_ref().ok_or(ClientError::ChangesTriesNotSupported)?, + zero: config_range.zero.0, + end: config_range.end.map(|(n, _)| n), + }, + &RootsStorage { + roots: (request.tries_roots.0, &request.tries_roots.2), + prev_roots: &remote_roots, + }, + &proof_storage, + request.first_block.0, + &ChangesTrieAnchorBlockId { + hash: convert_hash(&request.last_block.1), + number: request.last_block.0, + }, + remote_max_block, + request.storage_key.as_ref().map(Vec::as_slice), + &request.key) + .map_err(|err| ClientError::ChangesTrieAccessFailed(err))?; + result.extend(result_range); + } + + Ok(result) } /// Check CHT-based proof for changes tries roots. @@ -284,7 +288,7 @@ impl FetchChecker for LightDataChecker /// A view of BTreeMap as a changes trie roots storage. struct RootsStorage<'a, Number: SimpleArithmetic, Hash: 'a> { roots: (Number, &'a [Hash]), - prev_roots: BTreeMap, + prev_roots: &'a BTreeMap, } impl<'a, H, Number, Hash> ChangesTrieRootsStorage for RootsStorage<'a, Number, Hash> @@ -340,7 +344,7 @@ pub mod tests { use crate::in_mem::{Blockchain as InMemoryBlockchain}; use crate::light::fetcher::{FetchChecker, LightDataChecker, RemoteHeaderRequest}; use crate::light::blockchain::tests::{DummyStorage, DummyBlockchain}; - use sp_core::{blake2_256, Blake2Hasher, H256}; + use sp_core::{blake2_256, Blake2Hasher, ChangesTrieConfiguration, H256}; use sp_core::storage::{well_known_keys, StorageKey, ChildInfo}; use sp_runtime::generic::BlockId; use sp_state_machine::Backend; @@ -569,8 +573,13 @@ pub mod tests { // check proof on local client let local_roots_range = local_roots.clone()[(begin - 1) as usize..].to_vec(); + let config = ChangesTrieConfiguration::new(4, 2); let request = RemoteChangesRequest::
{ - changes_trie_config: runtime::changes_trie_config(), + changes_trie_configs: vec![sp_core::ChangesTrieConfigurationRange { + zero: (0, Default::default()), + end: None, + config: Some(config), + }], first_block: (begin, begin_hash), last_block: (end, end_hash), max_block: (max, max_hash), @@ -624,8 +633,13 @@ pub mod tests { ); // check proof on local client + let config = ChangesTrieConfiguration::new(4, 2); let request = RemoteChangesRequest::
{ - changes_trie_config: runtime::changes_trie_config(), + changes_trie_configs: vec![sp_core::ChangesTrieConfigurationRange { + zero: (0, Default::default()), + end: None, + config: Some(config), + }], first_block: (1, b1), last_block: (4, b4), max_block: (4, b4), @@ -665,8 +679,13 @@ pub mod tests { begin_hash, end_hash, begin_hash, max_hash, None, &key).unwrap(); let local_roots_range = local_roots.clone()[(begin - 1) as usize..].to_vec(); + let config = ChangesTrieConfiguration::new(4, 2); let request = RemoteChangesRequest::
{ - changes_trie_config: runtime::changes_trie_config(), + changes_trie_configs: vec![sp_core::ChangesTrieConfigurationRange { + zero: (0, Default::default()), + end: None, + config: Some(config), + }], first_block: (begin, begin_hash), last_block: (end, end_hash), max_block: (max, max_hash), diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 174eb733afb30..478fd780b98fa 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -110,7 +110,7 @@ use sp_runtime::{ }, }; -use sp_core::storage::well_known_keys; +use sp_core::{ChangesTrieConfiguration, storage::well_known_keys}; use frame_support::{ decl_module, decl_event, decl_storage, decl_error, storage, Parameter, traits::{Contains, Get, ModuleToIndex, OnReapAccount}, @@ -121,9 +121,6 @@ use codec::{Encode, Decode}; #[cfg(any(feature = "std", test))] use sp_io::TestExternalities; -#[cfg(any(feature = "std", test))] -use sp_core::ChangesTrieConfiguration; - pub mod offchain; /// Handler for when a new account has been created. @@ -293,6 +290,24 @@ decl_module! { storage::unhashed::put_raw(well_known_keys::CODE, &code); } + /// Set the new changes trie configuration. + #[weight = SimpleDispatchInfo::FixedOperational(20_000)] + pub fn set_changes_trie_config(origin, changes_trie_config: Option) { + ensure_root(origin)?; + match changes_trie_config.clone() { + Some(changes_trie_config) => storage::unhashed::put_raw( + well_known_keys::CHANGES_TRIE_CONFIG, + &changes_trie_config.encode(), + ), + None => storage::unhashed::kill(well_known_keys::CHANGES_TRIE_CONFIG), + } + + let log = generic::DigestItem::ChangesTrieSignal( + generic::ChangesTrieSignal::NewConfiguration(changes_trie_config), + ); + Self::deposit_log(log.into()); + } + /// Set some items of storage. #[weight = SimpleDispatchInfo::FixedOperational(10_000)] fn set_storage(origin, items: Vec) { diff --git a/primitives/api/proc-macro/src/impl_runtime_apis.rs b/primitives/api/proc-macro/src/impl_runtime_apis.rs index dc4031fbc4b8a..770a843bfa6c1 100644 --- a/primitives/api/proc-macro/src/impl_runtime_apis.rs +++ b/primitives/api/proc-macro/src/impl_runtime_apis.rs @@ -323,12 +323,13 @@ fn generate_runtime_api_base_structures() -> Result { }) } - fn into_storage_changes< - T: #crate_::ChangesTrieStorage<#crate_::HasherFor, #crate_::NumberFor> - >( + fn into_storage_changes( &self, backend: &Self::StateBackend, - changes_trie_storage: Option<&T>, + changes_trie_state: Option<&#crate_::ChangesTrieState< + #crate_::HasherFor, + #crate_::NumberFor, + >>, parent_hash: Block::Hash, ) -> std::result::Result< #crate_::StorageChanges, @@ -337,7 +338,7 @@ fn generate_runtime_api_base_structures() -> Result { self.initialized_block.borrow_mut().take(); self.changes.replace(Default::default()).into_storage_changes( backend, - changes_trie_storage, + changes_trie_state, parent_hash, self.storage_transaction_cache.replace(Default::default()), ) diff --git a/primitives/api/src/lib.rs b/primitives/api/src/lib.rs index 122e863228966..bde00d48172e8 100644 --- a/primitives/api/src/lib.rs +++ b/primitives/api/src/lib.rs @@ -36,7 +36,7 @@ extern crate self as sp_api; #[doc(hidden)] #[cfg(feature = "std")] pub use sp_state_machine::{ - OverlayedChanges, StorageProof, Backend as StateBackend, ChangesTrieStorage, + OverlayedChanges, StorageProof, Backend as StateBackend, ChangesTrieState, }; #[doc(hidden)] #[cfg(feature = "std")] @@ -325,10 +325,10 @@ pub trait ApiExt: ApiErrorExt { /// api functions. /// /// After executing this function, all collected changes are reset. - fn into_storage_changes, NumberFor>>( + fn into_storage_changes( &self, backend: &Self::StateBackend, - changes_trie_storage: Option<&T>, + changes_trie_state: Option<&ChangesTrieState, NumberFor>>, parent_hash: Block::Hash, ) -> Result, String> where Self: Sized; } diff --git a/primitives/api/test/tests/runtime_calls.rs b/primitives/api/test/tests/runtime_calls.rs index f45df7c517254..66412edae026d 100644 --- a/primitives/api/test/tests/runtime_calls.rs +++ b/primitives/api/test/tests/runtime_calls.rs @@ -185,7 +185,7 @@ fn record_proof_works() { // Use the proof backend to execute `execute_block`. let mut overlay = Default::default(); let executor = NativeExecutor::::new(WasmExecutionMethod::Interpreted, None); - execution_proof_check_on_trie_backend( + execution_proof_check_on_trie_backend::<_, u64, _>( &backend, &mut overlay, &executor, diff --git a/primitives/blockchain/src/backend.rs b/primitives/blockchain/src/backend.rs index 8571d76801079..101dcd1443d6b 100644 --- a/primitives/blockchain/src/backend.rs +++ b/primitives/blockchain/src/backend.rs @@ -228,11 +228,13 @@ pub trait Cache: Send + Sync { /// Returns cached value by the given key. /// /// Returned tuple is the range where value has been active and the value itself. + /// Fails if read from cache storage fails or if the value for block is discarded + /// (i.e. if block is earlier that best finalized, but it is not in canonical chain). fn get_at( &self, key: &well_known_cache_keys::Id, block: &BlockId, - ) -> Option<((NumberFor, Block::Hash), Option<(NumberFor, Block::Hash)>, Vec)>; + ) -> Result, Block::Hash), Option<(NumberFor, Block::Hash)>, Vec)>>; } /// Blockchain info diff --git a/primitives/blockchain/src/error.rs b/primitives/blockchain/src/error.rs index dbab93738e79f..d51ab787c72fb 100644 --- a/primitives/blockchain/src/error.rs +++ b/primitives/blockchain/src/error.rs @@ -106,6 +106,9 @@ pub enum Error { /// Changes tries are not supported. #[display(fmt = "Changes tries are not supported by the runtime")] ChangesTriesNotSupported, + /// Error reading changes tries configuration. + #[display(fmt = "Error reading changes tries configuration")] + ErrorReadingChangesTriesConfig, /// Key changes query has failed. #[display(fmt = "Failed to check changes proof: {}", _0)] #[from(ignore)] diff --git a/primitives/core/src/changes_trie.rs b/primitives/core/src/changes_trie.rs index 65619a9d45315..d38761ccf0fd1 100644 --- a/primitives/core/src/changes_trie.rs +++ b/primitives/core/src/changes_trie.rs @@ -38,6 +38,17 @@ pub struct ChangesTrieConfiguration { pub digest_levels: u32, } +/// Substrate changes trie configuration range. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ChangesTrieConfigurationRange { + /// Zero block of configuration. + pub zero: (Number, Hash), + /// Last block of configuration (if configuration has been deactivated at some point). + pub end: Option<(Number, Hash)>, + /// The configuration itself. None if changes tries were disabled in this range. + pub config: Option, +} + impl ChangesTrieConfiguration { /// Create new configuration given digest interval and levels. pub fn new(digest_interval: u32, digest_levels: u32) -> Self { diff --git a/primitives/core/src/lib.rs b/primitives/core/src/lib.rs index 32e65de40dbaa..ee768ee4a6226 100644 --- a/primitives/core/src/lib.rs +++ b/primitives/core/src/lib.rs @@ -75,7 +75,7 @@ mod tests; pub use self::hash::{H160, H256, H512, convert_hash}; pub use self::uint::U256; -pub use changes_trie::ChangesTrieConfiguration; +pub use changes_trie::{ChangesTrieConfiguration, ChangesTrieConfigurationRange}; #[cfg(feature = "full_crypto")] pub use crypto::{DeriveJunction, Pair, Public}; diff --git a/primitives/runtime/src/generic/digest.rs b/primitives/runtime/src/generic/digest.rs index 6653cfb8a86b3..fef02d4f00959 100644 --- a/primitives/runtime/src/generic/digest.rs +++ b/primitives/runtime/src/generic/digest.rs @@ -23,7 +23,7 @@ use sp_std::prelude::*; use crate::ConsensusEngineId; use crate::codec::{Decode, Encode, Input, Error}; -use sp_core::RuntimeDebug; +use sp_core::{ChangesTrieConfiguration, RuntimeDebug}; /// Generic header digest. #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)] @@ -97,10 +97,32 @@ pub enum DigestItem { /// by runtimes. Seal(ConsensusEngineId, Vec), + /// Digest item that contains signal from changes tries manager to the + /// native code. + ChangesTrieSignal(ChangesTrieSignal), + /// Some other thing. Unsupported and experimental. Other(Vec), } +/// Available changes trie signals. +#[derive(PartialEq, Eq, Clone, Encode, Decode)] +#[cfg_attr(feature = "std", derive(Debug))] +pub enum ChangesTrieSignal { + /// New changes trie configuration is enacted, starting from **next block**. + /// + /// The block that emits this signal will contain changes trie (CT) that covers + /// blocks range [BEGIN; current block], where BEGIN is (order matters): + /// - LAST_TOP_LEVEL_DIGEST_BLOCK+1 if top level digest CT has ever been created + /// using current configuration AND the last top level digest CT has been created + /// at block LAST_TOP_LEVEL_DIGEST_BLOCK; + /// - LAST_CONFIGURATION_CHANGE_BLOCK+1 if there has been CT configuration change + /// before and the last configuration change happened at block + /// LAST_CONFIGURATION_CHANGE_BLOCK; + /// - 1 otherwise. + NewConfiguration(Option), +} + #[cfg(feature = "std")] impl serde::Serialize for DigestItem { fn serialize(&self, seq: S) -> Result where S: serde::Serializer { @@ -141,6 +163,9 @@ pub enum DigestItemRef<'a, Hash: 'a> { /// Put a Seal on it. This is only used by native code, and is never seen /// by runtimes. Seal(&'a ConsensusEngineId, &'a Vec), + /// Digest item that contains signal from changes tries manager to the + /// native code. + ChangesTrieSignal(&'a ChangesTrieSignal), /// Any 'non-system' digest item, opaque to the native code. Other(&'a Vec), } @@ -152,11 +177,12 @@ pub enum DigestItemRef<'a, Hash: 'a> { #[repr(u32)] #[derive(Encode, Decode)] pub enum DigestItemType { + Other = 0, ChangesTrieRoot = 2, - PreRuntime = 6, Consensus = 4, Seal = 5, - Other = 0, + PreRuntime = 6, + ChangesTrieSignal = 7, } /// Type of a digest item that contains raw data; this also names the consensus engine ID where @@ -181,6 +207,7 @@ impl DigestItem { DigestItem::PreRuntime(ref v, ref s) => DigestItemRef::PreRuntime(v, s), DigestItem::Consensus(ref v, ref s) => DigestItemRef::Consensus(v, s), DigestItem::Seal(ref v, ref s) => DigestItemRef::Seal(v, s), + DigestItem::ChangesTrieSignal(ref s) => DigestItemRef::ChangesTrieSignal(s), DigestItem::Other(ref v) => DigestItemRef::Other(v), } } @@ -205,6 +232,11 @@ impl DigestItem { self.dref().as_seal() } + /// Returns `Some` if the entry is the `ChangesTrieSignal` entry. + pub fn as_changes_trie_signal(&self) -> Option<&ChangesTrieSignal> { + self.dref().as_changes_trie_signal() + } + /// Returns Some if `self` is a `DigestItem::Other`. pub fn as_other(&self) -> Option<&[u8]> { match *self { @@ -253,6 +285,9 @@ impl Decode for DigestItem { let vals: (ConsensusEngineId, Vec) = Decode::decode(input)?; Ok(DigestItem::Seal(vals.0, vals.1)) }, + DigestItemType::ChangesTrieSignal => Ok(DigestItem::ChangesTrieSignal( + Decode::decode(input)?, + )), DigestItemType::Other => Ok(DigestItem::Other( Decode::decode(input)?, )), @@ -293,6 +328,14 @@ impl<'a, Hash> DigestItemRef<'a, Hash> { } } + /// Cast this digest item into `ChangesTrieSignal`. + pub fn as_changes_trie_signal(&self) -> Option<&'a ChangesTrieSignal> { + match *self { + DigestItemRef::ChangesTrieSignal(ref changes_trie_signal) => Some(changes_trie_signal), + _ => None, + } + } + /// Cast this digest item into `PreRuntime` pub fn as_other(&self) -> Option<&'a [u8]> { match *self { @@ -342,6 +385,10 @@ impl<'a, Hash: Encode> Encode for DigestItemRef<'a, Hash> { DigestItemType::PreRuntime.encode_to(&mut v); (val, data).encode_to(&mut v); }, + DigestItemRef::ChangesTrieSignal(changes_trie_signal) => { + DigestItemType::ChangesTrieSignal.encode_to(&mut v); + changes_trie_signal.encode_to(&mut v); + }, DigestItemRef::Other(val) => { DigestItemType::Other.encode_to(&mut v); val.encode_to(&mut v); @@ -352,6 +399,15 @@ impl<'a, Hash: Encode> Encode for DigestItemRef<'a, Hash> { } } +impl ChangesTrieSignal { + /// Try to cast this signal to NewConfiguration. + pub fn as_new_configuration(&self) -> Option<&Option> { + match self { + ChangesTrieSignal::NewConfiguration(config) => Some(config), + } + } +} + impl<'a, Hash: Encode> codec::EncodeLike for DigestItemRef<'a, Hash> {} #[cfg(test)] diff --git a/primitives/runtime/src/generic/mod.rs b/primitives/runtime/src/generic/mod.rs index 2416bb70470be..5e9928ba1909a 100644 --- a/primitives/runtime/src/generic/mod.rs +++ b/primitives/runtime/src/generic/mod.rs @@ -33,7 +33,7 @@ pub use self::checked_extrinsic::CheckedExtrinsic; pub use self::header::Header; pub use self::block::{Block, SignedBlock, BlockId}; pub use self::digest::{ - Digest, DigestItem, DigestItemRef, OpaqueDigestItemId + Digest, DigestItem, DigestItemRef, OpaqueDigestItemId, ChangesTrieSignal, }; use crate::codec::Encode; diff --git a/primitives/state-machine/src/changes_trie/build.rs b/primitives/state-machine/src/changes_trie/build.rs index ded8e4f6d19ed..f717a1e6e3cc2 100644 --- a/primitives/state-machine/src/changes_trie/build.rs +++ b/primitives/state-machine/src/changes_trie/build.rs @@ -356,7 +356,6 @@ mod test { OverlayedChanges, Configuration, ) { - let config = Configuration { digest_interval: 4, digest_levels: 2 }; let backend: InMemoryBackend<_> = vec![ (vec![100], vec![255]), (vec![101], vec![255]), @@ -465,8 +464,9 @@ mod test { ].into_iter().collect(), CHILD_INFO_1.to_owned())), ].into_iter().collect(), }, - changes_trie_config: Some(config.clone()), + collect_extrinsics: true, }; + let config = Configuration { digest_interval: 4, digest_levels: 2 }; (backend, storage, changes, config) } @@ -553,7 +553,6 @@ mod test { InputPair::ExtrinsicIndex(ExtrinsicIndex { block: zero + 4, key: vec![100] }, vec![0, 2]), ]), ]); - } test_with_zero(0); @@ -597,7 +596,6 @@ mod test { InputPair::ExtrinsicIndex(ExtrinsicIndex { block: zero + 16, key: vec![100] }, vec![0, 2]), ]), ]); - } test_with_zero(0); @@ -706,8 +704,7 @@ mod test { #[test] fn cache_is_used_when_changes_trie_is_built() { - let (backend, mut storage, changes, _) = prepare_for_build(0); - let config = changes.changes_trie_config.as_ref().unwrap(); + let (backend, mut storage, changes, config) = prepare_for_build(0); let parent = AnchorBlockId { hash: Default::default(), number: 15 }; // override some actual values from storage with values from the cache @@ -770,6 +767,5 @@ mod test { InputPair::DigestIndex(DigestIndex { block: 16u64, key: vec![106] }, vec![4]), ], ); - } } diff --git a/primitives/state-machine/src/changes_trie/mod.rs b/primitives/state-machine/src/changes_trie/mod.rs index cb19d157dd80a..3fc98caf79023 100644 --- a/primitives/state-machine/src/changes_trie/mod.rs +++ b/primitives/state-machine/src/changes_trie/mod.rs @@ -63,7 +63,7 @@ pub use self::changes_iterator::{ key_changes, key_changes_proof, key_changes_proof_check, key_changes_proof_check_with_db, }; -pub use self::prune::{prune, oldest_non_pruned_trie}; +pub use self::prune::prune; use std::collections::{HashMap, HashSet}; use std::convert::TryInto; @@ -121,6 +121,18 @@ pub struct AnchorBlockId { pub number: Number, } +/// Changes tries state at some block. +pub struct State<'a, H, Number> { + /// Configuration that is active at given block. + pub config: Configuration, + /// Configuration activation block number. Zero if it is the first coonfiguration on the chain, + /// or number of the block that have emit NewConfiguration signal (thus activating configuration + /// starting from the **next** block). + pub zero: Number, + /// Underlying changes tries storage reference. + pub storage: &'a dyn Storage, +} + /// Changes trie storage. Provides access to trie roots and trie nodes. pub trait RootsStorage: Send + Sync { /// Resolve hash of the block into anchor. @@ -170,14 +182,43 @@ pub struct ConfigurationRange<'a, N> { pub end: Option, } +impl<'a, H, Number> State<'a, H, Number> { + /// Create state with given config and storage. + pub fn new( + config: Configuration, + zero: Number, + storage: &'a dyn Storage, + ) -> Self { + Self { + config, + zero, + storage, + } + } +} + +impl<'a, H, Number: Clone> Clone for State<'a, H, Number> { + fn clone(&self) -> Self { + State { + config: self.config.clone(), + zero: self.zero.clone(), + storage: self.storage, + } + } +} + +/// Create state where changes tries are disabled. +pub fn disabled_state<'a, H, Number>() -> Option> { + None +} + /// Compute the changes trie root and transaction for given block. /// Returns Err(()) if unknown `parent_hash` has been passed. /// Returns Ok(None) if there's no data to perform computation. -/// Panics if background storage returns an error (and `panic_on_storage_error` is `true`) OR -/// if insert to MemoryDB fails. -pub fn build_changes_trie<'a, B: Backend, S: Storage, H: Hasher, Number: BlockNumber>( +/// Panics if background storage returns an error OR if insert to MemoryDB fails. +pub fn build_changes_trie<'a, B: Backend, H: Hasher, Number: BlockNumber>( backend: &B, - storage: Option<&'a S>, + state: Option<&'a State<'a, H, Number>>, changes: &OverlayedChanges, parent_hash: H::Out, panic_on_storage_error: bool, @@ -185,11 +226,6 @@ pub fn build_changes_trie<'a, B: Backend, S: Storage, H: Hasher, N where H::Out: Ord + 'static + Encode, { - let (storage, config) = match (storage, changes.changes_trie_config.as_ref()) { - (Some(storage), Some(config)) => (storage, config), - _ => return Ok(None), - }; - /// Panics when `res.is_err() && panic`, otherwise it returns `Err(())` on an error. fn maybe_panic( res: std::result::Result, @@ -203,23 +239,35 @@ pub fn build_changes_trie<'a, B: Backend, S: Storage, H: Hasher, N }) } - // FIXME: remove this in https://github.com/paritytech/substrate/pull/3201 - let config = ConfigurationRange { - config, - zero: Zero::zero(), - end: None, + // when storage isn't provided, changes tries aren't created + let state = match state { + Some(state) => state, + None => return Ok(None), }; // build_anchor error should not be considered fatal - let parent = storage.build_anchor(parent_hash).map_err(|_| ())?; + let parent = state.storage.build_anchor(parent_hash).map_err(|_| ())?; let block = parent.number.clone() + One::one(); + // prepare configuration range - we already know zero block. Current block may be the end block if configuration + // has been changed in this block + let is_config_changed = match changes.storage(sp_core::storage::well_known_keys::CHANGES_TRIE_CONFIG) { + Some(Some(new_config)) => new_config != &state.config.encode()[..], + Some(None) => true, + None => false, + }; + let config_range = ConfigurationRange { + config: &state.config, + zero: state.zero.clone(), + end: if is_config_changed { Some(block.clone()) } else { None }, + }; + // storage errors are considered fatal (similar to situations when runtime fetches values from storage) let (input_pairs, child_input_pairs, digest_input_blocks) = maybe_panic( prepare_input::( backend, - storage, - config.clone(), + state.storage, + config_range.clone(), changes, &parent, ), @@ -227,7 +275,7 @@ pub fn build_changes_trie<'a, B: Backend, S: Storage, H: Hasher, N )?; // prepare cached data - let mut cache_action = prepare_cached_build_data(config, block.clone()); + let mut cache_action = prepare_cached_build_data(config_range, block.clone()); let needs_changed_keys = cache_action.collects_changed_keys(); cache_action = cache_action.set_digest_input_blocks(digest_input_blocks); diff --git a/primitives/state-machine/src/changes_trie/prune.rs b/primitives/state-machine/src/changes_trie/prune.rs index e16daea15821e..f6be3223ae9f8 100644 --- a/primitives/state-machine/src/changes_trie/prune.rs +++ b/primitives/state-machine/src/changes_trie/prune.rs @@ -19,52 +19,26 @@ use hash_db::Hasher; use sp_trie::Recorder; use log::warn; -use num_traits::{One, Zero}; +use num_traits::One; use crate::proving_backend::ProvingBackendRecorder; use crate::trie_backend_essence::TrieBackendEssence; -use crate::changes_trie::{AnchorBlockId, Configuration, Storage, BlockNumber}; +use crate::changes_trie::{AnchorBlockId, Storage, BlockNumber}; use crate::changes_trie::storage::TrieBackendAdapter; use crate::changes_trie::input::{ChildIndex, InputKey}; use codec::{Decode, Codec}; -/// Get number of oldest block for which changes trie is not pruned -/// given changes trie configuration, pruning parameter and number of -/// best finalized block. -pub fn oldest_non_pruned_trie( - config: &Configuration, - min_blocks_to_keep: Number, - best_finalized_block: Number, -) -> Number { - let max_digest_interval = config.max_digest_interval(); - let best_finalized_block_rem = best_finalized_block.clone() % max_digest_interval.into(); - let max_digest_block = best_finalized_block - best_finalized_block_rem; - match pruning_range(config, min_blocks_to_keep, max_digest_block) { - Some((_, last_pruned_block)) => last_pruned_block + One::one(), - None => One::one(), - } -} - /// Prune obsolete changes tries. Pruning happens at the same block, where highest /// level digest is created. Pruning guarantees to save changes tries for last /// `min_blocks_to_keep` blocks. We only prune changes tries at `max_digest_interval` /// ranges. -/// Returns MemoryDB that contains all deleted changes tries nodes. -pub fn prune, H: Hasher, Number: BlockNumber, F: FnMut(H::Out)>( - config: &Configuration, - storage: &S, - min_blocks_to_keep: Number, +pub fn prune( + storage: &dyn Storage, + first: Number, + last: Number, current_block: &AnchorBlockId, mut remove_trie_node: F, ) where H::Out: Codec { - - // select range for pruning - let (first, last) = match pruning_range(config, min_blocks_to_keep, current_block.number.clone()) { - Some((first, last)) => (first, last), - None => return, - }; - // delete changes trie for every block in range - // FIXME: limit `max_digest_interval` so that this cycle won't involve huge ranges let mut block = first; loop { if block >= last.clone() + One::one() { @@ -112,8 +86,8 @@ pub fn prune, H: Hasher, Number: BlockNumber, F: FnMut(H:: } // Prune a trie. -fn prune_trie, H: Hasher, Number: BlockNumber, F: FnMut(H::Out)>( - storage: &S, +fn prune_trie( + storage: &dyn Storage, root: H::Out, remove_trie_node: &mut F, ) where H::Out: Codec { @@ -136,100 +110,26 @@ fn prune_trie, H: Hasher, Number: BlockNumber, F: FnMut(H: } } -/// Select blocks range (inclusive from both ends) for pruning changes tries in. -fn pruning_range( - config: &Configuration, - min_blocks_to_keep: Number, - block: Number, -) -> Option<(Number, Number)> { - // compute number of changes tries we actually want to keep - let (prune_interval, blocks_to_keep) = if config.is_digest_build_enabled() { - // we only CAN prune at block where max-level-digest is created - let max_digest_interval = match config.digest_level_at_block(Zero::zero(), block.clone()) { - Some((digest_level, digest_interval, _)) if digest_level == config.digest_levels => - digest_interval, - _ => return None, - }; - - // compute maximal number of high-level digests to keep - let max_digest_intervals_to_keep = max_digest_intervals_to_keep(min_blocks_to_keep, max_digest_interval); - - // number of blocks BEFORE current block where changes tries are not pruned - ( - max_digest_interval, - max_digest_intervals_to_keep.checked_mul(&max_digest_interval.into()) - ) - } else { - ( - 1, - Some(min_blocks_to_keep) - ) - }; - - // last block for which changes trie is pruned - let last_block_to_prune = blocks_to_keep.and_then(|b| block.checked_sub(&b)); - let first_block_to_prune = last_block_to_prune - .clone() - .and_then(|b| b.checked_sub(&prune_interval.into())); - - last_block_to_prune - .and_then(|last| first_block_to_prune.map(|first| (first + One::one(), last))) -} - -/// Select pruning delay for the changes tries. To make sure we could build a changes -/// trie at block B, we need an access to previous: -/// max_digest_interval = config.digest_interval ^ config.digest_levels -/// blocks. So we can only prune blocks that are earlier than B - max_digest_interval. -/// The pruning_delay stands for number of max_digest_interval-s that we want to keep: -/// 0 or 1: means that only last changes trie is guaranteed to exists; -/// 2: the last changes trie + previous changes trie -/// ... -fn max_digest_intervals_to_keep( - min_blocks_to_keep: Number, - max_digest_interval: u32, -) -> Number { - // config.digest_level_at_block ensures that it is not zero - debug_assert!(max_digest_interval != 0); - - let max_digest_intervals_to_keep = min_blocks_to_keep / max_digest_interval.into(); - if max_digest_intervals_to_keep.is_zero() { - One::one() - } else { - max_digest_intervals_to_keep - } -} - #[cfg(test)] mod tests { use std::collections::HashSet; use sp_trie::MemoryDB; - use sp_core::Blake2Hasher; + use sp_core::{H256, Blake2Hasher}; use crate::backend::insert_into_memory_db; use crate::changes_trie::storage::InMemoryStorage; use codec::Encode; use super::*; - fn config(interval: u32, levels: u32) -> Configuration { - Configuration { - digest_interval: interval, - digest_levels: levels, - } - } - - fn prune_by_collect, H: Hasher>( - config: &Configuration, - storage: &S, - min_blocks_to_keep: u64, + fn prune_by_collect( + storage: &dyn Storage, + first: u64, + last: u64, current_block: u64, - ) -> HashSet where H::Out: Codec { + ) -> HashSet { let mut pruned_trie_nodes = HashSet::new(); - prune( - config, - storage, - min_blocks_to_keep, - &AnchorBlockId { hash: Default::default(), number: current_block }, - |node| { pruned_trie_nodes.insert(node); }, - ); + let anchor = AnchorBlockId { hash: Default::default(), number: current_block }; + prune(storage, first, last, &anchor, + |node| { pruned_trie_nodes.insert(node); }); pruned_trie_nodes } @@ -243,7 +143,9 @@ mod tests { &mut mdb1, vec![(vec![10], vec![20])]).unwrap(); let mut mdb2 = MemoryDB::::default(); let root2 = insert_into_memory_db::( - &mut mdb2, vec![(vec![11], vec![21]), (vec![12], vec![22])]).unwrap(); + &mut mdb2, + vec![(vec![11], vec![21]), (vec![12], vec![22])], + ).unwrap(); let mut mdb3 = MemoryDB::::default(); let ch_root3 = insert_into_memory_db::( &mut mdb3, vec![(vec![110], vec![120])]).unwrap(); @@ -254,7 +156,9 @@ mod tests { ]).unwrap(); let mut mdb4 = MemoryDB::::default(); let root4 = insert_into_memory_db::( - &mut mdb4, vec![(vec![15], vec![25])]).unwrap(); + &mut mdb4, + vec![(vec![15], vec![25])], + ).unwrap(); let storage = InMemoryStorage::new(); storage.insert(65, root1, mdb1); storage.insert(66, root2, mdb2); @@ -264,109 +168,20 @@ mod tests { storage } - // l1-digest is created every 2 blocks - // l2-digest is created every 4 blocks - // we do not want to keep any additional changes tries - // => only one l2-digest is saved AND it is pruned once next is created - let config = Configuration { digest_interval: 2, digest_levels: 2 }; let storage = prepare_storage(); - assert!(prune_by_collect(&config, &storage, 0, 69).is_empty()); - assert!(prune_by_collect(&config, &storage, 0, 70).is_empty()); - assert!(prune_by_collect(&config, &storage, 0, 71).is_empty()); - let non_empty = prune_by_collect(&config, &storage, 0, 72); - assert!(!non_empty.is_empty()); - storage.remove_from_storage(&non_empty); - assert!(storage.into_mdb().drain().is_empty()); + assert!(prune_by_collect(&storage, 20, 30, 90).is_empty()); + assert!(!storage.into_mdb().drain().is_empty()); - // l1-digest is created every 2 blocks - // l2-digest is created every 4 blocks - // we want keep 1 additional changes tries - let config = Configuration { digest_interval: 2, digest_levels: 2 }; let storage = prepare_storage(); - assert!(prune_by_collect(&config, &storage, 8, 69).is_empty()); - assert!(prune_by_collect(&config, &storage, 8, 70).is_empty()); - assert!(prune_by_collect(&config, &storage, 8, 71).is_empty()); - assert!(prune_by_collect(&config, &storage, 8, 72).is_empty()); - assert!(prune_by_collect(&config, &storage, 8, 73).is_empty()); - assert!(prune_by_collect(&config, &storage, 8, 74).is_empty()); - assert!(prune_by_collect(&config, &storage, 8, 75).is_empty()); - let non_empty = prune_by_collect(&config, &storage, 8, 76); - assert!(!non_empty.is_empty()); - storage.remove_from_storage(&non_empty); - assert!(storage.into_mdb().drain().is_empty()); + let prune60_65 = prune_by_collect(&storage, 60, 65, 90); + assert!(!prune60_65.is_empty()); + storage.remove_from_storage(&prune60_65); + assert!(!storage.into_mdb().drain().is_empty()); - // l1-digest is created every 2 blocks - // we want keep 2 additional changes tries - let config = Configuration { digest_interval: 2, digest_levels: 1 }; let storage = prepare_storage(); - assert!(prune_by_collect(&config, &storage, 4, 69).is_empty()); - let non_empty = prune_by_collect(&config, &storage, 4, 70); - assert!(!non_empty.is_empty()); - storage.remove_from_storage(&non_empty); - assert!(prune_by_collect(&config, &storage, 4, 71).is_empty()); - let non_empty = prune_by_collect(&config, &storage, 4, 72); - assert!(!non_empty.is_empty()); - storage.remove_from_storage(&non_empty); + let prune60_70 = prune_by_collect(&storage, 60, 70, 90); + assert!(!prune60_70.is_empty()); + storage.remove_from_storage(&prune60_70); assert!(storage.into_mdb().drain().is_empty()); } - - #[test] - fn pruning_range_works() { - // DIGESTS ARE NOT CREATED + NO TRIES ARE PRUNED - assert_eq!(pruning_range(&config(10, 0), 2u64, 2u64), None); - - // DIGESTS ARE NOT CREATED + SOME TRIES ARE PRUNED - assert_eq!(pruning_range(&config(10, 0), 100u64, 110u64), Some((10, 10))); - assert_eq!(pruning_range(&config(10, 0), 100u64, 210u64), Some((110, 110))); - - // DIGESTS ARE CREATED + NO TRIES ARE PRUNED - - assert_eq!(pruning_range(&config(10, 2), 2u64, 0u64), None); - assert_eq!(pruning_range(&config(10, 2), 30u64, 100u64), None); - assert_eq!(pruning_range(&config(::std::u32::MAX, 2), 1u64, 1024u64), None); - assert_eq!(pruning_range(&config(::std::u32::MAX, 2), ::std::u64::MAX, 1024u64), None); - assert_eq!(pruning_range(&config(32, 2), 2048u64, 512u64), None); - assert_eq!(pruning_range(&config(32, 2), 2048u64, 1024u64), None); - - // DIGESTS ARE CREATED + SOME TRIES ARE PRUNED - - // when we do not want to keep any highest-level-digests - // (system forces to keep at least one) - assert_eq!(pruning_range(&config(4, 2), 0u64, 32u64), Some((1, 16))); - assert_eq!(pruning_range(&config(4, 2), 0u64, 64u64), Some((33, 48))); - // when we want to keep 1 (last) highest-level-digest - assert_eq!(pruning_range(&config(4, 2), 16u64, 32u64), Some((1, 16))); - assert_eq!(pruning_range(&config(4, 2), 16u64, 64u64), Some((33, 48))); - // when we want to keep 1 (last) + 1 additional level digests - assert_eq!(pruning_range(&config(32, 2), 4096u64, 5120u64), Some((1, 1024))); - assert_eq!(pruning_range(&config(32, 2), 4096u64, 6144u64), Some((1025, 2048))); - } - - #[test] - fn max_digest_intervals_to_keep_works() { - assert_eq!(max_digest_intervals_to_keep(1024u64, 1025), 1u64); - assert_eq!(max_digest_intervals_to_keep(1024u64, 1023), 1u64); - assert_eq!(max_digest_intervals_to_keep(1024u64, 512), 2u64); - assert_eq!(max_digest_intervals_to_keep(1024u64, 511), 2u64); - assert_eq!(max_digest_intervals_to_keep(1024u64, 100), 10u64); - } - - #[test] - fn oldest_non_pruned_trie_works() { - // when digests are not created at all - assert_eq!(oldest_non_pruned_trie(&config(0, 0), 100u64, 10u64), 1); - assert_eq!(oldest_non_pruned_trie(&config(0, 0), 100u64, 110u64), 11); - - // when only l1 digests are created - assert_eq!(oldest_non_pruned_trie(&config(100, 1), 100u64, 50u64), 1); - assert_eq!(oldest_non_pruned_trie(&config(100, 1), 100u64, 110u64), 1); - assert_eq!(oldest_non_pruned_trie(&config(100, 1), 100u64, 210u64), 101); - - // when l2 digests are created - assert_eq!(oldest_non_pruned_trie(&config(100, 2), 100u64, 50u64), 1); - assert_eq!(oldest_non_pruned_trie(&config(100, 2), 100u64, 110u64), 1); - assert_eq!(oldest_non_pruned_trie(&config(100, 2), 100u64, 210u64), 1); - assert_eq!(oldest_non_pruned_trie(&config(100, 2), 100u64, 10110u64), 1); - assert_eq!(oldest_non_pruned_trie(&config(100, 2), 100u64, 20110u64), 10001); - } } diff --git a/primitives/state-machine/src/ext.rs b/primitives/state-machine/src/ext.rs index 6fc4312d1079d..3ee7292102f8e 100644 --- a/primitives/state-machine/src/ext.rs +++ b/primitives/state-machine/src/ext.rs @@ -18,7 +18,7 @@ use crate::{ backend::Backend, OverlayedChanges, StorageTransactionCache, - changes_trie::Storage as ChangesTrieStorage, + changes_trie::State as ChangesTrieState, }; use hash_db::Hasher; @@ -65,7 +65,7 @@ impl error::Error for Error { } /// Wraps a read-only backend, call executor, and current overlayed changes. -pub struct Ext<'a, H, N, B, T> +pub struct Ext<'a, H, N, B> where H: Hasher, B: 'a + Backend, @@ -77,8 +77,8 @@ pub struct Ext<'a, H, N, B, T> backend: &'a B, /// The cache for the storage transactions. storage_transaction_cache: &'a mut StorageTransactionCache, - /// Changes trie storage to read from. - changes_trie_storage: Option<&'a T>, + /// Changes trie state to read from. + changes_trie_state: Option>, /// Pseudo-unique id used for tracing. pub id: u16, /// Dummy usage of N arg. @@ -87,12 +87,11 @@ pub struct Ext<'a, H, N, B, T> extensions: Option<&'a mut Extensions>, } -impl<'a, H, N, B, T> Ext<'a, H, N, B, T> +impl<'a, H, N, B> Ext<'a, H, N, B> where H: Hasher, H::Out: Ord + 'static + codec::Codec, B: 'a + Backend, - T: 'a + ChangesTrieStorage, N: crate::changes_trie::BlockNumber, { @@ -101,13 +100,13 @@ where overlay: &'a mut OverlayedChanges, storage_transaction_cache: &'a mut StorageTransactionCache, backend: &'a B, - changes_trie_storage: Option<&'a T>, + changes_trie_state: Option>, extensions: Option<&'a mut Extensions>, ) -> Self { Ext { overlay, backend, - changes_trie_storage, + changes_trie_state, storage_transaction_cache, id: rand::random(), _phantom: Default::default(), @@ -124,12 +123,11 @@ where } #[cfg(test)] -impl<'a, H, N, B, T> Ext<'a, H, N, B, T> +impl<'a, H, N, B> Ext<'a, H, N, B> where H: Hasher, H::Out: Ord + 'static, B: 'a + Backend, - T: 'a + ChangesTrieStorage, N: crate::changes_trie::BlockNumber, { pub fn storage_pairs(&self) -> Vec<(Vec, Vec)> { @@ -146,12 +144,11 @@ where } } -impl<'a, H, B, T, N> Externalities for Ext<'a, H, N, B, T> +impl<'a, H, B, N> Externalities for Ext<'a, H, N, B> where H: Hasher, H::Out: Ord + 'static + codec::Codec, B: 'a + Backend, - T: 'a + ChangesTrieStorage, N: crate::changes_trie::BlockNumber, { fn storage(&self, key: &[u8]) -> Option> { @@ -564,7 +561,7 @@ where let _guard = sp_panic_handler::AbortGuard::force_abort(); let root = self.overlay.changes_trie_root( self.backend, - self.changes_trie_storage.clone(), + self.changes_trie_state.as_ref(), Decode::decode(&mut &parent_hash[..]).map_err(|e| trace!( target: "state-trace", @@ -586,11 +583,10 @@ where } } -impl<'a, H, B, T, N> sp_externalities::ExtensionStore for Ext<'a, H, N, B, T> +impl<'a, H, B, N> sp_externalities::ExtensionStore for Ext<'a, H, N, B> where H: Hasher, B: 'a + Backend, - T: 'a + ChangesTrieStorage, N: crate::changes_trie::BlockNumber, { fn extension_by_type_id(&mut self, type_id: TypeId) -> Option<&mut dyn Any> { @@ -602,19 +598,19 @@ where mod tests { use super::*; use hex_literal::hex; + use num_traits::Zero; use codec::Encode; use sp_core::{H256, Blake2Hasher, storage::well_known_keys::EXTRINSIC_INDEX, map}; use crate::{ changes_trie::{ Configuration as ChangesTrieConfiguration, - InMemoryStorage as InMemoryChangesTrieStorage, + InMemoryStorage as TestChangesTrieStorage, }, InMemoryBackend, overlayed_changes::OverlayedValue, }; use sp_core::storage::{Storage, StorageChild}; type TestBackend = InMemoryBackend; - type TestChangesTrieStorage = InMemoryChangesTrieStorage; - type TestExt<'a> = Ext<'a, Blake2Hasher, u64, TestBackend, TestChangesTrieStorage>; + type TestExt<'a> = Ext<'a, Blake2Hasher, u64, TestBackend>; fn prepare_overlay_with_changes() -> OverlayedChanges { OverlayedChanges { @@ -629,10 +625,14 @@ mod tests { }), ].into_iter().collect(), committed: Default::default(), - changes_trie_config: Some(ChangesTrieConfiguration { - digest_interval: 0, - digest_levels: 0, - }), + collect_extrinsics: true, + } + } + + fn changes_trie_config() -> ChangesTrieConfiguration { + ChangesTrieConfiguration { + digest_interval: 0, + digest_levels: 0, } } @@ -646,13 +646,11 @@ mod tests { } #[test] - fn storage_changes_root_is_none_when_extrinsic_changes_are_none() { + fn storage_changes_root_is_none_when_state_is_not_provided() { let mut overlay = prepare_overlay_with_changes(); let mut cache = StorageTransactionCache::default(); - overlay.changes_trie_config = None; - let storage = TestChangesTrieStorage::with_blocks(vec![(100, Default::default())]); let backend = TestBackend::default(); - let mut ext = TestExt::new(&mut overlay, &mut cache, &backend, Some(&storage), None); + let mut ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None); assert_eq!(ext.storage_changes_root(&H256::default().encode()).unwrap(), None); } @@ -661,8 +659,9 @@ mod tests { let mut overlay = prepare_overlay_with_changes(); let mut cache = StorageTransactionCache::default(); let storage = TestChangesTrieStorage::with_blocks(vec![(99, Default::default())]); + let state = Some(ChangesTrieState::new(changes_trie_config(), Zero::zero(), &storage)); let backend = TestBackend::default(); - let mut ext = TestExt::new(&mut overlay, &mut cache, &backend, Some(&storage), None); + let mut ext = TestExt::new(&mut overlay, &mut cache, &backend, state, None); assert_eq!( ext.storage_changes_root(&H256::default().encode()).unwrap(), Some(hex!("bb0c2ef6e1d36d5490f9766cfcc7dfe2a6ca804504c3bb206053890d6dd02376").to_vec()), @@ -675,8 +674,9 @@ mod tests { let mut cache = StorageTransactionCache::default(); overlay.prospective.top.get_mut(&vec![1]).unwrap().value = None; let storage = TestChangesTrieStorage::with_blocks(vec![(99, Default::default())]); + let state = Some(ChangesTrieState::new(changes_trie_config(), Zero::zero(), &storage)); let backend = TestBackend::default(); - let mut ext = TestExt::new(&mut overlay, &mut cache, &backend, Some(&storage), None); + let mut ext = TestExt::new(&mut overlay, &mut cache, &backend, state, None); assert_eq!( ext.storage_changes_root(&H256::default().encode()).unwrap(), Some(hex!("96f5aae4690e7302737b6f9b7f8567d5bbb9eac1c315f80101235a92d9ec27f4").to_vec()), diff --git a/primitives/state-machine/src/lib.rs b/primitives/state-machine/src/lib.rs index 55f2eb4941f90..b27bf47050f64 100644 --- a/primitives/state-machine/src/lib.rs +++ b/primitives/state-machine/src/lib.rs @@ -23,8 +23,8 @@ use log::{warn, trace}; use hash_db::Hasher; use codec::{Decode, Encode, Codec}; use sp_core::{ - storage::{well_known_keys, ChildInfo}, NativeOrEncoded, NeverNativeValue, - traits::{CodeExecutor, CallInWasmExt}, hexdisplay::HexDisplay + storage::ChildInfo, NativeOrEncoded, NeverNativeValue, + traits::{CodeExecutor, CallInWasmExt}, hexdisplay::HexDisplay, }; use overlayed_changes::OverlayedChangeSet; use sp_externalities::Extensions; @@ -49,15 +49,17 @@ pub use ext::Ext; pub use backend::Backend; pub use changes_trie::{ AnchorBlockId as ChangesTrieAnchorBlockId, + State as ChangesTrieState, Storage as ChangesTrieStorage, RootsStorage as ChangesTrieRootsStorage, InMemoryStorage as InMemoryChangesTrieStorage, BuildCache as ChangesTrieBuildCache, CacheAction as ChangesTrieCacheAction, ConfigurationRange as ChangesTrieConfigurationRange, - key_changes, key_changes_proof, key_changes_proof_check, + key_changes, key_changes_proof, + key_changes_proof_check, key_changes_proof_check_with_db, prune as prune_changes_tries, - oldest_non_pruned_trie as oldest_non_pruned_changes_trie, + disabled_state as disabled_changes_trie_state, BlockNumber as ChangesTrieBlockNumber, }; pub use overlayed_changes::{OverlayedChanges, StorageChanges, StorageTransactionCache}; @@ -171,7 +173,7 @@ fn always_untrusted_wasm() -> ExecutionManager +pub struct StateMachine<'a, B, H, N, Exec> where H: Hasher, B: Backend, @@ -183,23 +185,22 @@ pub struct StateMachine<'a, B, H, N, T, Exec> call_data: &'a [u8], overlay: &'a mut OverlayedChanges, extensions: Extensions, - changes_trie_storage: Option<&'a T>, + changes_trie_state: Option>, _marker: PhantomData<(H, N)>, storage_transaction_cache: Option<&'a mut StorageTransactionCache>, } -impl<'a, B, H, N, T, Exec> StateMachine<'a, B, H, N, T, Exec> where +impl<'a, B, H, N, Exec> StateMachine<'a, B, H, N, Exec> where H: Hasher, H::Out: Ord + 'static + codec::Codec, Exec: CodeExecutor + Clone + 'static, B: Backend, - T: ChangesTrieStorage, N: crate::changes_trie::BlockNumber, { /// Creates new substrate state machine. pub fn new( backend: &'a B, - changes_trie_storage: Option<&'a T>, + changes_trie_state: Option>, overlay: &'a mut OverlayedChanges, exec: &'a Exec, method: &'a str, @@ -215,7 +216,7 @@ impl<'a, B, H, N, T, Exec> StateMachine<'a, B, H, N, T, Exec> where call_data, extensions, overlay, - changes_trie_storage, + changes_trie_state, _marker: PhantomData, storage_transaction_cache: None, } @@ -273,7 +274,7 @@ impl<'a, B, H, N, T, Exec> StateMachine<'a, B, H, N, T, Exec> where self.overlay, cache, self.backend, - self.changes_trie_storage.clone(), + self.changes_trie_state.clone(), Some(&mut self.extensions), ); @@ -388,20 +389,8 @@ impl<'a, B, H, N, T, Exec> StateMachine<'a, B, H, N, T, Exec> where CallResult, ) -> CallResult { - // read changes trie configuration. The reason why we're doing it here instead of the - // `OverlayedChanges` constructor is that we need proofs for this read as a part of - // proof-of-execution on light clients. And the proof is recorded by the backend which - // is created after OverlayedChanges - - let init_overlay = |overlay: &mut OverlayedChanges, final_check: bool, backend: &B| { - let changes_trie_config = try_read_overlay_value( - overlay, - backend, - well_known_keys::CHANGES_TRIE_CONFIG - )?; - set_changes_trie_config(overlay, changes_trie_config, final_check) - }; - init_overlay(self.overlay, false, &self.backend)?; + let changes_tries_enabled = self.changes_trie_state.is_some(); + self.overlay.set_collect_extrinsics(changes_tries_enabled); let result = { let orig_prospective = self.overlay.prospective.clone(); @@ -433,16 +422,12 @@ impl<'a, B, H, N, T, Exec> StateMachine<'a, B, H, N, T, Exec> where } }; - if result.is_ok() { - init_overlay(self.overlay, true, self.backend)?; - } - result.map_err(|e| Box::new(e) as _) } } /// Prove execution using the given state backend, overlayed changes, and call executor. -pub fn prove_execution( +pub fn prove_execution( mut backend: B, overlay: &mut OverlayedChanges, exec: &Exec, @@ -454,10 +439,11 @@ where H: Hasher, H::Out: Ord + 'static + codec::Codec, Exec: CodeExecutor + Clone + 'static, + N: crate::changes_trie::BlockNumber, { let trie_backend = backend.as_trie_backend() .ok_or_else(|| Box::new(ExecutionError::UnableToGenerateProof) as Box)?; - prove_execution_on_trie_backend(trie_backend, overlay, exec, method, call_data) + prove_execution_on_trie_backend::<_, _, N, _>(trie_backend, overlay, exec, method, call_data) } /// Prove execution using the given trie backend, overlayed changes, and call executor. @@ -469,7 +455,7 @@ where /// /// Note: changes to code will be in place if this call is made again. For running partial /// blocks (e.g. a transaction at a time), ensure a different method is used. -pub fn prove_execution_on_trie_backend( +pub fn prove_execution_on_trie_backend( trie_backend: &TrieBackend, overlay: &mut OverlayedChanges, exec: &Exec, @@ -481,9 +467,10 @@ where H: Hasher, H::Out: Ord + 'static + codec::Codec, Exec: CodeExecutor + 'static + Clone, + N: crate::changes_trie::BlockNumber, { let proving_backend = proving_backend::ProvingBackend::new(trie_backend); - let mut sm = StateMachine::<_, H, _, InMemoryChangesTrieStorage, Exec>::new( + let mut sm = StateMachine::<_, H, N, Exec>::new( &proving_backend, None, overlay, exec, method, call_data, Extensions::default(), ); @@ -496,7 +483,7 @@ where } /// Check execution proof, generated by `prove_execution` call. -pub fn execution_proof_check( +pub fn execution_proof_check( root: H::Out, proof: StorageProof, overlay: &mut OverlayedChanges, @@ -508,13 +495,14 @@ where H: Hasher, Exec: CodeExecutor + Clone + 'static, H::Out: Ord + 'static + codec::Codec, + N: crate::changes_trie::BlockNumber, { let trie_backend = create_proof_check_backend::(root.into(), proof)?; - execution_proof_check_on_trie_backend(&trie_backend, overlay, exec, method, call_data) + execution_proof_check_on_trie_backend::<_, N, _>(&trie_backend, overlay, exec, method, call_data) } /// Check execution proof on proving backend, generated by `prove_execution` call. -pub fn execution_proof_check_on_trie_backend( +pub fn execution_proof_check_on_trie_backend( trie_backend: &TrieBackend, H>, overlay: &mut OverlayedChanges, exec: &Exec, @@ -525,8 +513,9 @@ where H: Hasher, H::Out: Ord + 'static + codec::Codec, Exec: CodeExecutor + Clone + 'static, + N: crate::changes_trie::BlockNumber, { - let mut sm = StateMachine::<_, H, _, InMemoryChangesTrieStorage, Exec>::new( + let mut sm = StateMachine::<_, H, N, Exec>::new( trie_backend, None, overlay, exec, method, call_data, Extensions::default(), ); @@ -692,44 +681,6 @@ where .map_err(|e| Box::new(e) as Box) } -/// Sets overlayed changes' changes trie configuration. Returns error if configuration -/// differs from previous OR config decode has failed. -fn set_changes_trie_config( - overlay: &mut OverlayedChanges, - config: Option>, - final_check: bool, -) -> Result<(), Box> { - let config = match config { - Some(v) => Some(Decode::decode(&mut &v[..]) - .map_err(|_| Box::new("Failed to decode changes trie configuration".to_owned()) as Box)?), - None => None, - }; - - if final_check && overlay.changes_trie_config.is_some() != config.is_some() { - return Err(Box::new("Changes trie configuration change is not supported".to_owned())); - } - - if let Some(config) = config { - if !overlay.set_changes_trie_config(config) { - return Err(Box::new("Changes trie configuration change is not supported".to_owned())); - } - } - Ok(()) -} - -/// Reads storage value from overlay or from the backend. -fn try_read_overlay_value( - overlay: &OverlayedChanges, - backend: &B, key: &[u8], -) -> Result>, Box> where H: Hasher, B: Backend { - match overlay.storage(key).map(|x| x.map(|x| x.to_vec())) { - Some(value) => Ok(value), - None => backend - .storage(key) - .map_err(|err| Box::new(ExecutionError::Backend(format!("{}", err))) as Box), - } -} - #[cfg(test)] mod tests { use std::collections::BTreeMap; @@ -737,10 +688,7 @@ mod tests { use overlayed_changes::OverlayedValue; use super::*; use super::ext::Ext; - use super::changes_trie::{ - InMemoryStorage as InMemoryChangesTrieStorage, - Configuration as ChangesTrieConfig, - }; + use super::changes_trie::Configuration as ChangesTrieConfig; use sp_core::{Blake2Hasher, map, traits::Externalities, storage::ChildStorageKey}; #[derive(Clone)] @@ -770,7 +718,7 @@ mod tests { ) -> (CallResult, bool) { if self.change_changes_trie_config { ext.place_storage( - well_known_keys::CHANGES_TRIE_CONFIG.to_vec(), + sp_core::storage::well_known_keys::CHANGES_TRIE_CONFIG.to_vec(), Some( ChangesTrieConfig { digest_interval: 777, @@ -816,11 +764,10 @@ mod tests { fn execute_works() { let backend = trie_backend::tests::test_trie(); let mut overlayed_changes = Default::default(); - let changes_trie_storage = InMemoryChangesTrieStorage::::new(); let mut state_machine = StateMachine::new( &backend, - Some(&changes_trie_storage), + changes_trie::disabled_state::<_, u64>(), &mut overlayed_changes, &DummyCodeExecutor { change_changes_trie_config: false, @@ -844,11 +791,10 @@ mod tests { fn execute_works_with_native_else_wasm() { let backend = trie_backend::tests::test_trie(); let mut overlayed_changes = Default::default(); - let changes_trie_storage = InMemoryChangesTrieStorage::::new(); let mut state_machine = StateMachine::new( &backend, - Some(&changes_trie_storage), + changes_trie::disabled_state::<_, u64>(), &mut overlayed_changes, &DummyCodeExecutor { change_changes_trie_config: false, @@ -869,11 +815,10 @@ mod tests { let mut consensus_failed = false; let backend = trie_backend::tests::test_trie(); let mut overlayed_changes = Default::default(); - let changes_trie_storage = InMemoryChangesTrieStorage::::new(); let mut state_machine = StateMachine::new( &backend, - Some(&changes_trie_storage), + changes_trie::disabled_state::<_, u64>(), &mut overlayed_changes, &DummyCodeExecutor { change_changes_trie_config: false, @@ -910,7 +855,7 @@ mod tests { // fetch execution proof from 'remote' full node let remote_backend = trie_backend::tests::test_trie(); let remote_root = remote_backend.storage_root(std::iter::empty()).0; - let (remote_result, remote_proof) = prove_execution( + let (remote_result, remote_proof) = prove_execution::<_, _, u64, _>( remote_backend, &mut Default::default(), &executor, @@ -919,7 +864,7 @@ mod tests { ).unwrap(); // check proof locally - let local_result = execution_proof_check::( + let local_result = execution_proof_check::( remote_root, remote_proof, &mut Default::default(), @@ -956,13 +901,12 @@ mod tests { }; { - let changes_trie_storage = InMemoryChangesTrieStorage::::new(); let mut cache = StorageTransactionCache::default(); let mut ext = Ext::new( &mut overlay, &mut cache, backend, - Some(&changes_trie_storage), + changes_trie::disabled_state::<_, u64>(), None, ); ext.clear_prefix(b"ab"); @@ -987,14 +931,13 @@ mod tests { fn set_child_storage_works() { let mut state = InMemoryBackend::::default(); let backend = state.as_trie_backend().unwrap(); - let changes_trie_storage = InMemoryChangesTrieStorage::::new(); let mut overlay = OverlayedChanges::default(); let mut cache = StorageTransactionCache::default(); let mut ext = Ext::new( &mut overlay, &mut cache, backend, - Some(&changes_trie_storage), + changes_trie::disabled_state::<_, u64>(), None, ); @@ -1080,30 +1023,6 @@ mod tests { ); } - #[test] - fn cannot_change_changes_trie_config() { - let backend = trie_backend::tests::test_trie(); - let mut overlayed_changes = Default::default(); - let changes_trie_storage = InMemoryChangesTrieStorage::::new(); - - let mut state_machine = StateMachine::new( - &backend, - Some(&changes_trie_storage), - &mut overlayed_changes, - &DummyCodeExecutor { - change_changes_trie_config: true, - native_available: false, - native_succeeds: true, - fallback_succeeds: true, - }, - "test", - &[], - Default::default(), - ); - - assert!(state_machine.execute(ExecutionStrategy::NativeWhenPossible).is_err()); - } - #[test] fn child_storage_uuid() { const CHILD_INFO_1: ChildInfo<'static> = ChildInfo::new_default(b"unique_id_1"); @@ -1115,13 +1034,12 @@ mod tests { let subtrie2 = ChildStorageKey::from_slice(b":child_storage:default:sub_test2").unwrap(); let mut transaction = { let backend = test_trie(); - let changes_trie_storage = InMemoryChangesTrieStorage::::new(); let mut cache = StorageTransactionCache::default(); let mut ext = Ext::new( &mut overlay, &mut cache, &backend, - Some(&changes_trie_storage), + changes_trie::disabled_state::<_, u64>(), None, ); ext.set_child_storage(subtrie1, CHILD_INFO_1, b"abc".to_vec(), b"def".to_vec()); @@ -1139,28 +1057,4 @@ mod tests { } assert!(!duplicate); } - - #[test] - fn cannot_change_changes_trie_config_with_native_else_wasm() { - let backend = trie_backend::tests::test_trie(); - let mut overlayed_changes = Default::default(); - let changes_trie_storage = InMemoryChangesTrieStorage::::new(); - - let mut state_machine = StateMachine::new( - &backend, - Some(&changes_trie_storage), - &mut overlayed_changes, - &DummyCodeExecutor { - change_changes_trie_config: true, - native_available: false, - native_succeeds: true, - fallback_succeeds: true, - }, - "test", - &[], - Default::default(), - ); - - assert!(state_machine.execute(ExecutionStrategy::NativeElseWasm).is_err()); - } } diff --git a/primitives/state-machine/src/overlayed_changes.rs b/primitives/state-machine/src/overlayed_changes.rs index eafd31241038b..6c41ed06ef03d 100644 --- a/primitives/state-machine/src/overlayed_changes.rs +++ b/primitives/state-machine/src/overlayed_changes.rs @@ -19,8 +19,8 @@ use crate::{ backend::Backend, ChangesTrieTransaction, changes_trie::{ - NO_EXTRINSIC_INDEX, Configuration as ChangesTrieConfig, BlockNumber, build_changes_trie, - Storage as ChangesTrieStorage, + NO_EXTRINSIC_INDEX, BlockNumber, build_changes_trie, + State as ChangesTrieState, }, }; @@ -43,9 +43,8 @@ pub struct OverlayedChanges { pub(crate) prospective: OverlayedChangeSet, /// Committed changes. pub(crate) committed: OverlayedChangeSet, - /// Changes trie configuration. None by default, but could be installed by the - /// runtime if it supports change tries. - pub(crate) changes_trie_config: Option, + /// True if extrinsiscs stats must be collected. + pub(crate) collect_extrinsics: bool, } /// The storage value, used inside OverlayedChanges. @@ -184,20 +183,9 @@ impl OverlayedChanges { self.prospective.is_empty() && self.committed.is_empty() } - /// Sets the changes trie configuration. - /// - /// Returns false if configuration has been set already and we now trying - /// to install different configuration. This isn't supported now. - pub(crate) fn set_changes_trie_config(&mut self, config: ChangesTrieConfig) -> bool { - if let Some(ref old_config) = self.changes_trie_config { - // we do not support changes trie configuration' change now - if *old_config != config { - return false; - } - } - - self.changes_trie_config = Some(config); - true + /// Ask to collect/not to collect extrinsics indices where key(s) has been changed. + pub fn set_collect_extrinsics(&mut self, collect_extrinsics: bool) { + self.collect_extrinsics = collect_extrinsics; } /// Returns a double-Option: None if the key is unknown (i.e. and the query should be referred @@ -442,11 +430,11 @@ impl OverlayedChanges { /// Convert this instance with all changes into a [`StorageChanges`] instance. pub fn into_storage_changes< - B: Backend, H: Hasher, N: BlockNumber, T: ChangesTrieStorage + B: Backend, H: Hasher, N: BlockNumber >( self, backend: &B, - changes_trie_storage: Option<&T>, + changes_trie_state: Option<&ChangesTrieState>, parent_hash: H::Out, mut cache: StorageTransactionCache, ) -> Result, String> where H::Out: Ord + Encode + 'static { @@ -463,7 +451,7 @@ impl OverlayedChanges { if cache.changes_trie_transaction.is_none() { self.changes_trie_root( backend, - changes_trie_storage, + changes_trie_state, parent_hash, false, &mut cache, @@ -501,7 +489,7 @@ impl OverlayedChanges { /// Changes that are made outside of extrinsics, are marked with /// `NO_EXTRINSIC_INDEX` index. fn extrinsic_index(&self) -> Option { - match self.changes_trie_config.is_some() { + match self.collect_extrinsics { true => Some( self.storage(EXTRINSIC_INDEX) .and_then(|idx| idx.and_then(|idx| Decode::decode(&mut &*idx).ok())) @@ -557,17 +545,17 @@ impl OverlayedChanges { /// # Panics /// /// Panics on storage error, when `panic_on_storage_error` is set. - pub fn changes_trie_root, T: ChangesTrieStorage>( + pub fn changes_trie_root<'a, H: Hasher, N: BlockNumber, B: Backend>( &self, backend: &B, - changes_trie_storage: Option<&T>, + changes_trie_state: Option<&'a ChangesTrieState<'a, H, N>>, parent_hash: H::Out, panic_on_storage_error: bool, cache: &mut StorageTransactionCache, ) -> Result, ()> where H::Out: Ord + Encode + 'static { - build_changes_trie::<_, T, H, N>( + build_changes_trie::<_, H, N>( backend, - changes_trie_storage, + changes_trie_state, self, parent_hash, panic_on_storage_error, @@ -656,7 +644,6 @@ mod tests { Blake2Hasher, traits::Externalities, storage::well_known_keys::EXTRINSIC_INDEX, }; use crate::InMemoryBackend; - use crate::changes_trie::InMemoryStorage as InMemoryChangesTrieStorage; use crate::ext::Ext; use super::*; @@ -718,13 +705,12 @@ mod tests { ..Default::default() }; - let changes_trie_storage = InMemoryChangesTrieStorage::::new(); let mut cache = StorageTransactionCache::default(); let mut ext = Ext::new( &mut overlay, &mut cache, &backend, - Some(&changes_trie_storage), + crate::changes_trie::disabled_state::<_, u64>(), None, ); const ROOT: [u8; 32] = hex!("39245109cef3758c2eed2ccba8d9b370a917850af3824bc8348d505df2c298fa"); @@ -732,57 +718,10 @@ mod tests { assert_eq!(&ext.storage_root()[..], &ROOT); } - #[test] - fn changes_trie_configuration_is_saved() { - let mut overlay = OverlayedChanges::default(); - assert!(overlay.changes_trie_config.is_none()); - assert_eq!( - overlay.set_changes_trie_config( - ChangesTrieConfig { digest_interval: 4, digest_levels: 1, }, - ), - true, - ); - assert!(overlay.changes_trie_config.is_some()); - } - - #[test] - fn changes_trie_configuration_is_saved_twice() { - let mut overlay = OverlayedChanges::default(); - assert!(overlay.changes_trie_config.is_none()); - assert_eq!(overlay.set_changes_trie_config(ChangesTrieConfig { - digest_interval: 4, digest_levels: 1, - }), true); - overlay.set_extrinsic_index(0); - overlay.set_storage(vec![1], Some(vec![2])); - assert_eq!(overlay.set_changes_trie_config(ChangesTrieConfig { - digest_interval: 4, digest_levels: 1, - }), true); - assert_eq!( - strip_extrinsic_index(&overlay.prospective.top), - vec![ - (vec![1], OverlayedValue { value: Some(vec![2]), - extrinsics: Some(vec![0].into_iter().collect()) }), - ].into_iter().collect(), - ); - } - - #[test] - fn panics_when_trying_to_save_different_changes_trie_configuration() { - let mut overlay = OverlayedChanges::default(); - assert_eq!(overlay.set_changes_trie_config(ChangesTrieConfig { - digest_interval: 4, digest_levels: 1, - }), true); - assert_eq!(overlay.set_changes_trie_config(ChangesTrieConfig { - digest_interval: 2, digest_levels: 1, - }), false); - } - #[test] fn extrinsic_changes_are_collected() { let mut overlay = OverlayedChanges::default(); - let _ = overlay.set_changes_trie_config(ChangesTrieConfig { - digest_interval: 4, digest_levels: 1, - }); + overlay.set_collect_extrinsics(true); overlay.set_storage(vec![100], Some(vec![101])); diff --git a/primitives/state-machine/src/testing.rs b/primitives/state-machine/src/testing.rs index 1baf6d6b4da70..450919088a4ca 100644 --- a/primitives/state-machine/src/testing.rs +++ b/primitives/state-machine/src/testing.rs @@ -17,12 +17,15 @@ //! Test implementation for Externalities. use std::any::{Any, TypeId}; +use codec::Decode; use hash_db::Hasher; use crate::{ backend::Backend, OverlayedChanges, StorageTransactionCache, ext::Ext, InMemoryBackend, changes_trie::{ + Configuration as ChangesTrieConfiguration, InMemoryStorage as ChangesTrieInMemoryStorage, BlockNumber as ChangesTrieBlockNumber, + State as ChangesTrieState, }, }; use sp_core::{ @@ -45,6 +48,7 @@ where as Backend>::Transaction, H, N >, backend: InMemoryBackend, + changes_trie_config: Option, changes_trie_storage: ChangesTrieInMemoryStorage, extensions: Extensions, } @@ -54,12 +58,19 @@ impl TestExternalities H::Out: Ord + 'static + codec::Codec { /// Get externalities implementation. - pub fn ext(&mut self) -> Ext, ChangesTrieInMemoryStorage> { + pub fn ext(&mut self) -> Ext> { Ext::new( &mut self.overlay, &mut self.storage_transaction_cache, &self.backend, - Some(&self.changes_trie_storage), + match self.changes_trie_config.clone() { + Some(config) => Some(ChangesTrieState { + config, + zero: 0.into(), + storage: &self.changes_trie_storage, + }), + None => None, + }, Some(&mut self.extensions), ) } @@ -72,21 +83,19 @@ impl TestExternalities /// Create a new instance of `TestExternalities` with code and storage. pub fn new_with_code(code: &[u8], mut storage: Storage) -> Self { let mut overlay = OverlayedChanges::default(); + let changes_trie_config = storage.top.get(CHANGES_TRIE_CONFIG) + .and_then(|v| Decode::decode(&mut &v[..]).ok()); + overlay.set_collect_extrinsics(changes_trie_config.is_some()); assert!(storage.top.keys().all(|key| !is_child_storage_key(key))); assert!(storage.children.keys().all(|key| is_child_storage_key(key))); - super::set_changes_trie_config( - &mut overlay, - storage.top.get(&CHANGES_TRIE_CONFIG.to_vec()).cloned(), - false, - ).expect("changes trie configuration is correct in test env; qed"); - storage.top.insert(HEAP_PAGES.to_vec(), 8u64.encode()); storage.top.insert(CODE.to_vec(), code.to_vec()); TestExternalities { overlay, + changes_trie_config, changes_trie_storage: ChangesTrieInMemoryStorage::new(), backend: storage.into(), extensions: Default::default(), diff --git a/test-utils/runtime/client/src/block_builder_ext.rs b/test-utils/runtime/client/src/block_builder_ext.rs index 7626ac2ffa5f4..6b9a6f79ab191 100644 --- a/test-utils/runtime/client/src/block_builder_ext.rs +++ b/test-utils/runtime/client/src/block_builder_ext.rs @@ -17,6 +17,7 @@ //! Block Builder extensions for tests. use sp_api::{ApiExt, ProvideRuntimeApi}; +use sp_core::ChangesTrieConfiguration; use sc_client_api::backend; use sp_runtime::traits::HasherFor; @@ -32,6 +33,11 @@ pub trait BlockBuilderExt { key: Vec, value: Option>, ) -> Result<(), sp_blockchain::Error>; + /// Add changes trie configuration update extrinsic to the block. + fn push_changes_trie_configuration_update( + &mut self, + new_config: Option, + ) -> Result<(), sp_blockchain::Error>; } impl<'a, A, B> BlockBuilderExt for sc_block_builder::BlockBuilder<'a, substrate_test_runtime::Block, A, B> where @@ -57,4 +63,11 @@ impl<'a, A, B> BlockBuilderExt for sc_block_builder::BlockBuilder<'a, substrate_ ) -> Result<(), sp_blockchain::Error> { self.push(substrate_test_runtime::Extrinsic::StorageChange(key, value)) } + + fn push_changes_trie_configuration_update( + &mut self, + new_config: Option, + ) -> Result<(), sp_blockchain::Error> { + self.push(substrate_test_runtime::Extrinsic::ChangesTrieConfigUpdate(new_config)) + } } diff --git a/test-utils/runtime/client/src/lib.rs b/test-utils/runtime/client/src/lib.rs index 0476be2f60201..7646f4a96023c 100644 --- a/test-utils/runtime/client/src/lib.rs +++ b/test-utils/runtime/client/src/lib.rs @@ -30,7 +30,7 @@ pub use sc_client::LongestChain; pub use self::block_builder_ext::BlockBuilderExt; -use sp_core::sr25519; +use sp_core::{sr25519, ChangesTrieConfiguration}; use sp_core::storage::{ChildInfo, Storage, StorageChild}; use substrate_test_runtime::genesismap::{GenesisConfig, additional_storage_with_genesis}; use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Hash as HashT, NumberFor, HasherFor}; @@ -42,7 +42,6 @@ use sc_client::{ }, }; - /// A prelude to import in tests. pub mod prelude { // Trait extensions @@ -92,7 +91,7 @@ pub type LightExecutor = sc_client::light::call_executor::GenesisCallExecutor< /// Parameters of test-client builder with test-runtime. #[derive(Default)] pub struct GenesisParameters { - support_changes_trie: bool, + changes_trie_config: Option, heap_pages_override: Option, extra_storage: Storage, } @@ -100,7 +99,7 @@ pub struct GenesisParameters { impl GenesisParameters { fn genesis_config(&self) -> GenesisConfig { GenesisConfig::new( - self.support_changes_trie, + self.changes_trie_config.clone(), vec![ sr25519::Public::from(Sr25519Keyring::Alice).into(), sr25519::Public::from(Sr25519Keyring::Bob).into(), @@ -171,9 +170,9 @@ pub trait TestClientBuilderExt: Sized { /// Returns a mutable reference to the genesis parameters. fn genesis_init_mut(&mut self) -> &mut GenesisParameters; - /// Enable or disable support for changes trie in genesis. - fn set_support_changes_trie(mut self, support_changes_trie: bool) -> Self { - self.genesis_init_mut().support_changes_trie = support_changes_trie; + /// Set changes trie configuration for genesis. + fn changes_trie_config(mut self, config: Option) -> Self { + self.genesis_init_mut().changes_trie_config = config; self } diff --git a/test-utils/runtime/src/genesismap.rs b/test-utils/runtime/src/genesismap.rs index f6fbcddf44e16..25d9a807ccee1 100644 --- a/test-utils/runtime/src/genesismap.rs +++ b/test-utils/runtime/src/genesismap.rs @@ -36,7 +36,7 @@ pub struct GenesisConfig { impl GenesisConfig { pub fn new( - support_changes_trie: bool, + changes_trie_config: Option, authorities: Vec, endowed_accounts: Vec, balance: u64, @@ -44,10 +44,7 @@ impl GenesisConfig { extra_storage: Storage, ) -> Self { GenesisConfig { - changes_trie_config: match support_changes_trie { - true => Some(super::changes_trie_config()), - false => None, - }, + changes_trie_config, authorities: authorities.clone(), balances: endowed_accounts.into_iter().map(|a| (a, balance)).collect(), heap_pages_override, diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index b4c2e849a6113..e68ed5f6fc17e 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -25,7 +25,7 @@ pub mod system; use sp_std::{prelude::*, marker::PhantomData}; use codec::{Encode, Decode, Input, Error}; -use sp_core::{Blake2Hasher, OpaqueMetadata, RuntimeDebug}; +use sp_core::{Blake2Hasher, OpaqueMetadata, RuntimeDebug, ChangesTrieConfiguration}; use sp_application_crypto::{ed25519, sr25519, RuntimeAppPublic}; use trie_db::{TrieMut, Trie}; use sp_trie::PrefixedMemoryDB; @@ -111,6 +111,7 @@ pub enum Extrinsic { Transfer(Transfer, AccountSignature), IncludeData(Vec), StorageChange(Vec, Option>), + ChangesTrieConfigUpdate(Option), } #[cfg(feature = "std")] @@ -135,6 +136,8 @@ impl BlindCheckable for Extrinsic { }, Extrinsic::IncludeData(_) => Err(InvalidTransaction::BadProof.into()), Extrinsic::StorageChange(key, value) => Ok(Extrinsic::StorageChange(key, value)), + Extrinsic::ChangesTrieConfigUpdate(new_config) => + Ok(Extrinsic::ChangesTrieConfigUpdate(new_config)), } } } @@ -196,14 +199,6 @@ pub fn run_tests(mut input: &[u8]) -> Vec { [stxs.len() as u8].encode() } -/// Changes trie configuration (optionally) used in tests. -pub fn changes_trie_config() -> sp_core::ChangesTrieConfiguration { - sp_core::ChangesTrieConfiguration { - digest_interval: 4, - digest_levels: 2, - } -} - /// A type that can not be decoded. #[derive(PartialEq)] pub struct DecodeFails { diff --git a/test-utils/runtime/src/system.rs b/test-utils/runtime/src/system.rs index b04ebc1cf2f56..4d12ab8437d5b 100644 --- a/test-utils/runtime/src/system.rs +++ b/test-utils/runtime/src/system.rs @@ -35,7 +35,7 @@ use frame_system::Trait; use crate::{ AccountId, BlockNumber, Extrinsic, Transfer, H256 as Hash, Block, Header, Digest, AuthorityId }; -use sp_core::storage::well_known_keys; +use sp_core::{storage::well_known_keys, ChangesTrieConfiguration}; const NONCE_OF: &[u8] = b"nonce:"; const BALANCE_OF: &[u8] = b"balance:"; @@ -51,6 +51,7 @@ decl_storage! { Number get(fn number): Option; ParentHash get(fn parent_hash): Hash; NewAuthorities get(fn new_authorities): Option>; + NewChangesTrieConfig get(fn new_changes_trie_config): Option>; StorageDigest get(fn storage_digest): Option; Authorities get(fn authorities) config(): Vec; } @@ -206,6 +207,8 @@ pub fn finalize_block() -> Header { let mut digest = ::take().expect("StorageDigest is set by `initialize_block`"); let o_new_authorities = ::take(); + let new_changes_trie_config = ::take(); + // This MUST come after all changes to storage are done. Otherwise we will fail the // “Storage root does not match that calculated” assertion. let storage_root = Hash::decode(&mut &storage_root()[..]) @@ -222,6 +225,12 @@ pub fn finalize_block() -> Header { digest.push(generic::DigestItem::Consensus(*b"babe", new_authorities.encode())); } + if let Some(new_config) = new_changes_trie_config { + digest.push(generic::DigestItem::ChangesTrieSignal( + generic::ChangesTrieSignal::NewConfiguration(new_config) + )); + } + Header { number, extrinsics_root, @@ -244,6 +253,8 @@ fn execute_transaction_backend(utx: &Extrinsic) -> ApplyExtrinsicResult { Extrinsic::AuthoritiesChange(ref new_auth) => execute_new_authorities_backend(new_auth), Extrinsic::IncludeData(_) => Ok(Ok(())), Extrinsic::StorageChange(key, value) => execute_storage_change(key, value.as_ref().map(|v| &**v)), + Extrinsic::ChangesTrieConfigUpdate(ref new_config) => + execute_changes_trie_config_update(new_config.clone()), } } @@ -286,6 +297,18 @@ fn execute_storage_change(key: &[u8], value: Option<&[u8]>) -> ApplyExtrinsicRes Ok(Ok(())) } +fn execute_changes_trie_config_update(new_config: Option) -> ApplyExtrinsicResult { + match new_config.clone() { + Some(new_config) => storage::unhashed::put_raw( + well_known_keys::CHANGES_TRIE_CONFIG, + &new_config.encode(), + ), + None => storage::unhashed::kill(well_known_keys::CHANGES_TRIE_CONFIG), + } + ::put(new_config); + Ok(Ok(())) +} + #[cfg(feature = "std")] fn info_expect_equal_hash(given: &Hash, expected: &Hash) { use sp_core::hexdisplay::HexDisplay;