{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-missed-specialisations #-}

module Cardano.Chain.Genesis.KeyHashes (
  GenesisKeyHashes (..),
)
where

import Cardano.Chain.Common (KeyHash)
import Cardano.Ledger.Binary
import Cardano.Prelude
import qualified Data.Map.Strict as M
import qualified Data.Set as Set
import Formatting (bprint)
import Formatting.Buildable (Buildable (..))
import NoThunks.Class (NoThunks (..))
import Text.JSON.Canonical (FromJSON (..), ToJSON (..))

-- | The set of genesis keys, who are able to produce blocks and submit votes
--   and proposals in the Byron era
newtype GenesisKeyHashes = GenesisKeyHashes
  { GenesisKeyHashes -> Set KeyHash
unGenesisKeyHashes :: Set KeyHash
  }
  deriving (Int -> GenesisKeyHashes -> ShowS
[GenesisKeyHashes] -> ShowS
GenesisKeyHashes -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GenesisKeyHashes] -> ShowS
$cshowList :: [GenesisKeyHashes] -> ShowS
show :: GenesisKeyHashes -> String
$cshow :: GenesisKeyHashes -> String
showsPrec :: Int -> GenesisKeyHashes -> ShowS
$cshowsPrec :: Int -> GenesisKeyHashes -> ShowS
Show, GenesisKeyHashes -> GenesisKeyHashes -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GenesisKeyHashes -> GenesisKeyHashes -> Bool
$c/= :: GenesisKeyHashes -> GenesisKeyHashes -> Bool
== :: GenesisKeyHashes -> GenesisKeyHashes -> Bool
$c== :: GenesisKeyHashes -> GenesisKeyHashes -> Bool
Eq, NonEmpty GenesisKeyHashes -> GenesisKeyHashes
GenesisKeyHashes -> GenesisKeyHashes -> GenesisKeyHashes
forall b. Integral b => b -> GenesisKeyHashes -> GenesisKeyHashes
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: forall b. Integral b => b -> GenesisKeyHashes -> GenesisKeyHashes
$cstimes :: forall b. Integral b => b -> GenesisKeyHashes -> GenesisKeyHashes
sconcat :: NonEmpty GenesisKeyHashes -> GenesisKeyHashes
$csconcat :: NonEmpty GenesisKeyHashes -> GenesisKeyHashes
<> :: GenesisKeyHashes -> GenesisKeyHashes -> GenesisKeyHashes
$c<> :: GenesisKeyHashes -> GenesisKeyHashes -> GenesisKeyHashes
Semigroup, Semigroup GenesisKeyHashes
GenesisKeyHashes
[GenesisKeyHashes] -> GenesisKeyHashes
GenesisKeyHashes -> GenesisKeyHashes -> GenesisKeyHashes
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [GenesisKeyHashes] -> GenesisKeyHashes
$cmconcat :: [GenesisKeyHashes] -> GenesisKeyHashes
mappend :: GenesisKeyHashes -> GenesisKeyHashes -> GenesisKeyHashes
$cmappend :: GenesisKeyHashes -> GenesisKeyHashes -> GenesisKeyHashes
mempty :: GenesisKeyHashes
$cmempty :: GenesisKeyHashes
Monoid, Context -> GenesisKeyHashes -> IO (Maybe ThunkInfo)
Proxy GenesisKeyHashes -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy GenesisKeyHashes -> String
$cshowTypeOf :: Proxy GenesisKeyHashes -> String
wNoThunks :: Context -> GenesisKeyHashes -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> GenesisKeyHashes -> IO (Maybe ThunkInfo)
noThunks :: Context -> GenesisKeyHashes -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> GenesisKeyHashes -> IO (Maybe ThunkInfo)
NoThunks)

instance Buildable GenesisKeyHashes where
  build :: GenesisKeyHashes -> Builder
build (GenesisKeyHashes Set KeyHash
m) =
    forall a. Format Builder a -> a
bprint (Format ([KeyHash] -> Builder) ([KeyHash] -> Builder)
"GenesisKeyHashes: " 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 a. Set a -> [a]
Set.toList Set KeyHash
m)

instance Monad m => ToJSON m GenesisKeyHashes where
  toJSON :: GenesisKeyHashes -> m JSValue
toJSON (GenesisKeyHashes Set KeyHash
stks) =
    forall (m :: * -> *) a. ToJSON m a => a -> m JSValue
toJSON forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> [(a, b)]
zip (forall a. Set a -> [a]
Set.toList Set KeyHash
stks) (forall a. a -> [a]
repeat (Word16
1 :: Word16))

instance MonadError SchemaError m => FromJSON m GenesisKeyHashes where
  fromJSON :: JSValue -> m GenesisKeyHashes
fromJSON =
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Set KeyHash -> GenesisKeyHashes
GenesisKeyHashes forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall k a. Map k a -> Set k
M.keysSet) forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall (m :: * -> *) a. FromJSON m a => JSValue -> m a
fromJSON @m @(Map KeyHash Word16)

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

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

instance EncCBOR GenesisKeyHashes where
  encCBOR :: GenesisKeyHashes -> Encoding
encCBOR (GenesisKeyHashes Set KeyHash
gkh) = Word -> Encoding
encodeListLen Word
1 forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR @(Set KeyHash) Set KeyHash
gkh

instance DecCBOR GenesisKeyHashes where
  decCBOR :: forall s. Decoder s GenesisKeyHashes
decCBOR = do
    forall s. Text -> Int -> Decoder s ()
enforceSize Text
"GenesisKeyHashes" Int
1
    Set KeyHash -> GenesisKeyHashes
GenesisKeyHashes forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. DecCBOR a => Decoder s a
decCBOR @(Set KeyHash)