diff --git a/storage/src/db_storage/mod.rs b/storage/src/db_storage/mod.rs index 10650db9f9..97d2a87ad5 100644 --- a/storage/src/db_storage/mod.rs +++ b/storage/src/db_storage/mod.rs @@ -51,7 +51,7 @@ impl DBStorage { let mut remove_cf_vec = Vec::new(); db_cfs_set.iter().for_each(|k| { if !cfs_set.contains(&k.as_str()) { - remove_cf_vec.push(k.clone()); + remove_cf_vec.push(<&std::string::String>::clone(k)); } }); ensure!( diff --git a/sync/src/tasks/block_sync_task.rs b/sync/src/tasks/block_sync_task.rs index 2d666612d1..6f08bf56f1 100644 --- a/sync/src/tasks/block_sync_task.rs +++ b/sync/src/tasks/block_sync_task.rs @@ -205,15 +205,15 @@ impl BlockCollector { fn apply_block(&mut self, block: Block, peer_id: Option) -> Result<()> { if let Err(err) = self.chain.apply(block.clone()) { - return match err.downcast::() { + match err.downcast::() { Ok(e) => { self.chain .get_storage() .save_failed_block(block.id(), block, peer_id)?; Err(e.into()) } - Err(e) => Err(e.into()), - }; + Err(e) => Err(e), + } } else { Ok(()) } @@ -297,16 +297,14 @@ mod tests { block_ids: Vec, ) -> BoxFuture)>>> { let blocks = self.blocks.lock().unwrap(); - let result: Result> = block_ids + let result: Result)>> = block_ids .iter() .map(|block_id| { - ( - blocks - .get(block_id) - .cloned() - .ok_or_else(|| format_err!("Can not find block by id: {:?}", block_id)), - None, - ) + if let Some(block) = blocks.get(block_id).cloned() { + Ok((block, None)) + } else { + Err(format_err!("Can not find block by id: {:?}", block_id)) + } }) .collect(); async { @@ -386,9 +384,9 @@ mod tests { let result = sync_task.await?; let last_block_number = result .iter() - .map(|(block, block_info)| { - assert!(block_info.is_none()); - block.header().number as i64 + .map(|block_data| { + assert!(block_data.info.is_none()); + block_data.block.header().number as i64 }) .fold(-1, |parent, current| { //ensure return block is ordered @@ -430,13 +428,13 @@ mod tests { let result = sync_task.await?; let last_block_number = result .iter() - .map(|(block, block_info)| { - if block.header().number() % 2 == 0 { - assert!(block_info.is_some()) + .map(|block_data| { + if block_data.block.header().number() % 2 == 0 { + assert!(block_data.info.is_some()) } else { - assert!(block_info.is_none()) + assert!(block_data.info.is_none()) } - block.header().number as i64 + block_data.block.header().number as i64 }) .fold(-1, |parent, current| { //ensure return block is ordered diff --git a/sync/src/tasks/mock.rs b/sync/src/tasks/mock.rs index 0cac6893a5..3a051ca55e 100644 --- a/sync/src/tasks/mock.rs +++ b/sync/src/tasks/mock.rs @@ -142,15 +142,14 @@ impl BlockFetcher for SyncNodeMocker { &self, block_ids: Vec, ) -> BoxFuture<'_, Result)>>> { - let result: Result> = block_ids + let result: Result)>> = block_ids .into_iter() .map(|block_id| { - ( - self.chain() - .get_block(block_id)? - .ok_or_else(|| format_err!("Can not find block by id: {}", block_id)), - None, - ) + if let Some(block) = self.chain().get_block(block_id)? { + Ok((block, None)) + } else { + Err(format_err!("Can not find block by id: {}", block_id)) + } }) .collect(); async move { diff --git a/sync/src/tasks/tests.rs b/sync/src/tasks/tests.rs index a853728a0c..e742ba65e9 100644 --- a/sync/src/tasks/tests.rs +++ b/sync/src/tasks/tests.rs @@ -11,6 +11,7 @@ use pin_utils::core_reexport::time::Duration; use starcoin_chain_api::ChainReader; use starcoin_chain_mock::BlockChain; use starcoin_genesis::Genesis; +use starcoin_storage::block_info::BlockInfoStore; use starcoin_storage::BlockStore; use starcoin_types::block::{Block, BlockBody, BlockHeader}; use starcoin_vm_types::genesis_config::{BuiltinNetworkID, ChainNetwork}; @@ -86,15 +87,16 @@ pub async fn test_failed_block() -> Result<()> { let net = ChainNetwork::new_builtin(BuiltinNetworkID::Halley); let (storage, startup_info, _) = Genesis::init_storage_for_test(&net)?; + let block_info = storage.get_block_info(startup_info.main)?.unwrap(); let chain = BlockChain::new(net.time_service(), startup_info.main, storage.clone())?; let (sender, _) = unbounded(); - let mut block_collector = BlockCollector::new_with_handle(chain, sender); + let mut block_collector = BlockCollector::new_with_handle(block_info, chain, sender); let mut header = BlockHeader::random(); header.number = 1; let body = BlockBody::new(Vec::new(), None); let failed_block = Block::new(header, body); let failed_block_id = failed_block.id(); - if let Err(_) = block_collector.apply_block_for_test(failed_block) { + if block_collector.apply_block_for_test(failed_block).is_err() { assert!(storage.get_failed_block_by_id(failed_block_id)?.is_some()); Ok(()) } else {