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

Automatic reference script #449

Merged
merged 95 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 91 commits
Commits
Show all changes
95 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
91b3309
homogenizing tests + make them depend on log
mmontin Sep 2, 2024
15699d2
one more refactoring
mmontin Sep 2, 2024
bbe4122
merging
mmontin Sep 5, 2024
70da48d
Merge branch 'mm/djed-audit' into mm/tests-on-logging
mmontin Sep 5, 2024
4d12255
refactoring testing in progress
mmontin Sep 5, 2024
1aed73d
finishing to rework testing
mmontin Sep 9, 2024
5d2ae4e
flipping ==>
mmontin Sep 9, 2024
6a87987
restructuring and commenting testing module
mmontin Sep 9, 2024
410d7b2
spreading around all the changes, removing fancy non-working type class
mmontin Sep 10, 2024
a53419a
better name
mmontin Sep 10, 2024
943d937
post review changes
mmontin Sep 10, 2024
3dd8be8
Merge branch 'mm/djed-audit' into mm/tests-on-logging
mmontin Sep 10, 2024
60a53ef
Merge branch 'main' into mm/tests-on-logging
mmontin Sep 10, 2024
c540418
ToScript -> ToVersionedScript
mmontin Aug 30, 2024
b234f9c
typos in comments
mmontin Aug 30, 2024
69cf234
first feature but it does not compile due to no ord on redeemers
mmontin Aug 30, 2024
cbbc28c
auto-ref-script feature
mmontin Aug 30, 2024
ee2b9fe
adapting tests
mmontin Aug 30, 2024
bcc1340
a first test of auto ref script is done
mmontin Aug 30, 2024
db4fb0f
logging the addition of a reference script
mmontin Aug 30, 2024
d1007c5
logging new ref scripts
mmontin Aug 30, 2024
3d17c7a
small upgrade in direct
mmontin Aug 30, 2024
919ae42
small improvement
mmontin Aug 30, 2024
fb7a68b
renaming redeemers
mmontin Aug 31, 2024
b66c5f1
renaming and refactoring of redeemers
mmontin Aug 31, 2024
1fd5f56
adapting tests
mmontin Aug 31, 2024
785cc17
post merge fix
mmontin Sep 10, 2024
d6a23e0
CHANGELOG.md
mmontin Sep 10, 2024
eef9371
Merge branch 'mm/tests-on-logging' into mm/auto-ref-scripts
mmontin Sep 10, 2024
4cbbb35
merging main into this
mmontin Sep 11, 2024
3121e02
post merge change
mmontin Sep 11, 2024
cf7be33
integrating review comments
mmontin Sep 12, 2024
a2320b3
txOptAutoReferenceScript set to False by default
mmontin Sep 12, 2024
dc06fc2
updating readme
mmontin Sep 12, 2024
6fa9cfd
Merge branch 'main' into mm/auto-ref-scripts
mmontin Sep 12, 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
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
constrained yet.
- PrettyCooked option `pcOptPrintLog`, which is a boolean, to turn on or off the log
display in the pretty printer. The default value is `True`.
- Reference inputs with proper reference scripts are now automatically attached
mmontin marked this conversation as resolved.
Show resolved Hide resolved
to redeemers. This can be turned off using `txOptAutoReferenceScripts`, in
which case the helper `withReferenceInput` can be used on a redeemer to
automatically attach a reference input.
mmontin marked this conversation as resolved.
Show resolved Hide resolved
- Capability to test the result of a mockchain run based on the log entries.

