{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Ouroboros.Consensus.Byron.Ledger.Block (
    -- * Hash
    ByronHash (..)
  , mkByronHash
    -- * Block
  , ByronBlock (..)
  , annotateByronBlock
  , mkByronBlock
    -- * Header
  , Header (..)
  , mkBoundaryByronHeader
  , mkByronHeader
  , mkRegularByronHeader
    -- * Dealing with EBBs
  , byronBlockIsEBB
  , byronHeaderIsEBB
  , knownEBBs
    -- * Low-level API
  , UnsizedHeader (..)
  , joinSizeHint
  , mkUnsizedHeader
  , splitSizeHint
  ) where

import qualified Cardano.Chain.Block as CC
import qualified Cardano.Chain.Byron.API as CC
import qualified Cardano.Chain.Slotting as CC
import qualified Cardano.Crypto.Hashing as CC
import           Cardano.Ledger.Binary
import qualified Crypto.Hash as Crypto
import           Data.ByteString (ByteString)
import qualified Data.ByteString as Strict
import           Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import           Data.Proxy
import           Data.Typeable
import           GHC.Generics (Generic)
import           NoThunks.Class (NoThunks (..))
import           Ouroboros.Consensus.Block
import qualified Ouroboros.Consensus.Byron.EBBs as EBBs
import           Ouroboros.Consensus.Byron.Ledger.Conversions
import           Ouroboros.Consensus.Byron.Ledger.Orphans ()
import           Ouroboros.Consensus.Util (ShowProxy (..))
import           Ouroboros.Consensus.Util.Condense
import           Ouroboros.Network.SizeInBytes (SizeInBytes)

{-------------------------------------------------------------------------------
  Header hash
-------------------------------------------------------------------------------}

newtype ByronHash = ByronHash { ByronHash -> HeaderHash
unByronHash :: CC.HeaderHash }
  deriving stock   (ByronHash -> ByronHash -> Bool
(ByronHash -> ByronHash -> Bool)
-> (ByronHash -> ByronHash -> Bool) -> Eq ByronHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ByronHash -> ByronHash -> Bool
== :: ByronHash -> ByronHash -> Bool
$c/= :: ByronHash -> ByronHash -> Bool
/= :: ByronHash -> ByronHash -> Bool
Eq, Eq ByronHash
Eq ByronHash =>
(ByronHash -> ByronHash -> Ordering)
-> (ByronHash -> ByronHash -> Bool)
-> (ByronHash -> ByronHash -> Bool)
-> (ByronHash -> ByronHash -> Bool)
-> (ByronHash -> ByronHash -> Bool)
-> (ByronHash -> ByronHash -> ByronHash)
-> (ByronHash -> ByronHash -> ByronHash)
-> Ord ByronHash
ByronHash -> ByronHash -> Bool
ByronHash -> ByronHash -> Ordering
ByronHash -> ByronHash -> ByronHash
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ByronHash -> ByronHash -> Ordering
compare :: ByronHash -> ByronHash -> Ordering
$c< :: ByronHash -> ByronHash -> Bool
< :: ByronHash -> ByronHash -> Bool
$c<= :: ByronHash -> ByronHash -> Bool
<= :: ByronHash -> ByronHash -> Bool
$c> :: ByronHash -> ByronHash -> Bool
> :: ByronHash -> ByronHash -> Bool
$c>= :: ByronHash -> ByronHash -> Bool
>= :: ByronHash -> ByronHash -> Bool
$cmax :: ByronHash -> ByronHash -> ByronHash
max :: ByronHash -> ByronHash -> ByronHash
$cmin :: ByronHash -> ByronHash -> ByronHash
min :: ByronHash -> ByronHash -> ByronHash
Ord, Int -> ByronHash -> ShowS
[ByronHash] -> ShowS
ByronHash -> String
(Int -> ByronHash -> ShowS)
-> (ByronHash -> String)
-> ([ByronHash] -> ShowS)
-> Show ByronHash
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ByronHash -> ShowS
showsPrec :: Int -> ByronHash -> ShowS
$cshow :: ByronHash -> String
show :: ByronHash -> String
$cshowList :: [ByronHash] -> ShowS
showList :: [ByronHash] -> ShowS
Show, (forall x. ByronHash -> Rep ByronHash x)
-> (forall x. Rep ByronHash x -> ByronHash) -> Generic ByronHash
forall x. Rep ByronHash x -> ByronHash
forall x. ByronHash -> Rep ByronHash x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ByronHash -> Rep ByronHash x
from :: forall x. ByronHash -> Rep ByronHash x
$cto :: forall x. Rep ByronHash x -> ByronHash
to :: forall x. Rep ByronHash x -> ByronHash
Generic)
  deriving newtype (Typeable ByronHash
Typeable ByronHash =>
(ByronHash -> Encoding)
-> ((forall t. EncCBOR t => Proxy t -> Size)
    -> Proxy ByronHash -> Size)
-> ((forall t. EncCBOR t => Proxy t -> Size)
    -> Proxy [ByronHash] -> Size)
-> EncCBOR ByronHash
ByronHash -> Encoding
(forall t. EncCBOR t => Proxy t -> Size)
-> Proxy [ByronHash] -> Size
(forall t. EncCBOR t => Proxy t -> Size) -> Proxy ByronHash -> Size
forall a.
Typeable a =>
(a -> Encoding)
-> ((forall t. EncCBOR t => Proxy t -> Size) -> Proxy a -> Size)
-> ((forall t. EncCBOR t => Proxy t -> Size) -> Proxy [a] -> Size)
-> EncCBOR a
$cencCBOR :: ByronHash -> Encoding
encCBOR :: ByronHash -> Encoding
$cencodedSizeExpr :: (forall t. EncCBOR t => Proxy t -> Size) -> Proxy ByronHash -> Size
encodedSizeExpr :: (forall t. EncCBOR t => Proxy t -> Size) -> Proxy ByronHash -> Size
$cencodedListSizeExpr :: (forall t. EncCBOR t => Proxy t -> Size)
-> Proxy [ByronHash] -> Size
encodedListSizeExpr :: (forall t. EncCBOR t => Proxy t -> Size)
-> Proxy [ByronHash] -> Size
EncCBOR, Typeable ByronHash
Typeable ByronHash =>
(forall s. Decoder s ByronHash)
-> (forall s. Proxy ByronHash -> Decoder s ())
-> (Proxy ByronHash -> Text)
-> DecCBOR ByronHash
Proxy ByronHash -> Text
forall s. Decoder s ByronHash
forall a.
Typeable a =>
(forall s. Decoder s a)
-> (forall s. Proxy a -> Decoder s ())
-> (Proxy a -> Text)
-> DecCBOR a
forall s. Proxy ByronHash -> Decoder s ()
$cdecCBOR :: forall s. Decoder s ByronHash
decCBOR :: forall s. Decoder s ByronHash
$cdropCBOR :: forall s. Proxy ByronHash -> Decoder s ()
dropCBOR :: forall s. Proxy ByronHash -> Decoder s ()
$clabel :: Proxy ByronHash -> Text
label :: Proxy ByronHash -> Text
DecCBOR, ByronHash -> String
(ByronHash -> String) -> Condense ByronHash
forall a. (a -> String) -> Condense a
$ccondense :: ByronHash -> String
condense :: ByronHash -> String
Condense)
  deriving anyclass (Context -> ByronHash -> IO (Maybe ThunkInfo)
Proxy ByronHash -> String
(Context -> ByronHash -> IO (Maybe ThunkInfo))
-> (Context -> ByronHash -> IO (Maybe ThunkInfo))
-> (Proxy ByronHash -> String)
-> NoThunks ByronHash
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
$cnoThunks :: Context -> ByronHash -> IO (Maybe ThunkInfo)
noThunks :: Context -> ByronHash -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> ByronHash -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> ByronHash -> IO (Maybe ThunkInfo)
$cshowTypeOf :: Proxy ByronHash -> String
showTypeOf :: Proxy ByronHash -> String
NoThunks)

mkByronHash :: CC.ABlockOrBoundaryHdr ByteString -> ByronHash
mkByronHash :: ABlockOrBoundaryHdr ByteString -> ByronHash
mkByronHash = HeaderHash -> ByronHash
ByronHash (HeaderHash -> ByronHash)
-> (ABlockOrBoundaryHdr ByteString -> HeaderHash)
-> ABlockOrBoundaryHdr ByteString
-> ByronHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ABlockOrBoundaryHdr ByteString -> HeaderHash
CC.abobHdrHash

instance ConvertRawHash ByronBlock where
  toShortRawHash :: forall (proxy :: * -> *).
proxy ByronBlock -> HeaderHash ByronBlock -> ShortByteString
toShortRawHash   proxy ByronBlock
_ = HeaderHash -> ShortByteString
forall algo a. AbstractHash algo a -> ShortByteString
CC.abstractHashToShort (HeaderHash -> ShortByteString)
-> (ByronHash -> HeaderHash) -> ByronHash -> ShortByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByronHash -> HeaderHash
unByronHash
  fromShortRawHash :: forall (proxy :: * -> *).
proxy ByronBlock -> ShortByteString -> HeaderHash ByronBlock
fromShortRawHash proxy ByronBlock
_ = HeaderHash -> ByronHash
ByronHash (HeaderHash -> ByronHash)
-> (ShortByteString -> HeaderHash) -> ShortByteString -> ByronHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> HeaderHash
forall algo a. ShortByteString -> AbstractHash algo a
CC.unsafeAbstractHashFromShort
  hashSize :: forall (proxy :: * -> *). proxy ByronBlock -> Word32
hashSize         proxy ByronBlock
_ = Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word32) -> Int -> Word32
forall a b. (a -> b) -> a -> b
$ Blake2b_256 -> Int
forall a. HashAlgorithm a => a -> Int
Crypto.hashDigestSize
                                        (String -> Blake2b_256
forall a. HasCallStack => String -> a
error String
"proxy" :: Crypto.Blake2b_256)

