Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit changes #445

Merged
merged 62 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
579ecbe
tests pass with new mockchain return type
Jul 4, 2024
fc36439
more log levels
Jul 4, 2024
38c0718
logging removal of unusable balancing utxos
Jul 4, 2024
b525aeb
improving logging in balancing
mmontin Jul 8, 2024
98b5e81
new log version with dedicated constructors
mmontin Jul 10, 2024
47f54a5
changing item
mmontin Jul 10, 2024
2ff0e46
integrating comments, adding comments and more readable bullets
mmontin Jul 10, 2024
73d8ab2
fixing the bug where collateral inputs were not resolved
mmontin Jul 10, 2024
a94db0a
CHANGELOG.md
mmontin Jul 10, 2024
580b35c
merging main in this
Jul 28, 2024
f2cfce6
integrating review comments
Jul 28, 2024
8764a1a
typo
Jul 28, 2024
798c8c3
removing useless instances
Jul 28, 2024
492d057
wip
mmontin Jul 7, 2024
817c777
reverting balancingspec
mmontin Jul 10, 2024
c59f14c
starting to consume scripts in balancing spec, to be continued
mmontin Jul 10, 2024
96b3250
reworking empty collaterals
mmontin Jul 11, 2024
34aa2af
2 first test groups passé
mmontin Jul 11, 2024
291df89
all tests fixed
mmontin Jul 11, 2024
5f696fd
doc
mmontin Jul 11, 2024
c8cf373
updating doc
mmontin Jul 11, 2024
73c209f
logging of unused collateral option
mmontin Jul 11, 2024
2275916
post-rebase small fixes
Jul 28, 2024
f44b98e
bye bye Ledger.TxOut
Jul 28, 2024
6ffc16e
Merge branch 'main' into mm/logger
Jul 29, 2024
089191f
Merge branch 'mm/logger' into mm/collateral-when-no-script
Jul 29, 2024
47e99e6
Merge branch 'mm/collateral-when-no-script' into mm/txout
Jul 29, 2024
14da58f
update capi
mmontin Jul 31, 2024
1ccb4b1
Proper script hash computation for all plutus versions
mmontin Jul 31, 2024
5114f60
helpers and qol changes
mmontin Jul 31, 2024
5c6adbe
MockChainSt has its own module now
mmontin Jul 31, 2024
db84394
Support for hashed datums in reference inputs
mmontin Jul 31, 2024
72e2820
withdrawal support
mmontin Jul 31, 2024
cc595b1
fixing balancing bug
mmontin Jul 31, 2024
63bb587
small post-rebase changes
mmontin Jul 31, 2024
97d1460
showbsspec finally gone
mmontin Jul 31, 2024
bdf3676
Recreating an index to pass to the new fee estimate function
mmontin Jul 31, 2024
2e6897d
merging logging into this
mmontin Aug 1, 2024
57fec8c
merging collaterals into this
mmontin Aug 1, 2024
cd8e5de
merging txout into this
mmontin Aug 1, 2024
5e465f4
merging bump cardano api into this
mmontin Aug 6, 2024
13271b9
post merge fix
mmontin Aug 6, 2024
395cc53
no tests built for dependencies, relying on cne directly
mmontin Aug 8, 2024
9d4be2d
CHANGELOG.md
mmontin Aug 8, 2024
1555195
merging update to new version of capi
mmontin Aug 8, 2024
4854587
relying on the fork for translation functions
mmontin Aug 8, 2024
f33029e
Merge branch 'mm/bump-capi' into mm/djed-audit
mmontin Aug 8, 2024
55c00e2
credential and staking credential of a wallet
mmontin Aug 29, 2024
785be72
moving time from either the lower or upper bound of current slot
mmontin Aug 29, 2024
cdc788b
depending on cne
mmontin Aug 29, 2024
c1d6f45
merging main into this
mmontin Aug 29, 2024
82f2b7f
merging main into this
mmontin Aug 29, 2024
ee333f6
post merge mini fix
mmontin Aug 29, 2024
70a0bee
merging collaterals into this
mmontin Aug 29, 2024
1554e51
merging txout into this
mmontin Aug 29, 2024
1654dbb
Merge branch 'main' into mm/collateral-when-no-script
mmontin Aug 30, 2024
bb45ed1
Merge branch 'mm/collateral-when-no-script' into mm/txout
mmontin Aug 30, 2024
3e1b3a1
Merge branch 'mm/txout' into mm/djed-audit
mmontin Aug 30, 2024
f08e504
reworking withdrawals for proper maps
mmontin Aug 30, 2024
b15fc69
hpack
mmontin Aug 30, 2024
bbe4122
merging
mmontin Sep 5, 2024
943d937
post review changes
mmontin Sep 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- `validatorToTypedValidatorV2`
- `walletPKHashToWallet` that retrives a wallet from a pkh. It used to be
present but somehow disapeared.
- It is now possible to reference an output which has a hashed datum.
- `txSkelHashedData` the gives all the datum hashes in inputs and reference inputs.

