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

module Cardano.Chain.Delegation.Payload (
  APayload (..),
  Payload,
  unsafePayload,
)
where

import qualified Cardano.Chain.Delegation.Certificate as Delegation
import Cardano.Ledger.Binary (
  Annotated (..),
  ByteSpan,
  DecCBOR (..),
  Decoded (..),
  EncCBOR (..),
  FromCBOR (..),
  ToCBOR (..),
  annotatedDecoder,
  fromByronCBOR,
  toByronCBOR,
 )
import Cardano.Prelude
import Data.Aeson (ToJSON)
import Formatting (bprint, int)
import Formatting.Buildable (Buildable (..))

-- | The delegation 'Payload' contains a list of delegation 'Certificate's
data APayload a = UnsafeAPayload
  { forall a. APayload a -> [ACertificate a]
getPayload :: [Delegation.ACertificate a]
  , forall a. APayload a -> a
getAnnotation :: a
  }
  deriving (Int -> APayload a -> ShowS
forall a. Show a => Int -> APayload a -> ShowS
forall a. Show a => [APayload a] -> ShowS
forall a. Show a => APayload a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [APayload a] -> ShowS
$cshowList :: forall a. Show a => [APayload a] -> ShowS
show :: APayload a -> String
$cshow :: forall a. Show a => APayload a -> String
showsPrec :: Int -> APayload a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> APayload a -> ShowS
Show, APayload a -> APayload a -> Bool
forall a. Eq a => APayload a -> APayload a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: APayload a -> APayload a -> Bool
$c/= :: forall a. Eq a => APayload a -> APayload a -> Bool
== :: APayload a -> APayload a -> Bool
$c== :: forall a. Eq a => APayload a -> APayload a -> Bool
Eq, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (APayload a) x -> APayload a
forall a x. APayload a -> Rep (APayload a) x
$cto :: forall a x. Rep (APayload a) x -> APayload a
$cfrom :: forall a x. APayload a -> Rep (APayload a) x
Generic, forall a b. a -> APayload b -> APayload a
forall a b. (a -> b) -> APayload a -> APayload b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> APayload b -> APayload a
$c<$ :: forall a b. a -> APayload b -> APayload a
fmap :: forall a b. (a -> b) -> APayload a -> APayload b
$cfmap :: forall a b. (a -> b) -> APayload a -> APayload b
Functor)
  deriving anyclass (forall a. NFData a => APayload a -> ()
forall a. (a -> ()) -> NFData a
rnf :: APayload a -> ()
$crnf :: forall a. NFData a => APayload a -> ()
NFData)

type Payload = APayload ()

unsafePayload :: [Delegation.Certificate] -> Payload
unsafePayload :: [Certificate] -> Payload
unsafePayload [Certificate]
sks = forall a. [ACertificate a] -> a -> APayload a
UnsafeAPayload [Certificate]
sks ()

instance Decoded (APayload ByteString) where
  type BaseType (APayload ByteString) = Payload
  recoverBytes :: APayload ByteString -> ByteString
recoverBytes = forall a. APayload a -> a
getAnnotation

instance Buildable (APayload a) where
  build :: APayload a -> Builder
build (UnsafeAPayload [ACertificate a]
psks a
_) =
    forall a. Format Builder a -> a
bprint
      (Format
  (Int -> [ACertificate a] -> Builder)
  (Int -> [ACertificate a] -> Builder)
"proxy signing keys (" forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a r. Integral a => Format r (a -> r)
int forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format ([ACertificate a] -> Builder) ([ACertificate a] -> Builder)
" items): " forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall (t :: * -> *) a r.
(Foldable t, Buildable a) =>
Format r (t a -> r)
listJson forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format Builder Builder
"\n")
      (forall a. HasLength a => a -> Int
length [ACertificate a]
psks)
      [ACertificate a]
psks

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

instance ToCBOR Payload where
  toCBOR :: Payload -> Encoding
toCBOR = forall a. EncCBOR a => a -> Encoding
toByronCBOR

instance FromCBOR Payload where
  fromCBOR :: forall s. Decoder s Payload
fromCBOR = forall a s. DecCBOR a => Decoder s a
fromByronCBOR

instance EncCBOR Payload where
  encCBOR :: Payload -> Encoding
encCBOR = forall a. EncCBOR a => a -> Encoding
encCBOR forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. APayload a -> [ACertificate a]
getPayload

instance DecCBOR Payload where
  decCBOR :: forall s. Decoder s Payload
decCBOR = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. DecCBOR a => Decoder s a
decCBOR @(APayload ByteSpan)

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

instance DecCBOR (APayload ByteSpan) where
  decCBOR :: forall s. Decoder s (APayload ByteSpan)
decCBOR = do
    (Annotated [ACertificate ByteSpan]
p ByteSpan
a) <- forall s a. Decoder s a -> Decoder s (Annotated a ByteSpan)
annotatedDecoder forall a s. DecCBOR a => Decoder s a
decCBOR
    forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. [ACertificate a] -> a -> APayload a
UnsafeAPayload [ACertificate ByteSpan]
p ByteSpan
a)