Skip to content

Commit f24decd

Browse files
authoredJan 31, 2025··
refactor: extract state sync logic from Chain (#12833)
Main result: number of LoC in `chain/chain/src/chain.rs` goes from 4690 to 4125. Please let me know if you want me to split this in multiple PRs. I can do this but I'm very tempted to just go forward because there are no functional changes... ### Biggest part (568+ lines) It is moving state sync-related methods from `chain/chain/src/chain.rs` to `chain/chain/src/state_sync/adapter.rs`. This is achieved by creating `ChainStateSyncAdapter` which needs access to much less data than `Chain`. ### Another change (156+ lines) To achieve the previous one, I wanted to read chain data from `ChainStoreAdapter`, not `ChainStore`. We are currently refactoring `ChainStore` and I didn't want to create new instances of it. We can allow read-only access to data which already exist in storage. For this, in turn, I needed to make `get_chunk_clone_from_header`, `get_block_header_on_chain_by_height` and `get_incoming_receipts_for_shard` require only `ChainStoreAdapter` access. I was able to move these to `chain/chain/src/store/utils.rs` without changing logic. Another bit of context: these methods previously required `ChainStoreAccess` in order to be queried by `ChainStoreUpdate`. But as @shreyan-gupta discovered, we don't actually need that, at least in almost all places of the code. So it was natural to me to simplify access to these functions already. ### Bonus It was very easy to move `get_state_sync_info` to `ShardTracker` and `StateRequestTracker` to new state sync folder - a nice example of hiding implementation details.
1 parent 6cd375e commit f24decd

26 files changed

+988
-843
lines changed
 

‎chain/chain/src/chain.rs

+36-601
Large diffs are not rendered by default.

‎chain/chain/src/chain_update.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::approval_verification::verify_approvals_and_threshold_orphan;
22
use crate::block_processing_utils::BlockPreprocessInfo;
33
use crate::chain::collect_receipts_from_response;
44
use crate::metrics::{SHARD_LAYOUT_NUM_SHARDS, SHARD_LAYOUT_VERSION};
5+
use crate::store::utils::get_block_header_on_chain_by_height;
56
use crate::store::{ChainStore, ChainStoreAccess, ChainStoreUpdate};
67
use crate::types::{
78
ApplyChunkBlockContext, ApplyChunkResult, ApplyChunkShardContext, RuntimeAdapter,
@@ -485,9 +486,13 @@ impl<'a> ChainUpdate<'a> {
485486
}
486487
};
487488

488-
let block_header = self
489-
.chain_store_update
490-
.get_block_header_on_chain_by_height(&sync_hash, chunk.height_included())?;
489+
// Note that block headers are already synced and can be taken
490+
// from store on disk.
491+
let block_header = get_block_header_on_chain_by_height(
492+
&self.chain_store_update.chain_store(),
493+
&sync_hash,
494+
chunk.height_included(),
495+
)?;
491496

492497
// Getting actual incoming receipts.
493498
let mut receipt_proof_responses: Vec<ReceiptProofResponse> = vec![];
@@ -625,8 +630,13 @@ impl<'a> ChainUpdate<'a> {
625630
let _span =
626631
tracing::debug_span!(target: "sync", "set_state_finalize_on_height", height, ?shard_id)
627632
.entered();
628-
let block_header_result =
629-
self.chain_store_update.get_block_header_on_chain_by_height(&sync_hash, height);
633+
// Note that block headers are already synced and can be taken
634+
// from store on disk.
635+
let block_header_result = get_block_header_on_chain_by_height(
636+
&self.chain_store_update.chain_store(),
637+
&sync_hash,
638+
height,
639+
);
630640
if let Err(_) = block_header_result {
631641
// No such height, go ahead.
632642
return Ok(true);

‎chain/chain/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub use doomslug::{Doomslug, DoomslugBlockProductionReadiness, DoomslugThreshold
77
pub use lightclient::{create_light_client_block_view, get_epoch_block_producers_view};
88
pub use near_chain_primitives::{self, Error};
99
pub use near_primitives::receipt::ReceiptResult;
10+
pub use store::utils::{get_chunk_clone_from_header, get_incoming_receipts_for_shard};
1011
pub use store::{
1112
ChainStore, ChainStoreAccess, ChainStoreUpdate, LatestWitnessesInfo, MerkleProofAccess,
1213
ReceiptFilter,
@@ -32,7 +33,6 @@ pub mod orphan;
3233
pub mod resharding;
3334
pub mod runtime;
3435
pub mod signature_verification;
35-
mod state_request_tracker;
3636
pub mod state_snapshot_actor;
3737
mod state_sync;
3838
pub mod stateless_validation;

0 commit comments

Comments
 (0)
Please sign in to comment.