{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
module Ouroboros.Consensus.Block.RealPoint (
RealPoint (..)
, decodeRealPoint
, encodeRealPoint
, blockRealPoint
, castRealPoint
, headerRealPoint
, pointToWithOriginRealPoint
, realPointHash
, realPointSlot
, realPointToPoint
, withOriginRealPointToPoint
) where
import Cardano.Binary (enforceSize)
import Codec.CBOR.Decoding (Decoder)
import Codec.CBOR.Encoding (Encoding, encodeListLen)
import Codec.Serialise (decode, encode)
import Data.Coerce
import Data.Proxy
import Data.Typeable (Typeable, typeRep)
import GHC.Generics
import NoThunks.Class (NoThunks (..))
import Ouroboros.Consensus.Block.Abstract
import Ouroboros.Consensus.Util.Condense
data RealPoint blk = RealPoint !SlotNo !(HeaderHash blk)
deriving ((forall x. RealPoint blk -> Rep (RealPoint blk) x)
-> (forall x. Rep (RealPoint blk) x -> RealPoint blk)
-> Generic (RealPoint blk)
forall x. Rep (RealPoint blk) x -> RealPoint blk
forall x. RealPoint blk -> Rep (RealPoint blk) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall blk x. Rep (RealPoint blk) x -> RealPoint blk
forall blk x. RealPoint blk -> Rep (RealPoint blk) x
$cfrom :: forall blk x. RealPoint blk -> Rep (RealPoint blk) x
from :: forall x. RealPoint blk -> Rep (RealPoint blk) x
$cto :: forall blk x. Rep (RealPoint blk) x -> RealPoint blk
to :: forall x. Rep (RealPoint blk) x -> RealPoint blk
Generic)
deriving instance StandardHash blk => Eq (RealPoint blk)
deriving instance StandardHash blk => Ord (RealPoint blk)
deriving instance StandardHash blk => Show (RealPoint blk)
instance (StandardHash blk, Typeable blk)
=> NoThunks (RealPoint blk) where
showTypeOf :: Proxy (RealPoint blk) -> String
showTypeOf Proxy (RealPoint blk)
_ = TypeRep -> String
forall a. Show a => a -> String
show (TypeRep -> String) -> TypeRep -> String
forall a b. (a -> b) -> a -> b
$ Proxy (RealPoint blk) -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @(RealPoint blk))
instance Condense (HeaderHash blk) => Condense (RealPoint blk) where
condense :: RealPoint blk -> String
condense (RealPoint SlotNo
s HeaderHash blk
h) = String
"(Point " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> SlotNo -> String
forall a. Condense a => a -> String
condense SlotNo
s String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
", " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> HeaderHash blk -> String
forall a. Condense a => a -> String
condense HeaderHash blk
h String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
encodeRealPoint :: (HeaderHash blk -> Encoding)
-> (RealPoint blk -> Encoding)
encodeRealPoint :: forall blk.
(HeaderHash blk -> Encoding) -> RealPoint blk -> Encoding
encodeRealPoint HeaderHash blk -> Encoding
encodeHash (RealPoint SlotNo
s HeaderHash blk
h) = [Encoding] -> Encoding
forall a. Monoid a => [a] -> a
mconcat [
Word -> Encoding
encodeListLen Word
2
, SlotNo -> Encoding
forall a. Serialise a => a -> Encoding
encode SlotNo
s
, HeaderHash blk -> Encoding
encodeHash HeaderHash blk
h
]
decodeRealPoint :: (forall s. Decoder s (HeaderHash blk))
-> (forall s. Decoder s (RealPoint blk))
decodeRealPoint :: forall blk.
(forall s. Decoder s (HeaderHash blk))
-> forall s. Decoder s (RealPoint blk)
decodeRealPoint forall s. Decoder s (HeaderHash blk)
decodeHash = do
Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"RealPoint" Int
2
SlotNo -> HeaderHash blk -> RealPoint blk
forall blk. SlotNo -> HeaderHash blk -> RealPoint blk
RealPoint (SlotNo -> HeaderHash blk -> RealPoint blk)
-> Decoder s SlotNo -> Decoder s (HeaderHash blk -> RealPoint blk)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s SlotNo
forall s. Decoder s SlotNo
forall a s. Serialise a => Decoder s a
decode Decoder s (HeaderHash blk -> RealPoint blk)
-> Decoder s (HeaderHash blk) -> Decoder s (RealPoint blk)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (HeaderHash blk)
forall s. Decoder s (HeaderHash blk)
decodeHash
realPointSlot :: RealPoint blk -> SlotNo
realPointSlot :: forall blk. RealPoint blk -> SlotNo
realPointSlot (RealPoint SlotNo
s HeaderHash blk
_) = SlotNo
s
realPointHash :: RealPoint blk -> HeaderHash blk
realPointHash :: forall blk. RealPoint blk -> HeaderHash blk
realPointHash (RealPoint SlotNo
_ HeaderHash blk
h) = HeaderHash blk
h
blockRealPoint :: HasHeader blk => blk -> RealPoint blk
blockRealPoint :: forall blk. HasHeader blk => blk -> RealPoint blk
blockRealPoint blk
blk = SlotNo -> HeaderHash blk -> RealPoint blk
forall blk. SlotNo -> HeaderHash blk -> RealPoint blk
RealPoint SlotNo
s HeaderHash blk
h
where
HeaderFields { headerFieldSlot :: forall k (b :: k). HeaderFields b -> SlotNo
headerFieldSlot = SlotNo
s, headerFieldHash :: forall k (b :: k). HeaderFields b -> HeaderHash b
headerFieldHash = HeaderHash blk
h } = blk -> HeaderFields blk
forall b. HasHeader b => b -> HeaderFields b
getHeaderFields blk
blk
headerRealPoint ::
( HasHeader (Header blk)
#if __GLASGOW_HASKELL__ >= 904
, HasHeader blk
#endif
)
=> Header blk
-> RealPoint blk
Header blk
hdr = SlotNo -> HeaderHash blk -> RealPoint blk
forall blk. SlotNo -> HeaderHash blk -> RealPoint blk
RealPoint SlotNo
s HeaderHash blk
h
where
HeaderFields { headerFieldSlot :: forall k (b :: k). HeaderFields b -> SlotNo
headerFieldSlot = SlotNo
s, headerFieldHash :: forall k (b :: k). HeaderFields b -> HeaderHash b
headerFieldHash = HeaderHash (Header blk)
h } = Header blk -> HeaderFields (Header blk)
forall b. HasHeader b => b -> HeaderFields b
getHeaderFields Header blk
hdr
realPointToPoint :: RealPoint blk -> Point blk
realPointToPoint :: forall blk. RealPoint blk -> Point blk
realPointToPoint (RealPoint SlotNo
s HeaderHash blk
h) = SlotNo -> HeaderHash blk -> Point blk
forall {k} (block :: k). SlotNo -> HeaderHash block -> Point block
BlockPoint SlotNo
s HeaderHash blk
h
withOriginRealPointToPoint :: WithOrigin (RealPoint blk) -> Point blk
withOriginRealPointToPoint :: forall blk. WithOrigin (RealPoint blk) -> Point blk
withOriginRealPointToPoint WithOrigin (RealPoint blk)
Origin = Point blk
forall {k} (block :: k). Point block
GenesisPoint
withOriginRealPointToPoint (NotOrigin RealPoint blk
p) = RealPoint blk -> Point blk
forall blk. RealPoint blk -> Point blk
realPointToPoint RealPoint blk
p
pointToWithOriginRealPoint :: Point blk -> WithOrigin (RealPoint blk)
pointToWithOriginRealPoint :: forall blk. Point blk -> WithOrigin (RealPoint blk)
pointToWithOriginRealPoint Point blk
GenesisPoint = WithOrigin (RealPoint blk)
forall t. WithOrigin t
Origin
pointToWithOriginRealPoint (BlockPoint SlotNo
s HeaderHash blk
h) = RealPoint blk -> WithOrigin (RealPoint blk)
forall t. t -> WithOrigin t
NotOrigin (RealPoint blk -> WithOrigin (RealPoint blk))
-> RealPoint blk -> WithOrigin (RealPoint blk)
forall a b. (a -> b) -> a -> b
$ SlotNo -> HeaderHash blk -> RealPoint blk
forall blk. SlotNo -> HeaderHash blk -> RealPoint blk
RealPoint SlotNo
s HeaderHash blk
h
castRealPoint ::
forall blk blk'. Coercible (HeaderHash blk) (HeaderHash blk')
=> RealPoint blk
-> RealPoint blk'
castRealPoint :: forall blk blk'.
Coercible (HeaderHash blk) (HeaderHash blk') =>
RealPoint blk -> RealPoint blk'
castRealPoint (RealPoint SlotNo
s HeaderHash blk
h) = SlotNo -> HeaderHash blk' -> RealPoint blk'
forall blk. SlotNo -> HeaderHash blk -> RealPoint blk
RealPoint SlotNo
s (HeaderHash blk -> HeaderHash blk'
forall a b. Coercible a b => a -> b
coerce HeaderHash blk
h)