Skip to content

Commit e543075

Browse files
committed
Draft JSON instances for HeadObservation
1 parent 710b117 commit e543075

File tree

12 files changed

+83
-16
lines changed

12 files changed

+83
-16
lines changed

hydra-tx/src/Hydra/Tx/Abort.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ abortTx committedUTxO scriptRegistry vk (headInput, initialHeadOutput) headToken
106106

107107
newtype AbortObservation = AbortObservation {headId :: HeadId}
108108
deriving stock (Eq, Show, Generic)
109+
deriving anyclass (ToJSON, FromJSON)
110+
111+
instance Arbitrary AbortObservation where
112+
arbitrary = genericArbitrary
113+
shrink = genericShrink
109114

110115
-- | Identify an abort tx by looking up the input spending the Head output and
111116
-- decoding its redeemer.

hydra-tx/src/Hydra/Tx/Close.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ data CloseObservation = CloseObservation
156156
, contestationDeadline :: UTCTime
157157
}
158158
deriving stock (Show, Eq, Generic)
159+
deriving anyclass (ToJSON, FromJSON)
160+
161+
instance Arbitrary CloseObservation where
162+
arbitrary = genericArbitrary
163+
shrink = genericShrink
159164

160165
-- | Identify a close tx by lookup up the input spending the Head output and
161166
-- decoding its redeemer.

hydra-tx/src/Hydra/Tx/CollectCom.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
{-# LANGUAGE DerivingVia #-}
23
{-# LANGUAGE DuplicateRecordFields #-}
34

45
module Hydra.Tx.CollectCom where
@@ -99,6 +100,15 @@ collectComTx networkId scriptRegistry vk headId headParameters (headInput, initi
99100

100101
newtype UTxOHash = UTxOHash ByteString
101102
deriving stock (Eq, Show, Generic)
103+
deriving (ToJSON, FromJSON) via (UsingRawBytesHex UTxOHash)
104+
105+
instance HasTypeProxy UTxOHash where
106+
data AsType UTxOHash = AsUTxOHash
107+
proxyToAsType _ = AsUTxOHash
108+
109+
instance SerialiseAsRawBytes UTxOHash where
110+
serialiseToRawBytes (UTxOHash bytes) = bytes
111+
deserialiseFromRawBytes _ = Right . UTxOHash
102112

103113
instance Arbitrary UTxOHash where
104114
arbitrary = UTxOHash . BS.pack <$> vectorOf 32 arbitrary
@@ -108,6 +118,11 @@ data CollectComObservation = CollectComObservation
108118
, utxoHash :: UTxOHash
109119
}
110120
deriving stock (Show, Eq, Generic)
121+
deriving anyclass (ToJSON, FromJSON)
122+
123+
instance Arbitrary CollectComObservation where
124+
arbitrary = genericArbitrary
125+
shrink = genericShrink
111126

112127
-- | Identify a collectCom tx by lookup up the input spending the Head output
113128
-- and decoding its redeemer.

hydra-tx/src/Hydra/Tx/Commit.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ data CommitObservation = CommitObservation
155155
, headId :: HeadId
156156
}
157157
deriving stock (Eq, Show, Generic)
158+
deriving anyclass (ToJSON, FromJSON)
159+
160+
instance Arbitrary CommitObservation where
161+
arbitrary = undefined -- TODO: Arbitrary UTxO in hydra-tx
162+
shrink = undefined -- TODO: Arbitrary UTxO in hydra-tx
158163

159164
-- | Identify a commit tx by:
160165
--

hydra-tx/src/Hydra/Tx/Contest.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ data ContestObservation = ContestObservation
155155
}
156156
deriving stock (Show, Eq, Generic)
157157

158+
instance ToJSON ContestObservation where
159+
toJSON = undefined -- TODO: ToJSON PubKeyHash?
160+
161+
instance FromJSON ContestObservation where
162+
parseJSON = undefined -- TODO: FromJSON PubKeyHash?
163+
164+
instance Arbitrary ContestObservation where
165+
arbitrary = undefined -- TODO: Arbitrary PubKeyHash
166+
shrink = undefined -- TODO: Arbitrary PubKeyHash
167+
158168
-- | Identify a close tx by lookup up the input spending the Head output and
159169
-- decoding its redeemer.
160170
observeContestTx ::

