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

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
  encCBORHeader,
  encCBORHeaderSize,
  encCBORHeaderToHash,
  decCBORAHeader,
  decCBORHeader,
  decCBORHeaderToHash,
  wrapHeaderBytes,
  encCBORBlockVersions,
  encCBORBlockVersionsSize,

  -- * Header Formatting
  renderHeader,

  -- * Boundary Header
  ABoundaryHeader (..),
  mkABoundaryHeader,
  encCBORABoundaryHeader,
  encCBORABoundaryHeaderSize,
  decCBORABoundaryHeader,
  boundaryHeaderHashAnnotated,
  wrapBoundaryBytes,

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

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

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

import Cardano.Chain.Block.Body (Body)
import Cardano.Chain.Block.Boundary (
  decCBORBoundaryConsensusData,
  dropBoundaryExtraHeaderDataRetainGenesisTag,
 )
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.Crypto.Raw (Raw)
import Cardano.Ledger.Binary (
  Annotated (..),
  ByteSpan,
  Case (..),
  DecCBOR (..),
  Decoded (..),
  Decoder,
  DecoderError (..),
  EncCBOR (..),
  Encoding,
  FromCBOR (..),
  Size,
  ToCBOR (..),
  annotatedDecoder,
  byronProtVer,
  cborError,
  decCBORAnnotated,
  dropBytes,
  dropInt32,
  encodeListLen,
  enforceSize,
  fromByronCBOR,
  serialize,
  szCases,
  szGreedy,
  toByronCBOR,
 )
import Cardano.Prelude hiding (cborError)
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
  { forall a. AHeader a -> Annotated ProtocolMagicId a
aHeaderProtocolMagicId :: !(Annotated ProtocolMagicId a)
  , forall a. AHeader a -> Annotated HeaderHash a
aHeaderPrevHash :: !(Annotated HeaderHash a)
  -- ^ Pointer to the header of the previous block
  , forall a. AHeader a -> Annotated SlotNumber a
aHeaderSlot :: !(Annotated SlotNumber a)
  -- ^ The slot number this block was published for
  , forall a. AHeader a -> Annotated ChainDifficulty a
aHeaderDifficulty :: !(Annotated ChainDifficulty a)
  -- ^ The chain difficulty up to this block
  , forall a. AHeader a -> ProtocolVersion
headerProtocolVersion :: !ProtocolVersion
  -- ^ The version of the protocol parameters this block is using
  , forall a. AHeader a -> SoftwareVersion
headerSoftwareVersion :: !SoftwareVersion
  -- ^ The software version this block was published from
  , forall a. AHeader a -> Annotated Proof a
aHeaderProof :: !(Annotated Proof a)
  -- ^ Proof of body
  , forall a. AHeader a -> VerificationKey
headerGenesisKey :: !VerificationKey
  -- ^ The genesis key that is delegating to publish this block
  , forall a. AHeader a -> ABlockSignature a
headerSignature :: !(ABlockSignature a)
  -- ^ The signature of the block, which contains the delegation certificate
  , forall a. AHeader a -> a
headerAnnotation :: !a
  -- ^ An annotation that captures the full header bytes
  , forall a. AHeader a -> a
headerExtraAnnotation :: !a
  -- ^ An annotation that captures the bytes from the deprecated ExtraHeaderData
  }
  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
