Skip to content

Commit e48572a

Browse files
committed
Modify abstract interface for LedgerDB/ChainDB for sublibraries
1 parent e1df723 commit e48572a

File tree

7 files changed

+113
-462
lines changed

7 files changed

+113
-462
lines changed

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/Impl/Args.hs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Ouroboros.Consensus.Block
2929
import Ouroboros.Consensus.Config
3030
import Ouroboros.Consensus.Ledger.Abstract
3131
import Ouroboros.Consensus.Ledger.Extended
32+
import Ouroboros.Consensus.Ledger.SupportsProtocol
3233
import Ouroboros.Consensus.Protocol.Abstract
3334
import Ouroboros.Consensus.Storage.ChainDB.API
3435
( GetLoEFragment
@@ -38,9 +39,11 @@ import Ouroboros.Consensus.Storage.ChainDB.Impl.Types
3839
( TraceEvent (..)
3940
)
4041
import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB
41-
import Ouroboros.Consensus.Storage.LedgerDB (LedgerDbFlavorArgs)
42+
import Ouroboros.Consensus.Storage.LedgerDB (LedgerDbBackendArgs)
4243
import qualified Ouroboros.Consensus.Storage.LedgerDB as LedgerDB
4344
import Ouroboros.Consensus.Storage.LedgerDB.Snapshots
45+
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.Backend as LedgerDB
46+
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.InMemory as InMemory
4447
import qualified Ouroboros.Consensus.Storage.VolatileDB as VolatileDB
4548
import Ouroboros.Consensus.Util.Args
4649
import Ouroboros.Consensus.Util.IOLike
@@ -131,13 +134,17 @@ defaultSpecificArgs =
131134
-- and must therefore be set explicitly.
132135
defaultArgs ::
133136
forall m blk.
134-
Monad m =>
137+
( IOLike m
138+
, LedgerDB.LedgerDbSerialiseConstraints blk
139+
, LedgerSupportsProtocol blk
140+
, LedgerDB.LedgerSupportsInMemoryLedgerDB (LedgerState blk)
141+
) =>
135142
Incomplete ChainDbArgs m blk
136143
defaultArgs =
137144
ChainDbArgs
138145
ImmutableDB.defaultArgs
139146
VolatileDB.defaultArgs
140-
LedgerDB.defaultArgs
147+
(LedgerDB.defaultArgs $ LedgerDB.SomeBackendArgs InMemory.InMemArgs)
141148
defaultSpecificArgs
142149

143150
ensureValidateAll ::
@@ -169,7 +176,7 @@ completeChainDbArgs ::
169176
(RelativeMountPoint -> SomeHasFS m) ->
170177
-- | Volatile FS, see 'NodeDatabasePaths'
171178
(RelativeMountPoint -> SomeHasFS m) ->
172-
Complete LedgerDbFlavorArgs m ->
179+
LedgerDbBackendArgs m blk ->
173180
-- | A set of incomplete arguments, possibly modified wrt @defaultArgs@
174181
Incomplete ChainDbArgs m blk ->
175182
Complete ChainDbArgs m blk
@@ -206,7 +213,7 @@ completeChainDbArgs
206213
LedgerDB.configLedgerDb
207214
cdbsTopLevelConfig
208215
(LedgerDB.ledgerDbCfgComputeLedgerEvents $ LedgerDB.lgrConfig (cdbLgrDbArgs defArgs))
209-
, LedgerDB.lgrFlavorArgs = flavorArgs
216+
, LedgerDB.lgrBackendArgs = flavorArgs
210217
, LedgerDB.lgrRegistry = registry
211218
}
212219
, cdbsArgs =

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/Impl/ChainSel.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ chainSelection chainSelEnv rr chainDiffs =
10941094
-- Just in case, explicitly yield to ensure that a capability (by
10951095
-- default, the node uses just two) has the opportunity to switch
10961096
-- to a ChainSync server thread.
1097-
yield
1097+
Ouroboros.Consensus.Util.IOLike.yield
10981098
pure pipeliningResult
10991099

