/
VolDB.hs
635 lines (580 loc) · 24.5 KB
/
VolDB.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wredundant-constraints #-}
-- | Thin wrapper around the VolatileDB
module Ouroboros.Storage.ChainDB.Impl.VolDB (
VolDB -- Opaque
-- * Initialization
, VolDbArgs(..)
, defaultArgs
, openDB
-- * Candidates
, candidates
, isReachable
, isReachableSTM
-- * Paths
, Path(..)
, computePath
, computePathSTM
-- * Getting and parsing blocks
, BlockFileParserError
, getKnownBlock
, getKnownHeader
, getKnownBlockComponent
, getBlockComponent
-- * Wrappers
, getIsMember
, getPredecessor
, getSuccessors
, getMaxSlotNo
, putBlock
, closeDB
, reopen
, garbageCollect
-- * Tracing
, TraceEvent
-- * Re-exports
, VolatileDBError
, VolDB.BlockValidationPolicy
-- * Exported for testing purposes
, mkVolDB
, blockFileParser'
) where
import Codec.CBOR.Decoding (Decoder)
import Codec.CBOR.Encoding (Encoding)
import qualified Codec.CBOR.Read as CBOR
import qualified Codec.CBOR.Write as CBOR
import Control.Monad (join)
import Control.Tracer (Tracer, nullTracer)
import qualified Data.ByteString.Lazy as Lazy
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NE
import Data.Maybe (mapMaybe)
import Data.Proxy (Proxy (..))
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Typeable (Typeable)
import GHC.Stack (HasCallStack)
import Streaming.Prelude (Of (..), Stream)
import qualified Streaming.Prelude as S
import System.FilePath ((</>))
import Cardano.Prelude (allNoUnexpectedThunks, Word64)
import Ouroboros.Network.Block (pattern BlockPoint, ChainHash (..),
pattern GenesisPoint, HasHeader (..), HeaderHash,
MaxSlotNo, Point, SlotNo, StandardHash, pointHash,
withHash)
import qualified Ouroboros.Network.Block as Block
import Ouroboros.Network.Point (WithOrigin (..))
import Ouroboros.Consensus.Block (Header, IsEBB)
import qualified Ouroboros.Consensus.Util.CBOR as Util.CBOR
import Ouroboros.Consensus.Util.IOLike
import Ouroboros.Storage.ChainDB.API (ChainDB)
import Ouroboros.Storage.ChainDB.API hiding (ChainDB (..), closeDB,
getMaxSlotNo)
import Ouroboros.Storage.ChainDB.Impl.BlockComponent
import Ouroboros.Storage.Common (BinaryInfo (..))
import Ouroboros.Storage.FS.API (HasFS, createDirectoryIfMissing)
import Ouroboros.Storage.FS.API.Types (MountPoint (..), mkFsPath)
import Ouroboros.Storage.FS.IO (ioHasFS)
import Ouroboros.Storage.Util.ErrorHandling (ErrorHandling,
ThrowCantCatch)
import qualified Ouroboros.Storage.Util.ErrorHandling as EH
import Ouroboros.Storage.VolatileDB (VolatileDB, VolatileDBError)
import qualified Ouroboros.Storage.VolatileDB as VolDB
-- | Thin wrapper around the VolatileDB (opaque type)
--
-- The intention is that all interaction with the VolatileDB goes through this
-- module.
data VolDB m blk = VolDB {
volDB :: VolatileDB (HeaderHash blk) m
, decHeader :: forall s. Decoder s (Lazy.ByteString -> Header blk)
, decBlock :: forall s. Decoder s (Lazy.ByteString -> blk)
-- ^ TODO introduce a newtype wrapper around the @s@ so we can use
-- generics to derive the NoUnexpectedThunks instance.
, encBlock :: blk -> BinaryInfo Encoding
, isEBB :: blk -> IsEBB
, addHdrEnv :: !(IsEBB -> Lazy.ByteString -> Lazy.ByteString)
, err :: ErrorHandling VolatileDBError m
, errSTM :: ThrowCantCatch VolatileDBError (STM m)
}
-- Universal type; we can't use generics
instance NoUnexpectedThunks (VolDB m blk) where
showTypeOf _ = "VolDB"
whnfNoUnexpectedThunks ctxt VolDB {..} = allNoUnexpectedThunks
[ noUnexpectedThunks ctxt volDB
, noUnexpectedThunks ctxt decHeader
, noUnexpectedThunks ctxt decBlock
, noUnexpectedThunks ctxt encBlock
, noUnexpectedThunks ctxt isEBB
, noUnexpectedThunks ctxt addHdrEnv
, noUnexpectedThunks ctxt err
, noUnexpectedThunks ctxt errSTM
]
-- | Short-hand for events traced by the VolDB wrapper.
type TraceEvent blk =
VolDB.TraceEvent Util.CBOR.ReadIncrementalErr (HeaderHash blk)
{-------------------------------------------------------------------------------
Initialization
-------------------------------------------------------------------------------}
data VolDbArgs m blk = forall h. VolDbArgs {
volHasFS :: HasFS m h
, volErr :: ErrorHandling VolatileDBError m
, volErrSTM :: ThrowCantCatch VolatileDBError (STM m)
, volCheckIntegrity :: blk -> Bool
, volBlocksPerFile :: Int
, volDecodeHeader :: forall s. Decoder s (Lazy.ByteString -> Header blk)
, volDecodeBlock :: forall s. Decoder s (Lazy.ByteString -> blk)
, volEncodeBlock :: blk -> BinaryInfo Encoding
, volIsEBB :: blk -> IsEBB
, volAddHdrEnv :: IsEBB -> Lazy.ByteString -> Lazy.ByteString
, volValidation :: VolDB.BlockValidationPolicy
, volTracer :: Tracer m (TraceEvent blk)
}
-- | Default arguments when using the 'IO' monad
--
-- The following fields must still be defined:
--
-- * 'volBlocksPerFile'
-- * 'volDecodeHeader'
-- * 'volDecodeBlock'
-- * 'volEncodeBlock'
-- * 'volIsEBB'
-- * 'volAddHdrEnv'
defaultArgs :: FilePath -> VolDbArgs IO blk
defaultArgs fp = VolDbArgs {
volErr = EH.exceptions
, volErrSTM = EH.throwSTM
, volHasFS = ioHasFS $ MountPoint (fp </> "volatile")
, volTracer = nullTracer
-- Fields without a default
, volCheckIntegrity = error "no default for volCheckIntegrity"
, volBlocksPerFile = error "no default for volBlocksPerFile"
, volDecodeHeader = error "no default for volDecodeHeader"
, volDecodeBlock = error "no default for volDecodeBlock"
, volEncodeBlock = error "no default for volEncodeBlock"
, volIsEBB = error "no default for volIsEBB"
, volAddHdrEnv = error "no default for volAddHdrEnv"
, volValidation = error "no default for volValidation"
}
openDB :: (IOLike m, HasHeader blk) => VolDbArgs m blk -> m (VolDB m blk)
openDB args@VolDbArgs{..} = do
createDirectoryIfMissing volHasFS True (mkFsPath [])
volDB <- VolDB.openDB
volHasFS
volErr
volErrSTM
(blockFileParser args)
volTracer
volBlocksPerFile
return VolDB
{ volDB = volDB
, err = volErr
, errSTM = volErrSTM
, decHeader = volDecodeHeader
, decBlock = volDecodeBlock
, encBlock = volEncodeBlock
, addHdrEnv = volAddHdrEnv
, isEBB = volIsEBB
}
-- | For testing purposes
mkVolDB :: VolatileDB (HeaderHash blk) m
-> (forall s. Decoder s (Lazy.ByteString -> Header blk))
-> (forall s. Decoder s (Lazy.ByteString -> blk))
-> (blk -> BinaryInfo Encoding)
-> (blk -> IsEBB)
-> (IsEBB -> Lazy.ByteString -> Lazy.ByteString)
-> ErrorHandling VolatileDBError m
-> ThrowCantCatch VolatileDBError (STM m)
-> VolDB m blk
mkVolDB volDB decHeader decBlock encBlock isEBB addHdrEnv err errSTM = VolDB {..}
{-------------------------------------------------------------------------------
Wrappers
-------------------------------------------------------------------------------}
getIsMember :: VolDB m blk -> STM m (HeaderHash blk -> Bool)
getIsMember db = withSTM db VolDB.getIsMember
getPredecessor :: VolDB m blk
-> STM m (HeaderHash blk -> WithOrigin (HeaderHash blk))
getPredecessor db = withSTM db VolDB.getPredecessor
getSuccessors :: VolDB m blk
-> STM m (WithOrigin (HeaderHash blk) -> Set (HeaderHash blk))
getSuccessors db = withSTM db VolDB.getSuccessors
getMaxSlotNo :: VolDB m blk
-> STM m MaxSlotNo
getMaxSlotNo db = withSTM db VolDB.getMaxSlotNo
putBlock :: (MonadCatch m, HasHeader blk) => VolDB m blk -> blk -> m ()
putBlock db@VolDB{..} b = withDB db $ \vol ->
VolDB.putBlock vol (extractInfo isEBB binInfo b) binaryBlob
where
binInfo@BinaryInfo { binaryBlob } = CBOR.toBuilder <$> encBlock b
closeDB :: (MonadCatch m, HasCallStack) => VolDB m blk -> m ()
closeDB db = withDB db VolDB.closeDB
reopen :: (MonadCatch m, HasCallStack) => VolDB m blk -> m ()
reopen db = withDB db VolDB.reOpenDB
garbageCollect :: MonadCatch m => VolDB m blk -> SlotNo -> m ()
garbageCollect db slotNo = withDB db $ \vol ->
VolDB.garbageCollect vol slotNo
{-------------------------------------------------------------------------------
Compute candidates
-------------------------------------------------------------------------------}
-- | Compute all candidates starting at the specified hash
--
-- PRECONDITION: the block to which the given point corresponds is part of the
-- VolatileDB.
--
-- The first element in each list of hashes is the hash /after/ the specified
-- hash. Thus, when building fragments from these lists of hashes, they
-- fragments must be /anchored/ at the specified hash, but not contain it.
--
-- NOTE: it is possible that no candidates are found, but don't forget that
-- the chain (fragment) ending with @B@ is also a potential candidate.
candidates
:: forall blk.
(WithOrigin (HeaderHash blk) -> Set (HeaderHash blk)) -- ^ @getSuccessors@
-> Point blk -- ^ @B@
-> [NonEmpty (HeaderHash blk)]
-- ^ Each element in the list is a list of hashes from which we can
-- construct a fragment anchored at the point @B@.
candidates succsOf b = mapMaybe NE.nonEmpty $ go (fromChainHash (pointHash b))
where
go :: WithOrigin (HeaderHash blk) -> [[HeaderHash blk]]
go mbHash = case Set.toList $ succsOf mbHash of
[] -> [[]]
succs -> [ next : candidate
| next <- succs
, candidate <- go (At next)
]
-- | Variant of 'isReachable' that obtains its arguments in the same 'STM'
-- transaction.
isReachableSTM :: (IOLike m, HasHeader blk)
=> VolDB m blk
-> STM m (Point blk) -- ^ The tip of the ImmutableDB (@I@).
-> Point blk -- ^ The point of the block (@B@) to check
-- the reachability of
-> STM m (Maybe (NonEmpty (HeaderHash blk)))
isReachableSTM volDB getI b = do
(predecessor, isMember, i) <- withSTM volDB $ \db ->
(,,) <$> VolDB.getPredecessor db <*> VolDB.getIsMember db <*> getI
return $ isReachable predecessor isMember i b
-- | Check whether the given point, corresponding to a block @B@, is reachable
-- from the tip of the ImmutableDB (@I@) by chasing the predecessors.
--
-- Returns a path (of hashes) from @I@ (excluded) to @B@ (included).
--
-- PRECONDITION: @B ∈ V@, i.e. @B@ is part of the VolatileDB.
--
-- 'True' <=> for all transitive predecessors @B'@ of @B@ we have @B' ∈ V@ or
-- @B' = I@.
isReachable :: forall blk. (HasHeader blk, HasCallStack)
=> (HeaderHash blk -> WithOrigin (HeaderHash blk)) -- ^ @getPredecessor@
-> (HeaderHash blk -> Bool) -- ^ @isMember@
-> Point blk -- ^ @I@
-> Point blk -- ^ @B@
-> Maybe (NonEmpty (HeaderHash blk))
isReachable predecessor isMember i b =
case computePath predecessor isMember from to of
-- Bounds are not on the same fork
Nothing -> Nothing
Just path -> case path of
CompletelyInVolDB hashes -> case NE.nonEmpty hashes of
Just hashes' -> Just hashes'
Nothing ->
error "impossible: empty list of hashes with an inclusive bound"
PartiallyInVolDB {} -> Nothing
NotInVolDB _ ->
error "impossible: block just added to VolatileDB missing"
where
from = StreamFromExclusive i
to = StreamToInclusive b
{-------------------------------------------------------------------------------
Compute paths
-------------------------------------------------------------------------------}
-- | Construct a path backwards through the VolatileDB.
--
-- We walk backwards through the VolatileDB, constructing a 'Path' from the
-- 'StreamTo' to the 'StreamFrom'.
--
-- If the range is invalid, 'Nothing' is returned.
--
-- See the documentation of 'Path'.
computePath
:: forall blk. HasHeader blk
=> (HeaderHash blk -> WithOrigin (HeaderHash blk)) -- ^ @getPredecessor@
-> (HeaderHash blk -> Bool) -- ^ @isMember@
-> StreamFrom blk
-> StreamTo blk
-> Maybe (Path blk)
computePath predecessor isMember from to = case to of
StreamToInclusive GenesisPoint
-> Nothing
StreamToExclusive GenesisPoint
-> Nothing
StreamToInclusive (BlockPoint { withHash = end })
| isMember end -> case from of
-- Easier to handle this special case (@StreamFromInclusive start,
-- StreamToInclusive end, start == end@) here:
StreamFromInclusive (BlockPoint { withHash = start })
| start == end -> return $ CompletelyInVolDB [end]
_ -> go [end] end
| otherwise -> return $ NotInVolDB end
StreamToExclusive (BlockPoint { withHash = end })
| isMember end -> go [] end
| otherwise -> return $ NotInVolDB end
where
-- Invariant: @isMember hash@ and @hash@ has been added to @acc@ (if
-- allowed by the bounds)
go :: [HeaderHash blk] -> HeaderHash blk -> Maybe (Path blk)
go acc hash = case predecessor hash of
-- Found genesis
Origin -> case from of
StreamFromInclusive _
-> Nothing
StreamFromExclusive GenesisPoint
-> return $ CompletelyInVolDB acc
StreamFromExclusive (BlockPoint {})
-> Nothing
At predHash
| isMember predHash -> case from of
StreamFromInclusive pt
| BlockHash predHash == Block.pointHash pt
-> return $ CompletelyInVolDB (predHash : acc)
StreamFromExclusive pt
| BlockHash predHash == Block.pointHash pt
-> return $ CompletelyInVolDB acc
-- Bound not yet reached, invariants both ok!
_ -> go (predHash : acc) predHash
-- Predecessor not in the VolatileDB
| StreamFromExclusive pt <- from
, BlockHash predHash == Block.pointHash pt
-- That's fine if we don't need to include it in the path.
-> return $ CompletelyInVolDB acc
| otherwise
-> return $ PartiallyInVolDB predHash acc
-- | Variant of 'computePath' that obtains its arguments in the same 'STM'
-- transaction. Throws an 'InvalidIteratorRange' exception when the range is
-- invalid (i.e., 'computePath' returned 'Nothing').
computePathSTM
:: forall m blk. (IOLike m, HasHeader blk)
=> VolDB m blk
-> StreamFrom blk
-> StreamTo blk
-> STM m (Path blk)
computePathSTM volDB from to = do
(predecessor, isMember) <- withSTM volDB $ \db ->
(,) <$> VolDB.getPredecessor db <*> VolDB.getIsMember db
case computePath predecessor isMember from to of
Just path -> return path
Nothing -> throwM $ InvalidIteratorRange from to
-- | A path through the VolatileDB from a 'StreamFrom' to a 'StreamTo'.
--
-- Invariant: the @ChainFragment@ (oldest first) constructed using the blocks
-- corresponding to the hashes in the path will be valid, i.e. the blocks will
-- fit onto each other.
data Path blk
= NotInVolDB (HeaderHash blk)
-- ^ The @end@ point (@'StreamToInclusive' end@ or @'StreamToExclusive'
-- end@) was not part of the VolatileDB.
| CompletelyInVolDB [HeaderHash blk]
-- ^ A complete path, from start point to end point was constructed from
-- the VolatileDB. The list contains the hashes from oldest to newest.
--
-- * If the lower bound was @'StreamFromInclusive' pt@, then @pt@ will be
-- the first element of the list.
-- * If the lower bound was @'StreamFromExclusive' pt@, then the first
-- element of the list will correspond to the first block after @pt@.
--
-- * If the upper bound was @'StreamToInclusive' pt@, then @pt@ will be
-- the last element of the list.
-- * If the upper bound was @'StreamToExclusive' pt@, then the last
-- element of the list will correspond to the first block before @pt@.
| PartiallyInVolDB (HeaderHash blk) [HeaderHash blk]
-- ^ Only a partial path could be constructed from the VolatileDB. The
-- missing predecessor could still be in the ImmutableDB. The list
-- contains the hashes from oldest to newest.
--
-- * The first element in the list is the point for which no predecessor
-- is available in the VolatileDB. The block corresponding to the point
-- itself, /is/ available in the VolatileDB.
-- * The first argument is the hash of predecessor, the block that is not
-- available in the VolatileDB.
--
-- Note: if the lower bound is exclusive, the block corresponding to it
-- doesn't have to be part of the VolatileDB, it will result in a
-- 'StartToEnd'.
--
-- The same invariants hold for the upper bound as for 'StartToEnd'.
deriving instance Show (HeaderHash blk) => Show (Path blk)
{-------------------------------------------------------------------------------
Getting and parsing blocks
-------------------------------------------------------------------------------}
getKnownBlock
:: (MonadCatch m, HasHeader blk)
=> VolDB m blk
-> HeaderHash blk
-> m blk
getKnownBlock volDB = join . getKnownBlockComponent volDB GetBlock
getKnownHeader
:: (MonadCatch m, HasHeader blk)
=> VolDB m blk
-> HeaderHash blk
-> m (Header blk)
getKnownHeader volDB = join . getKnownBlockComponent volDB GetHeader
getKnownBlockComponent
:: (MonadCatch m, HasHeader blk)
=> VolDB m blk
-> BlockComponent (ChainDB m blk) b
-> HeaderHash blk
-> m b
getKnownBlockComponent db blockComponent hash = do
mBlock <- mustExist db hash <$>
getBlockComponent db blockComponent hash
case mBlock of
Right b -> return b
Left err -> throwM err
getBlockComponent
:: forall m blk b. (MonadCatch m, HasHeader blk)
=> VolDB m blk
-> BlockComponent (ChainDB m blk) b
-> HeaderHash blk
-> m (Maybe b)
getBlockComponent db blockComponent hash = withDB db $ \vol ->
VolDB.getBlockComponent vol blockComponent' hash
where
blockComponent' = translateToRawDB (parse db) (addHdrEnv db) blockComponent
{-------------------------------------------------------------------------------
Parsing
-------------------------------------------------------------------------------}
type BlockFileParserError hash =
VolDB.ParserError hash Util.CBOR.ReadIncrementalErr
blockFileParser :: forall m blk. (IOLike m, HasHeader blk)
=> VolDbArgs m blk
-> VolDB.Parser
Util.CBOR.ReadIncrementalErr
m
(HeaderHash blk)
blockFileParser VolDbArgs{..} =
blockFileParser' volHasFS volIsEBB volEncodeBlock volDecodeBlock
volCheckIntegrity volValidation
-- | A version which is easier to use for tests, since it does not require
-- the whole @VolDbArgs@.
blockFileParser' :: forall m blk h. (IOLike m, HasHeader blk)
=> HasFS m h
-> (blk -> IsEBB)
-> (blk -> BinaryInfo Encoding)
-> (forall s. Decoder s (Lazy.ByteString -> blk))
-> (blk -> Bool)
-> VolDB.BlockValidationPolicy
-> VolDB.Parser
Util.CBOR.ReadIncrementalErr
m
(HeaderHash blk)
blockFileParser' hasFS isEBB encodeBlock decodeBlock validate validPolicy =
VolDB.Parser $ \fsPath -> Util.CBOR.withStreamIncrementalOffsets
hasFS decodeBlock fsPath (checkEntries [])
where
-- TODO: It looks weird that we use an encoding function 'encodeBlock'
-- during parsing, but this is quite cheap, since the encoding is already
-- cached. We should consider improving this, so that it does not create
-- confusion.
extractInfo' :: blk -> VolDB.BlockInfo (HeaderHash blk)
extractInfo' blk = extractInfo isEBB (encodeBlock blk) blk
noValidation :: Bool
noValidation = validPolicy == VolDB.NoValidation
checkEntries :: VolDB.ParsedInfo (HeaderHash blk)
-> Stream (Of (Word64, (Word64, blk)))
m
(Maybe (Util.CBOR.ReadIncrementalErr, Word64))
-> m (VolDB.ParsedInfo (HeaderHash blk),
Maybe (BlockFileParserError (HeaderHash blk)))
checkEntries parsed stream = S.next stream >>= \case
Left mbErr -> return (reverse parsed, VolDB.BlockReadErr . fst <$> mbErr)
Right ((offset, (size, blk)), stream') | noValidation || (validate blk) ->
let !blockInfo = extractInfo' blk
newParsed = (offset, (VolDB.BlockSize size, blockInfo))
in checkEntries (newParsed : parsed) stream'
Right ((_, (_, blk)), _) ->
let !bid = VolDB.bbid $ extractInfo' blk
in return (reverse parsed, Just (VolDB.BlockCorruptedErr bid))
{-------------------------------------------------------------------------------
Error handling
-------------------------------------------------------------------------------}
-- | Wrap calls to the VolatileDB and rethrow exceptions that may indicate
-- disk failure and should therefore trigger recovery
withDB :: forall m blk x. MonadThrow m
=> VolDB m blk
-> (VolatileDB (HeaderHash blk) m -> m x)
-> m x
withDB VolDB{..} k = EH.catchError err (k volDB) rethrow
where
rethrow :: VolatileDBError -> m x
rethrow e = case wrap e of
Just e' -> throwM e'
Nothing -> throwM e
wrap :: VolatileDBError -> Maybe ChainDbFailure
wrap (VolDB.UnexpectedError e) = Just (VolDbFailure e)
wrap VolDB.UserError{} = Nothing
-- | STM actions, by definition, cannot access the disk and therefore we don't
-- have to worry about catching exceptions here: any exceptions that may be
-- thrown indicate bugs, either in the ChainDB or in the VolatileDB
withSTM :: VolDB m blk
-> (VolatileDB (HeaderHash blk) m -> STM m x)
-> STM m x
withSTM VolDB{..} k = k volDB
mustExist :: forall proxy blk b. (StandardHash blk, Typeable blk)
=> proxy blk
-> HeaderHash blk
-> Maybe b
-> Either ChainDbFailure b
mustExist _ hash Nothing = Left $ VolDbMissingBlock (Proxy @blk) hash
mustExist _ _ (Just b) = Right $ b
-- TODO unify with ImmDB.parse
parse :: forall m blk b. (HasHeader blk, MonadThrow m)
=> VolDB m blk
-> BlockOrHeader blk b
-> BlockRef blk
-> Lazy.ByteString
-> m b -- ^ Throws 'ChainDbFailure'
parse db blockOrHeader blockRef bytes =
aux (CBOR.deserialiseFromBytes dec bytes)
where
dec :: forall s. Decoder s (Lazy.ByteString -> b)
dec = case blockOrHeader of
Block -> decBlock db
Header -> decHeader db
aux :: Either CBOR.DeserialiseFailure
(Lazy.ByteString, Lazy.ByteString -> b)
-> m b
aux (Right (bs, b))
| Lazy.null bs = return $ b bytes
| otherwise = throwM $ VolDbTrailingData blockRef bs
aux (Left err) = throwM $ VolDbParseFailure blockRef err
{-------------------------------------------------------------------------------
Auxiliary
-------------------------------------------------------------------------------}
fromChainHash :: ChainHash blk -> WithOrigin (HeaderHash blk)
fromChainHash GenesisHash = Origin
fromChainHash (BlockHash hash) = At hash
extractInfo :: HasHeader blk
=> (blk -> IsEBB)
-> BinaryInfo a
-> blk
-> VolDB.BlockInfo (HeaderHash blk)
extractInfo isEBB BinaryInfo{..} b = VolDB.BlockInfo {
bbid = blockHash b
, bslot = blockSlot b
, bpreBid = fromChainHash (blockPrevHash b)
, bisEBB = isEBB b
, bheaderOffset = headerOffset
, bheaderSize = headerSize
}