Skip to content

Commit f838726

Browse files
committed
ChainDB: define LedgerDB.GetVolatileSuffix via getCurrentChain
This requires some knot tying as we initialize the LedgerDB before the ChainDB is fully initialized. This is preparing for Peras, which will introduce a refined immutability criterion. Due to this change, the LedgerDB will automatically use the same criterion as the ChainDB (via `getCurrentChain`).
1 parent 0ea5fae commit f838726

File tree

1 file changed

+40
-2
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB

1 file changed

+40
-2
lines changed

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import Data.Functor.Contravariant ((>$<))
5353
import qualified Data.Map.Strict as Map
5454
import Data.Maybe.Strict (StrictMaybe (..))
5555
import GHC.Stack (HasCallStack)
56+
import NoThunks.Class
5657
import Ouroboros.Consensus.Block
5758
import Ouroboros.Consensus.Config
5859
import qualified Ouroboros.Consensus.Fragment.Validated as VF
@@ -86,6 +87,7 @@ import Ouroboros.Consensus.Util.STM
8687
( Fingerprint (..)
8788
, WithFingerprint (..)
8889
)
90+
import Ouroboros.Network.AnchoredFragment (AnchoredFragment)
8991
import qualified Ouroboros.Network.AnchoredFragment as AF
9092
import Ouroboros.Network.BlockFetch.ConsensusInterface
9193
( ChainSelStarvation (..)
@@ -160,14 +162,15 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
160162
(chainDB, testing, env) <- lift $ do
161163
traceWith tracer $ TraceOpenEvent (OpenedVolatileDB maxSlot)
162164
traceWith tracer $ TraceOpenEvent StartedOpeningLgrDB
163-
let secParam = configSecurityParam $ Args.cdbsTopLevelConfig cdbSpecificArgs
165+
(ledgerDbGetVolatileSuffix, setGetCurrentChainForLedgerDB) <-
166+
mkLedgerDbGetVolatileSuffix
164167
(lgrDB, replayed) <-
165168
LedgerDB.openDB
166169
argsLgrDb
167170
(ImmutableDB.streamAPI immutableDB)
168171
immutableDbTipPoint
169172
(Query.getAnyKnownBlock immutableDB volatileDB)
170-
(LedgerDB.praosGetVolatileSuffix secParam)
173+
ledgerDbGetVolatileSuffix
171174
traceWith tracer $ TraceOpenEvent OpenedLgrDB
172175

173176
varInvalid <- newTVarIO (WithFingerprint Map.empty (Fingerprint 0))
@@ -248,6 +251,9 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
248251
, cdbLoE = Args.cdbsLoE cdbSpecificArgs
249252
, cdbChainSelStarvation = varChainSelStarvation
250253
}
254+
255+
setGetCurrentChainForLedgerDB $ Query.getCurrentChain env
256+
251257
h <- fmap CDBHandle $ newTVarIO $ ChainDbOpen env
252258
let chainDB =
253259
API.ChainDB
@@ -306,6 +312,38 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
306312
tracer = Args.cdbsTracer cdbSpecificArgs
307313
Args.ChainDbArgs argsImmutableDb argsVolatileDb argsLgrDb cdbSpecificArgs = args
308314

315+
-- The LedgerDB requires a criterion ('LedgerDB.GetVolatileSuffix')
316+
-- determining which of its states are volatile/immutable. Once we have
317+
-- initialized the ChainDB we can defer this decision to
318+
-- 'Query.getCurrentChain'.
319+
--
320+
-- However, we initialize the LedgerDB before the ChainDB (for initial chain
321+
-- selection), so during that period, we temporarily consider no state (apart
322+
-- from the anchor state) as immutable. This is fine as we don't perform eg
323+
-- any rollbacks during this period.
324+
mkLedgerDbGetVolatileSuffix ::
325+
m
326+
( LedgerDB.GetVolatileSuffix m blk
327+
, STM m (AnchoredFragment (Header blk)) -> m ()
328+
)
329+
mkLedgerDbGetVolatileSuffix = do
330+
varGetCurrentChain ::
331+
StrictTMVar m (OnlyCheckWhnf (STM m (AnchoredFragment (Header blk)))) <-
332+
newEmptyTMVarIO
333+
let getVolatileSuffix =
334+
LedgerDB.GetVolatileSuffix $
335+
tryReadTMVar varGetCurrentChain >>= \case
336+
-- If @setVarChain@ has not yet been invoked, return the entire
337+
-- suffix as volatile.
338+
Nothing -> pure id
339+
-- Otherwise, return the suffix with the same length as the
340+
-- current chain.
341+
Just (OnlyCheckWhnf getCurrentChain) -> do
342+
curChainLen <- AF.length <$> getCurrentChain
343+
pure $ AF.anchorNewest (fromIntegral curChainLen)
344+
setVarChain = atomically . writeTMVar varGetCurrentChain . OnlyCheckWhnf
345+
pure (getVolatileSuffix, setVarChain)
346+
309347
-- | We use 'runInnerWithTempRegistry' for the component databases.
310348
innerOpenCont ::
311349
IOLike m =>

0 commit comments

Comments
 (0)