@@ -53,6 +53,7 @@ import Data.Functor.Contravariant ((>$<))
53
53
import qualified Data.Map.Strict as Map
54
54
import Data.Maybe.Strict (StrictMaybe (.. ))
55
55
import GHC.Stack (HasCallStack )
56
+ import NoThunks.Class
56
57
import Ouroboros.Consensus.Block
57
58
import Ouroboros.Consensus.Config
58
59
import qualified Ouroboros.Consensus.Fragment.Validated as VF
@@ -86,6 +87,7 @@ import Ouroboros.Consensus.Util.STM
86
87
( Fingerprint (.. )
87
88
, WithFingerprint (.. )
88
89
)
90
+ import Ouroboros.Network.AnchoredFragment (AnchoredFragment )
89
91
import qualified Ouroboros.Network.AnchoredFragment as AF
90
92
import Ouroboros.Network.BlockFetch.ConsensusInterface
91
93
( ChainSelStarvation (.. )
@@ -160,14 +162,15 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
160
162
(chainDB, testing, env) <- lift $ do
161
163
traceWith tracer $ TraceOpenEvent (OpenedVolatileDB maxSlot)
162
164
traceWith tracer $ TraceOpenEvent StartedOpeningLgrDB
163
- let secParam = configSecurityParam $ Args. cdbsTopLevelConfig cdbSpecificArgs
165
+ (ledgerDbGetVolatileSuffix, setGetCurrentChainForLedgerDB) <-
166
+ mkLedgerDbGetVolatileSuffix
164
167
(lgrDB, replayed) <-
165
168
LedgerDB. openDB
166
169
argsLgrDb
167
170
(ImmutableDB. streamAPI immutableDB)
168
171
immutableDbTipPoint
169
172
(Query. getAnyKnownBlock immutableDB volatileDB)
170
- ( LedgerDB. praosGetVolatileSuffix secParam)
173
+ ledgerDbGetVolatileSuffix
171
174
traceWith tracer $ TraceOpenEvent OpenedLgrDB
172
175
173
176
varInvalid <- newTVarIO (WithFingerprint Map. empty (Fingerprint 0 ))
@@ -248,6 +251,9 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
248
251
, cdbLoE = Args. cdbsLoE cdbSpecificArgs
249
252
, cdbChainSelStarvation = varChainSelStarvation
250
253
}
254
+
255
+ setGetCurrentChainForLedgerDB $ Query. getCurrentChain env
256
+
251
257
h <- fmap CDBHandle $ newTVarIO $ ChainDbOpen env
252
258
let chainDB =
253
259
API. ChainDB
@@ -306,6 +312,38 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
306
312
tracer = Args. cdbsTracer cdbSpecificArgs
307
313
Args. ChainDbArgs argsImmutableDb argsVolatileDb argsLgrDb cdbSpecificArgs = args
308
314
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
+
309
347
-- | We use 'runInnerWithTempRegistry' for the component databases.
310
348
innerOpenCont ::
311
349
IOLike m =>
0 commit comments