{-------------------------------------------------------------------------------
  Block
-------------------------------------------------------------------------------}

-- | Byron block
--
-- We cache two bits of information:
--
-- * We cache the slot number as this is not readily available for EBBs.
--   Having it cached allows us to e.g. give a 'HasHeader' instance.
-- * We cache the hash as this is expensive to compute and we need it often.
data ByronBlock = ByronBlock {
      ByronBlock -> ABlockOrBoundary ByteString
byronBlockRaw    :: !(CC.ABlockOrBoundary ByteString)
    , ByronBlock -> SlotNo
byronBlockSlotNo :: !SlotNo
    , ByronBlock -> ByronHash
byronBlockHash   :: !ByronHash
    }
  deriving (ByronBlock -> ByronBlock -> Bool
(ByronBlock -> ByronBlock -> Bool)
-> (ByronBlock -> ByronBlock -> Bool) -> Eq ByronBlock
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ByronBlock -> ByronBlock -> Bool
== :: ByronBlock -> ByronBlock -> Bool
$c/= :: ByronBlock -> ByronBlock -> Bool
/= :: ByronBlock -> ByronBlock -> Bool
Eq, Int -> ByronBlock -> ShowS
[ByronBlock] -> ShowS
ByronBlock -> String
(Int -> ByronBlock -> ShowS)
-> (ByronBlock -> String)
-> ([ByronBlock] -> ShowS)
-> Show ByronBlock
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ByronBlock -> ShowS
showsPrec :: Int -> ByronBlock -> ShowS
$cshow :: ByronBlock -> String
show :: ByronBlock -> String
$cshowList :: [ByronBlock] -> ShowS
showList :: [ByronBlock] -> ShowS
Show)

