Skip to content

Commit

Permalink
fix: create FlatStorageState inside RuntimeAdapter (#7710)
Browse files Browse the repository at this point in the history
We create FlatStorageState on Chain creation, but it currently happens for any RuntimeAdapter impl. Because KeyValueRuntime doesn't and shouldn't support flat storage, it leads to tests failures.

Here we move FSS creation inside RuntimeAdapter impl.

## Testing

https://buildkite.com/nearprotocol/nearcore-flat-state/builds/60#018383c5-7838-4e5b-86f1-da2baa381789 - number of failing tests drops from 77 to 4 - which are failing due to other reasons
  • Loading branch information
Longarithm committed Sep 28, 2022
1 parent c6f0059 commit c72bc7e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
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(
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

0 comments on commit c72bc7e

Please sign in to comment.