$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
/= :: 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
$cshowsPrec :: forall a. Show a => Int -> AHeader a -> ShowS
showsPrec :: Int -> AHeader a -> ShowS
$cshow :: forall a. Show a => AHeader a -> String
show :: AHeader a -> String
$cshowList :: forall a. Show a => [AHeader a] -> ShowS
showList :: [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
$cfrom :: forall a x. AHeader a -> Rep (AHeader a) x
from :: forall x. AHeader a -> Rep (AHeader a) x
$cto :: forall a x. Rep (AHeader a) x -> AHeader a
to :: forall x. Rep (AHeader a) x -> AHeader a
Generic, AHeader a -> ()
(AHeader a -> ()) -> NFData (AHeader a)
forall a. NFData a => AHeader a -> ()
forall a. (a -> ()) -> NFData a
$crnf :: forall a. NFData a => AHeader a -> ()
rnf :: AHeader a -> ()
NFData, (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
$cfmap :: forall a b. (a -> b) -> AHeader a -> AHeader b
fmap :: forall a b. (a -> b) -> AHeader a -> AHeader b
$c<$ :: forall a b. a -> AHeader b -> AHeader a
<$ :: forall a b. a -> AHeader b -> AHeader a
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
$cnoThunks :: forall a.
NoThunks a =>
Context -> AHeader a -> IO (Maybe ThunkInfo)
noThunks :: Context -> AHeader a -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall a.
NoThunks a =>
Context -> AHeader a -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> AHeader a -> IO (Maybe ThunkInfo)
$cshowTypeOf :: forall a. NoThunks a => Proxy (AHeader a) -> String
showTypeOf :: Proxy (AHeader a) -> String
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 (AHeader ())
-> EpochSlots
-> SlotNumber
-> SigningKey
-> Certificate
-> Body
-> ProtocolVersion
-> SoftwareVersion
-> AHeader ()
mkHeader ProtocolMagicId
pm Either GenesisHash (AHeader ())
prevHeader EpochSlots
epochSlots =
  ProtocolMagicId
-> HeaderHash
-> ChainDifficulty
-> EpochSlots
-> SlotNumber
-> SigningKey
-> Certificate
-> Body
-> ProtocolVersion
-> SoftwareVersion
-> AHeader ()
mkHeaderExplicit
    ProtocolMagicId
pm
    HeaderHash
prevHash
    ChainDifficulty
difficulty
    EpochSlots
epochSlots
  where
    prevHash :: HeaderHash
prevHash = (GenesisHash -> HeaderHash)
-> (AHeader () -> HeaderHash)
-> Either GenesisHash (AHeader ())
-> HeaderHash
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either GenesisHash -> HeaderHash
genesisHeaderHash (EpochSlots -> AHeader () -> HeaderHash
hashHeader EpochSlots
epochSlots) Either GenesisHash (AHeader ())
prevHeader
    difficulty :: ChainDifficulty
difficulty =
      (GenesisHash -> ChainDifficulty)
-> (AHeader () -> ChainDifficulty)
-> Either GenesisHash (AHeader ())
-> 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)
-> (AHeader () -> ChainDifficulty) -> AHeader () -> ChainDifficulty
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AHeader () -> ChainDifficulty
forall a. AHeader a -> ChainDifficulty
headerDifficulty)
        Either GenesisHash (AHeader ())
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
-> AHeader ()
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 ()
-> ()
-> ()
-> AHeader ()
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.
EncCBOR 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 :: forall a. 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 b c a. (b -> c) -> (a -> b) -> a -> c
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 :: forall a. 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 b c a. (b -> c) -> (a -> b) -> a -> c
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 :: forall a. 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 b c a. (b -> c) -> (a -> b) -> a -> c
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 :: forall a. 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 b c a. (b -> c) -> (a -> b) -> a -> c
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 :: forall a. 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 b c a. (b -> c) -> (a -> b) -> a -> c
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 :: forall a. 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 :: forall a. 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 b c a. (b -> c) -> (a -> b) -> a -> c
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 b c a. (b -> c) -> (a -> b) -> a -> c
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.
encCBORHeader :: EpochSlots -> Header -> Encoding
encCBORHeader :: EpochSlots -> AHeader () -> Encoding
encCBORHeader EpochSlots
es AHeader ()
h =
  Word -> Encoding
