Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent ckb-shared import ckb-db-schema #4524

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions shared/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 22 additions & 2 deletions store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 {
Expand Down
Loading