### Removed

Expand All @@ -29,6 +31,11 @@
`txSkelSomeRedeemerAndReferenceScript`,
`txSkelEmptyRedeemerAndReferenceScript`.
- `mkProposingScript` changed to `mkScript`
- `withDatumHashed` changed to `withUnresolvedDatumHash`
- `paysScriptDatumHashed` changed to `paysScriptUnresolvedDatumHash`
- `txSkelInputData` changed to `txSkelConsumedData`
- Pretty printing of hashed datum now includes the hash (and not only the
resolved datum).

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions cooked-validators.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ library
, cardano-api
, cardano-crypto
, cardano-data
, cardano-ledger-alonzo
, cardano-ledger-conway
, cardano-ledger-core
, cardano-ledger-shelley
Expand Down Expand Up @@ -203,6 +204,7 @@ test-suite spec
, cardano-api
, cardano-crypto
, cardano-data
, cardano-ledger-alonzo
, cardano-ledger-conway
, cardano-ledger-core
, cardano-ledger-shelley
Expand Down
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies:
- cardano-api
- cardano-crypto
- cardano-data
- cardano-ledger-alonzo
- cardano-ledger-core
- cardano-ledger-shelley
- cardano-ledger-conway
Expand Down
2 changes: 1 addition & 1 deletion src/Cooked/MockChain/Balancing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ estimateTxSkelFee :: (MonadBlockChainBalancing m) => TxSkel -> Fee -> Collateral
estimateTxSkelFee skel fee collateralIns returnCollateralWallet = do
-- We retrieve the necessary data to generate the transaction body
params <- getParams
managedData <- txSkelInputData skel
managedData <- txSkelHashedData skel
mmontin marked this conversation as resolved.
Show resolved Hide resolved
managedTxOuts <- lookupUtxosPl $ txSkelKnownTxOutRefs skel <> Set.toList collateralIns
managedValidators <- txSkelInputValidators skel
-- We generate the transaction body content, handling errors in the meantime
Expand Down
55 changes: 34 additions & 21 deletions src/Cooked/MockChain/BlockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ module Cooked.MockChain.BlockChain
txSkelReferenceInputUtxos,
txSkelInputValidators,
txSkelInputValue,
txSkelInputData,
txSkelHashedData,
txSkelConsumedData,
lookupUtxos,
lookupUtxosPl,
validateTxSkel',
Expand Down Expand Up @@ -340,28 +341,40 @@ lookupUtxos =
txSkelInputValue :: (MonadBlockChainBalancing m) => TxSkel -> m Api.Value
txSkelInputValue = (foldMap (Api.txOutValue . txOutV2FromLedger) <$>) . txSkelInputUtxos

