14
14
-- In addition, you will find here many helpers functions which can be derived
15
15
-- from the core definition of our blockchain.
16
16
module Cooked.MockChain.BlockChain
17
- ( MockChainError (.. ),
17
+ ( GenerateTxError (.. ),
18
+ MockChainError (.. ),
18
19
MockChainLogEntry (.. ),
19
20
MonadBlockChainBalancing (.. ),
20
21
MonadBlockChainWithoutValidation (.. ),
@@ -43,7 +44,6 @@ module Cooked.MockChain.BlockChain
43
44
txSkelReferenceInputUtxos ,
44
45
txSkelInputValidators ,
45
46
txSkelInputValue ,
46
- txSkelHashedData ,
47
47
txSkelInputDataAsHashes ,
48
48
lookupUtxos ,
49
49
validateTxSkel' ,
@@ -55,6 +55,7 @@ module Cooked.MockChain.BlockChain
55
55
)
56
56
where
57
57
58
+ import Cardano.Api qualified as Cardano
58
59
import Cardano.Api.Ledger qualified as Cardano
59
60
import Cardano.Ledger.Conway.PParams qualified as Conway
60
61
import Cardano.Node.Emulator qualified as Emulator
@@ -69,7 +70,6 @@ import Control.Monad.Writer
69
70
import Cooked.Conversion.ToCredential
70
71
import Cooked.Conversion.ToOutputDatum
71
72
import Cooked.Conversion.ToScriptHash
72
- import Cooked.MockChain.GenerateTx
73
73
import Cooked.MockChain.UtxoState
74
74
import Cooked.Output
75
75
import Cooked.Skeleton
@@ -91,7 +91,17 @@ import PlutusLedgerApi.V3 qualified as Api
91
91
92
92
-- * MockChain errors
93
93
94
- -- | The errors that can be produced by the 'MockChainT' monad
94
+ -- | Errors that can arise during transaction generation
95
+ data GenerateTxError
96
+ = -- | Error when translating a skeleton element to its Cardano counterpart
97
+ ToCardanoError String Ledger. ToCardanoError
98
+ | -- | Error when generating a Cardano transaction body
99
+ TxBodyError String Cardano. TxBodyError
100
+ | -- | Other generation error
101
+ GenerateTxErrorGeneral String
102
+ deriving (Show , Eq )
103
+
104
+ -- | Errors that can be produced by the 'MockChainT' monad
95
105
data MockChainError
96
106
= -- | Validation errors, either in Phase 1 or Phase 2
97
107
MCEValidationError Ledger. ValidationPhase Ledger. ValidationError
@@ -288,10 +298,10 @@ resolveReferenceScript out | Just (Api.ScriptHash hash) <- outputReferenceScript
288
298
return $ (fromAbstractOutput out) {concreteOutputReferenceScript = Just val}
289
299
resolveReferenceScript _ = return Nothing
290
300
291
- outputDatumFromTxOutRef :: (MonadBlockChainWithoutValidation m ) => Api. TxOutRef -> m (Maybe Api. OutputDatum )
301
+ outputDatumFromTxOutRef :: (MonadBlockChainBalancing m ) => Api. TxOutRef -> m (Maybe Api. OutputDatum )
292
302
outputDatumFromTxOutRef = ((outputOutputDatum <$> ) <$> ) . txOutByRef
293
303
294
- datumFromTxOutRef :: (MonadBlockChainWithoutValidation m ) => Api. TxOutRef -> m (Maybe Api. Datum )
304
+ datumFromTxOutRef :: (MonadBlockChainBalancing m ) => Api. TxOutRef -> m (Maybe Api. Datum )
295
305
datumFromTxOutRef oref = do
296
306
mOutputDatum <- outputDatumFromTxOutRef oref
297
307
case mOutputDatum of
@@ -300,10 +310,10 @@ datumFromTxOutRef oref = do
300
310
Just (Api. OutputDatum datum) -> return $ Just datum
301
311
Just (Api. OutputDatumHash datumHash) -> datumFromHash datumHash
302
312
303
- typedDatumFromTxOutRef :: (Api. FromData a , MonadBlockChainWithoutValidation m ) => Api. TxOutRef -> m (Maybe a )
313
+ typedDatumFromTxOutRef :: (Api. FromData a , MonadBlockChainBalancing m ) => Api. TxOutRef -> m (Maybe a )
304
314
typedDatumFromTxOutRef = ((>>= (\ (Api. Datum datum) -> Api. fromBuiltinData datum)) <$> ) . datumFromTxOutRef
305
315
306
- valueFromTxOutRef :: (MonadBlockChainWithoutValidation m ) => Api. TxOutRef -> m (Maybe Api. Value )
316
+ valueFromTxOutRef :: (MonadBlockChainBalancing m ) => Api. TxOutRef -> m (Maybe Api. Value )
307
317
valueFromTxOutRef = ((outputValue <$> ) <$> ) . txOutByRef
308
318
309
319
txSkelInputUtxos :: (MonadBlockChainBalancing m ) => TxSkel -> m (Map Api. TxOutRef Api. TxOut )
@@ -356,37 +366,16 @@ lookupUtxos =
356
366
txSkelInputValue :: (MonadBlockChainBalancing m ) => TxSkel -> m Api. Value
357
367
txSkelInputValue = (foldMap Api. txOutValue <$> ) . txSkelInputUtxos
358
368
359
- -- | Looks up and resolves the hashed datums on UTxOs the transaction consumes
360
- -- or references, which will be needed by the transaction body.
361
- txSkelHashedData :: (MonadBlockChainBalancing m ) => TxSkel -> m (Map Api. DatumHash Api. Datum )
362
- txSkelHashedData skel = do
363
- (Map. elems -> inputTxOuts) <- txSkelInputUtxos skel
364
- (Map. elems -> refInputTxOuts) <- txSkelReferenceInputUtxos skel
365
- foldM
366
- ( \ dat dHash ->
367
- maybeErrM
368
- (MCEUnknownDatum " txSkelHashedData: Transaction input with unknown datum hash" dHash)
369
- (\ rDat -> Map. insert dHash rDat dat)
370
- (datumFromHash dHash)
371
- )
372
- Map. empty
373
- (mapMaybe (fmap (^. outputDatumL) . isOutputWithDatumHash) $ inputTxOuts <> refInputTxOuts)
374
-
375
369
-- | Looks up the data on UTxOs the transaction consumes and returns their
376
- -- hashes. This corresponds to the keys of what should be removed from the
377
- -- stored datums in our mockchain. There can be duplicates, which is expected.
370
+ -- hashes.
378
371
txSkelInputDataAsHashes :: (MonadBlockChainBalancing m ) => TxSkel -> m [Api. DatumHash ]
379
372
txSkelInputDataAsHashes skel = do
380
- let outputToDatumHashM output = case output ^. outputDatumL of
381
- Api. OutputDatumHash dHash ->
382
- maybeErrM
383
- (MCEUnknownDatum " txSkelInputDataAsHashes: Transaction input with unknown datum hash" dHash)
384
- (Just . const dHash)
385
- (datumFromHash dHash)
386
- Api. OutputDatum datum -> return $ Just $ Script. datumHash datum
387
- Api. NoOutputDatum -> return Nothing
373
+ let outputToDatumHash output = case output ^. outputDatumL of
374
+ Api. OutputDatumHash dHash -> Just dHash
375
+ Api. OutputDatum datum -> Just $ Script. datumHash datum
376
+ Api. NoOutputDatum -> Nothing
388
377
(Map. elems -> inputTxOuts) <- txSkelInputUtxos skel
389
- catMaybes <$> mapM outputToDatumHashM inputTxOuts
378
+ return $ mapMaybe outputToDatumHash inputTxOuts
390
379
391
380
-- | This creates a payment from an existing UTXO
392
381
txOutRefToTxSkelOut ::
0 commit comments