Skip to content

Commit 21ac957

Browse files
committed
Switch ChainObservation to use HeadObservation
This will allow chain observers to use these valueu directly without further conversion to 'OnChainTx'. The latter will be the hydra-node specific type which contains already converted time fields. The change here is incomplete as HeadObservation is missing several instances and breaks the explorer API.
1 parent 1fd051c commit 21ac957

File tree

4 files changed

+43
-28
lines changed

4 files changed

+43
-28
lines changed

hydra-chain-observer/src/Hydra/Blockfrost/ChainObserver.hs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ import Hydra.Cardano.Api (
3131
import Hydra.Cardano.Api.Prelude (
3232
BlockHeader (..),
3333
)
34-
import Hydra.Chain.Direct.Handlers (convertObservation)
3534
import Hydra.ChainObserver.NodeClient (
3635
ChainObservation (..),
3736
ChainObserverLog (..),
3837
NodeClient (..),
3938
ObserverHandler,
40-
logOnChainTx,
39+
logObservation,
4140
observeAll,
4241
)
4342
import Hydra.Logging (Tracer, traceWith)
4443
import Hydra.Tx (IsTx (..))
44+
import Hydra.Tx.Observe (HeadObservation (..))
4545

4646
data APIBlockfrostError
4747
= BlockfrostError Text
@@ -169,16 +169,15 @@ rollForward tracer prj networkId observerHandler blockConfirmations (blockHash,
169169

170170
-- Collect head observations
171171
let (adjustedUTxO, observations) = observeAll networkId utxo receivedTxs
172-
let onChainTxs = mapMaybe convertObservation observations
173-
forM_ onChainTxs (traceWith tracer . logOnChainTx)
172+
mapM_ (traceWith tracer) $ mapMaybe logObservation observations
174173

175174
blockNo <- maybe (throwIO $ MissingBlockNo _blockHash) (pure . fromInteger) _blockHeight
176-
let observationsAt = ChainObservation point blockNo . Just <$> onChainTxs
175+
let observationsAt = ChainObservation point blockNo <$> observations
177176

178177
-- Call observer handler
179178
observerHandler $
180179
if null observationsAt
181-
then [ChainObservation point blockNo Nothing]
180+
then [ChainObservation point blockNo NoHeadTx]
182181
else observationsAt
183182

184183
-- Next

hydra-chain-observer/src/Hydra/ChainObserver/NodeClient.hs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@ import Hydra.Cardano.Api (
1313
UTxO,
1414
)
1515
import Hydra.Cardano.Api.Prelude (TxId)
16-
import Hydra.Chain (OnChainTx (..))
1716
import Hydra.Contract (ScriptInfo)
1817
import Hydra.Ledger.Cardano (adjustUTxO)
1918
import Hydra.Tx.HeadId (HeadId (..))
20-
import Hydra.Tx.Observe (HeadObservation (..), observeHeadTx)
19+
import Hydra.Tx.Observe (AbortObservation (..), CloseObservation (..), CollectComObservation (..), CommitObservation (..), ContestObservation (..), DecrementObservation (..), DepositObservation (..), FanoutObservation (..), HeadObservation (..), IncrementObservation (..), InitObservation (..), RecoverObservation (..), observeHeadTx)
2120

2221
type ObserverHandler m = [ChainObservation] -> m ()
2322

2423
data ChainObservation
2524
= ChainObservation
2625
{ point :: ChainPoint
2726
, blockNo :: BlockNo
28-
, observedTx :: Maybe (OnChainTx Tx)
27+
, -- FIXME: This breaks the explorer interface. Either move 'HeadObservation'
28+
-- into similar form, or create a new similar type. Cannot use 'OnChainTx'
29+
-- from hydra-node as that contains converted time and requires a
30+
-- 'TimeHandle' to create.
31+
observed :: HeadObservation
2932
}
3033
deriving stock (Eq, Show, Generic)
3134
deriving anyclass (ToJSON, FromJSON)
@@ -61,19 +64,20 @@ data ChainObserverLog
6164
deriving stock (Eq, Show, Generic)
6265
deriving anyclass (ToJSON)
6366

64-
logOnChainTx :: OnChainTx Tx -> ChainObserverLog
65-
logOnChainTx = \case
66-
OnInitTx{headId} -> HeadInitTx{headId}
67-
OnCommitTx{headId} -> HeadCommitTx{headId}
68-
OnCollectComTx{headId} -> HeadCollectComTx{headId}
69-
OnIncrementTx{headId} -> HeadIncrementTx{headId}
70-
OnDepositTx{headId} -> HeadDepositTx{headId}
71-
OnRecoverTx{headId} -> HeadRecoverTx{headId}
72-
OnDecrementTx{headId} -> HeadDecrementTx{headId}
73-
OnCloseTx{headId} -> HeadCloseTx{headId}
74-
OnFanoutTx{headId} -> HeadFanoutTx{headId}
75-
OnAbortTx{headId} -> HeadAbortTx{headId}
76-
OnContestTx{headId} -> HeadContestTx{headId}
67+
logObservation :: HeadObservation -> Maybe ChainObserverLog
68+
logObservation = \case
69+
NoHeadTx -> Nothing
70+
Init InitObservation{headId} -> Just HeadInitTx{headId}
71+
Commit CommitObservation{headId} -> Just HeadCommitTx{headId}
72+
Abort AbortObservation{headId} -> Just HeadAbortTx{headId}
73+
CollectCom CollectComObservation{headId} -> Just HeadCollectComTx{headId}
74+
Deposit DepositObservation{headId} -> Just HeadDepositTx{headId}
75+
Recover RecoverObservation{headId} -> Just HeadRecoverTx{headId}
76+
Increment IncrementObservation{headId} -> Just HeadIncrementTx{headId}
77+
Decrement DecrementObservation{headId} -> Just HeadDecrementTx{headId}
78+
Close CloseObservation{headId} -> Just HeadCloseTx{headId}
79+
Contest ContestObservation{headId} -> Just HeadContestTx{headId}
80+
Fanout FanoutObservation{headId} -> Just HeadFanoutTx{headId}
7781

7882
observeTx :: NetworkId -> UTxO -> Tx -> (UTxO, Maybe HeadObservation)
7983
observeTx networkId utxo tx =

hydra-chain-observer/src/Hydra/Ouroborus/ChainObserver.hs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ import Hydra.Cardano.Api (
2727
pattern Block,
2828
)
2929
import Hydra.Chain.CardanoClient (queryTip)
30-
import Hydra.Chain.Direct.Handlers (convertObservation)
3130
import Hydra.ChainObserver.NodeClient (
3231
ChainObservation (..),
3332
ChainObserverLog (..),
3433
NodeClient (..),
3534
ObserverHandler,
36-
logOnChainTx,
35+
logObservation,
3736
observeAll,
3837
)
3938
import Hydra.Logging (Tracer, traceWith)
39+
import Hydra.Tx.Observe (HeadObservation (..))
4040
import Ouroboros.Network.Protocol.ChainSync.Client (
4141
ChainSyncClient (..),
4242
ClientStIdle (..),
@@ -145,13 +145,12 @@ chainSyncClient tracer networkId startingPoint observerHandler =
145145
_ -> []
146146

147147
(utxo', observations) = observeAll networkId utxo txs
148-
onChainTxs = mapMaybe convertObservation observations
149148

150-
forM_ onChainTxs (traceWith tracer . logOnChainTx)
151-
let observationsAt = ChainObservation point blockNo . Just <$> onChainTxs
149+
mapM_ (traceWith tracer) $ mapMaybe logObservation observations
150+
let observationsAt = ChainObservation point blockNo <$> observations
152151
observerHandler $
153152
if null observationsAt
154-
then [ChainObservation point blockNo Nothing]
153+
then [ChainObservation point blockNo NoHeadTx]
155154
else observationsAt
156155

157156
pure $ clientStIdle utxo'

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ data HeadObservation
5252
| Fanout FanoutObservation
5353
deriving stock (Eq, Show, Generic)
5454

55+
instance ToJSON HeadObservation where
56+
-- TODO: implement explorer compatible JSON instance
57+
toJSON = undefined
58+
59+
instance FromJSON HeadObservation where
60+
-- TODO: implement explorer compatible JSON instance
61+
parseJSON = undefined
62+
63+
instance Arbitrary HeadObservation where
64+
-- TODO: implement arbitrary instance
65+
arbitrary = undefined
66+
shrink = undefined
67+
5568
-- | Observe any Hydra head transaction.
5669
observeHeadTx :: NetworkId -> UTxO -> Tx -> HeadObservation
5770
observeHeadTx networkId utxo tx =

0 commit comments

Comments
 (0)