Skip to content

Commit 71aca5d

Browse files
committed
LedgerDB: refactor acquireAtTarget
No behavioral change, just a refactoring by extracting common parts; convenient for follow-up changes
1 parent 36f6655 commit 71aca5d

File tree

2 files changed

+41
-56
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB

2 files changed

+41
-56
lines changed

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

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Ouroboros.Consensus.Storage.LedgerDB.V1 (mkInitDb) where
2323
import Control.Arrow ((>>>))
2424
import Control.Monad
2525
import Control.Monad.Except
26+
import Control.Monad.Trans (lift)
2627
import Control.ResourceRegistry
2728
import Control.Tracer
2829
import Data.Bifunctor (first)
@@ -721,38 +722,30 @@ acquireAtTarget ::
721722
ResourceRegistry m ->
722723
Either Word64 (Target (Point blk)) ->
723724
ReadLocked m (Either GetForkerError (Resources m l))
724-
acquireAtTarget ldbEnv rr (Right VolatileTip) =
725-
readLocked $ do
726-
dblog <- readTVarIO (ldbChangelog ldbEnv)
727-
Right . (,dblog) <$> acquire ldbEnv rr dblog
728-
acquireAtTarget ldbEnv rr (Right ImmutableTip) =
729-
readLocked $ do
730-
dblog <- readTVarIO (ldbChangelog ldbEnv)
731-
Right . (,rollbackToAnchor dblog)
732-
<$> acquire ldbEnv rr dblog
733-
acquireAtTarget ldbEnv rr (Right (SpecificPoint pt)) =
734-
readLocked $ do
735-
dblog <- readTVarIO (ldbChangelog ldbEnv)
736-
let immTip = getTip $ anchor dblog
737-
case rollback pt dblog of
738-
Nothing
739-
| pointSlot pt < pointSlot immTip -> pure $ Left $ PointTooOld Nothing
740-
| otherwise -> pure $ Left PointNotOnChain
741-
Just dblog' -> Right . (,dblog') <$> acquire ldbEnv rr dblog'
742-
acquireAtTarget ldbEnv rr (Left n) = readLocked $ do
743-
dblog <- readTVarIO (ldbChangelog ldbEnv)
744-
case rollbackN n dblog of
745-
Nothing ->
746-
return $
747-
Left $
725+
acquireAtTarget ldbEnv rr target = readLocked $ runExceptT $ do
726+
dblog <- lift $ readTVarIO (ldbChangelog ldbEnv)
727+
-- Get the prefix of the dblog ending in the specified target.
728+
dblog' <- case target of
729+
Right VolatileTip -> pure dblog
730+
Right ImmutableTip -> pure $ rollbackToAnchor dblog
731+
Right (SpecificPoint pt) -> do
732+
let immTip = getTip $ anchor dblog
733+
case rollback pt dblog of
734+
Nothing
735+
| pointSlot pt < pointSlot immTip -> throwError $ PointTooOld Nothing
736+
| otherwise -> throwError PointNotOnChain
737+
Just dblog' -> pure dblog'
738+
Left n -> case rollbackN n dblog of
739+
Nothing ->
740+
throwError $
748741
PointTooOld $
749-
Just $
742+
Just
750743
ExceededRollback
751744
{ rollbackMaximum = maxRollback dblog
752745
, rollbackRequested = n
753746
}
754-
Just dblog' ->
755-
Right . (,dblog') <$> acquire ldbEnv rr dblog'
747+
Just dblog' -> pure dblog'
748+
lift $ (,dblog') <$> acquire ldbEnv rr dblog'
756749

757750
acquire ::
758751
(IOLike m, GetTip l) =>

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

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Ouroboros.Consensus.Storage.LedgerDB.V2 (mkInitDb) where
2020
import Control.Arrow ((>>>))
2121
import qualified Control.Monad as Monad (void, (>=>))
2222
import Control.Monad.Except
23+
import Control.Monad.Trans (lift)
2324
import Control.RAWLock
2425
import qualified Control.RAWLock as RAWLock
2526
import Control.ResourceRegistry
@@ -563,39 +564,30 @@ acquireAtTarget ::
563564
Either Word64 (Target (Point blk)) ->
564565
LDBLock ->
565566
m (Either GetForkerError (StateRef m l))
566-
acquireAtTarget ldbEnv (Right VolatileTip) _ = do
567-
l <- readTVarIO (ldbSeq ldbEnv)
568-
let StateRef st tbs = currentHandle l
569-
t <- duplicate tbs
570-
pure $ Right $ StateRef st t
571-
acquireAtTarget ldbEnv (Right ImmutableTip) _ = do
572-
l <- readTVarIO (ldbSeq ldbEnv)
573-
let StateRef st tbs = anchorHandle l
574-
t <- duplicate tbs
575-
pure $ Right $ StateRef st t
576-
acquireAtTarget ldbEnv (Right (SpecificPoint pt)) _ = do
577-
dblog <- readTVarIO (ldbSeq ldbEnv)
578-
let immTip = getTip $ anchor dblog
579-
case currentHandle <$> rollback pt dblog of
580-
Nothing
581-
| pointSlot pt < pointSlot immTip -> pure $ Left $ PointTooOld Nothing
582-
| otherwise -> pure $ Left PointNotOnChain
583-
Just (StateRef st tbs) ->
584-
Right . StateRef st <$> duplicate tbs
585-
acquireAtTarget ldbEnv (Left n) _ = do
586-
dblog <- readTVarIO (ldbSeq ldbEnv)
587-
case currentHandle <$> rollbackN n dblog of
588-
Nothing ->
589-
return $
590-
Left $
567+
acquireAtTarget ldbEnv target _ = runExceptT $ do
568+
l <- lift $ readTVarIO (ldbSeq ldbEnv)
569+
StateRef st tbs <- case target of
570+
Right VolatileTip -> pure $ currentHandle l
571+
Right ImmutableTip -> pure $ anchorHandle l
572+
Right (SpecificPoint pt) -> do
573+
let immTip = getTip $ anchor l
574+
case rollback pt l of
575+
Nothing
576+
| pointSlot pt < pointSlot immTip -> throwError $ PointTooOld Nothing
577+
| otherwise -> throwError PointNotOnChain
578+
Just t' -> pure $ currentHandle t'
579+
Left n -> case rollbackN n l of
580+
Nothing ->
581+
throwError $
591582
PointTooOld $
592-
Just $
583+
Just
593584
ExceededRollback
594-
{ rollbackMaximum = maxRollback dblog
585+
{ rollbackMaximum = maxRollback l
595586
, rollbackRequested = n
596587
}
597-
Just (StateRef st tbs) ->
598-
Right . StateRef st <$> duplicate tbs
588+
Just l' -> pure $ currentHandle l'
589+
tbs' <- lift $ duplicate tbs
590+
pure $ StateRef st tbs'
599591

600592
newForkerAtTarget ::
601593
( HeaderHash l ~ HeaderHash blk

0 commit comments

Comments
 (0)