Skip to content

Commit

Permalink
ormoluifying to the new version
Browse files Browse the repository at this point in the history
  • Loading branch information
mmontin committed Mar 13, 2024
1 parent 0b4cdbe commit 854f913
Show file tree
Hide file tree
Showing 29 changed files with 174 additions and 174 deletions.
2 changes: 1 addition & 1 deletion src/Cooked/Attack/AddToken.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import qualified PlutusTx.Numeric as Pl
-- This attack adds an 'AddTokenLbl' with the token name of the additional
-- minted token(s). It returns additional value minted.
addTokenAttack ::
MonadTweak m =>
(MonadTweak m) =>
-- | For each policy that occurs in some 'Mints' constraint, return a list of
-- token names together with how many tokens with that name should be
-- minted.
Expand Down
4 changes: 2 additions & 2 deletions src/Cooked/Attack/DoubleSat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ doubleSatAttack groupings optic change attacker = do
addLabelTweak DoubleSatLbl
where
-- for each triple of additional inputs, outputs, and mints, calculate its balance
deltaBalance :: MonadTweak m => DoubleSatDelta -> m Pl.Value
deltaBalance :: (MonadTweak m) => DoubleSatDelta -> m Pl.Value
deltaBalance (inputs, outputs, mints) = do
inValue <- foldMap (outputValue . snd) . filter ((`elem` Map.keys inputs) . fst) <$> allUtxos
return $ inValue <> Pl.negate outValue <> mintValue
Expand All @@ -131,7 +131,7 @@ doubleSatAttack groupings optic change attacker = do
mintValue = txSkelMintsValue mints

-- Helper tweak to add a 'DoubleSatDelta' to a transaction
addDoubleSatDeltaTweak :: MonadTweak m => DoubleSatDelta -> m ()
addDoubleSatDeltaTweak :: (MonadTweak m) => DoubleSatDelta -> m ()
addDoubleSatDeltaTweak (ins, outs, mints) =
mapM_ (uncurry addInputTweak) (Map.toList ins)
>> mapM_ addOutputTweak outs
Expand Down
4 changes: 2 additions & 2 deletions src/Cooked/Attack/DupToken.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import qualified PlutusTx.Numeric as Pl
-- transaction using 'addLabel'. Returns the 'Value' by which the minted value
-- was increased.
dupTokenAttack ::
MonadTweak m =>
(MonadTweak m) =>
-- | A function describing how the amount of tokens specified by a 'Mints'
-- constraint should be changed, depending on the asset class and the amount
-- specified by the constraint. The given function @f@ should probably satisfy
Expand All @@ -37,7 +37,7 @@ dupTokenAttack change attacker = do
addLabelTweak DupTokenLbl
return totalIncrement
where
changeMintAmountsTweak :: MonadTweak m => m Pl.Value
changeMintAmountsTweak :: (MonadTweak m) => m Pl.Value
changeMintAmountsTweak = do
oldMintsList <- viewTweak $ txSkelMintsL % to txSkelMintsToList
let newMintsList =
Expand Down
8 changes: 4 additions & 4 deletions src/Cooked/Ltl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ data Ltl a
-- <> b@ as the modification that first applies @b@ and then @a@. Attention:
-- Since we use '<>' to define conjunction, if '<>' is not commutative,
-- conjunction will also fail to be commutative!
nowLater :: Monoid a => Ltl a -> [(a, Ltl a)]
nowLater :: (Monoid a) => Ltl a -> [(a, Ltl a)]
nowLater LtlTruth = [(mempty, LtlTruth)]
nowLater LtlFalsity = []
nowLater (LtlAtom g) = [(g, LtlTruth)]
Expand Down Expand Up @@ -120,7 +120,7 @@ finished (LtlRelease _ _) = True
-- then the third and so on. We'd still like to compute a list of @(doNow,
-- doLater)@ pairs as in 'nowLater', only that the @doLater@ should again be a
-- list of formulas.
nowLaterList :: Monoid a => [Ltl a] -> [(a, [Ltl a])]
nowLaterList :: (Monoid a) => [Ltl a] -> [(a, [Ltl a])]
nowLaterList = joinNowLaters . map nowLater
where
joinNowLaters [] = [(mempty, [])]
Expand Down Expand Up @@ -251,7 +251,7 @@ instance Monad (Staged op) where
-- (But to write this, @modification@ has to be a 'Monoid' to make
-- 'nowLaterList' work!) Look at the tests for this module and at
-- "Cooked.MockChain.Monad.Staged" for examples of how to use this type class.
class MonadPlus m => InterpLtl modification builtin m where
class (MonadPlus m) => InterpLtl modification builtin m where
interpBuiltin :: builtin a -> StateT [Ltl modification] m a

