{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumDecimals #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeSynonymInstances #-}

module Cardano.Chain.Block.Header
  ( -- * Header
    Header,
    AHeader (..),

    -- * Header Constructors
    mkHeader,
    mkHeaderExplicit,

    -- * Header Accessors
    headerProtocolMagicId,
    headerPrevHash,
    headerProof,
    headerSlot,
    headerIssuer,
    headerLength,
    headerDifficulty,
    headerToSign,

    -- * Header Binary Serialization
    toCBORHeader,
    toCBORHeaderSize,
    toCBORHeaderToHash,
    fromCBORAHeader,
    fromCBORHeader,
    fromCBORHeaderToHash,
    wrapHeaderBytes,
    toCBORBlockVersions,
    toCBORBlockVersionsSize,

    -- * Header Formatting
    renderHeader,

    -- * Boundary Header
    ABoundaryHeader (..),
    mkABoundaryHeader,
    toCBORABoundaryHeader,
    toCBORABoundaryHeaderSize,
    fromCBORABoundaryHeader,
    boundaryHeaderHashAnnotated,
    wrapBoundaryBytes,

    -- * HeaderHash
    HeaderHash,
    headerHashF,
    hashHeader,
    headerHashAnnotated,
    genesisHeaderHash,

    -- * BlockSignature
    BlockSignature,
    ABlockSignature (..),

    -- * ToSign
    ToSign (..),
    recoverSignedBytes,
  )
where

import Cardano.Binary
  ( Annotated (..),
    ByteSpan,
    Case (..),
    Decoded (..),
    Decoder,
    DecoderError (..),
    Encoding,
    FromCBOR (..),
    Raw,
    Size,
    ToCBOR (..),
    annotatedDecoder,
    dropBytes,
    dropInt32,
    encodeListLen,
    enforceSize,
    fromCBORAnnotated,
    serializeEncoding,
    szCases,
    szGreedy,
  )
import Cardano.Chain.Block.Body (Body)
import Cardano.Chain.Block.Boundary
  ( dropBoundaryExtraHeaderDataRetainGenesisTag,
    fromCBORBoundaryConsensusData,
  )
import Cardano.Chain.Block.Proof (Proof (..), mkProof)
import Cardano.Chain.Common (ChainDifficulty (..), dropEmptyAttributes)
import qualified Cardano.Chain.Delegation.Certificate as Delegation
import Cardano.Chain.Genesis.Hash (GenesisHash (..))
import Cardano.Chain.Slotting
  ( EpochAndSlotCount (..),
    EpochSlots,
    SlotNumber (..),
    WithEpochSlots (WithEpochSlots),
    fromSlotNumber,
    toSlotNumber,
  )
import Cardano.Chain.Update.ProtocolVersion (ProtocolVersion)
import Cardano.Chain.Update.SoftwareVersion (SoftwareVersion)
import Cardano.Crypto
  ( Hash,
    ProtocolMagicId (..),
    SignTag (..),
    Signature,
    SigningKey,
    VerificationKey,
    hashDecoded,
    hashHexF,
    hashRaw,
    serializeCborHash,
    sign,
    unsafeAbstractHash,
  )
import Cardano.Prelude
import Data.Aeson (ToJSON)
import qualified Data.ByteString as BS
import Data.Coerce (coerce)
import qualified Data.Map.Strict as Map (singleton)
import Data.Text.Lazy.Builder (Builder)
import Formatting (Format, bprint, build, int)
import qualified Formatting.Buildable as B
import NoThunks.Class (NoThunks (..))

--------------------------------------------------------------------------------
-- Header
--------------------------------------------------------------------------------

type Header = AHeader ()

data AHeader a = AHeader
  { AHeader a -> Annotated ProtocolMagicId a
aHeaderProtocolMagicId :: !(Annotated ProtocolMagicId a),
    -- | Pointer to the header of the previous block
    AHeader a -> Annotated HeaderHash a
aHeaderPrevHash :: !(Annotated HeaderHash a),
    -- | The slot number this block was published for
    AHeader a -> Annotated SlotNumber a
aHeaderSlot :: !(Annotated SlotNumber a),
    -- | The chain difficulty up to this block
    AHeader a -> Annotated ChainDifficulty a
aHeaderDifficulty :: !(Annotated ChainDifficulty a),
    -- | The version of the protocol parameters this block is using
    AHeader a -> ProtocolVersion
headerProtocolVersion :: !ProtocolVersion,
    -- | The software version this block was published from
    AHeader a -> SoftwareVersion
headerSoftwareVersion :: !SoftwareVersion,
    -- | Proof of body
    AHeader a -> Annotated Proof a
aHeaderProof :: !(Annotated Proof a),
    -- | The genesis key that is delegating to publish this block
    AHeader a -> VerificationKey
headerGenesisKey :: !VerificationKey,
    -- | The signature of the block, which contains the delegation certificate
    AHeader a -> ABlockSignature a
headerSignature :: !(ABlockSignature a),
    -- | An annotation that captures the full header bytes
    AHeader a -> a
headerAnnotation :: !a,
    -- | An annotation that captures the bytes from the deprecated ExtraHeaderData
    AHeader a -> a
headerExtraAnnotation :: !a
  }
  deriving (AHeader a -> AHeader a -> Bool
(AHeader a -> AHeader a -> Bool)
-> (AHeader a -> AHeader a -> Bool) -> Eq (AHeader a)
forall a. Eq a => AHeader a -> AHeader a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AHeader a -> AHeader a -> Bool
$c/= :: forall a. Eq a => AHeader a -> AHeader a -> Bool
== :: AHeader a -> AHeader a -> Bool
$c== :: forall a. Eq a => AHeader a -> AHeader a -> Bool
Eq, Int -> AHeader a -> ShowS
[AHeader a] -> ShowS
AHeader a -> String
(Int -> AHeader a -> ShowS)
-> (AHeader a -> String)
-> ([AHeader a] -> ShowS)
-> Show (AHeader a)
forall a. Show a => Int -> AHeader a -> ShowS
forall a. Show a => [AHeader a] -> ShowS
forall a. Show a => AHeader a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AHeader a] -> ShowS
$cshowList :: forall a. Show a => [AHeader a] -> ShowS
show :: AHeader a -> String
$cshow :: forall a. Show a => AHeader a -> String
showsPrec :: Int -> AHeader a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> AHeader a -> ShowS
Show, (forall x. AHeader a -> Rep (AHeader a) x)
-> (forall x. Rep (AHeader a) x -> AHeader a)
-> Generic (AHeader a)
forall x. Rep (AHeader a) x -> AHeader a
forall x. AHeader a -> Rep (AHeader a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (AHeader a) x -> AHeader a
forall a x. AHeader a -> Rep (AHeader a) x
$cto :: forall a x. Rep (AHeader a) x -> AHeader a
$cfrom :: forall a x. AHeader a -> Rep (AHeader a) x
Generic, AHeader a -> ()
(AHeader a -> ()) -> NFData (AHeader a)
forall a. NFData a => AHeader a -> ()
forall a. (a -> ()) -> NFData a
rnf :: AHeader a -> ()
$crnf :: forall a. NFData a => AHeader a -> ()
NFData, a -> AHeader b -> AHeader a
(a -> b) -> AHeader a -> AHeader b
(forall a b. (a -> b) -> AHeader a -> AHeader b)
-> (forall a b. a -> AHeader b -> AHeader a) -> Functor AHeader
forall a b. a -> AHeader b -> AHeader a
forall a b. (a -> b) -> AHeader a -> AHeader b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> AHeader b -> AHeader a
$c<$ :: forall a b. a -> AHeader b -> AHeader a
fmap :: (a -> b) -> AHeader a -> AHeader b
$cfmap :: forall a b. (a -> b) -> AHeader a -> AHeader b
Functor, Context -> AHeader a -> IO (Maybe ThunkInfo)
Proxy (AHeader a) -> String
(Context -> AHeader a -> IO (Maybe ThunkInfo))
-> (Context -> AHeader a -> IO (Maybe ThunkInfo))
-> (Proxy (AHeader a) -> String)
-> NoThunks (AHeader a)
forall a.
NoThunks a =>
Context -> AHeader a -> IO (Maybe ThunkInfo)
forall a. NoThunks a => Proxy (AHeader a) -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (AHeader a) -> String
$cshowTypeOf :: forall a. NoThunks a => Proxy (AHeader a) -> String
wNoThunks :: Context -> AHeader a -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall a.
NoThunks a =>
Context -> AHeader a -> IO (Maybe ThunkInfo)
noThunks :: Context -> AHeader a -> IO (Maybe ThunkInfo)
$cnoThunks :: forall a.
NoThunks a =>
Context -> AHeader a -> IO (Maybe ThunkInfo)
NoThunks)

-- Used for debugging purposes only
instance ToJSON a => ToJSON (AHeader a)

--------------------------------------------------------------------------------
-- Header Constructors
--------------------------------------------------------------------------------

-- | Smart constructor for 'Header'
mkHeader ::
  ProtocolMagicId ->
  Either GenesisHash Header ->
  -- | Number of slots per epoch. This is needed to convert the slot number to
  -- the legacy format used in 'ToSign', where a slot is identified by the
  -- epoch to which it belongs and the offset within that epoch (counted in
  -- number of slots).
  EpochSlots ->
  SlotNumber ->
  -- | The 'SigningKey' used for signing the block
  SigningKey ->
  -- | A certificate of delegation from a genesis key to the 'SigningKey'
  Delegation.Certificate ->
  Body ->
  ProtocolVersion ->
  SoftwareVersion ->
  Header
mkHeader :: ProtocolMagicId
-> Either GenesisHash Header
-> EpochSlots
-> SlotNumber
-> SigningKey
-> Certificate
-> Body
-> ProtocolVersion
-> SoftwareVersion
-> Header
mkHeader ProtocolMagicId
pm Either GenesisHash Header
prevHeader EpochSlots
epochSlots =
  ProtocolMagicId
-> HeaderHash
-> ChainDifficulty
-> EpochSlots
-> SlotNumber
-> SigningKey
-> Certificate
-> Body
-> ProtocolVersion
-> SoftwareVersion
-> Header
mkHeaderExplicit
    ProtocolMagicId
pm
    HeaderHash
prevHash
    ChainDifficulty
difficulty
    EpochSlots
epochSlots
  where
    prevHash :: HeaderHash
prevHash = (GenesisHash -> HeaderHash)
-> (Header -> HeaderHash)
-> Either GenesisHash Header
-> HeaderHash
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either GenesisHash -> HeaderHash
genesisHeaderHash (EpochSlots -> Header -> HeaderHash
hashHeader EpochSlots
epochSlots) Either GenesisHash Header
prevHeader
    difficulty :: ChainDifficulty
difficulty =
      (GenesisHash -> ChainDifficulty)
-> (Header -> ChainDifficulty)
-> Either GenesisHash Header
-> ChainDifficulty
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
        (ChainDifficulty -> GenesisHash -> ChainDifficulty
forall a b. a -> b -> a
const (ChainDifficulty -> GenesisHash -> ChainDifficulty)
-> ChainDifficulty -> GenesisHash -> ChainDifficulty
forall a b. (a -> b) -> a -> b
$ Word64 -> ChainDifficulty
ChainDifficulty Word64
0)
        (ChainDifficulty -> ChainDifficulty
forall a. Enum a => a -> a
succ (ChainDifficulty -> ChainDifficulty)
-> (Header -> ChainDifficulty) -> Header -> ChainDifficulty
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Header -> ChainDifficulty
forall a. AHeader a -> ChainDifficulty
headerDifficulty)
        Either GenesisHash Header
prevHeader

-- | Make a 'Header' for a given slot, with a given body, parent hash,
--   and difficulty. This takes care of some signing and consensus data.
mkHeaderExplicit ::
  ProtocolMagicId ->
  -- | Parent
  HeaderHash ->
  ChainDifficulty ->
  -- | See 'mkHeader'.
  EpochSlots ->
  SlotNumber ->
  -- | The 'SigningKey' used for signing the block
  SigningKey ->
  -- | A certificate of delegation from a genesis key to the 'SigningKey'
  Delegation.Certificate ->
  Body ->
  ProtocolVersion ->
  SoftwareVersion ->
  Header
mkHeaderExplicit :: ProtocolMagicId
-> HeaderHash
-> ChainDifficulty
-> EpochSlots
-> SlotNumber
-> SigningKey
-> Certificate
-> Body
-> ProtocolVersion
-> SoftwareVersion
-> Header
mkHeaderExplicit ProtocolMagicId
pm HeaderHash
prevHash ChainDifficulty
difficulty EpochSlots
epochSlots SlotNumber
slotNumber SigningKey
sk Certificate
dlgCert Body
body ProtocolVersion
pv SoftwareVersion
sv =
  Annotated ProtocolMagicId ()
-> Annotated HeaderHash ()
-> Annotated SlotNumber ()
-> Annotated ChainDifficulty ()
-> ProtocolVersion
-> SoftwareVersion
-> Annotated Proof ()
-> VerificationKey
-> ABlockSignature ()
-> ()
-> ()
-> Header
forall a.
Annotated ProtocolMagicId a
-> Annotated HeaderHash a
-> Annotated SlotNumber a
-> Annotated ChainDifficulty a
-> ProtocolVersion
-> SoftwareVersion
-> Annotated Proof a
-> VerificationKey
-> ABlockSignature a
-> a
-> a
-> AHeader a
AHeader
    (ProtocolMagicId -> () -> Annotated ProtocolMagicId ()
forall b a. b -> a -> Annotated b a
Annotated ProtocolMagicId
pm ())
    (HeaderHash -> () -> Annotated HeaderHash ()
forall b a. b -> a -> Annotated b a
Annotated HeaderHash
prevHash ())
    (SlotNumber -> () -> Annotated SlotNumber ()
forall b a. b -> a -> Annotated b a
Annotated SlotNumber
slotNumber ())
    (ChainDifficulty -> () -> Annotated ChainDifficulty ()
forall b a. b -> a -> Annotated b a
Annotated ChainDifficulty
difficulty ())
    ProtocolVersion
pv
    SoftwareVersion
sv
    (Proof -> () -> Annotated Proof ()
forall b a. b -> a -> Annotated b a
Annotated Proof
proof ())
    VerificationKey
genesisVK
    ABlockSignature ()
sig
    ()
    ()
  where
    proof :: Proof
proof = Body -> Proof
mkProof Body
body

    genesisVK :: VerificationKey
genesisVK = Certificate -> VerificationKey
forall a. ACertificate a -> VerificationKey
Delegation.issuerVK Certificate
dlgCert

    sig :: ABlockSignature ()
sig = Certificate -> Signature ToSign -> ABlockSignature ()
forall a. ACertificate a -> Signature ToSign -> ABlockSignature a
ABlockSignature Certificate
dlgCert (Signature ToSign -> ABlockSignature ())
-> Signature ToSign -> ABlockSignature ()
forall a b. (a -> b) -> a -> b
$ ProtocolMagicId
-> SignTag -> SigningKey -> ToSign -> Signature ToSign
forall a.
ToCBOR a =>
ProtocolMagicId -> SignTag -> SigningKey -> a -> Signature a
sign ProtocolMagicId
pm (VerificationKey -> SignTag
SignBlock VerificationKey
genesisVK) SigningKey
sk ToSign
toSign

    toSign :: ToSign
toSign = HeaderHash
-> Proof
-> EpochAndSlotCount
-> ChainDifficulty
-> ProtocolVersion
-> SoftwareVersion
-> ToSign
ToSign HeaderHash
prevHash Proof
proof EpochAndSlotCount
epochAndSlotCount ChainDifficulty
difficulty ProtocolVersion
pv SoftwareVersion
sv

    epochAndSlotCount :: EpochAndSlotCount
epochAndSlotCount = EpochSlots -> SlotNumber -> EpochAndSlotCount
fromSlotNumber EpochSlots
epochSlots SlotNumber
slotNumber

--------------------------------------------------------------------------------
-- Header Accessors
--------------------------------------------------------------------------------

headerProtocolMagicId :: AHeader a -> ProtocolMagicId
headerProtocolMagicId :: AHeader a -> ProtocolMagicId
headerProtocolMagicId = Annotated ProtocolMagicId a -> ProtocolMagicId
forall b a. Annotated b a -> b
unAnnotated (Annotated ProtocolMagicId a -> ProtocolMagicId)
-> (AHeader a -> Annotated ProtocolMagicId a)
-> AHeader a
-> ProtocolMagicId
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AHeader a -> Annotated ProtocolMagicId a
forall a. AHeader a -> Annotated ProtocolMagicId a
aHeaderProtocolMagicId

headerPrevHash :: AHeader a -> HeaderHash
headerPrevHash :: AHeader a -> HeaderHash
headerPrevHash = Annotated HeaderHash a -> HeaderHash
forall b a. Annotated b a -> b
unAnnotated (Annotated HeaderHash a -> HeaderHash)
-> (AHeader a -> Annotated HeaderHash a) -> AHeader a -> HeaderHash
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AHeader a -> Annotated HeaderHash a
forall a. AHeader a -> Annotated HeaderHash a
aHeaderPrevHash

headerSlot :: AHeader a -> SlotNumber
headerSlot :: AHeader a -> SlotNumber
headerSlot = Annotated SlotNumber a -> SlotNumber
forall b a. Annotated b a -> b
unAnnotated (Annotated SlotNumber a -> SlotNumber)
-> (AHeader a -> Annotated SlotNumber a) -> AHeader a -> SlotNumber
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AHeader a -> Annotated SlotNumber a
forall a. AHeader a -> Annotated SlotNumber a
aHeaderSlot

headerDifficulty :: AHeader a -> ChainDifficulty
headerDifficulty :: AHeader a -> ChainDifficulty
headerDifficulty = Annotated ChainDifficulty a -> ChainDifficulty
forall b a. Annotated b a -> b
unAnnotated (Annotated ChainDifficulty a -> ChainDifficulty)
-> (AHeader a -> Annotated ChainDifficulty a)
-> AHeader a
-> ChainDifficulty
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AHeader a -> Annotated ChainDifficulty a
forall a. AHeader a -> Annotated ChainDifficulty a
aHeaderDifficulty

headerProof :: AHeader a -> Proof
headerProof :: AHeader a -> Proof
headerProof = Annotated Proof a -> Proof
forall b a. Annotated b a -> b
unAnnotated (Annotated Proof a -> Proof)
-> (AHeader a -> Annotated Proof a) -> AHeader a -> Proof
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AHeader a -> Annotated Proof a
forall a. AHeader a -> Annotated Proof a
aHeaderProof

headerIssuer :: AHeader a -> VerificationKey
headerIssuer :: AHeader a -> VerificationKey
headerIssuer AHeader a
h = case AHeader a -> ABlockSignature a
forall a. AHeader a -> ABlockSignature a
headerSignature AHeader a
h of
  ABlockSignature ACertificate a
cert Signature ToSign
_ -> ACertificate a -> VerificationKey
forall a. ACertificate a -> VerificationKey
Delegation.delegateVK ACertificate a
cert

headerToSign :: EpochSlots -> AHeader a -> ToSign
headerToSign :: EpochSlots -> AHeader a -> ToSign
headerToSign EpochSlots
epochSlots AHeader a
h =
  HeaderHash
-> Proof
-> EpochAndSlotCount
-> ChainDifficulty
-> ProtocolVersion
-> SoftwareVersion
-> ToSign
ToSign
    (AHeader a -> HeaderHash
forall a. AHeader a -> HeaderHash
headerPrevHash AHeader a
h)
    (AHeader a -> Proof
forall a. AHeader a -> Proof
headerProof AHeader a
h)
    (EpochSlots -> SlotNumber -> EpochAndSlotCount
fromSlotNumber EpochSlots
epochSlots (SlotNumber -> EpochAndSlotCount)
-> SlotNumber -> EpochAndSlotCount
forall a b. (a -> b) -> a -> b
$ AHeader a -> SlotNumber
forall a. AHeader a -> SlotNumber
headerSlot AHeader a
h)
    (AHeader a -> ChainDifficulty
forall a. AHeader a -> ChainDifficulty
headerDifficulty AHeader a
h)
    (AHeader a -> ProtocolVersion
forall a. AHeader a -> ProtocolVersion
headerProtocolVersion AHeader a
h)
    (AHeader a -> SoftwareVersion
forall a. AHeader a -> SoftwareVersion
headerSoftwareVersion AHeader a
h)

headerLength :: AHeader ByteString -> Natural
headerLength :: AHeader ByteString -> Natural
headerLength = Int -> Natural
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Natural)
-> (AHeader ByteString -> Int) -> AHeader ByteString -> Natural
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> Int
BS.length (ByteString -> Int)
-> (AHeader ByteString -> ByteString) -> AHeader ByteString -> Int
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AHeader ByteString -> ByteString
forall a. AHeader a -> a
headerAnnotation

