tweag / cooked-validators

MIT License
39 stars 11 forks source link

Remove datum from `SpendsScript` with no feature loss #156

Closed florentc closed 1 year ago

florentc commented 2 years ago

The SpendsScript constraint has a parameter for the datum which is annoying and redundant with the spendable output itself. This was necessary to pretty print these constraints. This PR removes the parameter, keeps pretty-printing as it was, and adds convenience functions to write cleaner endpoints & traces.

Look at each commit of this PR to get a better grasp at individual changes.

The Example module showcases the changes. (you will need to disable the attack modules and tests if you want to try until these are adapted)

Main changes

Why this works?

A SpendableOut has a Pl.ChainIndexTxOut which contains a datum field. This datum is either (Either) an actual Pl.Datum or a datum hash (Pl.DatumHash). In practice, the existing classic SpendsScript constraint leads to a runtime error (DatumNotFound from plutus-apps) when the Pl.ChainIndexTxOut only has a hash. This means that we have always been manipulating ChainIndexTxOuts with the actual datum inside (when we fetch one with scriptUtxosSuchThat for instance). In other words, removing the datum parameter from SpendsScript does not change anything.

On the other hand, script utxos we extract from Pl.CardanoTx only have a hash. This is were the new functions datumFromTxOut, spOutResolveDatum, and spOutsFromCardanoTx come in handy: they look up the hash in the context of a BlockChain so that we have SpendableOuts which are usable by SpendsScript. This is something that we had not noticed before because we were only using SpendableOuts coming from utxosSuchThat functions.