Skip to content

Commit 7c8d483

Browse files
committed
Use ExceptT in readSnapshot, fix typos
1 parent 50c43bf commit 7c8d483

File tree

1 file changed

+8
-10
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB

1 file changed

+8
-10
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import Codec.Serialise.Decoding (Decoder)
3737
import qualified Codec.Serialise.Decoding as Dec
3838
import Codec.Serialise.Encoding (Encoding)
3939
import Control.Monad (forM, void)
40-
import Control.Monad.Except (ExceptT (..))
40+
import Control.Monad.Except (ExceptT (..), throwError, withExceptT)
4141
import Control.Tracer
4242
import Data.Bits
4343
import qualified Data.ByteString.Builder as BS
@@ -196,10 +196,10 @@ diskSnapshotIsTemporary = not . diskSnapshotIsPermanent
196196

197197
data ReadSnapshotErr =
198198
-- | Error while de-serialising data
199-
ReadSnaphotFailed ReadIncrementalErr
199+
ReadSnapshotFailed ReadIncrementalErr
200200
-- | Checksum of read snapshot differs from the one tracked by
201201
-- the corresponding '.checksum' file
202-
| ReadSnaphotDataCorruption
202+
| ReadSnapshotDataCorruption
203203
-- | A '.checksum' file does not exist for a @'DiskSnapshot'@
204204
| ReadSnapshotNoChecksumFile FsPath
205205
-- | A '.checksum' file exists for a @'DiskSnapshot'@, but its contents is invalid
@@ -219,13 +219,11 @@ readSnapshot ::
219219
-> ExceptT ReadSnapshotErr m (ExtLedgerState blk)
220220
readSnapshot someHasFS decLedger decHash snapshotName = do
221221
!snapshotCRC <- readCRC someHasFS (snapshotToChecksumPath snapshotName)
222-
ExceptT $
223-
readIncremental someHasFS decoder (snapshotToPath snapshotName) >>= \case
224-
Left e -> pure $ Left (ReadSnaphotFailed e)
225-
Right (ledgerState, checksumAsRead) ->
226-
if checksumAsRead /= snapshotCRC
227-
then pure $ Left ReadSnaphotDataCorruption
228-
else pure (Right ledgerState)
222+
(ledgerState, checksumAsRead) <- withExceptT ReadSnapshotFailed . ExceptT $
223+
readIncremental someHasFS decoder (snapshotToPath snapshotName)
224+
if checksumAsRead /= snapshotCRC
225+
then throwError ReadSnapshotDataCorruption
226+
else pure ledgerState
229227
where
230228
decoder :: Decoder s (ExtLedgerState blk)
231229
decoder = decodeSnapshotBackwardsCompatible (Proxy @blk) decLedger decHash

0 commit comments

Comments
 (0)