-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1618: Simplify the en/decoder for PBFtChainState r=mrBliss a=mrBliss Fixes #1609. Since we're breaking backwards compatibility of the `ExtLedgerState` serialisation format, we can get rid of the code dealing with backwards compatibility in `PBFtChainState`. Nevertheless, start using `Versioned` for `PBFtChainState` so that it will be easier in the future to handle changes to the format. Co-authored-by: Thomas Winant <[email protected]>
- Loading branch information
Showing
9 changed files
with
271 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
ouroboros-consensus/src/Ouroboros/Consensus/Util/Versioned.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
module Ouroboros.Consensus.Util.Versioned | ||
( Versioned (..) | ||
, VersionNumber -- opaque | ||
, VersionError (..) | ||
, VersionDecoder (..) | ||
, encodeVersioned | ||
, encodeVersion | ||
, decodeVersioned | ||
, decodeVersion | ||
) where | ||
|
||
import Codec.Serialise (Serialise (..)) | ||
import Codec.Serialise.Decoding (Decoder, decodeWord8) | ||
import Codec.Serialise.Encoding (Encoding, encodeListLen, encodeWord8) | ||
import Control.Exception (Exception) | ||
import Data.Word (Word8) | ||
|
||
import Cardano.Binary (enforceSize) | ||
|
||
|
||
newtype VersionNumber = VersionNumber Word8 | ||
deriving newtype (Eq, Ord, Num, Show) | ||
|
||
instance Serialise VersionNumber where | ||
encode (VersionNumber w) = encodeWord8 w | ||
decode = VersionNumber <$> decodeWord8 | ||
|
||
data Versioned a = Versioned | ||
{ versionNumber :: !VersionNumber | ||
, versioned :: !a | ||
} deriving (Eq, Show) | ||
|
||
data VersionError | ||
= IncompatibleVersion VersionNumber String | ||
-- ^ We cannot deserialise the version of the data with the given | ||
-- 'VersionNumber' because its data format is incompatible. | ||
-- | ||
-- For example, the given format lacks data that was added in later | ||
-- version that cannot be reconstructed from scratch. | ||
| UnknownVersion VersionNumber | ||
-- ^ The given 'VersionNumber' is unknown and thus not supported. | ||
| MigrationFailed VersionNumber String | ||
-- ^ A migration from the given 'VersionNumber' failed. See 'Migrate'. | ||
deriving stock (Show) | ||
deriving anyclass (Exception) | ||
|
||
-- | How to decode a version of a format. | ||
data VersionDecoder a where | ||
-- | This version is incompatible, fail with 'IncompatibleVersion' and the | ||
-- given message. | ||
Incompatible :: String | ||
-> VersionDecoder a | ||
|
||
-- | Decode the version using the given 'Decoder'. | ||
Decode :: (forall s. Decoder s a) | ||
-> VersionDecoder a | ||
|
||
-- | Decode an other format (@from@) and migrate from that. When migration | ||
-- fails, the version decoder will fail with @MigrationFailed@. | ||
Migrate :: VersionDecoder from | ||
-> (from -> Either String to) | ||
-> VersionDecoder to | ||
|
||
-- | Return a 'Decoder' for the given 'VersionDecoder'. | ||
getVersionDecoder | ||
:: VersionNumber | ||
-> VersionDecoder a | ||
-> forall s. Decoder s a | ||
getVersionDecoder vn = \case | ||
Incompatible msg -> fail $ show $ IncompatibleVersion vn msg | ||
Decode dec -> dec | ||
Migrate vDec migrate -> do | ||
from <- getVersionDecoder vn vDec | ||
case migrate from of | ||
Left msg -> fail $ show $ MigrationFailed vn msg | ||
Right to -> return to | ||
|
||
-- | Given a 'VersionNumber' and the encoding of an @a@, encode the | ||
-- corresponding @'Versioned' a@. Use 'decodeVersion' to decode it. | ||
encodeVersion | ||
:: VersionNumber | ||
-> Encoding | ||
-> Encoding | ||
encodeVersion vn encodedA = mconcat | ||
[ encodeListLen 2 | ||
, encode vn | ||
, encodedA | ||
] | ||
|
||
-- | Decode a /versioned/ @a@ (encoded using 'encodeVersion' or | ||
-- 'encodeVersioned'). | ||
-- | ||
-- The corresponding 'VersionDecoder' for the deserialised 'VersionNumber' is | ||
-- looked up in the given list. The first match is used (using the semantics | ||
-- of 'lookup'). When no match is found, a decoder that fails with | ||
-- 'UnknownVersion' is returned. | ||
decodeVersion | ||
:: [(VersionNumber, VersionDecoder a)] | ||
-> forall s. Decoder s a | ||
decodeVersion versionDecoders = | ||
versioned <$> decodeVersioned versionDecoders | ||
|
||
encodeVersioned | ||
:: ( a -> Encoding) | ||
-> (Versioned a -> Encoding) | ||
encodeVersioned enc (Versioned vn a) = | ||
encodeVersion vn (enc a) | ||
|
||
decodeVersioned | ||
:: [(VersionNumber, VersionDecoder a)] | ||
-> forall s. Decoder s (Versioned a) | ||
decodeVersioned versionDecoders = do | ||
enforceSize "Versioned" 2 | ||
vn <- decode | ||
case lookup vn versionDecoders of | ||
Nothing -> fail $ show $ UnknownVersion vn | ||
Just vDec -> Versioned vn <$> getVersionDecoder vn vDec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.