/
Close.hs
416 lines (376 loc) · 18.5 KB
/
Close.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Hydra.Chain.Direct.Contract.Close where
import Hydra.Cardano.Api
import Hydra.Prelude hiding (label)
import Cardano.Api.UTxO as UTxO
import Data.Maybe (fromJust)
import Hydra.Chain.Direct.Contract.Gen (genForParty, genHash, genMintedOrBurnedValue)
import Hydra.Chain.Direct.Contract.Mutation (
Mutation (..),
SomeMutation (..),
addParticipationTokens,
changeHeadOutputDatum,
changeMintedTokens,
replaceContestationDeadline,
replaceContesters,
replaceHeadId,
replaceParties,
replacePolicyIdWith,
replaceSnapshotNumber,
replaceUtxoHash,
)
import Hydra.Chain.Direct.Fixture (testNetworkId)
import qualified Hydra.Chain.Direct.Fixture as Fixture
import Hydra.Chain.Direct.ScriptRegistry (genScriptRegistry, registryUTxO)
import Hydra.Chain.Direct.TimeHandle (PointInTime)
import Hydra.Chain.Direct.Tx (ClosingSnapshot (..), OpenThreadOutput (..), UTxOHash (UTxOHash), closeTx, mkHeadId, mkHeadOutput)
import Hydra.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 qualified Hydra.Data.Party as OnChain
import Hydra.Ledger (hashUTxO)
import Hydra.Ledger.Cardano (genAddressInEra, genOneUTxOFor, genValue, genVerificationKey)
import Hydra.Ledger.Cardano.Evaluate (genValidityBoundsFromContestationPeriod)
import Hydra.Party (Party, deriveParty, partyToChain)
import Hydra.Snapshot (Snapshot (..), SnapshotNumber)
import Plutus.Orphans ()
import Plutus.V1.Ledger.Time (DiffMilliSeconds (..), fromMilliSeconds)
import Plutus.V2.Ledger.Api (BuiltinByteString, POSIXTime, PubKeyHash (PubKeyHash), toBuiltin, toData)
import Test.Hydra.Fixture (aliceSk, bobSk, carolSk)
import Test.QuickCheck (arbitrarySizedNatural, choose, elements, listOf1, oneof, suchThat)
import Test.QuickCheck.Instances ()
-- | Healthy close transaction for the generic case were we close a head
-- after one or more snapshot have been agreed upon between the members.
healthyCloseTx :: (Tx, UTxO)
healthyCloseTx =
(tx, lookupUTxO)
where
tx =
closeTx
scriptRegistry
somePartyCardanoVerificationKey
closingSnapshot
healthyCloseLowerBoundSlot
healthyCloseUpperBoundPointInTime
openThreadOutput
(mkHeadId Fixture.testPolicyId)
lookupUTxO =
UTxO.singleton (healthyOpenHeadTxIn, healthyOpenHeadTxOut)
<> registryUTxO scriptRegistry
scriptRegistry = genScriptRegistry `generateWith` 42
headDatum = fromPlutusData $ toData healthyOpenHeadDatum
openThreadOutput =
OpenThreadOutput
{ openThreadUTxO = (healthyOpenHeadTxIn, healthyOpenHeadTxOut, headDatum)
, openParties = healthyOnChainParties
, openContestationPeriod = healthyContestationPeriod
}
closingSnapshot :: ClosingSnapshot
closingSnapshot =
CloseWithConfirmedSnapshot
{ snapshotNumber = healthyCloseSnapshotNumber
, closeUtxoHash = UTxOHash $ hashUTxO @Tx healthyCloseUTxO
, signatures = healthySignature healthyCloseSnapshotNumber
}
-- | Healthy close transaction for the specific case were we close a head
-- with the initial UtxO, that is, no snapshot have been agreed upon and
-- signed by the head members yet.
healthyCloseInitialTx :: (Tx, UTxO)
healthyCloseInitialTx =
(tx, lookupUTxO)
where
tx =
closeTx
scriptRegistry
somePartyCardanoVerificationKey
closingSnapshot
healthyCloseLowerBoundSlot
healthyCloseUpperBoundPointInTime
openThreadOutput
(mkHeadId Fixture.testPolicyId)
lookupUTxO =
UTxO.singleton (healthyOpenHeadTxIn, healthyOpenHeadTxOut)
<> registryUTxO scriptRegistry
scriptRegistry = genScriptRegistry `generateWith` 42
headDatum = fromPlutusData $ toData healthyOpenHeadDatum
openThreadOutput =
OpenThreadOutput
{ openThreadUTxO = (healthyOpenHeadTxIn, healthyOpenHeadTxOut, headDatum)
, openParties = healthyOnChainParties
, openContestationPeriod = healthyContestationPeriod
}
closingSnapshot :: ClosingSnapshot
closingSnapshot =
CloseWithInitialSnapshot
{ openUtxoHash = UTxOHash $ hashUTxO @Tx healthyUTxO
}
-- NOTE: We need to use the contestation period when generating start/end tx
-- validity slots/time since if tx validity bound difference is bigger than
-- contestation period our close validator will fail
healthyCloseLowerBoundSlot :: SlotNo
healthyCloseUpperBoundPointInTime :: PointInTime
(healthyCloseLowerBoundSlot, healthyCloseUpperBoundPointInTime) =
genValidityBoundsFromContestationPeriod (fromChain healthyContestationPeriod) `generateWith` 42
healthyOpenHeadTxIn :: TxIn
healthyOpenHeadTxIn = generateWith arbitrary 42
healthyOpenHeadTxOut :: TxOut CtxUTxO
healthyOpenHeadTxOut =
mkHeadOutput testNetworkId Fixture.testPolicyId headTxOutDatum
& addParticipationTokens healthyParties
where
headTxOutDatum = toUTxOContext (mkTxOutDatum healthyOpenHeadDatum)
healthySnapshot :: Snapshot Tx
healthySnapshot =
Snapshot
{ number = healthyCloseSnapshotNumber
, utxo = healthyCloseUTxO
, confirmed = []
}
healthyCloseUTxO :: UTxO
healthyCloseUTxO =
(genOneUTxOFor somePartyCardanoVerificationKey `suchThat` (/= healthyUTxO))
`generateWith` 42
healthyCloseSnapshotNumber :: SnapshotNumber
healthyCloseSnapshotNumber = 1
healthyOpenHeadDatum :: Head.State
healthyOpenHeadDatum =
Head.Open
{ parties = healthyOnChainParties
, utxoHash = toBuiltin $ hashUTxO @Tx healthyUTxO
, contestationPeriod = healthyContestationPeriod
, headId = toPlutusCurrencySymbol Fixture.testPolicyId
}
healthyContestationPeriod :: OnChain.ContestationPeriod
healthyContestationPeriod = OnChain.contestationPeriodFromDiffTime $ fromInteger healthyContestationPeriodSeconds
healthyContestationPeriodSeconds :: Integer
healthyContestationPeriodSeconds = 10
healthyUTxO :: UTxO
healthyUTxO = genOneUTxOFor somePartyCardanoVerificationKey `generateWith` 42
somePartyCardanoVerificationKey :: VerificationKey PaymentKey
somePartyCardanoVerificationKey = 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 = healthySnapshot{number}
healthyContestationDeadline :: UTCTime
healthyContestationDeadline =
addUTCTime
(fromInteger healthyContestationPeriodSeconds)
(snd healthyCloseUpperBoundPointInTime)
healthyClosedUTxOHash :: BuiltinByteString
healthyClosedUTxOHash =
toBuiltin $ hashUTxO @Tx healthyClosedUTxO
healthyClosedUTxO :: UTxO
healthyClosedUTxO =
genOneUTxOFor somePartyCardanoVerificationKey `generateWith` 42
data CloseMutation
= -- | Ensures collectCom does not allow any output address but νHead.
NotContinueContract
| -- | Ensures the snapshot signature is multisigned by all valid Head
-- participants.
--
-- Invalidates the tx by changing the redeemer signature
-- but not the snapshot number in output head datum.
MutateSignatureButNotSnapshotNumber
| -- | Ensures the snapshot number is consistent with the signature.
--
-- Invalidates the tx by changing the snapshot number
-- in resulting head output but not the redeemer signature.
MutateSnapshotNumberButNotSignature
| -- | Check that snapshot numbers <= 0 need to close the head with the
-- initial UTxO hash.
MutateSnapshotNumberToLessThanEqualZero
| -- | Ensures the close 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
| -- | Ensures close is authenticated by a one of the Head members by changing
-- the signer used on the tx to not be one of PTs.
MutateRequiredSigner
| -- | Ensures close is authenticated by a one of the Head members by changing
-- the signer used on the tx to be empty.
MutateNoRequiredSigner
| -- | Ensures close is authenticated by a 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.
MutateCloseUTxOHash
| -- | 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
| -- | Invalidates the tx by changing the lower bound to be non finite.
MutateInfiniteLowerBound
| -- | Invalidates the tx by changing the upper bound to be non finite.
MutateInfiniteUpperBound
| -- | Invalidates the tx by changing the contestation deadline to not satisfy
-- `contestationDeadline = upperBound + contestationPeriod`.
MutateContestationDeadline
| -- | Invalidates the tx by changing the lower and upper bound to be not
-- bounded as per spec `upperBound - lowerBound <= contestationPeriod`.
--
-- This also changes the resulting `head output` contestation deadline to be
-- valid, so it satisfy `contestationDeadline = upperBound +
-- contestationPeriod`.
MutateValidityInterval
| -- | Ensure the Head cannot be closed with correct authentication from a
-- different Head. We simulate this by changing the head policy id of the ST
-- and PTs to be of a different head - a real attack would be to add inputs
-- with those tokens on top of spending the head output, a bit like a double
-- satisfaction attack. Note that the token name stays the same and
-- consistent with the signer. This will cause authentication failure
-- because the signer's PT, although with a consistent name, is not from the
-- right head (has a different policy id than in the datum).
CloseFromDifferentHead
| -- | Minting or burning of tokens should not be possible in close.
MutateTokenMintingOrBurning
| -- | Invalidates the tx by changing the contesters to be non empty.
MutateContesters
| -- | Invalidates the tx by changing output values arbitrarily to be different
-- (not preserved) from the head.
--
-- Ensures values are preserved between head input and output.
MutateValueInOutput
deriving (Generic, Show, Enum, Bounded)
genCloseMutation :: (Tx, UTxO) -> Gen SomeMutation
genCloseMutation (tx, _utxo) =
oneof
[ SomeMutation (Just $ toErrorCode NotPayingToHead) NotContinueContract <$> do
mutatedAddress <- genAddressInEra testNetworkId
pure $ ChangeOutput 0 (modifyTxOutAddress (const mutatedAddress) headTxOut)
, SomeMutation (Just $ toErrorCode InvalidSnapshotSignature) MutateSignatureButNotSnapshotNumber . ChangeHeadRedeemer <$> do
Head.Close . toPlutusSignatures <$> (arbitrary :: Gen (MultiSignature (Snapshot Tx)))
, SomeMutation (Just $ toErrorCode ClosedWithNonInitialHash) MutateSnapshotNumberToLessThanEqualZero <$> do
mutatedSnapshotNumber <- arbitrary `suchThat` (<= 0)
pure $ ChangeOutput 0 $ changeHeadOutputDatum (replaceSnapshotNumber mutatedSnapshotNumber) headTxOut
, SomeMutation (Just $ toErrorCode InvalidSnapshotSignature) MutateSnapshotNumberButNotSignature <$> do
mutatedSnapshotNumber <- arbitrarySizedNatural `suchThat` (> healthyCloseSnapshotNumber)
pure $ ChangeOutput 0 $ changeHeadOutputDatum (replaceSnapshotNumber $ toInteger mutatedSnapshotNumber) headTxOut
, SomeMutation (Just $ toErrorCode InvalidSnapshotSignature) SnapshotNotSignedByAllParties . ChangeInputHeadDatum <$> do
mutatedParties <- arbitrary `suchThat` (/= healthyOnChainParties)
pure $
Head.Open
{ parties = mutatedParties
, utxoHash = ""
, contestationPeriod = healthyContestationPeriod
, headId = toPlutusCurrencySymbol Fixture.testPolicyId
}
, SomeMutation (Just $ toErrorCode ChangedParameters) MutatePartiesInOutput <$> do
mutatedParties <- arbitrary `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
, SomeMutation (Just $ toErrorCode SignerIsNotAParticipant) MutateRequiredSigner <$> do
newSigner <- verificationKeyHash <$> genVerificationKey `suchThat` (/= somePartyCardanoVerificationKey)
pure $ ChangeRequiredSigners [newSigner]
, SomeMutation (Just $ toErrorCode NoSigners) MutateNoRequiredSigner <$> do
pure $ ChangeRequiredSigners []
, SomeMutation (Just $ toErrorCode TooManySigners) MutateMultipleRequiredSigner <$> do
otherSigners <- listOf1 (genVerificationKey `suchThat` (/= somePartyCardanoVerificationKey))
let signerAndOthers = somePartyCardanoVerificationKey : otherSigners
pure $ ChangeRequiredSigners (verificationKeyHash <$> signerAndOthers)
, SomeMutation (Just $ toErrorCode InvalidSnapshotSignature) MutateCloseUTxOHash . ChangeOutput 0 <$> do
mutatedUTxOHash <- genHash `suchThat` ((/= healthyClosedUTxOHash) . toBuiltin)
pure $ changeHeadOutputDatum (replaceUtxoHash $ toBuiltin mutatedUTxOHash) headTxOut
, SomeMutation (Just $ toErrorCode IncorrectClosedContestationDeadline) MutateContestationDeadline <$> do
mutatedDeadline <- genMutatedDeadline
pure $ ChangeOutput 0 $ changeHeadOutputDatum (replaceContestationDeadline mutatedDeadline) headTxOut
, SomeMutation (Just $ toErrorCode InfiniteLowerBound) MutateInfiniteLowerBound . ChangeValidityLowerBound <$> do
pure TxValidityNoLowerBound
, SomeMutation (Just $ toErrorCode InfiniteUpperBound) MutateInfiniteUpperBound . ChangeValidityUpperBound <$> do
pure TxValidityNoUpperBound
, SomeMutation (Just $ toErrorCode HasBoundedValidityCheckFailed) MutateValidityInterval <$> do
(lowerSlotNo, upperSlotNo, adjustedContestationDeadline) <- genOversizedTransactionValidity
pure $
Changes
[ ChangeValidityInterval (TxValidityLowerBound lowerSlotNo, TxValidityUpperBound upperSlotNo)
, ChangeOutput 0 $ changeHeadOutputDatum (replaceContestationDeadline adjustedContestationDeadline) headTxOut
]
, -- 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
otherHeadId <- headPolicyId <$> arbitrary `suchThat` (/= Fixture.testSeedInput)
pure $
Changes
[ ChangeOutput 0 (replacePolicyIdWith Fixture.testPolicyId otherHeadId headTxOut)
, ChangeInput
healthyOpenHeadTxIn
(replacePolicyIdWith Fixture.testPolicyId otherHeadId healthyOpenHeadTxOut)
( Just $
toScriptData
( Head.Close
{ signature =
toPlutusSignatures $
healthySignature healthyCloseSnapshotNumber
}
)
)
]
, SomeMutation (Just $ toErrorCode MintingOrBurningIsForbidden) MutateTokenMintingOrBurning
<$> (changeMintedTokens tx =<< genMintedOrBurnedValue)
, SomeMutation (Just $ toErrorCode ContestersNonEmpty) MutateContesters . ChangeOutput 0 <$> do
mutatedContesters <- listOf1 $ PubKeyHash . toBuiltin <$> genHash
pure $ headTxOut & changeHeadOutputDatum (replaceContesters mutatedContesters)
, SomeMutation (Just $ toErrorCode HeadValueIsNotPreserved) MutateValueInOutput <$> do
newValue <- genValue
pure $ ChangeOutput 0 (headTxOut{txOutValue = newValue})
]
where
genOversizedTransactionValidity = do
-- Implicit hypotheses: the slot length is and has always been 1 seconds so we can add slot with seconds
lowerValidityBound <- arbitrary :: Gen Word64
upperValidityBound <- choose (lowerValidityBound + fromIntegral healthyContestationPeriodSeconds, maxBound)
let adjustedContestationDeadline =
fromMilliSeconds . DiffMilliSeconds $ (healthyContestationPeriodSeconds + fromIntegral upperValidityBound) * 1000
pure (SlotNo lowerValidityBound, SlotNo upperValidityBound, adjustedContestationDeadline)
headTxOut = fromJust $ txOuts' tx !!? 0
data CloseInitialMutation
= MutateCloseContestationDeadline'
deriving (Generic, Show, Enum, Bounded)
-- | Mutations for the specific case of closing with the intial state.
-- We should probably validate all the mutation to this initial state but at
-- least we keep this regression test as we stumbled upon problems with the following case.
-- The nice thing to do would probably to generate either "normal" healthyCloseTx or
-- or healthyCloseInitialTx and apply all the mutations to it but we didn't manage to do that
-- right away.
genCloseInitialMutation :: (Tx, UTxO) -> Gen SomeMutation
genCloseInitialMutation (tx, _utxo) =
oneof
[ SomeMutation (Just $ toErrorCode IncorrectClosedContestationDeadline) MutateCloseContestationDeadline' <$> do
mutatedDeadline <- genMutatedDeadline
pure $ ChangeOutput 0 $ changeHeadOutputDatum (replaceContestationDeadline mutatedDeadline) headTxOut
]
where
headTxOut = fromJust $ txOuts' tx !!? 0
-- | Generate not acceptable, but interesting deadlines.
genMutatedDeadline :: Gen POSIXTime
genMutatedDeadline = do
oneof
[ valuesAroundZero
, valuesAroundDeadline
]
where
valuesAroundZero = arbitrary `suchThat` (/= deadline)
valuesAroundDeadline = arbitrary `suchThat` (/= 0) <&> (+ deadline)
deadline = posixFromUTCTime healthyContestationDeadline