Skip to content

Commit

Permalink
[refactor] Rename store in Chain to chain_store (#10392)
Browse files Browse the repository at this point in the history
Follow up from PR #10388
  • Loading branch information
Shreyan Gupta authored Jan 8, 2024
1 parent 454e8c5 commit a72ab6c
Show file tree
Hide file tree
Showing 28 changed files with 289 additions and 272 deletions.
2 changes: 1 addition & 1 deletion chain/chain/src/blocks_delay_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ impl Chain {
if self.is_in_processing(block_hash) {
return BlockProcessingStatus::InProcessing;
}
if self.store().block_exists(block_hash).unwrap_or_default() {
if self.chain_store().block_exists(block_hash).unwrap_or_default() {
return BlockProcessingStatus::Accepted;
}
if let Some(dropped_reason) = &block_info.dropped {
Expand Down
194 changes: 99 additions & 95 deletions chain/chain/src/chain.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions chain/chain/src/chain_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ pub struct ChainUpdate<'a> {

impl<'a> ChainUpdate<'a> {
pub fn new(
store: &'a mut ChainStore,
chain_store: &'a mut ChainStore,
epoch_manager: Arc<dyn EpochManagerAdapter>,
shard_tracker: ShardTracker,
runtime_adapter: Arc<dyn RuntimeAdapter>,
doomslug_threshold_mode: DoomslugThresholdMode,
transaction_validity_period: BlockHeightDelta,
) -> Self {
let chain_store_update: ChainStoreUpdate<'_> = store.store_update();
let chain_store_update: ChainStoreUpdate<'_> = chain_store.store_update();
Self::new_impl(
epoch_manager,
shard_tracker,
Expand Down
34 changes: 17 additions & 17 deletions chain/chain/src/garbage_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,24 @@ impl Chain {
pub fn clear_data(&mut self, tries: ShardTries, gc_config: &GCConfig) -> Result<(), Error> {
let _span = tracing::debug_span!(target: "garbage_collection", "clear_data").entered();

let head = self.store().head()?;
let tail = self.store().tail()?;
let head = self.chain_store().head()?;
let tail = self.chain_store().tail()?;
let gc_stop_height = self.runtime_adapter.get_gc_stop_height(&head.last_block_hash);
if gc_stop_height > head.height {
return Err(Error::GCError("gc_stop_height cannot be larger than head.height".into()));
}
let prev_epoch_id = self.get_block_header(&head.prev_block_hash)?.epoch_id().clone();
let epoch_change = prev_epoch_id != head.epoch_id;
let mut fork_tail = self.store().fork_tail()?;
let mut fork_tail = self.chain_store().fork_tail()?;
metrics::TAIL_HEIGHT.set(tail as i64);
metrics::FORK_TAIL_HEIGHT.set(fork_tail as i64);
metrics::CHUNK_TAIL_HEIGHT.set(self.store().chunk_tail()? as i64);
metrics::CHUNK_TAIL_HEIGHT.set(self.chain_store().chunk_tail()? as i64);
metrics::GC_STOP_HEIGHT.set(gc_stop_height as i64);
if epoch_change && fork_tail < gc_stop_height {
// if head doesn't change on the epoch boundary, we may update fork tail several times
// but that is fine since it doesn't affect correctness and also we limit the number of
// heights that fork cleaning goes through so it doesn't slow down client either.
let mut chain_store_update = self.mut_store().store_update();
let mut chain_store_update = self.mut_chain_store().store_update();
chain_store_update.update_fork_tail(gc_stop_height);
chain_store_update.commit()?;
fork_tail = gc_stop_height;
Expand All @@ -145,7 +145,7 @@ impl Chain {
if gc_blocks_remaining == 0 {
return Ok(());
}
let mut chain_store_update = self.mut_store().store_update();
let mut chain_store_update = self.mut_chain_store().store_update();
chain_store_update.update_fork_tail(height);
chain_store_update.commit()?;
}
Expand All @@ -156,15 +156,15 @@ impl Chain {
return Ok(());
}
let blocks_current_height = self
.store()
.chain_store()
.get_all_block_hashes_by_height(height)?
.values()
.flatten()
.cloned()
.collect::<Vec<_>>();
let epoch_manager = self.epoch_manager.clone();
let runtime = self.runtime_adapter.clone();
let mut chain_store_update = self.mut_store().store_update();
let mut chain_store_update = self.mut_chain_store().store_update();
if let Some(block_hash) = blocks_current_height.first() {
let prev_hash = *chain_store_update.get_block_header(block_hash)?.prev_hash();
let prev_block_refcount = chain_store_update.get_block_refcount(&prev_hash)?;
Expand Down Expand Up @@ -208,13 +208,13 @@ impl Chain {
pub fn clear_archive_data(&mut self, gc_height_limit: BlockHeightDelta) -> Result<(), Error> {
let _span = tracing::debug_span!(target: "chain", "clear_archive_data").entered();

let head = self.store().head()?;
let head = self.chain_store().head()?;
let gc_stop_height = self.runtime_adapter.get_gc_stop_height(&head.last_block_hash);
if gc_stop_height > head.height {
return Err(Error::GCError("gc_stop_height cannot be larger than head.height".into()));
}

let mut chain_store_update = self.mut_store().store_update();
let mut chain_store_update = self.mut_chain_store().store_update();
chain_store_update.clear_redundant_chunk_data(gc_stop_height, gc_height_limit)?;
metrics::CHUNK_TAIL_HEIGHT.set(chain_store_update.chunk_tail()? as i64);
metrics::GC_STOP_HEIGHT.set(gc_stop_height as i64);
Expand All @@ -228,7 +228,7 @@ impl Chain {
gc_blocks_remaining: &mut NumBlocks,
) -> Result<(), Error> {
let blocks_current_height = self
.store()
.chain_store()
.get_all_block_hashes_by_height(height)?
.values()
.flatten()
Expand All @@ -245,7 +245,7 @@ impl Chain {
// and it may be safely deleted
// and all its ancestors while there are no other sibling blocks rely on it.
let epoch_manager = self.epoch_manager.clone();
let mut chain_store_update = self.mut_store().store_update();
let mut chain_store_update = self.mut_chain_store().store_update();
if chain_store_update.get_block_refcount(&current_hash)? == 0 {
let prev_hash =
*chain_store_update.get_block_header(&current_hash)?.prev_hash();
Expand Down Expand Up @@ -281,19 +281,19 @@ impl Chain {

// GC all the data from current tail up to `gc_height`. In case tail points to a height where
// there is no block, we need to make sure that the last block before tail is cleaned.
let tail = self.store().tail()?;
let tail = self.chain_store().tail()?;
let mut tail_prev_block_cleaned = false;
for height in tail..gc_height {
let blocks_current_height = self
.store()
.chain_store()
.get_all_block_hashes_by_height(height)?
.values()
.flatten()
.cloned()
.collect::<Vec<_>>();
for block_hash in blocks_current_height {
let epoch_manager = self.epoch_manager.clone();
let mut chain_store_update = self.mut_store().store_update();
let mut chain_store_update = self.mut_chain_store().store_update();
if !tail_prev_block_cleaned {
let prev_block_hash =
*chain_store_update.get_block_header(&block_hash)?.prev_hash();
Expand All @@ -316,7 +316,7 @@ impl Chain {
}

// Clear Chunks data
let mut chain_store_update = self.mut_store().store_update();
let mut chain_store_update = self.mut_chain_store().store_update();
// The largest height of chunk we have in storage is head.height + 1
let chunk_height = std::cmp::min(head.height + 2, sync_height);
chain_store_update.clear_chunk_data_and_headers(chunk_height)?;
Expand All @@ -325,7 +325,7 @@ impl Chain {
// clear all trie data

let tries = self.runtime_adapter.get_tries();
let mut chain_store_update = self.mut_store().store_update();
let mut chain_store_update = self.mut_chain_store().store_update();
let mut store_update = tries.store_update();
store_update.delete_all(DBCol::State);
chain_store_update.merge(store_update);
Expand Down
2 changes: 1 addition & 1 deletion chain/chain/src/resharding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ impl Chain {
let child_shard_uids = state_roots.keys().cloned().collect_vec();
self.initialize_flat_storage(&prev_hash, &child_shard_uids)?;

let mut chain_store_update = self.mut_store().store_update();
let mut chain_store_update = self.mut_chain_store().store_update();
for (shard_uid, state_root) in state_roots {
// here we store the state roots in chunk_extra in the database for later use
let chunk_extra = ChunkExtra::new_with_only_state_root(&state_root);
Expand Down
38 changes: 19 additions & 19 deletions chain/chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2749,13 +2749,13 @@ mod tests {
let genesis = chain.get_block_by_height(0).unwrap();
let signer = Arc::new(create_test_signer("test1"));
let short_fork = vec![TestBlockBuilder::new(&genesis, signer.clone()).build()];
let mut store_update = chain.mut_store().store_update();
let mut store_update = chain.mut_chain_store().store_update();
store_update.save_block_header(short_fork[0].header().clone()).unwrap();
store_update.commit().unwrap();

let short_fork_head = short_fork[0].header().clone();
assert!(chain
.mut_store()
.mut_chain_store()
.check_transaction_validity_period(
&short_fork_head,
genesis.hash(),
Expand All @@ -2765,7 +2765,7 @@ mod tests {
let mut long_fork = vec![];
let mut prev_block = genesis;
for i in 1..(transaction_validity_period + 3) {
let mut store_update = chain.mut_store().store_update();
let mut store_update = chain.mut_chain_store().store_update();
let block = TestBlockBuilder::new(&prev_block, signer.clone()).height(i).build();
prev_block = block.clone();
store_update.save_block_header(block.header().clone()).unwrap();
Expand All @@ -2778,7 +2778,7 @@ mod tests {
let valid_base_hash = long_fork[1].hash();
let cur_header = &long_fork.last().unwrap().header();
assert!(chain
.mut_store()
.mut_chain_store()
.check_transaction_validity_period(
cur_header,
valid_base_hash,
Expand All @@ -2787,7 +2787,7 @@ mod tests {
.is_ok());
let invalid_base_hash = long_fork[0].hash();
assert_eq!(
chain.mut_store().check_transaction_validity_period(
chain.mut_chain_store().check_transaction_validity_period(
cur_header,
invalid_base_hash,
transaction_validity_period
Expand All @@ -2805,7 +2805,7 @@ mod tests {
let mut blocks = vec![];
let mut prev_block = genesis;
for i in 1..(transaction_validity_period + 2) {
let mut store_update = chain.mut_store().store_update();
let mut store_update = chain.mut_chain_store().store_update();
let block = TestBlockBuilder::new(&prev_block, signer.clone()).height(i).build();
prev_block = block.clone();
store_update.save_block_header(block.header().clone()).unwrap();
Expand All @@ -2818,7 +2818,7 @@ mod tests {
let valid_base_hash = blocks[1].hash();
let cur_header = &blocks.last().unwrap().header();
assert!(chain
.mut_store()
.mut_chain_store()
.check_transaction_validity_period(
cur_header,
valid_base_hash,
Expand All @@ -2829,14 +2829,14 @@ mod tests {
.height(transaction_validity_period + 3)
.build();

let mut store_update = chain.mut_store().store_update();
let mut store_update = chain.mut_chain_store().store_update();
store_update.save_block_header(new_block.header().clone()).unwrap();
store_update
.update_height_if_not_challenged(new_block.header().height(), *new_block.hash())
.unwrap();
store_update.commit().unwrap();
assert_eq!(
chain.mut_store().check_transaction_validity_period(
chain.mut_chain_store().check_transaction_validity_period(
new_block.header(),
valid_base_hash,
transaction_validity_period
Expand All @@ -2855,7 +2855,7 @@ mod tests {
let mut short_fork = vec![];
let mut prev_block = genesis.clone();
for i in 1..(transaction_validity_period + 2) {
let mut store_update = chain.mut_store().store_update();
let mut store_update = chain.mut_chain_store().store_update();
let block = TestBlockBuilder::new(&prev_block, signer.clone()).height(i).build();
prev_block = block.clone();
store_update.save_block_header(block.header().clone()).unwrap();
Expand All @@ -2865,7 +2865,7 @@ mod tests {

let short_fork_head = short_fork.last().unwrap().header().clone();
assert_eq!(
chain.mut_store().check_transaction_validity_period(
chain.mut_chain_store().check_transaction_validity_period(
&short_fork_head,
&genesis_hash,
transaction_validity_period
Expand All @@ -2875,7 +2875,7 @@ mod tests {
let mut long_fork = vec![];
let mut prev_block = genesis;
for i in 1..(transaction_validity_period * 5) {
let mut store_update = chain.mut_store().store_update();
let mut store_update = chain.mut_chain_store().store_update();
let block = TestBlockBuilder::new(&prev_block, signer.clone()).height(i).build();
prev_block = block.clone();
store_update.save_block_header(block.header().clone()).unwrap();
Expand All @@ -2884,7 +2884,7 @@ mod tests {
}
let long_fork_head = &long_fork.last().unwrap().header();
assert_eq!(
chain.mut_store().check_transaction_validity_period(
chain.mut_chain_store().check_transaction_validity_period(
long_fork_head,
&genesis_hash,
transaction_validity_period
Expand All @@ -2903,29 +2903,29 @@ mod tests {
block2.mut_header().get_mut().inner_lite.epoch_id = EpochId(hash(&[1, 2, 3]));
block2.mut_header().resign(&*signer);

let mut store_update = chain.mut_store().store_update();
let mut store_update = chain.mut_chain_store().store_update();
store_update.chain_store_cache_update.height_to_hashes.insert(1, Some(hash(&[1])));
store_update
.chain_store_cache_update
.blocks
.insert(*block1.header().hash(), block1.clone());
store_update.commit().unwrap();

let block_hash = chain.mut_store().height.get(&index_to_bytes(1).to_vec());
let block_hash = chain.mut_chain_store().height.get(&index_to_bytes(1).to_vec());
let epoch_id_to_hash =
chain.mut_store().block_hash_per_height.get(&index_to_bytes(1).to_vec());
chain.mut_chain_store().block_hash_per_height.get(&index_to_bytes(1).to_vec());

let mut store_update = chain.mut_store().store_update();
let mut store_update = chain.mut_chain_store().store_update();
store_update.chain_store_cache_update.height_to_hashes.insert(1, Some(hash(&[2])));
store_update
.chain_store_cache_update
.blocks
.insert(*block2.header().hash(), block2.clone());
store_update.commit().unwrap();

let block_hash1 = chain.mut_store().height.get(&index_to_bytes(1).to_vec());
let block_hash1 = chain.mut_chain_store().height.get(&index_to_bytes(1).to_vec());
let epoch_id_to_hash1 =
chain.mut_store().block_hash_per_height.get(&index_to_bytes(1).to_vec());
chain.mut_chain_store().block_hash_per_height.get(&index_to_bytes(1).to_vec());

assert_ne!(block_hash, block_hash1);
assert_ne!(epoch_id_to_hash, epoch_id_to_hash1);
Expand Down
4 changes: 2 additions & 2 deletions chain/chain/src/store_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ mod tests {
#[test]
fn test_io_error() {
let (chain, mut sv) = init();
let mut store_update = chain.store().store().store_update();
let mut store_update = chain.chain_store().store().store_update();
assert!(sv.validate_col(DBCol::Block).is_ok());
// Use `set_raw` to ruthlessly override block data with some garbage,
// simulating IO error.
Expand All @@ -439,7 +439,7 @@ mod tests {
#[test]
fn test_db_corruption() {
let (chain, mut sv) = init();
let mut store_update = chain.store().store().store_update();
let mut store_update = chain.chain_store().store().store_update();
assert!(sv.validate_col(DBCol::TrieChanges).is_ok());
store_update.set_ser::<[u8]>(DBCol::TrieChanges, "567".as_ref(), &[123]).unwrap();
store_update.commit().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub fn format_hash(hash: CryptoHash) -> String {
/// Displays chain from given store.
pub fn display_chain(me: &Option<AccountId>, chain: &mut Chain, tail: bool) {
let epoch_manager = chain.epoch_manager.clone();
let chain_store = chain.mut_store();
let chain_store = chain.mut_chain_store();
let head = chain_store.head().unwrap();
debug!(
"{:?} Chain head ({}): {} / {}",
Expand Down
Loading

0 comments on commit a72ab6c

Please sign in to comment.