/
Tx.hs
912 lines (826 loc) · 31.9 KB
/
Tx.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE TypeApplications #-}
-- | Smart constructors for creating Hydra protocol transactions to be used in
-- the 'Hydra.Chain.Direct' way of talking to the main-chain.
--
-- This module also encapsulates the transaction format used when talking to the
-- cardano-node, which is currently different from the 'Hydra.Ledger.Cardano',
-- thus we have not yet "reached" 'isomorphism'.
module Hydra.Chain.Direct.Tx where
import Hydra.Cardano.Api
import Hydra.Prelude
import qualified Cardano.Api.UTxO as UTxO
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Base16 as Base16
import qualified Data.Map as Map
import Hydra.Chain (HeadId (..), HeadParameters (..))
import Hydra.Chain.Direct.ScriptRegistry (ScriptRegistry (..))
import Hydra.Chain.Direct.TimeHandle (PointInTime)
import Hydra.ContestationPeriod (ContestationPeriod, fromChain, toChain)
import qualified Hydra.Contract.Commit as Commit
import qualified Hydra.Contract.Head as Head
import qualified Hydra.Contract.HeadState as Head
import qualified Hydra.Contract.HeadTokens as HeadTokens
import qualified Hydra.Contract.Initial as Initial
import Hydra.Contract.MintAction (MintAction (Burn, Mint))
import Hydra.Contract.Util (hydraHeadV1)
import Hydra.Crypto (MultiSignature, toPlutusSignatures)
import Hydra.Data.ContestationPeriod (addContestationPeriod, posixFromUTCTime)
import qualified Hydra.Data.ContestationPeriod as OnChain
import qualified Hydra.Data.Party as OnChain
import Hydra.Ledger (IsTx (hashUTxO))
import Hydra.Ledger.Cardano (addReferenceInputs)
import Hydra.Ledger.Cardano.Builder (
addExtraRequiredSigners,
addInputs,
addOutputs,
addVkInputs,
burnTokens,
emptyTxBody,
mintTokens,
setValidityLowerBound,
setValidityUpperBound,
unsafeBuildTransaction,
)
import Hydra.Party (Party, partyFromChain, partyToChain)
import Hydra.Snapshot (Snapshot (..), SnapshotNumber, fromChainSnapshot)
import Plutus.Orphans ()
import Plutus.V1.Ledger.Api (CurrencySymbol)
import Plutus.V2.Ledger.Api (CurrencySymbol (CurrencySymbol), fromBuiltin, fromData, toBuiltin)
import qualified Plutus.V2.Ledger.Api as Plutus
-- | Needed on-chain data to create Head transactions.
type UTxOWithScript = (TxIn, TxOut CtxUTxO, ScriptData)
newtype UTxOHash = UTxOHash ByteString
deriving (Eq, Show, Generic)
instance ToJSON UTxOHash where
toJSON (UTxOHash bytes) =
Aeson.String . decodeUtf8 $ Base16.encode bytes
instance FromJSON UTxOHash where
parseJSON = Aeson.withText "UTxOHash" $ \cborText ->
case Base16.decode $ encodeUtf8 cborText of
Left e -> fail e
Right bs -> pure $ UTxOHash bs
-- | Representation of the Head output after an Init transaction.
data InitialThreadOutput = InitialThreadOutput
{ initialThreadUTxO :: UTxOWithScript
, initialContestationPeriod :: OnChain.ContestationPeriod
, initialParties :: [OnChain.Party]
}
deriving (Eq, Show, Generic, ToJSON, FromJSON)
-- | Representation of the Head output after a CollectCom transaction.
data OpenThreadOutput = OpenThreadOutput
{ openThreadUTxO :: UTxOWithScript
, openContestationPeriod :: OnChain.ContestationPeriod
, openParties :: [OnChain.Party]
}
deriving (Eq, Show, Generic, ToJSON, FromJSON)
data ClosedThreadOutput = ClosedThreadOutput
{ closedThreadUTxO :: UTxOWithScript
, closedParties :: [OnChain.Party]
, closedContestationDeadline :: Plutus.POSIXTime
}
deriving (Eq, Show, Generic, ToJSON, FromJSON)
hydraHeadV1AssetName :: AssetName
hydraHeadV1AssetName = AssetName (fromBuiltin hydraHeadV1)
-- FIXME: sould not be hardcoded
headValue :: Value
headValue = lovelaceToValue (Lovelace 2_000_000)
-- * Create Hydra Head transactions
-- | Create the init transaction from some 'HeadParameters' and a single TxIn
-- which will be used as unique parameter for minting NFTs.
initTx ::
NetworkId ->
-- | All participants cardano keys.
[VerificationKey PaymentKey] ->
HeadParameters ->
TxIn ->
Tx
initTx networkId cardanoKeys parameters seed =
unsafeBuildTransaction $
emptyTxBody
& addVkInputs [seed]
& addOutputs
( mkHeadOutputInitial networkId policyId parameters :
map (mkInitialOutput networkId policyId) cardanoKeys
)
& mintTokens (HeadTokens.mkHeadTokenScript seed) Mint ((hydraHeadV1AssetName, 1) : participationTokens)
where
policyId = HeadTokens.headPolicyId seed
participationTokens =
[(assetNameFromVerificationKey vk, 1) | vk <- cardanoKeys]
mkHeadOutput :: NetworkId -> PolicyId -> TxOutDatum ctx -> TxOut ctx
mkHeadOutput networkId tokenPolicyId datum =
TxOut
(mkScriptAddress @PlutusScriptV2 networkId headScript)
(headValue <> valueFromList [(AssetId tokenPolicyId hydraHeadV1AssetName, 1)])
datum
ReferenceScriptNone
where
headScript = fromPlutusScript Head.validatorScript
mkHeadOutputInitial :: NetworkId -> PolicyId -> HeadParameters -> TxOut CtxTx
mkHeadOutputInitial networkId tokenPolicyId HeadParameters{contestationPeriod, parties} =
mkHeadOutput networkId tokenPolicyId headDatum
where
headDatum =
mkTxOutDatum $
Head.Initial
(toChain contestationPeriod)
(map partyToChain parties)
(toPlutusCurrencySymbol tokenPolicyId)
mkInitialOutput :: NetworkId -> PolicyId -> VerificationKey PaymentKey -> TxOut CtxTx
mkInitialOutput networkId tokenPolicyId (verificationKeyHash -> pkh) =
TxOut initialAddress initialValue initialDatum ReferenceScriptNone
where
initialValue =
headValue <> valueFromList [(AssetId tokenPolicyId (AssetName $ serialiseToRawBytes pkh), 1)]
initialAddress =
mkScriptAddress @PlutusScriptV2 networkId initialScript
initialScript =
fromPlutusScript Initial.validatorScript
initialDatum =
mkTxOutDatum $ Initial.datum (toPlutusCurrencySymbol tokenPolicyId)
-- | Craft a commit transaction which includes the "committed" utxo as a datum.
commitTx ::
-- | Published Hydra scripts to reference.
ScriptRegistry ->
NetworkId ->
HeadId ->
Party ->
-- | A single UTxO to commit to the Head
-- We currently limit committing one UTxO to the head because of size limitations.
Maybe (TxIn, TxOut CtxUTxO) ->
-- | The initial output (sent to each party) which should contain the PT and is
-- locked by initial script
(TxIn, TxOut CtxUTxO, Hash PaymentKey) ->
Tx
commitTx scriptRegistry networkId headId party utxo (initialInput, out, vkh) =
unsafeBuildTransaction $
emptyTxBody
& addInputs [(initialInput, initialWitness)]
& addReferenceInputs [initialScriptRef]
& addVkInputs (maybeToList mCommittedInput)
& addExtraRequiredSigners [vkh]
& addOutputs [commitOutput]
where
initialWitness =
BuildTxWith $
ScriptWitness scriptWitnessCtx $
mkScriptReference initialScriptRef initialScript initialDatum initialRedeemer
initialScript =
fromPlutusScript @PlutusScriptV2 Initial.validatorScript
initialScriptRef =
fst (initialReference scriptRegistry)
initialDatum =
mkScriptDatum $ Initial.datum (headIdToCurrencySymbol headId)
initialRedeemer =
toScriptData . Initial.redeemer $
Initial.ViaCommit (toPlutusTxOutRef <$> mCommittedInput)
mCommittedInput =
fst <$> utxo
commitOutput =
TxOut commitAddress commitValue commitDatum ReferenceScriptNone
commitScript =
fromPlutusScript Commit.validatorScript
commitAddress =
mkScriptAddress @PlutusScriptV2 networkId commitScript
commitValue =
txOutValue out <> maybe mempty (txOutValue . snd) utxo
commitDatum =
mkTxOutDatum $ mkCommitDatum party Head.validatorHash utxo (headIdToCurrencySymbol headId)
mkCommitDatum :: Party -> Plutus.ValidatorHash -> Maybe (TxIn, TxOut CtxUTxO) -> CurrencySymbol -> Plutus.Datum
mkCommitDatum party headValidatorHash utxo headId =
Commit.datum (partyToChain party, headValidatorHash, serializedUTxO, headId)
where
serializedUTxO = case utxo of
Nothing ->
Nothing
Just (i, o) ->
Commit.serializeCommit (i, o)
-- | Create a transaction collecting all "committed" utxo and opening a Head,
-- i.e. driving the Head script state.
collectComTx ::
NetworkId ->
-- | Party who's authorizing this transaction
VerificationKey PaymentKey ->
-- | Everything needed to spend the Head state-machine output.
InitialThreadOutput ->
-- | Data needed to spend the commit output produced by each party.
-- Should contain the PT and is locked by @ν_commit@ script.
Map TxIn (TxOut CtxUTxO, ScriptData) ->
-- | Head id
HeadId ->
Tx
collectComTx networkId vk initialThreadOutput commits headId =
unsafeBuildTransaction $
emptyTxBody
& addInputs ((headInput, headWitness) : (mkCommit <$> Map.toList commits))
& addOutputs [headOutput]
& addExtraRequiredSigners [verificationKeyHash vk]
where
InitialThreadOutput
{ initialThreadUTxO =
(headInput, initialHeadOutput, ScriptDatumForTxIn -> headDatumBefore)
, initialParties
, initialContestationPeriod
} =
initialThreadOutput
headWitness =
BuildTxWith $ ScriptWitness scriptWitnessCtx $ mkScriptWitness headScript headDatumBefore headRedeemer
headScript =
fromPlutusScript @PlutusScriptV2 Head.validatorScript
headRedeemer =
toScriptData Head.CollectCom
headOutput =
TxOut
(mkScriptAddress @PlutusScriptV2 networkId headScript)
(txOutValue initialHeadOutput <> commitValue)
headDatumAfter
ReferenceScriptNone
headDatumAfter =
mkTxOutDatum
Head.Open
{ Head.parties = initialParties
, utxoHash
, contestationPeriod = initialContestationPeriod
, headId = headIdToCurrencySymbol headId
}
extractCommit d =
case fromData $ toPlutusData d of
Nothing -> error "SNAFU"
Just ((_, _, Just o, _) :: Commit.DatumType) -> Just o
_ -> Nothing
utxoHash =
Head.hashPreSerializedCommits $ mapMaybe (extractCommit . snd . snd) $ Map.toList commits
mkCommit (commitInput, (_commitOutput, commitDatum)) =
( commitInput
, mkCommitWitness commitDatum
)
mkCommitWitness (ScriptDatumForTxIn -> commitDatum) =
BuildTxWith $ ScriptWitness scriptWitnessCtx $ mkScriptWitness commitScript commitDatum commitRedeemer
commitValue =
mconcat $ txOutValue . fst <$> Map.elems commits
commitScript =
fromPlutusScript @PlutusScriptV2 Commit.validatorScript
commitRedeemer =
toScriptData $ Commit.redeemer Commit.ViaCollectCom
-- | Low-level data type of a snapshot to close the head with. This is different
-- to the 'ConfirmedSnasphot', which is provided to `CloseTx` as it also
-- contains relevant chain state like the 'openUtxoHash'.
data ClosingSnapshot
= CloseWithInitialSnapshot {openUtxoHash :: UTxOHash}
| CloseWithConfirmedSnapshot
{ snapshotNumber :: SnapshotNumber
, closeUtxoHash :: UTxOHash
, -- XXX: This is a bit of a wart and stems from the fact that our
-- SignableRepresentation of 'Snapshot' is in fact the snapshotNumber
-- and the closeUtxoHash as also included above
signatures :: MultiSignature (Snapshot Tx)
}
-- | Create a transaction closing a head with either the initial snapshot or
-- with a multi-signed confirmed snapshot.
closeTx ::
-- | Party who's authorizing this transaction
VerificationKey PaymentKey ->
-- | The snapshot to close with, can be either initial or confirmed one.
ClosingSnapshot ->
-- | Lower validity slot number, usually a current or quite recent slot number.
SlotNo ->
-- | Upper validity slot and UTC time to compute the contestation deadline time.
PointInTime ->
-- | Everything needed to spend the Head state-machine output.
OpenThreadOutput ->
-- | Head identifier
HeadId ->
Tx
closeTx vk closing startSlotNo (endSlotNo, utcTime) openThreadOutput headId =
unsafeBuildTransaction $
emptyTxBody
& addInputs [(headInput, headWitness)]
& addOutputs [headOutputAfter]
& addExtraRequiredSigners [verificationKeyHash vk]
& setValidityLowerBound startSlotNo
& setValidityUpperBound endSlotNo
where
OpenThreadOutput
{ openThreadUTxO = (headInput, headOutputBefore, ScriptDatumForTxIn -> headDatumBefore)
, openContestationPeriod
, openParties
} = openThreadOutput
headWitness =
BuildTxWith $ ScriptWitness scriptWitnessCtx $ mkScriptWitness headScript headDatumBefore headRedeemer
headScript =
fromPlutusScript @PlutusScriptV2 Head.validatorScript
headRedeemer =
toScriptData
Head.Close
{ signature
}
headOutputAfter =
modifyTxOutDatum (const headDatumAfter) headOutputBefore
headDatumAfter =
mkTxOutDatum
Head.Closed
{ snapshotNumber
, utxoHash = toBuiltin utxoHashBytes
, parties = openParties
, contestationDeadline
, headId = headIdToCurrencySymbol headId
}
snapshotNumber = toInteger $ case closing of
CloseWithInitialSnapshot{} -> 0
CloseWithConfirmedSnapshot{snapshotNumber = sn} -> sn
UTxOHash utxoHashBytes = case closing of
CloseWithInitialSnapshot{openUtxoHash} -> openUtxoHash
CloseWithConfirmedSnapshot{closeUtxoHash} -> closeUtxoHash
-- FIXME the redeemer should be a maybe as it is useless in the initial close case
signature = case closing of
CloseWithInitialSnapshot{} -> mempty
CloseWithConfirmedSnapshot{signatures = s} -> toPlutusSignatures s
contestationDeadline =
addContestationPeriod (posixFromUTCTime utcTime) openContestationPeriod
-- XXX: This function is VERY similar to the 'closeTx' function (only notable
-- difference being the redeemer, which is in itself also the same structure as
-- the close's one. We could potentially refactor this to avoid repetition or do
-- something more principled at the protocol level itself and "merge" close and
-- contest as one operation.
contestTx ::
-- | Party who's authorizing this transaction
VerificationKey PaymentKey ->
-- | Contested snapshot number (i.e. the one we contest to)
Snapshot Tx ->
-- | Multi-signature of the whole snapshot
MultiSignature (Snapshot Tx) ->
-- | Current slot and posix time to be used as the contestation time.
PointInTime ->
-- | Everything needed to spend the Head state-machine output.
ClosedThreadOutput ->
HeadId ->
Tx
contestTx vk Snapshot{number, utxo} sig (slotNo, _) ClosedThreadOutput{closedThreadUTxO = (headInput, headOutputBefore, ScriptDatumForTxIn -> headDatumBefore), closedParties, closedContestationDeadline} headId =
unsafeBuildTransaction $
emptyTxBody
& addInputs [(headInput, headWitness)]
& addOutputs [headOutputAfter]
& addExtraRequiredSigners [verificationKeyHash vk]
& setValidityUpperBound slotNo
where
headWitness =
BuildTxWith $ ScriptWitness scriptWitnessCtx $ mkScriptWitness headScript headDatumBefore headRedeemer
headScript =
fromPlutusScript @PlutusScriptV2 Head.validatorScript
headRedeemer =
toScriptData
Head.Contest
{ signature = toPlutusSignatures sig
}
headOutputAfter =
modifyTxOutDatum (const headDatumAfter) headOutputBefore
headDatumAfter =
mkTxOutDatum
Head.Closed
{ snapshotNumber = toInteger number
, utxoHash
, parties = closedParties
, contestationDeadline = closedContestationDeadline
, headId = headIdToCurrencySymbol headId
}
utxoHash = toBuiltin $ hashUTxO @Tx utxo
-- | Create the fanout transaction, which distributes the closed state
-- accordingly. The head validator allows fanout only > deadline, so we need
-- to set the lower bound to be deadline + 1 slot.
fanoutTx ::
-- | Snapshotted UTxO to fanout on layer 1
UTxO ->
-- | Everything needed to spend the Head state-machine output.
UTxOWithScript ->
-- | Contestation deadline as SlotNo, used to set lower tx validity bound.
SlotNo ->
-- | Minting Policy script, made from initial seed
PlutusScript ->
Tx
fanoutTx utxo (headInput, headOutput, ScriptDatumForTxIn -> headDatumBefore) deadlineSlotNo headTokenScript =
unsafeBuildTransaction $
emptyTxBody
& addInputs [(headInput, headWitness)]
& addOutputs orderedTxOutsToFanout
& burnTokens headTokenScript Burn headTokens
& setValidityLowerBound (deadlineSlotNo + 1)
where
headWitness =
BuildTxWith $ ScriptWitness scriptWitnessCtx $ mkScriptWitness headScript headDatumBefore headRedeemer
headScript =
fromPlutusScript @PlutusScriptV2 Head.validatorScript
headRedeemer =
toScriptData (Head.Fanout $ fromIntegral $ length utxo)
headTokens =
headTokensFromValue headTokenScript (txOutValue headOutput)
orderedTxOutsToFanout =
toTxContext <$> toList utxo
data AbortTxError = OverlappingInputs
deriving (Show)
-- | Create transaction which aborts a head by spending the Head output and all
-- other "initial" outputs.
abortTx ::
-- | Committed UTxOs to reimburse.
UTxO ->
-- | Published Hydra scripts to reference.
ScriptRegistry ->
-- | Party who's authorizing this transaction
VerificationKey PaymentKey ->
-- | Everything needed to spend the Head state-machine output.
(TxIn, TxOut CtxUTxO, ScriptData) ->
-- | Script for monetary policy to burn tokens
PlutusScript ->
-- | Data needed to spend the initial output sent to each party to the Head.
-- Should contain the PT and is locked by initial script.
Map TxIn (TxOut CtxUTxO, ScriptData) ->
-- | Data needed to spend commit outputs.
-- Should contain the PT and is locked by commit script.
Map TxIn (TxOut CtxUTxO, ScriptData) ->
Either AbortTxError Tx
abortTx committedUTxO scriptRegistry vk (headInput, initialHeadOutput, ScriptDatumForTxIn -> headDatumBefore) headTokenScript initialsToAbort commitsToAbort
| isJust (lookup headInput initialsToAbort) =
Left OverlappingInputs
| otherwise =
Right $
unsafeBuildTransaction $
emptyTxBody
& addInputs ((headInput, headWitness) : initialInputs <> commitInputs)
& addReferenceInputs [initialScriptRef, commitScriptRef]
& addOutputs reimbursedOutputs
& burnTokens headTokenScript Burn headTokens
& addExtraRequiredSigners [verificationKeyHash vk]
where
headWitness =
BuildTxWith $ ScriptWitness scriptWitnessCtx $ mkScriptWitness headScript headDatumBefore headRedeemer
headScript =
fromPlutusScript @PlutusScriptV2 Head.validatorScript
headRedeemer =
toScriptData Head.Abort
initialInputs = mkAbortInitial <$> Map.toList initialsToAbort
commitInputs = mkAbortCommit <$> Map.toList commitsToAbort
headTokens =
headTokensFromValue headTokenScript $
mconcat
[ txOutValue initialHeadOutput
, foldMap (txOutValue . fst) initialsToAbort
, foldMap (txOutValue . fst) commitsToAbort
]
-- NOTE: Abort datums contain the datum of the spent state-machine input, but
-- also, the datum of the created output which is necessary for the
-- state-machine on-chain validator to control the correctness of the
-- transition.
mkAbortInitial (initialInput, (_, ScriptDatumForTxIn -> initialDatum)) =
(initialInput, mkAbortInitialWitness initialDatum)
mkAbortInitialWitness initialDatum =
BuildTxWith $
ScriptWitness scriptWitnessCtx $
mkScriptReference initialScriptRef initialScript initialDatum initialRedeemer
initialScriptRef =
fst (initialReference scriptRegistry)
initialScript =
fromPlutusScript @PlutusScriptV2 Initial.validatorScript
initialRedeemer =
toScriptData $ Initial.redeemer Initial.ViaAbort
mkAbortCommit (commitInput, (_, ScriptDatumForTxIn -> commitDatum)) =
(commitInput, mkAbortCommitWitness commitDatum)
mkAbortCommitWitness commitDatum =
BuildTxWith $
ScriptWitness scriptWitnessCtx $
mkScriptReference commitScriptRef commitScript commitDatum commitRedeemer
commitScriptRef =
fst (commitReference scriptRegistry)
commitScript =
fromPlutusScript @PlutusScriptV2 Commit.validatorScript
commitRedeemer =
toScriptData (Commit.redeemer Commit.ViaAbort)
reimbursedOutputs = toTxContext . snd <$> UTxO.pairs committedUTxO
-- * Observe Hydra Head transactions
data InitObservation = InitObservation
{ -- | The state machine UTxO produced by the Init transaction
-- This output should always be present and 'threaded' across all
-- transactions.
-- NOTE(SN): The Head's identifier is somewhat encoded in the TxOut's address
-- XXX(SN): Data and [OnChain.Party] are overlapping
threadOutput :: InitialThreadOutput
, initials :: [UTxOWithScript]
, commits :: [UTxOWithScript]
, headId :: HeadId
, headTokenScript :: PlutusScript
, contestationPeriod :: ContestationPeriod
, parties :: [Party]
}
deriving (Show, Eq)
-- XXX(SN): We should log decisions why a tx is not an initTx etc. instead of
-- only returning a Maybe, i.e. 'Either Reason (OnChainTx tx, OnChainHeadState)'
observeInitTx ::
NetworkId ->
[VerificationKey PaymentKey] ->
-- | Our node's contestation period
ContestationPeriod ->
Party ->
Tx ->
Maybe InitObservation
observeInitTx networkId cardanoKeys expectedCP party tx = do
-- FIXME: This is affected by "same structure datum attacks", we should be
-- using the Head script address instead.
(ix, headOut, headData, Head.Initial cp ps _headPolicyId) <- findFirst headOutput indexedOutputs
parties <- mapM partyFromChain ps
let contestationPeriod = fromChain cp
guard $ expectedCP == contestationPeriod
guard $ party `elem` parties
(headTokenPolicyId, headAssetName) <- findHeadAssetId headOut
let expectedNames = assetNameFromVerificationKey <$> cardanoKeys
let actualNames = assetNames headAssetName
guard $ sort expectedNames == sort actualNames
headTokenScript <- findScriptMinting tx headTokenPolicyId
pure
InitObservation
{ threadOutput =
InitialThreadOutput
{ initialThreadUTxO =
( mkTxIn tx ix
, toCtxUTxOTxOut headOut
, fromLedgerData headData
)
, initialParties = ps
, initialContestationPeriod = cp
}
, initials
, commits = []
, headId = mkHeadId headTokenPolicyId
, headTokenScript
, contestationPeriod
, parties
}
where
headOutput = \case
(ix, out@(TxOut _ _ (TxOutDatumInTx d) _)) ->
(ix,out,toLedgerData d,) <$> fromData (toPlutusData d)
_ -> Nothing
indexedOutputs = zip [0 ..] (txOuts' tx)
initialOutputs = filter (isInitial . snd) indexedOutputs
initials =
mapMaybe
( \(i, o) -> do
dat <- getScriptData o
pure (mkTxIn tx i, toCtxUTxOTxOut o, dat)
)
initialOutputs
isInitial (TxOut addr _ _ _) = addr == initialAddress
initialAddress = mkScriptAddress @PlutusScriptV2 networkId initialScript
initialScript = fromPlutusScript Initial.validatorScript
assetNames headAssetName =
[ assetName
| (AssetId _ assetName, _) <- txMintAssets tx
, assetName /= headAssetName
]
data CommitObservation = CommitObservation
{ commitOutput :: UTxOWithScript
, party :: Party
, committed :: UTxO
}
-- | Identify a commit tx by:
--
-- - Find which 'initial' tx input is being consumed,
-- - Find the redeemer corresponding to that 'initial', which contains the tx
-- input of the committed utxo,
-- - Find the outputs which pays to the commit validator,
-- - Using the datum of that output, deserialize the committed output,
-- - Reconstruct the committed UTxO from both values (tx input and output).
observeCommitTx ::
NetworkId ->
-- | Known (remaining) initial tx inputs.
[TxIn] ->
Tx ->
Maybe CommitObservation
observeCommitTx networkId initials tx = do
initialTxIn <- findInitialTxIn
mCommittedTxIn <- decodeInitialRedeemer initialTxIn
(commitIn, commitOut) <- findTxOutByAddress commitAddress tx
dat <- getScriptData commitOut
(onChainParty, _, onChainCommit, _headId) <- fromData @Commit.DatumType $ toPlutusData dat
party <- partyFromChain onChainParty
committed <-
-- TODO: We could simplify this by just using the datum. However, we would
-- need to ensure the commit is belonging to a head / is rightful. By just
-- looking for some known initials we achieve this (a bit complicated) now.
case (mCommittedTxIn, onChainCommit >>= Commit.deserializeCommit) of
(Nothing, Nothing) -> Just mempty
(Just i, Just (_i, o)) -> Just $ UTxO.singleton (i, o)
(Nothing, Just{}) -> error "found commit with no redeemer out ref but with serialized output."
(Just{}, Nothing) -> error "found commit with redeemer out ref but with no serialized output."
pure
CommitObservation
{ commitOutput = (commitIn, toUTxOContext commitOut, dat)
, party
, committed
}
where
findInitialTxIn =
case filter (`elem` initials) (txIns' tx) of
[input] -> Just input
_ -> Nothing
decodeInitialRedeemer =
findRedeemerSpending tx >=> \case
Initial.ViaAbort ->
Nothing
Initial.ViaCommit{committedRef} ->
Just (fromPlutusTxOutRef <$> committedRef)
commitAddress = mkScriptAddress @PlutusScriptV2 networkId commitScript
commitScript = fromPlutusScript Commit.validatorScript
data CollectComObservation = CollectComObservation
{ threadOutput :: OpenThreadOutput
, headId :: HeadId
, utxoHash :: UTxOHash
}
deriving (Show, Eq)
-- | Identify a collectCom tx by lookup up the input spending the Head output
-- and decoding its redeemer.
observeCollectComTx ::
-- | A UTxO set to lookup tx inputs
UTxO ->
Tx ->
Maybe CollectComObservation
observeCollectComTx utxo tx = do
(headInput, headOutput) <- findTxOutByScript @PlutusScriptV2 utxo headScript
redeemer <- findRedeemerSpending tx headInput
oldHeadDatum <- lookupScriptData tx headOutput
datum <- fromData $ toPlutusData oldHeadDatum
headId <- findStateToken headOutput
case (datum, redeemer) of
(Head.Initial{parties, contestationPeriod}, Head.CollectCom) -> do
(newHeadInput, newHeadOutput) <- findTxOutByScript @PlutusScriptV2 (utxoFromTx tx) headScript
newHeadDatum <- lookupScriptData tx newHeadOutput
utxoHash <- UTxOHash <$> decodeUtxoHash newHeadDatum
pure
CollectComObservation
{ threadOutput =
OpenThreadOutput
{ openThreadUTxO =
( newHeadInput
, newHeadOutput
, newHeadDatum
)
, openParties = parties
, openContestationPeriod = contestationPeriod
}
, headId
, utxoHash
}
_ -> Nothing
where
headScript = fromPlutusScript Head.validatorScript
decodeUtxoHash datum =
case fromData $ toPlutusData datum of
Just Head.Open{utxoHash} -> Just $ fromBuiltin utxoHash
_ -> Nothing
data CloseObservation = CloseObservation
{ threadOutput :: ClosedThreadOutput
, headId :: HeadId
, snapshotNumber :: SnapshotNumber
}
deriving (Show, Eq)
-- | Identify a close tx by lookup up the input spending the Head output and
-- decoding its redeemer.
observeCloseTx ::
-- | A UTxO set to lookup tx inputs
UTxO ->
Tx ->
Maybe CloseObservation
observeCloseTx utxo tx = do
(headInput, headOutput) <- findTxOutByScript @PlutusScriptV2 utxo headScript
redeemer <- findRedeemerSpending tx headInput
oldHeadDatum <- lookupScriptData tx headOutput
datum <- fromData $ toPlutusData oldHeadDatum
headId <- findStateToken headOutput
case (datum, redeemer) of
(Head.Open{parties}, Head.Close{}) -> do
(newHeadInput, newHeadOutput) <- findTxOutByScript @PlutusScriptV2 (utxoFromTx tx) headScript
newHeadDatum <- lookupScriptData tx newHeadOutput
(closeContestationDeadline, onChainSnapshotNumber) <- case fromData (toPlutusData newHeadDatum) of
Just Head.Closed{contestationDeadline, snapshotNumber} -> pure (contestationDeadline, snapshotNumber)
_ -> Nothing
pure
CloseObservation
{ threadOutput =
ClosedThreadOutput
{ closedThreadUTxO =
( newHeadInput
, newHeadOutput
, newHeadDatum
)
, closedParties = parties
, closedContestationDeadline = closeContestationDeadline
}
, headId
, snapshotNumber = fromChainSnapshot onChainSnapshotNumber
}
_ -> Nothing
where
headScript = fromPlutusScript Head.validatorScript
data ContestObservation = ContestObservation
{ contestedThreadOutput :: (TxIn, TxOut CtxUTxO, ScriptData)
, headId :: HeadId
, snapshotNumber :: SnapshotNumber
}
deriving (Show, Eq)
-- | Identify a close tx by lookup up the input spending the Head output and
-- decoding its redeemer.
observeContestTx ::
-- | A UTxO set to lookup tx inputs
UTxO ->
Tx ->
Maybe ContestObservation
observeContestTx utxo tx = do
(headInput, headOutput) <- findTxOutByScript @PlutusScriptV2 utxo headScript
redeemer <- findRedeemerSpending tx headInput
oldHeadDatum <- lookupScriptData tx headOutput
datum <- fromData $ toPlutusData oldHeadDatum
headId <- findStateToken headOutput
case (datum, redeemer) of
(Head.Closed{}, Head.Contest{}) -> do
(newHeadInput, newHeadOutput) <- findTxOutByScript @PlutusScriptV2 (utxoFromTx tx) headScript
newHeadDatum <- lookupScriptData tx newHeadOutput
let onChainSnapshotNumber = closedSnapshotNumber newHeadDatum
pure
ContestObservation
{ contestedThreadOutput =
( newHeadInput
, newHeadOutput
, newHeadDatum
)
, headId
, snapshotNumber = fromChainSnapshot onChainSnapshotNumber
}
_ -> Nothing
where
headScript = fromPlutusScript Head.validatorScript
closedSnapshotNumber headDatum =
case fromData $ toPlutusData headDatum of
Just Head.Closed{snapshotNumber} -> snapshotNumber
_ -> error "wrong state in output datum"
data FanoutObservation = FanoutObservation
-- | Identify a fanout tx by lookup up the input spending the Head output and
-- decoding its redeemer.
observeFanoutTx ::
-- | A UTxO set to lookup tx inputs
UTxO ->
Tx ->
Maybe FanoutObservation
observeFanoutTx utxo tx = do
headInput <- fst <$> findTxOutByScript @PlutusScriptV2 utxo headScript
findRedeemerSpending tx headInput
>>= \case
Head.Fanout{} -> pure FanoutObservation
_ -> Nothing
where
headScript = fromPlutusScript Head.validatorScript
data AbortObservation = AbortObservation
-- | Identify an abort tx by looking up the input spending the Head output and
-- decoding its redeemer.
-- FIXME: Add headId to AbortObservation to allow "upper layers" to
-- determine we are seeing an abort of "our head"
observeAbortTx ::
-- | A UTxO set to lookup tx inputs
UTxO ->
Tx ->
Maybe AbortObservation
observeAbortTx utxo tx = do
headInput <- fst <$> findTxOutByScript @PlutusScriptV2 utxo headScript
findRedeemerSpending tx headInput >>= \case
Head.Abort -> pure AbortObservation
_ -> Nothing
where
headScript = fromPlutusScript Head.validatorScript
-- * Helpers
mkHeadId :: PolicyId -> HeadId
mkHeadId =
HeadId . serialiseToRawBytes
headIdToCurrencySymbol :: HeadId -> CurrencySymbol
headIdToCurrencySymbol (HeadId headId) = CurrencySymbol (toBuiltin headId)
headTokensFromValue :: PlutusScript -> Value -> [(AssetName, Quantity)]
headTokensFromValue headTokenScript v =
[ (assetName, q)
| (AssetId pid assetName, q) <- valueToList v
, pid == scriptPolicyId (PlutusScript headTokenScript)
]
assetNameFromVerificationKey :: VerificationKey PaymentKey -> AssetName
assetNameFromVerificationKey =
AssetName . serialiseToRawBytes . verificationKeyHash
-- | Find first occurrence including a transformation.
findFirst :: Foldable t => (a -> Maybe b) -> t a -> Maybe b
findFirst fn = getFirst . foldMap (First . fn)
findHeadAssetId :: TxOut ctx -> Maybe (PolicyId, AssetName)
findHeadAssetId txOut =
flip findFirst (valueToList $ txOutValue txOut) $ \case
(AssetId pid aname, q)
| aname == hydraHeadV1AssetName && q == 1 ->
Just (pid, aname)
_ ->
Nothing
-- | Find (if it exists) the head identifier contained in given `TxOut`.
findStateToken :: TxOut ctx -> Maybe HeadId
findStateToken =
fmap (mkHeadId . fst) . findHeadAssetId