/
Contest.hs
389 lines (356 loc) · 17.3 KB
/
Contest.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Hydra.Chain.Direct.Contract.Contest where
import Hydra.Cardano.Api
import Hydra.Prelude hiding (label)
import Data.Maybe (fromJust)
import Cardano.Api.UTxO as UTxO
import Hydra.Chain.Direct.Contract.Gen (genForParty, genHash, genMintedOrBurnedValue)
import Hydra.Chain.Direct.Contract.Mutation (
Mutation (..),
SomeMutation (..),
addParticipationTokens,
changeHeadOutputDatum,
changeMintedTokens,
replaceContestationDeadline,
replaceContestationPeriod,
replaceContesters,
replaceHeadId,
replaceParties,
replacePolicyIdWith,
replaceSnapshotNumber,
replaceUtxoHash,
)
import Hydra.Chain.Direct.Fixture (testNetworkId, testPolicyId)
import qualified Hydra.Chain.Direct.Fixture as Fixture
import Hydra.Chain.Direct.ScriptRegistry (genScriptRegistry, registryUTxO)
import Hydra.Chain.Direct.Tx (ClosedThreadOutput (..), contestTx, mkHeadId, mkHeadOutput)
import Hydra.ContestationPeriod (ContestationPeriod, fromChain)
import Hydra.Contract.Error (toErrorCode)
import Hydra.Contract.HeadError (HeadError (..))
import qualified Hydra.Contract.HeadState as Head
import Hydra.Contract.HeadTokens (headPolicyId)
import Hydra.Contract.Util (UtilError (MintingOrBurningIsForbidden))
import Hydra.Crypto (HydraKey, MultiSignature, aggregate, sign, toPlutusSignatures)
import Hydra.Data.ContestationPeriod (posixFromUTCTime)
import qualified Hydra.Data.ContestationPeriod as OnChain
import Hydra.Data.Party (partyFromVerificationKeyBytes)
import qualified Hydra.Data.Party as OnChain
import Hydra.Ledger (hashUTxO)
import Hydra.Ledger.Cardano (genAddressInEra, genOneUTxOFor, genValue, genVerificationKey)
import Hydra.Ledger.Cardano.Evaluate (slotNoToUTCTime)
import Hydra.Party (Party, deriveParty, partyToChain)
import Hydra.Snapshot (Snapshot (..), SnapshotNumber)
import Plutus.Orphans ()
import Plutus.V2.Ledger.Api (BuiltinByteString, toBuiltin, toData)
import qualified Plutus.V2.Ledger.Api as Plutus
import Test.Hydra.Fixture (aliceSk, bobSk, carolSk)
import Test.QuickCheck (arbitrarySizedNatural, elements, listOf, listOf1, oneof, suchThat, vectorOf)
import Test.QuickCheck.Gen (choose)
import Test.QuickCheck.Instances ()
--
-- ContestTx
--
-- | Healthy contest tx where the contester is the first one to contest and
-- correctly pushing out the deadline by the contestation period.
healthyContestTx :: (Tx, UTxO)
healthyContestTx =
(tx, lookupUTxO)
where
lookupUTxO =
UTxO.singleton (healthyClosedHeadTxIn, healthyClosedHeadTxOut)
<> registryUTxO scriptRegistry
tx =
contestTx
scriptRegistry
healthyContesterVerificationKey
healthyContestSnapshot
(healthySignature healthyContestSnapshotNumber)
(healthySlotNo, slotNoToUTCTime healthySlotNo)
closedThreadOutput
(mkHeadId testPolicyId)
healthyContestationPeriod
scriptRegistry = genScriptRegistry `generateWith` 42
headDatum = fromPlutusData $ toData healthyClosedState
closedThreadOutput =
ClosedThreadOutput
{ closedThreadUTxO = (healthyClosedHeadTxIn, healthyClosedHeadTxOut, headDatum)
, closedParties =
healthyOnChainParties
, closedContestationDeadline = posixFromUTCTime healthyContestationDeadline
, closedContesters = []
}
healthyClosedHeadTxIn :: TxIn
healthyClosedHeadTxIn = generateWith arbitrary 42
healthyClosedHeadTxOut :: TxOut CtxUTxO
healthyClosedHeadTxOut =
mkHeadOutput testNetworkId testPolicyId headTxOutDatum
& addParticipationTokens healthyParties
where
headTxOutDatum = toUTxOContext (mkTxOutDatum healthyClosedState)
healthyContestSnapshot :: Snapshot Tx
healthyContestSnapshot =
Snapshot
{ number = healthyContestSnapshotNumber
, utxo = healthyContestUTxO
, confirmed = []
}
healthyContestSnapshotNumber :: SnapshotNumber
healthyContestSnapshotNumber = 4
healthyContestUTxO :: UTxO
healthyContestUTxO =
(genOneUTxOFor healthyContesterVerificationKey `suchThat` (/= healthyClosedUTxO))
`generateWith` 42
healthyContestUTxOHash :: BuiltinByteString
healthyContestUTxOHash =
toBuiltin $ hashUTxO @Tx healthyContestUTxO
healthyClosedState :: Head.State
healthyClosedState =
Head.Closed
{ snapshotNumber = fromIntegral healthyClosedSnapshotNumber
, utxoHash = healthyClosedUTxOHash
, parties = healthyOnChainParties
, contestationDeadline = posixFromUTCTime healthyContestationDeadline
, contestationPeriod = healthyOnChainContestationPeriod
, headId = toPlutusCurrencySymbol testPolicyId
, contesters = []
}
healthySlotNo :: SlotNo
healthySlotNo = arbitrary `generateWith` 42
healthyContestationDeadline :: UTCTime
healthyContestationDeadline =
addUTCTime
(fromInteger healthyContestationPeriodSeconds)
(slotNoToUTCTime healthySlotNo)
healthyOnChainContestationPeriod :: OnChain.ContestationPeriod
healthyOnChainContestationPeriod = OnChain.contestationPeriodFromDiffTime $ fromInteger healthyContestationPeriodSeconds
healthyContestationPeriod :: ContestationPeriod
healthyContestationPeriod = fromChain healthyOnChainContestationPeriod
healthyContestationPeriodSeconds :: Integer
healthyContestationPeriodSeconds = 10
healthyClosedSnapshotNumber :: SnapshotNumber
healthyClosedSnapshotNumber = 3
healthyClosedUTxOHash :: BuiltinByteString
healthyClosedUTxOHash =
toBuiltin $ hashUTxO @Tx healthyClosedUTxO
healthyClosedUTxO :: UTxO
healthyClosedUTxO =
genOneUTxOFor healthyContesterVerificationKey `generateWith` 42
healthyContesterVerificationKey :: VerificationKey PaymentKey
healthyContesterVerificationKey = flip generateWith 42 $ do
genForParty genVerificationKey <$> elements healthyParties
healthySigningKeys :: [SigningKey HydraKey]
healthySigningKeys = [aliceSk, bobSk, carolSk]
healthyParties :: [Party]
healthyParties = deriveParty <$> healthySigningKeys
healthyOnChainParties :: [OnChain.Party]
healthyOnChainParties = partyToChain <$> healthyParties
healthySignature :: SnapshotNumber -> MultiSignature (Snapshot Tx)
healthySignature number =
aggregate [sign sk snapshot | sk <- healthySigningKeys]
where
snapshot = healthyContestSnapshot{number}
-- FIXME: Should try to mutate the 'closedAt' recorded time to something else
data ContestMutation
= -- | Ensures collectCom does not allow any output address but νHead.
NotContinueContract
| -- | Invalidates the tx by changing the redeemer signature but not the
-- snapshot number in resulting head output.
--
-- Ensures the snapshot signature is multisigned by all valid Head
-- participants.
MutateSignatureButNotSnapshotNumber
| -- | Invalidates the tx by changing the snapshot number in resulting head
-- output but not the redeemer signature.
--
-- Ensures the snapshot signature is aligned with snapshot number.
MutateSnapshotNumberButNotSignature
| -- | Invalidates the tx by changing the contest snapshot number too old.
--
-- This is achieved by updating the head input datum to be older, so the
-- healthy snapshot number becomes too old.
MutateToNonNewerSnapshot
| -- | Ensures close is authenticated by one of the Head members by changing the signer
-- used on the tx to be not one of PTs.
MutateRequiredSigner
| -- | Ensures close is authenticated by one of the Head members by changing the signer
-- used on the tx to be empty.
MutateNoRequiredSigner
| -- | Ensures close is authenticated by one of the Head members by changing the signer
-- used on the tx to have multiple signers (including the signer to not fail for
-- SignerIsNotAParticipant).
MutateMultipleRequiredSigner
| -- | Invalidates the tx by changing the utxo hash in resulting head output.
--
-- Ensures the output state is consistent with the redeemer.
MutateContestUTxOHash
| -- | Ensures the contest snapshot is multisigned by all Head participants by
-- changing the parties in the input head datum. If they do not align the
-- multisignature will not be valid anymore.
SnapshotNotSignedByAllParties
| -- | Invalidates the tx by changing the upper bound to be beyond
-- contestation deadline from head input (stored state).
MutateValidityPastDeadline
| -- | Change the head policy id to simulate contestation using a ST and
-- signer from a different head. The signer shows a correct signature but
-- from a different head. This will cause the signer to not be present in
-- the participation tokens.
ContestFromDifferentHead
| -- | Minting or burning of tokens should not be possible in contest.
MutateTokenMintingOrBurning
| -- | Ensures a participant can only contest once by changing the head input
-- datum to already include the signer.
MutateInputContesters
| -- | Ensures a the signer needs to be added to the head output datum.
MutateContesters
| -- | Invalidates the tx by changing the output values arbitrarily to be
-- different (not preserved) from the head.
--
-- Ensures values are preserved between head input and output.
MutateValueInOutput
| -- | Not pushing the contestation deadline in head output datum should not
-- be allowed.
NotUpdateDeadlineAlthoughItShould
| -- | Pushes the deadline although this is the last contest. Instead of
-- creating another healthy case and mutate that one, this mutation just
-- changes the starting situation so that everyone else already contested.
-- Remember the 'healthyContestTx' is already pushing out the deadline.
PushDeadlineAlthoughItShouldNot
| -- | Ensures contestation period does not change between head input datum
-- and head output datum.
MutateOutputContestationPeriod
| -- | Ensures parties do not change between head input datum and head output
-- datum.
MutatePartiesInOutput
| -- | Ensures headId do not change between head input datum and head output
-- datum.
MutateHeadIdInOutput
deriving (Generic, Show, Enum, Bounded)
genContestMutation :: (Tx, UTxO) -> Gen SomeMutation
genContestMutation (tx, _utxo) =
oneof
[ SomeMutation (Just $ toErrorCode NotPayingToHead) NotContinueContract <$> do
mutatedAddress <- genAddressInEra testNetworkId
pure $ ChangeOutput 0 (modifyTxOutAddress (const mutatedAddress) headTxOut)
, SomeMutation (Just $ toErrorCode SignatureVerificationFailed) MutateSignatureButNotSnapshotNumber . ChangeHeadRedeemer <$> do
mutatedSignature <- arbitrary :: Gen (MultiSignature (Snapshot Tx))
pure $
Head.Contest
{ signature = toPlutusSignatures mutatedSignature
}
, SomeMutation (Just $ toErrorCode SignatureVerificationFailed) MutateSnapshotNumberButNotSignature <$> do
mutatedSnapshotNumber <- arbitrarySizedNatural `suchThat` (> healthyContestSnapshotNumber)
pure $ ChangeOutput 0 $ changeHeadOutputDatum (replaceSnapshotNumber $ toInteger mutatedSnapshotNumber) headTxOut
, SomeMutation (Just $ toErrorCode TooOldSnapshot) MutateToNonNewerSnapshot <$> do
mutatedSnapshotNumber <- choose (toInteger healthyContestSnapshotNumber, toInteger healthyContestSnapshotNumber + 1)
pure $
Changes
[ ChangeInputHeadDatum $
healthyClosedState & replaceSnapshotNumber mutatedSnapshotNumber
, ChangeHeadRedeemer $
Head.Contest
{ signature =
toPlutusSignatures $
healthySignature (fromInteger mutatedSnapshotNumber)
}
]
, SomeMutation (Just $ toErrorCode SignerIsNotAParticipant) MutateRequiredSigner <$> do
newSigner <- verificationKeyHash <$> genVerificationKey `suchThat` (/= healthyContesterVerificationKey)
pure $ ChangeRequiredSigners [newSigner]
, SomeMutation (Just $ toErrorCode NoSigners) MutateNoRequiredSigner <$> do
pure $ ChangeRequiredSigners []
, SomeMutation (Just $ toErrorCode TooManySigners) MutateMultipleRequiredSigner <$> do
otherSigners <- listOf1 (genVerificationKey `suchThat` (/= healthyContesterVerificationKey))
let signerAndOthers = healthyContesterVerificationKey : otherSigners
pure $ ChangeRequiredSigners (verificationKeyHash <$> signerAndOthers)
, SomeMutation (Just $ toErrorCode SignatureVerificationFailed) MutateContestUTxOHash . ChangeOutput 0 <$> do
mutatedUTxOHash <- genHash `suchThat` ((/= healthyContestUTxOHash) . toBuiltin)
pure $
changeHeadOutputDatum
(replaceUtxoHash (toBuiltin mutatedUTxOHash))
headTxOut
, SomeMutation (Just $ toErrorCode SignatureVerificationFailed) SnapshotNotSignedByAllParties . ChangeInputHeadDatum <$> do
mutatedParties <- arbitrary `suchThat` (/= healthyOnChainParties)
pure $
healthyClosedState & replaceParties mutatedParties
, SomeMutation (Just $ toErrorCode UpperBoundBeyondContestationDeadline) MutateValidityPastDeadline . ChangeValidityInterval <$> do
lb <- arbitrary
ub <- TxValidityUpperBound <$> arbitrary `suchThat` slotOverContestationDeadline
pure (lb, ub)
, -- 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) ContestFromDifferentHead <$> do
otherHeadId <- headPolicyId <$> arbitrary `suchThat` (/= healthyClosedHeadTxIn)
pure $
Changes
[ ChangeOutput 0 (replacePolicyIdWith testPolicyId otherHeadId headTxOut)
, ChangeInput
healthyClosedHeadTxIn
(replacePolicyIdWith testPolicyId otherHeadId healthyClosedHeadTxOut)
( Just $
toScriptData
( Head.Contest
{ signature =
toPlutusSignatures $
healthySignature healthyContestSnapshotNumber
}
)
)
]
, SomeMutation (Just $ toErrorCode MintingOrBurningIsForbidden) MutateTokenMintingOrBurning
<$> (changeMintedTokens tx =<< genMintedOrBurnedValue)
, SomeMutation (Just $ toErrorCode SignerAlreadyContested) MutateInputContesters . ChangeInputHeadDatum <$> do
let contester = toPlutusKeyHash (verificationKeyHash healthyContesterVerificationKey)
contesterAndSomeOthers = do
contesters <- listOf $ Plutus.PubKeyHash . toBuiltin <$> genHash
pure (contester : contesters)
mutatedContesters <-
oneof
[ pure [contester]
, contesterAndSomeOthers
]
pure $
healthyClosedState & replaceContesters mutatedContesters
, SomeMutation (Just $ toErrorCode ContesterNotIncluded) MutateContesters . ChangeOutput 0 <$> do
hashes <- listOf genHash
let mutatedContesters = Plutus.PubKeyHash . toBuiltin <$> hashes
pure $ changeHeadOutputDatum (replaceContesters mutatedContesters) headTxOut
, SomeMutation (Just $ toErrorCode HeadValueIsNotPreserved) MutateValueInOutput <$> do
newValue <- genValue
pure $ ChangeOutput 0 (headTxOut{txOutValue = newValue})
, SomeMutation (Just $ toErrorCode MustPushDeadline) NotUpdateDeadlineAlthoughItShould . ChangeOutput 0 <$> do
let deadline = posixFromUTCTime healthyContestationDeadline
-- Here we are replacing the contestationDeadline using the previous so we are not _pushing it_ further
-- Remember the 'healthyContestTx' is already pushing out the deadline.
pure $ headTxOut & changeHeadOutputDatum (replaceContestationDeadline deadline)
, SomeMutation (Just $ toErrorCode MustNotPushDeadline) PushDeadlineAlthoughItShouldNot <$> do
alreadyContested <- vectorOf (length healthyParties - 1) $ Plutus.PubKeyHash . toBuiltin <$> genHash
let contester = toPlutusKeyHash $ verificationKeyHash healthyContesterVerificationKey
pure $
Changes
[ ChangeOutput 0 (headTxOut & changeHeadOutputDatum (replaceContesters (contester : alreadyContested)))
, ChangeInputHeadDatum (healthyClosedState & replaceContesters alreadyContested)
]
, SomeMutation (Just $ toErrorCode ChangedParameters) MutateOutputContestationPeriod <$> do
randomCP <- arbitrary `suchThat` (/= healthyOnChainContestationPeriod)
pure $ ChangeOutput 0 (headTxOut & changeHeadOutputDatum (replaceContestationPeriod randomCP))
, SomeMutation (Just $ toErrorCode ChangedParameters) MutatePartiesInOutput <$> do
mutatedParties <-
-- The length of mutatedParties must be the same as
-- healthyOnChainParties so to not fail because of
-- `must not push contestation deadline`.
vectorOf
(length healthyOnChainParties)
( partyFromVerificationKeyBytes <$> genHash
)
`suchThat` (/= healthyOnChainParties)
pure $ ChangeOutput 0 $ changeHeadOutputDatum (replaceParties mutatedParties) headTxOut
, SomeMutation (Just $ toErrorCode ChangedParameters) MutateHeadIdInOutput <$> do
otherHeadId <- toPlutusCurrencySymbol . headPolicyId <$> arbitrary `suchThat` (/= Fixture.testSeedInput)
pure $ ChangeOutput 0 $ changeHeadOutputDatum (replaceHeadId otherHeadId) headTxOut
]
where
headTxOut = fromJust $ txOuts' tx !!? 0
slotOverContestationDeadline slotNo =
slotNoToUTCTime slotNo > healthyContestationDeadline