-
Notifications
You must be signed in to change notification settings - Fork 86
/
Decrement.hs
169 lines (145 loc) · 5.01 KB
/
Decrement.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
{-# LANGUAGE DuplicateRecordFields #-}
module Hydra.Chain.Direct.Contract.Decrement where
import Hydra.Cardano.Api
import Hydra.Chain.Direct.Contract.Mutation (
Mutation (..),
SomeMutation (..),
modifyInlineDatum,
replaceParties,
replaceSnapshotNumber,
)
import Hydra.Prelude hiding (label)
import Cardano.Api.UTxO as UTxO
import Data.Maybe (fromJust)
import Hydra.Chain (HeadParameters (..))
import Hydra.Chain.Direct.Fixture (testNetworkId, testPolicyId)
import Hydra.Chain.Direct.ScriptRegistry (genScriptRegistry, registryUTxO)
import Hydra.Chain.Direct.Tx (
decrementTx,
mkHeadId,
mkHeadOutput,
)
import Hydra.ContestationPeriod (ContestationPeriod, toChain)
import Hydra.Contract.Error (ToErrorCode (..))
import Hydra.Contract.HeadError (HeadError (..))
import Hydra.Contract.HeadState qualified as Head
import Hydra.Crypto (MultiSignature (..))
import Hydra.Data.Party qualified as OnChain
import Hydra.Ledger (IsTx (hashUTxO, withoutUTxO))
import Hydra.Ledger.Cardano (
adaOnly,
genTxOut,
genVerificationKey,
)
import Hydra.Party (Party, partyToChain, vkey)
import Hydra.Plutus.Orphans ()
import Hydra.Snapshot (Snapshot (..), SnapshotNumber)
import PlutusTx.Builtins (toBuiltin)
import Test.Hydra.Fixture (genForParty)
import Test.QuickCheck (arbitrarySizedNatural, elements, oneof)
import Test.QuickCheck.Gen (suchThat)
import Test.QuickCheck.Instances ()
healthyDecrementTx :: (Tx, UTxO)
healthyDecrementTx =
(tx, lookupUTxO)
where
lookupUTxO =
UTxO.singleton (headInput, headOutput)
<> registryUTxO scriptRegistry
tx =
decrementTx
scriptRegistry
somePartyCardanoVerificationKey
(mkHeadId testPolicyId)
parameters
(headInput, headOutput)
healthySnapshot
multisig
multisig = HydraMultiSignature $ arbitrary `generateWith` 42
parameters =
HeadParameters
{ parties = healthyParties
, contestationPeriod = healthyContestationPeriod
}
somePartyCardanoVerificationKey =
elements healthyParticipants `generateWith` 42
scriptRegistry = genScriptRegistry `generateWith` 42
headInput = generateWith arbitrary 42
headOutput' =
mkHeadOutput testNetworkId testPolicyId (toUTxOContext $ mkTxOutDatumInline healthyDatum)
headOutput = modifyTxOutValue (<> participationTokens) headOutput'
participationTokens =
valueFromList $
map
( \party ->
(AssetId testPolicyId (AssetName . serialiseToRawBytes . verificationKeyHash . vkey $ party), 1)
)
healthyParties
healthySnapshotNumber :: SnapshotNumber
healthySnapshotNumber = 1
healthySnapshot :: Snapshot Tx
healthySnapshot =
let (utxoToDecommit', utxo) = splitDecommitUTxO healthyUTxO
in Snapshot
{ headId = mkHeadId testPolicyId
, number = healthySnapshotNumber
, utxo
, confirmed = []
, utxoToDecommit = Just utxoToDecommit'
}
splitDecommitUTxO :: UTxO -> (UTxO, UTxO)
splitDecommitUTxO utxo =
case UTxO.pairs utxo of
[] -> error "empty utxo in splitDecommitUTxO"
(decommit : _rest) ->
let decommitUTxO' = UTxO.fromPairs [decommit]
in (utxo `withoutUTxO` decommitUTxO', decommitUTxO')
healthyContestationPeriod :: ContestationPeriod
healthyContestationPeriod =
arbitrary `generateWith` 42
healthyParticipants :: [VerificationKey PaymentKey]
healthyParticipants =
genForParty genVerificationKey <$> healthyParties
healthyUTxO :: UTxO
healthyUTxO =
adaOnly
<$> generateWith
(UTxO.fromPairs . (: []) <$> ((,) <$> genTxIn <*> genTxOut))
42
healthyDatum :: Head.State
healthyDatum =
Head.Open
{ utxoHash = toBuiltin $ hashUTxO @Tx healthyUTxO
, parties = healthyOnChainParties
, contestationPeriod = toChain healthyContestationPeriod
, snapshotNumber = toInteger healthySnapshotNumber
, headId = toPlutusCurrencySymbol testPolicyId
}
healthyParties :: [Party]
healthyParties =
[ generateWith arbitrary i | i <- [1 .. 3]
]
healthyOnChainParties :: [OnChain.Party]
healthyOnChainParties = partyToChain <$> healthyParties
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
genDecrementMutation (tx, _utxo) =
oneof
[ 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