-- | Look up the data on UTxOs the transaction consumes.
txSkelInputData :: (MonadBlockChainBalancing m) => TxSkel -> m (Map Api.DatumHash Api.Datum)
txSkelInputData skel = do
mDatumHashes <-
mapMaybe
( \output ->
case output ^. outputDatumL of
Api.NoOutputDatum -> Nothing
Api.OutputDatum datum -> Just $ Script.datumHash datum
Api.OutputDatumHash dHash -> Just dHash
)
. Map.elems
<$> txSkelInputUtxosPl skel
Map.fromList
<$> mapM
( \dHash ->
-- | Looks up and resolves the hashed datums on UTxOs the transaction consumes
-- or references, which will be needed by the transaction body.
txSkelHashedData :: (MonadBlockChainBalancing m) => TxSkel -> m (Map Api.DatumHash Api.Datum)
mmontin marked this conversation as resolved.
Show resolved Hide resolved
txSkelHashedData skel = do
let outputToDatumHashM output = case output ^. outputDatumL of
Api.OutputDatumHash dHash -> Just dHash
_ -> Nothing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it something useful elsewhere that we should put on top level?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something that could be reused was already existing indeed and I now used it: isOutputWithDatumHash. Thanks!

(Map.elems -> inputTxOuts) <- txSkelInputUtxosPl skel
(Map.elems -> refInputTxOuts) <- txSkelReferenceInputUtxosPl skel
foldM
( \dat dHash ->
maybeErrM
(MCEUnknownDatum "txSkelHashedData: Transaction input with unknown datum hash" dHash)
(\rDat -> Map.insert dHash rDat dat)
(datumFromHash dHash)
)
Map.empty
(mapMaybe outputToDatumHashM $ inputTxOuts <> refInputTxOuts)

-- | Looks us the data on UTxOs the transaction consumes. This corresponds to
mmontin marked this conversation as resolved.
Show resolved Hide resolved
-- the keys of what should be removed from the stored datums in our mockchain.
-- There can be duplicates, which is expected.
txSkelConsumedData :: (MonadBlockChainBalancing m) => TxSkel -> m [Api.DatumHash]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the names confusing. This ends with Data and returns a list of hashes, the previous one ends with HashedData and returns a map that resolve to the actual data.

Also I find "consumed" a bit misleading. This is the same as "spent"? This means the datum hashes of every piece of datum found in the inputs, doesn't it? Not a subset.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the name to txSkelInputDataAsHashes

txSkelConsumedData skel = do
let outputToDatumHashM output = case output ^. outputDatumL of
Api.OutputDatumHash dHash ->
maybeErrM
(MCEUnknownDatum "txSkelInputData: Transaction input with un-resolvable datum hash" dHash)
(dHash,)
(MCEUnknownDatum "txSkelConsumedData: Transaction input with unknown datum hash" dHash)
(Just . const dHash)
(datumFromHash dHash)
)
mDatumHashes
Api.OutputDatum datum -> return $ Just $ Script.datumHash datum
Api.NoOutputDatum -> return Nothing
(Map.elems -> inputTxOuts) <- txSkelInputUtxosPl skel
catMaybes <$> mapM outputToDatumHashM inputTxOuts

-- ** Slot and Time Management

