Skip to content

Commit 8582200

Browse files
authored
Add address query parameter to ws server (#1739)
<!-- Describe your change here --> To filter transaction server outputs by given address. Fixes #1719 --- <!-- Consider each and tick it off one way or the other --> * [x] CHANGELOG updated or not needed * [x] Documentation updated or not needed * [x] Haddocks updated or not needed * [x] No new TODOs introduced or explained herafter
2 parents 25f9f72 + 0e6b259 commit 8582200

File tree

20 files changed

+542
-46
lines changed

20 files changed

+542
-46
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ changes.
4848

4949
- Update mithril to `2442.0`
5050

51-
51+
- New websocket URL parameter `?address=...` to filter `SnapshotConfirmed`, `TxValid` and `TxInvalid` server outputs by address.
5252

5353
## [0.19.0] - 2024-09-13
5454

docs/docs/api-behavior.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ There are some options for API clients to control the server outputs. Server out
2020

2121
+ `history=no` -> Prevents historical outputs display. All server outputs are recorded and when a client re-connects these outputs are replayed unless `history=no` query param is used.
2222
+ `snapshot-utxo=no` -> In case of a `SnapshotConfirmed` message the `utxo` field in the inner `Snapshot` will be omitted.
23+
+ `address=$address` -> In the case of a `TxValid` or a `TxInvalid` message, it will be filtered if its `transaction` address does not contain a reference to the provided. In the case of a `SnapshotConfirmed` message, it will be filtered if its `confirmed` transactions do not contain an address that references the one provided.
2324

2425
## Replay of past server outputs
2526

hydra-cluster/hydra-cluster.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ test-suite tests
151151
Test.GeneratorSpec
152152
Test.Hydra.Cluster.CardanoCliSpec
153153
Test.Hydra.Cluster.FaucetSpec
154+
Test.Hydra.Cluster.HydraClientSpec
154155
Test.Hydra.Cluster.MithrilSpec
155156
Test.Hydra.Cluster.Utils
156157
Test.OfflineChainSpec

hydra-cluster/src/HydraNode.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ output tag pairs = object $ ("tag" .= tag) : pairs
8888
waitFor :: HasCallStack => Tracer IO HydraNodeLog -> NominalDiffTime -> [HydraClient] -> Aeson.Value -> IO ()
8989
waitFor tracer delay nodes v = waitForAll tracer delay nodes [v]
9090

91+
-- | Wait up to some time and succeed if no API server output matches the given predicate.
92+
waitNoMatch :: HasCallStack => NominalDiffTime -> HydraClient -> (Aeson.Value -> Maybe a) -> IO ()
93+
waitNoMatch delay client match = do
94+
result <- try (void $ waitMatch delay client match) :: IO (Either SomeException ())
95+
case result of
96+
Left _ -> pure () -- Success: waitMatch failed to find a match
97+
Right _ -> failure "waitNoMatch: A match was found when none was expected"
98+
9199
-- | Wait up to some time for an API server output to match the given predicate.
92100
waitMatch :: HasCallStack => NominalDiffTime -> HydraClient -> (Aeson.Value -> Maybe a) -> IO a
93101
waitMatch delay client@HydraClient{tracer, hydraNodeId} match = do
@@ -406,7 +414,7 @@ withConnectionToNode tracer hydraNodeId =
406414
port = fromInteger $ 4_000 + toInteger hydraNodeId
407415

408416
withConnectionToNodeHost :: forall a. Tracer IO HydraNodeLog -> Int -> Host -> Maybe String -> (HydraClient -> IO a) -> IO a
409-
withConnectionToNodeHost tracer hydraNodeId apiHost@Host{hostname, port} queryParams action = do
417+
withConnectionToNodeHost tracer hydraNodeId apiHost@Host{hostname, port} mQueryParams action = do
410418
connectedOnce <- newIORef False
411419
tryConnect connectedOnce (200 :: Int)
412420
where
@@ -424,9 +432,9 @@ withConnectionToNodeHost tracer hydraNodeId apiHost@Host{hostname, port} queryPa
424432
, Handler $ retryOrThrow (Proxy @HandshakeException)
425433
]
426434

427-
historyMode = fromMaybe "/" queryParams
435+
queryParams = fromMaybe "/" mQueryParams
428436

429-
doConnect connectedOnce = runClient (T.unpack hostname) (fromInteger . toInteger $ port) historyMode $
437+
doConnect connectedOnce = runClient (T.unpack hostname) (fromInteger . toInteger $ port) queryParams $
430438
\connection -> do
431439
atomicWriteIORef connectedOnce True
432440
traceWith tracer (NodeStarted hydraNodeId)

hydra-cluster/test/Test/EndToEndSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ timedTx tmpDir tracer node@RunningNode{networkId, nodeSocket} hydraScriptsTxId =
638638
-- Second submission: now valid
639639
send n1 $ input "NewTx" ["transaction" .= tx]
640640
waitFor hydraTracer 3 [n1] $
641-
output "TxValid" ["transactionId" .= txId tx, "headId" .= headId]
641+
output "TxValid" ["transactionId" .= txId tx, "headId" .= headId, "transaction" .= tx]
642642

643643
confirmedTransactions <- waitMatch 3 n1 $ \v -> do
644644
guard $ v ^? key "tag" == Just "SnapshotConfirmed"
@@ -693,7 +693,7 @@ initAndClose tmpDir tracer clusterIx hydraScriptsTxId node@RunningNode{nodeSocke
693693
aliceExternalSk
694694
send n1 $ input "NewTx" ["transaction" .= tx]
695695
waitFor hydraTracer 10 [n1, n2, n3] $
696-
output "TxValid" ["transactionId" .= txId tx, "headId" .= headId]
696+
output "TxValid" ["transactionId" .= txId tx, "headId" .= headId, "transaction" .= tx]
697697

698698
-- The expected new utxo set is the created payment to bob,
699699
-- alice's remaining utxo in head and whatever bot has

0 commit comments

Comments
 (0)