Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eval-exec committed Sep 4, 2023
1 parent 2b6029d commit cee99c3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
22 changes: 15 additions & 7 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl ChainController {
pub fn process_block(
&self,
block: Arc<BlockView>,
) -> (Result<bool, Error>, Vec<VerifyFailedBlockInfo>) {
) -> Result<Vec<VerifyFailedBlockInfo>, Error> {
self.internal_process_block(block, Switch::NONE)
}

Expand All @@ -99,7 +99,7 @@ impl ChainController {
&self,
block: Arc<BlockView>,
switch: Switch,
) -> (Result<bool, Error>, Vec<VerifyFailedBlockInfo>) {
) -> Result<Vec<VerifyFailedBlockInfo>, Error> {
Request::call(&self.process_block_sender, (block, switch)).unwrap_or_else(|| {
Err(InternalErrorKind::System
.other("Chain service has gone")
Expand Down Expand Up @@ -668,21 +668,28 @@ impl ChainService {
pub fn process_block_v2(
&self,
block: Arc<BlockView>,
peer_id: PeerId,
switch: Switch,
) -> (Result<bool, Error>, Vec<VerifyFailedBlockInfo>) {
) -> Vec<VerifyFailedBlockInfo> {
let block_number = block.number();
let block_hash = block.hash();
if block_number < 1 {
warn!("receive 0 number block: 0-{}", block_hash);
}

let failed_blocks_peer_ids: Vec<VerifyFailedBlockInfo> =
let mut failed_blocks_peer_ids: Vec<VerifyFailedBlockInfo> =
self.verify_failed_blocks_rx.iter().collect();

if !switch.disable_non_contextual() {
let result = self.non_contextual_verify(&block);
match result {
Err(err) => return (Err(err), failed_blocks_peer_ids),
Err(err) => {
failed_blocks_peer_ids.push(VerifyFailedBlockInfo {
block_hash,
peer_id,
});
return failed_blocks_peer_ids;
}
_ => {}
}
}
Expand All @@ -694,15 +701,16 @@ impl ChainService {
}
}
debug!(
"processing block: {}-{}, orphan_len: {}, (tip:unverified_tip):({}:{})",
"processing block: {}-{}, orphan_len: {}, (tip:unverified_tip):({}:{}), and return failed_blocks_peer_ids: {:?}",
block_number,
block_hash,
self.orphan_blocks_broker.len(),
self.shared.snapshot().tip_number(),
self.shared.get_unverified_tip().number(),
failed_blocks_peer_ids,
);

(Ok(false), failed_blocks_peer_ids)
failed_blocks_peer_ids
}

fn accept_block(&self, block: Arc<BlockView>) -> Result<Option<(HeaderView, U256)>, Error> {
Expand Down
1 change: 1 addition & 0 deletions shared/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ fn get_skip_height(height: BlockNumber) -> BlockNumber {

pub const SHRINK_THRESHOLD: usize = 300;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VerifyFailedBlockInfo {
pub block_hash: Byte32,
pub peer_id: PeerId,
Expand Down
2 changes: 1 addition & 1 deletion sync/src/synchronizer/block_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a> BlockProcess<'a> {
let shared = self.synchronizer.shared();

if shared.new_block_received(&block) {
let (this_block_verify_result, maliformed_peers) =
let (this_block_verify_result, malformed_peers) =
self.synchronizer.process_new_block(block.clone());

if let Err(err) = this_block_verify_result {
Expand Down
6 changes: 3 additions & 3 deletions sync/src/synchronizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,14 @@ impl Synchronizer {
pub fn process_new_block(
&self,
block: core::BlockView,
) -> (Result<bool, CKBError>, Vec<VerifyFailedBlockInfo>) {
) -> Result<Option<Vec<VerifyFailedBlockInfo>>, CKBError> {
let block_hash = block.hash();
let status = self.shared.active_chain().get_block_status(&block_hash);
// NOTE: Filtering `BLOCK_STORED` but not `BLOCK_RECEIVED`, is for avoiding
// stopping synchronization even when orphan_pool maintains dirty items by bugs.
if status.contains(BlockStatus::BLOCK_PARTIAL_STORED) {
error!("block {} already partial stored", block_hash);
(Ok(false), Vec::new())
Ok(Some(Vec::new()))
} else if status.contains(BlockStatus::HEADER_VALID) {
self.shared.insert_new_block(&self.chain, Arc::new(block))
} else {
Expand All @@ -369,7 +369,7 @@ impl Synchronizer {
status, block_hash,
);
// TODO which error should we return?
(Ok(false), Vec::new())
(Ok(Some(Vec::new())))
}
}

Expand Down
4 changes: 2 additions & 2 deletions sync/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ impl SyncShared {
&self,
chain: &ChainController,
block: Arc<core::BlockView>,
) -> (Result<bool, CKBError>, Vec<VerifyFailedBlockInfo>) {
) -> Result<Vec<VerifyFailedBlockInfo>, CKBError> {
// Insert the given block into orphan_block_pool if its parent is not found
// if !self.is_stored(&block.parent_hash()) {
// debug!(
Expand Down Expand Up @@ -1167,7 +1167,7 @@ impl SyncShared {
&self,
chain: &ChainController,
block: Arc<core::BlockView>,
) -> (Result<bool, CKBError>, Vec<VerifyFailedBlockInfo>) {
) -> Result<Vec<VerifyFailedBlockInfo>, CKBError> {
let ret = {
let mut assume_valid_target = self.state.assume_valid_target();
if let Some(ref target) = *assume_valid_target {
Expand Down

0 comments on commit cee99c3

Please sign in to comment.