From 4a05940a1d9c5f6c178dca49af125f62e593dfa2 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Wed, 17 Jul 2024 13:27:21 +0800 Subject: [PATCH] Move load COLUMN_NUMBER_HASH logic from ckb-shared to ckb-store --- shared/src/shared.rs | 27 +++++++++------------------ store/src/store.rs | 24 ++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/shared/src/shared.rs b/shared/src/shared.rs index fc3e9fea04..442d65a5cb 100644 --- a/shared/src/shared.rs +++ b/shared/src/shared.rs @@ -6,7 +6,7 @@ use ckb_chain_spec::consensus::Consensus; use ckb_constant::store::TX_INDEX_UPPER_BOUND; use ckb_constant::sync::MAX_TIP_AGE; use ckb_db::{Direction, IteratorMode}; -use ckb_db_schema::{COLUMN_BLOCK_BODY, COLUMN_NUMBER_HASH}; +use ckb_db_schema::COLUMN_BLOCK_BODY; use ckb_error::{AnyError, Error}; use ckb_notify::NotifyController; use ckb_proposal_table::ProposalView; @@ -187,23 +187,14 @@ impl Shared { e })?; - let pack_number: packed::Uint64 = number.pack(); - let prefix = pack_number.as_slice(); - for (key, value) in snapshot - .get_iter( - COLUMN_NUMBER_HASH, - IteratorMode::From(prefix, Direction::Forward), - ) - .take_while(|(key, _)| key.starts_with(prefix)) - { - let reader = packed::NumberHashReader::from_slice_should_be_ok(key.as_ref()); - let block_hash = reader.block_hash().to_entity(); - if &block_hash != hash { - let txs = - packed::Uint32Reader::from_slice_should_be_ok(value.as_ref()).unpack(); - side.insert(block_hash, (reader.number().to_entity(), txs)); - } - } + snapshot + .get_block_hashes(number) + .iter() + .for_each(|(block_hash, txs_len)| { + if &block_hash != hash { + side.insert(block_hash, (reader.number().to_entity(), txs_len)); + } + }); } self.store.write_sync(&batch).map_err(|e| { ckb_logger::error!("Freezer write_batch delete failed {}", e); diff --git a/store/src/store.rs b/store/src/store.rs index f686620701..f570cbcf16 100644 --- a/store/src/store.rs +++ b/store/src/store.rs @@ -8,8 +8,9 @@ use ckb_db_schema::{ Col, COLUMN_BLOCK_BODY, COLUMN_BLOCK_EPOCH, COLUMN_BLOCK_EXT, COLUMN_BLOCK_EXTENSION, COLUMN_BLOCK_FILTER, COLUMN_BLOCK_FILTER_HASH, COLUMN_BLOCK_HEADER, COLUMN_BLOCK_PROPOSAL_IDS, COLUMN_BLOCK_UNCLE, COLUMN_CELL, COLUMN_CELL_DATA, COLUMN_CELL_DATA_HASH, - COLUMN_CHAIN_ROOT_MMR, COLUMN_EPOCH, COLUMN_INDEX, COLUMN_META, COLUMN_TRANSACTION_INFO, - COLUMN_UNCLES, META_CURRENT_EPOCH_KEY, META_LATEST_BUILT_FILTER_DATA_KEY, META_TIP_HEADER_KEY, + COLUMN_CHAIN_ROOT_MMR, COLUMN_EPOCH, COLUMN_INDEX, COLUMN_META, COLUMN_NUMBER_HASH, + COLUMN_TRANSACTION_INFO, COLUMN_UNCLES, META_CURRENT_EPOCH_KEY, + META_LATEST_BUILT_FILTER_DATA_KEY, META_TIP_HEADER_KEY, }; use ckb_freezer::Freezer; use ckb_types::{ @@ -590,6 +591,25 @@ pub trait ChainStore: Send + Sync + Sized { self.get_ancestor(&header.parent_hash(), number) } } + + /// Gets (block_hash, txs_len) pair by a block_number from COLUMN_NUMBER_HASH + fn get_block_hashes(&self, block_number: BlockNumber) -> Vec<(packed::Byte32, u32)> { + let pack_number: packed::Uint64 = block_number.pack(); + let prefix = pack_number.as_slice(); + + self.get_iter( + COLUMN_NUMBER_HASH, + IteratorMode::From(prefix, Direction::Forward), + ) + .take_while(|(key, _)| key.starts_with(prefix)) + .map(|(key, value)| { + let reader = packed::NumberHashReader::from_slice_should_be_ok(key.as_ref()); + let block_hash = reader.block_hash().to_entity(); + let txs_len = packed::Uint32Reader::from_slice_should_be_ok(value.as_ref()).unpack(); + (block_hash, txs_len) + }) + .collect() + } } fn build_cell_meta_from_reader(out_point: OutPoint, reader: packed::CellEntryReader) -> CellMeta {