From 3485cf4896979dcee96b7a32c5b918d25362ea8f Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Sun, 1 Sep 2024 14:21:20 +0800 Subject: [PATCH] Make format strings more consistent within this file Pure tiny refactoring, no logical changes. The benefit of consistent format strings is 10 lines of deletion. The other style changes are added by the rust formatter. --- substrate/client/db/src/lib.rs | 201 ++++++++++++++++----------------- 1 file changed, 95 insertions(+), 106 deletions(-) diff --git a/substrate/client/db/src/lib.rs b/substrate/client/db/src/lib.rs index 8cc385b18bd74..87ef67dbe064c 100644 --- a/substrate/client/db/src/lib.rs +++ b/substrate/client/db/src/lib.rs @@ -538,7 +538,7 @@ impl BlockchainDb { fn insert_justifications_if_pinned(&self, hash: Block::Hash, justification: Justification) { let mut cache = self.pinned_blocks_cache.write(); if !cache.contains(hash) { - return + return; } let justifications = Justifications::from(justification); @@ -551,7 +551,7 @@ impl BlockchainDb { fn insert_persisted_justifications_if_pinned(&self, hash: Block::Hash) -> ClientResult<()> { let mut cache = self.pinned_blocks_cache.write(); if !cache.contains(hash) { - return Ok(()) + return Ok(()); } let justifications = self.justifications_uncached(hash)?; @@ -565,7 +565,7 @@ impl BlockchainDb { fn insert_persisted_body_if_pinned(&self, hash: Block::Hash) -> ClientResult<()> { let mut cache = self.pinned_blocks_cache.write(); if !cache.contains(hash) { - return Ok(()) + return Ok(()); } let body = self.body_uncached(hash)?; @@ -592,11 +592,11 @@ impl BlockchainDb { )? { Some(justifications) => match Decode::decode(&mut &justifications[..]) { Ok(justifications) => Ok(Some(justifications)), - Err(err) => + Err(err) => { return Err(sp_blockchain::Error::Backend(format!( - "Error decoding justifications: {}", - err - ))), + "Error decoding justifications: {err}" + ))) + }, }, None => Ok(None), } @@ -609,11 +609,11 @@ impl BlockchainDb { // Plain body match Decode::decode(&mut &body[..]) { Ok(body) => return Ok(Some(body)), - Err(err) => + Err(err) => { return Err(sp_blockchain::Error::Backend(format!( - "Error decoding body: {}", - err - ))), + "Error decoding body: {err}" + ))) + }, } } @@ -636,18 +636,17 @@ impl BlockchainDb { let ex = Block::Extrinsic::decode(&mut input).map_err( |err| { sp_blockchain::Error::Backend(format!( - "Error decoding indexed extrinsic: {}", - err + "Error decoding indexed extrinsic: {err}" )) }, )?; body.push(ex); }, - None => + None => { return Err(sp_blockchain::Error::Backend(format!( - "Missing indexed transaction {:?}", - hash - ))), + "Missing indexed transaction {hash:?}" + ))) + }, }; }, DbExtrinsic::Full(ex) => { @@ -655,13 +654,13 @@ impl BlockchainDb { }, } } - return Ok(Some(body)) + return Ok(Some(body)); }, - Err(err) => + Err(err) => { return Err(sp_blockchain::Error::Backend(format!( - "Error decoding body list: {}", - err - ))), + "Error decoding body list: {err}", + ))) + }, } } Ok(None) @@ -672,7 +671,7 @@ impl sc_client_api::blockchain::HeaderBackend for Blockcha fn header(&self, hash: Block::Hash) -> ClientResult> { let mut cache = self.header_cache.lock(); if let Some(result) = cache.get_refresh(&hash) { - return Ok(result.clone()) + return Ok(result.clone()); } let header = utils::read_header( &*self.db, @@ -724,7 +723,7 @@ impl sc_client_api::blockchain::Backend for BlockchainDb ClientResult>> { let cache = self.pinned_blocks_cache.read(); if let Some(result) = cache.body(&hash) { - return Ok(result.clone()) + return Ok(result.clone()); } self.body_uncached(hash) @@ -733,7 +732,7 @@ impl sc_client_api::blockchain::Backend for BlockchainDb ClientResult> { let cache = self.pinned_blocks_cache.read(); if let Some(result) = cache.justifications(&hash) { - return Ok(result.clone()) + return Ok(result.clone()); } self.justifications_uncached(hash) @@ -776,18 +775,19 @@ impl sc_client_api::blockchain::Backend for BlockchainDb transactions.push(t), - None => + None => { return Err(sp_blockchain::Error::Backend(format!( - "Missing indexed transaction {:?}", - hash - ))), + "Missing indexed transaction {hash:?}", + ))) + }, } } } Ok(Some(transactions)) }, - Err(err) => - Err(sp_blockchain::Error::Backend(format!("Error decoding body list: {}", err))), + Err(err) => { + Err(sp_blockchain::Error::Backend(format!("Error decoding body list: {err}"))) + }, } } } @@ -810,8 +810,7 @@ impl HeaderMetadata for BlockchainDb { }) .ok_or_else(|| { ClientError::UnknownBlock(format!( - "Header was not found in the database: {:?}", - hash + "Header was not found in the database: {hash:?}", )) }) }, @@ -851,14 +850,15 @@ impl BlockImportOperation { count += 1; let key = crate::offchain::concatenate_prefix_and_key(&prefix, &key); match value_operation { - OffchainOverlayedChange::SetValue(val) => - transaction.set_from_vec(columns::OFFCHAIN, &key, val), + OffchainOverlayedChange::SetValue(val) => { + transaction.set_from_vec(columns::OFFCHAIN, &key, val) + }, OffchainOverlayedChange::Remove => transaction.remove(columns::OFFCHAIN, &key), } } if count > 0 { - log::debug!(target: "sc_offchain", "Applied {} offchain indexing changes.", count); + log::debug!(target: "sc_offchain", "Applied {count} offchain indexing changes."); } } @@ -877,7 +877,7 @@ impl BlockImportOperation { state_version: StateVersion, ) -> ClientResult { if storage.top.keys().any(|k| well_known_keys::is_child_storage_key(k)) { - return Err(sp_blockchain::Error::InvalidState) + return Err(sp_blockchain::Error::InvalidState); } let child_delta = storage.children_default.values().map(|child_content| { @@ -1011,7 +1011,7 @@ impl sp_state_machine::Storage> for StorageDb Backend { // Older DB versions have no last state key. Check if the state is available and set it. let info = backend.blockchain.info(); - if info.finalized_state.is_none() && - info.finalized_hash != Default::default() && - sc_client_api::Backend::have_state_at( + if info.finalized_state.is_none() + && info.finalized_hash != Default::default() + && sc_client_api::Backend::have_state_at( &backend, info.finalized_hash, info.finalized_number, @@ -1285,10 +1285,10 @@ impl Backend { let meta = self.blockchain.meta.read(); - if meta.best_number.saturating_sub(best_number).saturated_into::() > - self.canonicalization_delay + if meta.best_number.saturating_sub(best_number).saturated_into::() + > self.canonicalization_delay { - return Err(sp_blockchain::Error::SetHeadTooOld) + return Err(sp_blockchain::Error::SetHeadTooOld); } let parent_exists = @@ -1307,7 +1307,7 @@ impl Backend { (&r.number, &r.hash) ); - return Err(sp_blockchain::Error::NotInFinalizedChain) + return Err(sp_blockchain::Error::NotInFinalizedChain); } retracted.push(r.hash); @@ -1345,14 +1345,13 @@ impl Backend { ) -> ClientResult<()> { let last_finalized = last_finalized.unwrap_or_else(|| self.blockchain.meta.read().finalized_hash); - if last_finalized != self.blockchain.meta.read().genesis_hash && - *header.parent_hash() != last_finalized + if last_finalized != self.blockchain.meta.read().genesis_hash + && *header.parent_hash() != last_finalized { return Err(sp_blockchain::Error::NonSequentialFinalization(format!( - "Last finalized {:?} not parent of {:?}", - last_finalized, + "Last finalized {last_finalized:?} not parent of {:?}", header.hash() - ))) + ))); } Ok(()) } @@ -1429,10 +1428,10 @@ impl Backend { hash_to_canonicalize, to_canonicalize.saturated_into(), ) { - return Ok(()) + return Ok(()); } - trace!(target: "db", "Canonicalize block #{} ({:?})", to_canonicalize, hash_to_canonicalize); + trace!(target: "db", "Canonicalize block #{to_canonicalize} ({hash_to_canonicalize:?})"); let commit = self.storage.state_db.canonicalize_block(&hash_to_canonicalize).map_err( sp_blockchain::Error::from_state_db::< sc_state_db::Error, @@ -1615,21 +1614,16 @@ impl Backend { let finalized = number_u64 == 0 || pending_block.leaf_state.is_final(); finalized } else { - (number.is_zero() && last_finalized_num.is_zero()) || - pending_block.leaf_state.is_final() + (number.is_zero() && last_finalized_num.is_zero()) + || pending_block.leaf_state.is_final() }; let header = &pending_block.header; let is_best = pending_block.leaf_state.is_best(); debug!( target: "db", - "DB Commit {:?} ({}), best={}, state={}, existing={}, finalized={}", - hash, - number, - is_best, + "DB Commit {hash:?} ({number}), best={is_best}, state={}, existing={existing_header}, finalized={finalized}", operation.commit_state, - existing_header, - finalized, ); self.state_usage.merge_sm(operation.old_state.usage_info()); @@ -1699,7 +1693,7 @@ impl Backend { debug!(target: "db", "Removed block gap."); } else { block_gap = Some((start, end)); - debug!(target: "db", "Update block gap. {:?}", block_gap); + debug!(target: "db", "Update block gap. {block_gap:?}"); transaction.set( columns::META, meta_keys::BLOCK_GAP, @@ -1707,13 +1701,14 @@ impl Backend { ); } } - } else if number > best_num + One::one() && - number > One::one() && self.blockchain.header(parent_hash)?.is_none() + } else if number > best_num + One::one() + && number > One::one() + && self.blockchain.header(parent_hash)?.is_none() { let gap = (best_num + One::one(), number - One::one()); transaction.set(columns::META, meta_keys::BLOCK_GAP, &gap.encode()); block_gap = Some(gap); - debug!(target: "db", "Detected block gap {:?}", block_gap); + debug!(target: "db", "Detected block gap {block_gap:?}"); } } @@ -1747,9 +1742,8 @@ impl Backend { }); } else { return Err(sp_blockchain::Error::UnknownBlock(format!( - "Cannot set head {:?}", - set_head - ))) + "Cannot set head {set_head:?}", + ))); } } @@ -1759,7 +1753,7 @@ impl Backend { // Code beyond this point can't fail. if let Some((header, hash)) = imported { - trace!(target: "db", "DB Commit done {:?}", hash); + trace!(target: "db", "DB Commit done {hash:?}"); let header_metadata = CachedHeaderMetadata::from(&header); self.blockchain.insert_header_metadata(header_metadata.hash, header_metadata); cache_header(&mut self.blockchain.header_cache.lock(), hash, Some(header)); @@ -1875,7 +1869,7 @@ impl Backend { transaction: &mut Transaction, id: BlockId, ) -> ClientResult<()> { - debug!(target: "db", "Removing block #{}", id); + debug!(target: "db", "Removing block #{id}"); utils::remove_from_db( transaction, &*self.storage.db, @@ -1901,17 +1895,18 @@ impl Backend { id, )?; match Vec::>::decode(&mut &index[..]) { - Ok(index) => + Ok(index) => { for ex in index { if let DbExtrinsic::Indexed { hash, .. } = ex { transaction.release(columns::TRANSACTION, hash); } - }, - Err(err) => + } + }, + Err(err) => { return Err(sp_blockchain::Error::Backend(format!( - "Error decoding body list: {}", - err - ))), + "Error decoding body list: {err}", + ))) + }, } } Ok(()) @@ -2135,17 +2130,17 @@ impl sc_client_api::backend::Backend for Backend { let last_finalized = self.blockchain.last_finalized()?; // We can do a quick check first, before doing a proper but more expensive check - if number > self.blockchain.info().finalized_number || - (hash != last_finalized && !is_descendent_of(&hash, &last_finalized)?) + if number > self.blockchain.info().finalized_number + || (hash != last_finalized && !is_descendent_of(&hash, &last_finalized)?) { - return Err(ClientError::NotInFinalizedChain) + return Err(ClientError::NotInFinalizedChain); } let justifications = if let Some(mut stored_justifications) = self.blockchain.justifications(hash)? { if !stored_justifications.append(justification) { - return Err(ClientError::BadJustification("Duplicate consensus engine ID".into())) + return Err(ClientError::BadJustification("Duplicate consensus engine ID".into())); } stored_justifications } else { @@ -2230,13 +2225,12 @@ impl sc_client_api::backend::Backend for Backend { let mut revert_blocks = || -> ClientResult> { for c in 0..n.saturated_into::() { if number_to_revert.is_zero() { - return Ok(c.saturated_into::>()) + return Ok(c.saturated_into::>()); } let mut transaction = Transaction::new(); let removed = self.blockchain.header(hash_to_revert)?.ok_or_else(|| { sp_blockchain::Error::UnknownBlock(format!( - "Error reverting to {}. Block header not found.", - hash_to_revert, + "Error reverting to {hash_to_revert}. Block header not found.", )) })?; let removed_hash = removed.hash(); @@ -2246,7 +2240,7 @@ impl sc_client_api::backend::Backend for Backend { if prev_number == best_number { best_hash } else { *removed.parent_hash() }; if !self.have_state_at(prev_hash, prev_number) { - return Ok(c.saturated_into::>()) + return Ok(c.saturated_into::>()); } match self.storage.state_db.revert_one() { @@ -2272,11 +2266,10 @@ impl sc_client_api::backend::Backend for Backend { reverted_finalized.insert(removed_hash); if let Some((hash, _)) = self.blockchain.info().finalized_state { if hash == hash_to_revert { - if !number_to_revert.is_zero() && - self.have_state_at( - prev_hash, - number_to_revert - One::one(), - ) { + if !number_to_revert.is_zero() + && self + .have_state_at(prev_hash, number_to_revert - One::one()) + { let lookup_key = utils::number_and_hash_to_lookup_key( number_to_revert - One::one(), prev_hash, @@ -2342,23 +2335,21 @@ impl sc_client_api::backend::Backend for Backend { let best_hash = self.blockchain.info().best_hash; if best_hash == hash { - return Err(sp_blockchain::Error::Backend(format!("Can't remove best block {:?}", hash))) + return Err(sp_blockchain::Error::Backend(format!("Can't remove best block {hash:?}"))); } let hdr = self.blockchain.header_metadata(hash)?; if !self.have_state_at(hash, hdr.number) { return Err(sp_blockchain::Error::UnknownBlock(format!( - "State already discarded for {:?}", - hash - ))) + "State already discarded for {hash:?}", + ))); } let mut leaves = self.blockchain.leaves.write(); if !leaves.contains(hdr.number, hash) { return Err(sp_blockchain::Error::Backend(format!( - "Can't remove non-leaf block {:?}", - hash - ))) + "Can't remove non-leaf block {hash:?}", + ))); } let mut transaction = Transaction::new(); @@ -2398,7 +2389,7 @@ impl sc_client_api::backend::Backend for Backend { if let Some(outcome) = remove_outcome { leaves.undo().undo_remove(outcome); } - return Err(e.into()) + return Err(e.into()); } self.blockchain().remove_header_metadata(hash); Ok(()) @@ -2420,7 +2411,7 @@ impl sc_client_api::backend::Backend for Backend { .build(); let state = RefTrackingState::new(db_state, self.storage.clone(), None); - return Ok(RecordStatsState::new(state, None, self.state_usage.clone())) + return Ok(RecordStatsState::new(state, None, self.state_usage.clone())); } } @@ -2446,8 +2437,7 @@ impl sc_client_api::backend::Backend for Backend { Ok(RecordStatsState::new(state, Some(hash), self.state_usage.clone())) } else { Err(sp_blockchain::Error::UnknownBlock(format!( - "State already discarded for {:?}", - hash + "State already discarded for {hash:?}", ))) } }, @@ -2512,16 +2502,14 @@ impl sc_client_api::backend::Backend for Backend { self.storage.state_db.pin(&hash, number.saturated_into::(), hint).map_err( |_| { sp_blockchain::Error::UnknownBlock(format!( - "Unable to pin: state already discarded for `{:?}`", - hash + "Unable to pin: state already discarded for `{hash:?}`", )) }, )?; } else { return Err(ClientError::UnknownBlock(format!( - "Can not pin block with hash `{:?}`. Block not found.", - hash - ))) + "Can not pin block with hash `{hash:?}`. Block not found.", + ))); } if self.blocks_pruning != BlocksPruning::KeepAll { @@ -4226,8 +4214,9 @@ pub(crate) mod tests { match pruning_mode { // we can only revert to blocks for which we have state, if pruning is enabled // then the last state available will be that of the latest finalized block - BlocksPruning::Some(_) => - assert_eq!(backend.blockchain().info().finalized_number, 8), + BlocksPruning::Some(_) => { + assert_eq!(backend.blockchain().info().finalized_number, 8) + }, // otherwise if we're not doing state pruning we can revert past finalized blocks _ => assert_eq!(backend.blockchain().info().finalized_number, 5), }