### Removed
Expand All @@ -34,10 +38,8 @@
- GHC bumped to 9.6.6
- Internal representation of redeemers have changed, and are similar for any
supported script purpose (minting, spending or proposing).
- Redeemers should now be built using one of the four following smart
constructors: `txSkelSomeRedeemer`, `txSkelEmptyRedeemer`,
`txSkelSomeRedeemerAndReferenceScript`,
`txSkelEmptyRedeemerAndReferenceScript`.
- Redeemers should now be built using one of the two following smart
constructors: `someTxSkelRedeemer`, `emptyTxSkelRedeemer`
mmontin marked this conversation as resolved.
Show resolved Hide resolved
- `mkProposingScript` changed to `mkScript`
- `withDatumHashed` changed to `withUnresolvedDatumHash`
- `paysScriptDatumHashed` changed to `paysScriptUnresolvedDatumHash`
Expand Down
3 changes: 2 additions & 1 deletion cooked-validators.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ library
Cooked.Conversion.ToHash
Cooked.Conversion.ToOutputDatum
Cooked.Conversion.ToPubKeyHash
Cooked.Conversion.ToScript
Cooked.Conversion.ToScriptHash
Cooked.Conversion.ToValue
Cooked.Conversion.ToVersionedScript
Cooked.Currencies
Cooked.InitialDistribution
Cooked.Ltl
Cooked.MockChain
Cooked.MockChain.AutoReferenceScripts
Cooked.MockChain.Balancing
Cooked.MockChain.BlockChain
Cooked.MockChain.Direct
Expand Down
2 changes: 1 addition & 1 deletion src/Cooked/Conversion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import Cooked.Conversion.ToCredential as X
import Cooked.Conversion.ToHash as X
import Cooked.Conversion.ToOutputDatum as X
import Cooked.Conversion.ToPubKeyHash as X
import Cooked.Conversion.ToScript as X
import Cooked.Conversion.ToScriptHash as X
import Cooked.Conversion.ToValue as X
import Cooked.Conversion.ToVersionedScript as X
20 changes: 0 additions & 20 deletions src/Cooked/Conversion/ToScript.hs

This file was deleted.

8 changes: 4 additions & 4 deletions src/Cooked/Conversion/ToScriptHash.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- | Objects from which a script hash can be extracted
module Cooked.Conversion.ToScriptHash where

import Cooked.Conversion.ToScript
import Cooked.Conversion.ToVersionedScript
import Plutus.Script.Utils.Scripts qualified as Script
import Plutus.Script.Utils.Typed qualified as Script
import PlutusLedgerApi.V3 qualified as Api
Expand All @@ -22,10 +22,10 @@ instance ToScriptHash (Script.Versioned Script.Script) where
toScriptHash = Script.scriptHash

instance ToScriptHash (Script.Versioned Script.Validator) where
toScriptHash = toScriptHash . toScript
toScriptHash = toScriptHash . toVersionedScript

instance ToScriptHash (Script.TypedValidator a) where
toScriptHash = toScriptHash . toScript
toScriptHash = toScriptHash . toVersionedScript

instance ToScriptHash (Script.Versioned Script.MintingPolicy) where
toScriptHash = toScriptHash . Script.mintingPolicyHash
toScriptHash = toScriptHash . toVersionedScript
20 changes: 20 additions & 0 deletions src/Cooked/Conversion/ToVersionedScript.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- | Objects from which a versioned script can be extracted
module Cooked.Conversion.ToVersionedScript where

import Plutus.Script.Utils.Scripts qualified as Script
import Plutus.Script.Utils.Typed qualified as Script

class ToVersionedScript a where
toVersionedScript :: a -> Script.Versioned Script.Script

instance ToVersionedScript (Script.Versioned Script.Script) where
toVersionedScript = id

instance ToVersionedScript (Script.Versioned Script.Validator) where
toVersionedScript (Script.Versioned (Script.Validator script) version) = Script.Versioned script version

instance ToVersionedScript (Script.TypedValidator a) where
toVersionedScript = toVersionedScript . Script.vValidatorScript

instance ToVersionedScript (Script.Versioned Script.MintingPolicy) where
toVersionedScript (Script.Versioned (Script.MintingPolicy script) version) = Script.Versioned script version
64 changes: 64 additions & 0 deletions src/Cooked/MockChain/AutoReferenceScripts.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
-- | This module provides a function to ensure that each redeemer used in a
-- skeleton is attached a reference scripts when a known utxos contains it.
mmontin marked this conversation as resolved.
Show resolved Hide resolved
module Cooked.MockChain.AutoReferenceScripts (toTxSkelWithReferenceScripts) where

import Control.Monad
import Cooked.Conversion
import Cooked.MockChain.BlockChain
import Cooked.MockChain.UtxoSearch
import Cooked.Output
import Cooked.Skeleton
import Data.Map qualified as Map
import Data.Maybe
import Optics.Core
import PlutusLedgerApi.V3 qualified as Api

