Skip to content

Commit 34b4660

Browse files
committed
Implement LSM-trees based ledger tables handles in LedgerDB V2
1 parent 99113e5 commit 34b4660

File tree

30 files changed

+903
-145
lines changed

30 files changed

+903
-145
lines changed

cabal.project

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ index-state:
1616
-- Bump this if you need newer packages from Hackage
1717
, hackage.haskell.org 2025-09-11T01:58:40Z
1818
-- Bump this if you need newer packages from CHaP
19-
, cardano-haskell-packages 2025-09-10T20:31:08Z
19+
, cardano-haskell-packages 2025-09-16T10:10:31Z
2020

2121
packages:
2222
ouroboros-consensus
@@ -49,3 +49,13 @@ if impl (ghc >= 9.12)
4949
allow-newer:
5050
-- https://github.com/kapralVV/Unique/issues/11
5151
, Unique:hashable
52+
53+
if impl (ghc >= 9.10)
54+
allow-newer:
55+
-- https://github.com/phadej/regression-simple/pull/14
56+
, regression-simple:base
57+
58+
-- source-repository-package
59+
-- type: git
60+
-- location: https://github.com/lehins/mempack
61+
-- tag: 2f2528780c715afd2f270447359c7cde632f2c49

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nix/haskell.nix

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,13 @@ in
8989
nativeBuildInputs = [
9090
final.fd
9191
final.cabal-docspec
92-
(hsPkgs.ghcWithPackages
93-
(ps: [ ps.latex-svg-image ] ++ lib.filter (p: p ? components.library)
94-
(lib.attrValues (haskell-nix.haskellLib.selectProjectPackages ps))))
92+
(hsPkgs.shellFor {
93+
withHoogle = false;
94+
exactDeps = true;
95+
packages = _: [ ];
96+
additional = (ps: [ ps.latex-svg-image ] ++ lib.filter (p: p ? components.library)
97+
(lib.attrValues (haskell-nix.haskellLib.selectProjectPackages ps)));
98+
}).ghc
9599
final.texliveFull
96100
];
97101

ouroboros-consensus-cardano/app/DBAnalyser/Parsers.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ parseDBAnalyserConfig =
4646
[ flag' V1InMem $
4747
mconcat
4848
[ long "v1-in-mem"
49-
, help "use v1 in-memory backing store"
49+
, help "use v1 in-memory backing store [deprecated]"
5050
]
5151
, flag' V1LMDB $
5252
mconcat
@@ -55,9 +55,14 @@ parseDBAnalyserConfig =
5555
]
5656
, flag' V2InMem $
5757
mconcat
58-
[ long "v2-in-mem"
58+
[ long "in-mem"
5959
, help "use v2 in-memory backend"
6060
]
61+
, flag' V2LSM $
62+
mconcat
63+
[ long "lsm"
64+
, help "use v2 LSM backend"
65+
]
6166
]
6267

6368
parseSelectDB :: Parser SelectDB

