Skip to content

Commit 7dbfc68

Browse files
committed
Implement LSM-trees based ledger tables handles in LedgerDB V2
1 parent 05bd678 commit 7dbfc68

File tree

30 files changed

+903
-140
lines changed

30 files changed

+903
-140
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ package, we should release it to CHaP instead (see the
336336
[CHaP README](https://github.com/IntersectMBO/cardano-haskell-packages)
337337
for more).
338338

339-
In general, we strive to avoid having `source-repository-package`s on our `main` branch. However, there are situations where we want to prevent pull requests from piling up while awaiting the release of upstream components[^1].
339+
In general, we strive to avoid having `source-repository-package`s on our `main` branch. However, there are situations where we want to prevent pull requests from piling up while awaiting the release of upstream components[^1].
340340
In these cases, we allow merging pull requests that contain `source-repository-package`s, provided the referenced commit is on the `main` branch of the upstream package.
341341

342342
If you do add a temporary `source-repository-package` stanza, you need to

cabal.project

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ repository cardano-haskell-packages
1414
-- update either of these.
1515
index-state:
1616
-- Bump this if you need newer packages from Hackage
17-
, hackage.haskell.org 2025-07-22T09:13:54Z
17+
, hackage.haskell.org 2025-08-07T11:24:08Z
1818
-- Bump this if you need newer packages from CHaP
1919
, cardano-haskell-packages 2025-08-21T09:41:03Z
2020

@@ -56,6 +56,11 @@ allow-newer:
5656
, fin:QuickCheck
5757
, bin:QuickCheck
5858

59+
if impl (ghc >= 9.10)
60+
allow-newer:
61+
-- https://github.com/phadej/regression-simple/pull/14
62+
, regression-simple:base
63+
5964
source-repository-package
6065
type: git
6166
location: https://github.com/IntersectMBO/cardano-ledger
@@ -90,3 +95,10 @@ constraints:
9095
plutus-core < 1.51,
9196
plutus-ledger-api < 1.51,
9297
plutus-tx < 1.51,
98+
99+
if os (windows)
100+
source-repository-package
101+
type: git
102+
location: https://github.com/jasagredo/digest
103+
tag: 329fc2a911878ffe47472751cb40aae20ab2c00a
104+
--sha256: sha256-84f8dFee9EfWbQ5UTLZ9MrsZ3JVojNhzfTGmWof6wHU=

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
@@ -588,6 +588,7 @@ library unstable-cardano-tools
588588
ouroboros-network-api,
589589
ouroboros-network-framework ^>=0.19,
590590
ouroboros-network-protocols,
591+
random,
591592
resource-registry,
592593
singletons,
593594
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 #-}
@@ -498,7 +497,7 @@ instance
498497
{ getShelleyHFCTxIn :: SL.TxIn
499498
}
500499
deriving stock (Show, Eq, Ord)
501-
deriving newtype NoThunks
500+
deriving newtype (NoThunks, MemPack)
502501

503502
injectCanonicalTxIn IZ txIn = ShelleyHFCTxIn txIn
504503
injectCanonicalTxIn (IS IZ) txIn = ShelleyHFCTxIn (coerce txIn)
@@ -508,10 +507,6 @@ instance
508507
ejectCanonicalTxIn (IS IZ) txIn = coerce (getShelleyHFCTxIn txIn)
509508
ejectCanonicalTxIn (IS (IS idx')) _ = case idx' of {}
510509

511-
deriving newtype instance
512-
ShelleyBasedHardForkConstraints proto1 era1 proto2 era2 =>
513-
MemPack (CanonicalTxIn (ShelleyBasedHardForkEras proto1 era1 proto2 era2))
514-
515510
instance
516511
ShelleyBasedHardForkConstraints proto1 era1 proto2 era2 =>
517512
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
@@ -82,8 +87,27 @@ openLedgerDB [email protected]{LedgerDB.lgrFlavorArgs = LedgerDB.L
8287
pure (ledgerDB, intLedgerDB)
8388
openLedgerDB lgrDbArgs@LedgerDB.LedgerDbArgs{LedgerDB.lgrFlavorArgs = LedgerDB.LedgerDbFlavorArgsV2 args} = do
8489
(snapManager, bss') <- case args of
85-
LedgerDB.V2.V2Args LedgerDB.V2.InMemoryHandleArgs -> pure (InMemory.snapshotManager lgrDbArgs, LedgerDB.V2.InMemoryHandleEnv)
86-
LedgerDB.V2.V2Args (LedgerDB.V2.LSMHandleArgs (LedgerDB.V2.LSMArgs x)) -> absurd x
90+
V2.V2Args V2.InMemoryHandleArgs -> pure (InMemory.snapshotManager lgrDbArgs, V2.InMemoryHandleEnv)
91+
V2.V2Args (V2.LSMHandleArgs (V2.LSMArgs path salt mkFS)) -> do
92+
(rk1, V2.SomeHasFSAndBlockIO fs' blockio) <- mkFS (LedgerDB.lgrRegistry lgrDbArgs)
93+
session <-
94+
allocate
95+
(LedgerDB.lgrRegistry lgrDbArgs)
96+
( \_ ->
97+
LSM.openSession
98+
( LedgerDBFlavorImplEvent . LedgerDB.FlavorImplSpecificTraceV2 . V2.LSMTrace
99+
>$< LedgerDB.lgrTracer lgrDbArgs
100+
)
101+
fs'
102+
blockio
103+
salt
104+
path
105+
)
106+
LSM.closeSession
107+
pure
108+
( LSM.snapshotManager (snd session) lgrDbArgs
109+
, V2.LSMHandleEnv (V2.LSMResources (fst session) (snd session) rk1)
110+
)
87111
(ledgerDB, _, intLedgerDB) <-
88112
LedgerDB.openDBInternal
89113
lgrDbArgs
@@ -126,6 +150,7 @@ analyse dbaConfig args =
126150
lock <- newMVar ()
127151
chainDBTracer <- mkTracer lock verbose
128152
analysisTracer <- mkTracer lock True
153+
lsmSalt <- fst . genWord64 <$> newStdGen
129154
ProtocolInfo{pInfoInitLedger = genesisLedger, pInfoConfig = cfg} <-
130155
mkProtocolInfo args
131156
let shfs = Node.stdMkChainDbHasFS dbDir
@@ -150,6 +175,13 @@ analyse dbaConfig args =
150175
V2InMem ->
151176
LedgerDB.LedgerDbFlavorArgsV2
152177
(LedgerDB.V2.V2Args LedgerDB.V2.InMemoryHandleArgs)
178+
V2LSM ->
179+
LedgerDB.LedgerDbFlavorArgsV2
180+
( LedgerDB.V2.V2Args
181+
( LedgerDB.V2.LSMHandleArgs
182+
(LedgerDB.V2.LSMArgs (mkFsPath ["lsm"]) lsmSalt (LSM.stdMkBlockIOFS dbDir))
183+
)
184+
)
153185
args' =
154186
ChainDB.completeChainDbArgs
155187
registry

0 commit comments

Comments
 (0)