-- | Searches through the known utxos for a utxo containing a reference script
-- with a given script hash, and returns the first such utxo found, if any.
mmontin marked this conversation as resolved.
Show resolved Hide resolved
retrieveReferenceScript :: (MonadBlockChain m, ToScriptHash s) => s -> m (Maybe Api.TxOutRef)
retrieveReferenceScript script = listToMaybe . (fst <$>) <$> runUtxoSearch (referenceScriptOutputsSearch script)

-- | Attempts to attach a reference script to a redeemer when it can be found in
mmontin marked this conversation as resolved.
Show resolved Hide resolved
-- the context. Will not override any existing reference script. If this results
-- in the addition of a reference script, will log the event.
updateRedeemer :: (MonadBlockChain m, ToScriptHash s) => s -> TxSkelRedeemer -> m TxSkelRedeemer
updateRedeemer script txSkelRed@(TxSkelRedeemer red Nothing) = do
oRefM <- retrieveReferenceScript script
case oRefM of
Nothing -> return txSkelRed
Just oRef -> do
logEvent $ MCLogAddedReferenceScript red oRef (toScriptHash script)
return $ TxSkelRedeemer red $ Just oRef
updateRedeemer _ redeemer = return redeemer

-- | Goes through the various parts of the skeleton where a redeemer can appear,
-- and attempt to attach a reference script to each of them, following the rules
-- from `updateRedeemer`
toTxSkelWithReferenceScripts :: (MonadBlockChain m) => TxSkel -> m TxSkel
toTxSkelWithReferenceScripts txSkel = do
newMints <- forM (txSkelMintsToList $ txSkel ^. txSkelMintsL) $ \(mPol, red, tk, nb) ->
(mPol,,tk,nb) <$> updateRedeemer mPol red
newInputs <- forM (Map.toList $ txSkel ^. txSkelInsL) $ \(oRef, red) -> do
outputM <- txOutByRef oRef
-- We retrieve the possible script hash of the current oRef
case (^. outputOwnerL) <$> (outputM >>= isScriptOutput) of
-- Either the txOutRef is unknown, or it belongs to a private key
Nothing -> return (oRef, red)
Just scriptHash -> (oRef,) <$> updateRedeemer scriptHash red
newProposals <- forM (txSkel ^. txSkelProposalsL) $ \prop ->
case prop ^. txSkelProposalWitnessL of
Nothing -> return prop
Just (script, red) -> flip (set txSkelProposalWitnessL) prop . Just . (script,) <$> updateRedeemer script red
newWithdrawals <- forM (Map.toList $ txSkel ^. txSkelWithdrawalsL) $ \(wit, (red, quantity)) -> case wit of
Right _ -> return (wit, (red, quantity))
Left script -> (Left script,) . (,quantity) <$> updateRedeemer script red
return $
txSkel
& txSkelMintsL
.~ txSkelMintsFromList newMints
& txSkelInsL
.~ Map.fromList newInputs
& txSkelProposalsL
.~ newProposals
& txSkelWithdrawalsL
.~ Map.fromList newWithdrawals
2 changes: 1 addition & 1 deletion src/Cooked/MockChain/Balancing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -398,5 +398,5 @@ computeBalancedTxSkel balancingWallet balancingUtxos txSkel@TxSkel {..} (Script.
-- a new output at the end of the list, to keep the order intact.
(txOutRefs, val) <- getOptimalCandidate candidatesRaw balancingWallet balancingError
return (txOutRefs, txSkelOuts ++ [paysPK balancingWallet val])
let newTxSkelIns = txSkelIns <> Map.fromList ((,txSkelEmptyRedeemer) <$> additionalInsTxOutRefs)
let newTxSkelIns = txSkelIns <> Map.fromList ((,emptyTxSkelRedeemer) <$> additionalInsTxOutRefs)
return $ (txSkel & txSkelOutsL .~ newTxSkelOuts) & txSkelInsL .~ newTxSkelIns
2 changes: 2 additions & 0 deletions src/Cooked/MockChain/BlockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ data MockChainLogEntry
-- depending on whether the user has provided an explicit wallet or a set of
-- utxos to be used as collaterals.
MCLogUnusedCollaterals (Either Wallet (Set Api.TxOutRef))
| -- | Logging the automatic addition of a reference script
MCLogAddedReferenceScript Redeemer Api.TxOutRef Script.ScriptHash

-- | Contains methods needed for balancing.
class (MonadFail m, MonadError MockChainError m) => MonadBlockChainBalancing m where
Expand Down
17 changes: 10 additions & 7 deletions src/Cooked/MockChain/Direct.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Control.Monad.Reader
import Control.Monad.State.Strict
import Control.Monad.Writer
import Cooked.InitialDistribution
import Cooked.MockChain.AutoReferenceScripts
import Cooked.MockChain.Balancing
import Cooked.MockChain.BlockChain
import Cooked.MockChain.GenerateTx
Expand Down Expand Up @@ -137,21 +138,24 @@ instance (Monad m) => MonadBlockChainWithoutValidation (MockChainT m) where
awaitSlot s = modify' (\st -> st {mcstCurrentSlot = max s (mcstCurrentSlot st)}) >> currentSlot

instance (Monad m) => MonadBlockChain (MockChainT m) where
validateTxSkel skelUnbal = do
validateTxSkel skelUnbal@TxSkel {..} | TxOpts {..} <- txSkelOpts = do
-- We log the submitted skeleton
gets mcstToSkelContext >>= logEvent . (`MCLogSubmittedTxSkel` skelUnbal)
-- We retrieve the current parameters
oldParams <- getParams
-- We compute the optionally modified parameters
let newParams = applyEmulatorParamsModification (txOptEmulatorParamsModification . txSkelOpts $ skelUnbal) oldParams
let newParams = applyEmulatorParamsModification txOptEmulatorParamsModification oldParams
-- We change the parameters for the duration of the validation process
setParams newParams
-- We ensure that the outputs have the required minimal amount of ada, when
-- requested in the skeleton options
minAdaSkelUnbal <- if txOptEnsureMinAda . txSkelOpts $ skelUnbal then toTxSkelWithMinAda skelUnbal else return skelUnbal
minAdaSkelUnbal <- (if txOptEnsureMinAda then toTxSkelWithMinAda else return) skelUnbal
-- We add reference scripts in the various redeemers of the skeleton, when
-- they can be found in the index and are requested in the skeleton options
minAdaRefScriptsSkelUnbal <- (if txOptAutoReferenceScripts then toTxSkelWithReferenceScripts else return) minAdaSkelUnbal
-- We balance the skeleton when requested in the skeleton option, and get
-- the associated fee, collateral inputs and return collateral wallet
(skel, fee, mCollaterals) <- balanceTxSkel minAdaSkelUnbal
(skel, fee, mCollaterals) <- balanceTxSkel minAdaRefScriptsSkelUnbal
-- We log the adjusted skeleton
gets mcstToSkelContext >>= \ctx -> logEvent $ MCLogAdjustedTxSkel ctx skel fee mCollaterals
-- We retrieve data that will be used in the transaction generation process:
Expand All @@ -169,7 +173,7 @@ instance (Monad m) => MonadBlockChain (MockChainT m) where
cardanoTx <- case generateTx fee newParams hashedData (insMap <> refInsMap <> collateralInsMap) insValidators mCollaterals skel of
Left err -> throwError . MCEGenerationError $ err
-- We apply post-generation modification when applicable
Right tx -> return $ Ledger.CardanoEmulatorEraTx $ applyRawModOnBalancedTx (txOptUnsafeModTx . txSkelOpts $ skelUnbal) tx
Right tx -> return $ Ledger.CardanoEmulatorEraTx $ applyRawModOnBalancedTx txOptUnsafeModTx tx
-- To run transaction validation we need a minimal ledger state
eLedgerState <- gets mcstToEmulatedLedgerState
-- We finally run the emulated validation, and we only care about the
Expand Down Expand Up @@ -203,8 +207,7 @@ instance (Monad m) => MonadBlockChain (MockChainT m) where
. addValidators (txSkelValidatorsInOutputs skel <> txSkelReferenceScripts skel)
)
-- We apply a change of slot when requested in the options
when (txOptAutoSlotIncrease $ txSkelOpts skel) $
modify' (\st -> st {mcstCurrentSlot = mcstCurrentSlot st + 1})
when txOptAutoSlotIncrease $ modify' (\st -> st {mcstCurrentSlot = mcstCurrentSlot st + 1})
-- We return the parameters to their original state
setParams oldParams
-- We log the validated transaction
Expand Down
2 changes: 1 addition & 1 deletion src/Cooked/MockChain/GenerateTx/Output.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ toCardanoTxOut (Pays output) = do
<$> Ledger.toCardanoScriptDataHash (Script.datumHash $ Api.Datum $ Api.toBuiltinData datum)
TxSkelOutDatum datum -> return $ Cardano.TxOutDatumInTx Cardano.AlonzoEraOnwardsConway $ toHashableScriptData datum
TxSkelOutInlineDatum datum -> return $ Cardano.TxOutDatumInline Cardano.BabbageEraOnwardsConway $ toHashableScriptData datum
return $ Cardano.TxOut address value datum $ Ledger.toCardanoReferenceScript (toScript <$> oRefScript)
return $ Cardano.TxOut address value datum $ Ledger.toCardanoReferenceScript (toVersionedScript <$> oRefScript)
2 changes: 1 addition & 1 deletion src/Cooked/MockChain/GenerateTx/Proposal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ toProposalProcedureAndWitness txSkelProposal@TxSkelProposal {..} anchorResolutio
let conwayProposalProcedure = Conway.ProposalProcedure (Emulator.Coin minDeposit) cred govAction anchor
(conwayProposalProcedure,) <$> case txSkelProposalWitness of
Nothing -> return Nothing
Just (script, redeemer) -> Just <$> liftTxGen (toScriptWitness (toScript script) redeemer Cardano.NoScriptDatumForStake)
Just (script, redeemer) -> Just <$> liftTxGen (toScriptWitness (toVersionedScript script) redeemer Cardano.NoScriptDatumForStake)

