Allow opening the database read-only mode

This commit is contained in:
Marek 2023-12-08 18:37:04 +01:00
parent 5203e85a46
commit 7ef6317fc4
10 changed files with 22 additions and 7 deletions

View File

@ -33,7 +33,7 @@ pub async fn init(
state: scan::State,
chain_tip_change: ChainTipChange,
) -> Result<(), Report> {
let storage = tokio::task::spawn_blocking(move || Storage::new(&config, network))
let storage = tokio::task::spawn_blocking(move || Storage::new(&config, network, false))
.wait_for_panics()
.await;

View File

@ -55,8 +55,8 @@ impl Storage {
///
/// This method can block while creating or reading database files, so it must be inside
/// spawn_blocking() in async code.
pub fn new(config: &Config, network: Network) -> Self {
let mut storage = Self::new_db(config, network);
pub fn new(config: &Config, network: Network, read_only: bool) -> Self {
let mut storage = Self::new_db(config, network, read_only);
for (sapling_key, birthday) in config.sapling_keys_to_scan.iter() {
storage.add_sapling_key(sapling_key, Some(zebra_chain::block::Height(*birthday)));

View File

@ -46,11 +46,11 @@ impl Storage {
/// If there is no existing database, creates a new database on disk.
///
/// New keys in `config` are not inserted into the database.
pub(crate) fn new_db(config: &Config, network: Network) -> Self {
pub(crate) fn new_db(config: &Config, network: Network, read_only: bool) -> Self {
Self::new_with_debug(
config, network,
// TODO: make format upgrades work with any database, then change this to `false`
true,
true, read_only,
)
}
@ -64,6 +64,7 @@ impl Storage {
config: &Config,
network: Network,
debug_skip_format_upgrades: bool,
read_only: bool,
) -> Self {
let db = ScannerDb::new(
config.db_config(),
@ -74,6 +75,7 @@ impl Storage {
SCANNER_COLUMN_FAMILIES_IN_CODE
.iter()
.map(ToString::to_string),
read_only,
);
let new_storage = Self { db };

View File

@ -186,7 +186,7 @@ fn scanning_fake_blocks_store_key_and_results() -> Result<()> {
zcash_client_backend::encoding::encode_extended_full_viewing_key("zxviews", &extfvk);
// Create a database
let mut s = crate::storage::Storage::new(&Config::ephemeral(), Network::Mainnet);
let mut s = crate::storage::Storage::new(&Config::ephemeral(), Network::Mainnet, false);
// Insert the generated key to the database
s.add_sapling_key(&key_to_be_stored, None);

View File

@ -143,6 +143,7 @@ impl FinalizedState {
false,
#[cfg(feature = "elasticsearch")]
elastic_db,
false,
)
}
@ -155,6 +156,7 @@ impl FinalizedState {
network: Network,
debug_skip_format_upgrades: bool,
#[cfg(feature = "elasticsearch")] elastic_db: Option<elasticsearch::Elasticsearch>,
read_only: bool,
) -> Self {
let db = ZebraDb::new(
config,
@ -165,6 +167,7 @@ impl FinalizedState {
STATE_COLUMN_FAMILIES_IN_CODE
.iter()
.map(ToString::to_string),
read_only,
);
#[cfg(feature = "elasticsearch")]

View File

@ -622,6 +622,7 @@ impl DiskDb {
format_version_in_code: &Version,
network: Network,
column_families_in_code: impl IntoIterator<Item = String>,
read_only: bool,
) -> DiskDb {
let db_kind = db_kind.as_ref();
let path = config.db_path(db_kind, format_version_in_code.major, network);
@ -644,7 +645,11 @@ impl DiskDb {
.unique()
.map(|cf_name| rocksdb::ColumnFamilyDescriptor::new(cf_name, db_options.clone()));
let db_result = DB::open_cf_descriptors(&db_options, &path, column_families);
let db_result = if read_only {
DB::open_cf_descriptors_read_only(&db_options, &path, column_families, false)
} else {
DB::open_cf_descriptors(&db_options, &path, column_families)
};
match db_result {
Ok(db) => {

View File

@ -96,6 +96,7 @@ impl ZebraDb {
network: Network,
debug_skip_format_upgrades: bool,
column_families_in_code: impl IntoIterator<Item = String>,
read_only: bool,
) -> ZebraDb {
let disk_version = database_format_version_on_disk(
config,
@ -129,6 +130,7 @@ impl ZebraDb {
format_version_in_code,
network,
column_families_in_code,
read_only,
),
};

View File

@ -89,6 +89,7 @@ fn test_block_db_round_trip_with(
STATE_COLUMN_FAMILIES_IN_CODE
.iter()
.map(ToString::to_string),
false,
);
// Check that each block round-trips to the database

View File

@ -377,5 +377,6 @@ fn new_ephemeral_db() -> ZebraDb {
STATE_COLUMN_FAMILIES_IN_CODE
.iter()
.map(ToString::to_string),
false,
)
}

View File

@ -99,6 +99,7 @@ pub(crate) fn new_state_with_mainnet_genesis(
true,
#[cfg(feature = "elasticsearch")]
None,
false,
);
let non_finalized_state = NonFinalizedState::new(network);