Skip to content

Commit c3a08d1

Browse files
committed
LedgerDB.V2.TestInternals: prune LedgerSeq
This is used in db-analyser only, where everything happens synchronously in a single thread, so it is fine to immediately prune. V1 already does this.
1 parent 11a32d0 commit c3a08d1

File tree

2 files changed

+23
-3
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB

2 files changed

+23
-3
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,14 @@ data TestInternals m l blk = TestInternals
305305
{ wipeLedgerDB :: m ()
306306
, takeSnapshotNOW :: WhereToTakeSnapshot -> Maybe String -> m ()
307307
, push :: ExtLedgerState blk DiffMK -> m ()
308+
-- ^ Push a ledger state, and prune the 'LedgerDB' w.r.t. the security parameter.
309+
--
310+
-- This does not modify the set of previously applied points.
308311
, reapplyThenPushNOW :: blk -> m ()
312+
-- ^ Apply block to the tip ledger state (using reapplication), and prune the
313+
-- 'LedgerDB' w.r.t. the security parameter.
314+
--
315+
-- This does not modify the set of previously applied points.
309316
, truncateSnapshots :: m ()
310317
, closeLedgerDB :: m ()
311318
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE BangPatterns #-}
12
{-# LANGUAGE CPP #-}
23
{-# LANGUAGE DataKinds #-}
34
{-# LANGUAGE DeriveAnyClass #-}
@@ -35,6 +36,7 @@ import Data.Set (Set)
3536
import qualified Data.Set as Set
3637
import Data.Void
3738
import Data.Word
39+
import qualified Debug.Trace as DT
3840
import GHC.Generics
3941
import NoThunks.Class
4042
import Ouroboros.Consensus.Block
@@ -210,8 +212,9 @@ mkInternals bss h =
210212
eFrk <- newForkerAtTarget h reg VolatileTip
211213
case eFrk of
212214
Left{} -> error "Unreachable, Volatile tip MUST be in LedgerDB"
213-
Right frk ->
215+
Right frk -> do
214216
forkerPush frk st >> atomically (forkerCommit frk) >> forkerClose frk
217+
getEnv h pruneLedgerSeq
215218
, reapplyThenPushNOW = \blk -> getEnv h $ \env -> withRegistry $ \reg -> do
216219
eFrk <- newForkerAtTarget h reg VolatileTip
217220
case eFrk of
@@ -226,6 +229,7 @@ mkInternals bss h =
226229
blk
227230
(st `withLedgerTables` tables)
228231
forkerPush frk st' >> atomically (forkerCommit frk) >> forkerClose frk
232+
pruneLedgerSeq env
229233
, wipeLedgerDB = getEnv h $ destroySnapshots . ldbHasFS
230234
, closeLedgerDB =
231235
let LDBHandle tvar = h
@@ -244,6 +248,12 @@ mkInternals bss h =
244248
InMemoryHandleArgs -> InMemory.takeSnapshot
245249
LSMHandleArgs x -> absurd x
246250

251+
pruneLedgerSeq :: LedgerDBEnv m (ExtLedgerState blk) blk -> m ()
252+
pruneLedgerSeq env =
253+
join $ atomically $ stateTVar (ldbSeq env) $ prune (LedgerDbPruneKeeping k)
254+
where
255+
k = ledgerDbCfgSecParam $ ldbCfg env
256+
247257
-- | Testing only! Truncate all snapshots in the DB.
248258
implIntTruncateSnapshots :: MonadThrow m => SomeHasFS m -> m ()
249259
implIntTruncateSnapshots sfs@(SomeHasFS fs) = do
@@ -412,8 +422,11 @@ implTryTakeSnapshot bss env mTime nrBlocks =
412422
-- periodically flush the accumulated differences to the disk. However, in the
413423
-- second version there is no need to do so, and because of that, this function
414424
-- does nothing in this case.
415-
implTryFlush :: Applicative m => LedgerDBEnv m l blk -> m ()
416-
implTryFlush _ = pure ()
425+
implTryFlush :: (MonadSTM m, GetTip l) => LedgerDBEnv m l blk -> m ()
426+
implTryFlush env = do
427+
LedgerSeq ldb <- readTVarIO $ ldbSeq env
428+
!_ <- DT.traceM $ "ledger seq len: " <> show (AS.length ldb)
429+
pure ()
417430

418431
implCloseDB :: IOLike m => LedgerDBHandle m l blk -> m ()
419432
implCloseDB (LDBHandle varState) = do

0 commit comments

Comments
 (0)