instance Condense ByronBlock where
  condense :: ByronBlock -> String
condense = ABlockOrBoundary ByteString -> String
forall a. Condense a => a -> String
condense (ABlockOrBoundary ByteString -> String)
-> (ByronBlock -> ABlockOrBoundary ByteString)
-> ByronBlock
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByronBlock -> ABlockOrBoundary ByteString
byronBlockRaw

instance ShowProxy ByronBlock where

mkByronBlock :: CC.EpochSlots -> CC.ABlockOrBoundary ByteString -> ByronBlock
mkByronBlock :: EpochSlots -> ABlockOrBoundary ByteString -> ByronBlock
mkByronBlock EpochSlots
epochSlots ABlockOrBoundary ByteString
blk = ByronBlock {
      byronBlockRaw :: ABlockOrBoundary ByteString
byronBlockRaw    = ABlockOrBoundary ByteString
blk
    , byronBlockSlotNo :: SlotNo
byronBlockSlotNo = SlotNumber -> SlotNo
fromByronSlotNo (SlotNumber -> SlotNo) -> SlotNumber -> SlotNo
forall a b. (a -> b) -> a -> b
$ EpochSlots -> ABlockOrBoundaryHdr ByteString -> SlotNumber
forall a. EpochSlots -> ABlockOrBoundaryHdr a -> SlotNumber
CC.abobHdrSlotNo EpochSlots
epochSlots ABlockOrBoundaryHdr ByteString
hdr
    , byronBlockHash :: ByronHash
byronBlockHash   = ABlockOrBoundaryHdr ByteString -> ByronHash
mkByronHash ABlockOrBoundaryHdr ByteString
hdr
    }
  where
    hdr :: ABlockOrBoundaryHdr ByteString
hdr = ABlockOrBoundary ByteString -> ABlockOrBoundaryHdr ByteString
forall a. ABlockOrBoundary a -> ABlockOrBoundaryHdr a
CC.abobHdrFromBlock ABlockOrBoundary ByteString
blk

-- | Construct Byron block from unannotated 'CC.Block'
--
-- This should be used only when forging blocks (not when receiving blocks
-- over the wire).
annotateByronBlock :: CC.EpochSlots -> CC.Block -> ByronBlock
annotateByronBlock :: EpochSlots -> Block -> ByronBlock
annotateByronBlock EpochSlots
es = EpochSlots -> ABlockOrBoundary ByteString -> ByronBlock
mkByronBlock EpochSlots
es (ABlockOrBoundary ByteString -> ByronBlock)
-> (Block -> ABlockOrBoundary ByteString) -> Block -> ByronBlock
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ABlock ByteString -> ABlockOrBoundary ByteString
forall a. ABlock a -> ABlockOrBoundary a
CC.ABOBBlock (ABlock ByteString -> ABlockOrBoundary ByteString)
-> (Block -> ABlock ByteString)
-> Block
-> ABlockOrBoundary ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EpochSlots -> Block -> ABlock ByteString
CC.reAnnotateBlock EpochSlots
es

{-------------------------------------------------------------------------------
  Header
-------------------------------------------------------------------------------}

