Skip to content

Commit

Permalink
merging txout into this
Browse files Browse the repository at this point in the history
  • Loading branch information
mmontin committed Aug 29, 2024
2 parents cdc788b + 70a0bee commit 1554e51
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 48 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- Partial support for withdrawals in txSkels. The rewarding scripts will be ran
and assets will be transferred. However, these withdrawals are not properly
constrainted yet.
- PrettyCooked option `pcOptLog`, which is a boolean, to turn on or off the log
- PrettyCooked option `pcOptPrintLog`, which is a boolean, to turn on or off the log
display in the pretty printer. The default value is `True`.

### Removed
Expand All @@ -42,6 +42,7 @@
- `txSkelInputData` changed to `txSkelConsumedData`
- Pretty printing of hashed datum now includes the hash (and not only the
resolved datum).
- Dependency to cardano-api bumped to 8.46.
- Logging has been reworked:
* it is no longer limited to `StagedMockChain` runs
* it is now a component of `MonadBlockChainBalancing`
Expand Down
2 changes: 1 addition & 1 deletion src/Cooked/MockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Cooked.MockChain (module X) where

import Cooked.MockChain.Balancing as X
import Cooked.MockChain.BlockChain as X hiding (MockChainLogEntry, publish)
import Cooked.MockChain.BlockChain as X hiding (MockChainLogEntry, logEvent)
import Cooked.MockChain.Direct as X hiding (MockChainReturn)
import Cooked.MockChain.MinAda as X
import Cooked.MockChain.MockChainSt as X (MockChainSt (..), mockChainSt0From)
Expand Down
6 changes: 3 additions & 3 deletions src/Cooked/MockChain/Balancing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ balanceTxSkel skelUnbal@TxSkel {..} = do
&& Map.null spendingScripts
&& null (txSkelWithdrawalsScripts skelUnbal)
case (noScriptInvolved, txOptCollateralUtxos txSkelOpts) of
(True, CollateralUtxosFromSet utxos _) -> publish (MCLogUnusedCollaterals $ Right utxos) >> return Nothing
(True, CollateralUtxosFromWallet cWallet) -> publish (MCLogUnusedCollaterals $ Left cWallet) >> return Nothing
(True, CollateralUtxosFromSet utxos _) -> logEvent (MCLogUnusedCollaterals $ Right utxos) >> return Nothing
(True, CollateralUtxosFromWallet cWallet) -> logEvent (MCLogUnusedCollaterals $ Left cWallet) >> return Nothing
(True, CollateralUtxosFromBalancingWallet) -> return Nothing
(False, CollateralUtxosFromSet utxos rWallet) -> return $ Just (utxos, rWallet)
(False, CollateralUtxosFromWallet cWallet) -> Just . (,cWallet) . Set.fromList . map fst <$> runUtxoSearch (onlyValueOutputsAtSearch cWallet)
Expand Down Expand Up @@ -137,7 +137,7 @@ balanceTxSkel skelUnbal@TxSkel {..} = do
where
filterAndWarn f s l
| (ok, toInteger . length -> koLength) <- partition f l =
unless (koLength == 0) (publish $ MCLogDiscardedUtxos koLength s) >> return ok
unless (koLength == 0) (logEvent $ MCLogDiscardedUtxos koLength s) >> return ok

-- | This computes the minimum and maximum possible fee a transaction can cost
-- based on the current protocol parameters
Expand Down
72 changes: 36 additions & 36 deletions src/Cooked/MockChain/BlockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,46 +87,46 @@ import PlutusLedgerApi.V3 qualified as Api
-- * MockChain errors

