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

[store] ChainStoreUpdate remove dependency on reading tentatively updated values #12162

Draft
wants to merge 14 commits into
base: master
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
16 changes: 16 additions & 0 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,22 @@ impl Chain {
should_save_state_transition_data,
)?;
chain_update.commit()?;

// On the epoch switch record the epoch light client block
// Note that we only do it if `new_head.is_some()`, i.e. if the current block is the head.
// This is necessary because the computation of the light client block relies on
// `ColNextBlockHash`-es populated, and they are only populated for the canonical
// chain. We need to be careful to avoid a situation when the first block of the epoch
// never becomes a tip of the canonical chain.
// Presently the epoch boundary is defined by the height, and the fork choice rule
// is also just height, so the very first block to cross the epoch end is guaranteed
// to be the head of the chain, and result in the light client block produced.
if new_head.is_some() {
let mut chain_update = self.chain_update();
chain_update.save_light_client_block(block.header())?;
chain_update.commit()?;
}

Ok(new_head)
}

Expand Down
40 changes: 16 additions & 24 deletions chain/chain/src/chain_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,35 +277,27 @@ impl<'a> ChainUpdate<'a> {

// Update the chain head if it's the new tip
let res = self.update_head(block.header())?;
Ok(res)
}

if res.is_some() {
// On the epoch switch record the epoch light client block
// Note that we only do it if `res.is_some()`, i.e. if the current block is the head.
// This is necessary because the computation of the light client block relies on
// `ColNextBlockHash`-es populated, and they are only populated for the canonical
// chain. We need to be careful to avoid a situation when the first block of the epoch
// never becomes a tip of the canonical chain.
// Presently the epoch boundary is defined by the height, and the fork choice rule
// is also just height, so the very first block to cross the epoch end is guaranteed
// to be the head of the chain, and result in the light client block produced.
let prev = self.chain_store_update.get_previous_header(block.header())?;
let prev_epoch_id = *prev.epoch_id();
if block.header().epoch_id() != &prev_epoch_id {
if prev.last_final_block() != &CryptoHash::default() {
let light_client_block = self.create_light_client_block(&prev)?;
self.chain_store_update
.save_epoch_light_client_block(&prev_epoch_id.0, light_client_block);
}
pub fn save_light_client_block(&mut self, header: &BlockHeader) -> Result<(), Error> {
let prev = self.chain_store_update.get_previous_header(header)?;
let prev_epoch_id = *prev.epoch_id();
if header.epoch_id() != &prev_epoch_id {
if prev.last_final_block() != &CryptoHash::default() {
let light_client_block = self.create_light_client_block(&prev)?;
self.chain_store_update
.save_epoch_light_client_block(&prev_epoch_id.0, light_client_block);
}

let shard_layout = self.epoch_manager.get_shard_layout_from_prev_block(prev.hash())?;
SHARD_LAYOUT_VERSION.set(shard_layout.version() as i64);
SHARD_LAYOUT_NUM_SHARDS.set(shard_layout.shard_ids().count() as i64);
}
Ok(res)

let shard_layout = self.epoch_manager.get_shard_layout_from_prev_block(prev.hash())?;
SHARD_LAYOUT_VERSION.set(shard_layout.version() as i64);
SHARD_LAYOUT_NUM_SHARDS.set(shard_layout.shard_ids().count() as i64);
Ok(())
}

pub fn create_light_client_block(
fn create_light_client_block(
&mut self,
header: &BlockHeader,
) -> Result<LightClientBlockView, Error> {
Expand Down
5 changes: 3 additions & 2 deletions chain/chain/src/garbage_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,10 @@ impl ChainStore {

let mut chain_store_update = self.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);
let res = chain_store_update.commit();
metrics::CHUNK_TAIL_HEIGHT.set(self.chunk_tail()? as i64);
metrics::GC_STOP_HEIGHT.set(gc_stop_height as i64);
chain_store_update.commit()
res
}

fn clear_forks_data(
Expand Down
Loading
Loading