-- | Byron header
--
-- See 'ByronBlock' for comments on why we cache certain values.
data instance Header ByronBlock = ByronHeader {
      Header ByronBlock -> ABlockOrBoundaryHdr ByteString
byronHeaderRaw    :: !(CC.ABlockOrBoundaryHdr ByteString)
    , Header ByronBlock -> SlotNo
byronHeaderSlotNo :: !SlotNo
    , Header ByronBlock -> ByronHash
byronHeaderHash   :: !ByronHash

      -- | Hint about the block size
      --
      -- This is used only for the block fetch client. When this value is
      -- wrong, block fetch might make suboptimal decisions, but it shouldn't
      -- /break/ anything
    , Header ByronBlock -> SizeInBytes
byronHeaderBlockSizeHint :: !SizeInBytes
    }
  deriving (Header ByronBlock -> Header ByronBlock -> Bool
(Header ByronBlock -> Header ByronBlock -> Bool)
-> (Header ByronBlock -> Header ByronBlock -> Bool)
-> Eq (Header ByronBlock)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Header ByronBlock -> Header ByronBlock -> Bool
== :: Header ByronBlock -> Header ByronBlock -> Bool
$c/= :: Header ByronBlock -> Header ByronBlock -> Bool
/= :: Header ByronBlock -> Header ByronBlock -> Bool
Eq, Int -> Header ByronBlock -> ShowS
[Header ByronBlock] -> ShowS
Header ByronBlock -> String
(Int -> Header ByronBlock -> ShowS)
-> (Header ByronBlock -> String)
-> ([Header ByronBlock] -> ShowS)
-> Show (Header ByronBlock)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Header ByronBlock -> ShowS
showsPrec :: Int -> Header ByronBlock -> ShowS
$cshow :: Header ByronBlock -> String
show :: Header ByronBlock -> String
$cshowList :: [Header ByronBlock] -> ShowS
showList :: [Header ByronBlock] -> ShowS
Show, (forall x. Header ByronBlock -> Rep (Header ByronBlock) x)
-> (forall x. Rep (Header ByronBlock) x -> Header ByronBlock)
-> Generic (Header ByronBlock)
forall x. Rep (Header ByronBlock) x -> Header ByronBlock
forall x. Header ByronBlock -> Rep (Header ByronBlock) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Header ByronBlock -> Rep (Header ByronBlock) x
from :: forall x. Header ByronBlock -> Rep (Header ByronBlock) x
$cto :: forall x. Rep (Header ByronBlock) x -> Header ByronBlock
to :: forall x. Rep (Header ByronBlock) x -> Header ByronBlock
Generic)

instance GetHeader ByronBlock where
  getHeader :: ByronBlock -> Header ByronBlock
getHeader ByronBlock{ABlockOrBoundary ByteString
SlotNo
ByronHash
byronBlockRaw :: ByronBlock -> ABlockOrBoundary ByteString
byronBlockSlotNo :: ByronBlock -> SlotNo
byronBlockHash :: ByronBlock -> ByronHash
byronBlockRaw :: ABlockOrBoundary ByteString
byronBlockSlotNo :: SlotNo
byronBlockHash :: ByronHash
..} = ByronHeader {
        byronHeaderRaw :: ABlockOrBoundaryHdr ByteString
byronHeaderRaw           = ABlockOrBoundary ByteString -> ABlockOrBoundaryHdr ByteString
forall a. ABlockOrBoundary a -> ABlockOrBoundaryHdr a
CC.abobHdrFromBlock ABlockOrBoundary ByteString
byronBlockRaw
      , byronHeaderSlotNo :: SlotNo
byronHeaderSlotNo        = SlotNo
byronBlockSlotNo
      , byronHeaderHash :: ByronHash
byronHeaderHash          = ByronHash
byronBlockHash
      , byronHeaderBlockSizeHint :: SizeInBytes
byronHeaderBlockSizeHint = (SizeInBytes -> SizeInBytes -> SizeInBytes
forall a. Num a => a -> a -> a
+ SizeInBytes
overhead) (SizeInBytes -> SizeInBytes)
-> (ByteString -> SizeInBytes) -> ByteString -> SizeInBytes
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> SizeInBytes
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> SizeInBytes)
-> (ByteString -> Int) -> ByteString -> SizeInBytes
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
Strict.length (ByteString -> SizeInBytes) -> ByteString -> SizeInBytes
forall a b. (a -> b) -> a -> b
$
          -- For some reason regular blocks lack a 'Decoded' instance
          case ABlockOrBoundary ByteString
byronBlockRaw of
            CC.ABOBBlock    ABlock ByteString
blk -> ABlock ByteString -> ByteString
forall a. ABlock a -> a
CC.blockAnnotation ABlock ByteString
blk
            CC.ABOBBoundary ABoundaryBlock ByteString
blk -> ABoundaryBlock ByteString -> ByteString
forall t. Decoded t => t -> ByteString
recoverBytes       ABoundaryBlock ByteString
blk
      }
    where
      -- The maximum block size is 65536, the CBOR-in-CBOR tag for this block
      -- is:
      --
      -- > D8 18          # tag(24)
      -- >    1A 00010000 # bytes(65536)
      --
      -- Which is 7 bytes, enough for up to 4294967295 bytes.
      overhead :: SizeInBytes