-- | Translates a list of skeleton proposals into a proposal procedures
toProposalProcedures :: [TxSkelProposal] -> AnchorResolution -> ProposalGen (Cardano.TxProposalProcedures Cardano.BuildTx Cardano.ConwayEra)
Expand Down
14 changes: 7 additions & 7 deletions src/Cooked/MockChain/GenerateTx/Witness.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ toRewardAccount cred =
(Ledger.toCardanoStakeKeyHash pubkeyHash)
return $ Cardano.KeyHashObj pkHash

-- | Translate a script and a reference script utxo into into either a plutus
-- script or a reference input containing the right script
-- | Translates a script and a reference script utxo into either a plutus script
-- or a reference input containing the right script
toPlutusScriptOrReferenceInput :: Script.Versioned Script.Script -> Maybe Api.TxOutRef -> WitnessGen (Cardano.PlutusScriptOrReferenceInput lang)
toPlutusScriptOrReferenceInput (Script.Versioned (Script.Script script) _) Nothing = return $ Cardano.PScript $ Cardano.PlutusScriptSerialised script
toPlutusScriptOrReferenceInput script (Just scriptOutRef) = do
Expand All @@ -66,18 +66,18 @@ toPlutusScriptOrReferenceInput script (Just scriptOutRef) = do

