{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
module Ouroboros.Consensus.Block.Abstract (
BlockProtocol
, BlockConfig
, CodecConfig
, StorageConfig
, GetPrevHash (..)
, blockPrevHash
, GetHeader (..)
, Header
, blockIsEBB
, blockToIsEBB
, getBlockHeaderFields
, headerHash
, headerPoint
, headerToIsEBB
, ConvertRawHash (..)
, decodeRawHash
, encodeRawHash
, succWithOrigin
, ChainHash (..)
, HasHeader (..)
, HeaderFields (..)
, HeaderHash
, Point (GenesisPoint, BlockPoint)
, StandardHash
, blockHash
, blockNo
, blockPoint
, blockSlot
, castHash
, castHeaderFields
, castPoint
, pointHash
, pointSlot
, BlockNo (..)
, EpochNo (..)
, EpochSize (..)
, SlotNo (..)
, WithOrigin (Origin, NotOrigin)
, fromWithOrigin
, withOrigin
, withOriginFromMaybe
, withOriginToMaybe
) where
import qualified Codec.Serialise as Serialise
import Codec.Serialise.Decoding (Decoder)
import Codec.Serialise.Encoding (Encoding)
import qualified Data.ByteString as Strict
import Data.ByteString.Short (ShortByteString)
import qualified Data.ByteString.Short as Short
import Data.Kind (Type)
import Data.Maybe (isJust)
import Data.Word (Word32)
import Cardano.Slotting.Block (BlockNo (..))
import Cardano.Slotting.Slot (EpochNo (..), EpochSize (..),
SlotNo (..), WithOrigin (Origin), fromWithOrigin,
withOrigin, withOriginFromMaybe, withOriginToMaybe)
import qualified Cardano.Slotting.Slot as Cardano
import Ouroboros.Network.Block (ChainHash (..), HasHeader (..),
HeaderFields (..), HeaderHash, Point, StandardHash,
blockHash, blockNo, blockPoint, blockSlot, castHash,
castHeaderFields, castPoint, pattern BlockPoint,
pattern GenesisPoint, pointHash, pointSlot)
import Ouroboros.Consensus.Block.EBB
type family BlockProtocol blk :: Type
data family BlockConfig blk :: Type
data family CodecConfig blk :: Type
data family StorageConfig blk :: Type
class (HasHeader blk, GetHeader blk) => GetPrevHash blk where
:: Header blk -> ChainHash blk
blockPrevHash :: GetPrevHash blk => blk -> ChainHash blk
blockPrevHash :: blk -> ChainHash blk
blockPrevHash = ChainHash blk -> ChainHash blk
forall b b'.
Coercible (HeaderHash b) (HeaderHash b') =>
ChainHash b -> ChainHash b'
castHash (ChainHash blk -> ChainHash blk)
-> (blk -> ChainHash blk) -> blk -> ChainHash blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header blk -> ChainHash blk
forall blk. GetPrevHash blk => Header blk -> ChainHash blk
headerPrevHash (Header blk -> ChainHash blk)
-> (blk -> Header blk) -> blk -> ChainHash blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. blk -> Header blk
forall blk. GetHeader blk => blk -> Header blk
getHeader
data family blk :: Type
class HasHeader (Header blk) => blk where
:: blk -> Header blk
:: Header blk -> blk -> Bool
:: Header blk -> Maybe EpochNo
headerToIsEBB :: GetHeader blk => Header blk -> IsEBB
= Bool -> IsEBB
toIsEBB (Bool -> IsEBB) -> (Header blk -> Bool) -> Header blk -> IsEBB
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe EpochNo -> Bool
forall a. Maybe a -> Bool
isJust (Maybe EpochNo -> Bool)
-> (Header blk -> Maybe EpochNo) -> Header blk -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header blk -> Maybe EpochNo
forall blk. GetHeader blk => Header blk -> Maybe EpochNo
headerIsEBB
blockIsEBB :: GetHeader blk => blk -> Maybe EpochNo
blockIsEBB :: blk -> Maybe EpochNo
blockIsEBB = Header blk -> Maybe EpochNo
forall blk. GetHeader blk => Header blk -> Maybe EpochNo
headerIsEBB (Header blk -> Maybe EpochNo)
-> (blk -> Header blk) -> blk -> Maybe EpochNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. blk -> Header blk
forall blk. GetHeader blk => blk -> Header blk
getHeader
blockToIsEBB :: GetHeader blk => blk -> IsEBB
blockToIsEBB :: blk -> IsEBB
blockToIsEBB = Header blk -> IsEBB
forall blk. GetHeader blk => Header blk -> IsEBB
headerToIsEBB (Header blk -> IsEBB) -> (blk -> Header blk) -> blk -> IsEBB
forall b c a. (b -> c) -> (a -> b) -> a -> c
. blk -> Header blk
forall blk. GetHeader blk => blk -> Header blk
getHeader
type instance BlockProtocol (Header blk) = BlockProtocol blk
type instance (Header blk) = HeaderHash blk
instance HasHeader blk => StandardHash (Header blk)
getBlockHeaderFields :: GetHeader blk => blk -> HeaderFields blk
= HeaderFields (Header blk) -> HeaderFields blk
forall b b'.
(HeaderHash b ~ HeaderHash b') =>
HeaderFields b -> HeaderFields b'
castHeaderFields (HeaderFields (Header blk) -> HeaderFields blk)
-> (blk -> HeaderFields (Header blk)) -> blk -> HeaderFields blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header blk -> HeaderFields (Header blk)
forall b. HasHeader b => b -> HeaderFields b
getHeaderFields (Header blk -> HeaderFields (Header blk))
-> (blk -> Header blk) -> blk -> HeaderFields (Header blk)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. blk -> Header blk
forall blk. GetHeader blk => blk -> Header blk
getHeader
headerHash :: HasHeader (Header blk) => Header blk -> HeaderHash blk
= Header blk -> HeaderHash blk
forall b. HasHeader b => b -> HeaderHash b
blockHash
headerPoint :: HasHeader (Header blk) => Header blk -> Point blk
= Point (Header blk) -> Point blk
forall b b'.
Coercible (HeaderHash b) (HeaderHash b') =>
Point b -> Point b'
castPoint (Point (Header blk) -> Point blk)
-> (Header blk -> Point (Header blk)) -> Header blk -> Point blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header blk -> Point (Header blk)
forall block. HasHeader block => block -> Point block
blockPoint
class ConvertRawHash blk where
toRawHash :: proxy blk -> HeaderHash blk -> Strict.ByteString
toRawHash proxy blk
p = ShortByteString -> ByteString
Short.fromShort (ShortByteString -> ByteString)
-> (HeaderHash blk -> ShortByteString)
-> HeaderHash blk
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. proxy blk -> HeaderHash blk -> ShortByteString
forall blk (proxy :: * -> *).
ConvertRawHash blk =>
proxy blk -> HeaderHash blk -> ShortByteString
toShortRawHash proxy blk
p
fromRawHash :: proxy blk -> Strict.ByteString -> HeaderHash blk
fromRawHash proxy blk
p = proxy blk -> ShortByteString -> HeaderHash blk
forall blk (proxy :: * -> *).
ConvertRawHash blk =>
proxy blk -> ShortByteString -> HeaderHash blk
fromShortRawHash proxy blk
p (ShortByteString -> HeaderHash blk)
-> (ByteString -> ShortByteString) -> ByteString -> HeaderHash blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShortByteString
Short.toShort
toShortRawHash :: proxy blk -> HeaderHash blk -> ShortByteString
toShortRawHash proxy blk
p = ByteString -> ShortByteString
Short.toShort (ByteString -> ShortByteString)
-> (HeaderHash blk -> ByteString)
-> HeaderHash blk
-> ShortByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. proxy blk -> HeaderHash blk -> ByteString
forall blk (proxy :: * -> *).
ConvertRawHash blk =>
proxy blk -> HeaderHash blk -> ByteString
toRawHash proxy blk
p
fromShortRawHash :: proxy blk -> ShortByteString -> HeaderHash blk
fromShortRawHash proxy blk
p = proxy blk -> ByteString -> HeaderHash blk
forall blk (proxy :: * -> *).
ConvertRawHash blk =>
proxy blk -> ByteString -> HeaderHash blk
fromRawHash proxy blk
p (ByteString -> HeaderHash blk)
-> (ShortByteString -> ByteString)
-> ShortByteString
-> HeaderHash blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ByteString
Short.fromShort
hashSize :: proxy blk -> Word32
{-# MINIMAL hashSize
, (toRawHash | toShortRawHash)
, (fromRawHash | fromShortRawHash) #-}
encodeRawHash :: ConvertRawHash blk
=> proxy blk -> HeaderHash blk -> Encoding
encodeRawHash :: proxy blk -> HeaderHash blk -> Encoding
encodeRawHash proxy blk
p = ShortByteString -> Encoding
forall a. Serialise a => a -> Encoding
Serialise.encode (ShortByteString -> Encoding)
-> (HeaderHash blk -> ShortByteString)
-> HeaderHash blk
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. proxy blk -> HeaderHash blk -> ShortByteString
forall blk (proxy :: * -> *).
ConvertRawHash blk =>
proxy blk -> HeaderHash blk -> ShortByteString
toShortRawHash proxy blk
p
decodeRawHash :: ConvertRawHash blk
=> proxy blk -> forall s. Decoder s (HeaderHash blk)
decodeRawHash :: proxy blk -> forall s. Decoder s (HeaderHash blk)
decodeRawHash proxy blk
p = proxy blk -> ShortByteString -> HeaderHash blk
forall blk (proxy :: * -> *).
ConvertRawHash blk =>
proxy blk -> ShortByteString -> HeaderHash blk
fromShortRawHash proxy blk
p (ShortByteString -> HeaderHash blk)
-> Decoder s ShortByteString -> Decoder s (HeaderHash blk)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ShortByteString
forall a s. Serialise a => Decoder s a
Serialise.decode
{-# COMPLETE Origin, NotOrigin #-}
pattern NotOrigin :: t -> WithOrigin t
pattern $bNotOrigin :: t -> WithOrigin t
$mNotOrigin :: forall r t. WithOrigin t -> (t -> r) -> (Void# -> r) -> r
NotOrigin t = Cardano.At t
succWithOrigin :: (Bounded t, Enum t) => WithOrigin t -> t
succWithOrigin :: WithOrigin t -> t
succWithOrigin = t -> (t -> t) -> WithOrigin t -> t
forall b t. b -> (t -> b) -> WithOrigin t -> b
withOrigin t
forall a. Bounded a => a
minBound t -> t
forall a. Enum a => a -> a
succ