@@ -19,6 +19,7 @@ module Ouroboros.Consensus.Storage.LedgerDB.V2.Forker
1919 , module Ouroboros.Consensus.Storage.LedgerDB.Forker
2020 ) where
2121
22+ import qualified Control.Monad as Monad
2223import Control.RAWLock (RAWLock )
2324import Control.ResourceRegistry
2425import Control.Tracer
@@ -158,24 +159,37 @@ implForkerCommit env = do
158159 stateTVar
159160 foeSwitchVar
160161 ( \ (LedgerSeq olddb) -> fromMaybe theImpossible $ do
162+ {-
163+ An example of the flow in this complicated switch:
164+
165+ olddb: A :| [ B, C, D ]
166+ toKeepLdb: A :| [ B, C ]
167+ toCloseLdb: C :| [ D ]
168+
169+ lseq: C' :| [ E, F ]
170+
171+ newdb: A :| [ B, C, E, F ]
172+ transfer: [ E, F ]
173+ to close: [ D ]
174+ -}
175+
161176 -- Split the selection at the intersection point. The snd component will
162177 -- have to be closed.
163- (toKeepBase, toCloseLdb) <- AS. splitAfterMeasure intersectionSlot (either predicate predicate) olddb
164- (toCloseForker, toKeepTip) <-
165- AS. splitAfterMeasure intersectionSlot (either predicate predicate) lseq
166- -- Join the prefix of the selection with the sequence in the forker
167- newdb <- AS. join (const $ const True ) toKeepBase toKeepTip
168- -- Do /not/ close the anchor of @toClose@, as that is also the
169- -- tip of @olddb'@ which will be used in @newdb@.
178+ (toKeepLdb, toCloseLdb) <- AS. splitAfterMeasure intersectionSlot (either predicate predicate) olddb
179+ -- Do /not/ close the anchor of @toCloseLdb@, as that is also the
180+ -- tip of @toKeepBase@ which will be used in @newdb@.
170181 let ldbToClose = case toCloseLdb of
171182 AS. Empty _ -> Nothing
172183 _ AS. :< closeOld' -> Just (LedgerSeq closeOld')
173184 transferCommitted = do
174- closeLedgerSeq ( LedgerSeq toCloseForker )
185+ Monad. void $ release (foeInitialHandleKey env )
175186
176187 -- All the other remaining handles are transferred to the LedgerDB registry
177188 keys <- transferRegistry foeResourceRegistry foeLedgerDbRegistry
178- mapM_ (\ (k, v) -> transfer (tables v) k) $ zip keys (AS. toOldestFirst toKeepTip)
189+ mapM_ (\ (k, v) -> transfer (tables v) k) $ zip keys (AS. toOldestFirst lseq)
190+
191+ -- Join the prefix of the selection with the sequence in the forker
192+ newdb <- AS. join (const $ const True ) toKeepLdb lseq
179193
180194 pure ((transferCommitted, ldbToClose), LedgerSeq newdb)
181195 )
0 commit comments