Skip to content

Commit

Permalink
Remove RocksdbStorage::new_blocking()
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Oct 24, 2023
1 parent 7e14384 commit 88b2932
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
4 changes: 2 additions & 2 deletions node/libs/storage/src/block_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl RocksdbStorage {
}

/// Gets a block by its number.
pub(crate) fn block_blocking(&self, number: BlockNumber) -> anyhow::Result<Option<FinalBlock>> {
fn block_blocking(&self, number: BlockNumber) -> anyhow::Result<Option<FinalBlock>> {
let db = self.read();

let Some(raw_block) = db
Expand Down Expand Up @@ -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.");
Expand Down
37 changes: 18 additions & 19 deletions node/libs/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,38 @@ impl RocksdbStorage {
ctx: &ctx::Ctx,
genesis_block: &validator::FinalBlock,
path: &Path,
) -> anyhow::Result<Self> {
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<Self> {
) -> StorageResult<Self> {
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)
}
Expand Down

0 comments on commit 88b2932

Please sign in to comment.