Expand Down
9 changes: 5 additions & 4 deletions src/Cooked/MockChain/Direct.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,16 @@ instance (Monad m) => MonadBlockChain (MockChainT m) where
-- We retrieve data that will be used in the transaction generation process:
-- datums, validators and various kinds of inputs. This idea is to provide a
-- rich-enough context for the transaction generation to succeed.
insData <- txSkelInputData skel
hashedData <- txSkelHashedData skel
insData <- txSkelConsumedData skel
mmontin marked this conversation as resolved.
Show resolved Hide resolved
insValidators <- txSkelInputValidators skel
insMap <- txSkelInputUtxosPl skel
refInsMap <- txSkelReferenceInputUtxosPl skel
collateralInsMap <- lookupUtxosPl $ Set.toList collateralIns
-- We attempt to generate the transaction associated with the balanced
-- skeleton and the retrieved data. This is an internal generation, there is
-- no validation involved yet.
cardanoTx <- case generateTx fee returnCollateralWallet collateralIns newParams insData (insMap <> refInsMap <> collateralInsMap) insValidators skel of
cardanoTx <- case generateTx fee returnCollateralWallet collateralIns newParams hashedData (insMap <> refInsMap <> collateralInsMap) insValidators skel of
Left err -> throwError . MCEGenerationError $ err
-- We apply post-generation modification when applicable
Right tx -> return $ Ledger.CardanoEmulatorEraTx $ applyRawModOnBalancedTx (txOptUnsafeModTx . txSkelOpts $ skelUnbal) tx
Expand Down Expand Up @@ -210,8 +211,8 @@ instance (Monad m) => MonadBlockChain (MockChainT m) where
-- Otherwise, we update known validators and datums.
Nothing ->
modify'
( removeDatums (Map.keys insData)
. addDatums (Map.toList $ txSkelDataInOutputs skel)
( removeDatums insData
. addDatums (txSkelDataInOutputs skel)
. addValidators (txSkelValidatorsInOutputs skel <> txSkelReferenceScripts skel)
)
-- We apply a change of slot when requested in the options
Expand Down
73 changes: 57 additions & 16 deletions src/Cooked/MockChain/GenerateTx/Body.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
module Cooked.MockChain.GenerateTx.Body where

import Cardano.Api qualified as Cardano
import Cardano.Api.Shelley qualified as Cardano
import Cardano.Ledger.Alonzo.Tx qualified as Alonzo
import Cardano.Ledger.Alonzo.TxBody qualified as Alonzo
import Cardano.Ledger.Alonzo.TxWits qualified as Alonzo
import Cardano.Ledger.Conway.PParams qualified as Conway
import Cardano.Ledger.Plutus qualified as Cardano
import Cardano.Node.Emulator.Internal.Node qualified as Emulator
import Control.Lens qualified as Lens
import Control.Monad
import Control.Monad.Reader
import Cooked.MockChain.GenerateTx.Collateral qualified as Collateral
Expand All @@ -12,10 +19,11 @@ import Cooked.MockChain.GenerateTx.Output qualified as Output
import Cooked.MockChain.GenerateTx.Proposal qualified as Proposal
import Cooked.Skeleton
import Cooked.Wallet
import Data.Bifunctor
import Data.Either.Combinators
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Set (Set)
import Data.Set qualified as Set
import Ledger.Address qualified as Ledger
import Ledger.Tx qualified as Ledger
import Ledger.Tx.CardanoAPI qualified as Ledger
Expand Down Expand Up @@ -86,8 +94,8 @@ txSkelToBodyContent skel@TxSkel {..} | txSkelReferenceInputs <- txSkelReferenceT
let txMetadata = Cardano.TxMetadataNone -- That's what plutus-apps does as well
txAuxScripts = Cardano.TxAuxScriptsNone -- That's what plutus-apps does as well
txWithdrawals = Cardano.TxWithdrawalsNone -- That's what plutus-apps does as well
txCertificates = Cardano.TxCertificatesNone -- That's what plutus-apps does as well
txUpdateProposal = Cardano.TxUpdateProposalNone -- That's what plutus-apps does as well
txCertificates = Cardano.TxCertificatesNone -- That's what plutus-apps does as well
mmontin marked this conversation as resolved.
Show resolved Hide resolved
txScriptValidity = Cardano.TxScriptValidityNone -- That's what plutus-apps does as well
txVotingProcedures = Nothing -- TODO, same as above
return Cardano.TxBodyContent {..}
Expand All @@ -97,17 +105,50 @@ txSkelToBodyContent skel@TxSkel {..} | txSkelReferenceInputs <- txSkelReferenceT
txSkelToCardanoTx :: TxSkel -> BodyGen (Cardano.Tx Cardano.ConwayEra)
txSkelToCardanoTx txSkel = do
txBodyContent <- txSkelToBodyContent txSkel
cardanoTxUnsigned <-
lift $
bimap
(TxBodyError "generateTx: ")
(`Cardano.Tx` [])
(Cardano.createAndValidateTransactionBody Cardano.ShelleyBasedEraConway txBodyContent)
foldM
( \tx wal ->
case Ledger.addCardanoTxWitness (Ledger.toWitness $ Ledger.PaymentPrivateKey $ walletSK wal) (Ledger.CardanoTx tx Cardano.ShelleyBasedEraConway) of
Ledger.CardanoTx tx' Cardano.ShelleyBasedEraConway -> return tx'
_ -> throwOnString "txSkelToCardanoTx: Wrong output era"
)
cardanoTxUnsigned
(txSkelSigners txSkel)

-- We create the associated Shelley TxBody
txBody@(Cardano.ShelleyTxBody a body c dats e f) <-
lift $ mapLeft (TxBodyError "generateTx :") $ Cardano.createAndValidateTransactionBody Cardano.ShelleyBasedEraConway txBodyContent

-- There is a chance that the body is in need of additional data. This happens
-- when the set of reference inputs contains hashed datums that will need to
-- be resolved during phase 2 validation. All that follows until the
-- definition of "txBody'" aims at doing just that. In the process, we have to
-- reconstruct the body with the new data and the associated hash. Hopefully,
-- in the future, cardano-api provides a way to add those data in the body
-- directly without requiring this methods, which somewhat feels like a hack.
mmontin marked this conversation as resolved.
Show resolved Hide resolved
mData <- asks managedData
mTxOut <- asks managedTxOuts
refIns <- forM (txSkelReferenceTxOutRefs txSkel) $ \oRef ->
throwOnLookup ("txSkelToCardanoTx: Unable to resolve TxOutRef " <> show oRef) oRef mTxOut
let datumHashes = [hash | (Api.TxOut _ _ (Api.OutputDatumHash hash) _) <- refIns]
additionalData <- forM datumHashes $ \dHash ->
throwOnLookup ("txSkelToCardanoTx: Unable to resolve datum hash " <> show dHash) dHash mData
let additionalDataMap = Map.fromList [(Cardano.hashData dat, dat) | Api.Datum (Cardano.Data . Api.toData -> dat) <- additionalData]
toLangDepViewParam <- asks (Conway.getLanguageView . Cardano.unLedgerProtocolParameters . Emulator.ledgerProtocolParameters . params)
let txDats' = Alonzo.TxDats additionalDataMap
(era, datums, redeemers) = case dats of
Cardano.TxBodyNoScriptData -> (Cardano.AlonzoEraOnwardsConway, txDats', Alonzo.Redeemers Map.empty)
Cardano.TxBodyScriptData era' txDats reds -> (era', txDats <> txDats', reds)
witnesses = Cardano.collectTxBodyScriptWitnesses Cardano.ShelleyBasedEraConway txBodyContent
languages = [toCardanoLanguage v | (_, Cardano.AnyScriptWitness (Cardano.PlutusScriptWitness _ v _ _ _ _)) <- witnesses]
scriptIntegrityHash =
Cardano.alonzoEraOnwardsConstraints era $
Alonzo.hashScriptIntegrity (Set.fromList $ toLangDepViewParam <$> languages) redeemers datums
body' = body Lens.& Alonzo.scriptIntegrityHashTxBodyL Lens..~ scriptIntegrityHash
txBody' = Cardano.ShelleyTxBody a body' c (Cardano.TxBodyScriptData era datums redeemers) e f
mmontin marked this conversation as resolved.
Show resolved Hide resolved

-- We return the transaction signed by all the required signers. The body is
-- chosen based on whether or not it required additional data.
return $
Ledger.getEmulatorEraTx $
foldl
(flip Ledger.addCardanoTxWitness)
(Ledger.CardanoEmulatorEraTx $ Cardano.Tx (if null additionalDataMap then txBody else txBody') [])
(Ledger.toWitness . Ledger.PaymentPrivateKey . walletSK <$> txSkelSigners txSkel)
where
toCardanoLanguage :: Cardano.PlutusScriptVersion lang -> Cardano.Language
toCardanoLanguage = \case
Cardano.PlutusScriptV1 -> Cardano.PlutusV1
Cardano.PlutusScriptV2 -> Cardano.PlutusV2
Cardano.PlutusScriptV3 -> Cardano.PlutusV3
21 changes: 17 additions & 4 deletions src/Cooked/Pretty/Cooked.hs
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,12 @@ prettyTxSkelOut opts (Pays output) =
"Datum (inlined):"
<+> (PP.align . prettyCookedOpt opts)
(output ^. outputDatumL)
Api.OutputDatumHash _datum ->
Api.OutputDatumHash dHash ->
Just $
"Datum (hashed):"
"Datum (hashed)"
<+> "("
<> prettyHash (pcOptHashes opts) (toHash dHash)
<> "):"
Comment on lines +369 to +371
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should have a flag in the prettyprinting options to enable this on demand and hide it by default. The default printing might be starting to get a bit crowded (this is not the reason, this is just a drop, it just makes me think about it). In general, we don't really track datum hashes and this would be lighter by default.

For instance pcOptPrintDatumHashes like there already is a pcOptPrintTxHashes, false by default.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will leave these changes to later changes in the pretty printer.

<+> (PP.align . prettyCookedOpt opts)
(output ^. outputDatumL)
Api.NoOutputDatum -> Nothing,
Expand All @@ -371,9 +374,19 @@ prettyTxSkelOutDatumMaybe opts txSkelOutDatum@(TxSkelOutInlineDatum _) =
Just $
"Datum (inlined):"
<+> PP.align (prettyCookedOpt opts txSkelOutDatum)
prettyTxSkelOutDatumMaybe opts txSkelOutDatum =
prettyTxSkelOutDatumMaybe opts txSkelOutDatum@(TxSkelOutDatumHash dat) =
Just $
"Datum (hashed):"
"Datum (hashed)"
<+> "("
<> prettyHash (pcOptHashes opts) (toHash $ Script.datumHash $ Api.Datum $ Api.toBuiltinData dat)
<> "):"
mmontin marked this conversation as resolved.
Show resolved Hide resolved
<+> PP.align (prettyCookedOpt opts txSkelOutDatum)
prettyTxSkelOutDatumMaybe opts txSkelOutDatum@(TxSkelOutDatum dat) =
Just $
"Datum (hashed)"
<+> "("
<> prettyHash (pcOptHashes opts) (toHash $ Script.datumHash $ Api.Datum $ Api.toBuiltinData dat)
<> "):"
mmontin marked this conversation as resolved.
Show resolved Hide resolved
<+> PP.align (prettyCookedOpt opts txSkelOutDatum)

prettyTxSkelIn :: PrettyCookedOpts -> SkelContext -> (Api.TxOutRef, TxSkelRedeemer) -> DocCooked
Expand Down
32 changes: 17 additions & 15 deletions src/Cooked/Skeleton.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ module Cooked.Skeleton
paysPK,
paysScript,
paysScriptInlineDatum,
paysScriptDatumHash,
paysScriptUnresolvedDatumHash,
paysScriptNoDatum,
withDatum,
withInlineDatum,
withDatumHash,
withUnresolvedDatumHash,
withReferenceScript,
withStakingCredential,
TxSkelRedeemer (..),
Expand Down Expand Up @@ -870,8 +870,9 @@ paysPK pkh value =
(Nothing @(Script.Versioned Script.Script))
)

-- | Pays a script a certain value with a certain datum, using the
-- | Pays a script a certain value with a certain datum hash, using the
mmontin marked this conversation as resolved.
Show resolved Hide resolved
-- 'TxSkelOutDatum' constructor. (See the documentation of 'TxSkelOutDatum'.)
-- The datum is resolved in the transaction.
paysScript ::
( Api.ToData (Script.DatumType a),
Show (Script.DatumType a),
Expand Down Expand Up @@ -917,9 +918,9 @@ paysScriptInlineDatum validator datum value =
(Nothing @(Script.Versioned Script.Script))
)

-- | Pays a script a certain value with a certain hashed (not resolved in
-- transaction) datum.
paysScriptDatumHash ::
-- | Pays a script a certain value with a certain hashed datum which is not
-- resolved in the transaction (as opposed to "paysScript").
paysScriptUnresolvedDatumHash ::
( Api.ToData (Script.DatumType a),
Show (Script.DatumType a),
Typeable (Script.DatumType a),
Expand All @@ -931,7 +932,7 @@ paysScriptDatumHash ::
Script.DatumType a ->
Api.Value ->
TxSkelOut
paysScriptDatumHash validator datum value =
paysScriptUnresolvedDatumHash validator datum value =
Pays
( ConcreteOutput
validator
Expand All @@ -942,7 +943,7 @@ paysScriptDatumHash validator datum value =
)

-- | Pays a script a certain value without any datum. Intended to be used with
-- 'withDatum', 'withDatumHash', or 'withInlineDatum' to try a datum whose type
-- 'withDatum', 'withUnresolvedDatumHash', or 'withInlineDatum' to try a datum whose type
-- does not match the validator's.
paysScriptNoDatum :: (Typeable a) => Script.TypedValidator a -> Api.Value -> TxSkelOut
paysScriptNoDatum validator value =
Expand All @@ -968,8 +969,8 @@ withInlineDatum (Pays output) datum = Pays $ (fromAbstractOutput output) {concre
-- | Set the datum in a payment to the given hashed (not resolved in the
-- transaction) datum (whose type may not fit the typed validator in case of a
-- script).
withDatumHash :: (Api.ToData a, Show a, Typeable a, PlutusTx.Eq a, PrettyCooked a) => TxSkelOut -> a -> TxSkelOut
withDatumHash (Pays output) datum = Pays $ (fromAbstractOutput output) {concreteOutputDatum = TxSkelOutDatumHash datum}
withUnresolvedDatumHash :: (Api.ToData a, Show a, Typeable a, PlutusTx.Eq a, PrettyCooked a) => TxSkelOut -> a -> TxSkelOut
withUnresolvedDatumHash (Pays output) datum = Pays $ (fromAbstractOutput output) {concreteOutputDatum = TxSkelOutDatumHash datum}

-- | Add a reference script to a transaction output (or replace it if there is
-- already one)
Expand Down Expand Up @@ -1058,18 +1059,19 @@ data SkelContext = SkelContext
txSkelValueInOutputs :: TxSkel -> Api.Value
txSkelValueInOutputs = foldOf (txSkelOutsL % folded % txSkelOutValueL)

-- | Return all data on transaction outputs.
txSkelDataInOutputs :: TxSkel -> Map Api.DatumHash TxSkelOutDatum
-- | Return all data on transaction outputs. This can contain duplicates, which
-- is intended.
txSkelDataInOutputs :: TxSkel -> [(Api.DatumHash, TxSkelOutDatum)]
txSkelDataInOutputs =
foldMapOf
( txSkelOutsL
% folded
% txSkelOutDatumL
)
( \txSkelOutDatum -> do
( \txSkelOutDatum ->
maybe
Map.empty
(\datum -> Map.singleton (Script.datumHash datum) txSkelOutDatum)
[]
(\datum -> [(Script.datumHash datum, txSkelOutDatum)])
(txSkelOutUntypedDatum txSkelOutDatum)
)

Expand Down
2 changes: 1 addition & 1 deletion tests/Cooked/BalancingSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ initialDistributionBalancing =
paysPK alice (Script.ada 30),
paysPK alice (Script.lovelace 1280229 <> banana 3) `withDatum` (10 :: Integer),
paysPK alice (Script.ada 1 <> banana 7) `withReferenceScript` (alwaysTrueValidator @MockContract),
paysPK alice (Script.ada 105 <> banana 2) `withDatumHash` ()
paysPK alice (Script.ada 105 <> banana 2) `withUnresolvedDatumHash` ()
]

type TestBalancingOutcome = (TxSkel, TxSkel, Integer, Set Api.TxOutRef, [Api.TxOutRef])
Expand Down
Loading