11{-# LANGUAGE DerivingStrategies #-}
2+ {-# LANGUAGE FlexibleContexts #-}
23{-# LANGUAGE FlexibleInstances #-}
34{-# LANGUAGE LambdaCase #-}
45{-# LANGUAGE NamedFieldPuns #-}
@@ -14,6 +15,7 @@ module Cardano.Tools.DB
1415 StandardPoint ,
1516 pattern StandardPoint ,
1617 Result (.. ),
18+ DBError (.. ),
1719 DB ,
1820 DBTrace ,
1921 SlotNo ,
@@ -24,11 +26,13 @@ module Cardano.Tools.DB
2426 getParent ,
2527 getBlock ,
2628 getSnapshot ,
27- getSnapshots ,
29+ listSnapshots ,
2830 parsePoint ,
2931 makePoint ,
3032 makeSlot ,
3133 mkPoint ,
34+ listBlocks ,
35+ toBytestring ,
3236 )
3337where
3438
@@ -42,6 +46,7 @@ import Control.Tracer (Tracer (..))
4246import Data.Aeson (FromJSON (.. ), ToJSON , object , toJSON , withObject , (.:) , (.=) )
4347import qualified Data.ByteString.Base16 as Hex
4448import qualified Data.ByteString.Lazy as LBS
49+ import Data.String (IsString (fromString ))
4550import Data.Text (Text )
4651import qualified Data.Text as Text
4752import Data.Text.Encoding (decodeUtf8 , encodeUtf8 )
@@ -59,7 +64,7 @@ import qualified Ouroboros.Consensus.Node.InitStorage as Node
5964import Ouroboros.Consensus.Node.ProtocolInfo (ProtocolInfo (.. ))
6065import Ouroboros.Consensus.Shelley.Ledger (LedgerState (shelleyLedgerState ), ShelleyTip (.. ), shelleyLedgerTip )
6166import Ouroboros.Consensus.Shelley.Protocol.Abstract (ShelleyHash (.. ))
62- import Ouroboros.Consensus.Storage.ChainDB (BlockComponent (.. ), ChainDB , TraceEvent , defaultArgs , getBlockComponent )
67+ import Ouroboros.Consensus.Storage.ChainDB (BlockComponent (.. ), ChainDB , IteratorResult ( .. ), TraceEvent , defaultArgs , getBlockComponent , streamAll )
6368import qualified Ouroboros.Consensus.Storage.ChainDB as ChainDB
6469import Ouroboros.Consensus.Storage.ChainDB.Impl.Args (completeChainDbArgs , updateTracer )
6570import Ouroboros.Consensus.Storage.LedgerDB (Checkpoint (unCheckpoint ), LedgerDB (.. ))
@@ -149,26 +154,37 @@ makePoint slotTxt hashTxt =
149154 Right bytes -> Just $ RealPoint slot (fromRawHash (Proxy @ StandardBlock ) bytes)
150155 Left _ -> Nothing
151156
152- data Result a = Malformed | NotFound | Found a
157+ data DBError
158+ = NotFound
159+ | Malformed
160+ | MalformedQuery Text
161+ | InitialHeader
162+ | UnknownStateType
163+ deriving stock (Eq , Show )
164+
165+ toBytestring :: (IsString s ) => DBError -> s
166+ toBytestring = fromString . show
167+
168+ data Result a = Err DBError | Found a
153169 deriving stock (Eq , Show )
154170
155171getHeader :: ChainDB IO StandardBlock -> StandardPoint -> IO (Result LBS. ByteString )
156172getHeader db point =
157- maybe NotFound Found <$> getBlockComponent db GetRawHeader point
173+ maybe ( Err NotFound ) Found <$> getBlockComponent db GetRawHeader point
158174
159175getParent :: ChainDB IO StandardBlock -> StandardPoint -> IO (Result LBS. ByteString )
160176getParent db point =
161177 getBlockComponent db GetHeader point >>= \ case
162- Nothing -> pure NotFound
178+ Nothing -> pure ( Err NotFound )
163179 Just header ->
164180 case headerPrevHash header of
165181 BlockHash headerHash ->
166182 getHeader db (RealPoint (SlotNo 0 ) headerHash) -- FIXME
167- GenesisHash -> pure Malformed
183+ GenesisHash -> pure ( Err InitialHeader )
168184
169185getBlock :: ChainDB IO StandardBlock -> StandardPoint -> IO (Result LBS. ByteString )
170186getBlock db point =
171- maybe NotFound Found <$> getBlockComponent db GetRawBlock point
187+ maybe ( Err NotFound ) Found <$> getBlockComponent db GetRawBlock point
172188
173189pointOfState :: LedgerState StandardBlock -> StandardPoint
174190pointOfState = \ case
@@ -193,12 +209,24 @@ pointOfState = \case
193209makeSlot :: Text -> Maybe SlotNo
194210makeSlot slotTxt = fromInteger <$> readMaybe (Text. unpack slotTxt)
195211
196- getSnapshots :: ChainDB IO StandardBlock -> IO [StandardPoint ]
197- getSnapshots db = do
212+ listSnapshots :: ChainDB IO StandardBlock -> IO [StandardPoint ]
213+ listSnapshots db = do
198214 LedgerDB {ledgerDbCheckpoints} <- atomically $ ChainDB. getLedgerDB db
199215 let snapshotsList :: [LedgerState StandardBlock ] = ledgerState . unCheckpoint <$> Seq. toOldestFirst ledgerDbCheckpoints
200216 pure $ pointOfState <$> snapshotsList
201217
218+ listBlocks :: ChainDB IO StandardBlock -> IO [StandardPoint ]
219+ listBlocks db = do
220+ withRegistry $ \ registry ->
221+ streamAll db registry GetHeader >>= go []
222+ where
223+ go acc iter = do
224+ ChainDB. iteratorNext iter >>= \ case
225+ IteratorResult hdr -> go (headerRealPoint hdr : acc) iter
226+ IteratorExhausted -> pure acc
227+ IteratorBlockGCed _ ->
228+ error " block on the current chain was garbage-collected"
229+
202230getSnapshot :: ChainDB IO StandardBlock -> SlotNo -> IO (Result LBS. ByteString )
203231getSnapshot db slot = do
204232 LedgerDB {ledgerDbCheckpoints} <- atomically $ ChainDB. getLedgerDB db
@@ -207,5 +235,5 @@ getSnapshot db slot = do
207235 case ledgerState $ unCheckpoint snapshot of
208236 LedgerStateBabbage state ->
209237 pure $ Found $ serialize (toEnum 10 ) $ shelleyLedgerState state
210- _other -> pure NotFound
211- _other -> pure NotFound
238+ _other -> pure ( Err UnknownStateType )
239+ _other -> pure ( Err NotFound )
0 commit comments