-- | Translates a script with its associated redeemer and datum to a script
-- witness.
toScriptWitness :: (ToScript a) => a -> TxSkelRedeemer -> Cardano.ScriptDatum b -> WitnessGen (Cardano.ScriptWitness b Cardano.ConwayEra)
toScriptWitness (toScript -> script@(Script.Versioned _ version)) (TxSkelRedeemer {..}) datum =
toScriptWitness :: (ToVersionedScript a) => a -> TxSkelRedeemer -> Cardano.ScriptDatum b -> WitnessGen (Cardano.ScriptWitness b Cardano.ConwayEra)
toScriptWitness (toVersionedScript -> script@(Script.Versioned _ version)) (TxSkelRedeemer {..}) datum =
let scriptData = case txSkelRedeemer of
EmptyRedeemer -> Ledger.toCardanoScriptData $ Api.toBuiltinData ()
SomeRedeemer s -> Ledger.toCardanoScriptData $ Api.toBuiltinData s
in case version of
Script.PlutusV1 ->
(\x -> Cardano.PlutusScriptWitness Cardano.PlutusScriptV1InConway Cardano.PlutusScriptV1 x datum scriptData Ledger.zeroExecutionUnits)
<$> toPlutusScriptOrReferenceInput script txSkelReferenceScript
<$> toPlutusScriptOrReferenceInput script txSkelReferenceInput
Script.PlutusV2 ->
(\x -> Cardano.PlutusScriptWitness Cardano.PlutusScriptV2InConway Cardano.PlutusScriptV2 x datum scriptData Ledger.zeroExecutionUnits)
<$> toPlutusScriptOrReferenceInput script txSkelReferenceScript
<$> toPlutusScriptOrReferenceInput script txSkelReferenceInput
Script.PlutusV3 ->
(\x -> Cardano.PlutusScriptWitness Cardano.PlutusScriptV3InConway Cardano.PlutusScriptV3 x datum scriptData Ledger.zeroExecutionUnits)
<$> toPlutusScriptOrReferenceInput script txSkelReferenceScript
<$> toPlutusScriptOrReferenceInput script txSkelReferenceInput
4 changes: 2 additions & 2 deletions src/Cooked/MockChain/MockChainSt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import Cardano.Ledger.Shelley.API qualified as Shelley
import Cardano.Ledger.Shelley.LedgerState qualified as Shelley
import Cardano.Node.Emulator.Internal.Node qualified as Emulator
import Control.Arrow
import Cooked.Conversion.ToScript
import Cooked.Conversion.ToScriptHash
import Cooked.Conversion.ToVersionedScript
import Cooked.InitialDistribution
import Cooked.MockChain.GenerateTx (GenerateTxError (..), generateTxOut)
import Cooked.MockChain.UtxoState
Expand Down Expand Up @@ -178,7 +178,7 @@ referenceScriptMap0From (InitialDistribution initDist) =
unitMaybeFrom :: TxSkelOut -> Maybe (Script.ValidatorHash, Script.Versioned Script.Validator)
unitMaybeFrom (Pays output) = do
refScript <- view outputReferenceScriptL output
let vScript@(Script.Versioned script version) = toScript refScript
let vScript@(Script.Versioned script version) = toVersionedScript refScript
Api.ScriptHash scriptHash = toScriptHash vScript
return (Script.ValidatorHash scriptHash, Script.Versioned (Script.Validator script) version)