--------------------------------------------------------------------------------
-- Header Binary Serialization
--------------------------------------------------------------------------------

-- | Encode a header, without taking in to account deprecated epoch boundary
-- blocks.
toCBORHeader :: EpochSlots -> Header -> Encoding
toCBORHeader :: EpochSlots -> Header -> Encoding
toCBORHeader EpochSlots
es Header
h =
  Word -> Encoding
encodeListLen Word
5
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ProtocolMagicId -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Header -> ProtocolMagicId
forall a. AHeader a -> ProtocolMagicId
headerProtocolMagicId Header
h)
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> HeaderHash -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Header -> HeaderHash
forall a. AHeader a -> HeaderHash
headerPrevHash Header
h)
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Proof -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Header -> Proof
forall a. AHeader a -> Proof
headerProof Header
h)
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ( Word -> Encoding
encodeListLen Word
4
           Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> EpochAndSlotCount -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (EpochSlots -> SlotNumber -> EpochAndSlotCount
fromSlotNumber EpochSlots
es (SlotNumber -> EpochAndSlotCount)
-> SlotNumber -> EpochAndSlotCount
forall a b. (a -> b) -> a -> b
$ Header -> SlotNumber
forall a. AHeader a -> SlotNumber
headerSlot Header
h)
           Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> VerificationKey -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Header -> VerificationKey
forall a. AHeader a -> VerificationKey
headerGenesisKey Header
h)
           Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ChainDifficulty -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Header -> ChainDifficulty
forall a. AHeader a -> ChainDifficulty
headerDifficulty Header
h)
           Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ABlockSignature () -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Header -> ABlockSignature ()
forall a. AHeader a -> ABlockSignature a
headerSignature Header
h)
       )
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ProtocolVersion -> SoftwareVersion -> Encoding
toCBORBlockVersions (Header -> ProtocolVersion
forall a. AHeader a -> ProtocolVersion
headerProtocolVersion Header
h) (Header -> SoftwareVersion
forall a. AHeader a -> SoftwareVersion
headerSoftwareVersion Header
h)

toCBORHeaderSize :: Proxy EpochSlots -> Proxy (AHeader a) -> Size
toCBORHeaderSize :: Proxy EpochSlots -> Proxy (AHeader a) -> Size
toCBORHeaderSize Proxy EpochSlots
es Proxy (AHeader a)
hdr =
  Size