encodeListLen Word
5
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ProtocolMagicId -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR (AHeader () -> ProtocolMagicId
forall a. AHeader a -> ProtocolMagicId
headerProtocolMagicId AHeader ()
h)
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> HeaderHash -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR (AHeader () -> HeaderHash
forall a. AHeader a -> HeaderHash
headerPrevHash AHeader ()
h)
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Proof -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR (AHeader () -> Proof
forall a. AHeader a -> Proof
headerProof AHeader ()
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. EncCBOR a => a -> Encoding
encCBOR (EpochSlots -> SlotNumber -> EpochAndSlotCount
fromSlotNumber EpochSlots
es (SlotNumber -> EpochAndSlotCount)
-> SlotNumber -> EpochAndSlotCount
forall a b. (a -> b) -> a -> b
$ AHeader () -> SlotNumber
forall a. AHeader a -> SlotNumber
headerSlot AHeader ()
h)
           Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> VerificationKey -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR (AHeader () -> VerificationKey
forall a. AHeader a -> VerificationKey
headerGenesisKey AHeader ()
h)
           Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ChainDifficulty -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR (AHeader () -> ChainDifficulty
forall a. AHeader a -> ChainDifficulty
headerDifficulty AHeader ()
h)
           Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ABlockSignature () -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR (AHeader () -> ABlockSignature ()
forall a. AHeader a -> ABlockSignature a
headerSignature AHeader ()
h)
       )
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ProtocolVersion -> SoftwareVersion -> Encoding
encCBORBlockVersions (AHeader () -> ProtocolVersion
forall a. AHeader a -> ProtocolVersion
headerProtocolVersion AHeader ()
h) (AHeader () -> SoftwareVersion
forall a. AHeader a -> SoftwareVersion
headerSoftwareVersion AHeader ()
h)

encCBORHeaderSize :: Proxy EpochSlots -> Proxy (AHeader a) -> Size
encCBORHeaderSize :: forall a. Proxy EpochSlots -> Proxy (AHeader a) -> Size
encCBORHeaderSize 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 a b. Proxy (a -> b) -> Proxy a -> Proxy b
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 'EncCBOR' @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 (AHeader () -> ABlockSignature ()
forall a. AHeader a -> ABlockSignature a
headerSignature (AHeader () -> ABlockSignature ())
-> (AHeader a -> AHeader ()) -> AHeader a -> ABlockSignature ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
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 -> AHeader ()
forall a b. (a -> b) -> AHeader a -> AHeader b
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
encCBORBlockVersionsSize
      (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)

encCBORBlockVersions :: ProtocolVersion -> SoftwareVersion -> Encoding
encCBORBlockVersions :: ProtocolVersion -> SoftwareVersion -> Encoding
encCBORBlockVersions ProtocolVersion
pv SoftwareVersion
sv =
  Word -> Encoding
encodeListLen Word
4
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ProtocolVersion -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR ProtocolVersion
pv
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> SoftwareVersion -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR SoftwareVersion
sv
    -- Encoding of empty Attributes
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Map Word8 LByteString -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR (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. EncCBOR a => a -> Encoding
encCBOR (LByteString -> Hash Raw
hashRaw LByteString
"\129\160")

encCBORBlockVersionsSize :: Proxy ProtocolVersion -> Proxy SoftwareVersion -> Size
encCBORBlockVersionsSize :: Proxy ProtocolVersion -> Proxy SoftwareVersion -> Size
encCBORBlockVersionsSize 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))

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

decCBORAHeader :: EpochSlots -> Decoder s (AHeader ByteSpan)
decCBORAHeader :: forall s. EpochSlots -> Decoder s (AHeader ByteSpan)
decCBORAHeader EpochSlots
epochSlots = do
  Annotated
    ( pm
      , prevHash
      , proof
      , (slot, genesisKey, difficulty, sig)
      , Annotated (protocolVersion, softwareVersion) extraByteSpan
      )
    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. DecCBOR a => Decoder s (Annotated a ByteSpan)
decCBORAnnotated
        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 a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (Annotated HeaderHash ByteSpan)
forall a s. DecCBOR a => Decoder s (Annotated a ByteSpan)
decCBORAnnotated
        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 a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (Annotated Proof ByteSpan)
