@@ -11,16 +11,19 @@ import Cardano.Slotting.Slot (WithOrigin (..))
1111import Cardano.Tools.DBAnalyser.HasAnalysis
1212import Cardano.Tools.DBTruncater.Types
1313import Control.Monad
14+ import Control.Monad.Trans.Class (lift )
15+ import Control.Monad.Trans.Maybe (MaybeT (.. ))
1416import Control.Tracer
17+ import Data.Foldable (asum )
18+ import Data.Functor ((<&>) )
1519import Data.Functor.Identity
16- import Data.Traversable (for )
1720import Ouroboros.Consensus.Block
1821import Ouroboros.Consensus.Config
1922import Ouroboros.Consensus.Node as Node
2023import Ouroboros.Consensus.Node.InitStorage as Node
2124import Ouroboros.Consensus.Storage.Common
2225import Ouroboros.Consensus.Storage.ImmutableDB (ImmutableDB , Iterator ,
23- IteratorResult (.. ))
26+ IteratorResult (.. ), Tip ( .. ) )
2427import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB
2528import Ouroboros.Consensus.Storage.ImmutableDB.Impl
2629import Ouroboros.Consensus.Util.IOLike
@@ -56,33 +59,35 @@ truncate DBTruncaterConfig{ dbDir, truncateAfter, verbose } args = do
5659 }
5760
5861 withDB immutableDBArgs $ \ (immutableDB, internal) -> do
59- mLastHdr :: Maybe (Header block ) <- case truncateAfter of
60- TruncateAfterSlot slotNo -> do
61- mHash <- getHashForSlot internal slotNo
62- for (RealPoint slotNo <$> mHash) $
63- ImmutableDB. getKnownBlockComponent immutableDB GetHeader
62+ tip <- atomically $ ImmutableDB. getTip immutableDB
63+ let truncationBeyondTip = case truncateAfter of
64+ TruncateAfterSlot slotNo -> (tipSlotNo <$> tip) <= NotOrigin slotNo
65+ TruncateAfterBlock bno -> (tipBlockNo <$> tip) <= NotOrigin bno
66+ if truncationBeyondTip
67+ then putStrLn $ " Nothing to truncate, tip stays at " <> show tip
68+ else do
69+ mLastHdr :: Maybe (Header block ) <- case truncateAfter of
70+ TruncateAfterSlot slotNo -> runMaybeT $ asum $
71+ [slotNo, slotNo - 1 .. 0 ] <&> \ s -> do
72+ pt <- RealPoint s <$> MaybeT (getHashForSlot internal s)
73+ lift $ ImmutableDB. getKnownBlockComponent immutableDB GetHeader pt
6474
65- TruncateAfterBlock bno -> do
66- -- At the moment, we're just running a linear search with streamAll to
67- -- find the correct block to truncate from, but we could in theory do this
68- -- more quickly by binary searching the chunks of the ImmutableDB.
69- iterator <- ImmutableDB. streamAll immutableDB registry GetHeader
70- findLast ((<= bno) . blockNo) iterator
75+ TruncateAfterBlock bno -> do
76+ -- At the moment, we're just running a linear search with streamAll to
77+ -- find the correct block to truncate from, but we could in theory do this
78+ -- more quickly by binary searching the chunks of the ImmutableDB.
79+ iterator <- ImmutableDB. streamAll immutableDB registry GetHeader
80+ findLast ((<= bno) . blockNo) iterator
7181
72- case ImmutableDB. headerToTip <$> mLastHdr of
73- Nothing ->
74- putStrLn $ mconcat
75- [ " Unable to find a truncate point. This is because the ImmutableDB"
76- , " does not contain a block with the given slot or block number."
77- ]
78- Just newTip -> do
79- when verbose $ do
82+ case ImmutableDB. headerToTip <$> mLastHdr of
83+ Nothing -> fail " Couldn't find a point to truncate to!"
84+ Just newTip -> do
8085 putStrLn $ mconcat
8186 [ " Truncating the ImmutableDB using the following block as the "
8287 , " new tip:\n "
8388 , " " , show newTip
8489 ]
85- deleteAfter internal (At newTip)
90+ deleteAfter internal (At newTip)
8691
8792-- | Given a predicate, and an iterator, find the last item for which
8893-- the predicate passes.
0 commit comments