@@ -44,6 +44,7 @@ import Control.Monad.Except
4444import Control.Monad.State hiding (state )
4545import Control.ResourceRegistry
4646import Control.Tracer (Tracer (.. ))
47+ import Data.Functor.Contravariant ((>$<) )
4748import qualified Data.List as L
4849import Data.Map.Strict (Map )
4950import qualified Data.Map.Strict as Map
@@ -63,11 +64,14 @@ import Ouroboros.Consensus.Storage.LedgerDB.V1 as V1
6364import Ouroboros.Consensus.Storage.LedgerDB.V1.Args hiding
6465 ( LedgerDbFlavorArgs
6566 )
67+ import Ouroboros.Consensus.Storage.LedgerDB.V1.Snapshots as V1
6668import Ouroboros.Consensus.Storage.LedgerDB.V2 as V2
6769import Ouroboros.Consensus.Storage.LedgerDB.V2.Args hiding
6870 ( LedgerDbFlavorArgs
6971 )
7072import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.Args as V2
73+ import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.InMemory as InMemory
74+ import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.LSM as LSM
7175import Ouroboros.Consensus.Util hiding (Some )
7276import Ouroboros.Consensus.Util.Args
7377import Ouroboros.Consensus.Util.IOLike
@@ -102,7 +106,9 @@ tests =
102106 , testProperty " InMemV2" $
103107 prop_sequential 100000 inMemV2TestArguments noFilePath simulatedFS
104108 , testProperty " LMDB" $
105- prop_sequential 1000 lmdbTestArguments realFilePath realFS
109+ prop_sequential 1000 lmdbTestArguments (realFilePath " lmdb" ) realFS
110+ , testProperty " LSM" $
111+ prop_sequential 1000 lsmTestArguments (realFilePath " lsm" ) realFS
106112 ]
107113
108114prop_sequential ::
@@ -156,9 +162,10 @@ data TestArguments m = TestArguments
156162noFilePath :: IO (FilePath , IO () )
157163noFilePath = pure (" Bogus" , pure () )
158164
159- realFilePath :: IO (FilePath , IO () )
160- realFilePath = liftIO $ do
161- tmpdir <- (FilePath. </> " test_lmdb" ) <$> Dir. getTemporaryDirectory
165+ realFilePath :: String -> IO (FilePath , IO () )
166+ realFilePath l = liftIO $ do
167+ tmpdir <- (FilePath. </> (" test_" <> l)) <$> Dir. getTemporaryDirectory
168+ Dir. createDirectoryIfMissing False tmpdir
162169 pure
163170 ( tmpdir
164171 , do
@@ -197,6 +204,17 @@ inMemV2TestArguments secParam _ =
197204 , argLedgerDbCfg = extLedgerDbConfig secParam
198205 }
199206
207+ lsmTestArguments ::
208+ SecurityParam ->
209+ FilePath ->
210+ TestArguments IO
211+ lsmTestArguments secParam fp =
212+ TestArguments
213+ { argFlavorArgs =
214+ LedgerDbFlavorArgsV2 $ V2Args $ LSMHandleArgs $ LSMArgs fp LSM. stdGenSalt (LSM. stdMkBlockIOFS fp)
215+ , argLedgerDbCfg = extLedgerDbConfig secParam
216+ }
217+
200218lmdbTestArguments ::
201219 SecurityParam ->
202220 FilePath ->
@@ -490,19 +508,40 @@ openLedgerDB flavArgs env cfg fs = do
490508 Nothing
491509 (ldb, _, od) <- case flavArgs of
492510 LedgerDbFlavorArgsV1 bss ->
493- let initDb =
511+ let snapManager = V1. snapshotManager args
512+ initDb =
494513 V1. mkInitDb
495514 args
496515 bss
497516 getBlock
498- in openDBInternal args initDb stream replayGoal
499- LedgerDbFlavorArgsV2 bss ->
517+ snapManager
518+ in openDBInternal args initDb snapManager stream replayGoal
519+ LedgerDbFlavorArgsV2 bss -> do
520+ (snapManager, bss') <- case bss of
521+ V2. V2Args V2. InMemoryHandleArgs -> pure (InMemory. snapshotManager args, V2. InMemoryHandleEnv )
522+ V2. V2Args (V2. LSMHandleArgs (V2. LSMArgs path genSalt mkFS)) -> do
523+ (rk1, V2. SomeHasFSAndBlockIO fs' blockio) <- mkFS (lgrRegistry args)
524+ session <-
525+ allocate
526+ (lgrRegistry args)
527+ ( \ _ -> do
528+ salt <- genSalt
529+ LSM. openSession
530+ (LedgerDBFlavorImplEvent . FlavorImplSpecificTraceV2 . V2. LSMTrace >$< lgrTracer args)
531+ fs'
532+ blockio
533+ salt
534+ (mkFsPath [path])
535+ )
536+ LSM. closeSession
537+ pure (LSM. snapshotManager (snd session) args, V2. LSMHandleEnv session rk1)
500538 let initDb =
501539 V2. mkInitDb
502540 args
503- bss
541+ bss'
504542 getBlock
505- in openDBInternal args initDb stream replayGoal
543+ snapManager
544+ openDBInternal args initDb snapManager stream replayGoal
506545 withRegistry $ \ reg -> do
507546 vr <- validateFork ldb reg (const $ pure () ) BlockCache. empty 0 (map getHeader volBlocks)
508547 case vr of
@@ -617,6 +656,7 @@ mkTrackOpenHandles = do
617656 atomically $ modifyTVar varOpen $ case ev of
618657 V2. TraceLedgerTablesHandleCreate -> succ
619658 V2. TraceLedgerTablesHandleClose -> pred
659+ _ -> id
620660 _ -> pure ()
621661 pure (tracer, readTVarIO varOpen)
622662
0 commit comments