forall a s. DecCBOR a => Decoder s (Annotated a ByteSpan)
decCBORAnnotated
        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 a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> 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 a b. (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((EpochAndSlotCount -> SlotNumber)
-> Annotated EpochAndSlotCount ByteSpan
-> Annotated SlotNumber ByteSpan
forall a b c. (a -> b) -> Annotated a c -> Annotated b c
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. DecCBOR a => Decoder s (Annotated a ByteSpan)
decCBORAnnotated
            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 a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s VerificationKey
forall s. Decoder s VerificationKey
forall a s. DecCBOR a => Decoder s a
decCBOR
            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 a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (Annotated ChainDifficulty ByteSpan)
forall a s. DecCBOR a => Decoder s (Annotated a ByteSpan)
decCBORAnnotated
            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 a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (ABlockSignature ByteSpan)
forall s. Decoder s (ABlockSignature ByteSpan)
forall a s. DecCBOR a => Decoder s a
decCBOR
        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 a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (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)
decCBORBlockVersions
  pure
    $ AHeader
      pm
      prevHash
      slot
      difficulty
      protocolVersion
      softwareVersion
      proof
      genesisKey
      sig
      byteSpan
      extraByteSpan

decCBORBlockVersions :: Decoder s (ProtocolVersion, SoftwareVersion)
decCBORBlockVersions :: forall s. Decoder s (ProtocolVersion, SoftwareVersion)
decCBORBlockVersions = 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 s. Decoder s ProtocolVersion
forall a s. DecCBOR a => Decoder s a
decCBOR Decoder s (SoftwareVersion -> (ProtocolVersion, SoftwareVersion))
-> Decoder s SoftwareVersion
-> Decoder s (ProtocolVersion, SoftwareVersion)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s SoftwareVersion
forall s. Decoder s SoftwareVersion
forall a s. DecCBOR a => Decoder s a
decCBOR Decoder s (ProtocolVersion, SoftwareVersion)
-> Decoder s () -> Decoder s (ProtocolVersion, SoftwareVersion)
forall a b. Decoder s a -> Decoder s b -> Decoder s a
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 a b. Decoder s a -> Decoder s b -> Decoder s a
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
encCBORHeaderToHash :: EpochSlots -> Header -> Encoding
encCBORHeaderToHash :: EpochSlots -> AHeader () -> Encoding
encCBORHeaderToHash EpochSlots
epochSlots AHeader ()
h =
  Word -> Encoding
encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR (Word
1 :: Word) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> EpochSlots -> AHeader () -> Encoding
encCBORHeader EpochSlots
epochSlots AHeader ()
h

decCBORHeaderToHash :: EpochSlots -> Decoder s (Maybe Header)
decCBORHeaderToHash :: forall s. EpochSlots -> Decoder s (Maybe (AHeader ()))
decCBORHeaderToHash EpochSlots
epochSlots = do
  Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"Header" Int
2
  forall a s. DecCBOR a => Decoder s a
decCBOR @Word Decoder s Word
-> (Word -> Decoder s (Maybe (AHeader ())))
-> Decoder s (Maybe (AHeader ()))
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
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)
decCBORABoundaryHeader
      Maybe (AHeader ()) -> Decoder s (Maybe (AHeader ()))
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (AHeader ())
forall a. Maybe a
Nothing
    Word
1 -> AHeader () -> Maybe (AHeader ())
forall a. a -> Maybe a
Just (AHeader () -> Maybe (AHeader ()))
-> Decoder s (AHeader ()) -> Decoder s (Maybe (AHeader ()))
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> EpochSlots -> Decoder s (AHeader ())
forall s. EpochSlots -> Decoder s (AHeader ())
decCBORHeader EpochSlots
epochSlots
    Word
t -> DecoderError -> Decoder s (Maybe (AHeader ()))
forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError (DecoderError -> Decoder s (Maybe (AHeader ())))
-> DecoderError -> Decoder s (Maybe (AHeader ()))
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 (AHeader ()) -> Builder
build (WithEpochSlots EpochSlots
es AHeader ()
header) = EpochSlots -> AHeader () -> Builder
renderHeader EpochSlots
es AHeader ()
header

renderHeader :: EpochSlots -> Header -> Builder
renderHeader :: EpochSlots -> AHeader () -> Builder
renderHeader EpochSlots
es AHeader ()
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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
    (AHeader () -> HeaderHash
forall a. AHeader a -> HeaderHash
headerPrevHash AHeader ()
header)
    (AHeader () -> SlotNumber
forall a. AHeader a -> SlotNumber
headerSlot AHeader ()
header)
    (ChainDifficulty -> Word64
unChainDifficulty (ChainDifficulty -> Word64) -> ChainDifficulty -> Word64
forall a b. (a -> b) -> a -> b
$ AHeader () -> ChainDifficulty
forall a. AHeader a -> ChainDifficulty
headerDifficulty AHeader ()
header)
    (AHeader () -> ProtocolVersion
forall a. AHeader a -> ProtocolVersion
headerProtocolVersion AHeader ()
header)
    (AHeader () -> SoftwareVersion
forall a. AHeader a -> SoftwareVersion
headerSoftwareVersion AHeader ()
header)
    (AHeader () -> VerificationKey
forall a. AHeader a -> VerificationKey
headerGenesisKey AHeader ()
header)
    (AHeader () -> ABlockSignature ()
forall a. AHeader a -> ABlockSignature a
headerSignature AHeader ()
header)
  where
    headerHash :: HeaderHash
    headerHash :: HeaderHash
headerHash = EpochSlots -> AHeader () -> HeaderHash
hashHeader EpochSlots
es AHeader ()
header

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

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

-- | Specialized formatter for 'HeaderHash'
headerHashF :: Format r (HeaderHash -> r)
headerHashF :: forall r. 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
forall a b. Coercible a b => a -> b
coerce (Hash Raw -> HeaderHash)
-> (GenesisHash -> Hash Raw) -> GenesisHash -> HeaderHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
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 <> encCBOR (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 'encCBORHeaderToHash'
hashHeader :: EpochSlots -> Header -> HeaderHash
hashHeader :: EpochSlots -> AHeader () -> HeaderHash
hashHeader EpochSlots
es = LByteString -> HeaderHash
forall algo a.
HashAlgorithm algo =>
LByteString -> AbstractHash algo a
unsafeAbstractHash (LByteString -> HeaderHash)
-> (AHeader () -> LByteString) -> AHeader () -> HeaderHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Version -> Encoding -> LByteString
forall a. EncCBOR a => Version -> a -> LByteString
serialize Version
byronProtVer (Encoding -> LByteString)
-> (AHeader () -> Encoding) -> AHeader () -> LByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. EpochSlots -> AHeader () -> Encoding
encCBORHeaderToHash EpochSlots
es

headerHashAnnotated :: AHeader ByteString -> HeaderHash
headerHashAnnotated :: AHeader ByteString -> HeaderHash
headerHashAnnotated = AHeader ByteString -> Hash (BaseType (AHeader ByteString))
AHeader ByteString -> HeaderHash
forall t. Decoded t => t -> Hash (BaseType t)
hashDecoded (AHeader ByteString -> HeaderHash)
-> (AHeader ByteString -> AHeader ByteString)
-> AHeader ByteString
-> HeaderHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
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 a b. (a -> b) -> AHeader a -> AHeader b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> ByteString
wrapHeaderBytes

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

data ABoundaryHeader a = UnsafeABoundaryHeader
  { forall a. ABoundaryHeader a -> Either GenesisHash HeaderHash
boundaryPrevHash :: !(Either GenesisHash HeaderHash)
  , forall a. ABoundaryHeader a -> Word64
boundaryEpoch :: !Word64
  , forall a. ABoundaryHeader a -> ChainDifficulty
boundaryDifficulty :: !ChainDifficulty
  , forall a. 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
$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
/= :: 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
$cshowsPrec :: forall a. Show a => Int -> ABoundaryHeader a -> ShowS
showsPrec :: Int -> ABoundaryHeader a -> ShowS
$cshow :: forall a. Show a => ABoundaryHeader a -> String
show :: ABoundaryHeader a -> String
$cshowList :: forall a. Show a => [ABoundaryHeader a] -> ShowS
showList :: [ABoundaryHeader a] -> ShowS
Show, (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
$cfmap :: forall a b. (a -> b) -> ABoundaryHeader a -> ABoundaryHeader b
fmap :: forall a b. (a -> b) -> ABoundaryHeader a -> ABoundaryHeader b
$c<$ :: forall a b. a -> ABoundaryHeader b -> ABoundaryHeader a
<$ :: forall a b. a -> ABoundaryHeader b -> ABoundaryHeader a
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
$cfrom :: forall a x. ABoundaryHeader a -> Rep (ABoundaryHeader a) x
from :: forall x. ABoundaryHeader a -> Rep (ABoundaryHeader a) x
$cto :: forall a x. Rep (ABoundaryHeader a) x -> ABoundaryHeader a
to :: forall x. Rep (ABoundaryHeader a) x -> ABoundaryHeader a
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
$cnoThunks :: forall a.
NoThunks a =>
Context -> ABoundaryHeader a -> IO (Maybe ThunkInfo)
noThunks :: Context -> ABoundaryHeader a -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall a.
NoThunks a =>
Context -> ABoundaryHeader a -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> ABoundaryHeader a -> IO (Maybe ThunkInfo)
$cshowTypeOf :: forall a. NoThunks a => Proxy (ABoundaryHeader a) -> String
showTypeOf :: Proxy (ABoundaryHeader a) -> String
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 :: forall a.
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 = Hash (BaseType (ABoundaryHeader ByteString)) -> HeaderHash
forall a b. Coercible a b => a -> b
coerce (Hash (BaseType (ABoundaryHeader ByteString)) -> HeaderHash)
-> (ABoundaryHeader ByteString
    -> Hash (BaseType (ABoundaryHeader ByteString)))
-> ABoundaryHeader ByteString
-> HeaderHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ABoundaryHeader ByteString
-> Hash (BaseType (ABoundaryHeader ByteString))
forall t. Decoded t => t -> Hash (BaseType t)
hashDecoded (ABoundaryHeader ByteString
 -> Hash (BaseType (ABoundaryHeader ByteString)))
-> (ABoundaryHeader ByteString -> ABoundaryHeader ByteString)
-> ABoundaryHeader ByteString
-> Hash (BaseType (ABoundaryHeader ByteString))
forall b c a. (b -> c) -> (a -> b) -> a -> c
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 a b. (a -> b) -> ABoundaryHeader a -> ABoundaryHeader b
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 `decCBORBoundaryHeader`, 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).
encCBORABoundaryHeader :: ProtocolMagicId -> ABoundaryHeader a -> Encoding
encCBORABoundaryHeader :: forall a. ProtocolMagicId -> ABoundaryHeader a -> Encoding
encCBORABoundaryHeader ProtocolMagicId
pm ABoundaryHeader a
hdr =
  Word -> Encoding
encodeListLen Word
5
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ProtocolMagicId -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR 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. EncCBOR a => a -> Encoding
encCBOR (GenesisHash -> HeaderHash
genesisHeaderHash GenesisHash
gh)
           Right HeaderHash
hh -> HeaderHash -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR 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. EncCBOR a => a -> Encoding
encCBOR ([()] -> Hash [()]
forall a. EncCBOR 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. EncCBOR a => a -> Encoding
encCBOR (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. EncCBOR a => a -> Encoding
encCBOR (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. EncCBOR a => a -> Encoding
encCBOR 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

encCBORABoundaryHeaderSize :: Proxy ProtocolMagicId -> Proxy (ABoundaryHeader a) -> Size
encCBORABoundaryHeaderSize :: forall a.
Proxy ProtocolMagicId -> Proxy (ABoundaryHeader a) -> Size
encCBORABoundaryHeaderSize 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 :: forall a b. 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 :: forall a b. Proxy (Either a b) -> Proxy b
pFromRight Proxy (Either a b)
_ = Proxy b
forall {k} (t :: k). Proxy t
Proxy

decCBORABoundaryHeader :: Decoder s (ABoundaryHeader ByteSpan)
decCBORABoundaryHeader :: forall s. Decoder s (ABoundaryHeader ByteSpan)
decCBORABoundaryHeader = do
  Annotated header 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
    hh <- Decoder s HeaderHash
forall s. Decoder s HeaderHash
forall a s. DecCBOR a => Decoder s a
decCBOR
    -- BoundaryBodyProof
    dropBytes
    (epoch, difficulty) <- decCBORBoundaryConsensusData
    isGen <- dropBoundaryExtraHeaderDataRetainGenesisTag
    let 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
forall a b. Coercible a b => a -> b
coerce HeaderHash
hh) else HeaderHash -> Either GenesisHash HeaderHash
forall a b. b -> Either a b
Right HeaderHash
hh
    pure $ mkABoundaryHeader hh' epoch difficulty ()
  pure (header {boundaryHeaderAnnotation = 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
  { forall a. ABlockSignature a -> ACertificate a
delegationCertificate :: !(Delegation.ACertificate a)
  , forall 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
$cshowsPrec :: forall a. Show a => Int -> ABlockSignature a -> ShowS
showsPrec :: Int -> ABlockSignature a -> ShowS
$cshow :: forall a. Show a => ABlockSignature a -> String
show :: ABlockSignature a -> String
$cshowList :: forall a. Show a => [ABlockSignature a] -> ShowS
showList :: [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
$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
/= :: 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
$cfrom :: forall a x. ABlockSignature a -> Rep (ABlockSignature a) x
from :: forall x. ABlockSignature a -> Rep (ABlockSignature a) x
$cto :: forall a x. Rep (ABlockSignature a) x -> ABlockSignature a
to :: forall x. Rep (ABlockSignature a) x -> ABlockSignature a
Generic, (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
$cfmap :: forall a b. (a -> b) -> ABlockSignature a -> ABlockSignature b
fmap :: forall a b. (a -> b) -> ABlockSignature a -> ABlockSignature b
$c<$ :: forall a b. a -> ABlockSignature b -> ABlockSignature a
<$ :: forall a b. a -> ABlockSignature b -> ABlockSignature a
Functor)
  deriving anyclass (ABlockSignature a -> ()
(ABlockSignature a -> ()) -> NFData (ABlockSignature a)
forall a. NFData a => ABlockSignature a -> ()
forall a. (a -> ()) -> NFData a
$crnf :: forall a. NFData a => ABlockSignature a -> ()
rnf :: 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
$cnoThunks :: forall a.
NoThunks a =>
Context -> ABlockSignature a -> IO (Maybe ThunkInfo)
noThunks :: Context -> ABlockSignature a -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall a.
NoThunks a =>
Context -> ABlockSignature a -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> ABlockSignature a -> IO (Maybe ThunkInfo)
$cshowTypeOf :: forall a. NoThunks a => Proxy (ABlockSignature a) -> String
showTypeOf :: Proxy (ABlockSignature a) -> String
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 b c a. Format b c -> Format a b -> Format a c
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 b c a. Format b c -> Format a b -> Format a c
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 () -> Encoding
forall a. EncCBOR a => a -> Encoding
toByronCBOR
  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 Proxy t -> Size
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 Proxy t -> Size
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 :: forall s. Decoder s (ABlockSignature ())
fromCBOR = Decoder s (ABlockSignature ())
forall a s. DecCBOR a => Decoder s a
fromByronCBOR

instance FromCBOR (ABlockSignature ByteSpan) where
  fromCBOR :: forall s. Decoder s (ABlockSignature ByteSpan)
fromCBOR = Decoder s (ABlockSignature ByteSpan)
forall a s. DecCBOR a => Decoder s a
fromByronCBOR

instance EncCBOR BlockSignature where
  encCBOR :: ABlockSignature () -> Encoding
encCBOR (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. EncCBOR a => a -> Encoding
encCBOR (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. EncCBOR a => a -> Encoding
encCBOR Certificate
cert Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Signature ToSign -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR Signature ToSign
sig)

instance DecCBOR BlockSignature where
  decCBOR :: forall s. Decoder s (ABlockSignature ())
decCBOR = 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 a s. DecCBOR a => Decoder s a
decCBOR @(ABlockSignature ByteSpan)

instance DecCBOR (ABlockSignature ByteSpan) where
  decCBOR :: forall s. Decoder s (ABlockSignature ByteSpan)
decCBOR = do
    Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"BlockSignature" Int
2
    Decoder s Word8
forall s. Decoder s Word8
forall a s. DecCBOR a => Decoder s a
decCBOR Decoder s Word8
-> (Word8 -> Decoder s (ABlockSignature ByteSpan))
-> Decoder s (ABlockSignature ByteSpan)
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
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 a b. a -> Decoder s b -> Decoder s a
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 a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (ACertificate ByteSpan)
forall s. Decoder s (ACertificate ByteSpan)
forall a s. DecCBOR a => Decoder s a
decCBOR
          Decoder s (Signature ToSign -> ABlockSignature ByteSpan)
-> Decoder s (Signature ToSign)
-> Decoder s (ABlockSignature ByteSpan)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (Signature ToSign)
forall s. Decoder s (Signature ToSign)
forall a s. DecCBOR a => Decoder s a
decCBOR
      Word8
t -> DecoderError -> Decoder s (ABlockSignature ByteSpan)
forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m 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 b c a. (b -> c) -> (a -> b) -> a -> c
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 b c a. (b -> c) -> (a -> b) -> a -> c
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 b c a. (b -> c) -> (a -> b) -> a -> c
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 b c a. (b -> c) -> (a -> b) -> a -> c
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
  { ToSign -> HeaderHash
tsHeaderHash :: !HeaderHash
  -- ^ Hash of previous header in the chain
  , 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
$c== :: ToSign -> ToSign -> Bool
== :: ToSign -> ToSign -> Bool
$c/= :: ToSign -> ToSign -> Bool
/= :: 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
$cshowsPrec :: Int -> ToSign -> ShowS
showsPrec :: Int -> ToSign -> ShowS
$cshow :: ToSign -> String
show :: ToSign -> String
$cshowList :: [ToSign] -> ShowS
showList :: [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
$cfrom :: forall x. ToSign -> Rep ToSign x
from :: forall x. ToSign -> Rep ToSign x
$cto :: forall x. Rep ToSign x -> ToSign
to :: forall x. Rep ToSign x -> ToSign
Generic)

instance ToCBOR ToSign where
  toCBOR :: ToSign -> Encoding
toCBOR = ToSign -> Encoding
forall a. EncCBOR a => a -> Encoding
toByronCBOR
  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 Proxy t -> Size
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 Proxy t -> Size
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 Proxy t -> Size
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 Proxy t -> Size
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
encCBORBlockVersionsSize (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 :: forall s. Decoder s ToSign
fromCBOR = Decoder s ToSign
forall a s. DecCBOR a => Decoder s a
fromByronCBOR

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

instance DecCBOR ToSign where
  decCBOR :: forall s. Decoder s ToSign
decCBOR = 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 a b. (a -> b) -> Decoder s a -> Decoder s b
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 s. Decoder s HeaderHash
forall a s. DecCBOR a => Decoder s a
decCBOR Decoder
  s
  (Proof
   -> EpochAndSlotCount
   -> ChainDifficulty
   -> ProtocolVersion
   -> SoftwareVersion
   -> ToSign)
-> Decoder s Proof
-> Decoder
     s
     (EpochAndSlotCount
      -> ChainDifficulty -> ProtocolVersion -> SoftwareVersion -> ToSign)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Proof
forall s. Decoder s Proof
forall a s. DecCBOR a => Decoder s a
decCBOR Decoder
  s
  (EpochAndSlotCount
   -> ChainDifficulty -> ProtocolVersion -> SoftwareVersion -> ToSign)
-> Decoder s EpochAndSlotCount
-> Decoder
     s (ChainDifficulty -> ProtocolVersion -> SoftwareVersion -> ToSign)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s EpochAndSlotCount
forall s. Decoder s EpochAndSlotCount
forall a s. DecCBOR a => Decoder s a
decCBOR Decoder
  s (ChainDifficulty -> ProtocolVersion -> SoftwareVersion -> ToSign)
-> Decoder s ChainDifficulty
-> Decoder s (ProtocolVersion -> SoftwareVersion -> ToSign)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s ChainDifficulty
forall s. Decoder s ChainDifficulty
forall a s. DecCBOR a => Decoder s a
decCBOR)
      Decoder s ((ProtocolVersion, SoftwareVersion) -> ToSign)
-> Decoder s (ProtocolVersion, SoftwareVersion) -> Decoder s ToSign
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (ProtocolVersion, SoftwareVersion)
forall s. Decoder s (ProtocolVersion, SoftwareVersion)
decCBORBlockVersions