Expand Down
22 changes: 16 additions & 6 deletions src/Cooked/Pretty/Cooked.hs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,23 @@ instance PrettyCooked MockChainLogEntry where
prettyCookedOpt opts (MCLogNewTx txId) = "New transaction:" <+> prettyCookedOpt opts txId
prettyCookedOpt opts (MCLogDiscardedUtxos n s) = prettyCookedOpt opts n <+> "balancing utxos were discarded:" <+> PP.pretty s
prettyCookedOpt opts (MCLogUnusedCollaterals (Left cWallet)) =
"Specific request to fetch collateral utxos from"
<+> prettyCookedOpt opts (walletPKHash cWallet)
<+> "has been disregarded because the transaction does not require collaterals"
"Specific request to fetch collateral utxos from "
<> prettyCookedOpt opts (walletPKHash cWallet)
<> " has been disregarded because the transaction does not require collaterals"
prettyCookedOpt opts (MCLogUnusedCollaterals (Right (length -> n))) =
"Specific request to fetch collateral utxos from the given set of"
<+> prettyCookedOpt opts n
<+> "elements has been disregarded because the transaction does not require collaterals"
"Specific request to fetch collateral utxos from the given set of "
<> prettyCookedOpt opts n
<> " elements has been disregarded because the transaction does not require collaterals"
prettyCookedOpt opts (MCLogAddedReferenceScript red oRef sHash) =
"A reference script sitting at "
mmontin marked this conversation as resolved.
Show resolved Hide resolved
<> prettyCookedOpt opts oRef
<> " has been automatically associated to redeemer "
<> ( case red of
EmptyRedeemer -> "Empty"
SomeRedeemer s -> prettyCookedOpt opts s
)
<> " for script "
<> prettyCookedOpt opts sHash

prettyTxSkel :: PrettyCookedOpts -> SkelContext -> TxSkel -> DocCooked
prettyTxSkel opts skelContext (TxSkel lbl txopts mints signers validityRange ins insReference outs proposals withdrawals) =
Expand Down
Loading