Skip to content

Commit 2037a0f

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 045f1cc commit 2037a0f

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
@@ -299,7 +299,14 @@ data TestInternals m l blk = TestInternals
299299
{ wipeLedgerDB :: m ()
300300
, takeSnapshotNOW :: WhereToTakeSnapshot -> Maybe String -> m ()
301301
, push :: ExtLedgerState blk DiffMK -> m ()
302+
-- ^ Push a ledger state, and prune the 'LedgerDB' w.r.t. the security parameter.
303+
--
304+
-- This does not modify the set of previously applied points.
302305
, reapplyThenPushNOW :: blk -> m ()
306+
-- ^ Apply block to the tip ledger state (using reapplication), and prune the
307+
-- 'LedgerDB' w.r.t. the security parameter.
308+
--
309+
-- This does not modify the set of previously applied points.
303310
, truncateSnapshots :: m ()
304311
, closeLedgerDB :: m ()
305312
}

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
@@ -209,8 +211,9 @@ mkInternals bss h =
209211
eFrk <- newForkerAtTarget h reg VolatileTip
210212
case eFrk of
211213
Left{} -> error "Unreachable, Volatile tip MUST be in LedgerDB"
212-
Right frk ->
214+
Right frk -> do
213215
forkerPush frk st >> atomically (forkerCommit frk) >> forkerClose frk
216+
getEnv h pruneLedgerSeq
214217
, reapplyThenPushNOW = \blk -> getEnv h $ \env -> withRegistry $ \reg -> do
215218
eFrk <- newForkerAtTarget h reg VolatileTip
216219
case eFrk of
@@ -225,6 +228,7 @@ mkInternals bss h =
225228
blk
226229
(st `withLedgerTables` tables)
227230
forkerPush frk st' >> atomically (forkerCommit frk) >> forkerClose frk
231+
pruneLedgerSeq env
228232
, wipeLedgerDB = getEnv h $ destroySnapshots . ldbHasFS
229233
, closeLedgerDB =
230234
let LDBHandle tvar = h
@@ -243,6 +247,12 @@ mkInternals bss h =
243247
InMemoryHandleArgs -> InMemory.takeSnapshot
244248
LSMHandleArgs x -> absurd x
245249

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

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

0 commit comments

Comments
 (0)