-- | Interpret a 'Staged' computation into a suitable domain, using the function
Expand Down Expand Up @@ -297,7 +297,7 @@ interpLtlAndPruneUnfinished f = do
-- LTL modifications beside the method above.

-- | Monads that allow modificaitons with LTL formulas.
class Monad m => MonadModal m where
class (Monad m) => MonadModal m where
type Modification m :: Type
modifyLtl :: Ltl (Modification m) -> m a -> m a

Expand Down
36 changes: 18 additions & 18 deletions src/Cooked/MockChain/Balancing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import qualified Plutus.Script.Utils.Value as Pl
import qualified Plutus.V2.Ledger.Api as PV2
import qualified PlutusTx.Numeric as Pl

balancedTxSkel :: MonadBlockChainBalancing m => TxSkel -> m (TxSkel, Fee, Set PV2.TxOutRef)
balancedTxSkel :: (MonadBlockChainBalancing m) => TxSkel -> m (TxSkel, Fee, Set PV2.TxOutRef)
balancedTxSkel skelUnbal = do
let balancingWallet =
case txOptBalanceWallet . txSkelOpts $ skelUnbal of
Expand All @@ -63,7 +63,7 @@ balancedTxSkel skelUnbal = do

-- | Take the output of 'balancedTxSkel' and turn it into an actual Cardano
-- transaction.
balancedTx :: MonadBlockChainBalancing m => (TxSkel, Fee, Set PV2.TxOutRef) -> m (C.Tx C.BabbageEra)
balancedTx :: (MonadBlockChainBalancing m) => (TxSkel, Fee, Set PV2.TxOutRef) -> m (C.Tx C.BabbageEra)
balancedTx (skel, fee, collateralInputs) = do
params <- applyEmulatorParamsModification (txOptEmulatorParamsModification . txSkelOpts $ skel) <$> getParams
consumedData <- txSkelInputData skel
Expand All @@ -89,7 +89,7 @@ balancedTx (skel, fee, collateralInputs) = do
-- | Ensure that the transaction outputs have the necessary minimum amount of
-- Ada on them. This will only be applied if the 'txOptEnsureMinAda' is set to
-- @True@.
ensureTxSkelOutsMinAda :: MonadBlockChainBalancing m => TxSkel -> m TxSkel
ensureTxSkelOutsMinAda :: (MonadBlockChainBalancing m) => TxSkel -> m TxSkel
ensureTxSkelOutsMinAda skel = do
theParams <- applyEmulatorParamsModification (txOptEmulatorParamsModification . txSkelOpts $ skel) <$> getParams
case mapM (ensureTxSkelOutHasMinAda theParams) $ skel ^. txSkelOutsL of
Expand All @@ -116,16 +116,16 @@ ensureTxSkelOutsMinAda skel = do
then ensureTxSkelOutHasMinAda theParams updatedTxSkelOut
else return txSkelOut

txSkelInputUtxosPV2 :: MonadBlockChainBalancing m => TxSkel -> m (Map PV2.TxOutRef PV2.TxOut)
txSkelInputUtxosPV2 :: (MonadBlockChainBalancing m) => TxSkel -> m (Map PV2.TxOutRef PV2.TxOut)
txSkelInputUtxosPV2 = lookupUtxosPV2 . Map.keys . txSkelIns

txSkelInputUtxos :: MonadBlockChainBalancing m => TxSkel -> m (Map PV2.TxOutRef Ledger.TxOut)
txSkelInputUtxos :: (MonadBlockChainBalancing m) => TxSkel -> m (Map PV2.TxOutRef Ledger.TxOut)
txSkelInputUtxos = lookupUtxos . Map.keys . txSkelIns

