Skip to content

Commit 7908694

Browse files
authored
Cleanup ChainSelection and LedgerDB (#1880)
# Description The ContT experiment showed some improvements that could be made for clarity on both ChainSelection and the LedgerDB. The Changelog fragment describes each cleanup.
2 parents b3de332 + 530c7f0 commit 7908694

File tree

17 files changed

+319
-612
lines changed

17 files changed

+319
-612
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,11 @@ storeLedgerStateAt slotNo ledgerAppMode env = do
436436
FromLedgerState initLedgerDB internal = startFrom
437437

438438
process :: () -> blk -> IO (NextStep, ())
439-
process _ blk = do
439+
process _ blk = withRegistry $ \reg -> do
440440
let ledgerCfg = ExtLedgerCfg cfg
441441
oldLedger <- IOLike.atomically $ LedgerDB.getVolatileTip initLedgerDB
442442
frk <-
443-
LedgerDB.getForkerAtTarget initLedgerDB registry VolatileTip >>= \case
443+
LedgerDB.getForkerAtTarget initLedgerDB reg VolatileTip >>= \case
444444
Left{} -> error "Unreachable, volatile tip MUST be in the LedgerDB"
445445
Right f -> pure f
446446
tbs <- LedgerDB.forkerReadTables frk (getBlockKeySets blk)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
- Cleanup Chain selection:
24+
- Removed `Ouroboros.Consensus.Fragment.Validated` as it was only an
25+
indirection over `Ouroboros.Consensus.Fragment.ValidatedDiff`.
26+
- Initial chain selection now also performs the commit on the forker and
27+
returns only the selected fragment of headers.
28+
- Deleted `ChainAndLedger` as it was an unnecessary indirection.
29+
- Deleted `validateCandidate` as it was an unnecessary indirection to
30+
`ledgerValidateCandidate` which was now renamed to `validatedCandidate`.
31+
- Cleanup LedgerDB:
32+
- `validateFork` allows for `l` other than `ExtLedgerState`.
33+
- `validateFork` expects a non-empty list of headers.
34+
- `ValidateArgs` now expects an `l` that can be different to `ExtLedgerState`.
35+
- `Ap` has been simplified to monomorphize the constraints. `applyBlock` and
36+
related functions now run on an appropriate monad.
37+
- Classes `ThrowsLedgerErrors` and `ResolvesBlocks` have been deletes as
38+
unnecessary.
39+
- `MonadBase` scattered constraints have been removed as unnecessary.
40+
- `AnnLedgerError` carries a point to the latest valid block instead of a
41+
forker.

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ library
9898
Ouroboros.Consensus.Config.SupportsNode
9999
Ouroboros.Consensus.Forecast
100100
Ouroboros.Consensus.Fragment.Diff
101-
Ouroboros.Consensus.Fragment.Validated
102101
Ouroboros.Consensus.Fragment.ValidatedDiff
103102
Ouroboros.Consensus.Genesis.Governor
104103
Ouroboros.Consensus.HardFork.Abstract
@@ -371,7 +370,6 @@ library
371370
these ^>=1.2,
372371
time,
373372
transformers,
374-
transformers-base,
375373
typed-protocols ^>=1.2,
376374
vector ^>=0.13,
377375

@@ -567,7 +565,6 @@ library unstable-consensus-testlib
567565
temporary,
568566
text,
569567
time,
570-
transformers-base,
571568
tree-diff,
572569
utf8-string,
573570
vector,
@@ -726,7 +723,6 @@ test-suite consensus-test
726723
tasty-quickcheck,
727724
time,
728725
transformers,
729-
transformers-base,
730726
tree-diff,
731727
typed-protocols:{examples, stateful, typed-protocols} ^>=1.2,
732728
unstable-consensus-testlib,

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Fragment/Validated.hs

Lines changed: 0 additions & 136 deletions
This file was deleted.

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Fragment/ValidatedDiff.hs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,16 @@ module Ouroboros.Consensus.Fragment.ValidatedDiff
1212
( ValidatedChainDiff (ValidatedChainDiff)
1313
, getChainDiff
1414
, getLedger
15-
, new
16-
, toValidatedFragment
1715

1816
-- * Monadic
1917
, newM
20-
, toValidatedFragmentM
2118
) where
2219

2320
import Control.Monad.Except (throwError)
2421
import GHC.Stack (HasCallStack)
2522
import Ouroboros.Consensus.Block
2623
import Ouroboros.Consensus.Fragment.Diff (ChainDiff)
2724
import qualified Ouroboros.Consensus.Fragment.Diff as Diff
28-
import Ouroboros.Consensus.Fragment.Validated (ValidatedFragment)
29-
import qualified Ouroboros.Consensus.Fragment.Validated as VF
3025
import Ouroboros.Consensus.Ledger.Abstract
3126
import Ouroboros.Consensus.Util.Assert
3227
import Ouroboros.Consensus.Util.IOLike (MonadSTM (..))
@@ -52,21 +47,6 @@ pattern ValidatedChainDiff d l <- UnsafeValidatedChainDiff d l
5247

5348
{-# COMPLETE ValidatedChainDiff #-}
5449

55-
-- | Create a 'ValidatedChainDiff'.
56-
--
57-
-- PRECONDITION:
58-
--
59-
-- > getTip chainDiff == ledgerTipPoint ledger
60-
new ::
61-
forall b l mk.
62-
(GetTip l, HasHeader b, HeaderHash l ~ HeaderHash b, HasCallStack) =>
63-
ChainDiff b ->
64-
l mk ->
65-
ValidatedChainDiff b (l mk)
66-
new chainDiff ledger =
67-
assertWithMsg (pointInvariant (getTip ledger) chainDiff) $
68-
UnsafeValidatedChainDiff chainDiff ledger
69-
7050
pointInvariant ::
7151
forall l b.
7252
(HeaderHash b ~ HeaderHash l, HasHeader b) =>
@@ -88,17 +68,15 @@ pointInvariant ledgerTip0 chainDiff = precondition
8868
<> " /= "
8969
<> show ledgerTip
9070

91-
toValidatedFragment ::
92-
(GetTip l, HasHeader b, HeaderHash l ~ HeaderHash b, HasCallStack) =>
93-
ValidatedChainDiff b (l mk) ->
94-
ValidatedFragment b (l mk)
95-
toValidatedFragment (UnsafeValidatedChainDiff cs l) =
96-
VF.ValidatedFragment (Diff.getSuffix cs) l
97-
9871
{-------------------------------------------------------------------------------
9972
Monadic
10073
-------------------------------------------------------------------------------}
10174

75+
-- | Create a 'ValidatedChainDiff'.
76+
--
77+
-- PRECONDITION:
78+
--
79+
-- > getTip chainDiff == ledgerTipPoint ledger
10280
newM ::
10381
forall m b l.
10482
( MonadSTM m
@@ -115,15 +93,3 @@ newM chainDiff ledger = do
11593
pure $
11694
assertWithMsg (pointInvariant ledgerTip chainDiff) $
11795
UnsafeValidatedChainDiff chainDiff ledger
118-
119-
toValidatedFragmentM ::
120-
( MonadSTM m
121-
, GetTipSTM m l
122-
, HasHeader b
123-
, HeaderHash l ~ HeaderHash b
124-
, HasCallStack
125-
) =>
126-
ValidatedChainDiff b l ->
127-
m (ValidatedFragment b l)
128-
toValidatedFragmentM (UnsafeValidatedChainDiff cs l) =
129-
VF.newM (Diff.getSuffix cs) l

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import GHC.Stack (HasCallStack)
5858
import NoThunks.Class
5959
import Ouroboros.Consensus.Block
6060
import Ouroboros.Consensus.Config
61-
import qualified Ouroboros.Consensus.Fragment.Validated as VF
6261
import Ouroboros.Consensus.HardFork.Abstract
6362
import Ouroboros.Consensus.HeaderValidation (mkHeaderWithTime)
6463
import Ouroboros.Consensus.Ledger.Extended (ledgerState)
@@ -185,26 +184,18 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
185184
traceWith initChainSelTracer StartedInitChainSelection
186185
initialLoE <- Args.cdbsLoE cdbSpecificArgs
187186
initialWeights <- atomically $ PerasCertDB.getWeightSnapshot perasCertDB
188-
chain <- withRegistry $ \rr -> do
189-
chainAndLedger <-
190-
ChainSel.initialChainSelection
191-
immutableDB
192-
volatileDB
193-
lgrDB
194-
rr
195-
initChainSelTracer
196-
(Args.cdbsTopLevelConfig cdbSpecificArgs)
197-
varInvalid
198-
(void initialLoE)
199-
(forgetFingerprint initialWeights)
200-
traceWith initChainSelTracer InitialChainSelected
201-
202-
let chain = VF.validatedFragment chainAndLedger
203-
ledger = VF.validatedLedger chainAndLedger
204-
205-
atomically $ LedgerDB.forkerCommit ledger
206-
LedgerDB.forkerClose ledger
207-
pure chain
187+
chain <- withRegistry $ \rr ->
188+
ChainSel.initialChainSelection
189+
immutableDB
190+
volatileDB
191+
lgrDB
192+
rr
193+
initChainSelTracer
194+
(Args.cdbsTopLevelConfig cdbSpecificArgs)
195+
varInvalid
196+
(void initialLoE)
197+
(forgetFingerprint initialWeights)
198+
traceWith initChainSelTracer InitialChainSelected
208199
LedgerDB.tryFlush lgrDB
209200

210201
curLedger <- atomically $ LedgerDB.getVolatileTip lgrDB

0 commit comments

Comments
 (0)