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

module Cardano.Ledger.Allegra.BlockBody () where

import Cardano.Ledger.Allegra.Era (AllegraEra)
import Cardano.Ledger.Allegra.Tx ()
import Cardano.Ledger.BaseTypes (ProtVer (..))
import Cardano.Ledger.Binary (
  Annotator,
  DecCBOR (decCBOR),
  EncCBOR (..),
  EncCBORGroup (..),
  decodeRecordNamed,
  encodeListLen,
  serialize',
  toPlainEncoding,
 )
import qualified Cardano.Ledger.Binary.Plain as Plain
import Cardano.Ledger.Block (Block (..))
import Cardano.Ledger.Core (EraBlockBody (..), eraProtVerLow)
import Cardano.Ledger.Shelley.BlockBody (
  ShelleyBlockBody,
  mkBasicBlockBodyShelley,
  shelleyBlockBodyHash,
  txSeqBlockBodyShelleyL,
 )
import qualified Data.ByteString as BS
import Data.Typeable (Typeable)

instance EraBlockBody AllegraEra where
  type BlockBody AllegraEra = ShelleyBlockBody AllegraEra
  mkBasicBlockBody :: BlockBody AllegraEra
mkBasicBlockBody = BlockBody AllegraEra
forall era.
(EraBlockBody era, SafeToHash (TxWits era),
 BlockBody era ~ ShelleyBlockBody era) =>
BlockBody era
mkBasicBlockBodyShelley
  txSeqBlockBodyL :: Lens' (BlockBody AllegraEra) (StrictSeq (Tx TopTx AllegraEra))
txSeqBlockBodyL = (StrictSeq (Tx TopTx AllegraEra)
 -> f (StrictSeq (Tx TopTx AllegraEra)))
-> BlockBody AllegraEra -> f (BlockBody AllegraEra)
forall era.
(EraBlockBody era, SafeToHash (TxWits era),
 BlockBody era ~ ShelleyBlockBody era) =>
Lens' (BlockBody era) (StrictSeq (Tx TopTx era))
Lens' (BlockBody AllegraEra) (StrictSeq (Tx TopTx AllegraEra))
txSeqBlockBodyShelleyL
  hashBlockBody :: BlockBody AllegraEra -> Hash HASH EraIndependentBlockBody
hashBlockBody = BlockBody AllegraEra -> Hash HASH EraIndependentBlockBody
ShelleyBlockBody AllegraEra -> Hash HASH EraIndependentBlockBody
forall era.
ShelleyBlockBody era -> Hash HASH EraIndependentBlockBody
shelleyBlockBodyHash
  blockBodySize :: ProtVer -> BlockBody AllegraEra -> Int
blockBodySize (ProtVer Version
v Word32
_) = ByteString -> Int
BS.length (ByteString -> Int)
-> (ShelleyBlockBody AllegraEra -> ByteString)
-> ShelleyBlockBody AllegraEra
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Version -> Encoding -> ByteString
forall a. EncCBOR a => Version -> a -> ByteString
serialize' Version
v (Encoding -> ByteString)
-> (ShelleyBlockBody AllegraEra -> Encoding)
-> ShelleyBlockBody AllegraEra
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShelleyBlockBody AllegraEra -> Encoding
forall a. EncCBORGroup a => a -> Encoding
encCBORGroup

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

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

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