txSkelReferenceInputUtxosPV2 :: MonadBlockChainBalancing m => TxSkel -> m (Map PV2.TxOutRef PV2.TxOut)
txSkelReferenceInputUtxosPV2 :: (MonadBlockChainBalancing m) => TxSkel -> m (Map PV2.TxOutRef PV2.TxOut)
txSkelReferenceInputUtxosPV2 skel = Map.map txOutV2FromLedger <$> txSkelReferenceInputUtxos skel

txSkelReferenceInputUtxos :: MonadBlockChainBalancing m => TxSkel -> m (Map PV2.TxOutRef Ledger.TxOut)
txSkelReferenceInputUtxos :: (MonadBlockChainBalancing m) => TxSkel -> m (Map PV2.TxOutRef Ledger.TxOut)
txSkelReferenceInputUtxos skel =
lookupUtxos $
mapMaybe
Expand All @@ -137,7 +137,7 @@ txSkelReferenceInputUtxos skel =
++ (Set.toList . txSkelInsReference $ skel)

-- | All validators which protect transaction inputs
txSkelInputValidators :: MonadBlockChainBalancing m => TxSkel -> m (Map PV2.ValidatorHash (Pl.Versioned PV2.Validator))
txSkelInputValidators :: (MonadBlockChainBalancing m) => TxSkel -> m (Map PV2.ValidatorHash (Pl.Versioned PV2.Validator))
txSkelInputValidators skel = do
utxos <- Map.toList <$> lookupUtxosPV2 (Map.keys . txSkelIns $ skel)
mValidators <-
Expand All @@ -160,10 +160,10 @@ txSkelInputValidators skel = do
-- Go through all of the 'PV2.TxOutRef's in the list and look them up in the
-- state of the blockchain. If any 'PV2.TxOutRef' can't be resolved, throw an
-- error.
lookupUtxosPV2 :: MonadBlockChainBalancing m => [PV2.TxOutRef] -> m (Map PV2.TxOutRef PV2.TxOut)
lookupUtxosPV2 :: (MonadBlockChainBalancing m) => [PV2.TxOutRef] -> m (Map PV2.TxOutRef PV2.TxOut)
lookupUtxosPV2 outRefs = Map.map txOutV2FromLedger <$> lookupUtxos outRefs

lookupUtxos :: MonadBlockChainBalancing m => [PV2.TxOutRef] -> m (Map PV2.TxOutRef Ledger.TxOut)
lookupUtxos :: (MonadBlockChainBalancing m) => [PV2.TxOutRef] -> m (Map PV2.TxOutRef Ledger.TxOut)
lookupUtxos outRefs = do
Map.fromList
<$> mapM
Expand All @@ -182,13 +182,13 @@ lookupUtxos outRefs = do

-- | look up the UTxOs the transaction consumes, and sum the value contained in
-- them.
txSkelInputValue :: MonadBlockChainBalancing m => TxSkel -> m PV2.Value
txSkelInputValue :: (MonadBlockChainBalancing m) => TxSkel -> m PV2.Value
txSkelInputValue skel = do
txSkelInputs <- txSkelInputUtxos skel
return $ foldMap (PV2.txOutValue . txOutV2FromLedger) txSkelInputs

