-
Notifications
You must be signed in to change notification settings - Fork 86
/
DirectChainSpec.hs
542 lines (481 loc) · 24.2 KB
/
DirectChainSpec.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
{-# LANGUAGE DuplicateRecordFields #-}
module Test.DirectChainSpec where
import Hydra.Prelude
import Test.Hydra.Prelude
import CardanoClient (
QueryPoint (QueryTip),
RunningNode (..),
buildAddress,
queryTip,
queryUTxO,
submitTx,
waitForUTxO,
)
import CardanoNode (NodeLog, withCardanoNodeDevnet)
import Control.Concurrent.STM (newEmptyTMVarIO, takeTMVar)
import Control.Concurrent.STM.TMVar (putTMVar)
import Hydra.Cardano.Api (
ChainPoint (..),
CtxUTxO,
Key (SigningKey),
PaymentKey,
TxOut,
UTxO',
lovelaceToValue,
signTx,
txOutValue,
unFile,
)
import Hydra.Chain (
Chain (Chain, draftCommitTx, postTx),
ChainEvent (..),
HeadParameters (..),
OnChainTx (..),
PostChainTx (..),
PostTxError (..),
initHistory,
)
import Hydra.Chain.Direct (
IntersectionNotFoundException (..),
loadChainContext,
mkTinyWallet,
withDirectChain,
)
import Hydra.Chain.Direct.Handlers (DirectChainLog)
import Hydra.Chain.Direct.ScriptRegistry (queryScriptRegistry)
import Hydra.Chain.Direct.State (initialChainState)
import Hydra.Chain.Direct.Tx (verificationKeyToOnChainId)
import Hydra.Cluster.Faucet (
FaucetLog,
publishHydraScriptsAs,
seedFromFaucet,
seedFromFaucet_,
)
import Hydra.Cluster.Fixture (
Actor (Alice, Bob, Carol, Faucet),
alice,
aliceSk,
bob,
carol,
cperiod,
)
import Hydra.Cluster.Util (chainConfigFor, keysFor, modifyConfig, readConfigFile)
import Hydra.Crypto (aggregate, sign)
import Hydra.HeadId (HeadId, HeadSeed (..))
import Hydra.Ledger (IsTx (..))
import Hydra.Ledger.Cardano (Tx, genKeyPair, mkBlueprintTx, utxoFromTxOutWithWitness, utxoToTxOutWithWitness)
import Hydra.Logging (Tracer, nullTracer, showLogsOnFailure)
import Hydra.OnChainId (OnChainId)
import Hydra.Options (
ChainConfig (..),
DirectChainConfig (..),
toArgNetworkId,
)
import Hydra.Party (Party)
import Hydra.Snapshot (ConfirmedSnapshot (..), Snapshot (..))
import System.FilePath ((</>))
import System.Process (proc, readCreateProcess)
import Test.QuickCheck (generate)
spec :: Spec
spec = around (showLogsOnFailure "DirectChainSpec") $ do
it "can init and abort a head given nothing has been committed" $ \tracer -> do
withTempDir "hydra-cluster" $ \tmp -> do
withCardanoNodeDevnet (contramap FromNode tracer) tmp $ \node@RunningNode{nodeSocket} -> do
(aliceCardanoVk, _) <- keysFor Alice
seedFromFaucet_ node aliceCardanoVk 100_000_000 (contramap FromFaucet tracer)
hydraScriptsTxId <- publishHydraScriptsAs node Faucet
-- Alice setup
aliceChainConfig <- chainConfigFor Alice tmp nodeSocket hydraScriptsTxId [Bob, Carol] cperiod
withDirectChainTest (contramap (FromDirectChain "alice") tracer) aliceChainConfig alice $ \aliceChain@DirectChainTest{postTx} -> do
-- Bob setup
bobChainConfig <- chainConfigFor Bob tmp nodeSocket hydraScriptsTxId [Alice, Carol] cperiod
withDirectChainTest nullTracer bobChainConfig bob $ \bobChain@DirectChainTest{} -> do
-- Scenario
participants <- loadParticipants [Alice, Bob, Carol]
let headParameters = HeadParameters cperiod [alice, bob, carol]
postTx $ InitTx{participants, headParameters}
(aliceHeadId, aliceHeadSeed) <- aliceChain `observesInTimeSatisfying` hasInitTxWith headParameters participants
(bobHeadId, bobHeadSeed) <- bobChain `observesInTimeSatisfying` hasInitTxWith headParameters participants
aliceHeadSeed `shouldBe` bobHeadSeed
postTx $ AbortTx{utxo = mempty, headSeed = aliceHeadSeed}
aliceChain `observesInTime` OnAbortTx{headId = aliceHeadId}
bobChain `observesInTime` OnAbortTx{headId = bobHeadId}
it "can init and abort a 2-parties head after one party has committed" $ \tracer -> do
withTempDir "hydra-cluster" $ \tmp -> do
withCardanoNodeDevnet (contramap FromNode tracer) tmp $ \node@RunningNode{nodeSocket, networkId} -> do
hydraScriptsTxId <- publishHydraScriptsAs node Faucet
-- Alice setup
(aliceCardanoVk, _) <- keysFor Alice
seedFromFaucet_ node aliceCardanoVk 100_000_000 (contramap FromFaucet tracer)
aliceChainConfig <- chainConfigFor Alice tmp nodeSocket hydraScriptsTxId [Bob, Carol] cperiod
withDirectChainTest (contramap (FromDirectChain "alice") tracer) aliceChainConfig alice $
\aliceChain@DirectChainTest{postTx} -> do
-- Bob setup
bobChainConfig <- chainConfigFor Bob tmp nodeSocket hydraScriptsTxId [Alice, Carol] cperiod
withDirectChainTest (contramap (FromDirectChain "bob") tracer) bobChainConfig bob $
\bobChain@DirectChainTest{} -> do
-- Scenario
let aliceCommitment = 66_000_000
-- Mimic "external commit" by using different keys for Alice.
(aliceExternalVk, aliceExternalSk) <- generate genKeyPair
aliceUTxO <- seedFromFaucet node aliceExternalVk aliceCommitment (contramap FromFaucet tracer)
participants <- loadParticipants [Alice, Bob, Carol]
let headParameters = HeadParameters cperiod [alice, bob, carol]
postTx $ InitTx{participants, headParameters}
(aliceHeadId, aliceHeadSeed) <- aliceChain `observesInTimeSatisfying` hasInitTxWith headParameters participants
(bobHeadId, bobHeadSeed) <- bobChain `observesInTimeSatisfying` hasInitTxWith headParameters participants
aliceHeadSeed `shouldBe` bobHeadSeed
externalCommit node aliceChain aliceExternalSk aliceHeadId aliceUTxO
aliceChain `observesInTime` OnCommitTx aliceHeadId alice aliceUTxO
bobChain `observesInTime` OnCommitTx bobHeadId alice aliceUTxO
postTx $ AbortTx{utxo = aliceUTxO, headSeed = aliceHeadSeed}
--
aliceChain `observesInTime` OnAbortTx{headId = aliceHeadId}
bobChain `observesInTime` OnAbortTx{headId = bobHeadId}
let aliceExternalAddress = buildAddress aliceExternalVk networkId
-- Expect that Alice got her committed value back to her
-- external address
utxo <- queryUTxO networkId nodeSocket QueryTip [aliceExternalAddress]
let aliceValues = txOutValue <$> toList utxo
aliceValues `shouldContain` [lovelaceToValue aliceCommitment]
it "cannot abort a non-participating head" $ \tracer ->
withTempDir "hydra-cluster" $ \tmp -> do
withCardanoNodeDevnet (contramap FromNode tracer) tmp $ \node@RunningNode{nodeSocket} -> do
hydraScriptsTxId <- publishHydraScriptsAs node Faucet
-- Alice setup
(aliceCardanoVk, _) <- keysFor Alice
seedFromFaucet_ node aliceCardanoVk 100_000_000 (contramap FromFaucet tracer)
aliceChainConfig <- chainConfigFor Alice tmp nodeSocket hydraScriptsTxId [] cperiod
withDirectChainTest (contramap (FromDirectChain "alice") tracer) aliceChainConfig alice $
\aliceChain@DirectChainTest{postTx = alicePostTx} -> do
-- Bob setup
(bobCardanoVk, _) <- keysFor Bob
seedFromFaucet_ node bobCardanoVk 100_000_000 (contramap FromFaucet tracer)
bobChainConfig <- chainConfigFor Bob tmp nodeSocket hydraScriptsTxId [] cperiod
withDirectChainTest nullTracer bobChainConfig bob $
\bobChain@DirectChainTest{postTx = bobPostTx} -> do
-- Scenario
aliceParticipants <- loadParticipants [Carol, Alice]
let headParameters = HeadParameters cperiod [alice, carol]
alicePostTx $ InitTx{participants = aliceParticipants, headParameters}
void $ aliceChain `observesInTimeSatisfying` hasInitTxWith headParameters aliceParticipants
-- Expect bob's chain layer to see the init of alice and carols exlusive head
headSeed <- snd <$> bobChain `observesInTimeSatisfying` hasInitTxWith headParameters aliceParticipants
-- headSeed2 <- generate arbitrary
bobPostTx AbortTx{utxo = mempty, headSeed}
`shouldThrow` \case
-- Note: We expect bob to be able to construct the abort tx but failing to submit
FailedToConstructAbortTx @Tx -> False
ScriptFailedInWallet{} -> True
_ -> False
it "can commit" $ \tracer ->
withTempDir "hydra-cluster" $ \tmp -> do
withCardanoNodeDevnet (contramap FromNode tracer) tmp $ \node@RunningNode{nodeSocket} -> do
hydraScriptsTxId <- publishHydraScriptsAs node Faucet
-- Alice setup
(aliceCardanoVk, aliceCardanoSk) <- keysFor Alice
seedFromFaucet_ node aliceCardanoVk 100_000_000 (contramap FromFaucet tracer)
aliceChainConfig <- chainConfigFor Alice tmp nodeSocket hydraScriptsTxId [] cperiod
withDirectChainTest (contramap (FromDirectChain "alice") tracer) aliceChainConfig alice $
\aliceChain@DirectChainTest{postTx} -> do
aliceUTxO <- seedFromFaucet node aliceCardanoVk 1_000_000 (contramap FromFaucet tracer)
participants <- loadParticipants [Alice]
let headParameters = HeadParameters cperiod [alice]
postTx $ InitTx{participants, headParameters}
headId <- fst <$> aliceChain `observesInTimeSatisfying` hasInitTxWith headParameters participants
-- deliberately use alice's key known to hydra-node to trigger the error
externalCommit node aliceChain aliceCardanoSk headId aliceUTxO
`shouldThrow` \case
(SpendingNodeUtxoForbidden :: PostTxError Tx) -> True
_ -> False
(aliceExternalVk, aliceExternalSk) <- generate genKeyPair
newAliceUTxO <- seedFromFaucet node aliceExternalVk 1_000_000 (contramap FromFaucet tracer)
externalCommit node aliceChain aliceExternalSk headId newAliceUTxO
aliceChain `observesInTime` OnCommitTx headId alice newAliceUTxO
it "can commit empty UTxO" $ \tracer -> do
withTempDir "hydra-cluster" $ \tmp -> do
withCardanoNodeDevnet (contramap FromNode tracer) tmp $ \node@RunningNode{nodeSocket} -> do
hydraScriptsTxId <- publishHydraScriptsAs node Faucet
-- Alice setup
(aliceCardanoVk, _) <- keysFor Alice
seedFromFaucet_ node aliceCardanoVk 100_000_000 (contramap FromFaucet tracer)
aliceChainConfig <- chainConfigFor Alice tmp nodeSocket hydraScriptsTxId [] cperiod
withDirectChainTest (contramap (FromDirectChain "alice") tracer) aliceChainConfig alice $
\aliceChain@DirectChainTest{postTx} -> do
-- Scenario
participants <- loadParticipants [Alice]
let headParameters = HeadParameters cperiod [alice]
postTx $ InitTx{participants, headParameters}
headId <- fst <$> aliceChain `observesInTimeSatisfying` hasInitTxWith headParameters participants
(_, aliceExternalSk) <- generate genKeyPair
externalCommit node aliceChain aliceExternalSk headId mempty
aliceChain `observesInTime` OnCommitTx headId alice mempty
it "can open, close & fanout a Head" $ \tracer -> do
withTempDir "hydra-cluster" $ \tmp -> do
withCardanoNodeDevnet (contramap FromNode tracer) tmp $ \node@RunningNode{nodeSocket, networkId} -> do
hydraScriptsTxId <- publishHydraScriptsAs node Faucet
-- Alice setup
(aliceCardanoVk, _) <- keysFor Alice
seedFromFaucet_ node aliceCardanoVk 100_000_000 (contramap FromFaucet tracer)
aliceChainConfig <- chainConfigFor Alice tmp nodeSocket hydraScriptsTxId [] cperiod
withDirectChainTest (contramap (FromDirectChain "alice") tracer) aliceChainConfig alice $
\aliceChain@DirectChainTest{postTx} -> do
-- Scenario
(aliceExternalVk, aliceExternalSk) <- generate genKeyPair
someUTxO <- seedFromFaucet node aliceExternalVk 1_000_000 (contramap FromFaucet tracer)
participants <- loadParticipants [Alice]
let headParameters = HeadParameters cperiod [alice]
postTx $ InitTx{participants, headParameters}
(headId, headSeed) <- aliceChain `observesInTimeSatisfying` hasInitTxWith headParameters participants
externalCommit node aliceChain aliceExternalSk headId someUTxO
aliceChain `observesInTime` OnCommitTx headId alice someUTxO
postTx $ CollectComTx someUTxO headId headParameters
aliceChain `observesInTime` OnCollectComTx{headId}
let snapshot =
Snapshot
{ headId
, number = 1
, utxo = someUTxO
, confirmed = []
}
postTx . CloseTx headId headParameters $
ConfirmedSnapshot
{ snapshot
, signatures = aggregate [sign aliceSk snapshot]
}
deadline <-
waitMatch aliceChain $ \case
Observation{observedTx = OnCloseTx{snapshotNumber, contestationDeadline}}
| snapshotNumber == 1 -> Just contestationDeadline
_ -> Nothing
now <- getCurrentTime
unless (deadline > now) $
failure $
"contestationDeadline in the past: " <> show deadline <> ", now: " <> show now
delayUntil deadline
waitMatch aliceChain $ \case
Tick t _ | t > deadline -> Just ()
_ -> Nothing
postTx $
FanoutTx
{ utxo = someUTxO
, headSeed
, contestationDeadline = deadline
}
aliceChain `observesInTime` OnFanoutTx headId
failAfter 5 $
waitForUTxO networkId nodeSocket someUTxO
it "can restart head to point in the past and replay on-chain events" $ \tracer -> do
withTempDir "hydra-cluster" $ \tmp -> do
withCardanoNodeDevnet (contramap FromNode tracer) tmp $ \node@RunningNode{nodeSocket, networkId} -> do
hydraScriptsTxId <- publishHydraScriptsAs node Faucet
(aliceCardanoVk, _) <- keysFor Alice
seedFromFaucet_ node aliceCardanoVk 100_000_000 (contramap FromFaucet tracer)
-- Alice setup
aliceChainConfig <- chainConfigFor Alice tmp nodeSocket hydraScriptsTxId [] cperiod
participants <- loadParticipants [Alice]
let headParameters = HeadParameters cperiod [alice]
-- Scenario
tip <- withDirectChainTest (contramap (FromDirectChain "alice") tracer) aliceChainConfig alice $
\aliceChain@DirectChainTest{postTx} -> do
tip <- queryTip networkId nodeSocket
postTx $ InitTx{participants, headParameters}
void $ aliceChain `observesInTimeSatisfying` hasInitTxWith headParameters participants
pure tip
let aliceChainConfig' = aliceChainConfig & modifyConfig (\cfg -> cfg{startChainFrom = Just tip})
-- REVIEW: It's a bit weird now that we would use the original chain
-- state here. Does this test even make sense with persistence?
withDirectChainTest (contramap (FromDirectChain "alice") tracer) aliceChainConfig' alice $
\aliceChain@DirectChainTest{} ->
void $ aliceChain `observesInTimeSatisfying` hasInitTxWith headParameters participants
it "cannot restart head to an unknown point" $ \tracer -> do
withTempDir "hydra-cluster" $ \tmp -> do
withCardanoNodeDevnet (contramap FromNode tracer) tmp $ \node@RunningNode{nodeSocket} -> do
(aliceCardanoVk, _) <- keysFor Alice
seedFromFaucet_ node aliceCardanoVk 100_000_000 (contramap FromFaucet tracer)
hydraScriptsTxId <- publishHydraScriptsAs node Faucet
let headerHash = fromString (replicate 64 '0')
let fakeTip = ChainPoint 42 headerHash
aliceChainConfig <-
chainConfigFor Alice tmp nodeSocket hydraScriptsTxId [] cperiod
<&> modifyConfig (\cfg -> cfg{startChainFrom = Just fakeTip})
let action = withDirectChainTest (contramap (FromDirectChain "alice") tracer) aliceChainConfig alice $ \_ ->
threadDelay 5 >> fail "should not execute main action but did?"
action `shouldThrow` \case
IntersectionNotFound{} -> True
it "can publish and query reference scripts in a timely manner" $ \tracer -> do
withTempDir "hydra-cluster" $ \tmp -> do
withCardanoNodeDevnet (contramap FromNode tracer) tmp $ \RunningNode{nodeSocket, networkId} -> do
readConfigFile ("credentials" </> "faucet.sk") >>= writeFileBS (tmp </> "faucet.sk")
hydraScriptsTxIdStr <-
readCreateProcess
( proc
"hydra-node"
( "publish-scripts"
: mconcat
[ ["--node-socket", unFile nodeSocket]
, toArgNetworkId networkId
, ["--cardano-signing-key", tmp </> "faucet.sk"]
]
)
)
""
let hydraScriptsTxId = fromString hydraScriptsTxIdStr
failAfter 5 $ void $ queryScriptRegistry networkId nodeSocket hydraScriptsTxId
it "can only contest once" $ \tracer -> do
withTempDir "hydra-cluster" $ \tmp -> do
withCardanoNodeDevnet (contramap FromNode tracer) tmp $ \node@RunningNode{nodeSocket} -> do
hydraScriptsTxId <- publishHydraScriptsAs node Faucet
-- Alice setup
(aliceCardanoVk, _) <- keysFor Alice
seedFromFaucet_ node aliceCardanoVk 100_000_000 (contramap FromFaucet tracer)
aliceChainConfig <- chainConfigFor Alice tmp nodeSocket hydraScriptsTxId [] cperiod
withDirectChainTest (contramap (FromDirectChain "alice") tracer) aliceChainConfig alice $
\aliceChain@DirectChainTest{postTx} -> do
(aliceExternalVk, aliceExternalSk) <- generate genKeyPair
someUTxO <- seedFromFaucet node aliceExternalVk 1_000_000 (contramap FromFaucet tracer)
participants <- loadParticipants [Alice]
let headParameters = HeadParameters cperiod [alice]
postTx $ InitTx{participants, headParameters}
headId <- fst <$> aliceChain `observesInTimeSatisfying` hasInitTxWith headParameters participants
externalCommit node aliceChain aliceExternalSk headId someUTxO
aliceChain `observesInTime` OnCommitTx headId alice someUTxO
postTx $ CollectComTx someUTxO headId headParameters
-- Head is open with someUTxO
aliceChain `observesInTime` OnCollectComTx headId
-- Alice close with the initial snapshot U0
postTx $ CloseTx headId headParameters InitialSnapshot{headId, initialUTxO = someUTxO}
deadline <- waitMatch aliceChain $ \case
Observation{observedTx = OnCloseTx{snapshotNumber, contestationDeadline}}
| snapshotNumber == 0 -> Just contestationDeadline
_ -> Nothing
-- Alice contests with some snapshot U1 -> successful
let snapshot1 =
Snapshot
{ headId
, number = 1
, utxo = someUTxO
, confirmed = []
}
postTx . ContestTx headId headParameters $
ConfirmedSnapshot
{ snapshot = snapshot1
, signatures = aggregate [sign aliceSk snapshot1]
}
aliceChain `observesInTime` OnContestTx{headId, snapshotNumber = 1, contestationDeadline = deadline}
-- Alice contests with some snapshot U2 -> expect fail
let snapshot2 =
Snapshot
{ headId
, number = 2
, utxo = someUTxO
, confirmed = []
}
let contestAgain =
postTx . ContestTx headId headParameters $
ConfirmedSnapshot
{ snapshot = snapshot2
, signatures = aggregate [sign aliceSk snapshot2]
}
-- NOTE: We deliberately expect the transaction creation and
-- submission code of the Chain.Direct module to fail here because
-- the scripts don't validate. That is, the on-chain code prevented
-- this from happening and NOT any off-chain guard we added. Also
-- note, that we don't try to check whether it's failing for the
-- right reason here (see corresponding mutation test for this).
contestAgain `shouldThrow` \case
(ScriptFailedInWallet{} :: PostTxError Tx) -> True
_ -> False
data DirectChainTestLog
= FromNode NodeLog
| FromDirectChain Text DirectChainLog
| FromFaucet FaucetLog
deriving stock (Show, Generic)
deriving anyclass (ToJSON)
data DirectChainTest tx m = DirectChainTest
{ postTx :: PostChainTx tx -> m ()
, waitCallback :: m (ChainEvent tx)
, draftCommitTx :: HeadId -> UTxOType tx -> tx -> m tx
}
-- | Wrapper around 'withDirectChain' that threads a 'ChainStateType tx' through
-- 'postTx' and 'waitCallback' calls.
withDirectChainTest ::
Tracer IO DirectChainLog ->
ChainConfig ->
Party ->
(DirectChainTest Tx IO -> IO a) ->
IO a
withDirectChainTest tracer config party action = do
directConfig <- case config of
Direct cfg -> pure cfg
otherConfig -> failure $ "unexpected chainConfig: " <> show otherConfig
ctx <- loadChainContext directConfig party
eventMVar <- newEmptyTMVarIO
let callback event = atomically $ putTMVar eventMVar event
wallet <- mkTinyWallet tracer directConfig
withDirectChain tracer directConfig ctx wallet (initHistory initialChainState) callback $ \Chain{postTx, draftCommitTx} -> do
action
DirectChainTest
{ postTx
, waitCallback = atomically $ takeTMVar eventMVar
, draftCommitTx = \headId utxo blueprintTx -> do
eTx <- draftCommitTx headId utxo blueprintTx
case eTx of
Left e -> throwIO e
Right tx -> pure tx
}
hasInitTxWith :: (HasCallStack, IsTx tx) => HeadParameters -> [OnChainId] -> OnChainTx tx -> IO (HeadId, HeadSeed)
hasInitTxWith HeadParameters{contestationPeriod = expectedContestationPeriod, parties = expectedParties} expectedParticipants = \case
OnInitTx{headId, headSeed, headParameters = HeadParameters{contestationPeriod, parties}, participants} -> do
expectedParticipants `shouldMatchList` participants
expectedContestationPeriod `shouldBe` contestationPeriod
expectedParties `shouldMatchList` parties
pure (headId, headSeed)
tx -> failure ("Unexpected observation: " <> show tx)
observesInTime :: IsTx tx => DirectChainTest tx IO -> OnChainTx tx -> IO ()
observesInTime chain expected =
observesInTimeSatisfying chain (`shouldBe` expected)
observesInTimeSatisfying :: DirectChainTest tx IO -> (OnChainTx tx -> IO a) -> IO a
observesInTimeSatisfying DirectChainTest{waitCallback} check =
failAfter 10 go
where
go = do
e <- waitCallback
case e of
Observation{observedTx} ->
check observedTx
_TickOrRollback ->
go
waitMatch :: DirectChainTest tx IO -> (ChainEvent tx -> Maybe b) -> IO b
waitMatch DirectChainTest{waitCallback} match = go
where
go = do
a <- waitCallback
maybe go pure (match a)
delayUntil :: (MonadDelay m, MonadTime m) => UTCTime -> m ()
delayUntil target = do
now <- getCurrentTime
threadDelay . realToFrac $ diffUTCTime target now
-- Commit using a wallet/external unknown to a hydra-node.
externalCommit ::
RunningNode ->
DirectChainTest Tx IO ->
SigningKey PaymentKey ->
HeadId ->
UTxO' (TxOut CtxUTxO) ->
IO ()
externalCommit node hydraClient externalSk headId utxoToCommit = do
let utxoToCommit' = utxoToTxOutWithWitness utxoToCommit
blueprintTx = mkBlueprintTx utxoToCommit'
utxo = utxoFromTxOutWithWitness utxoToCommit'
commitTx <- draftCommitTx headId utxo blueprintTx
let signedTx = signTx externalSk commitTx
submitTx node signedTx
where
DirectChainTest{draftCommitTx} = hydraClient
-- | Load key files for given 'Actor's (see keysFor) and directly convert them to 'OnChainId'.
loadParticipants :: [Actor] -> IO [OnChainId]
loadParticipants actors =
forM actors $ \a -> do
(vk, _) <- keysFor a
pure $ verificationKeyToOnChainId vk