Skip to content

Commit

Permalink
add sync test
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhhuang committed Jan 30, 2024
1 parent 832cd35 commit 6dc984d
Show file tree
Hide file tree
Showing 6 changed files with 569 additions and 483 deletions.
4 changes: 2 additions & 2 deletions chain/tests/test_txn_info_and_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ pub fn gen_txns(seq_num: &mut u64) -> Result<Vec<SignedUserTransaction>> {
fn test_transaction_info_and_proof_1() -> Result<()> {
// generate 5 block
let config = Arc::new(NodeConfig::random_for_test());
let mut block_chain: <std::prelude::v1::Result<_, anyhow::Error> as Try>::Output = test_helper::gen_blockchain_for_test(config.net())?;
block_chain.set_test_flexidag_fork_height(2);
let mut block_chain = test_helper::gen_blockchain_for_test(config.net())?;
block_chain.get_storage().save_dag_fork_number(2)?;
let _current_header = block_chain.current_header();
let miner_account = AccountInfo::random();
let mut seq_num = 0;
Expand Down
129 changes: 128 additions & 1 deletion sync/src/tasks/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ use futures::{FutureExt, StreamExt};
use futures_timer::Delay;
use network_api::messages::NotificationMessage;
use network_api::{PeerId, PeerInfo, PeerSelector, PeerStrategy};
use network_p2p_core::export::log::info;
use network_p2p_core::{NetRpcError, RpcErrorCode};
use rand::Rng;
use starcoin_account_api::AccountInfo;
use starcoin_accumulator::accumulator_info::AccumulatorInfo;
use starcoin_accumulator::{Accumulator, MerkleAccumulator};
use starcoin_chain::BlockChain;
use starcoin_chain_api::ChainReader;
Expand All @@ -27,9 +29,14 @@ use starcoin_storage::Storage;
use starcoin_sync_api::SyncTarget;
use starcoin_types::block::{Block, BlockIdAndNumber, BlockInfo, BlockNumber};
use starcoin_types::startup_info::ChainInfo;
use std::sync::Arc;
use starcoin_types::U256;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use super::block_sync_task::SyncBlockData;
use super::BlockLocalStore;

pub enum ErrorStrategy {
_RateLimitErr,
Timeout(u64),
Expand Down Expand Up @@ -134,6 +141,126 @@ impl BlockIdFetcher for MockBlockIdFetcher {
}
}

#[derive(Default)]
pub struct MockLocalBlockStore {
store: Mutex<HashMap<HashValue, SyncBlockData>>,
}

impl MockLocalBlockStore {
pub fn new() -> Self {
Self::default()
}

pub fn mock(&self, block: &Block) {
let block_id = block.id();
let block_info = BlockInfo::new(
block_id,
U256::from(1),
AccumulatorInfo::new(HashValue::random(), vec![], 0, 0),
AccumulatorInfo::new(HashValue::random(), vec![], 0, 0),
);
self.store.lock().unwrap().insert(
block.id(),
SyncBlockData::new(block.clone(), Some(block_info), Some(PeerId::random())),
);
}
}

impl BlockLocalStore for MockLocalBlockStore {
fn get_block_with_info(&self, block_ids: Vec<HashValue>) -> Result<Vec<Option<SyncBlockData>>> {
let store = self.store.lock().unwrap();
Ok(block_ids.iter().map(|id| store.get(id).cloned()).collect())
}
}

#[derive(Default)]
pub struct MockBlockFetcher {
pub blocks: Mutex<HashMap<HashValue, Block>>,
}

impl MockBlockFetcher {
pub fn new() -> Self {
Self::default()
}

pub fn put(&self, block: Block) {
self.blocks.lock().unwrap().insert(block.id(), block);
}
}

impl BlockFetcher for MockBlockFetcher {
fn fetch_blocks(
&self,
block_ids: Vec<HashValue>,
) -> BoxFuture<Result<Vec<(Block, Option<PeerId>)>>> {
let blocks = self.blocks.lock().unwrap();
let result: Result<Vec<(Block, Option<PeerId>)>> = block_ids
.iter()
.map(|block_id| {
if let Some(block) = blocks.get(block_id).cloned() {
Ok((block, Some(PeerId::random())))
} else {
Err(format_err!("Can not find block by id: {:?}", block_id))
}
})
.collect();
async {
Delay::new(Duration::from_millis(100)).await;
result
}
.boxed()
}

fn fetch_block_headers(
&self,
block_ids: Vec<HashValue>,
) -> BoxFuture<Result<Vec<(HashValue, Option<starcoin_types::block::BlockHeader>)>>> {
let blocks = self.blocks.lock().unwrap();
let result = block_ids
.iter()
.map(|block_id| {
if let Some(block) = blocks.get(block_id).cloned() {
Ok((block.id(), Some(block.header().clone())))
} else {
Err(format_err!("Can not find block by id: {:?}", block_id))
}
})
.collect();
async {
Delay::new(Duration::from_millis(100)).await;
result
}
.boxed()
}

fn fetch_dag_block_children(
&self,
block_ids: Vec<HashValue>,
) -> BoxFuture<Result<Vec<HashValue>>> {
let blocks = self.blocks.lock().unwrap();
let mut result = vec![];
block_ids.iter().for_each(|block_id| {
if let Some(block) = blocks.get(block_id).cloned() {
while let Some(hashes) = block.header().parents_hash() {
for hash in hashes {
if result.contains(&hash) {
continue;
}
result.push(hash);
}
}
} else {
info!("Can not find block by id: {:?}", block_id)
}
});
async {
Delay::new(Duration::from_millis(100)).await;
Ok(result)
}
.boxed()
}
}

pub struct SyncNodeMocker {
pub peer_id: PeerId,
pub chain_mocker: MockChain,
Expand Down
Loading

0 comments on commit 6dc984d

Please sign in to comment.