{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
module Ouroboros.Consensus.Node.Serialisation (
SerialiseNodeToClient (..)
, SerialiseNodeToNode (..)
, SerialiseResult (..)
, defaultDecodeCBORinCBOR
, defaultEncodeCBORinCBOR
, Some (..)
) where
import Codec.CBOR.Decoding (Decoder)
import Codec.CBOR.Encoding (Encoding)
import Codec.Serialise (Serialise (decode, encode))
import Data.SOP.BasicFunctors
import Ouroboros.Network.Block (unwrapCBORinCBOR, wrapCBORinCBOR)
import Ouroboros.Network.Protocol.LocalStateQuery.Codec (Some (..))
import Ouroboros.Consensus.Block
import Ouroboros.Consensus.Ledger.SupportsMempool (ApplyTxErr,
GenTxId)
import Ouroboros.Consensus.Node.NetworkProtocolVersion
import Ouroboros.Consensus.TypeFamilyWrappers
class SerialiseNodeToNode blk a where
encodeNodeToNode :: CodecConfig blk -> BlockNodeToNodeVersion blk -> a -> Encoding
decodeNodeToNode :: CodecConfig blk -> BlockNodeToNodeVersion blk -> forall s. Decoder s a
default encodeNodeToNode
:: Serialise a
=> CodecConfig blk -> BlockNodeToNodeVersion blk -> a -> Encoding
encodeNodeToNode CodecConfig blk
_ccfg BlockNodeToNodeVersion blk
_version = a -> Encoding
forall a. Serialise a => a -> Encoding
encode
default decodeNodeToNode
:: Serialise a
=> CodecConfig blk -> BlockNodeToNodeVersion blk -> forall s. Decoder s a
decodeNodeToNode CodecConfig blk
_ccfg BlockNodeToNodeVersion blk
_version = Decoder s a
forall a s. Serialise a => Decoder s a
decode
class SerialiseNodeToClient blk a where
encodeNodeToClient :: CodecConfig blk -> BlockNodeToClientVersion blk -> a -> Encoding
decodeNodeToClient :: CodecConfig blk -> BlockNodeToClientVersion blk -> forall s. Decoder s a
default encodeNodeToClient
:: Serialise a
=> CodecConfig blk -> BlockNodeToClientVersion blk -> a -> Encoding
encodeNodeToClient CodecConfig blk
_ccfg BlockNodeToClientVersion blk
_version = a -> Encoding
forall a. Serialise a => a -> Encoding
encode
default decodeNodeToClient
:: Serialise a
=> CodecConfig blk -> BlockNodeToClientVersion blk -> forall s. Decoder s a
decodeNodeToClient CodecConfig blk
_ccfg BlockNodeToClientVersion blk
_version = Decoder s a
forall a s. Serialise a => Decoder s a
decode
class SerialiseResult blk query where
encodeResult
:: forall result.
CodecConfig blk
-> BlockNodeToClientVersion blk
-> query result
-> result -> Encoding
decodeResult
:: forall result.
CodecConfig blk
-> BlockNodeToClientVersion blk
-> query result
-> forall s. Decoder s result
defaultEncodeCBORinCBOR :: Serialise a => a -> Encoding
defaultEncodeCBORinCBOR :: a -> Encoding
defaultEncodeCBORinCBOR = (a -> Encoding) -> a -> Encoding
forall a. (a -> Encoding) -> a -> Encoding
wrapCBORinCBOR a -> Encoding
forall a. Serialise a => a -> Encoding
encode
defaultDecodeCBORinCBOR :: Serialise a => Decoder s a
defaultDecodeCBORinCBOR :: Decoder s a
defaultDecodeCBORinCBOR = (forall s. Decoder s (ByteString -> a)) -> forall s. Decoder s a
forall a.
(forall s. Decoder s (ByteString -> a)) -> forall s. Decoder s a
unwrapCBORinCBOR (a -> ByteString -> a
forall a b. a -> b -> a
const (a -> ByteString -> a)
-> Decoder s a -> Decoder s (ByteString -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s a
forall a s. Serialise a => Decoder s a
decode)
instance SerialiseNodeToNode blk blk
=> SerialiseNodeToNode blk (I blk) where
encodeNodeToNode :: CodecConfig blk -> BlockNodeToNodeVersion blk -> I blk -> Encoding
encodeNodeToNode CodecConfig blk
cfg BlockNodeToNodeVersion blk
version (I blk
h) =
CodecConfig blk -> BlockNodeToNodeVersion blk -> blk -> Encoding
forall blk a.
SerialiseNodeToNode blk a =>
CodecConfig blk -> BlockNodeToNodeVersion blk -> a -> Encoding
encodeNodeToNode CodecConfig blk
cfg BlockNodeToNodeVersion blk
version blk
h
decodeNodeToNode :: CodecConfig blk
-> BlockNodeToNodeVersion blk -> forall s. Decoder s (I blk)
decodeNodeToNode CodecConfig blk
cfg BlockNodeToNodeVersion blk
version =
blk -> I blk
forall a. a -> I a
I (blk -> I blk) -> Decoder s blk -> Decoder s (I blk)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CodecConfig blk
-> BlockNodeToNodeVersion blk -> forall s. Decoder s blk
forall blk a.
SerialiseNodeToNode blk a =>
CodecConfig blk
-> BlockNodeToNodeVersion blk -> forall s. Decoder s a
decodeNodeToNode CodecConfig blk
cfg BlockNodeToNodeVersion blk
version
instance SerialiseNodeToClient blk blk
=> SerialiseNodeToClient blk (I blk) where
encodeNodeToClient :: CodecConfig blk
-> BlockNodeToClientVersion blk -> I blk -> Encoding
encodeNodeToClient CodecConfig blk
cfg BlockNodeToClientVersion blk
version (I blk
h) =
CodecConfig blk -> BlockNodeToClientVersion blk -> blk -> Encoding
forall blk a.
SerialiseNodeToClient blk a =>
CodecConfig blk -> BlockNodeToClientVersion blk -> a -> Encoding
encodeNodeToClient CodecConfig blk
cfg BlockNodeToClientVersion blk
version blk
h
decodeNodeToClient :: CodecConfig blk
-> BlockNodeToClientVersion blk -> forall s. Decoder s (I blk)
decodeNodeToClient CodecConfig blk
cfg BlockNodeToClientVersion blk
version =
blk -> I blk
forall a. a -> I a
I (blk -> I blk) -> Decoder s blk -> Decoder s (I blk)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CodecConfig blk
-> BlockNodeToClientVersion blk -> forall s. Decoder s blk
forall blk a.
SerialiseNodeToClient blk a =>
CodecConfig blk
-> BlockNodeToClientVersion blk -> forall s. Decoder s a
decodeNodeToClient CodecConfig blk
cfg BlockNodeToClientVersion blk
version
instance SerialiseNodeToNode blk (GenTxId blk)
=> SerialiseNodeToNode blk (WrapGenTxId blk) where
encodeNodeToNode :: CodecConfig blk
-> BlockNodeToNodeVersion blk -> WrapGenTxId blk -> Encoding
encodeNodeToNode CodecConfig blk
cfg BlockNodeToNodeVersion blk
version (WrapGenTxId GenTxId blk
h) =
CodecConfig blk
-> BlockNodeToNodeVersion blk -> GenTxId blk -> Encoding
forall blk a.
SerialiseNodeToNode blk a =>
CodecConfig blk -> BlockNodeToNodeVersion blk -> a -> Encoding
encodeNodeToNode CodecConfig blk
cfg BlockNodeToNodeVersion blk
version GenTxId blk
h
decodeNodeToNode :: CodecConfig blk
-> BlockNodeToNodeVersion blk
-> forall s. Decoder s (WrapGenTxId blk)
decodeNodeToNode CodecConfig blk
cfg BlockNodeToNodeVersion blk
version =
GenTxId blk -> WrapGenTxId blk
forall blk. GenTxId blk -> WrapGenTxId blk
WrapGenTxId (GenTxId blk -> WrapGenTxId blk)
-> Decoder s (GenTxId blk) -> Decoder s (WrapGenTxId blk)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CodecConfig blk
-> BlockNodeToNodeVersion blk -> forall s. Decoder s (GenTxId blk)
forall blk a.
SerialiseNodeToNode blk a =>
CodecConfig blk
-> BlockNodeToNodeVersion blk -> forall s. Decoder s a
decodeNodeToNode CodecConfig blk
cfg BlockNodeToNodeVersion blk
version
instance SerialiseNodeToClient blk (GenTxId blk)
=> SerialiseNodeToClient blk (WrapGenTxId blk) where
encodeNodeToClient :: CodecConfig blk
-> BlockNodeToClientVersion blk -> WrapGenTxId blk -> Encoding
encodeNodeToClient CodecConfig blk
cfg BlockNodeToClientVersion blk
version (WrapGenTxId GenTxId blk
h) =
CodecConfig blk
-> BlockNodeToClientVersion blk -> GenTxId blk -> Encoding
forall blk a.
SerialiseNodeToClient blk a =>
CodecConfig blk -> BlockNodeToClientVersion blk -> a -> Encoding
encodeNodeToClient CodecConfig blk
cfg BlockNodeToClientVersion blk
version GenTxId blk
h
decodeNodeToClient :: CodecConfig blk
-> BlockNodeToClientVersion blk
-> forall s. Decoder s (WrapGenTxId blk)
decodeNodeToClient CodecConfig blk
cfg BlockNodeToClientVersion blk
version =
GenTxId blk -> WrapGenTxId blk
forall blk. GenTxId blk -> WrapGenTxId blk
WrapGenTxId (GenTxId blk -> WrapGenTxId blk)
-> Decoder s (GenTxId blk) -> Decoder s (WrapGenTxId blk)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CodecConfig blk
-> BlockNodeToClientVersion blk
-> forall s. Decoder s (GenTxId blk)
forall blk a.
SerialiseNodeToClient blk a =>
CodecConfig blk
-> BlockNodeToClientVersion blk -> forall s. Decoder s a
decodeNodeToClient CodecConfig blk
cfg BlockNodeToClientVersion blk
version
instance SerialiseNodeToClient blk (ApplyTxErr blk)
=> SerialiseNodeToClient blk (WrapApplyTxErr blk) where
encodeNodeToClient :: CodecConfig blk
-> BlockNodeToClientVersion blk -> WrapApplyTxErr blk -> Encoding
encodeNodeToClient CodecConfig blk
cfg BlockNodeToClientVersion blk
version (WrapApplyTxErr ApplyTxErr blk
h) =
CodecConfig blk
-> BlockNodeToClientVersion blk -> ApplyTxErr blk -> Encoding
forall blk a.
SerialiseNodeToClient blk a =>
CodecConfig blk -> BlockNodeToClientVersion blk -> a -> Encoding
encodeNodeToClient CodecConfig blk
cfg BlockNodeToClientVersion blk
version ApplyTxErr blk
h
decodeNodeToClient :: CodecConfig blk
-> BlockNodeToClientVersion blk
-> forall s. Decoder s (WrapApplyTxErr blk)
decodeNodeToClient CodecConfig blk
cfg BlockNodeToClientVersion blk
version =
ApplyTxErr blk -> WrapApplyTxErr blk
forall blk. ApplyTxErr blk -> WrapApplyTxErr blk
WrapApplyTxErr (ApplyTxErr blk -> WrapApplyTxErr blk)
-> Decoder s (ApplyTxErr blk) -> Decoder s (WrapApplyTxErr blk)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CodecConfig blk
-> BlockNodeToClientVersion blk
-> forall s. Decoder s (ApplyTxErr blk)
forall blk a.
SerialiseNodeToClient blk a =>
CodecConfig blk
-> BlockNodeToClientVersion blk -> forall s. Decoder s a
decodeNodeToClient CodecConfig blk
cfg BlockNodeToClientVersion blk
version