From a6cab5d0113b85563e26da2cd6a24f3ad0e5eb28 Mon Sep 17 00:00:00 2001 From: Michal Nazarewicz Date: Fri, 10 Jun 2022 18:44:51 +0200 Subject: [PATCH] store: refactor StoreOpener; add Store::opener and related changes (#7015) Firstly, replace StoreOpener constructors with static methods on the Store struct. Specifically, add Store::opener (which acts just like StoreOpener::new which is now private) and Store::tmp_opener (which replaces StoreOpener::with_default_config). Secondly, incorporate creation of the temporary directory into the Store::tmp_opener method. Previously, test code would create a temporary directory and use StoreOpener::with_default_config. Now, this is all rolled into single Store::tmp_opener method. The motivation for that is to make it painfully obvious that Store::tmp_opener is meant for tests only. Anything that operates on a non-temporary storage will have to use Store::opener and provide it with a StoreConfig. Lastly, change StoreOpener to resolve the path to the storage during construction. This replaces `home_dir` that the struct was holding with a `path: PathBuf`. This means that StoreOpener::get_path will no longer resolve the path on each call and have its return value ready to go. Issue: https://github.com/near/nearcore/issues/6857 --- chain/indexer/src/streamer/mod.rs | 6 +- .../src/peer_manager/peer_store_test.rs | 37 +++++----- core/store/Cargo.toml | 2 +- core/store/benches/store_bench.rs | 6 +- core/store/src/config.rs | 73 ++++++++----------- core/store/src/db.rs | 14 ++-- core/store/src/lib.rs | 18 +++++ genesis-tools/genesis-populate/src/main.rs | 2 +- .../genesis-populate/src/state_dump.rs | 3 +- nearcore/benches/store.rs | 2 +- nearcore/src/lib.rs | 6 +- nearcore/src/runtime/mod.rs | 71 ++++++------------ runtime/runtime-params-estimator/src/main.rs | 3 +- test-utils/runtime-tester/src/run_test.rs | 6 +- test-utils/store-validator/src/main.rs | 2 +- tools/mock_node/src/setup.rs | 2 +- tools/state-viewer/src/cli.rs | 2 +- 17 files changed, 116 insertions(+), 139 deletions(-) diff --git a/chain/indexer/src/streamer/mod.rs b/chain/indexer/src/streamer/mod.rs index d7aa33886e5..2beb2844a89 100644 --- a/chain/indexer/src/streamer/mod.rs +++ b/chain/indexer/src/streamer/mod.rs @@ -287,9 +287,9 @@ pub(crate) async fn start( blocks_sink: mpsc::Sender, ) { info!(target: INDEXER, "Starting Streamer..."); - let mut indexer_db_path = - near_store::StoreOpener::new(&indexer_config.home_dir, &store_config).get_path(); - indexer_db_path.push("indexer"); + let indexer_db_path = near_store::Store::opener(&indexer_config.home_dir, &store_config) + .get_path() + .join("indexer"); // TODO: implement proper error handling let db = DB::open_default(indexer_db_path).unwrap(); diff --git a/chain/network/src/peer_manager/peer_store_test.rs b/chain/network/src/peer_manager/peer_store_test.rs index e8741570d58..c8e8ef9ee9e 100644 --- a/chain/network/src/peer_manager/peer_store_test.rs +++ b/chain/network/src/peer_manager/peer_store_test.rs @@ -1,7 +1,7 @@ use near_crypto::{KeyType, SecretKey}; use near_network_primitives::types::{Blacklist, BlacklistEntry}; use near_store::test_utils::create_test_store; -use near_store::StoreOpener; +use near_store::{Store, StoreOpener}; use std::collections::HashSet; use std::net::{Ipv4Addr, SocketAddrV4}; @@ -29,19 +29,19 @@ fn gen_peer_info(port: u16) -> PeerInfo { #[test] fn ban_store() { - let tmp_dir = tempfile::Builder::new().prefix("_test_store_ban").tempdir().unwrap(); + let (_tmp_dir, opener) = Store::tmp_opener(); let peer_info_a = gen_peer_info(0); let peer_info_to_ban = gen_peer_info(1); let boot_nodes = vec![peer_info_a, peer_info_to_ban.clone()]; { - let store = StoreOpener::with_default_config(tmp_dir.path()).open(); + let store = opener.open(); let mut peer_store = PeerStore::new(store, &boot_nodes, Default::default()).unwrap(); assert_eq!(peer_store.healthy_peers(3).len(), 2); peer_store.peer_ban(&peer_info_to_ban.id, ReasonForBan::Abusive).unwrap(); assert_eq!(peer_store.healthy_peers(3).len(), 1); } { - let store_new = StoreOpener::with_default_config(tmp_dir.path()).open(); + let store_new = opener.open(); let peer_store_new = PeerStore::new(store_new, &boot_nodes, Default::default()).unwrap(); assert_eq!(peer_store_new.healthy_peers(3).len(), 1); } @@ -49,12 +49,12 @@ fn ban_store() { #[test] fn test_unconnected_peer() { - let tmp_dir = tempfile::Builder::new().prefix("_test_store_ban").tempdir().unwrap(); + let (_tmp_dir, opener) = Store::tmp_opener(); let peer_info_a = gen_peer_info(0); let peer_info_to_ban = gen_peer_info(1); let boot_nodes = vec![peer_info_a, peer_info_to_ban]; { - let store = StoreOpener::with_default_config(tmp_dir.path()).open(); + let store = opener.open(); let peer_store = PeerStore::new(store, &boot_nodes, Default::default()).unwrap(); assert!(peer_store.unconnected_peer(|_| false).is_some()); assert!(peer_store.unconnected_peer(|_| true).is_none()); @@ -283,8 +283,7 @@ fn check_ignore_blacklisted_peers() { #[test] fn remove_blacklisted_peers_from_store() { - let tmp_dir = - tempfile::Builder::new().prefix("_remove_blacklisted_peers_from_store").tempdir().unwrap(); + let (_tmp_dir, opener) = Store::tmp_opener(); let (peer_ids, peer_infos): (Vec<_>, Vec<_>) = (0..3) .map(|i| { let id = get_peer_id(format!("node{}", i)); @@ -295,24 +294,24 @@ fn remove_blacklisted_peers_from_store() { // Add three peers. { - let store = StoreOpener::with_default_config(tmp_dir.path()).open(); + let store = opener.open(); let mut peer_store = PeerStore::new(store, &[], Default::default()).unwrap(); peer_store.add_indirect_peers(peer_infos.clone().into_iter()).unwrap(); } - assert_peers_in_store(tmp_dir.path(), &peer_ids); + assert_peers_in_store(&opener, &peer_ids); // Blacklisted peers are removed from the store. { - let store = StoreOpener::with_default_config(tmp_dir.path()).open(); + let store = opener.open(); let blacklist: Blacklist = [BlacklistEntry::from_addr(peer_infos[2].addr.unwrap())].into_iter().collect(); let _peer_store = PeerStore::new(store, &[], blacklist).unwrap(); } - assert_peers_in_store(tmp_dir.path(), &peer_ids[0..2]); + assert_peers_in_store(&opener, &peer_ids[0..2]); } -fn assert_peers_in_store(store_path: &std::path::Path, expected: &[PeerId]) { - let store = StoreOpener::with_default_config(store_path).open(); +fn assert_peers_in_store(opener: &StoreOpener, expected: &[PeerId]) { + let store = opener.open(); let stored_peers: HashSet = HashSet::from_iter( store.iter(DBCol::Peers).map(|(key, _)| PeerId::try_from_slice(key.as_ref()).unwrap()), ); @@ -336,7 +335,7 @@ fn assert_peers_in_cache( #[test] fn test_delete_peers() { - let tmp_dir = tempfile::Builder::new().prefix("_test_delete_peers").tempdir().unwrap(); + let (_tmp_dir, opener) = Store::tmp_opener(); let (peer_ids, peer_infos): (Vec<_>, Vec<_>) = (0..3) .map(|i| { let id = get_peer_id(format!("node{}", i)); @@ -348,18 +347,18 @@ fn test_delete_peers() { peer_infos.iter().map(|info| info.addr.unwrap().clone()).collect::>(); { - let store = StoreOpener::with_default_config(tmp_dir.path()).open(); + let store = opener.open(); let mut peer_store = PeerStore::new(store, &[], Default::default()).unwrap(); peer_store.add_indirect_peers(peer_infos.into_iter()).unwrap(); } - assert_peers_in_store(tmp_dir.path(), &peer_ids); + assert_peers_in_store(&opener, &peer_ids); { - let store = StoreOpener::with_default_config(tmp_dir.path()).open(); + let store = opener.open(); let mut peer_store = PeerStore::new(store, &[], Default::default()).unwrap(); assert_peers_in_cache(&peer_store, &peer_ids, &peer_addresses); peer_store.delete_peers(&peer_ids).unwrap(); assert_peers_in_cache(&peer_store, &[], &[]); } - assert_peers_in_store(tmp_dir.path(), &[]); + assert_peers_in_store(&opener, &[]); } diff --git a/core/store/Cargo.toml b/core/store/Cargo.toml index 3c376ed31e4..a5f7bc71a58 100644 --- a/core/store/Cargo.toml +++ b/core/store/Cargo.toml @@ -17,6 +17,7 @@ enum-map = "2.1.0" rocksdb = { version = "0.18.0", default-features = false, features = ["snappy", "lz4", "zstd", "zlib"] } serde = { version = "1", features = ["derive"] } serde_json = "1" +tempfile = "3" num_cpus = "1.11" rand = "0.7" strum = { version = "0.24", features = ["derive"] } @@ -36,7 +37,6 @@ near-cache = { path = "../../utils/near-cache" } [dev-dependencies] assert_matches = "1.5.0" -tempfile = "3" bencher = "0.1.5" rand = "0.7" diff --git a/core/store/benches/store_bench.rs b/core/store/benches/store_bench.rs index d7ac4d7e47c..1e4367cdd52 100644 --- a/core/store/benches/store_bench.rs +++ b/core/store/benches/store_bench.rs @@ -3,7 +3,7 @@ extern crate bencher; use bencher::{black_box, Bencher}; use near_primitives::errors::StorageError; -use near_store::{DBCol, Store, StoreOpener}; +use near_store::{DBCol, Store}; use std::time::{Duration, Instant}; /// Run a benchmark to generate `num_keys` keys, each of size `key_size`, then write then @@ -16,8 +16,8 @@ fn benchmark_write_then_read_successful( max_value_size: usize, col: DBCol, ) { - let tmp_dir = tempfile::Builder::new().tempdir().unwrap(); - let store = StoreOpener::with_default_config(tmp_dir.path()).open(); + let (_tmp_dir, opener) = Store::tmp_opener(); + let store = opener.open(); let keys = generate_keys(num_keys, key_size); write_to_db(&store, &keys, max_value_size, col); diff --git a/core/store/src/config.rs b/core/store/src/config.rs index 91616bd1e92..1ae9f95a095 100644 --- a/core/store/src/config.rs +++ b/core/store/src/config.rs @@ -46,19 +46,19 @@ pub struct StoreConfig { } fn default_enable_statistics_export() -> bool { - StoreConfig::const_default().enable_statistics_export + StoreConfig::DEFAULT.enable_statistics_export } fn default_max_open_files() -> u32 { - StoreConfig::const_default().max_open_files + StoreConfig::DEFAULT.max_open_files } fn default_col_state_cache_size() -> bytesize::ByteSize { - StoreConfig::const_default().col_state_cache_size + StoreConfig::DEFAULT.col_state_cache_size } fn default_block_size() -> bytesize::ByteSize { - StoreConfig::const_default().block_size + StoreConfig::DEFAULT.block_size } impl StoreConfig { @@ -81,16 +81,14 @@ impl StoreConfig { /// then. const DEFAULT_BLOCK_SIZE: bytesize::ByteSize = bytesize::ByteSize::kib(16); - const fn const_default() -> Self { - Self { - path: None, - enable_statistics: false, - enable_statistics_export: true, - max_open_files: Self::DEFAULT_MAX_OPEN_FILES, - col_state_cache_size: Self::DEFAULT_COL_STATE_CACHE_SIZE, - block_size: Self::DEFAULT_BLOCK_SIZE, - } - } + pub const DEFAULT: Self = Self { + path: None, + enable_statistics: false, + enable_statistics_export: true, + max_open_files: Self::DEFAULT_MAX_OPEN_FILES, + col_state_cache_size: Self::DEFAULT_COL_STATE_CACHE_SIZE, + block_size: Self::DEFAULT_BLOCK_SIZE, + }; /// Returns cache size for given column. pub const fn col_cache_size(&self, col: crate::DBCol) -> bytesize::ByteSize { @@ -103,7 +101,7 @@ impl StoreConfig { impl Default for StoreConfig { fn default() -> Self { - Self::const_default() + Self::DEFAULT } } @@ -118,14 +116,16 @@ pub fn get_store_path(base_path: &std::path::Path) -> std::path::PathBuf { /// Typical usage: /// /// ```ignore -/// let store = StoreOpener::new(&near_config.config.store) +/// let store = Store::opener(&near_config.config.store) /// .home(neard_home_dir) /// .open(); /// ``` pub struct StoreOpener<'a> { - /// Near home directory; path to the database is resolved relative to this - /// directory. - home_dir: &'a std::path::Path, + /// Path to the database. + /// + /// This is resolved from nearcore home directory and store configuration + /// passed to [`Store::opener`]. + path: std::path::PathBuf, /// Configuration as provided by the user. config: &'a StoreConfig, @@ -136,17 +136,10 @@ pub struct StoreOpener<'a> { impl<'a> StoreOpener<'a> { /// Initialises a new opener with given home directory and store config. - pub fn new(home_dir: &'a std::path::Path, config: &'a StoreConfig) -> Self { - Self { home_dir, config, read_only: false } - } - - /// Initialises a new opener with given home directory and default config. - /// - /// This is meant for tests only. Production code should always read store - /// configuration from a config file and use [`Self::new`] instead. - pub fn with_default_config(home_dir: &'a std::path::Path) -> Self { - static CONFIG: StoreConfig = StoreConfig::const_default(); - Self::new(home_dir, &CONFIG) + pub(crate) fn new(home_dir: &std::path::Path, config: &'a StoreConfig) -> Self { + let path = + home_dir.join(config.path.as_deref().unwrap_or(std::path::Path::new(STORE_PATH))); + Self { path, config, read_only: false } } /// Configure whether the database should be opened in read-only mode. @@ -168,15 +161,14 @@ impl<'a> StoreOpener<'a> { /// Returns path to the underlying RocksDB database. /// - /// Does not check whether the database actually exists. It merely - /// constructs the path where the database would be if it existed. - pub fn get_path(&self) -> std::path::PathBuf { - self.home_dir.join(self.config.path.as_deref().unwrap_or(std::path::Path::new(STORE_PATH))) + /// Does not check whether the database actually exists. + pub fn get_path(&self) -> &std::path::Path { + &self.path } /// Returns version of the database; or `None` if it does not exist. pub fn get_version_if_exists(&self) -> Result, crate::db::DBError> { - std::fs::canonicalize(self.get_path()) + std::fs::canonicalize(&self.path) .ok() .map(|path| crate::RocksDB::get_version(&path)) .transpose() @@ -187,16 +179,15 @@ impl<'a> StoreOpener<'a> { /// Panics on failure. // TODO(mina86): Change it to return Result. pub fn open(&self) -> crate::Store { - let path = self.get_path(); - if std::fs::canonicalize(&path).is_ok() { - tracing::info!(target: "near", path=%path.display(), "Opening RocksDB database"); + if std::fs::canonicalize(&self.path).is_ok() { + tracing::info!(target: "near", path=%self.path.display(), "Opening RocksDB database"); } else if self.read_only { - tracing::error!(target: "near", path=%path.display(), "Database does not exist"); + tracing::error!(target: "near", path=%self.path.display(), "Database does not exist"); panic!("Failed to open non-existent the database"); } else { - tracing::info!(target: "near", path=%path.display(), "Creating new RocksDB database"); + tracing::info!(target: "near", path=%self.path.display(), "Creating new RocksDB database"); } - let db = crate::RocksDB::open(&path, &self.config, self.read_only) + let db = crate::RocksDB::open(&self.path, &self.config, self.read_only) .expect("Failed to open the database"); crate::Store::new(std::sync::Arc::new(db)) } diff --git a/core/store/src/db.rs b/core/store/src/db.rs index 43cb5ac05cf..daf77ea961d 100644 --- a/core/store/src/db.rs +++ b/core/store/src/db.rs @@ -780,7 +780,7 @@ fn parse_statistics(statistics: &str) -> Result(home_dir: &std::path::Path, config: &'a StoreConfig) -> StoreOpener<'a> { + StoreOpener::new(home_dir, config) + } + + /// Initialises a new opener for temporary store. + /// + /// This is meant for tests only. It **panics** if a temporary directory + /// cannot be created. + /// + /// Caller must hold the temporary directory returned as first element of + /// the tuple while the store is open. + pub fn tmp_opener() -> (tempfile::TempDir, StoreOpener<'static>) { + let dir = tempfile::tempdir().unwrap(); + let opener = Self::opener(dir.path(), &StoreConfig::DEFAULT); + (dir, opener) + } + pub(crate) fn new(storage: Arc) -> Store { Store { storage } } diff --git a/genesis-tools/genesis-populate/src/main.rs b/genesis-tools/genesis-populate/src/main.rs index d20142e04f6..b1ccddc63d6 100644 --- a/genesis-tools/genesis-populate/src/main.rs +++ b/genesis-tools/genesis-populate/src/main.rs @@ -29,7 +29,7 @@ fn main() { let near_config = load_config(home_dir, GenesisValidationMode::Full) .unwrap_or_else(|e| panic!("Error loading config: {:#}", e)); - let store = near_store::StoreOpener::new(home_dir, &near_config.config.store).open(); + let store = near_store::Store::opener(home_dir, &near_config.config.store).open(); GenesisBuilder::from_config_and_store(home_dir, Arc::new(near_config.genesis), store) .add_additional_accounts(additional_accounts_num) .add_additional_accounts_contract(near_test_contracts::trivial_contract().to_vec()) diff --git a/genesis-tools/genesis-populate/src/state_dump.rs b/genesis-tools/genesis-populate/src/state_dump.rs index 5c495ff8abd..ecfb4f624d3 100644 --- a/genesis-tools/genesis-populate/src/state_dump.rs +++ b/genesis-tools/genesis-populate/src/state_dump.rs @@ -17,7 +17,8 @@ pub struct StateDump { impl StateDump { pub fn from_dir(dir: &Path, store_home_dir: &Path) -> Self { - let store = near_store::StoreOpener::with_default_config(store_home_dir).open(); + let store = + near_store::Store::opener(store_home_dir, &near_store::StoreConfig::DEFAULT).open(); let state_file = dir.join(STATE_DUMP_FILE); store .load_from_file(DBCol::State, state_file.as_path()) diff --git a/nearcore/benches/store.rs b/nearcore/benches/store.rs index af1cc71cf30..99a164d5a73 100644 --- a/nearcore/benches/store.rs +++ b/nearcore/benches/store.rs @@ -28,7 +28,7 @@ fn read_trie_items(bench: &mut Bencher, shard_id: usize, read_only: bool) { bench.iter(move || { tracing::info!(target: "neard", "{:?}", home_dir); - let store = near_store::StoreOpener::new(&home_dir, &near_config.config.store) + let store = near_store::Store::opener(&home_dir, &near_config.config.store) .read_only(read_only) .open(); diff --git a/nearcore/src/lib.rs b/nearcore/src/lib.rs index cd7c2e2d62b..ec113782c40 100644 --- a/nearcore/src/lib.rs +++ b/nearcore/src/lib.rs @@ -202,7 +202,7 @@ fn apply_store_migrations_if_exists( } fn init_and_migrate_store(home_dir: &Path, near_config: &NearConfig) -> anyhow::Result { - let opener = StoreOpener::new(home_dir, &near_config.config.store); + let opener = Store::opener(home_dir, &near_config.config.store); let exists = apply_store_migrations_if_exists(&opener, near_config)?; let store = opener.open(); if !exists { @@ -379,7 +379,7 @@ pub fn recompress_storage(home_dir: &Path, opts: RecompressOpts) -> anyhow::Resu .map_err(|err| anyhow::anyhow!("setrlimit: NOFILE: {}", err))?; } - let src_opener = StoreOpener::new(home_dir, &config.store).read_only(true); + let src_opener = Store::opener(home_dir, &config.store).read_only(true); let src_path = src_opener.get_path(); if let Some(db_version) = src_opener.get_version_if_exists()? { anyhow::ensure!( @@ -398,7 +398,7 @@ pub fn recompress_storage(home_dir: &Path, opts: RecompressOpts) -> anyhow::Resu // Note: opts.dest_dir is resolved relative to current working directory // (since it’s a command line option) which is why we set home to cwd. let cwd = std::env::current_dir()?; - let dst_opener = StoreOpener::new(&cwd, &dst_config); + let dst_opener = Store::opener(&cwd, &dst_config); let dst_path = dst_opener.get_path(); anyhow::ensure!( !dst_opener.check_if_exists(), diff --git a/nearcore/src/runtime/mod.rs b/nearcore/src/runtime/mod.rs index 0a4fca9170f..1852fa9195b 100644 --- a/nearcore/src/runtime/mod.rs +++ b/nearcore/src/runtime/mod.rs @@ -2074,13 +2074,11 @@ mod test { impl TestEnv { pub fn new( - prefix: &str, validators: Vec>, epoch_length: BlockHeightDelta, has_reward: bool, ) -> Self { Self::new_with_tracking( - prefix, validators, epoch_length, TrackedConfig::new_empty(), @@ -2089,14 +2087,12 @@ mod test { } pub fn new_with_minimum_stake_divisor( - prefix: &str, validators: Vec>, epoch_length: BlockHeightDelta, has_reward: bool, stake_divisor: u64, ) -> Self { Self::new_with_tracking_and_minimum_stake_divisor( - prefix, validators, epoch_length, TrackedConfig::new_empty(), @@ -2106,14 +2102,12 @@ mod test { } pub fn new_with_tracking( - prefix: &str, validators: Vec>, epoch_length: BlockHeightDelta, tracked_config: TrackedConfig, has_reward: bool, ) -> Self { Self::new_with_tracking_and_minimum_stake_divisor( - prefix, validators, epoch_length, tracked_config, @@ -2123,15 +2117,14 @@ mod test { } fn new_with_tracking_and_minimum_stake_divisor( - prefix: &str, validators: Vec>, epoch_length: BlockHeightDelta, tracked_config: TrackedConfig, has_reward: bool, minimum_stake_divisor: Option, ) -> Self { - let dir = tempfile::Builder::new().prefix(prefix).tempdir().unwrap(); - let store = near_store::StoreOpener::with_default_config(dir.path()).open(); + let (dir, opener) = Store::tmp_opener(); + let store = opener.open(); let all_validators = validators.iter().fold(BTreeSet::new(), |acc, x| { acc.union(&x.iter().cloned().collect()).cloned().collect() }); @@ -2330,7 +2323,7 @@ mod test { let validators = (0..num_nodes) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let mut env = TestEnv::new("test_validator_rotation", vec![validators.clone()], 2, false); + let mut env = TestEnv::new(vec![validators.clone()], 2, false); let block_producers: Vec<_> = validators .iter() .map(|id| InMemoryValidatorSigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref())) @@ -2436,8 +2429,7 @@ mod test { let validators = (0..num_nodes) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let mut env = - TestEnv::new("test_validator_stake_change", vec![validators.clone()], 2, false); + let mut env = TestEnv::new(vec![validators.clone()], 2, false); let block_producers: Vec<_> = validators .iter() .map(|id| InMemoryValidatorSigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref())) @@ -2478,12 +2470,7 @@ mod test { let validators = (0..num_nodes) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let mut env = TestEnv::new( - "test_validator_stake_change_multiple_times", - vec![validators.clone()], - 4, - false, - ); + let mut env = TestEnv::new(vec![validators.clone()], 4, false); let block_producers: Vec<_> = validators .iter() .map(|id| InMemoryValidatorSigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref())) @@ -2587,12 +2574,7 @@ mod test { let validators = (0..num_nodes) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let mut env = TestEnv::new( - "test_validator_stake_change_multiple_times", - vec![validators.clone()], - 5, - false, - ); + let mut env = TestEnv::new(vec![validators.clone()], 5, false); let block_producers: Vec<_> = validators .iter() .map(|id| InMemoryValidatorSigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref())) @@ -2631,8 +2613,7 @@ mod test { let validators = (0..2) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let env = - TestEnv::new("verify_validator_signature_failure", vec![validators.clone()], 2, true); + let env = TestEnv::new(vec![validators.clone()], 2, true); let data = [0; 32]; let signer = InMemorySigner::from_seed( validators[0].clone(), @@ -2664,7 +2645,7 @@ mod test { let validators = (0..num_nodes) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let mut env = TestEnv::new("test_state_sync", vec![validators.clone()], 2, false); + let mut env = TestEnv::new(vec![validators.clone()], 2, false); let block_producers: Vec<_> = validators .iter() .map(|id| InMemoryValidatorSigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref())) @@ -2684,7 +2665,7 @@ mod test { .unwrap(); let root_node = env.runtime.get_state_root_node(0, &block_hash, &env.state_roots[0]).unwrap(); - let mut new_env = TestEnv::new("test_state_sync", vec![validators], 2, false); + let mut new_env = TestEnv::new(vec![validators], 2, false); for i in 1..=2 { let prev_hash = hash(&[new_env.head.height as u8]); let cur_hash = hash(&[(new_env.head.height + 1) as u8]); @@ -2761,8 +2742,7 @@ mod test { let validators = (0..num_nodes) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let mut env = - TestEnv::new("test_validator_get_validator_info", vec![validators.clone()], 2, false); + let mut env = TestEnv::new(vec![validators.clone()], 2, false); let block_producers: Vec<_> = validators .iter() .map(|id| InMemoryValidatorSigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref())) @@ -2910,7 +2890,6 @@ mod test { .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); let mut env = TestEnv::new_with_tracking( - "test_validator_get_validator_info", vec![validators.clone(), vec![validators[0].clone()]], 2, TrackedConfig::Accounts(vec![validators[1].clone()]), @@ -2984,12 +2963,8 @@ mod test { #[test] fn test_challenges() { - let mut env = TestEnv::new( - "test_challenges", - vec![vec!["test1".parse().unwrap(), "test2".parse().unwrap()]], - 2, - true, - ); + let mut env = + TestEnv::new(vec![vec!["test1".parse().unwrap(), "test2".parse().unwrap()]], 2, true); env.step( vec![vec![]], vec![true], @@ -3033,7 +3008,7 @@ mod test { let validators = (0..num_nodes) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let mut env = TestEnv::new("test_challenges", vec![validators.clone()], 3, false); + let mut env = TestEnv::new(vec![validators.clone()], 3, false); let block_producers: Vec<_> = validators .iter() .map(|id| InMemoryValidatorSigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref())) @@ -3123,7 +3098,7 @@ mod test { let validators = (0..num_nodes) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let mut env = TestEnv::new("test_challenges", vec![validators.clone()], 5, false); + let mut env = TestEnv::new(vec![validators.clone()], 5, false); let signers: Vec<_> = validators .iter() .map(|id| InMemorySigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref())) @@ -3174,7 +3149,7 @@ mod test { let validators = (0..num_nodes) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let mut env = TestEnv::new("test_challenges", vec![validators], 5, false); + let mut env = TestEnv::new(vec![validators], 5, false); env.step( vec![vec![]], vec![true], @@ -3217,7 +3192,6 @@ mod test { .collect::>(); let mut env = if !cfg!(feature = "protocol_feature_chunk_only_producers") { TestEnv::new_with_minimum_stake_divisor( - "test_fishermen_stake", vec![validators.clone()], 4, false, @@ -3226,7 +3200,7 @@ mod test { 20000, ) } else { - TestEnv::new("test_fishermen_stake", vec![validators.clone()], 4, false) + TestEnv::new(vec![validators.clone()], 4, false) }; let block_producers: Vec<_> = validators .iter() @@ -3294,7 +3268,6 @@ mod test { .collect::>(); let mut env = if !cfg!(feature = "protocol_feature_chunk_only_producers") { TestEnv::new_with_minimum_stake_divisor( - "test_fishermen_unstake", vec![validators.clone()], 2, false, @@ -3303,7 +3276,7 @@ mod test { 20000, ) } else { - TestEnv::new("test_fishermen_unstake", vec![validators.clone()], 2, false) + TestEnv::new(vec![validators.clone()], 2, false) }; let block_producers: Vec<_> = validators .iter() @@ -3360,8 +3333,7 @@ mod test { let epoch_length = 40; let validators = (0..num_nodes).map(|i| format!("test{}", i + 1).parse().unwrap()).collect::>(); - let mut env = - TestEnv::new("test_validator_reward", vec![validators.clone()], epoch_length, true); + let mut env = TestEnv::new(vec![validators.clone()], epoch_length, true); let block_producers: Vec<_> = validators .iter() .map(|id| InMemoryValidatorSigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref())) @@ -3393,8 +3365,7 @@ mod test { let validators = (0..num_nodes) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let mut env = - TestEnv::new("test_validator_delete_account", vec![validators.clone()], 4, false); + let mut env = TestEnv::new(vec![validators.clone()], 4, false); let block_producers: Vec<_> = validators .iter() .map(|id| InMemoryValidatorSigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref())) @@ -3443,7 +3414,7 @@ mod test { let validators = (0..num_nodes) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let mut env = TestEnv::new("test_proposal_deduped", vec![validators.clone()], 4, false); + let mut env = TestEnv::new(vec![validators.clone()], 4, false); let block_producers: Vec<_> = validators .iter() .map(|id| InMemoryValidatorSigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref())) @@ -3468,7 +3439,7 @@ mod test { let validators = (0..num_nodes) .map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()) .collect::>(); - let mut env = TestEnv::new("test_proposal_deduped", vec![validators.clone()], 4, false); + let mut env = TestEnv::new(vec![validators.clone()], 4, false); let block_producers: Vec<_> = validators .iter() .map(|id| InMemoryValidatorSigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref())) diff --git a/runtime/runtime-params-estimator/src/main.rs b/runtime/runtime-params-estimator/src/main.rs index 82809d41f38..337ca0190e0 100644 --- a/runtime/runtime-params-estimator/src/main.rs +++ b/runtime/runtime-params-estimator/src/main.rs @@ -143,8 +143,7 @@ fn main() -> anyhow::Result<()> { let near_config = nearcore::load_config(&state_dump_path, GenesisValidationMode::Full) .context("Error loading config")?; - let store = - near_store::StoreOpener::new(&state_dump_path, &near_config.config.store).open(); + let store = near_store::Store::opener(&state_dump_path, &near_config.config.store).open(); GenesisBuilder::from_config_and_store( &state_dump_path, Arc::new(near_config.genesis), diff --git a/test-utils/runtime-tester/src/run_test.rs b/test-utils/runtime-tester/src/run_test.rs index da9c3fde83a..29801a0502e 100644 --- a/test-utils/runtime-tester/src/run_test.rs +++ b/test-utils/runtime-tester/src/run_test.rs @@ -47,10 +47,8 @@ impl Scenario { let (tempdir, store) = if self.use_in_memory_store { (None, create_test_store()) } else { - let tempdir = tempfile::tempdir() - .unwrap_or_else(|err| panic!("failed to create temporary directory: {}", err)); - let store = near_store::StoreOpener::with_default_config(tempdir.path()).open(); - (Some(tempdir), store) + let (tempdir, opener) = near_store::Store::tmp_opener(); + (Some(tempdir), opener.open()) }; let mut env = TestEnv::builder(ChainGenesis::from(&genesis)) diff --git a/test-utils/store-validator/src/main.rs b/test-utils/store-validator/src/main.rs index 01b459683db..a9d07755dac 100644 --- a/test-utils/store-validator/src/main.rs +++ b/test-utils/store-validator/src/main.rs @@ -30,7 +30,7 @@ fn main() { let near_config = load_config(home_dir, GenesisValidationMode::Full) .unwrap_or_else(|e| panic!("Error loading config: {:#}", e)); - let store = near_store::StoreOpener::new(home_dir, &near_config.config.store).open(); + let store = near_store::Store::opener(home_dir, &near_config.config.store).open(); let runtime_adapter: Arc = Arc::new(nearcore::NightshadeRuntime::from_config(home_dir, store.clone(), &near_config)); diff --git a/tools/mock_node/src/setup.rs b/tools/mock_node/src/setup.rs index 957832baaa5..224bbaa81ca 100644 --- a/tools/mock_node/src/setup.rs +++ b/tools/mock_node/src/setup.rs @@ -33,7 +33,7 @@ fn setup_runtime( let store = if in_memory_storage { create_test_store() } else { - near_store::StoreOpener::new(home_dir, &config.config.store).open() + near_store::Store::opener(home_dir, &config.config.store).open() }; Arc::new(NightshadeRuntime::from_config(home_dir, store, config)) diff --git a/tools/state-viewer/src/cli.rs b/tools/state-viewer/src/cli.rs index a3f3ffab89d..85aba338ab9 100644 --- a/tools/state-viewer/src/cli.rs +++ b/tools/state-viewer/src/cli.rs @@ -70,7 +70,7 @@ impl StateViewerSubCommand { pub fn run(self, home_dir: &Path, genesis_validation: GenesisValidationMode, readwrite: bool) { let near_config = load_config(home_dir, genesis_validation) .unwrap_or_else(|e| panic!("Error loading config: {:#}", e)); - let store = near_store::StoreOpener::new(home_dir, &near_config.config.store) + let store = near_store::Store::opener(home_dir, &near_config.config.store) .read_only(!readwrite) .open(); match self {