Skip to content

Commit

Permalink
test.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangong3624749 committed Dec 9, 2020
1 parent c08df48 commit 43e03c2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
2 changes: 1 addition & 1 deletion storage/src/db_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
36 changes: 17 additions & 19 deletions sync/src/tasks/block_sync_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ impl BlockCollector {

fn apply_block(&mut self, block: Block, peer_id: Option<PeerId>) -> Result<()> {
if let Err(err) = self.chain.apply(block.clone()) {
return match err.downcast::<ConnectBlockError>() {
match err.downcast::<ConnectBlockError>() {
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(())
}
Expand Down Expand Up @@ -297,16 +297,14 @@ mod tests {
block_ids: Vec<HashValue>,
) -> BoxFuture<Result<Vec<(Block, Option<PeerId>)>>> {
let blocks = self.blocks.lock().unwrap();
let result: Result<Vec<Block>> = block_ids
let result: Result<Vec<(Block, Option<PeerId>)>> = 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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions sync/src/tasks/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,14 @@ impl BlockFetcher for SyncNodeMocker {
&self,
block_ids: Vec<HashValue>,
) -> BoxFuture<'_, Result<Vec<(Block, Option<PeerId>)>>> {
let result: Result<Vec<Block>> = block_ids
let result: Result<Vec<(Block, Option<PeerId>)>> = 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 {
Expand Down
6 changes: 4 additions & 2 deletions sync/src/tasks/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 43e03c2

Please sign in to comment.