Skip to content

Commit

Permalink
Decrement mutation: Add validator check for snapshot number changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ffakenz authored and ch1bo committed Apr 17, 2024
1 parent 6f637e6 commit 179d370
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Decrement.hs
Expand Up @@ -3,7 +3,13 @@
module Hydra.Chain.Direct.Contract.Decrement where

import Hydra.Cardano.Api
import Hydra.Chain.Direct.Contract.Mutation (Mutation (..), SomeMutation (..), modifyInlineDatum, replaceParties)
import Hydra.Chain.Direct.Contract.Mutation (
Mutation (..),
SomeMutation (..),
modifyInlineDatum,
replaceParties,
replaceSnapshotNumber,
)
import Hydra.Prelude hiding (label)

import Cardano.Api.UTxO as UTxO
Expand All @@ -30,10 +36,10 @@ import Hydra.Ledger.Cardano (
)
import Hydra.Party (Party, partyToChain, vkey)
import Hydra.Plutus.Orphans ()
import Hydra.Snapshot (Snapshot (..))
import Hydra.Snapshot (Snapshot (..), SnapshotNumber)
import PlutusTx.Builtins (toBuiltin)
import Test.Hydra.Fixture (genForParty)
import Test.QuickCheck (elements, oneof)
import Test.QuickCheck (arbitrarySizedNatural, elements, oneof)
import Test.QuickCheck.Gen (suchThat)
import Test.QuickCheck.Instances ()

Expand Down Expand Up @@ -83,12 +89,15 @@ healthyDecrementTx =
)
healthyParties

healthySnapshotNumber :: SnapshotNumber
healthySnapshotNumber = 1

healthySnapshot :: Snapshot Tx
healthySnapshot =
let (utxoToDecommit', utxo) = splitDecommitUTxO healthyUTxO
in Snapshot
{ headId = mkHeadId testPolicyId
, number = 1
, number = healthySnapshotNumber
, utxo
, confirmed = []
, utxoToDecommit = Just utxoToDecommit'
Expand Down Expand Up @@ -138,6 +147,11 @@ data DecrementMutation
= -- | Ensures parties do not change between head input datum and head output
-- datum.
MutatePartiesInOutput
| -- | Invalidates the tx by changing the snapshot number in resulting head
-- output.
--
-- Ensures the snapshot number is aligned.
MutateSnapshotNumber
deriving stock (Generic, Show, Enum, Bounded)

genDecrementMutation :: (Tx, UTxO) -> Gen SomeMutation
Expand All @@ -146,6 +160,9 @@ genDecrementMutation (tx, _utxo) =
[ SomeMutation (Just $ toErrorCode ChangedParameters) MutatePartiesInOutput <$> do
mutatedParties <- arbitrary `suchThat` (/= healthyOnChainParties)
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceParties mutatedParties) headTxOut
, SomeMutation (Just "H38") MutateSnapshotNumber <$> do
mutatedSnapshotNumber <- arbitrarySizedNatural `suchThat` (/= healthySnapshotNumber)
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceSnapshotNumber $ toInteger mutatedSnapshotNumber) headTxOut
]
where
headTxOut = fromJust $ txOuts' tx !!? 0

0 comments on commit 179d370

Please sign in to comment.