-- | The errors that can be produced by the 'MockChainT' monad
data MockChainError where
-- | Validation errors, either in Phase 1 or Phase 2
MCEValidationError :: Ledger.ValidationPhase -> Ledger.ValidationError -> MockChainError
-- | Thrown when the balancing wallet does not have enough funds
MCEUnbalanceable :: Wallet -> Api.Value -> TxSkel -> MockChainError
-- | Thrown when not enough collateral are provided. Built upon the fee, the
-- percentage and the expected minimal collateral value.
MCENoSuitableCollateral :: Integer -> Integer -> Api.Value -> MockChainError
-- | Thrown when an error occured during transaction generation
MCEGenerationError :: GenerateTxError -> MockChainError
-- | Thrown when an output reference is missing from the mockchain state
MCEUnknownOutRefError :: String -> Api.TxOutRef -> MockChainError
-- | Same as 'MCEUnknownOutRefError' for validators.
MCEUnknownValidator :: String -> Script.ValidatorHash -> MockChainError
-- | Same as 'MCEUnknownOutRefError' for datums.
MCEUnknownDatum :: String -> Api.DatumHash -> MockChainError
-- | Used to provide 'MonadFail' instances.
FailWith :: String -> MockChainError
data MockChainError
= -- | Validation errors, either in Phase 1 or Phase 2
MCEValidationError Ledger.ValidationPhase Ledger.ValidationError
| -- | Thrown when the balancing wallet does not have enough funds
MCEUnbalanceable Wallet Api.Value TxSkel
| -- | Thrown when not enough collateral are provided. Built upon the fee, the
-- percentage and the expected minimal collateral value.
MCENoSuitableCollateral Integer Integer Api.Value
| -- | Thrown when an error occured during transaction generation
MCEGenerationError GenerateTxError
| -- | Thrown when an output reference is missing from the mockchain state
MCEUnknownOutRefError String Api.TxOutRef
| -- | Same as 'MCEUnknownOutRefError' for validators.
MCEUnknownValidator String Script.ValidatorHash
| -- | Same as 'MCEUnknownOutRefError' for datums.
MCEUnknownDatum String Api.DatumHash
| -- | Used to provide 'MonadFail' instances.
FailWith String
deriving (Show, Eq)

-- * MockChain logs

-- | This represents the specific events that should be logged when processing
-- transactions. If a new kind of event arises, then a new constructor should be
-- provided here.
data MockChainLogEntry where
-- | Logging a Skeleton as it is submitted by the user.
MCLogSubmittedTxSkel :: SkelContext -> TxSkel -> MockChainLogEntry
-- | Logging a Skeleton as it has been adjusted by the balancing mechanism,
-- alongside fee, collateral utxos and return collateral wallet.
MCLogAdjustedTxSkel :: SkelContext -> TxSkel -> Integer -> Maybe (Set Api.TxOutRef, Wallet) -> MockChainLogEntry
-- | Logging the appearance of a new transaction, after a skeleton has been
-- successfully sent for validation.
MCLogNewTx :: Api.TxId -> MockChainLogEntry
-- | Logging the fact that utxos provided by the user for balancing have to be
-- discarded for a given reason.
MCLogDiscardedUtxos :: Integer -> String -> MockChainLogEntry
-- | Logging the fact that utxos provided as collaterals will not be used
-- because the transaction does not need involve scripts.
MCLogUnusedCollaterals :: Either Wallet (Set Api.TxOutRef) -> MockChainLogEntry
data MockChainLogEntry
= -- | Logging a Skeleton as it is submitted by the user.
MCLogSubmittedTxSkel SkelContext TxSkel
| -- | Logging a Skeleton as it has been adjusted by the balancing mechanism,
-- alongside fee, and posisble collateral utxos and return collateral wallet.
MCLogAdjustedTxSkel SkelContext TxSkel Integer (Maybe (Set Api.TxOutRef, Wallet))
| -- | Logging the appearance of a new transaction, after a skeleton has been
-- successfully sent for validation.
MCLogNewTx Api.TxId
| -- | Logging the fact that utxos provided by the user for balancing have to be
-- discarded for a specific reason.
MCLogDiscardedUtxos Integer String
| -- | Logging the fact that utxos provided as collaterals will not be used
-- because the transaction does not need involve scripts.
MCLogUnusedCollaterals (Either Wallet (Set Api.TxOutRef))

-- | Contains methods needed for balancing.
class (MonadFail m, MonadError MockChainError m) => MonadBlockChainBalancing m where
Expand All @@ -147,7 +147,7 @@ class (MonadFail m, MonadError MockChainError m) => MonadBlockChainBalancing m w
txOutByRef :: Api.TxOutRef -> m (Maybe Api.TxOut)

-- | Logs an event that occured during a BlockChain run
publish :: MockChainLogEntry -> m ()
logEvent :: MockChainLogEntry -> m ()