11001100
-- \| Clear a tentative header that turned out to be invalid. Also, roll

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

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{-# LANGUAGE FlexibleContexts #-}
2-
{-# LANGUAGE GADTs #-}
3-
{-# LANGUAGE KindSignatures #-}
42
{-# LANGUAGE NamedFieldPuns #-}
5-
{-# LANGUAGE RankNTypes #-}
63
{-# LANGUAGE ScopedTypeVariables #-}
4+
{-# LANGUAGE TypeApplications #-}
75

86
module Ouroboros.Consensus.Storage.LedgerDB
97
( -- * API
@@ -17,11 +15,12 @@ module Ouroboros.Consensus.Storage.LedgerDB
1715
, openDBInternal
1816
) where
1917

20-
import Control.ResourceRegistry
2118
import Data.Functor.Contravariant ((>$<))
2219
import Data.Word
2320
import Ouroboros.Consensus.Block
21+
import Ouroboros.Consensus.Config
2422
import Ouroboros.Consensus.HardFork.Abstract
23+
import Ouroboros.Consensus.Ledger.Extended
2524
import Ouroboros.Consensus.Ledger.Inspect
2625
import Ouroboros.Consensus.Ledger.SupportsProtocol
2726
import Ouroboros.Consensus.Storage.ImmutableDB.Stream
@@ -33,9 +32,7 @@ import Ouroboros.Consensus.Storage.LedgerDB.TraceEvent
3332
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1 as V1
3433
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.Snapshots as V1
3534
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2 as V2
36-
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.Args as V2
37-
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.InMemory as InMemory
38-
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.LSM as LSM
35+
import Ouroboros.Consensus.Storage.LedgerDB.V2.Backend
3936
import Ouroboros.Consensus.Util.Args
4037
import Ouroboros.Consensus.Util.CallStack
4138
import Ouroboros.Consensus.Util.IOLike
@@ -70,46 +67,35 @@ openDB
7067
stream
7168
replayGoal
7269
getBlock
73-
getVolatileSuffix = case lgrFlavorArgs args of
74-
LedgerDbFlavorArgsV1 bss ->
75-
let snapManager = V1.snapshotManager args
76-
initDb =
77-
V1.mkInitDb
78-
args
79-
bss
80-
getBlock
81-
snapManager
82-
getVolatileSuffix
83-
in doOpenDB args initDb snapManager stream replayGoal
84-
LedgerDbFlavorArgsV2 bss -> do
85-
(snapManager, bss') <- case bss of
86-
V2.V2Args V2.InMemoryHandleArgs -> pure (InMemory.snapshotManager args, V2.InMemoryHandleEnv)
87-
V2.V2Args (V2.LSMHandleArgs (V2.LSMArgs path salt mkFS)) -> do
88-
(rk1, V2.SomeHasFSAndBlockIO fs blockio) <- mkFS (lgrRegistry args)
89-
session <-
90-
allocate
91-
(lgrRegistry args)
92-
( \_ ->
93-
LSM.openSession
94-
(LedgerDBFlavorImplEvent . FlavorImplSpecificTraceV2 . V2.LSMTrace >$< lgrTracer args)
95-
fs
96-
blockio
97-
salt
98-
path
99-
)
100-
LSM.closeSession
101-
pure
102-
( LSM.snapshotManager (snd session) args
103-
, V2.LSMHandleEnv (V2.LSMResources (fst session) (snd session) rk1)
104-
)
105-
let initDb =
106-
V2.mkInitDb
107-
args
108-
bss'
109-
getBlock
110-
snapManager
111-
getVolatileSuffix
112-
doOpenDB args initDb snapManager stream replayGoal
70+
getVolatileSuffix =
71+
case lgrBackendArgs args of
72+
LedgerDbBackendArgsV1 bss ->
73+
let snapManager = V1.snapshotManager args
74+
initDb =
75+
V1.mkInitDb
76+
args
77+
bss
78+
getBlock
79+
snapManager
80+
getVolatileSuffix
81+
in doOpenDB args initDb snapManager stream replayGoal
82+
LedgerDbBackendArgsV2 (SomeBackendArgs bArgs) -> do
83+
res <-
84+
mkResources
85+
(Proxy @blk)
86+
(LedgerDBFlavorImplEvent . FlavorImplSpecificTraceV2 >$< lgrTracer args)
87+
bArgs
88+
(lgrRegistry args)
89+
(lgrHasFS args)
90+
let snapManager =
91+
snapshotManager
92+
(Proxy @blk)
93+
res
94+
(configCodec . getExtLedgerCfg . ledgerDbCfg $ lgrConfig args)
95+
(LedgerDBSnapshotEvent >$< lgrTracer args)
96+
(lgrHasFS args)
97+
let initDb = V2.mkInitDb args getBlock snapManager getVolatileSuffix res
98+
doOpenDB args initDb snapManager stream replayGoal
11399

114100
{-------------------------------------------------------------------------------
115101
Opening a LedgerDB

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66
{-# LANGUAGE DerivingVia #-}
77
{-# LANGUAGE FlexibleContexts #-}
88
{-# LANGUAGE FlexibleInstances #-}
9-
{-# LANGUAGE GADTs #-}
10-
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
119
{-# LANGUAGE MultiParamTypeClasses #-}
1210
{-# LANGUAGE NamedFieldPuns #-}
13-
{-# LANGUAGE PolyKinds #-}
14-
{-# LANGUAGE QuantifiedConstraints #-}
1511
{-# LANGUAGE RankNTypes #-}
1612
{-# LANGUAGE ScopedTypeVariables #-}
1713
{-# LANGUAGE StandaloneDeriving #-}
@@ -154,22 +150,32 @@ module Ouroboros.Consensus.Storage.LedgerDB.API
154150
-- * Snapshots
155151
, SnapCounters (..)
156152

153+
-- * Streaming
154+
, StreamingBackend (..)
155+
, Yield
156+
, Sink
157+
, Decoders (..)
158+
157159
-- * Testing
158160
, TestInternals (..)
159161
, TestInternals'
160162
, WhereToTakeSnapshot (..)
161163
) where
162164

165+
import Codec.CBOR.Decoding
166+
import Codec.CBOR.Read
163167
import Codec.Serialise
164168
import qualified Control.Monad as Monad
165169
import Control.Monad.Class.MonadTime.SI
166170
import Control.Monad.Except
167171
import Control.ResourceRegistry
168172
import Control.Tracer
173+
import Data.ByteString (ByteString)
169174
import Data.Functor.Contravariant ((>$<))
170175
import Data.Kind
171176
import qualified Data.Map.Strict as Map
172177
import Data.MemPack
178+
import Data.Proxy
173179
import Data.Set (Set)
174180
import Data.Void (absurd)
175181
import Data.Word
@@ -195,6 +201,8 @@ import Ouroboros.Consensus.Util.IOLike
195201
import Ouroboros.Consensus.Util.IndexedMemPack
196202
import Ouroboros.Network.Block
197203
import Ouroboros.Network.Protocol.LocalStateQuery.Type
204+
import Streaming
205+
import System.FS.CRC
198206

199207
{-------------------------------------------------------------------------------
200208
Main API
@@ -797,3 +805,41 @@ data LedgerDbPrune
797805
-- slot.
798806
LedgerDbPruneBeforeSlot SlotNo
799807
deriving Show
808+
809+
{-------------------------------------------------------------------------------
810+
Streaming
811+
-------------------------------------------------------------------------------}
812+
813+
-- | A backend that supports streaming the ledger tables
814+
class StreamingBackend m backend l where
815+
data YieldArgs m backend l
816+
817+
data SinkArgs m backend l
818+
819+
yield :: Proxy backend -> YieldArgs m backend l -> Yield m l
820+
821+
sink :: Proxy backend -> SinkArgs m backend l -> Sink m l
822+
823+
type Yield m l =
824+
l EmptyMK ->
825+
( ( Stream
826+
(Of (TxIn l, TxOut l))
827+
(ExceptT DeserialiseFailure m)
828+
(Stream (Of ByteString) m (Maybe CRC)) ->
829+
ExceptT DeserialiseFailure m (Stream (Of ByteString) m (Maybe CRC, Maybe CRC))
830+
)
831+
) ->
832+
ExceptT DeserialiseFailure m (Maybe CRC, Maybe CRC)
833+
834+
type Sink m l =
835+
l EmptyMK ->
836+
Stream
837+
(Of (TxIn l, TxOut l))
838+
(ExceptT DeserialiseFailure m)
839+
(Stream (Of ByteString) m (Maybe CRC)) ->
840+
ExceptT DeserialiseFailure m (Stream (Of ByteString) m (Maybe CRC, Maybe CRC))
841+
842+
data Decoders l
843+
= Decoders
844+
(forall s. Decoder s (TxIn l))
845+
(forall s. Decoder s (TxOut l))

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

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1-
{-# LANGUAGE ConstraintKinds #-}
21
{-# LANGUAGE DataKinds #-}
32
{-# LANGUAGE DeriveAnyClass #-}
43
{-# LANGUAGE DeriveGeneric #-}
54
{-# LANGUAGE DerivingVia #-}
65
{-# LANGUAGE FlexibleContexts #-}
7-
{-# LANGUAGE FlexibleInstances #-}
8-
{-# LANGUAGE GADTs #-}
96
{-# LANGUAGE NumericUnderscores #-}
10-
{-# LANGUAGE PolyKinds #-}
11-
{-# LANGUAGE QuantifiedConstraints #-}
127
{-# LANGUAGE RankNTypes #-}
13-
{-# LANGUAGE ScopedTypeVariables #-}
148
{-# LANGUAGE StandaloneKindSignatures #-}
15-
{-# LANGUAGE TypeFamilies #-}
16-
{-# LANGUAGE UndecidableInstances #-}
179

1810
-- | Arguments for LedgerDB initialization.
1911
module Ouroboros.Consensus.Storage.LedgerDB.Args
2012
( LedgerDbArgs (..)
2113
, LedgerDbBackendArgs (..)
22-
, LedgerDbFlavorArgs (..)
2314
, QueryBatchSize (..)
2415
, defaultArgs
2516
, defaultQueryBatchSize
@@ -44,21 +35,13 @@ import Ouroboros.Consensus.Storage.LedgerDB.API
4435
import Ouroboros.Consensus.Storage.LedgerDB.Snapshots
4536
import Ouroboros.Consensus.Storage.LedgerDB.TraceEvent
4637
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.Args as V1
47-
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.Args as V2
38+
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.Backend as V2
4839
import Ouroboros.Consensus.Util.Args
4940
import Ouroboros.Consensus.Util.IOLike
5041
import Ouroboros.Network.AnchoredSeq (AnchoredSeq)
5142
import qualified Ouroboros.Network.AnchoredSeq as AS
5243
import System.FS.API
5344

54-
data LedgerDbBackendArgs m
55-
= V1LMDB (Complete V1.LedgerDbFlavorArgs m)
56-
| V2InMemory
57-
| V2LSM
58-
-- | The filepath **relative to the fast storage device** in which we will
59-
-- open/create the LSM-tree database.
60-
FilePath
61-
6245
{-------------------------------------------------------------------------------
6346
Arguments
6447
-------------------------------------------------------------------------------}
@@ -75,7 +58,7 @@ data LedgerDbArgs f m blk = LedgerDbArgs
7558
, lgrHasFS :: HKD f (SomeHasFS m)
7659
, lgrConfig :: LedgerDbCfgF f (ExtLedgerState blk)
7760
, lgrTracer :: Tracer m (TraceEvent blk)
78-
, lgrFlavorArgs :: LedgerDbFlavorArgs f m
61+
, lgrBackendArgs :: LedgerDbBackendArgs m blk
7962
, lgrRegistry :: HKD f (ResourceRegistry m)
8063
, lgrQueryBatchSize :: QueryBatchSize
8164
, lgrStartSnapshot :: Maybe DiskSnapshot
@@ -87,8 +70,9 @@ data LedgerDbArgs f m blk = LedgerDbArgs
8770
-- | Default arguments
8871
defaultArgs ::
8972
Applicative m =>
73+
V2.SomeBackendArgs m blk ->
9074
Incomplete LedgerDbArgs m blk
91-
defaultArgs =
75+
defaultArgs backendArgs =
9276
LedgerDbArgs
9377
{ lgrSnapshotPolicyArgs = defaultSnapshotPolicyArgs
9478
, lgrGenesis = NoDefault
@@ -98,24 +82,24 @@ defaultArgs =
9882
, lgrTracer = nullTracer
9983
, -- This value is the closest thing to a pre-UTxO-HD node, and as such it
10084
-- will be the default for end-users.
101-
lgrFlavorArgs = LedgerDbFlavorArgsV2 (V2.V2Args V2.InMemoryHandleArgs)
85+
lgrBackendArgs = LedgerDbBackendArgsV2 backendArgs
10286
, lgrRegistry = NoDefault
10387
, lgrStartSnapshot = Nothing
10488
}
10589

106-
data LedgerDbFlavorArgs f m
107-
= LedgerDbFlavorArgsV1 (V1.LedgerDbFlavorArgs f m)
108-
| LedgerDbFlavorArgsV2 (V2.LedgerDbFlavorArgs f m)
90+
data LedgerDbBackendArgs m blk
91+
= LedgerDbBackendArgsV1 (V1.LedgerDbBackendArgs m (ExtLedgerState blk))
92+
| LedgerDbBackendArgsV2 (V2.SomeBackendArgs m blk)
10993

11094
{-------------------------------------------------------------------------------
11195
QueryBatchSize
11296
-------------------------------------------------------------------------------}
11397

114-
-- | The /maximum/ number of keys to read in a backing store range query.
98+
-- | The /maximum/ number of keys to read in a forker range query.
11599
--
116100
-- When performing a ledger state query that involves on-disk parts of the
117101
-- ledger state, we might have to read ranges of key-value pair data (e.g.,
118-
-- UTxO) from disk using backing store range queries. Instead of reading all
102+
-- UTxO) from disk using forker range queries. Instead of reading all
119103
-- data in one go, we read it in batches. 'QueryBatchSize' determines the size
120104
-- of these batches.
121105
--

0 commit comments

Comments
 (0)