overhead = SizeInBytes
7 {- CBOR-in-CBOR -} SizeInBytes -> SizeInBytes -> SizeInBytes
forall a. Num a => a -> a -> a
+ SizeInBytes
2 {- EBB tag -}

  -- Check if a block matches its header
  --
  -- Note that we cannot check this for an EBB, as the EBB header doesn't
  -- store a hash of the EBB body.
  blockMatchesHeader :: Header ByronBlock -> ByronBlock -> Bool
blockMatchesHeader Header ByronBlock
hdr ByronBlock
blk =
      ABlockOrBoundaryHdr ByteString
-> ABlockOrBoundary ByteString -> Bool
CC.abobMatchesBody (Header ByronBlock -> ABlockOrBoundaryHdr ByteString
byronHeaderRaw Header ByronBlock
hdr) (ByronBlock -> ABlockOrBoundary ByteString
byronBlockRaw ByronBlock
blk)

  headerIsEBB :: Header ByronBlock -> Maybe EpochNo
headerIsEBB Header ByronBlock
hdr = case Header ByronBlock -> ABlockOrBoundaryHdr ByteString
byronHeaderRaw Header ByronBlock
hdr of
    CC.ABOBBlockHdr AHeader ByteString
_       -> Maybe EpochNo
forall a. Maybe a
Nothing
    CC.ABOBBoundaryHdr ABoundaryHeader ByteString
bhdr -> EpochNo -> Maybe EpochNo
forall a. a -> Maybe a
Just
                              (EpochNo -> Maybe EpochNo)
-> (ABoundaryHeader ByteString -> EpochNo)
-> ABoundaryHeader ByteString
-> Maybe EpochNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> EpochNo
EpochNo
                              (Word64 -> EpochNo)
-> (ABoundaryHeader ByteString -> Word64)
-> ABoundaryHeader ByteString
-> EpochNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ABoundaryHeader ByteString -> Word64
forall a. ABoundaryHeader a -> Word64
CC.boundaryEpoch
                              (ABoundaryHeader ByteString -> Maybe EpochNo)
-> ABoundaryHeader ByteString -> Maybe EpochNo
forall a b. (a -> b) -> a -> b
$ ABoundaryHeader ByteString
bhdr

instance Condense (Header ByronBlock) where
  condense :: Header ByronBlock -> String
condense = (AHeader ByteString -> String)
-> (ABoundaryHeader ByteString -> String)
-> ABlockOrBoundaryHdr ByteString
-> String
forall a b.
(AHeader a -> b)
-> (ABoundaryHeader a -> b) -> ABlockOrBoundaryHdr a -> b
CC.aBlockOrBoundaryHdr AHeader ByteString -> String
forall a. Condense a => a -> String
condense ABoundaryHeader ByteString -> String
forall a. Condense a => a -> String
condense (ABlockOrBoundaryHdr ByteString -> String)
-> (Header ByronBlock -> ABlockOrBoundaryHdr ByteString)
-> Header ByronBlock
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header ByronBlock -> ABlockOrBoundaryHdr ByteString
byronHeaderRaw

instance ShowProxy (Header ByronBlock) where

instance NoThunks (Header ByronBlock) where
  showTypeOf :: Proxy (Header ByronBlock) -> String
showTypeOf Proxy (Header ByronBlock)
_ = TypeRep -> String
forall a. Show a => a -> String
show (TypeRep -> String) -> TypeRep -> String
forall a b. (a -> b) -> a -> b
$ Proxy (Header ByronBlock) -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @(Header ByronBlock))

mkByronHeader :: CC.EpochSlots
              -> CC.ABlockOrBoundaryHdr ByteString
              -> SizeInBytes -- ^ Block size hint
              -> Header ByronBlock
mkByronHeader :: EpochSlots
-> ABlockOrBoundaryHdr ByteString
-> SizeInBytes
-> Header ByronBlock
mkByronHeader EpochSlots
epochSlots = UnsizedHeader -> SizeInBytes -> Header ByronBlock
joinSizeHint (UnsizedHeader -> SizeInBytes -> Header ByronBlock)
-> (ABlockOrBoundaryHdr ByteString -> UnsizedHeader)
-> ABlockOrBoundaryHdr ByteString
-> SizeInBytes
-> Header ByronBlock
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EpochSlots -> ABlockOrBoundaryHdr ByteString -> UnsizedHeader
mkUnsizedHeader EpochSlots
epochSlots

mkRegularByronHeader :: CC.AHeader ByteString
                     -> SizeInBytes
                     -> Header ByronBlock
mkRegularByronHeader :: AHeader ByteString -> SizeInBytes -> Header ByronBlock
mkRegularByronHeader = UnsizedHeader -> SizeInBytes -> Header ByronBlock
joinSizeHint (UnsizedHeader -> SizeInBytes -> Header ByronBlock)
-> (AHeader ByteString -> UnsizedHeader)
-> AHeader ByteString
-> SizeInBytes
-> Header ByronBlock
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AHeader ByteString -> UnsizedHeader
mkRegularUnsizedHeader

mkBoundaryByronHeader :: SlotNo
                      -> CC.ABoundaryHeader ByteString
                      -> SizeInBytes
                      -> Header ByronBlock