class (MonadBlockChainBalancing m) => MonadBlockChainWithoutValidation m where
-- | Returns a list of all currently known outputs.
Expand Down Expand Up @@ -493,7 +493,7 @@ instance (MonadTrans t, MonadBlockChainBalancing m, Monad (t m), MonadError Mock
utxosAt = lift . utxosAt
txOutByRef = lift . txOutByRef
datumFromHash = lift . datumFromHash
publish = lift . publish
logEvent = lift . logEvent

instance (MonadTrans t, MonadBlockChainWithoutValidation m, Monad (t m), MonadError MockChainError (AsTrans t m)) => MonadBlockChainWithoutValidation (AsTrans t m) where
allUtxos = lift allUtxos
Expand Down Expand Up @@ -537,7 +537,7 @@ instance (MonadBlockChainBalancing m) => MonadBlockChainBalancing (ListT m) wher
utxosAt = lift . utxosAt
txOutByRef = lift . txOutByRef
datumFromHash = lift . datumFromHash
publish = lift . publish
logEvent = lift . logEvent

instance (MonadBlockChainWithoutValidation m) => MonadBlockChainWithoutValidation (ListT m) where
allUtxos = lift allUtxos
Expand Down
8 changes: 4 additions & 4 deletions src/Cooked/MockChain/Direct.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ instance (Monad m) => MonadBlockChainBalancing (MockChainT m) where
txOutByRef outref = gets $ Map.lookup outref . getIndex . mcstIndex
datumFromHash datumHash = (txSkelOutUntypedDatum <=< Just . fst <=< Map.lookup datumHash) <$> gets mcstDatums
utxosAt addr = filter ((addr ==) . outputAddress . snd) <$> allUtxos
publish l = tell [l]
logEvent l = tell [l]

instance (Monad m) => MonadBlockChainWithoutValidation (MockChainT m) where
allUtxos = gets $ Map.toList . getIndex . mcstIndex
Expand All @@ -139,7 +139,7 @@ instance (Monad m) => MonadBlockChainWithoutValidation (MockChainT m) where
instance (Monad m) => MonadBlockChain (MockChainT m) where
validateTxSkel skelUnbal = do
-- We log the submitted skeleton
gets mcstToSkelContext >>= publish . (`MCLogSubmittedTxSkel` skelUnbal)
gets mcstToSkelContext >>= logEvent . (`MCLogSubmittedTxSkel` skelUnbal)
-- We retrieve the current parameters
oldParams <- getParams
-- We compute the optionally modified parameters
Expand All @@ -153,7 +153,7 @@ instance (Monad m) => MonadBlockChain (MockChainT m) where
-- the associated fee, collateral inputs and return collateral wallet
(skel, fee, mCollaterals) <- balanceTxSkel minAdaSkelUnbal
-- We log the adjusted skeleton
gets mcstToSkelContext >>= \ctx -> publish $ MCLogAdjustedTxSkel ctx skel fee mCollaterals
gets mcstToSkelContext >>= \ctx -> logEvent $ MCLogAdjustedTxSkel ctx skel fee mCollaterals
-- 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.
Expand Down Expand Up @@ -208,6 +208,6 @@ instance (Monad m) => MonadBlockChain (MockChainT m) where
-- We return the parameters to their original state
setParams oldParams
-- We log the validated transaction
publish $ MCLogNewTx (Ledger.fromCardanoTxId $ Ledger.getCardanoTxId cardanoTx)
logEvent $ MCLogNewTx (Ledger.fromCardanoTxId $ Ledger.getCardanoTxId cardanoTx)
-- We return the validated transaction
return cardanoTx
6 changes: 3 additions & 3 deletions src/Cooked/MockChain/Staged.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ data MockChainBuiltin a where
AllUtxos :: MockChainBuiltin [(Api.TxOutRef, Api.TxOut)]
UtxosAt :: Api.Address -> MockChainBuiltin [(Api.TxOutRef, Api.TxOut)]
ValidatorFromHash :: Script.ValidatorHash -> MockChainBuiltin (Maybe (Script.Versioned Script.Validator))
Publish :: MockChainLogEntry -> MockChainBuiltin ()
LogEvent :: MockChainLogEntry -> MockChainBuiltin ()
-- | The empty set of traces
Empty :: MockChainBuiltin a
-- | The union of two sets of traces
Expand Down Expand Up @@ -137,7 +137,7 @@ instance InterpLtl (UntypedTweak InterpMockChain) MockChainBuiltin InterpMockCha
interpBuiltin (Fail msg) = fail msg
interpBuiltin (ThrowError err) = throwError err
interpBuiltin (CatchError act handler) = catchError (interpLtl act) (interpLtl . handler)
interpBuiltin (Publish entry) = publish entry
interpBuiltin (LogEvent entry) = logEvent entry

-- ** Helpers to run tweaks for use in tests for tweaks

Expand Down Expand Up @@ -202,7 +202,7 @@ instance MonadBlockChainBalancing StagedMockChain where
txOutByRef = singletonBuiltin . TxOutByRef
utxosAt = singletonBuiltin . UtxosAt
validatorFromHash = singletonBuiltin . ValidatorFromHash
publish = singletonBuiltin . Publish
logEvent = singletonBuiltin . LogEvent

instance MonadBlockChainWithoutValidation StagedMockChain where
allUtxos = singletonBuiltin AllUtxos
Expand Down

0 comments on commit 1554e51

Please sign in to comment.