{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Cardano.Ledger.Babbage.UTxO (
getBabbageSupplementalDataHashes,
getBabbageSpendingDatum,
getBabbageScriptsProvided,
getReferenceScripts,
getReferenceScriptsNonDistinct,
) where
import Cardano.Ledger.Alonzo.TxWits (unTxDats)
import Cardano.Ledger.Alonzo.UTxO (
AlonzoEraUTxO (..),
AlonzoScriptsNeeded,
getAlonzoScriptsHashesNeeded,
getAlonzoScriptsNeeded,
getAlonzoWitsVKeyNeeded,
)
import Cardano.Ledger.Babbage.Core
import Cardano.Ledger.Babbage.Era (BabbageEra)
import Cardano.Ledger.BaseTypes (StrictMaybe (..), strictMaybeToMaybe)
import Cardano.Ledger.Binary (sizedValue)
import Cardano.Ledger.Crypto
import Cardano.Ledger.Mary.UTxO (getConsumedMaryValue, getProducedMaryValue)
import Cardano.Ledger.Plutus.Data (Data)
import Cardano.Ledger.Shelley.UTxO (getShelleyMinFeeTxUtxo)
import Cardano.Ledger.TxIn (TxIn)
import Cardano.Ledger.UTxO (EraUTxO (..), ScriptsProvided (..), UTxO (..))
import Control.Applicative
import Control.SetAlgebra (eval, (◁))
import Data.Foldable (toList)
import qualified Data.Map.Strict as Map
import Data.Set (Set)
import qualified Data.Set as Set
import Lens.Micro
instance Crypto c => EraUTxO (BabbageEra c) where
{-# SPECIALIZE instance EraUTxO (BabbageEra StandardCrypto) #-}
type ScriptsNeeded (BabbageEra c) = AlonzoScriptsNeeded (BabbageEra c)
getConsumedValue :: PParams (BabbageEra c)
-> (Credential 'Staking (EraCrypto (BabbageEra c)) -> Maybe Coin)
-> (Credential 'DRepRole (EraCrypto (BabbageEra c)) -> Maybe Coin)
-> UTxO (BabbageEra c)
-> TxBody (BabbageEra c)
-> Value (BabbageEra c)
getConsumedValue = forall era.
(MaryEraTxBody era, Value era ~ MaryValue (EraCrypto era)) =>
PParams era
-> (Credential 'Staking (EraCrypto era) -> Maybe Coin)
-> (Credential 'DRepRole (EraCrypto era) -> Maybe Coin)
-> UTxO era
-> TxBody era
-> MaryValue (EraCrypto era)
getConsumedMaryValue
getProducedValue :: PParams (BabbageEra c)
-> (KeyHash 'StakePool (EraCrypto (BabbageEra c)) -> Bool)
-> TxBody (BabbageEra c)
-> Value (BabbageEra c)
getProducedValue = forall era.
(MaryEraTxBody era, Value era ~ MaryValue (EraCrypto era)) =>
PParams era
-> (KeyHash 'StakePool (EraCrypto era) -> Bool)
-> TxBody era
-> MaryValue (EraCrypto era)
getProducedMaryValue
getScriptsProvided :: UTxO (BabbageEra c)
-> Tx (BabbageEra c) -> ScriptsProvided (BabbageEra c)
getScriptsProvided = forall era.
(EraTx era, BabbageEraTxBody era) =>
UTxO era -> Tx era -> ScriptsProvided era
getBabbageScriptsProvided
getScriptsNeeded :: UTxO (BabbageEra c)
-> TxBody (BabbageEra c) -> ScriptsNeeded (BabbageEra c)
getScriptsNeeded = forall era.
(MaryEraTxBody era, AlonzoEraScript era) =>
UTxO era -> TxBody era -> AlonzoScriptsNeeded era
getAlonzoScriptsNeeded
{-# INLINEABLE getScriptsNeeded #-}
getScriptsHashesNeeded :: ScriptsNeeded (BabbageEra c)
-> Set (ScriptHash (EraCrypto (BabbageEra c)))
getScriptsHashesNeeded = forall era.
AlonzoScriptsNeeded era -> Set (ScriptHash (EraCrypto era))
getAlonzoScriptsHashesNeeded
getWitsVKeyNeeded :: CertState (BabbageEra c)
-> UTxO (BabbageEra c)
-> TxBody (BabbageEra c)
-> Set (KeyHash 'Witness (EraCrypto (BabbageEra c)))
getWitsVKeyNeeded = forall era.
(EraTx era, AlonzoEraTxBody era, ShelleyEraTxBody era) =>
CertState era
-> UTxO era -> TxBody era -> Set (KeyHash 'Witness (EraCrypto era))
getAlonzoWitsVKeyNeeded
getMinFeeTxUtxo :: PParams (BabbageEra c)
-> Tx (BabbageEra c) -> UTxO (BabbageEra c) -> Coin
getMinFeeTxUtxo PParams (BabbageEra c)
pp Tx (BabbageEra c)
tx UTxO (BabbageEra c)
_ = forall era. EraTx era => PParams era -> Tx era -> Coin
getShelleyMinFeeTxUtxo PParams (BabbageEra c)
pp Tx (BabbageEra c)
tx
instance Crypto c => AlonzoEraUTxO (BabbageEra c) where
getSupplementalDataHashes :: UTxO (BabbageEra c)
-> TxBody (BabbageEra c)
-> Set (DataHash (EraCrypto (BabbageEra c)))
getSupplementalDataHashes = forall era.
BabbageEraTxBody era =>
UTxO era -> TxBody era -> Set (DataHash (EraCrypto era))
getBabbageSupplementalDataHashes
getSpendingDatum :: UTxO (BabbageEra c)
-> Tx (BabbageEra c)
-> PlutusPurpose AsItem (BabbageEra c)
-> Maybe (Data (BabbageEra c))
getSpendingDatum = forall era.
(AlonzoEraTx era, BabbageEraTxOut era) =>
UTxO era -> Tx era -> PlutusPurpose AsItem era -> Maybe (Data era)
getBabbageSpendingDatum
getBabbageSupplementalDataHashes ::
BabbageEraTxBody era =>
UTxO era ->
TxBody era ->
Set.Set (DataHash (EraCrypto era))
getBabbageSupplementalDataHashes :: forall era.
BabbageEraTxBody era =>
UTxO era -> TxBody era -> Set (DataHash (EraCrypto era))
getBabbageSupplementalDataHashes (UTxO Map (TxIn (EraCrypto era)) (TxOut era)
utxo) TxBody era
txBody =
forall a. Ord a => [a] -> Set a
Set.fromList [DataHash (EraCrypto era)
dh | TxOut era
txOut <- [TxOut era]
outs, SJust DataHash (EraCrypto era)
dh <- [TxOut era
txOut forall s a. s -> Getting a s a -> a
^. forall era.
AlonzoEraTxOut era =>
Lens' (TxOut era) (StrictMaybe (DataHash (EraCrypto era)))
dataHashTxOutL]]
where
newOuts :: [TxOut era]
newOuts = forall a b. (a -> b) -> [a] -> [b]
map forall a. Sized a -> a
sizedValue forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> [a]
toList forall a b. (a -> b) -> a -> b
$ TxBody era
txBody forall s a. s -> Getting a s a -> a
^. forall era.
BabbageEraTxBody era =>
SimpleGetter (TxBody era) (StrictSeq (Sized (TxOut era)))
allSizedOutputsTxBodyF
referencedOuts :: [TxOut era]
referencedOuts = forall k a. Map k a -> [a]
Map.elems forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Map (TxIn (EraCrypto era)) (TxOut era)
utxo (TxBody era
txBody forall s a. s -> Getting a s a -> a
^. forall era.
BabbageEraTxBody era =>
Lens' (TxBody era) (Set (TxIn (EraCrypto era)))
referenceInputsTxBodyL)
outs :: [TxOut era]
outs = [TxOut era]
newOuts forall a. Semigroup a => a -> a -> a
<> [TxOut era]
referencedOuts
getBabbageSpendingDatum ::
( AlonzoEraTx era
, BabbageEraTxOut era
) =>
UTxO era ->
Tx era ->
PlutusPurpose AsItem era ->
Maybe (Data era)
getBabbageSpendingDatum :: forall era.
(AlonzoEraTx era, BabbageEraTxOut era) =>
UTxO era -> Tx era -> PlutusPurpose AsItem era -> Maybe (Data era)
getBabbageSpendingDatum (UTxO Map (TxIn (EraCrypto era)) (TxOut era)
utxo) Tx era
tx PlutusPurpose AsItem era
sp = do
AsItem TxIn (EraCrypto era)
txIn <- forall era (f :: * -> * -> *).
AlonzoEraScript era =>
PlutusPurpose f era -> Maybe (f Word32 (TxIn (EraCrypto era)))
toSpendingPurpose PlutusPurpose AsItem era
sp
TxOut era
txOut <- forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup TxIn (EraCrypto era)
txIn Map (TxIn (EraCrypto era)) (TxOut era)
utxo
let txOutDataFromWits :: Maybe (Data era)
txOutDataFromWits = do
DataHash (EraCrypto era)
dataHash <- forall a. StrictMaybe a -> Maybe a
strictMaybeToMaybe (TxOut era
txOut forall s a. s -> Getting a s a -> a
^. forall era.
AlonzoEraTxOut era =>
Lens' (TxOut era) (StrictMaybe (DataHash (EraCrypto era)))
dataHashTxOutL)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup DataHash (EraCrypto era)
dataHash (forall era. TxDats era -> Map (DataHash (EraCrypto era)) (Data era)
unTxDats (Tx era
tx forall s a. s -> Getting a s a -> a
^. forall era. EraTx era => Lens' (Tx era) (TxWits era)
witsTxL forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall era. AlonzoEraTxWits era => Lens' (TxWits era) (TxDats era)
datsTxWitsL))
forall a. StrictMaybe a -> Maybe a
strictMaybeToMaybe (TxOut era
txOut forall s a. s -> Getting a s a -> a
^. forall era.
BabbageEraTxOut era =>
Lens' (TxOut era) (StrictMaybe (Data era))
dataTxOutL) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe (Data era)
txOutDataFromWits
getBabbageScriptsProvided ::
( EraTx era
, BabbageEraTxBody era
) =>
UTxO era ->
Tx era ->
ScriptsProvided era
getBabbageScriptsProvided :: forall era.
(EraTx era, BabbageEraTxBody era) =>
UTxO era -> Tx era -> ScriptsProvided era
getBabbageScriptsProvided UTxO era
utxo Tx era
tx = forall era.
Map (ScriptHash (EraCrypto era)) (Script era)
-> ScriptsProvided era
ScriptsProvided Map (ScriptHash (EraCrypto era)) (Script era)
ans
where
txBody :: TxBody era
txBody = Tx era
tx forall s a. s -> Getting a s a -> a
^. forall era. EraTx era => Lens' (Tx era) (TxBody era)
bodyTxL
ins :: Set (TxIn (EraCrypto era))
ins = (TxBody era
txBody forall s a. s -> Getting a s a -> a
^. forall era.
BabbageEraTxBody era =>
Lens' (TxBody era) (Set (TxIn (EraCrypto era)))
referenceInputsTxBodyL) forall a. Ord a => Set a -> Set a -> Set a
`Set.union` (TxBody era
txBody forall s a. s -> Getting a s a -> a
^. forall era.
EraTxBody era =>
Lens' (TxBody era) (Set (TxIn (EraCrypto era)))
inputsTxBodyL)
ans :: Map (ScriptHash (EraCrypto era)) (Script era)
ans = forall era.
BabbageEraTxOut era =>
UTxO era
-> Set (TxIn (EraCrypto era))
-> Map (ScriptHash (EraCrypto era)) (Script era)
getReferenceScripts UTxO era
utxo Set (TxIn (EraCrypto era))
ins forall k a. Ord k => Map k a -> Map k a -> Map k a
`Map.union` (Tx era
tx forall s a. s -> Getting a s a -> a
^. forall era. EraTx era => Lens' (Tx era) (TxWits era)
witsTxL forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall era.
EraTxWits era =>
Lens' (TxWits era) (Map (ScriptHash (EraCrypto era)) (Script era))
scriptTxWitsL)
getReferenceScripts ::
BabbageEraTxOut era =>
UTxO era ->
Set (TxIn (EraCrypto era)) ->
Map.Map (ScriptHash (EraCrypto era)) (Script era)
getReferenceScripts :: forall era.
BabbageEraTxOut era =>
UTxO era
-> Set (TxIn (EraCrypto era))
-> Map (ScriptHash (EraCrypto era)) (Script era)
getReferenceScripts UTxO era
utxo Set (TxIn (EraCrypto era))
ins = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList (forall era.
BabbageEraTxOut era =>
UTxO era
-> Set (TxIn (EraCrypto era))
-> [(ScriptHash (EraCrypto era), Script era)]
getReferenceScriptsNonDistinct UTxO era
utxo Set (TxIn (EraCrypto era))
ins)
getReferenceScriptsNonDistinct ::
BabbageEraTxOut era =>
UTxO era ->
Set (TxIn (EraCrypto era)) ->
[(ScriptHash (EraCrypto era), Script era)]
getReferenceScriptsNonDistinct :: forall era.
BabbageEraTxOut era =>
UTxO era
-> Set (TxIn (EraCrypto era))
-> [(ScriptHash (EraCrypto era), Script era)]
getReferenceScriptsNonDistinct (UTxO Map (TxIn (EraCrypto era)) (TxOut era)
mp) Set (TxIn (EraCrypto era))
inputs =
[ (forall era.
EraScript era =>
Script era -> ScriptHash (EraCrypto era)
hashScript Script era
script, Script era
script)
| TxOut era
txOut <- forall k a. Map k a -> [a]
Map.elems (forall s t. Embed s t => Exp t -> s
eval (Set (TxIn (EraCrypto era))
inputs forall k s1 s2 (f :: * -> * -> *) v.
(Ord k, HasExp s1 (Sett k ()), HasExp s2 (f k v)) =>
s1 -> s2 -> Exp (f k v)
◁ Map (TxIn (EraCrypto era)) (TxOut era)
mp))
, SJust Script era
script <- [TxOut era
txOut forall s a. s -> Getting a s a -> a
^. forall era.
BabbageEraTxOut era =>
Lens' (TxOut era) (StrictMaybe (Script era))
referenceScriptTxOutL]
]