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

fix: block apply in the past #11767

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@ impl RuntimeStorageConfig {
state_patch: Default::default(),
}
}

/// Creates a [RuntimeStorageConfig] with [StorageDataSource::DbTrieOnly].
/// Flat storage is disabled because it is implied to be missing.
///
/// This's meant to be used only to replay blocks.
pub fn with_db_trie_only(state_root: StateRoot) -> Self {
Trisfald marked this conversation as resolved.
Show resolved Hide resolved
Self {
state_root,
use_flat_storage: false,
source: StorageDataSource::DbTrieOnly,
state_patch: Default::default(),
}
}
}

#[derive(Clone)]
Expand Down
41 changes: 11 additions & 30 deletions tools/state-viewer/src/apply_chain_range.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::cli::ApplyRangeMode;
use crate::cli::{ApplyRangeMode, StorageSource};
use near_chain::chain::collect_receipts_from_response;
use near_chain::migrations::check_if_block_is_first_with_chunk_of_version;
use near_chain::types::{
ApplyChunkBlockContext, ApplyChunkResult, ApplyChunkShardContext, RuntimeAdapter,
RuntimeStorageConfig, StorageDataSource,
};
use near_chain::{ChainStore, ChainStoreAccess, ChainStoreUpdate};
use near_chain_configs::Genesis;
Expand Down Expand Up @@ -125,8 +124,7 @@ fn apply_block_from_range(
verbose_output: bool,
csv_file_mutex: &Mutex<Option<&mut File>>,
only_contracts: bool,
use_flat_storage: bool,
use_trie_for_free: bool,
storage: StorageSource,
) {
// normally save_trie_changes depends on whether the node is
// archival, but here we don't care, and can just set it to false
Expand Down Expand Up @@ -234,15 +232,9 @@ fn apply_block_from_range(
}
}

let mut storage =
RuntimeStorageConfig::new(*chunk_inner.prev_state_root(), use_flat_storage);
if use_trie_for_free {
storage.source = StorageDataSource::DbTrieOnly;
}

runtime_adapter
.apply_chunk(
storage,
storage.create_runtime_storage(*chunk_inner.prev_state_root()),
ApplyChunkReason::UpdateTrackedShard,
ApplyChunkShardContext {
shard_id,
Expand All @@ -266,14 +258,9 @@ fn apply_block_from_range(
chain_store.get_chunk_extra(block.header().prev_hash(), &shard_uid).unwrap();
prev_chunk_extra = Some(chunk_extra.clone());

let mut storage = RuntimeStorageConfig::new(*chunk_extra.state_root(), use_flat_storage);
if use_trie_for_free {
storage.source = StorageDataSource::DbTrieOnly;
}

runtime_adapter
.apply_chunk(
storage,
storage.create_runtime_storage(*chunk_extra.state_root()),
ApplyChunkReason::UpdateTrackedShard,
ApplyChunkShardContext {
shard_id,
Expand Down Expand Up @@ -389,8 +376,7 @@ pub fn apply_chain_range(
verbose_output: bool,
csv_file: Option<&mut File>,
only_contracts: bool,
use_flat_storage: bool,
use_trie_for_free: bool,
storage: StorageSource,
) {
let parent_span = tracing::debug_span!(
target: "state_viewer",
Expand All @@ -400,16 +386,14 @@ pub fn apply_chain_range(
?end_height,
%shard_id,
only_contracts,
use_flat_storage,
use_trie_for_free)
?storage)
.entered();
let chain_store = ChainStore::new(store.clone(), genesis.config.genesis_height, false);
let (start_height, end_height) = match mode {
ApplyRangeMode::Benchmarking => {
// Benchmarking mode requires flat storage and retrieves start and
// end heights from flat storage and chain.
assert!(use_flat_storage);
assert!(!use_trie_for_free);
assert!(matches!(storage, StorageSource::FlatStorage));
assert!(start_height.is_none());
assert!(end_height.is_none());

Expand Down Expand Up @@ -473,8 +457,7 @@ pub fn apply_chain_range(
verbose_output,
&csv_file_mutex,
only_contracts,
use_flat_storage,
use_trie_for_free,
storage,
);
};

Expand Down Expand Up @@ -559,7 +542,7 @@ mod test {
use nearcore::NightshadeRuntime;

use crate::apply_chain_range::apply_chain_range;
use crate::cli::ApplyRangeMode;
use crate::cli::{ApplyRangeMode, StorageSource};

fn setup(epoch_length: NumBlocks) -> (Store, Genesis, TestEnv) {
let mut genesis =
Expand Down Expand Up @@ -658,8 +641,7 @@ mod test {
true,
None,
false,
false,
false,
StorageSource::Trie,
);
}

Expand Down Expand Up @@ -703,8 +685,7 @@ mod test {
true,
Some(file.as_file_mut()),
false,
false,
false,
StorageSource::Trie,
);
let mut csv = String::new();
file.as_file_mut().seek(SeekFrom::Start(0)).unwrap();
Expand Down
Loading
Loading