Skip to content

Commit a4ccf99

Browse files
committed
ouroboros-consensus: add ChainDB.waitForImmutableBlock
This function allows waiting until the immutables DB's tip is past the target slot.
1 parent 9119746 commit a4ccf99

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/API.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,16 @@ data ChainDB m blk = ChainDB
405405
, getPerasCertSnapshot :: STM m (PerasCertSnapshot blk)
406406
-- ^ Get the Peras certificate snapshot, containing the currently-known
407407
-- certificates boosting blocks newer than the immutable tip.
408+
, waitForImmutableBlock :: RealPoint blk -> m (Maybe (RealPoint blk))
409+
-- ^ wait until the immutable tip gets past the given slot:
410+
-- - returns the block when it becomes the immutable tip,
411+
-- reading it from disk;
412+
-- - if no block was found at the target slot, returns the immutable block
413+
-- at the next filled slot;
414+
-- - returns 'Nothing' if no block was found on disk at all.
415+
--
416+
-- Currently, the only use-case of this function is to verify the immutability
417+
-- of a block from the big ledger peer snapshot file.
408418
, closeDB :: m ()
409419
-- ^ Close the ChainDB
410420
--

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/Impl.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
290290
, addPerasCertAsync = getEnv1 h ChainSel.addPerasCertAsync
291291
, getPerasWeightSnapshot = getEnvSTM h Query.getPerasWeightSnapshot
292292
, getPerasCertSnapshot = getEnvSTM h Query.getPerasCertSnapshot
293+
, waitForImmutableBlock = getEnv1 h Query.waitForImmutableBlock
293294
}
294295
addBlockTestFuse <- newFuse "test chain selection"
295296
copyTestFuse <- newFuse "test copy to immutable db"

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/Impl/Query.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{-# LANGUAGE FlexibleContexts #-}
22
{-# LANGUAGE GADTs #-}
3+
{-# LANGUAGE LambdaCase #-}
34
{-# LANGUAGE MultiWayIf #-}
5+
{-# LANGUAGE NamedFieldPuns #-}
46
{-# LANGUAGE RecordWildCards #-}
57
{-# LANGUAGE ScopedTypeVariables #-}
68
{-# LANGUAGE TypeOperators #-}
@@ -26,6 +28,7 @@ module Ouroboros.Consensus.Storage.ChainDB.Impl.Query
2628
, getTipBlock
2729
, getTipHeader
2830
, getTipPoint
31+
, waitForImmutableBlock
2932

3033
-- * Low-level queries
3134
, getAnyBlockComponent
@@ -34,6 +37,7 @@ module Ouroboros.Consensus.Storage.ChainDB.Impl.Query
3437
, getChainSelStarvation
3538
) where
3639

40+
import Cardano.Ledger.BaseTypes (WithOrigin (..))
3741
import Control.ResourceRegistry (ResourceRegistry)
3842
import qualified Data.Map.Strict as Map
3943
import qualified Data.Set as Set
@@ -296,6 +300,20 @@ getPerasCertSnapshot ::
296300
ChainDbEnv m blk -> STM m (PerasCertSnapshot blk)
297301
getPerasCertSnapshot CDB{..} = PerasCertDB.getCertSnapshot cdbPerasCertDB
298302

303+
-- | Wait until the given point becomes immutable:
304+
-- - blocks until the immutable tip slot number is lower than the block's slot number;
305+
-- - ones the immutable tip is older than the slot of the target point:
306+
-- * returns the block at the target slot if it is occupied;n
307+
-- * otherwise, returns the block from the next occupied slot.
308+
waitForImmutableBlock :: IOLike m => ChainDbEnv m blk -> RealPoint blk -> m (Maybe (RealPoint blk))
309+
waitForImmutableBlock CDB{cdbImmutableDB} targetRealPoint = do
310+
atomically (ImmutableDB.getTip cdbImmutableDB) >>= \case
311+
Origin -> pure Nothing
312+
At tip ->
313+
if ImmutableDB.tipSlotNo tip >= realPointSlot targetRealPoint
314+
then ImmutableDB.getBlockAtOrAfterPoint cdbImmutableDB targetRealPoint
315+
else pure Nothing
316+
299317
{-------------------------------------------------------------------------------
300318
Unifying interface over the immutable DB and volatile DB, but independent
301319
of the ledger DB. These functions therefore do not require the entire

0 commit comments

Comments
 (0)