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 (.. ))
3741import Control.ResourceRegistry (ResourceRegistry )
3842import qualified Data.Map.Strict as Map
3943import qualified Data.Set as Set
@@ -296,6 +300,20 @@ getPerasCertSnapshot ::
296300 ChainDbEnv m blk -> STM m (PerasCertSnapshot blk )
297301getPerasCertSnapshot 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