Skip to content

Commit 7ded356

Browse files
committed
LedgerDB.V2: adapt queries for DbChangelog of length >k
1 parent 9fa66b0 commit 7ded356

File tree

2 files changed

+24
-5
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB

2 files changed

+24
-5
lines changed

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V2.hs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,13 @@ implGetVolatileTip ::
267267
implGetVolatileTip = fmap current . readTVar . ldbSeq
268268

269269
implGetImmutableTip ::
270-
MonadSTM m =>
270+
(MonadSTM m, GetTip l) =>
271271
LedgerDBEnv m l blk ->
272272
STM m (l EmptyMK)
273-
implGetImmutableTip = fmap anchor . readTVar . ldbSeq
273+
implGetImmutableTip env =
274+
anchor . volatileSuffix k <$> readTVar (ldbSeq env)
275+
where
276+
k = ledgerDbCfgSecParam $ ldbCfg env
274277

275278
implGetPastLedgerState ::
276279
( MonadSTM m
@@ -280,7 +283,10 @@ implGetPastLedgerState ::
280283
, HeaderHash l ~ HeaderHash blk
281284
) =>
282285
LedgerDBEnv m l blk -> Point blk -> STM m (Maybe (l EmptyMK))
283-
implGetPastLedgerState env point = getPastLedgerAt point <$> readTVar (ldbSeq env)
286+
implGetPastLedgerState env point =
287+
getPastLedgerAt point . volatileSuffix k <$> readTVar (ldbSeq env)
288+
where
289+
k = ledgerDbCfgSecParam $ ldbCfg env
284290

285291
implGetHeaderStateHistory ::
286292
( MonadSTM m
@@ -304,7 +310,10 @@ implGetHeaderStateHistory env = do
304310
pure
305311
. HeaderStateHistory
306312
. AS.bimap mkHeaderStateWithTime' mkHeaderStateWithTime'
307-
$ getLedgerSeq ldb
313+
. getLedgerSeq
314+
$ volatileSuffix k ldb
315+
where
316+
k = ledgerDbCfgSecParam $ ldbCfg env
308317

309318
implValidate ::
310319
forall m l blk.
@@ -557,7 +566,7 @@ acquireAtTarget ::
557566
LDBLock ->
558567
m (Either GetForkerError (StateRef m l))
559568
acquireAtTarget ldbEnv target _ = runExceptT $ do
560-
l <- lift $ readTVarIO (ldbSeq ldbEnv)
569+
l <- lift $ volatileSuffix k <$> readTVarIO (ldbSeq ldbEnv)
561570
StateRef st tbs <- case target of
562571
Right VolatileTip -> pure $ currentHandle l
563572
Right ImmutableTip -> pure $ anchorHandle l
@@ -580,6 +589,8 @@ acquireAtTarget ldbEnv target _ = runExceptT $ do
580589
Just l' -> pure $ currentHandle l'
581590
tbs' <- lift $ duplicate tbs
582591
pure $ StateRef st tbs'
592+
where
593+
k = ledgerDbCfgSecParam $ ldbCfg ldbEnv
583594

584595
newForkerAtTarget ::
585596
( HeaderHash l ~ HeaderHash blk

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V2/LedgerSeq.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module Ouroboros.Consensus.Storage.LedgerDB.V2.LedgerSeq
5252
, snapshots
5353
, tip
5454
, volatileStatesBimap
55+
, volatileSuffix
5556
) where
5657

5758
import Cardano.Ledger.BaseTypes
@@ -512,6 +513,13 @@ volatileStatesBimap f g =
512513
AS.bimap (f . state) (g . state)
513514
. getLedgerSeq
514515

516+
-- | Take the suffix containing the @k@ most recent states. The 'LedgerSeq' can
517+
-- contain more than @k@ states if we adopted new blocks, but garbage collection
518+
-- has not yet been run.
519+
volatileSuffix :: GetTip l => SecurityParam -> LedgerSeq m l -> LedgerSeq m l
520+
volatileSuffix (SecurityParam k) =
521+
LedgerSeq . AS.anchorNewest (unNonZero k) . getLedgerSeq
522+
515523
{-------------------------------------------------------------------------------
516524
docspec setup
517525
-------------------------------------------------------------------------------}

0 commit comments

Comments
 (0)