Skip to content

Commit

Permalink
fix(state-sync): Lookup fewer BlockInfos during state sync finalizati…
Browse files Browse the repository at this point in the history
…on (#9324)

Also apply state parts and finalize state sync using state-viewer
  • Loading branch information
nikurt authored Jul 20, 2023
1 parent 6f0b319 commit a77e109
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
10 changes: 4 additions & 6 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5479,12 +5479,10 @@ impl<'a> ChainUpdate<'a> {

let chunk_header = chunk.cloned_header();
let gas_limit = chunk_header.gas_limit();
let is_first_block_with_chunk_of_version = check_if_block_is_first_with_chunk_of_version(
&mut self.chain_store_update,
self.epoch_manager.as_ref(),
&chunk_header.prev_block_hash(),
shard_id,
)?;
// This is set to false because the value is only relevant
// during protocol version RestoreReceiptsAfterFixApplyChunks.
// TODO(nikurt): Determine the value correctly.
let is_first_block_with_chunk_of_version = false;

let apply_result = self.runtime_adapter.apply_transactions(
shard_id,
Expand Down
43 changes: 32 additions & 11 deletions tools/state-viewer/src/state_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ pub(crate) enum StatePartsSubCommand {
/// Use if those headers or blocks are not available.
#[clap(long)]
state_root: Option<StateRoot>,
/// If provided, this value will be used instead of looking it up in the headers.
/// Use if those headers or blocks are not available.
#[clap(long)]
sync_hash: Option<CryptoHash>,
/// Choose a single part id.
/// If None - affects all state parts.
#[clap(long)]
Expand All @@ -69,6 +73,13 @@ pub(crate) enum StatePartsSubCommand {
#[clap(subcommand)]
epoch_selection: EpochSelection,
},
/// Finalize state sync.
Finalize {
/// If provided, this value will be used instead of looking it up in the headers.
/// Use if those headers or blocks are not available.
#[clap(long)]
sync_hash: CryptoHash,
},
}

impl StatePartsSubCommand {
Expand Down Expand Up @@ -110,7 +121,13 @@ impl StatePartsSubCommand {
let credentials_file =
near_config.config.s3_credentials_file.clone().map(|file| home_dir.join(file));
match self {
StatePartsSubCommand::Load { action, state_root, part_id, epoch_selection } => {
StatePartsSubCommand::Load {
action,
state_root,
sync_hash,
part_id,
epoch_selection,
} => {
let external = create_external_connection(
root_dir,
s3_bucket,
Expand All @@ -124,6 +141,7 @@ impl StatePartsSubCommand {
shard_id,
part_id,
state_root,
sync_hash,
&mut chain,
chain_id,
store,
Expand Down Expand Up @@ -154,6 +172,9 @@ impl StatePartsSubCommand {
StatePartsSubCommand::ReadStateHeader { epoch_selection } => {
read_state_header(epoch_selection, shard_id, &chain, store)
}
StatePartsSubCommand::Finalize { sync_hash } => {
finalize_state_sync(sync_hash, shard_id, &mut chain)
}
}
});
sys.run().unwrap();
Expand Down Expand Up @@ -271,17 +292,18 @@ async fn load_state_parts(
shard_id: ShardId,
part_id: Option<u64>,
maybe_state_root: Option<StateRoot>,
maybe_sync_hash: Option<CryptoHash>,
chain: &mut Chain,
chain_id: &str,
store: Store,
external: &ExternalConnection,
) {
let epoch_id = epoch_selection.to_epoch_id(store, chain);
let (state_root, epoch_height, epoch_id, sync_hash) =
if let (Some(state_root), EpochSelection::EpochHeight { epoch_height }) =
(maybe_state_root, &epoch_selection)
if let (Some(state_root), Some(sync_hash), EpochSelection::EpochHeight { epoch_height }) =
(maybe_state_root, maybe_sync_hash, &epoch_selection)
{
(state_root, *epoch_height, epoch_id, None)
(state_root, *epoch_height, epoch_id, sync_hash)
} else {
let epoch = chain.epoch_manager.get_epoch_info(&epoch_id).unwrap();

Expand All @@ -291,7 +313,7 @@ async fn load_state_parts(
let state_header = chain.get_state_response_header(shard_id, sync_hash).unwrap();
let state_root = state_header.chunk_prev_state_root();

(state_root, epoch.epoch_height(), epoch_id, Some(sync_hash))
(state_root, epoch.epoch_height(), epoch_id, sync_hash)
};

let directory_path =
Expand Down Expand Up @@ -328,12 +350,7 @@ async fn load_state_parts(
match action {
LoadAction::Apply => {
chain
.set_state_part(
shard_id,
sync_hash.unwrap(),
PartId::new(part_id, num_parts),
&part,
)
.set_state_part(shard_id, sync_hash, PartId::new(part_id, num_parts), &part)
.unwrap();
chain
.runtime_adapter
Expand Down Expand Up @@ -471,6 +488,10 @@ fn read_state_header(
tracing::info!(target: "state-parts", ?epoch_id, ?sync_hash, ?state_header);
}

fn finalize_state_sync(sync_hash: CryptoHash, shard_id: ShardId, chain: &mut Chain) {
chain.set_state_finalize(shard_id, sync_hash, Ok(())).unwrap()
}

fn get_part_ids(part_from: Option<u64>, part_to: Option<u64>, num_parts: u64) -> Range<u64> {
part_from.unwrap_or(0)..part_to.unwrap_or(num_parts)
}

0 comments on commit a77e109

Please sign in to comment.