ouroboros-consensus-cardano/app/snapshot-converter.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.BackingStore.Impl.LMDB
3838
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.DbChangelog as V1
3939
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.Lock as V1
4040
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.Snapshots as V1
41+
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.InMemory as InMemory
4142
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.InMemory as V2
4243
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.LedgerSeq as V2
4344
import Ouroboros.Consensus.Util.CRC
@@ -199,7 +200,7 @@ load config@Config{inpath = pathToDiskSnapshot -> Just (fs@(SomeHasFS hasFS), pa
199200
checkSnapshotFileStructure Mem path fs
200201
(ls, _) <- withExceptT SnapshotError $ V2.loadSnapshot nullTracer rr ccfg fs ds
201202
let h = V2.currentHandle ls
202-
(V2.state h,) <$> Trans.lift (V2.readAll (V2.tables h))
203+
(V2.state h,) <$> Trans.lift (V2.readAll (V2.tables h) (V2.state h))
203204
LMDB -> do
204205
checkSnapshotFileStructure LMDB path fs
205206
((dbch, k, bstore), _) <-
@@ -240,7 +241,7 @@ store config@Config{outpath = pathToDiskSnapshot -> Just (fs@(SomeHasFS hasFS),
240241
Mem -> do
241242
lseq <- V2.empty state tbs $ V2.newInMemoryLedgerTablesHandle nullTracer fs
242243
let h = V2.currentHandle lseq
243-
Monad.void $ V2.implTakeSnapshot ccfg nullTracer fs suffix h
244+
Monad.void $ InMemory.implTakeSnapshot ccfg nullTracer fs suffix h
244245
LMDB -> do
245246
chlog <- newTVarIO (V1.empty state)
246247
lock <- V1.mkLedgerDBLock
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
For top level release notes, leave all the headers commented out.
6+
-->
7+
8+
<!--
9+
### Patch
10+
11+
- A bullet item for the Patch category.
12+
13+
-->
14+
<!--
15+
### Non-Breaking
16+
17+
- A bullet item for the Non-Breaking category.
18+
19+
-->
20+
<!--
21+
### Breaking
22+
23+
- A bullet item for the Breaking category.
24+
25+
-->

ouroboros-consensus-cardano/ouroboros-consensus-cardano.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ library unstable-cardano-tools
592592
ouroboros-network-api,
593593
ouroboros-network-framework ^>=0.19,
594594
ouroboros-network-protocols,
595+
random,
595596
resource-registry,
596597
singletons,
597598
sop-core,

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/ShelleyHFC.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
{-# LANGUAGE OverloadedStrings #-}
1111
{-# LANGUAGE RecordWildCards #-}
1212
{-# LANGUAGE ScopedTypeVariables #-}
13-
{-# LANGUAGE StandaloneDeriving #-}
1413
{-# LANGUAGE TypeApplications #-}
1514
{-# LANGUAGE TypeFamilies #-}
1615
{-# LANGUAGE TypeOperators #-}
@@ -429,16 +428,14 @@ instance
429428
{ getShelleyBlockHFCTxIn :: SL.TxIn
430429
}
431430
deriving stock (Show, Eq, Ord)
432-
deriving newtype NoThunks
431+
deriving newtype (NoThunks, MemPack)
433432

434433
injectCanonicalTxIn IZ txIn = ShelleyBlockHFCTxIn txIn
435434
injectCanonicalTxIn (IS idx') _ = case idx' of {}
436435

437436
ejectCanonicalTxIn IZ txIn = getShelleyBlockHFCTxIn txIn
438437
ejectCanonicalTxIn (IS idx') _ = case idx' of {}
439438

440-
deriving newtype instance MemPack (CanonicalTxIn '[ShelleyBlock proto era])
441-
442439
{-------------------------------------------------------------------------------
443440
HardForkTxOut
444441
-------------------------------------------------------------------------------}

ouroboros-consensus-cardano/src/unstable-cardano-testlib/Test/ThreadNet/Infra/ShelleyBasedHardFork.hs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
{-# LANGUAGE PatternSynonyms #-}
1313
{-# LANGUAGE RankNTypes #-}
1414
{-# LANGUAGE ScopedTypeVariables #-}
15-
{-# LANGUAGE StandaloneDeriving #-}
1615
{-# LANGUAGE TypeApplications #-}
1716
{-# LANGUAGE TypeFamilies #-}
1817
{-# LANGUAGE TypeOperators #-}
@@ -507,7 +506,7 @@ instance
507506
{ getShelleyHFCTxIn :: SL.TxIn
508507
}
509508
deriving stock (Show, Eq, Ord)
510-
deriving newtype NoThunks
509+
deriving newtype (NoThunks, MemPack)
511510

512511
injectCanonicalTxIn IZ txIn = ShelleyHFCTxIn txIn
513512
injectCanonicalTxIn (IS IZ) txIn = ShelleyHFCTxIn (coerce txIn)
@@ -517,10 +516,6 @@ instance
517516
ejectCanonicalTxIn (IS IZ) txIn = coerce (getShelleyHFCTxIn txIn)
518517
ejectCanonicalTxIn (IS (IS idx')) _ = case idx' of {}
519518

520-
deriving newtype instance
521-
ShelleyBasedHardForkConstraints proto1 era1 proto2 era2 =>
522-
MemPack (CanonicalTxIn (ShelleyBasedHardForkEras proto1 era1 proto2 era2))
523-
524519
instance
525520
ShelleyBasedHardForkConstraints proto1 era1 proto2 era2 =>
526521
HasHardForkTxOut (ShelleyBasedHardForkEras proto1 era1 proto2 era2)

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Run.hs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import Cardano.Tools.DBAnalyser.HasAnalysis
1515
import Cardano.Tools.DBAnalyser.Types
1616
import Control.ResourceRegistry
1717
import Control.Tracer (Tracer (..), nullTracer)
18+
import Data.Functor.Contravariant ((>$<))
1819
import qualified Data.SOP.Dict as Dict
1920
import Data.Singletons (Sing, SingI (..))
20-
import Data.Void
2121
import qualified Debug.Trace as Debug
2222
import Ouroboros.Consensus.Block
2323
import Ouroboros.Consensus.Config
@@ -35,19 +35,24 @@ import qualified Ouroboros.Consensus.Storage.ChainDB as ChainDB
3535
import qualified Ouroboros.Consensus.Storage.ChainDB.Impl.Args as ChainDB
3636
import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB
3737
import qualified Ouroboros.Consensus.Storage.ImmutableDB.Stream as ImmutableDB
38+
import Ouroboros.Consensus.Storage.LedgerDB (TraceEvent (..))
3839
import qualified Ouroboros.Consensus.Storage.LedgerDB as LedgerDB
3940
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1 as LedgerDB.V1
4041
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.Args as LedgerDB.V1
4142
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.BackingStore.Impl.LMDB as LMDB
42-
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.Snapshots as LedgerDB.V1
43+
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.Snapshots as V1
4344
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2 as LedgerDB.V2
4445
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.Args as LedgerDB.V2
46+
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.Args as V2
4547
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.InMemory as InMemory
48+
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.LSM as LSM
4649
import Ouroboros.Consensus.Util.Args
4750
import Ouroboros.Consensus.Util.IOLike
4851
import Ouroboros.Consensus.Util.Orphans ()
4952
import Ouroboros.Network.Block (genesisPoint)
53+
import System.FS.API
5054
import System.IO
55+
import System.Random
5156
import Text.Printf (printf)
5257

5358
{-------------------------------------------------------------------------------
@@ -66,7 +71,7 @@ openLedgerDB ::
6671
, LedgerDB.TestInternals' IO blk
6772
)
6873
openLedgerDB lgrDbArgs@LedgerDB.LedgerDbArgs{LedgerDB.lgrFlavorArgs = LedgerDB.LedgerDbFlavorArgsV1 bss} = do
69-
let snapManager = LedgerDB.V1.snapshotManager lgrDbArgs
74+
let snapManager = V1.snapshotManager lgrDbArgs
7075
(ledgerDB, _, intLedgerDB) <-
7176
LedgerDB.openDBInternal
7277
lgrDbArgs
@@ -83,8 +88,27 @@ openLedgerDB [email protected]{LedgerDB.lgrFlavorArgs = LedgerDB.L
8388
pure (ledgerDB, intLedgerDB)
8489
openLedgerDB lgrDbArgs@LedgerDB.LedgerDbArgs{LedgerDB.lgrFlavorArgs = LedgerDB.LedgerDbFlavorArgsV2 args} = do
8590
(snapManager, bss') <- case args of
86-
LedgerDB.V2.V2Args LedgerDB.V2.InMemoryHandleArgs -> pure (InMemory.snapshotManager lgrDbArgs, LedgerDB.V2.InMemoryHandleEnv)
87-
LedgerDB.V2.V2Args (LedgerDB.V2.LSMHandleArgs (LedgerDB.V2.LSMArgs x)) -> absurd x
91+
V2.V2Args V2.InMemoryHandleArgs -> pure (InMemory.snapshotManager lgrDbArgs, V2.InMemoryHandleEnv)
92+
V2.V2Args (V2.LSMHandleArgs (V2.LSMArgs path salt mkFS)) -> do
93+
(rk1, V2.SomeHasFSAndBlockIO fs' blockio) <- mkFS (LedgerDB.lgrRegistry lgrDbArgs)
94+
session <-
95+
allocate
96+
(LedgerDB.lgrRegistry lgrDbArgs)
97+
( \_ ->
98+
LSM.openSession
99+
( LedgerDBFlavorImplEvent . LedgerDB.FlavorImplSpecificTraceV2 . V2.LSMTrace
100+
>$< LedgerDB.lgrTracer lgrDbArgs
101+
)
102+
fs'
103+
blockio
104+
salt
105+
path
106+
)
107+
LSM.closeSession
108+
pure
109+
( LSM.snapshotManager (snd session) lgrDbArgs
110+
, V2.LSMHandleEnv (V2.LSMResources (fst session) (snd session) rk1)
111+
)
88112
(ledgerDB, _, intLedgerDB) <-
89113
LedgerDB.openDBInternal
90114
lgrDbArgs
@@ -128,6 +152,7 @@ analyse dbaConfig args =
128152
lock <- newMVar ()
129153
chainDBTracer <- mkTracer lock verbose
130154
analysisTracer <- mkTracer lock True
155+
lsmSalt <- fst . genWord64 <$> newStdGen
131156
ProtocolInfo{pInfoInitLedger = genesisLedger, pInfoConfig = cfg} <-
132157
mkProtocolInfo args
133158
let shfs = Node.stdMkChainDbHasFS dbDir
@@ -152,6 +177,13 @@ analyse dbaConfig args =
152177
V2InMem ->
153178
LedgerDB.LedgerDbFlavorArgsV2
154179
(LedgerDB.V2.V2Args LedgerDB.V2.InMemoryHandleArgs)
180+
V2LSM ->
181+
LedgerDB.LedgerDbFlavorArgsV2
182+
( LedgerDB.V2.V2Args
183+
( LedgerDB.V2.LSMHandleArgs
184+
(LedgerDB.V2.LSMArgs (mkFsPath ["lsm"]) lsmSalt (LSM.stdMkBlockIOFS dbDir))
185+
)
186+
)
155187
args' =
156188
ChainDB.completeChainDbArgs
157189
registry

0 commit comments

Comments
 (0)