mkBoundaryByronHeader :: SlotNo
-> ABoundaryHeader ByteString -> SizeInBytes -> Header ByronBlock
mkBoundaryByronHeader SlotNo
slotNo = UnsizedHeader -> SizeInBytes -> Header ByronBlock
joinSizeHint (UnsizedHeader -> SizeInBytes -> Header ByronBlock)
-> (ABoundaryHeader ByteString -> UnsizedHeader)
-> ABoundaryHeader ByteString
-> SizeInBytes
-> Header ByronBlock
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SlotNo -> ABoundaryHeader ByteString -> UnsizedHeader
mkBoundaryUnsizedHeader SlotNo
slotNo

{-------------------------------------------------------------------------------
  HasHeader instances

  This doesn't do much more than pass to the instance for headers.
-------------------------------------------------------------------------------}

type instance HeaderHash ByronBlock = ByronHash
instance StandardHash ByronBlock

instance HasHeader ByronBlock where
  getHeaderFields :: ByronBlock -> HeaderFields ByronBlock
getHeaderFields = ByronBlock -> HeaderFields ByronBlock
forall blk. GetHeader blk => blk -> HeaderFields blk
getBlockHeaderFields

instance HasHeader (Header ByronBlock) where
  getHeaderFields :: Header ByronBlock -> HeaderFields (Header ByronBlock)
getHeaderFields Header ByronBlock
hdr = HeaderFields {
        headerFieldHash :: HeaderHash (Header ByronBlock)
headerFieldHash    = Header ByronBlock -> ByronHash
byronHeaderHash Header ByronBlock
hdr
      , headerFieldSlot :: SlotNo
headerFieldSlot    = Header ByronBlock -> SlotNo
byronHeaderSlotNo Header ByronBlock
hdr
      , headerFieldBlockNo :: BlockNo
headerFieldBlockNo = ChainDifficulty -> BlockNo
fromByronBlockNo (ChainDifficulty -> BlockNo)
-> (ABlockOrBoundaryHdr ByteString -> ChainDifficulty)
-> ABlockOrBoundaryHdr ByteString
-> BlockNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ABlockOrBoundaryHdr ByteString -> ChainDifficulty
forall a. ABlockOrBoundaryHdr a -> ChainDifficulty
CC.abobHdrChainDifficulty (ABlockOrBoundaryHdr ByteString -> BlockNo)
-> ABlockOrBoundaryHdr ByteString -> BlockNo
forall a b. (a -> b) -> a -> b
$ Header ByronBlock -> ABlockOrBoundaryHdr ByteString
byronHeaderRaw Header ByronBlock
hdr
      }

instance GetPrevHash ByronBlock where
  headerPrevHash :: Header ByronBlock -> ChainHash ByronBlock
headerPrevHash = Maybe HeaderHash -> ChainHash ByronBlock
fromByronPrevHash (Maybe HeaderHash -> ChainHash ByronBlock)
-> (Header ByronBlock -> Maybe HeaderHash)
-> Header ByronBlock
-> ChainHash ByronBlock
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ABlockOrBoundaryHdr ByteString -> Maybe HeaderHash
forall a. ABlockOrBoundaryHdr a -> Maybe HeaderHash
CC.abobHdrPrevHash (ABlockOrBoundaryHdr ByteString -> Maybe HeaderHash)
-> (Header ByronBlock -> ABlockOrBoundaryHdr ByteString)
-> Header ByronBlock
-> Maybe HeaderHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header ByronBlock -> ABlockOrBoundaryHdr ByteString
byronHeaderRaw

fromByronPrevHash :: Maybe CC.HeaderHash -> ChainHash ByronBlock
fromByronPrevHash :: Maybe HeaderHash -> ChainHash ByronBlock
fromByronPrevHash = \case
    Maybe HeaderHash
Nothing -> ChainHash ByronBlock
forall {k} (b :: k). ChainHash b
GenesisHash
    Just HeaderHash
h  -> HeaderHash ByronBlock -> ChainHash ByronBlock
forall {k} (b :: k). HeaderHash b -> ChainHash b
BlockHash (HeaderHash -> ByronHash
ByronHash HeaderHash
h)

{-------------------------------------------------------------------------------
  Dealing with EBBs
-------------------------------------------------------------------------------}

byronHeaderIsEBB :: Header ByronBlock -> IsEBB
byronHeaderIsEBB :: Header ByronBlock -> IsEBB
byronHeaderIsEBB = ABlockOrBoundaryHdr ByteString -> IsEBB
forall a. ABlockOrBoundaryHdr a -> IsEBB
go (ABlockOrBoundaryHdr ByteString -> IsEBB)
-> (Header ByronBlock -> ABlockOrBoundaryHdr ByteString)
-> Header ByronBlock
-> IsEBB
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header ByronBlock -> ABlockOrBoundaryHdr ByteString
byronHeaderRaw
  where
    go :: CC.ABlockOrBoundaryHdr a -> IsEBB
    go :: forall a. ABlockOrBoundaryHdr a -> IsEBB
go (CC.ABOBBlockHdr    AHeader a
_) = IsEBB
IsNotEBB
    go (CC.ABOBBoundaryHdr ABoundaryHeader a
_) = IsEBB
IsEBB