1 -- encodeListLen 5
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy ProtocolMagicId -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (AHeader a -> ProtocolMagicId
forall a. AHeader a -> ProtocolMagicId
headerProtocolMagicId (AHeader a -> ProtocolMagicId)
-> Proxy (AHeader a) -> Proxy ProtocolMagicId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (AHeader a)
hdr)
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy HeaderHash -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (AHeader a -> HeaderHash
forall a. AHeader a -> HeaderHash
headerPrevHash (AHeader a -> HeaderHash) -> Proxy (AHeader a) -> Proxy HeaderHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (AHeader a)
hdr)
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy Proof -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (AHeader a -> Proof
forall a. AHeader a -> Proof
headerProof (AHeader a -> Proof) -> Proxy (AHeader a) -> Proxy Proof
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (AHeader a)
hdr)
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ ( Size
1
          Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy EpochAndSlotCount -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (EpochSlots -> SlotNumber -> EpochAndSlotCount
fromSlotNumber (EpochSlots -> SlotNumber -> EpochAndSlotCount)
-> Proxy EpochSlots -> Proxy (SlotNumber -> EpochAndSlotCount)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy EpochSlots
es Proxy (SlotNumber -> EpochAndSlotCount)
-> Proxy SlotNumber -> Proxy EpochAndSlotCount
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (AHeader a -> SlotNumber
forall a. AHeader a -> SlotNumber
headerSlot (AHeader a -> SlotNumber) -> Proxy (AHeader a) -> Proxy SlotNumber
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (AHeader a)
hdr))
          Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy VerificationKey -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (AHeader a -> VerificationKey
forall a. AHeader a -> VerificationKey
headerGenesisKey (AHeader a -> VerificationKey)
-> Proxy (AHeader a) -> Proxy VerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (AHeader a)
hdr)
          Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy ChainDifficulty -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (AHeader a -> ChainDifficulty
forall a. AHeader a -> ChainDifficulty
headerDifficulty (AHeader a -> ChainDifficulty)
-> Proxy (AHeader a) -> Proxy ChainDifficulty
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (AHeader a)
hdr)
          -- there is only 'ToCBOR' @ASignature ()@ instance, we
          -- must map 'a' to '()'
          Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy (ABlockSignature ()) -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (Header -> ABlockSignature ()
forall a. AHeader a -> ABlockSignature a
headerSignature (Header -> ABlockSignature ())
-> (AHeader a -> Header) -> AHeader a -> ABlockSignature ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a -> ()) -> AHeader a -> Header
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (() -> a -> ()
forall a b. a -> b -> a
const ()) (AHeader a -> ABlockSignature ())
-> Proxy (AHeader a) -> Proxy (ABlockSignature ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (AHeader a)
hdr)
      )
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy ProtocolVersion -> Proxy SoftwareVersion -> Size
toCBORBlockVersionsSize
      (AHeader a -> ProtocolVersion
forall a. AHeader a -> ProtocolVersion
headerProtocolVersion (AHeader a -> ProtocolVersion)
-> Proxy (AHeader a) -> Proxy ProtocolVersion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (AHeader a)
hdr)
      (AHeader a -> SoftwareVersion
forall a. AHeader a -> SoftwareVersion
headerSoftwareVersion (AHeader a -> SoftwareVersion)
-> Proxy (AHeader a) -> Proxy SoftwareVersion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (AHeader a)
hdr)

toCBORBlockVersions :: ProtocolVersion -> SoftwareVersion -> Encoding
toCBORBlockVersions :: ProtocolVersion -> SoftwareVersion -> Encoding
toCBORBlockVersions ProtocolVersion
pv SoftwareVersion
sv =
  Word -> Encoding
encodeListLen Word
4
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ProtocolVersion -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR ProtocolVersion
pv
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> SoftwareVersion -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR SoftwareVersion
sv
    -- Encoding of empty Attributes
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Map Word8 LByteString -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Map Word8 LByteString
forall a. Monoid a => a
mempty :: Map Word8 LByteString)
    -- Hash of the encoding of empty ExtraBodyData
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Hash Raw -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (LByteString -> Hash Raw
hashRaw LByteString
"\129\160")

toCBORBlockVersionsSize :: Proxy ProtocolVersion -> Proxy SoftwareVersion -> Size
toCBORBlockVersionsSize :: Proxy ProtocolVersion -> Proxy SoftwareVersion -> Size
toCBORBlockVersionsSize Proxy ProtocolVersion
pv Proxy SoftwareVersion
sv =
  Size
1
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy ProtocolVersion -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy Proxy ProtocolVersion
pv
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy SoftwareVersion -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy Proxy SoftwareVersion
sv
    -- empty attributes dictionary
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
1
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy (Hash Raw) -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (Proxy (Hash Raw)
forall k (t :: k). Proxy t
Proxy :: Proxy (Hash Raw))

fromCBORHeader :: EpochSlots -> Decoder s Header
fromCBORHeader :: EpochSlots -> Decoder s Header
fromCBORHeader EpochSlots
epochSlots = AHeader ByteSpan -> Header
forall (f :: * -> *) a. Functor f => f a -> f ()
void (AHeader ByteSpan -> Header)
-> Decoder s (AHeader ByteSpan) -> Decoder s Header
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> EpochSlots -> Decoder s (AHeader ByteSpan)
forall s. EpochSlots -> Decoder s (AHeader ByteSpan)
fromCBORAHeader EpochSlots
epochSlots

fromCBORAHeader :: EpochSlots -> Decoder s (AHeader ByteSpan)
fromCBORAHeader :: EpochSlots -> Decoder s (AHeader ByteSpan)
fromCBORAHeader EpochSlots
epochSlots = do
  Annotated
    ( Annotated ProtocolMagicId ByteSpan
pm,
      Annotated HeaderHash ByteSpan
prevHash,
      Annotated Proof ByteSpan
proof,
      (Annotated SlotNumber ByteSpan
slot, VerificationKey
genesisKey, Annotated ChainDifficulty ByteSpan
difficulty, ABlockSignature ByteSpan
sig),
      Annotated (ProtocolVersion
protocolVersion, SoftwareVersion
softwareVersion) ByteSpan
extraByteSpan
      )
    ByteSpan
