Skip to content

Commit

Permalink
Temporary put parent header to PreloadUnverified, put small BlockView to
Browse files Browse the repository at this point in the history
LonelyBlockHash

Signed-off-by: Eval EXEC <[email protected]>
  • Loading branch information
eval-exec committed May 15, 2024
1 parent 95c26d9 commit ff3cab9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 31 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ckb-network = { path = "../network", version = "= 0.116.0-pre" }
ckb-tx-pool = { path = "../tx-pool", version = "= 0.116.0-pre" }
minstant = "0.1.4"
dashmap = "4.0"
either = "1.11.0"

[dev-dependencies]
ckb-test-chain-utils = { path = "../util/test-chain-utils", version = "= 0.116.0-pre" }
Expand Down
23 changes: 16 additions & 7 deletions chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use ckb_logger::{error, info};
use ckb_store::{ChainDB, ChainStore};
use ckb_types::prelude::{Pack, Unpack};
use ckb_types::H256;
use either::Either;
pub use init::start_chain_services;

type ProcessBlockRequest = Request<LonelyBlock, ()>;
Expand Down Expand Up @@ -70,7 +71,7 @@ pub struct LonelyBlock {
/// LonelyBlock is the block which we have not check weather its parent is stored yet
pub struct LonelyBlockHash {
/// block
pub block_number_and_hash: BlockNumberAndHash,
pub block_number_and_hash: Either<BlockNumberAndHash, Arc<BlockView>>,

pub parent_hash: Byte32,

Expand Down Expand Up @@ -99,9 +100,14 @@ impl From<LonelyBlock> for LonelyBlockHash {
let epoch_number: EpochNumber = block.epoch().number();

LonelyBlockHash {
block_number_and_hash: BlockNumberAndHash {
number: block_number,
hash: block_hash,
block_number_and_hash: if block.data().serialized_size_without_uncle_proposals() > 12800
{
Either::Right(block)
} else {
Either::Left(BlockNumberAndHash {
number: block_number,
hash: block_hash,
})
},
parent_hash,
epoch_number,
Expand All @@ -119,23 +125,26 @@ impl LonelyBlockHash {
}

pub fn number_hash(&self) -> BlockNumberAndHash {
self.block_number_and_hash.clone()
match self.block_number_and_hash.as_ref() {
Either::Left(block_number_and_hash) => block_number_and_hash.to_owned(),
Either::Right(block) => BlockNumberAndHash::new(block.number(), block.hash()),
}
}

pub fn epoch_number(&self) -> EpochNumber {
self.epoch_number
}

pub fn hash(&self) -> Byte32 {
self.block_number_and_hash.hash()
self.number_hash().hash()
}

pub fn parent_hash(&self) -> Byte32 {
self.parent_hash.clone()
}

pub fn number(&self) -> BlockNumber {
self.block_number_and_hash.number()
self.number_hash().number()
}
}

Expand Down
19 changes: 9 additions & 10 deletions chain/src/orphan_broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ impl OrphanBroker {
}

fn delete_block(&self, lonely_block: &LonelyBlockHash) {
let block_hash = lonely_block.block_number_and_hash.hash();
let block_number = lonely_block.block_number_and_hash.number();
let block_hash = lonely_block.hash();
let block_number = lonely_block.number();
let parent_hash = lonely_block.parent_hash();

delete_unverified_block(self.shared.store(), block_hash, block_number, parent_hash);
}

fn process_invalid_block(&self, lonely_block: LonelyBlockHash) {
let block_hash = lonely_block.block_number_and_hash.hash();
let block_number = lonely_block.block_number_and_hash.number();
let block_hash = lonely_block.hash();
let block_number = lonely_block.number();
let parent_hash = lonely_block.parent_hash();

self.delete_block(&lonely_block);
Expand All @@ -107,8 +107,8 @@ impl OrphanBroker {
}

pub(crate) fn process_lonely_block(&self, lonely_block: LonelyBlockHash) {
let block_hash = lonely_block.block_number_and_hash.hash();
let block_number = lonely_block.block_number_and_hash.number();
let block_hash = lonely_block.hash();
let block_number = lonely_block.number();
let parent_hash = lonely_block.parent_hash();
let parent_is_pending_verify = self.is_pending_verify.contains(&parent_hash);
let parent_status = self.shared.get_block_status(&parent_hash);
Expand Down Expand Up @@ -162,8 +162,8 @@ impl OrphanBroker {
}

fn send_unverified_block(&self, lonely_block: LonelyBlockHash) {
let block_number = lonely_block.block_number_and_hash.number();
let block_hash = lonely_block.block_number_and_hash.hash();
let block_number = lonely_block.number();
let block_hash = lonely_block.hash();

if let Some(metrics) = ckb_metrics::handle() {
metrics
Expand Down Expand Up @@ -203,8 +203,7 @@ impl OrphanBroker {
}

fn process_descendant(&self, lonely_block: LonelyBlockHash) {
self.is_pending_verify
.insert(lonely_block.block_number_and_hash.hash());
self.is_pending_verify.insert(lonely_block.hash());

self.send_unverified_block(lonely_block)
}
Expand Down
51 changes: 39 additions & 12 deletions chain/src/preload_unverified_blocks_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use ckb_channel::{Receiver, Sender};
use ckb_logger::{debug, error, info};
use ckb_shared::Shared;
use ckb_store::ChainStore;
use ckb_types::core::HeaderView;
use crossbeam::select;
use either::Either;
use std::cell::Cell;
use std::sync::Arc;

pub(crate) struct PreloadUnverifiedBlocksChannel {
Expand All @@ -13,6 +16,9 @@ pub(crate) struct PreloadUnverifiedBlocksChannel {
unverified_block_tx: Sender<UnverifiedBlock>,

stop_rx: Receiver<()>,

// after we load a block from store, we put block.parent_header into this cell
prev_header: Cell<HeaderView>,
}

impl PreloadUnverifiedBlocksChannel {
Expand All @@ -22,11 +28,19 @@ impl PreloadUnverifiedBlocksChannel {
unverified_block_tx: Sender<UnverifiedBlock>,
stop_rx: Receiver<()>,
) -> Self {
let tip_hash = shared.snapshot().tip_hash();

let tip_header = shared
.store()
.get_block_header(&tip_hash)
.expect("must get tip header");

PreloadUnverifiedBlocksChannel {
shared,
preload_unverified_rx,
unverified_block_tx,
stop_rx,
prev_header: Cell::new(tip_header),
}
}

Expand All @@ -51,8 +65,8 @@ impl PreloadUnverifiedBlocksChannel {
}

fn preload_unverified_channel(&self, task: LonelyBlockHash) {
let block_number = task.block_number_and_hash.number();
let block_hash = task.block_number_and_hash.hash();
let block_number = task.number();
let block_hash = task.hash();
let unverified_block: UnverifiedBlock = self.load_full_unverified_block_by_hash(task);

if let Some(metrics) = ckb_metrics::handle() {
Expand Down Expand Up @@ -82,17 +96,30 @@ impl PreloadUnverifiedBlocksChannel {
verify_callback,
} = task;

let block_view = self
.shared
.store()
.get_block(&block_number_and_hash.hash())
.expect("block stored");
let block = Arc::new(block_view);
let block = {
match block_number_and_hash {
Either::Left(number_and_hash) => {
let block_view = self
.shared
.store()
.get_block(&number_and_hash.hash())
.expect("block stored");
Arc::new(block_view)
}
Either::Right(block) => block,
}
};

let parent_header = {
self.shared
.store()
.get_block_header(&parent_hash)
.expect("parent header stored")
let prev_header = self.prev_header.replace(block.header());
if prev_header.hash() == parent_hash {
prev_header
} else {
self.shared
.store()
.get_block_header(&parent_hash)
.expect("parent header stored")
}
};

UnverifiedBlock {
Expand Down

0 comments on commit ff3cab9

Please sign in to comment.