hydra-tx/src/Hydra/Tx/Decrement.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ data DecrementObservation = DecrementObservation
9999
, distributedUTxO :: UTxO
100100
}
101101
deriving stock (Show, Eq, Generic)
102+
deriving anyclass (ToJSON, FromJSON)
103+
104+
instance Arbitrary DecrementObservation where
105+
arbitrary = undefined -- TODO: Arbitrary UTxO in hydra-tx
106+
shrink = undefined -- TODO: Arbitrary UTxO in hydra-tx
102107

103108
observeDecrementTx ::
104109
UTxO ->

hydra-tx/src/Hydra/Tx/Deposit.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ data DepositObservation = DepositObservation
8383
}
8484
deriving stock (Show, Eq, Generic)
8585

86+
instance ToJSON DepositObservation where
87+
toJSON = undefined -- TODO: ToJSON POSIXTime?
88+
89+
instance FromJSON DepositObservation where
90+
parseJSON = undefined -- TODO: FromJSON POSIXTime?
91+
92+
instance Arbitrary DepositObservation where
93+
arbitrary = undefined -- TODO: Arbitrary TxId and UTxO
94+
shrink = undefined -- TODO: Arbitrary TxId and UTxO
95+
8696
-- | Observe a deposit transaction by decoding the target head id, deposit
8797
-- deadline and deposited utxo in the datum.
8898
--

hydra-tx/src/Hydra/Tx/Fanout.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import Cardano.Api.UTxO qualified as UTxO
77
import Hydra.Contract.Head qualified as Head
88
import Hydra.Contract.HeadState qualified as Head
99
import Hydra.Contract.MintAction (MintAction (..))
10-
import Hydra.Ledger.Cardano.Builder (
11-
burnTokens,
12-
unsafeBuildTransaction,
13-
)
10+
import Hydra.Ledger.Cardano.Builder (burnTokens, unsafeBuildTransaction)
1411
import Hydra.Tx.HeadId (HeadId)
1512
import Hydra.Tx.ScriptRegistry (ScriptRegistry (..))
1613
import Hydra.Tx.Utils (findStateToken, headTokensFromValue, mkHydraHeadV1TxName)
@@ -78,8 +75,16 @@ fanoutTx scriptRegistry utxo utxoToCommit utxoToDecommit (headInput, headOutput)
7875

7976
-- * Observation
8077

81-
data FanoutObservation = FanoutObservation {headId :: HeadId, fanoutUTxO :: UTxO}
78+
data FanoutObservation = FanoutObservation
79+
{ headId :: HeadId
80+
, fanoutUTxO :: UTxO
81+
}
8282
deriving stock (Eq, Show, Generic)
83+
deriving anyclass (ToJSON, FromJSON)
84+
85+
instance Arbitrary FanoutObservation where
86+
arbitrary = undefined -- TODO: Arbitrary UTxO in hydra
87+
shrink = undefined -- TODO: Arbitrary UTxO in hydra-tx
8388

8489
-- | Identify a fanout tx by lookup up the input spending the Head output and
8590
-- decoding its redeemer.

hydra-tx/src/Hydra/Tx/Increment.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ data IncrementObservation = IncrementObservation
113113
, depositTxId :: TxId
114114
}
115115
deriving stock (Show, Eq, Generic)
116+
deriving anyclass (ToJSON, FromJSON)
117+
118+
instance Arbitrary IncrementObservation where
119+
arbitrary = undefined -- TODO: Arbitrary TxId
120+
shrink = undefined -- TODO: Arbitrary TxId
116121

117122
observeIncrementTx ::
118123
UTxO ->

hydra-tx/src/Hydra/Tx/Init.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ data InitObservation = InitObservation
9393
participants :: [OnChainId]
9494
}
9595
deriving stock (Show, Eq, Generic)
96+
deriving anyclass (ToJSON, FromJSON)
97+
98+
instance Arbitrary InitObservation where
99+
arbitrary = genericArbitrary
100+
shrink = genericShrink
96101

97102
data NotAnInitReason
98103
= NoHeadOutput

0 commit comments

Comments
 (0)