byteSpan <-
    Decoder
  s
  (Annotated ProtocolMagicId ByteSpan, Annotated HeaderHash ByteSpan,
   Annotated Proof ByteSpan,
   (Annotated SlotNumber ByteSpan, VerificationKey,
    Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
   Annotated (ProtocolVersion, SoftwareVersion) ByteSpan)
-> Decoder
     s
     (Annotated
        (Annotated ProtocolMagicId ByteSpan, Annotated HeaderHash ByteSpan,
         Annotated Proof ByteSpan,
         (Annotated SlotNumber ByteSpan, VerificationKey,
          Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
         Annotated (ProtocolVersion, SoftwareVersion) ByteSpan)
        ByteSpan)
forall s a. Decoder s a -> Decoder s (Annotated a ByteSpan)
annotatedDecoder (Decoder
   s
   (Annotated ProtocolMagicId ByteSpan, Annotated HeaderHash ByteSpan,
    Annotated Proof ByteSpan,
    (Annotated SlotNumber ByteSpan, VerificationKey,
     Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
    Annotated (ProtocolVersion, SoftwareVersion) ByteSpan)
 -> Decoder
      s
      (Annotated
         (Annotated ProtocolMagicId ByteSpan, Annotated HeaderHash ByteSpan,
          Annotated Proof ByteSpan,
          (Annotated SlotNumber ByteSpan, VerificationKey,
           Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
          Annotated (ProtocolVersion, SoftwareVersion) ByteSpan)
         ByteSpan))
-> Decoder
     s
     (Annotated ProtocolMagicId ByteSpan, Annotated HeaderHash ByteSpan,
      Annotated Proof ByteSpan,
      (Annotated SlotNumber ByteSpan, VerificationKey,
       Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
      Annotated (ProtocolVersion, SoftwareVersion) ByteSpan)
-> Decoder
     s
     (Annotated
        (Annotated ProtocolMagicId ByteSpan, Annotated HeaderHash ByteSpan,
         Annotated Proof ByteSpan,
         (Annotated SlotNumber ByteSpan, VerificationKey,
          Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
         Annotated (ProtocolVersion, SoftwareVersion) ByteSpan)
        ByteSpan)
forall a b. (a -> b) -> a -> b
$ do
      Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"Header" Int
5
      (,,,,)
        (Annotated ProtocolMagicId ByteSpan
 -> Annotated HeaderHash ByteSpan
 -> Annotated Proof ByteSpan
 -> (Annotated SlotNumber ByteSpan, VerificationKey,
     Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan)
 -> Annotated (ProtocolVersion, SoftwareVersion) ByteSpan
 -> (Annotated ProtocolMagicId ByteSpan,
     Annotated HeaderHash ByteSpan, Annotated Proof ByteSpan,
     (Annotated SlotNumber ByteSpan, VerificationKey,
      Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
     Annotated (ProtocolVersion, SoftwareVersion) ByteSpan))
-> Decoder s (Annotated ProtocolMagicId ByteSpan)
-> Decoder
     s
     (Annotated HeaderHash ByteSpan
      -> Annotated Proof ByteSpan
      -> (Annotated SlotNumber ByteSpan, VerificationKey,
          Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan)
      -> Annotated (ProtocolVersion, SoftwareVersion) ByteSpan
      -> (Annotated ProtocolMagicId ByteSpan,
          Annotated HeaderHash ByteSpan, Annotated Proof ByteSpan,
          (Annotated SlotNumber ByteSpan, VerificationKey,
           Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
          Annotated (ProtocolVersion, SoftwareVersion) ByteSpan))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Annotated ProtocolMagicId ByteSpan)
forall a s. FromCBOR a => Decoder s (Annotated a ByteSpan)
fromCBORAnnotated
        Decoder
  s
  (Annotated HeaderHash ByteSpan
   -> Annotated Proof ByteSpan
   -> (Annotated SlotNumber ByteSpan, VerificationKey,
       Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan)
   -> Annotated (ProtocolVersion, SoftwareVersion) ByteSpan
   -> (Annotated ProtocolMagicId ByteSpan,
       Annotated HeaderHash ByteSpan, Annotated Proof ByteSpan,
       (Annotated SlotNumber ByteSpan, VerificationKey,
        Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
       Annotated (ProtocolVersion, SoftwareVersion) ByteSpan))
-> Decoder s (Annotated HeaderHash ByteSpan)
-> Decoder
     s
     (Annotated Proof ByteSpan
      -> (Annotated SlotNumber ByteSpan, VerificationKey,
          Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan)
      -> Annotated (ProtocolVersion, SoftwareVersion) ByteSpan
      -> (Annotated ProtocolMagicId ByteSpan,
          Annotated HeaderHash ByteSpan, Annotated Proof ByteSpan,
          (Annotated SlotNumber ByteSpan, VerificationKey,
           Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
          Annotated (ProtocolVersion, SoftwareVersion) ByteSpan))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (Annotated HeaderHash ByteSpan)
forall a s. FromCBOR a => Decoder s (Annotated a ByteSpan)
fromCBORAnnotated
        Decoder
  s
  (Annotated Proof ByteSpan
   -> (Annotated SlotNumber ByteSpan, VerificationKey,
       Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan)
   -> Annotated (ProtocolVersion, SoftwareVersion) ByteSpan
   -> (Annotated ProtocolMagicId ByteSpan,
       Annotated HeaderHash ByteSpan, Annotated Proof ByteSpan,
       (Annotated SlotNumber ByteSpan, VerificationKey,
        Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
       Annotated (ProtocolVersion, SoftwareVersion) ByteSpan))
-> Decoder s (Annotated Proof ByteSpan)
-> Decoder
     s
     ((Annotated SlotNumber ByteSpan, VerificationKey,
       Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan)
      -> Annotated (ProtocolVersion, SoftwareVersion) ByteSpan
      -> (Annotated ProtocolMagicId ByteSpan,
          Annotated HeaderHash ByteSpan, Annotated Proof ByteSpan,
          (Annotated SlotNumber ByteSpan, VerificationKey,
           Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
          Annotated (ProtocolVersion, SoftwareVersion) ByteSpan))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (Annotated Proof ByteSpan)
forall a s. FromCBOR a => Decoder s (Annotated a ByteSpan)
fromCBORAnnotated
        Decoder
  s
  ((Annotated SlotNumber ByteSpan, VerificationKey,
    Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan)
   -> Annotated (ProtocolVersion, SoftwareVersion) ByteSpan
   -> (Annotated ProtocolMagicId ByteSpan,
       Annotated HeaderHash ByteSpan, Annotated Proof ByteSpan,
       (Annotated SlotNumber ByteSpan, VerificationKey,
        Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
       Annotated (ProtocolVersion, SoftwareVersion) ByteSpan))
-> Decoder
     s
     (Annotated SlotNumber ByteSpan, VerificationKey,
      Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan)
-> Decoder
     s
     (Annotated (ProtocolVersion, SoftwareVersion) ByteSpan
      -> (Annotated ProtocolMagicId ByteSpan,
          Annotated HeaderHash ByteSpan, Annotated Proof ByteSpan,
          (Annotated SlotNumber ByteSpan, VerificationKey,
           Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
          Annotated (ProtocolVersion, SoftwareVersion) ByteSpan))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> do
          Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"ConsensusData" Int
4
          (,,,)
            -- Next, we decode a 'EpochAndSlotCount' into a 'SlotNumber': the `EpochAndSlotCount`
            -- used in 'AConsensusData' is encoded as a epoch and slot-count
            -- pair.
            (Annotated SlotNumber ByteSpan
 -> VerificationKey
 -> Annotated ChainDifficulty ByteSpan
 -> ABlockSignature ByteSpan
 -> (Annotated SlotNumber ByteSpan, VerificationKey,
     Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan))
-> Decoder s (Annotated SlotNumber ByteSpan)
-> Decoder
     s
     (VerificationKey
      -> Annotated ChainDifficulty ByteSpan
      -> ABlockSignature ByteSpan
      -> (Annotated SlotNumber ByteSpan, VerificationKey,
          Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Annotated EpochAndSlotCount ByteSpan
 -> Annotated SlotNumber ByteSpan)
-> Decoder s (Annotated EpochAndSlotCount ByteSpan)
-> Decoder s (Annotated SlotNumber ByteSpan)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((EpochAndSlotCount -> SlotNumber)
-> Annotated EpochAndSlotCount ByteSpan
-> Annotated SlotNumber ByteSpan
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (EpochSlots -> EpochAndSlotCount -> SlotNumber
toSlotNumber EpochSlots
epochSlots)) Decoder s (Annotated EpochAndSlotCount ByteSpan)
forall a s. FromCBOR a => Decoder s (Annotated a ByteSpan)
fromCBORAnnotated
            Decoder
  s
  (VerificationKey
   -> Annotated ChainDifficulty ByteSpan
   -> ABlockSignature ByteSpan
   -> (Annotated SlotNumber ByteSpan, VerificationKey,
       Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan))
-> Decoder s VerificationKey
-> Decoder
     s
     (Annotated ChainDifficulty ByteSpan
      -> ABlockSignature ByteSpan
      -> (Annotated SlotNumber ByteSpan, VerificationKey,
          Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s VerificationKey
forall a s. FromCBOR a => Decoder s a
fromCBOR
            Decoder
  s
  (Annotated ChainDifficulty ByteSpan
   -> ABlockSignature ByteSpan
   -> (Annotated SlotNumber ByteSpan, VerificationKey,
       Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan))
-> Decoder s (Annotated ChainDifficulty ByteSpan)
-> Decoder
     s
     (ABlockSignature ByteSpan
      -> (Annotated SlotNumber ByteSpan, VerificationKey,
          Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (Annotated ChainDifficulty ByteSpan)
forall a s. FromCBOR a => Decoder s (Annotated a ByteSpan)
fromCBORAnnotated
            Decoder
  s
  (ABlockSignature ByteSpan
   -> (Annotated SlotNumber ByteSpan, VerificationKey,
       Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan))
-> Decoder s (ABlockSignature ByteSpan)
-> Decoder
     s
     (Annotated SlotNumber ByteSpan, VerificationKey,
      Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (ABlockSignature ByteSpan)
forall a s. FromCBOR a => Decoder s a
fromCBOR
        Decoder
  s
  (Annotated (ProtocolVersion, SoftwareVersion) ByteSpan
   -> (Annotated ProtocolMagicId ByteSpan,
       Annotated HeaderHash ByteSpan, Annotated Proof ByteSpan,
       (Annotated SlotNumber ByteSpan, VerificationKey,
        Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
       Annotated (ProtocolVersion, SoftwareVersion) ByteSpan))
-> Decoder
     s (Annotated (ProtocolVersion, SoftwareVersion) ByteSpan)
-> Decoder
     s
     (Annotated ProtocolMagicId ByteSpan, Annotated HeaderHash ByteSpan,
      Annotated Proof ByteSpan,
      (Annotated SlotNumber ByteSpan, VerificationKey,
       Annotated ChainDifficulty ByteSpan, ABlockSignature ByteSpan),
      Annotated (ProtocolVersion, SoftwareVersion) ByteSpan)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (ProtocolVersion, SoftwareVersion)
-> Decoder
     s (Annotated (ProtocolVersion, SoftwareVersion) ByteSpan)
forall s a. Decoder s a -> Decoder s (Annotated a ByteSpan)
annotatedDecoder Decoder s (ProtocolVersion, SoftwareVersion)
forall s. Decoder s (ProtocolVersion, SoftwareVersion)
fromCBORBlockVersions
  AHeader ByteSpan -> Decoder s (AHeader ByteSpan)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AHeader ByteSpan -> Decoder s (AHeader ByteSpan))
-> AHeader ByteSpan -> Decoder s (AHeader ByteSpan)
forall a b. (a -> b) -> a -> b
$
    Annotated ProtocolMagicId ByteSpan
-> Annotated HeaderHash ByteSpan
-> Annotated SlotNumber ByteSpan
-> Annotated ChainDifficulty ByteSpan
-> ProtocolVersion
-> SoftwareVersion
-> Annotated Proof ByteSpan
-> VerificationKey
-> ABlockSignature ByteSpan
-> ByteSpan
-> ByteSpan
-> AHeader ByteSpan
forall a.
Annotated ProtocolMagicId a
-> Annotated HeaderHash a
-> Annotated SlotNumber a
-> Annotated ChainDifficulty a
-> ProtocolVersion
-> SoftwareVersion
-> Annotated Proof a
-> VerificationKey
-> ABlockSignature a
-> a
-> a
-> AHeader a
AHeader
      Annotated ProtocolMagicId ByteSpan
pm
      Annotated HeaderHash ByteSpan
prevHash
      Annotated SlotNumber ByteSpan
slot
      Annotated ChainDifficulty ByteSpan
difficulty
      ProtocolVersion
protocolVersion
      SoftwareVersion
softwareVersion
      Annotated Proof ByteSpan
proof
      VerificationKey
genesisKey
      ABlockSignature ByteSpan
sig
      ByteSpan
byteSpan
      ByteSpan
extraByteSpan

fromCBORBlockVersions :: Decoder s (ProtocolVersion, SoftwareVersion)
fromCBORBlockVersions :: Decoder s (ProtocolVersion, SoftwareVersion)
fromCBORBlockVersions = do
  Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"BlockVersions" Int
4
  (,) (ProtocolVersion
 -> SoftwareVersion -> (ProtocolVersion, SoftwareVersion))
-> Decoder s ProtocolVersion
-> Decoder
     s (SoftwareVersion -> (ProtocolVersion, SoftwareVersion))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ProtocolVersion
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder s (SoftwareVersion -> (ProtocolVersion, SoftwareVersion))
-> Decoder s SoftwareVersion
-> Decoder s (ProtocolVersion, SoftwareVersion)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s SoftwareVersion
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder s (ProtocolVersion, SoftwareVersion)
-> Decoder s () -> Decoder s (ProtocolVersion, SoftwareVersion)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Decoder s ()
forall s. Dropper s
dropEmptyAttributes Decoder s (ProtocolVersion, SoftwareVersion)
-> Decoder s () -> Decoder s (ProtocolVersion, SoftwareVersion)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Decoder s ()
forall s. Dropper s
dropBytes

instance Decoded (AHeader ByteString) where
  type BaseType (AHeader ByteString) = Header
  recoverBytes :: AHeader ByteString -> ByteString
recoverBytes = AHeader ByteString -> ByteString
forall a. AHeader a -> a
headerAnnotation

-- | Encode a 'Header' accounting for deprecated epoch boundary blocks
--
--   This encoding is only used when hashing the header for backwards
--   compatibility, but should not be used when serializing a header within a
--   block
toCBORHeaderToHash :: EpochSlots -> Header -> Encoding
toCBORHeaderToHash :: EpochSlots -> Header -> Encoding
toCBORHeaderToHash EpochSlots
epochSlots Header
h =
  Word -> Encoding
encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Word
1 :: Word) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> EpochSlots -> Header -> Encoding
toCBORHeader EpochSlots
epochSlots Header
h

fromCBORHeaderToHash :: EpochSlots -> Decoder s (Maybe Header)
fromCBORHeaderToHash :: EpochSlots -> Decoder s (Maybe Header)
fromCBORHeaderToHash EpochSlots
epochSlots = do
  Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"Header" Int
2
  forall s. FromCBOR Word => Decoder s Word
forall a s. FromCBOR a => Decoder s a
fromCBOR @Word Decoder s Word
-> (Word -> Decoder s (Maybe Header)) -> Decoder s (Maybe Header)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Word
0 -> do
      Decoder s (ABoundaryHeader ByteSpan) -> Decoder s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void Decoder s (ABoundaryHeader ByteSpan)
forall s. Decoder s (ABoundaryHeader ByteSpan)
fromCBORABoundaryHeader
      Maybe Header -> Decoder s (Maybe Header)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Header
forall a. Maybe a
Nothing
    Word
1 -> Header -> Maybe Header
forall a. a -> Maybe a
Just (Header -> Maybe Header)
-> Decoder s Header -> Decoder s (Maybe Header)
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> EpochSlots -> Decoder s Header
forall s. EpochSlots -> Decoder s Header
fromCBORHeader EpochSlots
epochSlots
    Word
t -> DecoderError -> Decoder s (Maybe Header)
forall e s a. Buildable e => e -> Decoder s a
cborError (DecoderError -> Decoder s (Maybe Header))
-> DecoderError -> Decoder s (Maybe Header)
forall a b. (a -> b) -> a -> b
$ Text -> Word8 -> DecoderError
DecoderErrorUnknownTag Text
"Header" (Word -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
t)

--------------------------------------------------------------------------------
-- Header Formatting
--------------------------------------------------------------------------------

instance B.Buildable (WithEpochSlots Header) where
  build :: WithEpochSlots Header -> Builder
build (WithEpochSlots EpochSlots
es Header
header) = EpochSlots -> Header -> Builder
renderHeader EpochSlots
es Header
header

renderHeader :: EpochSlots -> Header -> Builder
renderHeader :: EpochSlots -> Header -> Builder
renderHeader EpochSlots
es Header
header =
  Format
  Builder
  (HeaderHash
   -> HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> HeaderHash
-> HeaderHash
-> SlotNumber
-> Word64
-> ProtocolVersion
-> SoftwareVersion
-> VerificationKey
-> ABlockSignature ()
-> Builder
forall a. Format Builder a -> a
bprint
    ( Format
  (HeaderHash
   -> HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (HeaderHash
   -> HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
"Header:\n"
        Format
  (HeaderHash
   -> HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (HeaderHash
   -> HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (HeaderHash
      -> HeaderHash
      -> SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (HeaderHash
      -> HeaderHash
      -> SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (HeaderHash
   -> HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (HeaderHash
   -> HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
"    hash: "
        Format
  (HeaderHash
   -> HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (HeaderHash
   -> HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (HeaderHash
      -> HeaderHash
      -> SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (HeaderHash
      -> HeaderHash
      -> SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (HeaderHash
   -> HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
forall r algo a. Format r (AbstractHash algo a -> r)
hashHexF
        Format
  (HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (HeaderHash
   -> HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (HeaderHash
      -> SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (HeaderHash
      -> HeaderHash
      -> SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
"\n"
        Format
  (HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (HeaderHash
      -> SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (HeaderHash
      -> SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
"    previous block: "
        Format
  (HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (HeaderHash
      -> SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (HeaderHash
      -> SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
forall r algo a. Format r (AbstractHash algo a -> r)
hashHexF
        Format
  (SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (HeaderHash
   -> SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (HeaderHash
      -> SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
"\n"
        Format
  (SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
"    slot: "
        Format
  (SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
forall a r. Buildable a => Format r (a -> r)
build
        Format
  (Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (SlotNumber
   -> Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (SlotNumber
      -> Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
"\n"
        Format
  (Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
"    difficulty: "
        Format
  (Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
forall a r. Integral a => Format r (a -> r)
int
        Format
  (ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (Word64
   -> ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (Word64
      -> ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
"\n"
        Format
  (ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
"    protocol: v"
        Format
  (ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
  (ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
-> Format
     Builder
     (ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (SoftwareVersion
   -> VerificationKey -> ABlockSignature () -> Builder)
  (ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
forall a r. Buildable a => Format r (a -> r)
build
        Format
  (SoftwareVersion
   -> VerificationKey -> ABlockSignature () -> Builder)
  (ProtocolVersion
   -> SoftwareVersion
   -> VerificationKey
   -> ABlockSignature ()
   -> Builder)
-> Format
     Builder
     (SoftwareVersion
      -> VerificationKey -> ABlockSignature () -> Builder)
-> Format
     Builder
     (ProtocolVersion
      -> SoftwareVersion
      -> VerificationKey
      -> ABlockSignature ()
      -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (SoftwareVersion
   -> VerificationKey -> ABlockSignature () -> Builder)
  (SoftwareVersion
   -> VerificationKey -> ABlockSignature () -> Builder)
"\n"
        Format
  (SoftwareVersion
   -> VerificationKey -> ABlockSignature () -> Builder)
  (SoftwareVersion
   -> VerificationKey -> ABlockSignature () -> Builder)
-> Format
     Builder
     (SoftwareVersion
      -> VerificationKey -> ABlockSignature () -> Builder)
-> Format
     Builder
     (SoftwareVersion
      -> VerificationKey -> ABlockSignature () -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (SoftwareVersion
   -> VerificationKey -> ABlockSignature () -> Builder)
  (SoftwareVersion
   -> VerificationKey -> ABlockSignature () -> Builder)
"    software: "
        Format
  (SoftwareVersion
   -> VerificationKey -> ABlockSignature () -> Builder)
  (SoftwareVersion
   -> VerificationKey -> ABlockSignature () -> Builder)
-> Format
     Builder
     (SoftwareVersion
      -> VerificationKey -> ABlockSignature () -> Builder)
-> Format
     Builder
     (SoftwareVersion
      -> VerificationKey -> ABlockSignature () -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (VerificationKey -> ABlockSignature () -> Builder)
  (SoftwareVersion
   -> VerificationKey -> ABlockSignature () -> Builder)
forall a r. Buildable a => Format r (a -> r)
build
        Format
  (VerificationKey -> ABlockSignature () -> Builder)
  (SoftwareVersion
   -> VerificationKey -> ABlockSignature () -> Builder)
-> Format
     Builder (VerificationKey -> ABlockSignature () -> Builder)
-> Format
     Builder
     (SoftwareVersion
      -> VerificationKey -> ABlockSignature () -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (VerificationKey -> ABlockSignature () -> Builder)
  (VerificationKey -> ABlockSignature () -> Builder)
"\n"
        Format
  (VerificationKey -> ABlockSignature () -> Builder)
  (VerificationKey -> ABlockSignature () -> Builder)
-> Format
     Builder (VerificationKey -> ABlockSignature () -> Builder)
-> Format
     Builder (VerificationKey -> ABlockSignature () -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (VerificationKey -> ABlockSignature () -> Builder)
  (VerificationKey -> ABlockSignature () -> Builder)
"    genesis key: "
        Format
  (VerificationKey -> ABlockSignature () -> Builder)
  (VerificationKey -> ABlockSignature () -> Builder)
-> Format
     Builder (VerificationKey -> ABlockSignature () -> Builder)
-> Format
     Builder (VerificationKey -> ABlockSignature () -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (ABlockSignature () -> Builder)
  (VerificationKey -> ABlockSignature () -> Builder)
forall a r. Buildable a => Format r (a -> r)
build
        Format
  (ABlockSignature () -> Builder)
  (VerificationKey -> ABlockSignature () -> Builder)
-> Format Builder (ABlockSignature () -> Builder)
-> Format
     Builder (VerificationKey -> ABlockSignature () -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (ABlockSignature () -> Builder) (ABlockSignature () -> Builder)
"\n"
        Format
  (ABlockSignature () -> Builder) (ABlockSignature () -> Builder)
-> Format Builder (ABlockSignature () -> Builder)
-> Format Builder (ABlockSignature () -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format
  (ABlockSignature () -> Builder) (ABlockSignature () -> Builder)
"    signature: "
        Format
  (ABlockSignature () -> Builder) (ABlockSignature () -> Builder)
-> Format Builder (ABlockSignature () -> Builder)
-> Format Builder (ABlockSignature () -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format Builder (ABlockSignature () -> Builder)
forall a r. Buildable a => Format r (a -> r)
build
    )
    HeaderHash
headerHash
    (Header -> HeaderHash
forall a. AHeader a -> HeaderHash
headerPrevHash Header
header)
    (Header -> SlotNumber
forall a. AHeader a -> SlotNumber
headerSlot Header
header)
    (ChainDifficulty -> Word64
unChainDifficulty (ChainDifficulty -> Word64) -> ChainDifficulty -> Word64
forall a b. (a -> b) -> a -> b
$ Header -> ChainDifficulty
forall a. AHeader a -> ChainDifficulty
headerDifficulty Header
header)
    (Header -> ProtocolVersion
forall a. AHeader a -> ProtocolVersion
headerProtocolVersion Header
header)
    (Header -> SoftwareVersion
forall a. AHeader a -> SoftwareVersion
headerSoftwareVersion Header
header)
    (Header -> VerificationKey
forall a. AHeader a -> VerificationKey
headerGenesisKey Header
header)
    (Header -> ABlockSignature ()
forall a. AHeader a -> ABlockSignature a
headerSignature Header
header)
  where
    headerHash :: HeaderHash
    headerHash :: HeaderHash
headerHash = EpochSlots -> Header -> HeaderHash
hashHeader EpochSlots
es Header
header

--------------------------------------------------------------------------------
-- HeaderHash
--------------------------------------------------------------------------------

-- | 'Hash' of block header
type HeaderHash = Hash Header

-- | Specialized formatter for 'HeaderHash'
headerHashF :: Format r (HeaderHash -> r)
headerHashF :: Format r (HeaderHash -> r)
headerHashF = Format r (HeaderHash -> r)
forall a r. Buildable a => Format r (a -> r)
build

-- | Extract the genesis hash and cast it into a header hash.
genesisHeaderHash :: GenesisHash -> HeaderHash
genesisHeaderHash :: GenesisHash -> HeaderHash
genesisHeaderHash = Hash Raw -> HeaderHash
coerce (Hash Raw -> HeaderHash)
-> (GenesisHash -> Hash Raw) -> GenesisHash -> HeaderHash
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. GenesisHash -> Hash Raw
unGenesisHash

-- | These bytes must be prepended when hashing raw boundary header data
--
--   In the Byron release, hashes were taken over a data type that was never
--   directly serialized to the blockchain, so these magic bytes cannot be
--   determined from the raw header data.
--
--   These bytes are from `encodeListLen 2 <> toCBOR (1 :: Word8)`
wrapHeaderBytes :: ByteString -> ByteString
wrapHeaderBytes :: ByteString -> ByteString
wrapHeaderBytes = ByteString -> ByteString -> ByteString
forall a. Monoid a => a -> a -> a
mappend ByteString
"\130\SOH"

-- | Hash the serialised representation of a `Header`
--
--   For backwards compatibility we have to take the hash of the header
--   serialised with 'toCBORHeaderToHash'
hashHeader :: EpochSlots -> Header -> HeaderHash
hashHeader :: EpochSlots -> Header -> HeaderHash
hashHeader EpochSlots
es = LByteString -> HeaderHash
forall algo a.
HashAlgorithm algo =>
LByteString -> AbstractHash algo a
unsafeAbstractHash (LByteString -> HeaderHash)
-> (Header -> LByteString) -> Header -> HeaderHash
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Encoding -> LByteString
serializeEncoding (Encoding -> LByteString)
-> (Header -> Encoding) -> Header -> LByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. EpochSlots -> Header -> Encoding
toCBORHeaderToHash EpochSlots
es

headerHashAnnotated :: AHeader ByteString -> HeaderHash
headerHashAnnotated :: AHeader ByteString -> HeaderHash
headerHashAnnotated = AHeader ByteString -> HeaderHash
forall t. Decoded t => t -> Hash (BaseType t)
hashDecoded (AHeader ByteString -> HeaderHash)
-> (AHeader ByteString -> AHeader ByteString)
-> AHeader ByteString
-> HeaderHash
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (ByteString -> ByteString)
-> AHeader ByteString -> AHeader ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> ByteString
wrapHeaderBytes

--------------------------------------------------------------------------------
-- BoundaryHeader
--------------------------------------------------------------------------------

data ABoundaryHeader a = UnsafeABoundaryHeader
  { ABoundaryHeader a -> Either GenesisHash HeaderHash
boundaryPrevHash :: !(Either GenesisHash HeaderHash),
    ABoundaryHeader a -> Word64
boundaryEpoch :: !Word64,
    ABoundaryHeader a -> ChainDifficulty
boundaryDifficulty :: !ChainDifficulty,
    ABoundaryHeader a -> a
boundaryHeaderAnnotation :: !a
  }
  deriving (ABoundaryHeader a -> ABoundaryHeader a -> Bool
(ABoundaryHeader a -> ABoundaryHeader a -> Bool)
-> (ABoundaryHeader a -> ABoundaryHeader a -> Bool)
-> Eq (ABoundaryHeader a)
forall a. Eq a => ABoundaryHeader a -> ABoundaryHeader a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ABoundaryHeader a -> ABoundaryHeader a -> Bool
$c/= :: forall a. Eq a => ABoundaryHeader a -> ABoundaryHeader a -> Bool
== :: ABoundaryHeader a -> ABoundaryHeader a -> Bool
$c== :: forall a. Eq a => ABoundaryHeader a -> ABoundaryHeader a -> Bool
Eq, Int -> ABoundaryHeader a -> ShowS
[ABoundaryHeader a] -> ShowS
ABoundaryHeader a -> String
(Int -> ABoundaryHeader a -> ShowS)
-> (ABoundaryHeader a -> String)
-> ([ABoundaryHeader a] -> ShowS)
-> Show (ABoundaryHeader a)
forall a. Show a => Int -> ABoundaryHeader a -> ShowS
forall a. Show a => [ABoundaryHeader a] -> ShowS
forall a. Show a => ABoundaryHeader a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ABoundaryHeader a] -> ShowS
$cshowList :: forall a. Show a => [ABoundaryHeader a] -> ShowS
show :: ABoundaryHeader a -> String
$cshow :: forall a. Show a => ABoundaryHeader a -> String
showsPrec :: Int -> ABoundaryHeader a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> ABoundaryHeader a -> ShowS
Show, a -> ABoundaryHeader b -> ABoundaryHeader a
(a -> b) -> ABoundaryHeader a -> ABoundaryHeader b
(forall a b. (a -> b) -> ABoundaryHeader a -> ABoundaryHeader b)
-> (forall a b. a -> ABoundaryHeader b -> ABoundaryHeader a)
-> Functor ABoundaryHeader
forall a b. a -> ABoundaryHeader b -> ABoundaryHeader a
forall a b. (a -> b) -> ABoundaryHeader a -> ABoundaryHeader b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> ABoundaryHeader b -> ABoundaryHeader a
$c<$ :: forall a b. a -> ABoundaryHeader b -> ABoundaryHeader a
fmap :: (a -> b) -> ABoundaryHeader a -> ABoundaryHeader b
$cfmap :: forall a b. (a -> b) -> ABoundaryHeader a -> ABoundaryHeader b
Functor, (forall x. ABoundaryHeader a -> Rep (ABoundaryHeader a) x)
-> (forall x. Rep (ABoundaryHeader a) x -> ABoundaryHeader a)
-> Generic (ABoundaryHeader a)
forall x. Rep (ABoundaryHeader a) x -> ABoundaryHeader a
forall x. ABoundaryHeader a -> Rep (ABoundaryHeader a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (ABoundaryHeader a) x -> ABoundaryHeader a
forall a x. ABoundaryHeader a -> Rep (ABoundaryHeader a) x
$cto :: forall a x. Rep (ABoundaryHeader a) x -> ABoundaryHeader a
$cfrom :: forall a x. ABoundaryHeader a -> Rep (ABoundaryHeader a) x
Generic, Context -> ABoundaryHeader a -> IO (Maybe ThunkInfo)
Proxy (ABoundaryHeader a) -> String
(Context -> ABoundaryHeader a -> IO (Maybe ThunkInfo))
-> (Context -> ABoundaryHeader a -> IO (Maybe ThunkInfo))
-> (Proxy (ABoundaryHeader a) -> String)
-> NoThunks (ABoundaryHeader a)
forall a.
NoThunks a =>
Context -> ABoundaryHeader a -> IO (Maybe ThunkInfo)
forall a. NoThunks a => Proxy (ABoundaryHeader a) -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (ABoundaryHeader a) -> String
$cshowTypeOf :: forall a. NoThunks a => Proxy (ABoundaryHeader a) -> String
wNoThunks :: Context -> ABoundaryHeader a -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall a.
NoThunks a =>
Context -> ABoundaryHeader a -> IO (Maybe ThunkInfo)
noThunks :: Context -> ABoundaryHeader a -> IO (Maybe ThunkInfo)
$cnoThunks :: forall a.
NoThunks a =>
Context -> ABoundaryHeader a -> IO (Maybe ThunkInfo)
NoThunks)

-- Used for debugging purposes only
instance ToJSON a => ToJSON (ABoundaryHeader a)

-- | Smart constructor for 'ABoundaryHeader'
--
-- Makes sure that the hash is forced.
mkABoundaryHeader ::
  Either GenesisHash HeaderHash ->
  Word64 ->
  ChainDifficulty ->
  a ->
  ABoundaryHeader a
mkABoundaryHeader :: Either GenesisHash HeaderHash
-> Word64 -> ChainDifficulty -> a -> ABoundaryHeader a
mkABoundaryHeader Either GenesisHash HeaderHash
prevHash Word64
epoch ChainDifficulty
dty a
ann =
  case Either GenesisHash HeaderHash
prevHash of
    Left !GenesisHash
genHash -> Either GenesisHash HeaderHash
-> Word64 -> ChainDifficulty -> a -> ABoundaryHeader a
forall a.
Either GenesisHash HeaderHash
-> Word64 -> ChainDifficulty -> a -> ABoundaryHeader a
UnsafeABoundaryHeader (GenesisHash -> Either GenesisHash HeaderHash
forall a b. a -> Either a b
Left GenesisHash
genHash) Word64
epoch ChainDifficulty
dty a
ann
    Right !HeaderHash
hdrHash -> Either GenesisHash HeaderHash
-> Word64 -> ChainDifficulty -> a -> ABoundaryHeader a
forall a.
Either GenesisHash HeaderHash
-> Word64 -> ChainDifficulty -> a -> ABoundaryHeader a
UnsafeABoundaryHeader (HeaderHash -> Either GenesisHash HeaderHash
forall a b. b -> Either a b
Right HeaderHash
hdrHash) Word64
epoch ChainDifficulty
dty a
ann

instance Decoded (ABoundaryHeader ByteString) where
  type BaseType (ABoundaryHeader ByteString) = ABoundaryHeader ()
  recoverBytes :: ABoundaryHeader ByteString -> ByteString
recoverBytes = ABoundaryHeader ByteString -> ByteString
forall a. ABoundaryHeader a -> a
boundaryHeaderAnnotation

-- | Compute the hash of a boundary block header from its annotation.
-- It uses `wrapBoundaryBytes`, for the hash must be computed on the header
-- bytes tagged with the CBOR list length and tag discriminator, which is
-- the encoding chosen by cardano-sl.
boundaryHeaderHashAnnotated :: ABoundaryHeader ByteString -> HeaderHash
boundaryHeaderHashAnnotated :: ABoundaryHeader ByteString -> HeaderHash
boundaryHeaderHashAnnotated = AbstractHash Blake2b_256 (ABoundaryHeader ()) -> HeaderHash
coerce (AbstractHash Blake2b_256 (ABoundaryHeader ()) -> HeaderHash)
-> (ABoundaryHeader ByteString
    -> AbstractHash Blake2b_256 (ABoundaryHeader ()))
-> ABoundaryHeader ByteString
-> HeaderHash
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ABoundaryHeader ByteString
-> AbstractHash Blake2b_256 (ABoundaryHeader ())
forall t. Decoded t => t -> Hash (BaseType t)
hashDecoded (ABoundaryHeader ByteString
 -> AbstractHash Blake2b_256 (ABoundaryHeader ()))
-> (ABoundaryHeader ByteString -> ABoundaryHeader ByteString)
-> ABoundaryHeader ByteString
-> AbstractHash Blake2b_256 (ABoundaryHeader ())
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (ByteString -> ByteString)
-> ABoundaryHeader ByteString -> ABoundaryHeader ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> ByteString
wrapBoundaryBytes

-- | Encode from a boundary header with any annotation. This does not
-- necessarily invert `fromCBORBoundaryHeader`, because that decoder drops
-- information that this encoder replaces, such as the body proof (assumes
-- the body is empty) and the extra header data (sets it to empty map).
toCBORABoundaryHeader :: ProtocolMagicId -> ABoundaryHeader a -> Encoding
toCBORABoundaryHeader :: ProtocolMagicId -> ABoundaryHeader a -> Encoding
toCBORABoundaryHeader ProtocolMagicId
pm ABoundaryHeader a
hdr =
  Word -> Encoding
encodeListLen Word
5
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ProtocolMagicId -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR ProtocolMagicId
pm
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ( case ABoundaryHeader a -> Either GenesisHash HeaderHash
forall a. ABoundaryHeader a -> Either GenesisHash HeaderHash
boundaryPrevHash ABoundaryHeader a
hdr of
           Left GenesisHash
gh -> HeaderHash -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (GenesisHash -> HeaderHash
genesisHeaderHash GenesisHash
gh)
           Right HeaderHash
hh -> HeaderHash -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR HeaderHash
hh
       )
    -- Body proof
    -- The body is always an empty slot leader schedule, so we hash that.
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Hash [()] -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR ([()] -> Hash [()]
forall a. ToCBOR a => a -> Hash a
serializeCborHash ([] :: [()]))
    -- Consensus data
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ( Word -> Encoding
encodeListLen Word
2
           -- Epoch
           Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (ABoundaryHeader a -> Word64
forall a. ABoundaryHeader a -> Word64
boundaryEpoch ABoundaryHeader a
hdr)
           -- Chain difficulty
           Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ChainDifficulty -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (ABoundaryHeader a -> ChainDifficulty
forall a. ABoundaryHeader a -> ChainDifficulty
boundaryDifficulty ABoundaryHeader a
hdr)
       )
    -- Extra data
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ( Word -> Encoding
encodeListLen Word
1
           Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Map Word8 LByteString -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR Map Word8 LByteString
genesisTag
       )
  where
    -- Genesis tag to indicate the presence of a genesis hash in a non-zero
    -- epoch. See 'dropBoundaryExtraHeaderDataRetainGenesisTag' for more
    -- details on this.
    genesisTag :: Map Word8 LByteString
genesisTag = case (ABoundaryHeader a -> Either GenesisHash HeaderHash
forall a. ABoundaryHeader a -> Either GenesisHash HeaderHash
boundaryPrevHash ABoundaryHeader a
hdr, ABoundaryHeader a -> Word64
forall a. ABoundaryHeader a -> Word64
boundaryEpoch ABoundaryHeader a
hdr) of
      (Left GenesisHash
_, Word64
n) | Word64
n Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Word64
0 -> Word8 -> LByteString -> Map Word8 LByteString
forall k a. k -> a -> Map k a
Map.singleton Word8
255 LByteString
"Genesis"
      (Either GenesisHash HeaderHash, Word64)
_ -> Map Word8 LByteString
forall a. Monoid a => a
mempty :: Map Word8 LByteString

toCBORABoundaryHeaderSize :: Proxy ProtocolMagicId -> Proxy (ABoundaryHeader a) -> Size
toCBORABoundaryHeaderSize :: Proxy ProtocolMagicId -> Proxy (ABoundaryHeader a) -> Size
toCBORABoundaryHeaderSize Proxy ProtocolMagicId
pm Proxy (ABoundaryHeader a)
hdr =
  Size
1
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy ProtocolMagicId -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy Proxy ProtocolMagicId
pm
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ [Case Size] -> Size
szCases
      [ Text -> Size -> Case Size
forall t. Text -> t -> Case t
Case Text
"GenesisHash" (Size -> Case Size) -> Size -> Case Size
forall a b. (a -> b) -> a -> b
$
          Proxy GenesisHash -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (Proxy GenesisHash -> Size) -> Proxy GenesisHash -> Size
forall a b. (a -> b) -> a -> b
$
            Proxy (Either GenesisHash HeaderHash) -> Proxy GenesisHash
forall a b. Proxy (Either a b) -> Proxy a
pFromLeft (Proxy (Either GenesisHash HeaderHash) -> Proxy GenesisHash)
-> Proxy (Either GenesisHash HeaderHash) -> Proxy GenesisHash
forall a b. (a -> b) -> a -> b
$
              ABoundaryHeader a -> Either GenesisHash HeaderHash
forall a. ABoundaryHeader a -> Either GenesisHash HeaderHash
boundaryPrevHash (ABoundaryHeader a -> Either GenesisHash HeaderHash)
-> Proxy (ABoundaryHeader a)
-> Proxy (Either GenesisHash HeaderHash)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (ABoundaryHeader a)
hdr,
        Text -> Size -> Case Size
forall t. Text -> t -> Case t
Case Text
"HeaderHash" (Size -> Case Size) -> Size -> Case Size
forall a b. (a -> b) -> a -> b
$
          Proxy HeaderHash -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (Proxy HeaderHash -> Size) -> Proxy HeaderHash -> Size
forall a b. (a -> b) -> a -> b
$
            Proxy (Either GenesisHash HeaderHash) -> Proxy HeaderHash
forall a b. Proxy (Either a b) -> Proxy b
pFromRight (Proxy (Either GenesisHash HeaderHash) -> Proxy HeaderHash)
-> Proxy (Either GenesisHash HeaderHash) -> Proxy HeaderHash
forall a b. (a -> b) -> a -> b
$
              ABoundaryHeader a -> Either GenesisHash HeaderHash
forall a. ABoundaryHeader a -> Either GenesisHash HeaderHash
boundaryPrevHash (ABoundaryHeader a -> Either GenesisHash HeaderHash)
-> Proxy (ABoundaryHeader a)
-> Proxy (Either GenesisHash HeaderHash)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (ABoundaryHeader a)
hdr
      ]
    -- Body proof
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy (Hash LByteString) -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (Proxy (Hash LByteString)
forall k (t :: k). Proxy t
Proxy :: Proxy (Hash LByteString))
    -- Consensus data
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ ( Size
1
          Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy Word64 -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (ABoundaryHeader a -> Word64
forall a. ABoundaryHeader a -> Word64
boundaryEpoch (ABoundaryHeader a -> Word64)
-> Proxy (ABoundaryHeader a) -> Proxy Word64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (ABoundaryHeader a)
hdr)
          Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy ChainDifficulty -> Size
forall a. ToCBOR a => Proxy a -> Size
szGreedy (ABoundaryHeader a -> ChainDifficulty
forall a. ABoundaryHeader a -> ChainDifficulty
boundaryDifficulty (ABoundaryHeader a -> ChainDifficulty)
-> Proxy (ABoundaryHeader a) -> Proxy ChainDifficulty
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (ABoundaryHeader a)
hdr)
      )
    -- Extra data
    Size -> Size -> Size
forall a. Num a => a -> a -> a
+ ( Size
1
          Size -> Size -> Size
forall a. Num a => a -> a -> a
+ [Case Size] -> Size
szCases
            [ Text -> Size -> Case Size
forall t. Text -> t -> Case t
Case Text
"Genesis" Size
11,
              Text -> Size -> Case Size
forall t. Text -> t -> Case t
Case Text
"" Size
1
            ]
      )
  where
    pFromLeft :: Proxy (Either a b) -> Proxy a
    pFromLeft :: Proxy (Either a b) -> Proxy a
pFromLeft Proxy (Either a b)
_ = Proxy a
forall k (t :: k). Proxy t
Proxy

    pFromRight :: Proxy (Either a b) -> Proxy b
    pFromRight :: Proxy (Either a b) -> Proxy b
pFromRight Proxy (Either a b)
_ = Proxy b
forall k (t :: k). Proxy t
Proxy

fromCBORABoundaryHeader :: Decoder s (ABoundaryHeader ByteSpan)
fromCBORABoundaryHeader :: Decoder s (ABoundaryHeader ByteSpan)
fromCBORABoundaryHeader = do
  Annotated ABoundaryHeader ()
header ByteSpan
bytespan <- Decoder s (ABoundaryHeader ())
-> Decoder s (Annotated (ABoundaryHeader ()) ByteSpan)
forall s a. Decoder s a -> Decoder s (Annotated a ByteSpan)
annotatedDecoder (Decoder s (ABoundaryHeader ())
 -> Decoder s (Annotated (ABoundaryHeader ()) ByteSpan))
-> Decoder s (ABoundaryHeader ())
-> Decoder s (Annotated (ABoundaryHeader ()) ByteSpan)
forall a b. (a -> b) -> a -> b
$ do
    Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"BoundaryHeader" Int
5
    Decoder s ()
forall s. Dropper s
dropInt32
    -- HeaderHash
    HeaderHash
hh <- Decoder s HeaderHash
forall a s. FromCBOR a => Decoder s a
fromCBOR
    -- BoundaryBodyProof
    Decoder s ()
forall s. Dropper s
dropBytes
    (Word64
epoch, ChainDifficulty
difficulty) <- Decoder s (Word64, ChainDifficulty)
forall s. Decoder s (Word64, ChainDifficulty)
fromCBORBoundaryConsensusData
    Bool
isGen <- Decoder s Bool
forall s. Decoder s Bool
dropBoundaryExtraHeaderDataRetainGenesisTag
    let hh' :: Either GenesisHash HeaderHash
hh' = if Word64
epoch Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
0 Bool -> Bool -> Bool
|| Bool
isGen then GenesisHash -> Either GenesisHash HeaderHash
forall a b. a -> Either a b
Left (HeaderHash -> GenesisHash
coerce HeaderHash
hh) else HeaderHash -> Either GenesisHash HeaderHash
forall a b. b -> Either a b
Right HeaderHash
hh
    ABoundaryHeader () -> Decoder s (ABoundaryHeader ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ABoundaryHeader () -> Decoder s (ABoundaryHeader ()))
-> ABoundaryHeader () -> Decoder s (ABoundaryHeader ())
forall a b. (a -> b) -> a -> b
$ Either GenesisHash HeaderHash
-> Word64 -> ChainDifficulty -> () -> ABoundaryHeader ()
forall a.
Either GenesisHash HeaderHash
-> Word64 -> ChainDifficulty -> a -> ABoundaryHeader a
mkABoundaryHeader Either GenesisHash HeaderHash
hh' Word64
epoch ChainDifficulty
difficulty ()
  ABoundaryHeader ByteSpan -> Decoder s (ABoundaryHeader ByteSpan)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ABoundaryHeader ()
header {boundaryHeaderAnnotation :: ByteSpan
boundaryHeaderAnnotation = ByteSpan
bytespan})

-- | These bytes must be prepended when hashing raw boundary header data
--
--   In the Byron release, hashes were taken over a data type that was never
--   directly serialized to the blockchain, so these magic bytes cannot be
--   determined from the raw header data.
wrapBoundaryBytes :: ByteString -> ByteString
wrapBoundaryBytes :: ByteString -> ByteString
wrapBoundaryBytes = ByteString -> ByteString -> ByteString
forall a. Monoid a => a -> a -> a
mappend ByteString
"\130\NUL"

--------------------------------------------------------------------------------
-- BlockSignature
--------------------------------------------------------------------------------

type BlockSignature = ABlockSignature ()

-- | Signature of the 'Block'
--
--   We use a heavyweight delegation scheme, so the signature has two parts:
--
--   1. A delegation certificate from a genesis key to the block signer
--   2. The actual signature over `ToSign`
data ABlockSignature a = ABlockSignature
  { ABlockSignature a -> ACertificate a
delegationCertificate :: !(Delegation.ACertificate a),
    ABlockSignature a -> Signature ToSign
signature :: !(Signature ToSign)
  }
  deriving (Int -> ABlockSignature a -> ShowS
[ABlockSignature a] -> ShowS
ABlockSignature a -> String
(Int -> ABlockSignature a -> ShowS)
-> (ABlockSignature a -> String)
-> ([ABlockSignature a] -> ShowS)
-> Show (ABlockSignature a)
forall a. Show a => Int -> ABlockSignature a -> ShowS
forall a. Show a => [ABlockSignature a] -> ShowS
forall a. Show a => ABlockSignature a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ABlockSignature a] -> ShowS
$cshowList :: forall a. Show a => [ABlockSignature a] -> ShowS
show :: ABlockSignature a -> String
$cshow :: forall a. Show a => ABlockSignature a -> String
showsPrec :: Int -> ABlockSignature a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> ABlockSignature a -> ShowS
Show, ABlockSignature a -> ABlockSignature a -> Bool
(ABlockSignature a -> ABlockSignature a -> Bool)
-> (ABlockSignature a -> ABlockSignature a -> Bool)
-> Eq (ABlockSignature a)
forall a. Eq a => ABlockSignature a -> ABlockSignature a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ABlockSignature a -> ABlockSignature a -> Bool
$c/= :: forall a. Eq a => ABlockSignature a -> ABlockSignature a -> Bool
== :: ABlockSignature a -> ABlockSignature a -> Bool
$c== :: forall a. Eq a => ABlockSignature a -> ABlockSignature a -> Bool
Eq, (forall x. ABlockSignature a -> Rep (ABlockSignature a) x)
-> (forall x. Rep (ABlockSignature a) x -> ABlockSignature a)
-> Generic (ABlockSignature a)
forall x. Rep (ABlockSignature a) x -> ABlockSignature a
forall x. ABlockSignature a -> Rep (ABlockSignature a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (ABlockSignature a) x -> ABlockSignature a
forall a x. ABlockSignature a -> Rep (ABlockSignature a) x
$cto :: forall a x. Rep (ABlockSignature a) x -> ABlockSignature a
$cfrom :: forall a x. ABlockSignature a -> Rep (ABlockSignature a) x
Generic, a -> ABlockSignature b -> ABlockSignature a
(a -> b) -> ABlockSignature a -> ABlockSignature b
(forall a b. (a -> b) -> ABlockSignature a -> ABlockSignature b)
-> (forall a b. a -> ABlockSignature b -> ABlockSignature a)
-> Functor ABlockSignature
forall a b. a -> ABlockSignature b -> ABlockSignature a
forall a b. (a -> b) -> ABlockSignature a -> ABlockSignature b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> ABlockSignature b -> ABlockSignature a
$c<$ :: forall a b. a -> ABlockSignature b -> ABlockSignature a
fmap :: (a -> b) -> ABlockSignature a -> ABlockSignature b
$cfmap :: forall a b. (a -> b) -> ABlockSignature a -> ABlockSignature b
Functor)
  deriving anyclass (ABlockSignature a -> ()
(ABlockSignature a -> ()) -> NFData (ABlockSignature a)
forall a. NFData a => ABlockSignature a -> ()
forall a. (a -> ()) -> NFData a
rnf :: ABlockSignature a -> ()
$crnf :: forall a. NFData a => ABlockSignature a -> ()
NFData, Context -> ABlockSignature a -> IO (Maybe ThunkInfo)
Proxy (ABlockSignature a) -> String
(Context -> ABlockSignature a -> IO (Maybe ThunkInfo))
-> (Context -> ABlockSignature a -> IO (Maybe ThunkInfo))
-> (Proxy (ABlockSignature a) -> String)
-> NoThunks (ABlockSignature a)
forall a.
NoThunks a =>
Context -> ABlockSignature a -> IO (Maybe ThunkInfo)
forall a. NoThunks a => Proxy (ABlockSignature a) -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (ABlockSignature a) -> String
$cshowTypeOf :: forall a. NoThunks a => Proxy (ABlockSignature a) -> String
wNoThunks :: Context -> ABlockSignature a -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall a.
NoThunks a =>
Context -> ABlockSignature a -> IO (Maybe ThunkInfo)
noThunks :: Context -> ABlockSignature a -> IO (Maybe ThunkInfo)
$cnoThunks :: forall a.
NoThunks a =>
Context -> ABlockSignature a -> IO (Maybe ThunkInfo)
NoThunks)

instance B.Buildable BlockSignature where
  build :: ABlockSignature () -> Builder
build (ABlockSignature Certificate
cert Signature ToSign
_) =
    Format Builder (Certificate -> Builder) -> Certificate -> Builder
forall a. Format Builder a -> a
bprint
      ( Format (Certificate -> Builder) (Certificate -> Builder)
"BlockSignature:\n"
          Format (Certificate -> Builder) (Certificate -> Builder)
-> Format Builder (Certificate -> Builder)
-> Format Builder (Certificate -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format (Certificate -> Builder) (Certificate -> Builder)
"  Delegation certificate: "
          Format (Certificate -> Builder) (Certificate -> Builder)
-> Format Builder (Certificate -> Builder)
-> Format Builder (Certificate -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format Builder (Certificate -> Builder)
forall a r. Buildable a => Format r (a -> r)
build
      )
      Certificate
cert

-- Used for debugging purposes only
instance ToJSON a => ToJSON (ABlockSignature a)

instance ToCBOR BlockSignature where
  toCBOR :: ABlockSignature () -> Encoding
toCBOR (ABlockSignature Certificate
cert Signature ToSign
sig) =
    -- Tag 0 was previously used for BlockSignature (no delegation)
    -- Tag 1 was previously used for BlockPSignatureLight
    Word -> Encoding
encodeListLen Word
2
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word8 -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Word8
2 :: Word8)
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Word -> Encoding
encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Certificate -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR Certificate
cert Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Signature ToSign -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR Signature ToSign
sig)

  encodedSizeExpr :: (forall a. ToCBOR a => Proxy a -> Size)
-> Proxy (ABlockSignature ()) -> Size
encodedSizeExpr forall a. ToCBOR a => Proxy a -> Size
size Proxy (ABlockSignature ())
sig =
    Size
3
      Size -> Size -> Size
forall a. Num a => a -> a -> a
+ (forall a. ToCBOR a => Proxy a -> Size)
-> Proxy Certificate -> Size
forall a.
ToCBOR a =>
(forall a. ToCBOR a => Proxy a -> Size) -> Proxy a -> Size
encodedSizeExpr forall a. ToCBOR a => Proxy a -> Size
size (ABlockSignature () -> Certificate
forall a. ABlockSignature a -> ACertificate a
delegationCertificate (ABlockSignature () -> Certificate)
-> Proxy (ABlockSignature ()) -> Proxy Certificate
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (ABlockSignature ())
sig)
      Size -> Size -> Size
forall a. Num a => a -> a -> a
+ (forall a. ToCBOR a => Proxy a -> Size)
-> Proxy (Signature ToSign) -> Size
forall a.
ToCBOR a =>
(forall a. ToCBOR a => Proxy a -> Size) -> Proxy a -> Size
encodedSizeExpr forall a. ToCBOR a => Proxy a -> Size
size (ABlockSignature () -> Signature ToSign
forall a. ABlockSignature a -> Signature ToSign
signature (ABlockSignature () -> Signature ToSign)
-> Proxy (ABlockSignature ()) -> Proxy (Signature ToSign)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy (ABlockSignature ())
sig)

instance FromCBOR BlockSignature where
  fromCBOR :: Decoder s (ABlockSignature ())
fromCBOR = ABlockSignature ByteSpan -> ABlockSignature ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ABlockSignature ByteSpan -> ABlockSignature ())
-> Decoder s (ABlockSignature ByteSpan)
-> Decoder s (ABlockSignature ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s.
FromCBOR (ABlockSignature ByteSpan) =>
Decoder s (ABlockSignature ByteSpan)
forall a s. FromCBOR a => Decoder s a
fromCBOR @(ABlockSignature ByteSpan)

instance FromCBOR (ABlockSignature ByteSpan) where
  fromCBOR :: Decoder s (ABlockSignature ByteSpan)
fromCBOR = do
    Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"BlockSignature" Int
2
    Decoder s Word8
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder s Word8
-> (Word8 -> Decoder s (ABlockSignature ByteSpan))
-> Decoder s (ABlockSignature ByteSpan)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Word8
2 ->
        ACertificate ByteSpan
-> Signature ToSign -> ABlockSignature ByteSpan
forall a. ACertificate a -> Signature ToSign -> ABlockSignature a
ABlockSignature
          (ACertificate ByteSpan
 -> Signature ToSign -> ABlockSignature ByteSpan)
-> Decoder s ()
-> Decoder
     s
     (ACertificate ByteSpan
      -> Signature ToSign -> ABlockSignature ByteSpan)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"BlockSignature" Int
2
          Decoder
  s
  (ACertificate ByteSpan
   -> Signature ToSign -> ABlockSignature ByteSpan)
-> Decoder s (ACertificate ByteSpan)
-> Decoder s (Signature ToSign -> ABlockSignature ByteSpan)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (ACertificate ByteSpan)
forall a s. FromCBOR a => Decoder s a
fromCBOR
          Decoder s (Signature ToSign -> ABlockSignature ByteSpan)
-> Decoder s (Signature ToSign)
-> Decoder s (ABlockSignature ByteSpan)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (Signature ToSign)
forall a s. FromCBOR a => Decoder s a
fromCBOR
      Word8
t -> DecoderError -> Decoder s (ABlockSignature ByteSpan)
forall e s a. Buildable e => e -> Decoder s a
cborError (DecoderError -> Decoder s (ABlockSignature ByteSpan))
-> DecoderError -> Decoder s (ABlockSignature ByteSpan)
forall a b. (a -> b) -> a -> b
$ Text -> Word8 -> DecoderError
DecoderErrorUnknownTag Text
"BlockSignature" Word8
t

--------------------------------------------------------------------------------
-- ToSign
--------------------------------------------------------------------------------

-- | Produces the ByteString that was signed in the block
recoverSignedBytes ::
  EpochSlots -> AHeader ByteString -> Annotated ToSign ByteString
recoverSignedBytes :: EpochSlots -> AHeader ByteString -> Annotated ToSign ByteString
recoverSignedBytes EpochSlots
es AHeader ByteString
h = ToSign -> ByteString -> Annotated ToSign ByteString
forall b a. b -> a -> Annotated b a
Annotated (EpochSlots -> AHeader ByteString -> ToSign
forall a. EpochSlots -> AHeader a -> ToSign
headerToSign EpochSlots
es AHeader ByteString
h) ByteString
bytes
  where
    bytes :: ByteString
bytes =
      [ByteString] -> ByteString
BS.concat
        [ ByteString
"\133",
          -- This is the value of Codec.CBOR.Write.toLazyByteString (encodeListLen 5)
          -- It is hard coded here because the signed bytes included it as an
          -- implementation artifact
          (Annotated HeaderHash ByteString -> ByteString
forall b a. Annotated b a -> a
annotation (Annotated HeaderHash ByteString -> ByteString)
-> (AHeader ByteString -> Annotated HeaderHash ByteString)
-> AHeader ByteString
-> ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AHeader ByteString -> Annotated HeaderHash ByteString
forall a. AHeader a -> Annotated HeaderHash a
aHeaderPrevHash) AHeader ByteString
h,
          (Annotated Proof ByteString -> ByteString
forall b a. Annotated b a -> a
annotation (Annotated Proof ByteString -> ByteString)
-> (AHeader ByteString -> Annotated Proof ByteString)
-> AHeader ByteString
-> ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AHeader ByteString -> Annotated Proof ByteString
forall a. AHeader a -> Annotated Proof a
aHeaderProof) AHeader ByteString
h,
          (Annotated SlotNumber ByteString -> ByteString
forall b a. Annotated b a -> a
annotation (Annotated SlotNumber ByteString -> ByteString)
-> (AHeader ByteString -> Annotated SlotNumber ByteString)
-> AHeader ByteString
-> ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AHeader ByteString -> Annotated SlotNumber ByteString
forall a. AHeader a -> Annotated SlotNumber a
aHeaderSlot) AHeader ByteString
h,
          (Annotated ChainDifficulty ByteString -> ByteString
forall b a. Annotated b a -> a
annotation (Annotated ChainDifficulty ByteString -> ByteString)
-> (AHeader ByteString -> Annotated ChainDifficulty ByteString)
-> AHeader ByteString
-> ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AHeader ByteString -> Annotated ChainDifficulty ByteString
forall a. AHeader a -> Annotated ChainDifficulty a
aHeaderDifficulty) AHeader ByteString
h,
          AHeader ByteString -> ByteString
forall a. AHeader a -> a
headerExtraAnnotation AHeader ByteString
h
        ]

-- | Data to be signed in 'Block'
data ToSign = ToSign
  { -- | Hash of previous header in the chain
    ToSign -> HeaderHash
tsHeaderHash :: !HeaderHash,
    ToSign -> Proof
tsBodyProof :: !Proof,
    ToSign -> EpochAndSlotCount
tsSlot :: !EpochAndSlotCount,
    ToSign -> ChainDifficulty
tsDifficulty :: !ChainDifficulty,
    ToSign -> ProtocolVersion
tsProtocolVersion :: !ProtocolVersion,
    ToSign -> SoftwareVersion
tsSoftwareVersion :: !SoftwareVersion
  }
  deriving (ToSign -> ToSign -> Bool
(ToSign -> ToSign -> Bool)
-> (ToSign -> ToSign -> Bool) -> Eq ToSign
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ToSign -> ToSign -> Bool
$c/= :: ToSign -> ToSign -> Bool
== :: ToSign -> ToSign -> Bool
$c== :: ToSign -> ToSign -> Bool
Eq, Int -> ToSign -> ShowS
[ToSign] -> ShowS
ToSign -> String
(Int -> ToSign -> ShowS)
-> (ToSign -> String) -> ([ToSign] -> ShowS) -> Show ToSign
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ToSign] -> ShowS
$cshowList :: [ToSign] -> ShowS
show :: ToSign -> String
$cshow :: ToSign -> String
showsPrec :: Int -> ToSign -> ShowS
$cshowsPrec :: Int -> ToSign -> ShowS
Show, (forall x. ToSign -> Rep ToSign x)
-> (forall x. Rep ToSign x -> ToSign) -> Generic ToSign
forall x. Rep ToSign x -> ToSign
forall x. ToSign -> Rep ToSign x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ToSign x -> ToSign
$cfrom :: forall x. ToSign -> Rep ToSign x
Generic)

instance ToCBOR ToSign where
  toCBOR :: ToSign -> Encoding
toCBOR ToSign
ts =
    Word -> Encoding
encodeListLen Word
5
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> HeaderHash -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (ToSign -> HeaderHash
tsHeaderHash ToSign
ts)
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Proof -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (ToSign -> Proof
tsBodyProof ToSign
ts)
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> EpochAndSlotCount -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (ToSign -> EpochAndSlotCount
tsSlot ToSign
ts)
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ChainDifficulty -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (ToSign -> ChainDifficulty
tsDifficulty ToSign
ts)
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ProtocolVersion -> SoftwareVersion -> Encoding
toCBORBlockVersions (ToSign -> ProtocolVersion
tsProtocolVersion ToSign
ts) (ToSign -> SoftwareVersion
tsSoftwareVersion ToSign
ts)

  encodedSizeExpr :: (forall a. ToCBOR a => Proxy a -> Size) -> Proxy ToSign -> Size
encodedSizeExpr forall a. ToCBOR a => Proxy a -> Size
size Proxy ToSign
ts =
    Size
1
      Size -> Size -> Size
forall a. Num a => a -> a -> a
+ (forall a. ToCBOR a => Proxy a -> Size) -> Proxy HeaderHash -> Size
forall a.
ToCBOR a =>
(forall a. ToCBOR a => Proxy a -> Size) -> Proxy a -> Size
encodedSizeExpr forall a. ToCBOR a => Proxy a -> Size
size (ToSign -> HeaderHash
tsHeaderHash (ToSign -> HeaderHash) -> Proxy ToSign -> Proxy HeaderHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy ToSign
ts)
      Size -> Size -> Size
forall a. Num a => a -> a -> a
+ (forall a. ToCBOR a => Proxy a -> Size) -> Proxy Proof -> Size
forall a.
ToCBOR a =>
(forall a. ToCBOR a => Proxy a -> Size) -> Proxy a -> Size
encodedSizeExpr forall a. ToCBOR a => Proxy a -> Size
size (ToSign -> Proof
tsBodyProof (ToSign -> Proof) -> Proxy ToSign -> Proxy Proof
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy ToSign
ts)
      Size -> Size -> Size
forall a. Num a => a -> a -> a
+ (forall a. ToCBOR a => Proxy a -> Size)
-> Proxy EpochAndSlotCount -> Size
forall a.
ToCBOR a =>
(forall a. ToCBOR a => Proxy a -> Size) -> Proxy a -> Size
encodedSizeExpr forall a. ToCBOR a => Proxy a -> Size
size (ToSign -> EpochAndSlotCount
tsSlot (ToSign -> EpochAndSlotCount)
-> Proxy ToSign -> Proxy EpochAndSlotCount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy ToSign
ts)
      Size -> Size -> Size
forall a. Num a => a -> a -> a
+ (forall a. ToCBOR a => Proxy a -> Size)
-> Proxy ChainDifficulty -> Size
forall a.
ToCBOR a =>
(forall a. ToCBOR a => Proxy a -> Size) -> Proxy a -> Size
encodedSizeExpr forall a. ToCBOR a => Proxy a -> Size
size (ToSign -> ChainDifficulty
tsDifficulty (ToSign -> ChainDifficulty)
-> Proxy ToSign -> Proxy ChainDifficulty
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy ToSign
ts)
      Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Proxy ProtocolVersion -> Proxy SoftwareVersion -> Size
toCBORBlockVersionsSize (ToSign -> ProtocolVersion
tsProtocolVersion (ToSign -> ProtocolVersion)
-> Proxy ToSign -> Proxy ProtocolVersion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy ToSign
ts) (ToSign -> SoftwareVersion
tsSoftwareVersion (ToSign -> SoftwareVersion)
-> Proxy ToSign -> Proxy SoftwareVersion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy ToSign
ts)

instance FromCBOR ToSign where
  fromCBOR :: Decoder s ToSign
fromCBOR = do
    Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"ToSign" Int
5
    ((ProtocolVersion -> SoftwareVersion -> ToSign)
 -> (ProtocolVersion, SoftwareVersion) -> ToSign)
-> Decoder s (ProtocolVersion -> SoftwareVersion -> ToSign)
-> Decoder s ((ProtocolVersion, SoftwareVersion) -> ToSign)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ProtocolVersion -> SoftwareVersion -> ToSign)
-> (ProtocolVersion, SoftwareVersion) -> ToSign
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (HeaderHash
-> Proof
-> EpochAndSlotCount
-> ChainDifficulty
-> ProtocolVersion
-> SoftwareVersion
-> ToSign
ToSign (HeaderHash
 -> Proof
 -> EpochAndSlotCount
 -> ChainDifficulty
 -> ProtocolVersion
 -> SoftwareVersion
 -> ToSign)
-> Decoder s HeaderHash
-> Decoder
     s
     (Proof
      -> EpochAndSlotCount
      -> ChainDifficulty
      -> ProtocolVersion
      -> SoftwareVersion
      -> ToSign)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s HeaderHash
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder
  s
  (Proof
   -> EpochAndSlotCount
   -> ChainDifficulty
   -> ProtocolVersion
   -> SoftwareVersion
   -> ToSign)
-> Decoder s Proof
-> Decoder
     s
     (EpochAndSlotCount
      -> ChainDifficulty -> ProtocolVersion -> SoftwareVersion -> ToSign)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Proof
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder
  s
  (EpochAndSlotCount
   -> ChainDifficulty -> ProtocolVersion -> SoftwareVersion -> ToSign)
-> Decoder s EpochAndSlotCount
-> Decoder
     s (ChainDifficulty -> ProtocolVersion -> SoftwareVersion -> ToSign)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s EpochAndSlotCount
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder
  s (ChainDifficulty -> ProtocolVersion -> SoftwareVersion -> ToSign)
-> Decoder s ChainDifficulty
-> Decoder s (ProtocolVersion -> SoftwareVersion -> ToSign)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s ChainDifficulty
forall a s. FromCBOR a => Decoder s a
fromCBOR)
      Decoder s ((ProtocolVersion, SoftwareVersion) -> ToSign)
-> Decoder s (ProtocolVersion, SoftwareVersion) -> Decoder s ToSign
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (ProtocolVersion, SoftwareVersion)
forall s. Decoder s (ProtocolVersion, SoftwareVersion)
fromCBORBlockVersions