diff --git a/node/libs/storage/src/block_store.rs b/node/libs/storage/src/block_store.rs index 763b6601..623c1ac1 100644 --- a/node/libs/storage/src/block_store.rs +++ b/node/libs/storage/src/block_store.rs @@ -265,7 +265,7 @@ impl RocksdbStorage { } /// Gets a block by its number. - pub(crate) fn block_blocking(&self, number: BlockNumber) -> anyhow::Result> { + fn block_blocking(&self, number: BlockNumber) -> anyhow::Result> { let db = self.read(); let Some(raw_block) = db @@ -305,7 +305,7 @@ impl RocksdbStorage { // ---------------- Write methods ---------------- /// Insert a new block into the database. - pub(crate) fn put_block_blocking(&self, finalized_block: &FinalBlock) -> anyhow::Result<()> { + fn put_block_blocking(&self, finalized_block: &FinalBlock) -> anyhow::Result<()> { let db = self.write(); let block_number = finalized_block.block.number; tracing::debug!("Inserting new block #{block_number} into the database."); diff --git a/node/libs/storage/src/lib.rs b/node/libs/storage/src/lib.rs index 7699f1a7..d5f682fa 100644 --- a/node/libs/storage/src/lib.rs +++ b/node/libs/storage/src/lib.rs @@ -66,39 +66,38 @@ impl RocksdbStorage { ctx: &ctx::Ctx, genesis_block: &validator::FinalBlock, path: &Path, - ) -> anyhow::Result { - scope::run!(ctx, |_, scope| async { - Ok(scope - .spawn_blocking(|| Self::new_blocking(genesis_block, path)) - .join(ctx) - .await?) - }) - .await - } - - /// Blocking version of [`Self::new()`]. - fn new_blocking(genesis_block: &validator::FinalBlock, path: &Path) -> anyhow::Result { + ) -> StorageResult { let mut options = rocksdb::Options::default(); options.create_missing_column_families(true); options.create_if_missing(true); - let db = rocksdb::DB::open(&options, path).context("Failed opening RocksDB")?; + let db = scope::run!(ctx, |_, s| async { + Ok(s.spawn_blocking(|| { + rocksdb::DB::open(&options, path) + .context("Failed opening RocksDB") + .map_err(StorageError::Database) + }) + .join(ctx) + .await?) + }) + .await?; + let this = Self { inner: RwLock::new(db), cached_last_contiguous_block_number: AtomicU64::new(0), block_writes_sender: watch::channel(genesis_block.block.number).0, }; - if let Some(stored_genesis_block) = this.block_blocking(genesis_block.block.number)? { - anyhow::ensure!( - stored_genesis_block.block == genesis_block.block, - "Mismatch between stored and expected genesis block" - ); + if let Some(stored_genesis_block) = this.block(ctx, genesis_block.block.number).await? { + if stored_genesis_block.block != genesis_block.block { + let err = anyhow::anyhow!("Mismatch between stored and expected genesis block"); + return Err(StorageError::Database(err)); + } } else { tracing::debug!( "Genesis block not present in RocksDB at `{path}`; saving {genesis_block:?}", path = path.display() ); - this.put_block_blocking(genesis_block)?; + this.put_block(ctx, genesis_block).await?; } Ok(this) }