{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

module Cardano.Chain.UTxO.UTxOConfiguration
  ( UTxOConfiguration (..),
    defaultUTxOConfiguration,
    mkUTxOConfiguration,
  )
where

import Cardano.Binary
  ( FromCBOR (..),
    ToCBOR (..),
    encodeListLen,
    enforceSize,
  )
import Cardano.Chain.Common.Address (Address)
import Cardano.Chain.Common.Compact (CompactAddress, toCompactAddress)
import Cardano.Prelude
import qualified Data.Set as Set
import NoThunks.Class (NoThunks (..))

-- | Additional configuration for ledger validation.
data UTxOConfiguration = UTxOConfiguration
  { -- | Set of source address which are asset-locked. Transactions which
    -- use these addresses as transaction inputs will be deemed invalid.
    UTxOConfiguration -> Set CompactAddress
tcAssetLockedSrcAddrs :: !(Set CompactAddress)
  }
  deriving (UTxOConfiguration -> UTxOConfiguration -> Bool
(UTxOConfiguration -> UTxOConfiguration -> Bool)
-> (UTxOConfiguration -> UTxOConfiguration -> Bool)
-> Eq UTxOConfiguration
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UTxOConfiguration -> UTxOConfiguration -> Bool
$c/= :: UTxOConfiguration -> UTxOConfiguration -> Bool
== :: UTxOConfiguration -> UTxOConfiguration -> Bool
$c== :: UTxOConfiguration -> UTxOConfiguration -> Bool
Eq, Int -> UTxOConfiguration -> ShowS
[UTxOConfiguration] -> ShowS
UTxOConfiguration -> String
(Int -> UTxOConfiguration -> ShowS)
-> (UTxOConfiguration -> String)
-> ([UTxOConfiguration] -> ShowS)
-> Show UTxOConfiguration
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UTxOConfiguration] -> ShowS
$cshowList :: [UTxOConfiguration] -> ShowS
show :: UTxOConfiguration -> String
$cshow :: UTxOConfiguration -> String
showsPrec :: Int -> UTxOConfiguration -> ShowS
$cshowsPrec :: Int -> UTxOConfiguration -> ShowS
Show, (forall x. UTxOConfiguration -> Rep UTxOConfiguration x)
-> (forall x. Rep UTxOConfiguration x -> UTxOConfiguration)
-> Generic UTxOConfiguration
forall x. Rep UTxOConfiguration x -> UTxOConfiguration
forall x. UTxOConfiguration -> Rep UTxOConfiguration x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep UTxOConfiguration x -> UTxOConfiguration
$cfrom :: forall x. UTxOConfiguration -> Rep UTxOConfiguration x
Generic, Context -> UTxOConfiguration -> IO (Maybe ThunkInfo)
Proxy UTxOConfiguration -> String
(Context -> UTxOConfiguration -> IO (Maybe ThunkInfo))
-> (Context -> UTxOConfiguration -> IO (Maybe ThunkInfo))
-> (Proxy UTxOConfiguration -> String)
-> NoThunks UTxOConfiguration
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy UTxOConfiguration -> String
$cshowTypeOf :: Proxy UTxOConfiguration -> String
wNoThunks :: Context -> UTxOConfiguration -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> UTxOConfiguration -> IO (Maybe ThunkInfo)
noThunks :: Context -> UTxOConfiguration -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> UTxOConfiguration -> IO (Maybe ThunkInfo)
NoThunks)

instance ToCBOR UTxOConfiguration where
  toCBOR :: UTxOConfiguration -> Encoding
toCBOR (UTxOConfiguration Set CompactAddress
tcAssetLockedSrcAddrs_) =
    Word -> Encoding
encodeListLen Word
1
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Set CompactAddress -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR @(Set CompactAddress) Set CompactAddress
tcAssetLockedSrcAddrs_

instance FromCBOR UTxOConfiguration where
  fromCBOR :: Decoder s UTxOConfiguration
fromCBOR = do
    Text -> Int -> Decoder s ()
forall s. Text -> Int -> Decoder s ()
enforceSize Text
"UTxOConfiguration" Int
1
    Set CompactAddress -> UTxOConfiguration
UTxOConfiguration (Set CompactAddress -> UTxOConfiguration)
-> Decoder s (Set CompactAddress) -> Decoder s UTxOConfiguration
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s.
FromCBOR (Set CompactAddress) =>
Decoder s (Set CompactAddress)
forall a s. FromCBOR a => Decoder s a
fromCBOR @(Set CompactAddress)

defaultUTxOConfiguration :: UTxOConfiguration
defaultUTxOConfiguration :: UTxOConfiguration
defaultUTxOConfiguration =
  UTxOConfiguration :: Set CompactAddress -> UTxOConfiguration
UTxOConfiguration
    { tcAssetLockedSrcAddrs :: Set CompactAddress
tcAssetLockedSrcAddrs = Set CompactAddress
forall a. Set a
Set.empty
    }

mkUTxOConfiguration :: [Address] -> UTxOConfiguration
mkUTxOConfiguration :: [Address] -> UTxOConfiguration
mkUTxOConfiguration [Address]
lockedSrcAddrs =
  UTxOConfiguration :: Set CompactAddress -> UTxOConfiguration
UTxOConfiguration
    { tcAssetLockedSrcAddrs :: Set CompactAddress
tcAssetLockedSrcAddrs = [CompactAddress] -> Set CompactAddress
forall a. Ord a => [a] -> Set a
Set.fromList ((Address -> CompactAddress) -> [Address] -> [CompactAddress]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map Address -> CompactAddress
toCompactAddress [Address]
lockedSrcAddrs)
    }