Skip to content

Commit

Permalink
Merge pull request #1446 from input-output-hk/lc/multiple-errors-in-m…
Browse files Browse the repository at this point in the history
…utation-tests

Allow mutation tests to expect one of many error codes.
  • Loading branch information
locallycompact committed May 22, 2024
2 parents 95dcae7 + 90e18c4 commit d82a44f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 92 deletions.
26 changes: 13 additions & 13 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Abort.hs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ data AbortMutation
genAbortMutation :: (Tx, UTxO) -> Gen SomeMutation
genAbortMutation (tx, utxo) =
oneof
[ SomeMutation (Just $ toErrorCode BurntTokenNumberMismatch) MutateParties . ChangeInputHeadDatum <$> do
[ SomeMutation (pure $ toErrorCode BurntTokenNumberMismatch) MutateParties . ChangeInputHeadDatum <$> do
moreParties <- (: healthyParties) <$> arbitrary
c <- arbitrary
pure $
Expand All @@ -170,21 +170,21 @@ genAbortMutation (tx, utxo) =
(partyToChain <$> moreParties)
(toPlutusCurrencySymbol $ headPolicyId testSeedInput)
(toPlutusTxOutRef testSeedInput)
, SomeMutation (Just $ toErrorCode BurntTokenNumberMismatch) DropCollectedInput <$> do
, SomeMutation (pure $ toErrorCode BurntTokenNumberMismatch) DropCollectedInput <$> do
let abortableInputs = UTxO.pairs $ UTxO.filter (not . isHeadOutput) (resolveInputsUTxO utxo tx)
(toDropTxIn, toDropTxOut) <- elements abortableInputs
pure $
Changes
[ RemoveInput toDropTxIn
, ChangeMintedValue $ removePTFromMintedValue toDropTxOut tx
]
, SomeMutation (Just $ toErrorCode ReimbursedOutputsDontMatch) DropOneCommitOutput . RemoveOutput <$> choose (0, fromIntegral (length (txOuts' tx) - 1))
, SomeMutation (Just $ toErrorCode BurntTokenNumberMismatch) MutateThreadTokenQuantity <$> changeMintedValueQuantityFrom tx (-1)
, SomeMutation (Just $ toErrorCode BurntTokenNumberMismatch) BurnOneTokenMore <$> addPTWithQuantity tx (-1)
, SomeMutation (Just $ toErrorCode SignerIsNotAParticipant) MutateRequiredSigner <$> do
, SomeMutation (pure $ toErrorCode ReimbursedOutputsDontMatch) DropOneCommitOutput . RemoveOutput <$> choose (0, fromIntegral (length (txOuts' tx) - 1))
, SomeMutation (pure $ toErrorCode BurntTokenNumberMismatch) MutateThreadTokenQuantity <$> changeMintedValueQuantityFrom tx (-1)
, SomeMutation (pure $ toErrorCode BurntTokenNumberMismatch) BurnOneTokenMore <$> addPTWithQuantity tx (-1)
, SomeMutation (pure $ toErrorCode SignerIsNotAParticipant) MutateRequiredSigner <$> do
newSigner <- verificationKeyHash <$> genVerificationKey
pure $ ChangeRequiredSigners [newSigner]
, SomeMutation (Just $ toErrorCode BurntTokenNumberMismatch) MutateUseDifferentHeadToAbort <$> do
, SomeMutation (pure $ toErrorCode BurntTokenNumberMismatch) MutateUseDifferentHeadToAbort <$> do
mutatedSeed <- arbitrary `suchThat` (/= testSeedInput)
pure $
ChangeInputHeadDatum
Expand All @@ -194,7 +194,7 @@ genAbortMutation (tx, utxo) =
, Head.headId = toPlutusCurrencySymbol $ headPolicyId mutatedSeed
, Head.seed = toPlutusTxOutRef mutatedSeed
}
, SomeMutation (Just $ toErrorCode BurntTokenNumberMismatch) UseInputFromOtherHead <$> do
, SomeMutation (pure $ toErrorCode BurntTokenNumberMismatch) UseInputFromOtherHead <$> do
(txIn, txOut) <- elements healthyInitials
otherHeadId <- fmap headPolicyId (arbitrary `suchThat` (/= testSeedInput))
pure $
Expand All @@ -205,12 +205,12 @@ genAbortMutation (tx, utxo) =
ChangeInput txIn (replacePolicyIdWith testPolicyId otherHeadId txOut) (Just $ toScriptData Initial.ViaAbort)
, ChangeMintedValue (removePTFromMintedValue txOut tx)
]
, SomeMutation (Just $ toErrorCode ReimbursedOutputsDontMatch) ReorderCommitOutputs <$> do
, SomeMutation (pure $ toErrorCode ReimbursedOutputsDontMatch) ReorderCommitOutputs <$> do
let outputs = txOuts' tx
outputs' <- shuffle outputs `suchThat` (/= outputs)
let reorderedOutputs = uncurry ChangeOutput <$> zip [0 ..] outputs'
pure $ Changes reorderedOutputs
, SomeMutation (Just $ toErrorCode MintingNotAllowed) MintOnAbort <$> do
, SomeMutation (pure $ toErrorCode MintingNotAllowed) MintOnAbort <$> do
mintAPT <- addPTWithQuantity tx 1
-- We need to also remove one party to make sure the vHead validator
-- still thinks it's the right number of tokens getting burned.
Expand All @@ -224,7 +224,7 @@ genAbortMutation (tx, utxo) =
, Head.seed = toPlutusTxOutRef testSeedInput
}
pure $ Changes [mintAPT, removeOneParty]
, SomeMutation Nothing ExtractValue <$> do
, SomeMutation [] ExtractValue <$> do
divertFunds <- do
let allValue = foldMap txOutValue $ txOuts' tx
extractionTxOut <- do
Expand All @@ -242,8 +242,8 @@ genAbortMutation (tx, utxo) =
, RemoveInput healthyHeadInput
]
++ divertFunds
, SomeMutation (Just $ toErrorCode STNotBurnedError) DoNotBurnST
, SomeMutation (pure $ toErrorCode STNotBurnedError) DoNotBurnST
<$> changeMintedTokens tx (valueFromList [(AssetId (headPolicyId testSeedInput) hydraHeadV1AssetName, 1)])
, SomeMutation (Just $ toErrorCode STNotBurned) DoNotBurnSTInitial
, SomeMutation (pure $ toErrorCode STNotBurned) DoNotBurnSTInitial
<$> changeMintedTokens tx (valueFromList [(AssetId (headPolicyId testSeedInput) hydraHeadV1AssetName, 1)])
]
42 changes: 21 additions & 21 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Close.hs
Original file line number Diff line number Diff line change
Expand Up @@ -291,18 +291,18 @@ data CloseMutation
genCloseMutation :: (Tx, UTxO) -> Gen SomeMutation
genCloseMutation (tx, _utxo) =
oneof
[ SomeMutation (Just $ toErrorCode NotPayingToHead) NotContinueContract <$> do
[ SomeMutation (pure $ toErrorCode NotPayingToHead) NotContinueContract <$> do
mutatedAddress <- genAddressInEra Fixture.testNetworkId
pure $ ChangeOutput 0 (modifyTxOutAddress (const mutatedAddress) headTxOut)
, SomeMutation (Just $ toErrorCode SignatureVerificationFailed) MutateSignatureButNotSnapshotNumber . ChangeHeadRedeemer <$> do
, SomeMutation (pure $ toErrorCode SignatureVerificationFailed) MutateSignatureButNotSnapshotNumber . ChangeHeadRedeemer <$> do
Head.Close . toPlutusSignatures <$> (arbitrary :: Gen (MultiSignature (Snapshot Tx)))
, SomeMutation (Just $ toErrorCode ClosedWithNonInitialHash) MutateSnapshotNumberToLessThanEqualZero <$> do
, SomeMutation (pure $ toErrorCode ClosedWithNonInitialHash) MutateSnapshotNumberToLessThanEqualZero <$> do
mutatedSnapshotNumber <- arbitrary `suchThat` (<= 0)
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceSnapshotNumber mutatedSnapshotNumber) headTxOut
, SomeMutation (Just $ toErrorCode SignatureVerificationFailed) MutateSnapshotNumberButNotSignature <$> do
, SomeMutation (pure $ toErrorCode SignatureVerificationFailed) MutateSnapshotNumberButNotSignature <$> do
mutatedSnapshotNumber <- arbitrarySizedNatural `suchThat` (> healthyCloseSnapshotNumber)
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceSnapshotNumber $ toInteger mutatedSnapshotNumber) headTxOut
, SomeMutation (Just $ toErrorCode SignatureVerificationFailed) SnapshotNotSignedByAllParties . ChangeInputHeadDatum <$> do
, SomeMutation (pure $ toErrorCode SignatureVerificationFailed) SnapshotNotSignedByAllParties . ChangeInputHeadDatum <$> do
mutatedParties <- arbitrary `suchThat` (/= healthyOnChainParties)
pure $
Head.Open
Expand All @@ -311,35 +311,35 @@ genCloseMutation (tx, _utxo) =
, contestationPeriod = healthyContestationPeriod
, headId = toPlutusCurrencySymbol Fixture.testPolicyId
}
, SomeMutation (Just $ toErrorCode ChangedParameters) MutatePartiesInOutput <$> do
, SomeMutation (pure $ toErrorCode ChangedParameters) MutatePartiesInOutput <$> do
mutatedParties <- arbitrary `suchThat` (/= healthyOnChainParties)
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceParties mutatedParties) headTxOut
, SomeMutation (Just $ toErrorCode ChangedParameters) MutateHeadIdInOutput <$> do
, SomeMutation (pure $ toErrorCode ChangedParameters) MutateHeadIdInOutput <$> do
otherHeadId <- toPlutusCurrencySymbol . headPolicyId <$> arbitrary `suchThat` (/= Fixture.testSeedInput)
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceHeadId otherHeadId) headTxOut
, SomeMutation (Just $ toErrorCode SignerIsNotAParticipant) MutateRequiredSigner <$> do
, SomeMutation (pure $ toErrorCode SignerIsNotAParticipant) MutateRequiredSigner <$> do
newSigner <- verificationKeyHash <$> genVerificationKey `suchThat` (/= somePartyCardanoVerificationKey)
pure $ ChangeRequiredSigners [newSigner]
, SomeMutation (Just $ toErrorCode NoSigners) MutateNoRequiredSigner <$> do
, SomeMutation (pure $ toErrorCode NoSigners) MutateNoRequiredSigner <$> do
pure $ ChangeRequiredSigners []
, SomeMutation (Just $ toErrorCode TooManySigners) MutateMultipleRequiredSigner <$> do
, SomeMutation (pure $ toErrorCode TooManySigners) MutateMultipleRequiredSigner <$> do
otherSigners <- listOf1 (genVerificationKey `suchThat` (/= somePartyCardanoVerificationKey))
let signerAndOthers = somePartyCardanoVerificationKey : otherSigners
pure $ ChangeRequiredSigners (verificationKeyHash <$> signerAndOthers)
, SomeMutation (Just $ toErrorCode SignatureVerificationFailed) MutateCloseUTxOHash . ChangeOutput 0 <$> do
, SomeMutation (pure $ toErrorCode SignatureVerificationFailed) MutateCloseUTxOHash . ChangeOutput 0 <$> do
mutatedUTxOHash <- genHash `suchThat` ((/= healthyClosedUTxOHash) . toBuiltin)
pure $ modifyInlineDatum (replaceUtxoHash $ toBuiltin mutatedUTxOHash) headTxOut
, SomeMutation (Just $ toErrorCode IncorrectClosedContestationDeadline) MutateContestationDeadline <$> do
, SomeMutation (pure $ toErrorCode IncorrectClosedContestationDeadline) MutateContestationDeadline <$> do
mutatedDeadline <- genMutatedDeadline
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceContestationDeadline mutatedDeadline) headTxOut
, SomeMutation (Just $ toErrorCode ChangedParameters) MutateContestationPeriod <$> do
, SomeMutation (pure $ toErrorCode ChangedParameters) MutateContestationPeriod <$> do
mutatedPeriod <- arbitrary
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceContestationPeriod mutatedPeriod) headTxOut
, SomeMutation (Just $ toErrorCode InfiniteLowerBound) MutateInfiniteLowerBound . ChangeValidityLowerBound <$> do
, SomeMutation (pure $ toErrorCode InfiniteLowerBound) MutateInfiniteLowerBound . ChangeValidityLowerBound <$> do
pure TxValidityNoLowerBound
, SomeMutation (Just $ toErrorCode InfiniteUpperBound) MutateInfiniteUpperBound . ChangeValidityUpperBound <$> do
, SomeMutation (pure $ toErrorCode InfiniteUpperBound) MutateInfiniteUpperBound . ChangeValidityUpperBound <$> do
pure TxValidityNoUpperBound
, SomeMutation (Just $ toErrorCode HasBoundedValidityCheckFailed) MutateValidityInterval <$> do
, SomeMutation (pure $ toErrorCode HasBoundedValidityCheckFailed) MutateValidityInterval <$> do
(lowerSlotNo, upperSlotNo, adjustedContestationDeadline) <- genOversizedTransactionValidity
pure $
Changes
Expand All @@ -348,7 +348,7 @@ genCloseMutation (tx, _utxo) =
]
, -- XXX: This is a bit confusing and not giving much value. Maybe we can remove this.
-- This also seems to be covered by MutateRequiredSigner
SomeMutation (Just $ toErrorCode SignerIsNotAParticipant) CloseFromDifferentHead <$> do
SomeMutation (pure $ toErrorCode SignerIsNotAParticipant) CloseFromDifferentHead <$> do
otherHeadId <- headPolicyId <$> arbitrary `suchThat` (/= Fixture.testSeedInput)
pure $
Changes
Expand All @@ -366,12 +366,12 @@ genCloseMutation (tx, _utxo) =
)
)
]
, SomeMutation (Just $ toErrorCode MintingOrBurningIsForbidden) MutateTokenMintingOrBurning
, SomeMutation (pure $ toErrorCode MintingOrBurningIsForbidden) MutateTokenMintingOrBurning
<$> (changeMintedTokens tx =<< genMintedOrBurnedValue)
, SomeMutation (Just $ toErrorCode ContestersNonEmpty) MutateContesters . ChangeOutput 0 <$> do
, SomeMutation (pure $ toErrorCode ContestersNonEmpty) MutateContesters . ChangeOutput 0 <$> do
mutatedContesters <- listOf1 $ PubKeyHash . toBuiltin <$> genHash
pure $ headTxOut & modifyInlineDatum (replaceContesters mutatedContesters)
, SomeMutation (Just $ toErrorCode HeadValueIsNotPreserved) MutateValueInOutput <$> do
, SomeMutation (pure $ toErrorCode HeadValueIsNotPreserved) MutateValueInOutput <$> do
newValue <- genValue
pure $ ChangeOutput 0 (headTxOut{txOutValue = newValue})
]
Expand All @@ -398,7 +398,7 @@ data CloseInitialMutation
-- right away.
genCloseInitialMutation :: (Tx, UTxO) -> Gen SomeMutation
genCloseInitialMutation (tx, _utxo) =
SomeMutation (Just $ toErrorCode IncorrectClosedContestationDeadline) MutateCloseContestationDeadline' <$> do
SomeMutation (pure $ toErrorCode IncorrectClosedContestationDeadline) MutateCloseContestationDeadline' <$> do
mutatedDeadline <- genMutatedDeadline
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceContestationDeadline mutatedDeadline) headTxOut
where
Expand Down
20 changes: 10 additions & 10 deletions hydra-node/test/Hydra/Chain/Direct/Contract/CollectCom.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import Hydra.Contract.HeadError (HeadError (..))
import Hydra.Contract.HeadState qualified as Head
import Hydra.Contract.HeadTokens (headPolicyId)
import Hydra.Contract.Initial qualified as Initial
import Hydra.Contract.InitialError (InitialError (ExpectedSingleCommitOutput))
import Hydra.Contract.InitialError (InitialError (ExpectedSingleCommitOutput, LockedValueDoesNotMatch))
import Hydra.Contract.Util (UtilError (MintingOrBurningIsForbidden))
import Hydra.Data.Party qualified as OnChain
import Hydra.Ledger.Cardano (genAddressInEra, genUTxOAdaOnlyOfSize, genVerificationKey)
Expand Down Expand Up @@ -204,10 +204,10 @@ data CollectComMutation
genCollectComMutation :: (Tx, UTxO) -> Gen SomeMutation
genCollectComMutation (tx, _utxo) =
oneof
[ SomeMutation (Just $ toErrorCode NotPayingToHead) NotContinueContract <$> do
[ SomeMutation (pure $ toErrorCode NotPayingToHead) NotContinueContract <$> do
mutatedAddress <- genAddressInEra testNetworkId
pure $ ChangeOutput 0 (modifyTxOutAddress (const mutatedAddress) headTxOut)
, SomeMutation (Just $ toErrorCode NotAllValueCollected) ExtractSomeValue <$> do
, SomeMutation (pure $ toErrorCode NotAllValueCollected) ExtractSomeValue <$> do
-- Remove a random asset and quantity from headOutput
removedValue <- do
let allAssets = valueToList $ txOutValue headTxOut
Expand All @@ -228,15 +228,15 @@ genCollectComMutation (tx, _utxo) =
[ ChangeOutput 0 $ modifyTxOutValue (\v -> v <> negateValue removedValue) headTxOut
, AppendOutput extractionTxOut
]
, SomeMutation (Just $ toErrorCode IncorrectUtxoHash) MutateOpenUTxOHash . ChangeOutput 0 <$> mutateUTxOHash
, SomeMutation (Just $ toErrorCode MissingCommits) MutateNumberOfParties <$> do
, SomeMutation (pure $ toErrorCode IncorrectUtxoHash) MutateOpenUTxOHash . ChangeOutput 0 <$> mutateUTxOHash
, SomeMutation (pure $ toErrorCode MissingCommits) MutateNumberOfParties <$> do
moreParties <- (: healthyOnChainParties) <$> arbitrary
pure $
Changes
[ ChangeInputHeadDatum $ replaceParties moreParties healthyCollectComInitialDatum
, ChangeOutput 0 $ mutatedPartiesHeadTxOut moreParties headTxOut
]
, SomeMutation (Just $ toErrorCode STNotSpent) MutateHeadId <$> do
, SomeMutation (pure $ toErrorCode STNotSpent) MutateHeadId <$> do
-- XXX: This mutation is unrealistic. It would only change the headId in
-- the value, but not in the datum. This is not allowed by the protocol
-- prior to this transaction.
Expand All @@ -245,10 +245,10 @@ genCollectComMutation (tx, _utxo) =
<$> fmap headPolicyId (arbitrary `suchThat` (/= testSeedInput))
<*> pure (toUTxOContext $ mkTxOutDatumInline healthyCollectComInitialDatum)
return $ ChangeInput healthyHeadTxIn illedHeadResolvedInput (Just $ toScriptData Head.CollectCom)
, SomeMutation (Just $ toErrorCode SignerIsNotAParticipant) MutateRequiredSigner <$> do
, SomeMutation (pure $ toErrorCode SignerIsNotAParticipant) MutateRequiredSigner <$> do
newSigner <- verificationKeyHash <$> genVerificationKey
pure $ ChangeRequiredSigners [newSigner]
, SomeMutation (Just $ toErrorCode ExpectedSingleCommitOutput) MutateCommitToInitial <$> do
, SomeMutation (map toErrorCode [ExpectedSingleCommitOutput, LockedValueDoesNotMatch]) MutateCommitToInitial <$> do
-- By changing a commit output to an initial, we simulate a situation
-- where we do pretend to have collected every commit, but we just
-- changed one back to be an initial. This should be caught by the
Expand All @@ -262,9 +262,9 @@ genCollectComMutation (tx, _utxo) =
(Just . toScriptData . Initial.redeemer $ Initial.ViaCommit [toPlutusTxOutRef txIn])
, AddScript $ fromPlutusScript Initial.validatorScript
]
, SomeMutation (Just $ toErrorCode MintingOrBurningIsForbidden) MutateTokenMintingOrBurning
, SomeMutation (pure $ toErrorCode MintingOrBurningIsForbidden) MutateTokenMintingOrBurning
<$> (changeMintedTokens tx =<< genMintedOrBurnedValue)
, SomeMutation (Just $ toErrorCode STIsMissingInTheOutput) RemoveSTFromOutput <$> do
, SomeMutation (pure $ toErrorCode STIsMissingInTheOutput) RemoveSTFromOutput <$> do
let out = List.head $ txOuts' tx
let stAssetId = AssetId (headPolicyId testSeedInput) hydraHeadV1AssetName
let newValue = filterValue (/= stAssetId) (txOutValue out)
Expand Down
Loading

0 comments on commit d82a44f

Please sign in to comment.