-- | Look up the data on UTxOs the transaction consumes.
txSkelInputData :: MonadBlockChainBalancing m => TxSkel -> m (Map PV2.DatumHash PV2.Datum)
txSkelInputData :: (MonadBlockChainBalancing m) => TxSkel -> m (Map PV2.DatumHash PV2.Datum)
txSkelInputData skel = do
txSkelInputs <- Map.elems <$> txSkelInputUtxosPV2 skel
mDatums <-
Expand All @@ -205,7 +205,7 @@ txSkelInputData skel = do
txSkelInputs
return . Map.fromList . catMaybes $ mDatums
where
datumFromHashWithError :: MonadBlockChainBalancing m => Pl.DatumHash -> m PV2.Datum
datumFromHashWithError :: (MonadBlockChainBalancing m) => Pl.DatumHash -> m PV2.Datum
datumFromHashWithError dHash = do
mDatum <- datumFromHash dHash
case mDatum of
Expand Down Expand Up @@ -237,7 +237,7 @@ getEmulatorUTxO m =
--
-- This function also adjusts the transaction outputs to contain at least the
-- minimum Ada amount, if the 'txOptEnsureMinAda option is @True@.
setFeeAndBalance :: MonadBlockChainBalancing m => Wallet -> TxSkel -> m (TxSkel, Fee)
setFeeAndBalance :: (MonadBlockChainBalancing m) => Wallet -> TxSkel -> m (TxSkel, Fee)
setFeeAndBalance balanceWallet skel0 = do
-- do the min Ada adjustment if it's requested
skel <-
Expand Down Expand Up @@ -287,7 +287,7 @@ setFeeAndBalance balanceWallet skel0 = do
-- Inspired by https://github.com/input-output-hk/plutus-apps/blob/d4255f05477fd8477ee9673e850ebb9ebb8c9657/plutus-contract/src/Wallet/Emulator/Wallet.hs#L329

calcFee ::
MonadBlockChainBalancing m =>
(MonadBlockChainBalancing m) =>
Int ->
Fee ->
Emulator.UTxO Emulator.EmulatorEra ->
Expand Down Expand Up @@ -346,7 +346,7 @@ estimateTxSkelFee params cUtxoIndex managedData managedTxOuts managedValidators
C.Lovelace fee -> pure $ Fee fee

-- | Calculates the collateral for a transaction
calcCollateral :: MonadBlockChainBalancing m => Wallet -> m (Set PV2.TxOutRef)
calcCollateral :: (MonadBlockChainBalancing m) => Wallet -> m (Set PV2.TxOutRef)
calcCollateral w = do
souts <-
runUtxoSearch $
Expand All @@ -360,7 +360,7 @@ calcCollateral w = do
-- investigated further for a better approach?
return $ Set.fromList $ take 1 (fst <$> souts)

balanceTxFromAux :: MonadBlockChainBalancing m => Wallet -> TxSkel -> Fee -> m TxSkel
balanceTxFromAux :: (MonadBlockChainBalancing m) => Wallet -> TxSkel -> Fee -> m TxSkel
balanceTxFromAux balanceWallet txskel fee = do
bres@(BalanceTxRes {newInputs, returnValue, availableUtxos}) <- calcBalanceTx balanceWallet txskel fee
case applyBalanceTx (walletPKHash balanceWallet) bres txskel of
Expand Down Expand Up @@ -397,7 +397,7 @@ data BalanceTxRes = BalanceTxRes
-- | Calculate the changes needed to balance a transaction with money from a
-- given wallet. Every transaction that is sent to the chain must be balanced,
-- that is: @inputs + mints == outputs + fee + burns@.
calcBalanceTx :: MonadBlockChainBalancing m => Wallet -> TxSkel -> Fee -> m BalanceTxRes
calcBalanceTx :: (MonadBlockChainBalancing m) => Wallet -> TxSkel -> Fee -> m BalanceTxRes
calcBalanceTx balanceWallet skel fee = do
inValue <- (<> positivePart (txSkelMintsValue $ txSkelMints skel)) <$> txSkelInputValue skel -- transaction inputs + minted value
let outValue = txSkelOutputValue skel fee -- transaction outputs + fee + burned value
Expand Down
38 changes: 19 additions & 19 deletions src/Cooked/MockChain/BlockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class (MonadFail m, MonadError MockChainError m) => MonadBlockChainBalancing m w
-- | Returns an output given a reference to it
txOutByRefLedger :: PV2.TxOutRef -> m (Maybe Ledger.TxOut)

class MonadBlockChainBalancing m => MonadBlockChainWithoutValidation m where
class (MonadBlockChainBalancing m) => MonadBlockChainWithoutValidation m where
-- | Returns a list of all currently known outputs.
allUtxosLedger :: m [(PV2.TxOutRef, Ledger.TxOut)]

