Skip to content

Commit 538d3d1

Browse files
committed
Generate healthy deposit transactions
This makes sure we test a wider range of transactions and we can easily switch to this approach for deposits as the mutations are fully defined on the mutated tx/utxo.
1 parent cd1c401 commit 538d3d1

File tree

3 files changed

+34
-37
lines changed

3 files changed

+34
-37
lines changed

hydra-tx/test/Hydra/Tx/Contract/ContractSpec.hs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import Hydra.Tx.Contract.Contest.ContestCurrent (genContestMutation)
4444
import Hydra.Tx.Contract.Contest.ContestDec (genContestDecMutation)
4545
import Hydra.Tx.Contract.Contest.Healthy (healthyContestTx)
4646
import Hydra.Tx.Contract.Decrement (genDecrementMutation, healthyDecrementTx)
47-
import Hydra.Tx.Contract.Deposit (genDepositMutation, healthyDepositTx)
47+
import Hydra.Tx.Contract.Deposit (genDepositMutation, genHealthyDepositTx)
4848
import Hydra.Tx.Contract.FanOut (genFanoutMutation, healthyFanoutTx)
4949
import Hydra.Tx.Contract.Increment (genIncrementMutation, healthyIncrementTx)
5050
import Hydra.Tx.Contract.Init (genInitMutation, healthyInitTx)
@@ -126,18 +126,19 @@ spec = parallel $ do
126126
propMutation healthyDecrementTx genDecrementMutation
127127
describe "Deposit" $ do
128128
prop "healthy evaluates" $
129-
propTransactionEvaluates healthyDepositTx
129+
forAll genHealthyDepositTx propTransactionEvaluates
130130
prop "healthy observed" $
131-
isJust $
132-
observeDepositTx testNetworkId (fst healthyDepositTx)
131+
forAll genHealthyDepositTx $ \(tx, _) ->
132+
isJust $ observeDepositTx testNetworkId tx
133133
prop "mutated not observed" $
134-
forAll (genDepositMutation healthyDepositTx) $ \SomeMutation{label, mutation} -> do
135-
let (tx, utxo) = healthyDepositTx & applyMutation mutation
136-
counterexample ("Mutated transaction: " <> renderTxWithUTxO utxo tx) $
137-
property (isNothing $ observeDepositTx testNetworkId tx)
138-
& counterexample "Mutated transaction still observed"
139-
& genericCoverTable [label]
140-
& checkCoverage
134+
forAll genHealthyDepositTx $ \(tx, utxo) ->
135+
forAll (genDepositMutation (tx, utxo)) $ \SomeMutation{label, mutation} -> do
136+
let (tx', utxo') = (tx, utxo) & applyMutation mutation
137+
counterexample ("Mutated transaction: " <> renderTxWithUTxO utxo' tx') $
138+
property (isNothing $ observeDepositTx testNetworkId tx')
139+
& counterexample "Mutated transaction still observed"
140+
& genericCoverTable [label]
141+
& checkCoverage
141142
describe "Recover" $ do
142143
prop "is healthy" $
143144
propTransactionEvaluates healthyRecoverTx

hydra-tx/test/Hydra/Tx/Contract/Deposit.hs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
-- | Healthy deposit transactions and mutations
2-
--
32
-- As no Hydra script is run in these transactions, the mutations here should
43
-- make the deposit transaction not observed as a valid deposi.
54
module Hydra.Tx.Contract.Deposit where
@@ -13,23 +12,23 @@ import Hydra.Tx (mkHeadId)
1312
import Hydra.Tx.BlueprintTx (CommitBlueprintTx (..))
1413
import Hydra.Tx.Deposit (depositTx)
1514
import Test.Hydra.Tx.Fixture (depositDeadline, testNetworkId, testPolicyId)
16-
import Test.Hydra.Tx.Gen (genUTxO)
15+
import Test.Hydra.Tx.Gen (genUTxOSized)
1716
import Test.Hydra.Tx.Mutation (Mutation (ChangeOutput), SomeMutation (..))
18-
import Test.QuickCheck (chooseInteger, elements, oneof, resize)
17+
import Test.QuickCheck (chooseInteger, elements, oneof)
1918

20-
healthyDepositTx :: (Tx, UTxO)
21-
healthyDepositTx =
22-
(tx, healthyDepositUTxO)
23-
where
24-
tx =
25-
depositTx
26-
testNetworkId
27-
(mkHeadId testPolicyId)
28-
CommitBlueprintTx{blueprintTx = txSpendingUTxO healthyDepositUTxO, lookupUTxO = healthyDepositUTxO}
29-
depositDeadline
30-
31-
healthyDepositUTxO :: UTxO
32-
healthyDepositUTxO = resize 3 genUTxO `generateWith` 42
19+
genHealthyDepositTx :: Gen (Tx, UTxO)
20+
genHealthyDepositTx = do
21+
-- XXX: Ideally we would want to have more arbitrary utxo here, but 'genUTxO'
22+
-- and other generators yield value quantities that fail to be put into
23+
-- transaction outputs.
24+
healthyDepositUTxO <- genUTxOSized 1
25+
let tx =
26+
depositTx
27+
testNetworkId
28+
(mkHeadId testPolicyId)
29+
CommitBlueprintTx{blueprintTx = txSpendingUTxO healthyDepositUTxO, lookupUTxO = healthyDepositUTxO}
30+
depositDeadline -- TODO: should generate
31+
pure (tx, healthyDepositUTxO)
3332

3433
data DepositMutation
3534
= -- | Change the output value to a subset of the deposited value. This

hydra-tx/test/Hydra/Tx/Contract/Increment.hs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@ import Hydra.Data.Party qualified as OnChain
2626
import Hydra.Ledger.Cardano.Time (slotNoFromUTCTime)
2727
import Hydra.Plutus.Orphans ()
2828
import Hydra.Tx.ContestationPeriod (ContestationPeriod, toChain)
29-
import Hydra.Tx.Contract.Deposit (healthyDepositTx, healthyDepositUTxO)
3029
import Hydra.Tx.Crypto (HydraKey, MultiSignature (..), aggregate, sign, toPlutusSignatures)
3130
import Hydra.Tx.Deposit qualified as Deposit
3231
import Hydra.Tx.HeadId (mkHeadId)
3332
import Hydra.Tx.HeadParameters (HeadParameters (..))
34-
import Hydra.Tx.Increment (
35-
incrementTx,
36-
)
33+
import Hydra.Tx.Increment (incrementTx)
3734
import Hydra.Tx.Init (mkHeadOutput)
3835
import Hydra.Tx.IsTx (IsTx (hashUTxO))
3936
import Hydra.Tx.Party (Party, deriveParty, partyToChain)
@@ -43,7 +40,7 @@ import Hydra.Tx.Utils (adaOnly)
4340
import PlutusLedgerApi.V2 qualified as Plutus
4441
import PlutusTx.Builtins (toBuiltin)
4542
import Test.Hydra.Tx.Fixture (aliceSk, bobSk, carolSk, depositDeadline, slotLength, systemStart, testNetworkId, testPolicyId)
46-
import Test.Hydra.Tx.Gen (genForParty, genScriptRegistry, genUTxOSized, genValue, genVerificationKey)
43+
import Test.Hydra.Tx.Gen (genForParty, genScriptRegistry, genUTxO, genUTxOSized, genValue, genVerificationKey)
4744
import Test.QuickCheck (arbitrarySizedNatural, elements, oneof, suchThat)
4845
import Test.QuickCheck.Instances ()
4946

@@ -53,7 +50,7 @@ healthyIncrementTx =
5350
where
5451
lookupUTxO =
5552
UTxO.singleton (headInput, headOutput)
56-
<> depositUTxO
53+
<> healthyDepositUTxO
5754
<> registryUTxO scriptRegistry
5855

5956
tx =
@@ -64,7 +61,7 @@ healthyIncrementTx =
6461
parameters
6562
(headInput, headOutput)
6663
healthySnapshot
67-
depositUTxO
64+
healthyDepositUTxO
6865
(slotNoFromUTCTime systemStart slotLength depositDeadline)
6966
healthySignature
7067

@@ -83,8 +80,8 @@ healthyIncrementTx =
8380
& addParticipationTokens healthyParticipants
8481
& modifyTxOutValue (<> foldMap txOutValue healthyUTxO)
8582

86-
depositUTxO :: UTxO
87-
depositUTxO = utxoFromTx (fst healthyDepositTx)
83+
healthyDepositUTxO :: UTxO
84+
healthyDepositUTxO = genUTxO `generateWith` 42
8885

8986
somePartyCardanoVerificationKey :: VerificationKey PaymentKey
9087
somePartyCardanoVerificationKey =
@@ -195,7 +192,7 @@ genIncrementMutation (tx, utxo) =
195192
Head.IncrementRedeemer
196193
{ signature = invalidSignature
197194
, snapshotNumber = fromIntegral healthySnapshotNumber
198-
, increment = toPlutusTxOutRef $ fst $ List.head $ UTxO.pairs depositUTxO
195+
, increment = toPlutusTxOutRef $ fst $ List.head $ UTxO.pairs healthyDepositUTxO
199196
}
200197
, SomeMutation (pure $ toErrorCode HeadValueIsNotPreserved) ChangeHeadValue <$> do
201198
newValue <- genValue `suchThat` (/= txOutValue headTxOut)

0 commit comments

Comments
 (0)