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: create FlatStorageState inside RuntimeAdapter #7710

Merged
merged 8 commits into from
Sep 28, 2022
Merged
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
6 changes: 2 additions & 4 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ use near_primitives::shard_layout::{
};
use near_primitives::version::PROTOCOL_VERSION;
#[cfg(feature = "protocol_feature_flat_state")]
use near_store::flat_state::{FlatStateDelta, FlatStorageState};
use near_store::flat_state::FlatStateDelta;
use once_cell::sync::OnceCell;
use rayon::iter::{IntoParallelIterator, ParallelIterator};

Expand Down Expand Up @@ -629,13 +629,11 @@ impl Chain {
// set up flat storage
#[cfg(feature = "protocol_feature_flat_state")]
for shard_id in 0..runtime_adapter.num_shards(&block_head.epoch_id)? {
let flat_storage_state = FlatStorageState::new(
store.store().clone(),
runtime_adapter.create_flat_storage_state_for_shard(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe remove the feature flag here and add another implementation of create_flat_storage_state_for_shard when the feature is not enabled? I think this creates less code that's gated by the feature.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't help much, because ChainAccessForFlatStorage needs to be gated.

shard_id,
store.head()?.height,
&store,
);
runtime_adapter.add_flat_storage_state_for_shard(shard_id, flat_storage_state);
}

info!(target: "chain", "Init: header head @ #{} {}; block head @ #{} {}",
Expand Down
9 changes: 7 additions & 2 deletions chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ use crate::{BlockProcessingArtifact, Provenance};
use near_primitives::epoch_manager::ShardConfig;
use near_primitives::time::Clock;
use near_primitives::utils::MaybeValidated;
#[cfg(feature = "protocol_feature_flat_state")]
use near_store::flat_state::ChainAccessForFlatStorage;
use near_store::flat_state::FlatStorageState;

pub use self::validator_schedule::ValidatorSchedule;
Expand Down Expand Up @@ -709,10 +711,13 @@ impl RuntimeAdapter for KeyValueRuntime {
fn get_flat_storage_state_for_shard(&self, _shard_id: ShardId) -> Option<FlatStorageState> {
None
}
fn add_flat_storage_state_for_shard(

#[cfg(feature = "protocol_feature_flat_state")]
fn create_flat_storage_state_for_shard(
&self,
_shard_id: ShardId,
_flat_storage_state: FlatStorageState,
_latest_block_height: BlockHeight,
_chain_access: &dyn ChainAccessForFlatStorage,
) {
}

Expand Down
8 changes: 6 additions & 2 deletions chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ use near_primitives::version::{
MIN_PROTOCOL_VERSION_NEP_92_FIX,
};
use near_primitives::views::{EpochValidatorInfo, QueryRequest, QueryResponse};
#[cfg(feature = "protocol_feature_flat_state")]
use near_store::flat_state::ChainAccessForFlatStorage;
use near_store::flat_state::FlatStorageState;
use near_store::{PartialStorage, ShardTries, Store, StoreUpdate, Trie, WrappedTrieChanges};

Expand Down Expand Up @@ -286,10 +288,12 @@ pub trait RuntimeAdapter: EpochManagerAdapter + Send + Sync {

fn get_flat_storage_state_for_shard(&self, shard_id: ShardId) -> Option<FlatStorageState>;

fn add_flat_storage_state_for_shard(
#[cfg(feature = "protocol_feature_flat_state")]
fn create_flat_storage_state_for_shard(
&self,
shard_id: ShardId,
flat_storage_state: FlatStorageState,
latest_block_height: BlockHeight,
chain_access: &dyn ChainAccessForFlatStorage,
);

fn set_flat_storage_state_for_genesis(
Expand Down
2 changes: 1 addition & 1 deletion core/store/src/flat_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ mod imp {
}))
}

/// When a node starts from an empty database, this function must be called to ensure
/// When a node starts from an empty database, this function must be called to ensure
/// information such as flat head is set up correctly in the database.
/// Note that this function is different from `add_flat_storage_state_for_shard`,
/// it must be called before `add_flat_storage_state_for_shard` if the node starts from
Expand Down
10 changes: 8 additions & 2 deletions nearcore/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ use near_primitives::views::{
AccessKeyInfoView, CallResult, EpochValidatorInfo, QueryRequest, QueryResponse,
QueryResponseKind, ViewApplyState, ViewStateResult,
};
#[cfg(feature = "protocol_feature_flat_state")]
use near_store::flat_state::ChainAccessForFlatStorage;
use near_store::flat_state::{FlatStateFactory, FlatStorageState};
use near_store::split_state::get_delayed_receipts;
use near_store::{
Expand Down Expand Up @@ -699,11 +701,15 @@ impl RuntimeAdapter for NightshadeRuntime {
self.flat_state_factory.get_flat_storage_state_for_shard(shard_id)
}

fn add_flat_storage_state_for_shard(
#[cfg(feature = "protocol_feature_flat_state")]
fn create_flat_storage_state_for_shard(
&self,
shard_id: ShardId,
flat_storage_state: FlatStorageState,
latest_block_height: BlockHeight,
chain_access: &dyn ChainAccessForFlatStorage,
) {
let flat_storage_state =
FlatStorageState::new(self.store.clone(), shard_id, latest_block_height, chain_access);
self.flat_state_factory.add_flat_storage_state_for_shard(shard_id, flat_storage_state)
}

Expand Down