Skip to content

Commit

Permalink
Prestore block to db before orphan pool, use fill unverified thread t…
Browse files Browse the repository at this point in the history
…o pre load unverified block

Signed-off-by: Eval EXEC <execvy@gmail.com>
  • Loading branch information
eval-exec committed Mar 26, 2024
1 parent 6aac1b1 commit 40b203c
Show file tree
Hide file tree
Showing 15 changed files with 730 additions and 733 deletions.
47 changes: 23 additions & 24 deletions chain/src/chain_service.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! CKB chain service.
#![allow(missing_docs)]

use crate::{LonelyBlock, ProcessBlockRequest};
use ckb_channel::{select, Receiver, Sender};
use crate::orphan_broker::OrphanBroker;
use crate::{LonelyBlock, LonelyBlockHash, ProcessBlockRequest};
use ckb_channel::{select, Receiver};
use ckb_error::{Error, InternalErrorKind};
use ckb_logger::{self, debug, error, info, warn};
use ckb_shared::block_status::BlockStatus;
Expand All @@ -13,26 +14,25 @@ use ckb_verification::{BlockVerifier, NonContextualBlockTxsVerifier};
use ckb_verification_traits::Verifier;

/// Chain background service to receive LonelyBlock and only do `non_contextual_verify`
#[derive(Clone)]
pub(crate) struct ChainService {
shared: Shared,

process_block_rx: Receiver<ProcessBlockRequest>,

lonely_block_tx: Sender<LonelyBlock>,
orphan_broker: OrphanBroker,
}
impl ChainService {
/// Create a new ChainService instance with shared.
pub(crate) fn new(
shared: Shared,
process_block_rx: Receiver<ProcessBlockRequest>,

lonely_block_tx: Sender<LonelyBlock>,
consume_orphan: OrphanBroker,
) -> ChainService {
ChainService {
shared,
process_block_rx,
lonely_block_tx,
orphan_broker: consume_orphan,
}
}

Expand Down Expand Up @@ -127,25 +127,24 @@ impl ChainService {
}
}

if let Some(metrics) = ckb_metrics::handle() {
metrics
.ckb_chain_lonely_block_ch_len
.set(self.lonely_block_tx.len() as i64)
if let Err(err) = self.insert_block(&lonely_block) {
error!(
"insert block {}-{} failed: {:?}",
block_number, block_hash, err
);
self.shared.block_status_map().remove(&block_hash);
lonely_block.execute_callback(Err(err));
return;
}

match self.lonely_block_tx.send(lonely_block) {
Ok(_) => {
debug!(
"processing block: {}-{}, (tip:unverified_tip):({}:{})",
block_number,
block_hash,
self.shared.snapshot().tip_number(),
self.shared.get_unverified_tip().number(),
);
}
Err(_) => {
error!("Failed to notify new block to orphan pool, It seems that the orphan pool has exited.");
}
}
let lonely_block_hash: LonelyBlockHash = lonely_block.into();
self.orphan_broker.process_lonely_block(lonely_block_hash);
}

fn insert_block(&self, lonely_block: &LonelyBlock) -> Result<(), ckb_error::Error> {
let db_txn = self.shared.store().begin_transaction();
db_txn.insert_block(lonely_block.block())?;
db_txn.commit()?;
Ok(())
}
}
Loading

0 comments on commit 40b203c

Please sign in to comment.