byronBlockIsEBB :: ByronBlock -> IsEBB
byronBlockIsEBB :: ByronBlock -> IsEBB
byronBlockIsEBB = Header ByronBlock -> IsEBB
byronHeaderIsEBB (Header ByronBlock -> IsEBB)
-> (ByronBlock -> Header ByronBlock) -> ByronBlock -> IsEBB
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByronBlock -> Header ByronBlock
forall blk. GetHeader blk => blk -> Header blk
getHeader

knownEBBs :: Map (HeaderHash ByronBlock) (ChainHash ByronBlock)
knownEBBs :: Map (HeaderHash ByronBlock) (ChainHash ByronBlock)
knownEBBs = [(HeaderHash ByronBlock, ChainHash ByronBlock)]
-> Map (HeaderHash ByronBlock) (ChainHash ByronBlock)
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(HeaderHash ByronBlock, ChainHash ByronBlock)]
 -> Map (HeaderHash ByronBlock) (ChainHash ByronBlock))
-> [(HeaderHash ByronBlock, ChainHash ByronBlock)]
-> Map (HeaderHash ByronBlock) (ChainHash ByronBlock)
forall a b. (a -> b) -> a -> b
$ ((HeaderHash, Maybe HeaderHash)
 -> (ByronHash, ChainHash ByronBlock))
-> [(HeaderHash, Maybe HeaderHash)]
-> [(ByronHash, ChainHash ByronBlock)]
forall a b. (a -> b) -> [a] -> [b]
map (HeaderHash, Maybe HeaderHash) -> (ByronHash, ChainHash ByronBlock)
aux [(HeaderHash, Maybe HeaderHash)]
EBBs.knownEBBs
  where
    aux :: (CC.HeaderHash, Maybe CC.HeaderHash)
        -> (ByronHash, ChainHash ByronBlock)
    aux :: (HeaderHash, Maybe HeaderHash) -> (ByronHash, ChainHash ByronBlock)
aux (HeaderHash
ebb, Maybe HeaderHash
Nothing)   = (HeaderHash -> ByronHash
ByronHash HeaderHash
ebb, ChainHash ByronBlock
forall {k} (b :: k). ChainHash b
GenesisHash)
    aux (HeaderHash
ebb, Just HeaderHash
prev) = (HeaderHash -> ByronHash
ByronHash HeaderHash
ebb, HeaderHash ByronBlock -> ChainHash ByronBlock
forall {k} (b :: k). HeaderHash b -> ChainHash b
BlockHash (HeaderHash -> ByronHash
ByronHash HeaderHash
prev))

{-------------------------------------------------------------------------------
  Unsized header
-------------------------------------------------------------------------------}

-- | Header without a size hint
--
-- Defined in order to support backwards compatible binary encodings.
data UnsizedHeader = UnsizedHeader {
      UnsizedHeader -> ABlockOrBoundaryHdr ByteString
unsizedHeaderRaw    :: !(CC.ABlockOrBoundaryHdr ByteString)
    , UnsizedHeader -> SlotNo
unsizedHeaderSlotNo :: !SlotNo
    , UnsizedHeader -> ByronHash
unsizedHeaderHash   :: !ByronHash
    }

mkUnsizedHeader :: CC.EpochSlots
                -> CC.ABlockOrBoundaryHdr ByteString
                -> UnsizedHeader
mkUnsizedHeader :: EpochSlots -> ABlockOrBoundaryHdr ByteString -> UnsizedHeader
mkUnsizedHeader EpochSlots
epochSlots = \case
    CC.ABOBBlockHdr    AHeader ByteString
hdr -> AHeader ByteString -> UnsizedHeader
mkRegularUnsizedHeader AHeader ByteString
hdr
    CC.ABOBBoundaryHdr ABoundaryHeader ByteString
hdr -> SlotNo -> ABoundaryHeader ByteString -> UnsizedHeader
mkBoundaryUnsizedHeader SlotNo
slotNo ABoundaryHeader ByteString
hdr
      where
        slotNo :: SlotNo
slotNo = SlotNumber -> SlotNo
fromByronSlotNo (SlotNumber -> SlotNo) -> SlotNumber -> SlotNo
forall a b. (a -> b) -> a -> b
$
            EpochSlots -> Word64 -> SlotNumber
CC.boundaryBlockSlot EpochSlots
epochSlots (ABoundaryHeader ByteString -> Word64
forall a. ABoundaryHeader a -> Word64
CC.boundaryEpoch ABoundaryHeader ByteString
hdr)