Expand All @@ -137,7 +137,7 @@ class MonadBlockChainBalancing m => MonadBlockChainWithoutValidation m where
awaitSlot :: Ledger.Slot -> m Ledger.Slot

-- | The main abstraction of the blockchain.
class MonadBlockChainWithoutValidation m => MonadBlockChain m where
class (MonadBlockChainWithoutValidation m) => MonadBlockChain m where
-- | Generates, balances and validates a transaction from a skeleton.
-- It returns the validated transaction and updates the state of the
-- blockchain. In 'MockChainT', this means:
Expand All @@ -148,13 +148,13 @@ class MonadBlockChainWithoutValidation m => MonadBlockChain m where
-- - adds the validators on outputs to the 'mcstValidators'.
validateTxSkel :: TxSkel -> m Ledger.CardanoTx

allUtxos :: MonadBlockChainWithoutValidation m => m [(PV2.TxOutRef, PV2.TxOut)]
allUtxos :: (MonadBlockChainWithoutValidation m) => m [(PV2.TxOutRef, PV2.TxOut)]
allUtxos = fmap (second txOutV2FromLedger) <$> allUtxosLedger

utxosAt :: MonadBlockChainBalancing m => PV2.Address -> m [(PV2.TxOutRef, PV2.TxOut)]
utxosAt :: (MonadBlockChainBalancing m) => PV2.Address -> m [(PV2.TxOutRef, PV2.TxOut)]
utxosAt address = fmap (second txOutV2FromLedger) <$> utxosAtLedger address

txOutByRef :: MonadBlockChainBalancing m => PV2.TxOutRef -> m (Maybe PV2.TxOut)
txOutByRef :: (MonadBlockChainBalancing m) => PV2.TxOutRef -> m (Maybe PV2.TxOut)
txOutByRef oref = fmap txOutV2FromLedger <$> txOutByRefLedger oref

-- | Retrieve the ordered list of outputs of the given "CardanoTx".
Expand Down Expand Up @@ -279,14 +279,14 @@ resolveReferenceScript out =
(out ^. outputDatumL)
(Just val)

outputDatumFromTxOutRef :: MonadBlockChainWithoutValidation m => PV2.TxOutRef -> m (Maybe PV2.OutputDatum)
outputDatumFromTxOutRef :: (MonadBlockChainWithoutValidation m) => PV2.TxOutRef -> m (Maybe PV2.OutputDatum)
outputDatumFromTxOutRef oref = do
mOut <- txOutByRef oref
case mOut of
Nothing -> return Nothing
Just out -> return . Just $ outputOutputDatum out

datumFromTxOutRef :: MonadBlockChainWithoutValidation m => PV2.TxOutRef -> m (Maybe PV2.Datum)
datumFromTxOutRef :: (MonadBlockChainWithoutValidation m) => PV2.TxOutRef -> m (Maybe PV2.Datum)
datumFromTxOutRef oref = do
mOutputDatum <- outputDatumFromTxOutRef oref
case mOutputDatum of
Expand All @@ -306,7 +306,7 @@ typedDatumFromTxOutRef oref = do
Nothing -> return Nothing
Just (PV2.Datum datum) -> return $ PV2.fromBuiltinData datum

valueFromTxOutRef :: MonadBlockChainWithoutValidation m => PV2.TxOutRef -> m (Maybe PV2.Value)
valueFromTxOutRef :: (MonadBlockChainWithoutValidation m) => PV2.TxOutRef -> m (Maybe PV2.Value)
valueFromTxOutRef oref = do
mOut <- txOutByRef oref
case mOut of
Expand Down Expand Up @@ -373,7 +373,7 @@ awaitEnclosingSlot :: (MonadBlockChainWithoutValidation m) => PV2.POSIXTime -> m
awaitEnclosingSlot = awaitSlot <=< getEnclosingSlot

-- | The infinite range of slots ending before or at the given POSIX time
slotRangeBefore :: MonadBlockChainWithoutValidation m => PV2.POSIXTime -> m Ledger.SlotRange
slotRangeBefore :: (MonadBlockChainWithoutValidation m) => PV2.POSIXTime -> m Ledger.SlotRange
slotRangeBefore t = do
n <- getEnclosingSlot t
(_, b) <- slotToTimeInterval n
Expand All @@ -386,7 +386,7 @@ slotRangeBefore t = do
else return $ PV2.to (n - 1)

