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

module Cardano.Ledger.Shelley.Block () where

import Cardano.Ledger.Binary (
  Annotator,
  DecCBOR (decCBOR),
  EncCBOR (..),
  EncCBORGroup (..),
  decodeRecordNamed,
  encodeListLen,
  toPlainEncoding,
 )
import qualified Cardano.Ledger.Binary.Plain as Plain
import Cardano.Ledger.Block (Block (..))
import Cardano.Ledger.Core
import Cardano.Ledger.Shelley.BlockBody ()
import Cardano.Ledger.Shelley.Era
import Cardano.Ledger.Shelley.Tx ()
import Data.Typeable (Typeable)

instance EncCBOR h => EncCBOR (Block h ShelleyEra) where
  encCBOR :: Block h ShelleyEra -> Encoding
encCBOR (Block h
h BlockBody ShelleyEra
txns) =
    Word -> Encoding
encodeListLen Word
4 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
<> ShelleyBlockBody ShelleyEra -> Encoding
forall a. EncCBORGroup a => a -> Encoding
encCBORGroup BlockBody ShelleyEra
ShelleyBlockBody ShelleyEra
txns

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

instance
  (DecCBOR (Annotator h), Typeable h) =>
  DecCBOR (Annotator (Block h ShelleyEra))
  where
  decCBOR :: forall s. Decoder s (Annotator (Block h ShelleyEra))
decCBOR =
    Text
-> (Annotator (Block h ShelleyEra) -> Int)
-> Decoder s (Annotator (Block h ShelleyEra))
-> Decoder s (Annotator (Block h ShelleyEra))
forall a s. Text -> (a -> Int) -> Decoder s a -> Decoder s a
decodeRecordNamed Text
"Block" (Int -> Annotator (Block h ShelleyEra) -> Int
forall a b. a -> b -> a
const Int
4) (Decoder s (Annotator (Block h ShelleyEra))
 -> Decoder s (Annotator (Block h ShelleyEra)))
-> Decoder s (Annotator (Block h ShelleyEra))
-> Decoder s (Annotator (Block h ShelleyEra))
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
      txns <- decCBOR
      pure $ Block <$> header <*> txns