/
CollectCom.hs
269 lines (242 loc) · 9.75 KB
/
CollectCom.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
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Hydra.Chain.Direct.Contract.CollectCom where
import Hydra.Cardano.Api
import Hydra.Prelude hiding (label)
import qualified Cardano.Api.UTxO as UTxO
import qualified Data.Map as Map
import Data.Maybe (fromJust)
import Hydra.Chain.Direct.Contract.Gen (genForParty, genHash, genMintedOrBurnedValue)
import Hydra.Chain.Direct.Contract.Mutation (
Mutation (..),
SomeMutation (..),
changeHeadOutputDatum,
changeMintedTokens,
replaceParties,
)
import Hydra.Chain.Direct.Fixture (
testNetworkId,
testPolicyId,
testSeedInput,
)
import Hydra.Chain.Direct.ScriptRegistry (genScriptRegistry, registryUTxO)
import Hydra.Chain.Direct.Tx (
InitialThreadOutput (..),
assetNameFromVerificationKey,
collectComTx,
headValue,
mkCommitDatum,
mkHeadId,
mkHeadOutput,
mkInitialOutput,
)
import qualified Hydra.Contract.Commit as Commit
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 qualified Hydra.Data.ContestationPeriod as OnChain
import qualified Hydra.Data.Party as OnChain
import Hydra.Ledger.Cardano (genAdaOnlyUTxO, genAddressInEra, genTxIn, genVerificationKey)
import Hydra.Party (Party, partyToChain)
import Plutus.Orphans ()
import Plutus.V2.Ledger.Api (toBuiltin, toData)
import Test.QuickCheck (choose, elements, oneof, suchThat)
import Test.QuickCheck.Instances ()
import qualified Prelude
--
-- CollectComTx
--
healthyCollectComTx :: (Tx, UTxO)
healthyCollectComTx =
(tx, lookupUTxO)
where
lookupUTxO =
UTxO.singleton (healthyHeadInput, healthyHeadResolvedInput)
<> UTxO (txOut <$> healthyCommits)
<> registryUTxO scriptRegistry
tx =
collectComTx
testNetworkId
scriptRegistry
somePartyCardanoVerificationKey
initialThreadOutput
((txOut &&& scriptData) <$> healthyCommits)
(mkHeadId testPolicyId)
scriptRegistry = genScriptRegistry `generateWith` 42
somePartyCardanoVerificationKey = flip generateWith 42 $ do
genForParty genVerificationKey <$> elements healthyParties
headDatum = fromPlutusData $ toData healthyCollectComInitialDatum
initialThreadOutput =
InitialThreadOutput
{ initialThreadUTxO = (healthyHeadInput, healthyHeadResolvedInput, headDatum)
, initialParties = healthyOnChainParties
, initialContestationPeriod = healthyContestationPeriod
}
healthyCommits :: Map TxIn HealthyCommit
healthyCommits =
(uncurry healthyCommitOutput <$> zip healthyParties committedUTxO)
& Map.fromList
where
committedUTxO =
generateWith
(replicateM (length healthyParties) genCommittableTxOut)
42
healthyContestationPeriod :: OnChain.ContestationPeriod
healthyContestationPeriod =
arbitrary `generateWith` 42
healthyHeadInput :: TxIn
healthyHeadInput =
generateWith arbitrary 42
healthyHeadResolvedInput :: TxOut CtxUTxO
healthyHeadResolvedInput =
mkHeadOutput
testNetworkId
testPolicyId
(toUTxOContext $ mkTxOutDatum healthyCollectComInitialDatum)
healthyCollectComInitialDatum :: Head.State
healthyCollectComInitialDatum =
Head.Initial
{ contestationPeriod = healthyContestationPeriod
, parties = healthyOnChainParties
, headId = toPlutusCurrencySymbol testPolicyId
, seed = toPlutusTxOutRef testSeedInput
}
healthyOnChainParties :: [OnChain.Party]
healthyOnChainParties =
partyToChain <$> healthyParties
healthyParties :: [Party]
healthyParties = flip generateWith 42 $ do
alice <- arbitrary
bob <- arbitrary
carol <- arbitrary
pure [alice, bob, carol]
genCommittableTxOut :: Gen (TxIn, TxOut CtxUTxO)
genCommittableTxOut =
Prelude.head . UTxO.pairs <$> (genAdaOnlyUTxO `suchThat` (\u -> length u > 1))
data HealthyCommit = HealthyCommit
{ cardanoKey :: VerificationKey PaymentKey
, txOut :: TxOut CtxUTxO
, scriptData :: ScriptData
}
deriving (Show)
healthyCommitOutput ::
Party ->
(TxIn, TxOut CtxUTxO) ->
(TxIn, HealthyCommit)
healthyCommitOutput party committed =
( txIn
, HealthyCommit
{ cardanoKey
, txOut = toCtxUTxOTxOut (TxOut commitAddress commitValue (mkTxOutDatum commitDatum) ReferenceScriptNone)
, scriptData = fromPlutusData (toData commitDatum)
}
)
where
txIn = genTxIn `genForParty` party
cardanoKey = genVerificationKey `genForParty` party
commitScript =
fromPlutusScript Commit.validatorScript
commitAddress =
mkScriptAddress @PlutusScriptV2 testNetworkId commitScript
commitValue =
headValue
<> (txOutValue . snd) committed
<> valueFromList
[ (AssetId testPolicyId (assetNameFromVerificationKey cardanoKey), 1)
]
commitDatum =
mkCommitDatum party (Just committed) (toPlutusCurrencySymbol $ headPolicyId healthyHeadInput)
data CollectComMutation
= -- | Ensures collectCom does not allow any output address but νHead.
NotContinueContract
| -- | Needs to prevent that not all value is collected into the head output.
ExtractSomeValue
| MutateOpenUTxOHash
| -- | Ensures collectCom cannot collect from an initial UTxO.
MutateCommitToInitial
| -- | Every party should have commited and been taken into account for the
-- collectCom transaction to be valid. Here we increase the number of
-- parties in input and output but keep the commits unchanged. This
-- simulates the situation where one participant would not have commited
-- already or whose commit would have been ignored by the collectCom
-- transaction.
MutateNumberOfParties
| MutateHeadId
| MutateRequiredSigner
| -- | Minting or burning of tokens should not be possible in collectCom.
MutateTokenMintingOrBurning
deriving (Generic, Show, Enum, Bounded)
genCollectComMutation :: (Tx, UTxO) -> Gen SomeMutation
genCollectComMutation (tx, _utxo) =
oneof
[ SomeMutation (Just $ toErrorCode NotPayingToHead) NotContinueContract <$> do
mutatedAddress <- genAddressInEra testNetworkId
pure $ ChangeOutput 0 (modifyTxOutAddress (const mutatedAddress) headTxOut)
, SomeMutation (Just $ toErrorCode NotAllValueCollected) ExtractSomeValue <$> do
-- Remove a random asset and quantity from headOutput
removedValue <- do
let allAssets = valueToList $ txOutValue headTxOut
nonPTs = flip filter allAssets $ \case
(AssetId pid _, _) -> pid /= testPolicyId
_ -> True
(assetId, Quantity n) <- elements nonPTs
q <- Quantity <$> choose (0, n)
pure $ valueFromList [(assetId, q)]
-- Add another output which would extract the 'removedValue'. The ledger
-- would check for this, and this is needed because the way we implement
-- collectCom checks.
extractionTxOut <- do
someAddress <- genAddressInEra testNetworkId
pure $ TxOut someAddress removedValue TxOutDatumNone ReferenceScriptNone
pure $
Changes
[ ChangeOutput 0 $ modifyTxOutValue (\v -> v <> negateValue removedValue) headTxOut
, AddOutput extractionTxOut
]
, SomeMutation (Just $ toErrorCode IncorrectUtxoHash) MutateOpenUTxOHash . ChangeOutput 0 <$> mutateUTxOHash
, SomeMutation (Just $ toErrorCode MissingCommits) MutateNumberOfParties <$> do
moreParties <- (: healthyOnChainParties) <$> arbitrary
pure $
Changes
[ ChangeInputHeadDatum $ replaceParties moreParties healthyCollectComInitialDatum
, ChangeOutput 0 $ mutatedPartiesHeadTxOut moreParties headTxOut
]
, SomeMutation (Just $ 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.
illedHeadResolvedInput <-
mkHeadOutput
<$> pure testNetworkId
<*> fmap headPolicyId (arbitrary `suchThat` (/= testSeedInput))
<*> pure (toUTxOContext $ mkTxOutDatum healthyCollectComInitialDatum)
return $ ChangeInput healthyHeadInput illedHeadResolvedInput (Just $ toScriptData Head.CollectCom)
, SomeMutation (Just $ toErrorCode SignerIsNotAParticipant) MutateRequiredSigner <$> do
newSigner <- verificationKeyHash <$> genVerificationKey
pure $ ChangeRequiredSigners [newSigner]
, SomeMutation (Just $ toErrorCode DatumNotFound) MutateCommitToInitial <$> do
-- we're satisfied with "datum not found" as the current version of the validator will consider
-- the initial input as if it were a commit input, hence fetching the datum which is expected
-- in a commit and complaining that it did not find it
(txIn, HealthyCommit{cardanoKey}) <- elements $ Map.toList healthyCommits
pure $ ChangeInput txIn (toUTxOContext $ mkInitialOutput testNetworkId testSeedInput cardanoKey) Nothing
, SomeMutation (Just $ toErrorCode MintingOrBurningIsForbidden) MutateTokenMintingOrBurning
<$> (changeMintedTokens tx =<< genMintedOrBurnedValue)
]
where
headTxOut = fromJust $ txOuts' tx !!? 0
mutatedPartiesHeadTxOut parties =
changeHeadOutputDatum $ \case
Head.Open{utxoHash, contestationPeriod, headId} ->
Head.Open{Head.parties = parties, contestationPeriod, utxoHash, headId}
st -> error $ "Unexpected state " <> show st
mutateUTxOHash = do
mutatedUTxOHash <- genHash
pure $ changeHeadOutputDatum (mutateState mutatedUTxOHash) headTxOut
mutateState mutatedUTxOHash = \case
Head.Open{parties, contestationPeriod, headId} ->
Head.Open{parties, contestationPeriod, Head.utxoHash = toBuiltin mutatedUTxOHash, headId}
st -> st