mkRegularUnsizedHeader :: CC.AHeader ByteString -> UnsizedHeader
mkRegularUnsizedHeader :: AHeader ByteString -> UnsizedHeader
mkRegularUnsizedHeader AHeader ByteString
hdr = UnsizedHeader {
      unsizedHeaderRaw :: ABlockOrBoundaryHdr ByteString
unsizedHeaderRaw    = ABlockOrBoundaryHdr ByteString
hdr'
    , unsizedHeaderSlotNo :: SlotNo
unsizedHeaderSlotNo = SlotNumber -> SlotNo
fromByronSlotNo (SlotNumber -> SlotNo) -> SlotNumber -> SlotNo
forall a b. (a -> b) -> a -> b
$ AHeader ByteString -> SlotNumber
forall a. AHeader a -> SlotNumber
CC.headerSlot AHeader ByteString
hdr
    , unsizedHeaderHash :: ByronHash
unsizedHeaderHash   = ABlockOrBoundaryHdr ByteString -> ByronHash
mkByronHash ABlockOrBoundaryHdr ByteString
hdr'
    }
  where
    hdr' :: CC.ABlockOrBoundaryHdr ByteString
    hdr' :: ABlockOrBoundaryHdr ByteString
hdr' = AHeader ByteString -> ABlockOrBoundaryHdr ByteString
forall a. AHeader a -> ABlockOrBoundaryHdr a
CC.ABOBBlockHdr AHeader ByteString
hdr

-- | For a boundary header, we must be told the slot
mkBoundaryUnsizedHeader :: SlotNo
                        -> CC.ABoundaryHeader ByteString
                        -> UnsizedHeader
mkBoundaryUnsizedHeader :: SlotNo -> ABoundaryHeader ByteString -> UnsizedHeader
mkBoundaryUnsizedHeader SlotNo
slotNo ABoundaryHeader ByteString
hdr = UnsizedHeader {
      unsizedHeaderRaw :: ABlockOrBoundaryHdr ByteString
unsizedHeaderRaw    = ABlockOrBoundaryHdr ByteString
hdr'
    , unsizedHeaderSlotNo :: SlotNo
unsizedHeaderSlotNo = SlotNo
slotNo
    , unsizedHeaderHash :: ByronHash
unsizedHeaderHash   = ABlockOrBoundaryHdr ByteString -> ByronHash
mkByronHash ABlockOrBoundaryHdr ByteString
hdr'
    }
  where
    hdr' :: CC.ABlockOrBoundaryHdr ByteString
    hdr' :: ABlockOrBoundaryHdr ByteString
hdr' = ABoundaryHeader ByteString -> ABlockOrBoundaryHdr ByteString
forall a. ABoundaryHeader a -> ABlockOrBoundaryHdr a
CC.ABOBBoundaryHdr ABoundaryHeader ByteString
hdr

splitSizeHint :: Header ByronBlock -> (UnsizedHeader, SizeInBytes)
splitSizeHint :: Header ByronBlock -> (UnsizedHeader, SizeInBytes)
splitSizeHint ByronHeader{ABlockOrBoundaryHdr ByteString
SlotNo
SizeInBytes
ByronHash
byronHeaderRaw :: Header ByronBlock -> ABlockOrBoundaryHdr ByteString
byronHeaderSlotNo :: Header ByronBlock -> SlotNo
byronHeaderHash :: Header ByronBlock -> ByronHash
byronHeaderBlockSizeHint :: Header ByronBlock -> SizeInBytes
byronHeaderRaw :: ABlockOrBoundaryHdr ByteString
byronHeaderSlotNo :: SlotNo
byronHeaderHash :: ByronHash
byronHeaderBlockSizeHint :: SizeInBytes
..} = (
      UnsizedHeader {
          unsizedHeaderRaw :: ABlockOrBoundaryHdr ByteString
unsizedHeaderRaw    = ABlockOrBoundaryHdr ByteString
byronHeaderRaw
        , unsizedHeaderSlotNo :: SlotNo
unsizedHeaderSlotNo = SlotNo
byronHeaderSlotNo
        , unsizedHeaderHash :: ByronHash
unsizedHeaderHash   = ByronHash
byronHeaderHash
        }
    , SizeInBytes
byronHeaderBlockSizeHint
    )

joinSizeHint :: UnsizedHeader -> SizeInBytes -> Header ByronBlock
joinSizeHint :: UnsizedHeader -> SizeInBytes -> Header ByronBlock
joinSizeHint UnsizedHeader{ABlockOrBoundaryHdr ByteString
SlotNo
ByronHash
unsizedHeaderRaw :: UnsizedHeader -> ABlockOrBoundaryHdr ByteString
unsizedHeaderSlotNo :: UnsizedHeader -> SlotNo
unsizedHeaderHash :: UnsizedHeader -> ByronHash
unsizedHeaderRaw :: ABlockOrBoundaryHdr ByteString
unsizedHeaderSlotNo :: SlotNo
unsizedHeaderHash :: ByronHash
..} SizeInBytes
size = ByronHeader {
      byronHeaderRaw :: ABlockOrBoundaryHdr ByteString
byronHeaderRaw           = ABlockOrBoundaryHdr ByteString
unsizedHeaderRaw
    , byronHeaderSlotNo :: SlotNo
byronHeaderSlotNo        = SlotNo
unsizedHeaderSlotNo
    , byronHeaderHash :: ByronHash
byronHeaderHash          = ByronHash
unsizedHeaderHash
    , byronHeaderBlockSizeHint :: SizeInBytes
byronHeaderBlockSizeHint = SizeInBytes
size
    }