{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Ledger.Dijkstra.Block () where

import Cardano.Ledger.Binary (
  Annotator,
  DecCBOR (decCBOR),
  EncCBOR (..),
  decodeRecordNamed,
  encodeListLen,
  toPlainEncoding,
 )
import qualified Cardano.Ledger.Binary.Plain as Plain
import Cardano.Ledger.Block (Block (..))
import Cardano.Ledger.Core (EraBlockBody, eraProtVerLow)
import Cardano.Ledger.Dijkstra.BlockBody.Internal ()
import Cardano.Ledger.Dijkstra.Era (DijkstraEra)
import Data.Typeable (Typeable)

instance EncCBOR h => EncCBOR (Block h DijkstraEra) where
  encCBOR :: Block h DijkstraEra -> Encoding
encCBOR (Block h
h BlockBody DijkstraEra
body) =
    Word -> Encoding
encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> h -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR h
h Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> DijkstraBlockBody DijkstraEra -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR BlockBody DijkstraEra
DijkstraBlockBody DijkstraEra
body

instance (EncCBOR h, Typeable h) => Plain.ToCBOR (Block h DijkstraEra) where
  toCBOR :: Block h DijkstraEra -> Encoding
toCBOR = Version -> Encoding -> Encoding
toPlainEncoding (forall era. Era era => Version
eraProtVerLow @DijkstraEra) (Encoding -> Encoding)
-> (Block h DijkstraEra -> Encoding)
-> Block h DijkstraEra
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Block h DijkstraEra -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR

instance
  (DecCBOR (Annotator h), Typeable h, EraBlockBody DijkstraEra) =>
  DecCBOR (Annotator (Block h DijkstraEra))
  where
  decCBOR :: forall s. Decoder s (Annotator (Block h DijkstraEra))
decCBOR =
    Text
-> (Annotator (Block h DijkstraEra) -> Int)
-> Decoder s (Annotator (Block h DijkstraEra))
-> Decoder s (Annotator (Block h DijkstraEra))
forall a s. Text -> (a -> Int) -> Decoder s a -> Decoder s a
decodeRecordNamed Text
"Block" (Int -> Annotator (Block h DijkstraEra) -> Int
forall a b. a -> b -> a
const Int
2) (Decoder s (Annotator (Block h DijkstraEra))
 -> Decoder s (Annotator (Block h DijkstraEra)))
-> Decoder s (Annotator (Block h DijkstraEra))
-> Decoder s (Annotator (Block h DijkstraEra))
forall a b. (a -> b) -> a -> b
$ do
      header <- Decoder s (Annotator h)
forall s. Decoder s (Annotator h)
forall a s. DecCBOR a => Decoder s a
decCBOR
      body <- decCBOR
      pure $ Block <$> header <*> body