tweag / cooked-validators

MIT License
39 stars 11 forks source link

Shall `utxosSuchThisAndThat'` use datum hashes or datums? #184

Closed 0xd34df00d closed 1 year ago

0xd34df00d commented 1 year ago

Right now, utxosSuchThisAndThat' gets the datum hash via fromCardanoTxOutDatumHash and uses that to look up the datum in the map of managed datums. Inline datums aren't managed, so we could either (1) make them managed, or (2) use fromCardanoTxOutDatum and discriminate between OutputDatum and OutputDatumHash.

I tried the second approach briefly, but it gives me

FailWith "Can't convert from builtin data at: TxOutRef {txOutRefId = 66f6ebcb5d68cde8a0c251fb3eafc16409248f082a8e3ea73d04f176448adfa5, txOutRefIdx = 0}; are you sure this is the right type?"

on some tests, the cause of which I don't understand.

0xd34df00d commented 1 year ago

For the record, the diff for (2) is fairly trivial:

diff --git a/cooked-validators/src/Cooked/MockChain/Monad/Direct.hs b/cooked-validators/src/Cooked/MockChain/Monad/Direct.hs
index 58fb322..ea34f3d 100644
--- a/cooked-validators/src/Cooked/MockChain/Monad/Direct.hs
+++ b/cooked-validators/src/Cooked/MockChain/Monad/Direct.hs
@@ -377,26 +377,27 @@ utxosSuchThisAndThat' addrPred datumPred = do
       case cTxOutToCito out of
         Nothing -> pure Nothing
         Just cito -> do
-          mDatum <- extractDatum oref cito $ Pl.fromCardanoTxOutDatumHash cDatum
+          mDatum <- extractDatum oref cito $ Pl.fromCardanoTxOutDatum cDatum
           pure $
             if datumPred mDatum (Pl._ciTxOutValue cito)
                then Just (cito, mDatum)
                else Nothing

-    extractDatum :: Pl.TxOutRef -> Pl.ChainIndexTxOut -> Maybe Pl.DatumHash -> MockChainT m (Maybe a)
-    extractDatum oref cito Nothing
+    extractDatum :: Pl.TxOutRef -> Pl.ChainIndexTxOut -> Pl.OutputDatum -> MockChainT m (Maybe a)
+    extractDatum oref cito Pl.NoOutputDatum
       | isScriptCito cito = fail $ "ScriptCredential with no datum hash: " ++ show oref
       | otherwise = pure Nothing
-    extractDatum oref _ (Just dh) = do
+    extractDatum oref _ (Pl.OutputDatumHash dh) = do
       managedDatums <- gets mcstDatums
       Just <$> case dh `M.lookup` managedDatums of
            -- TODO PORT previously this was an error only for script datums,
            -- but I think it should be an error for any datum hash
            -- that's mentioned but cannot be found in managedDatums.
            Nothing -> fail $ "Unmanaged datum with hash: " ++ show dh ++ " at: " ++ show oref
-           Just datum -> maybe failBadConvert pure (Pl.fromBuiltinData $ Pl.getDatum datum)
-      where
-        failBadConvert = fail $ "Can't convert from builtin data at: " ++ show oref ++ "; are you sure this is the right type?"
+           Just datum -> maybe (failBadConvert oref) pure (Pl.fromBuiltinData $ Pl.getDatum datum)
+    extractDatum oref _ (Pl.OutputDatum datum) = maybe (failBadConvert oref) pure (Pl.fromBuiltinData $ Pl.getDatum datum)
+
+    failBadConvert oref = fail $ "Can't convert from builtin data at: " ++ show oref ++ "; are you sure this is the right type?"

 -- | Check 'utxosSuchThat' for details
 utxosSuchThat' ::