-- | The infinite range of slots starting after or at the given POSIX time
slotRangeAfter :: MonadBlockChainWithoutValidation m => PV2.POSIXTime -> m Ledger.SlotRange
slotRangeAfter :: (MonadBlockChainWithoutValidation m) => PV2.POSIXTime -> m Ledger.SlotRange
slotRangeAfter t = do
n <- getEnclosingSlot t
(a, _) <- slotToTimeInterval n
Expand Down Expand Up @@ -435,17 +435,17 @@ deriving via (AsTrans (WriterT w) m) instance (Monoid w, MonadBlockChainWithoutV

deriving via (AsTrans (WriterT w) m) instance (Monoid w, MonadBlockChain m) => MonadBlockChain (WriterT w m)

deriving via (AsTrans (ReaderT r) m) instance MonadBlockChainBalancing m => MonadBlockChainBalancing (ReaderT r m)
deriving via (AsTrans (ReaderT r) m) instance (MonadBlockChainBalancing m) => MonadBlockChainBalancing (ReaderT r m)

deriving via (AsTrans (ReaderT r) m) instance MonadBlockChainWithoutValidation m => MonadBlockChainWithoutValidation (ReaderT r m)
deriving via (AsTrans (ReaderT r) m) instance (MonadBlockChainWithoutValidation m) => MonadBlockChainWithoutValidation (ReaderT r m)

deriving via (AsTrans (ReaderT r) m) instance MonadBlockChain m => MonadBlockChain (ReaderT r m)
deriving via (AsTrans (ReaderT r) m) instance (MonadBlockChain m) => MonadBlockChain (ReaderT r m)

deriving via (AsTrans (StateT s) m) instance MonadBlockChainBalancing m => MonadBlockChainBalancing (StateT s m)
deriving via (AsTrans (StateT s) m) instance (MonadBlockChainBalancing m) => MonadBlockChainBalancing (StateT s m)

deriving via (AsTrans (StateT s) m) instance MonadBlockChainWithoutValidation m => MonadBlockChainWithoutValidation (StateT s m)
deriving via (AsTrans (StateT s) m) instance (MonadBlockChainWithoutValidation m) => MonadBlockChainWithoutValidation (StateT s m)

deriving via (AsTrans (StateT s) m) instance MonadBlockChain m => MonadBlockChain (StateT s m)
deriving via (AsTrans (StateT s) m) instance (MonadBlockChain m) => MonadBlockChain (StateT s m)

-- 'ListT' has no 'MonadTransControl' instance, so the @deriving via ...@
-- machinery is unusable here. However, there is
Expand All @@ -456,17 +456,17 @@ deriving via (AsTrans (StateT s) m) instance MonadBlockChain m => MonadBlockChai
-- 'MonadBlockChainWithoutValidation' and 'MonadBlockChain' instances for
-- 'ListT', instead of more black magic...

instance MonadBlockChainBalancing m => MonadBlockChainBalancing (ListT m) where
instance (MonadBlockChainBalancing m) => MonadBlockChainBalancing (ListT m) where
getParams = lift getParams
validatorFromHash = lift . validatorFromHash
utxosAtLedger = lift . utxosAtLedger
txOutByRefLedger = lift . txOutByRefLedger
datumFromHash = lift . datumFromHash

instance MonadBlockChainWithoutValidation m => MonadBlockChainWithoutValidation (ListT m) where
instance (MonadBlockChainWithoutValidation m) => MonadBlockChainWithoutValidation (ListT m) where
allUtxosLedger = lift allUtxosLedger
currentSlot = lift currentSlot
awaitSlot = lift . awaitSlot

instance MonadBlockChain m => MonadBlockChain (ListT m) where
instance (MonadBlockChain m) => MonadBlockChain (ListT m) where
validateTxSkel = lift . validateTxSkel
Loading

0 comments on commit 854f913

Please sign in to comment.