Skip to content

Commit 3710199

Browse files
authored
Merge branch 'master' into enhance-start-up-logs
2 parents 1560f9c + 76715b8 commit 3710199

File tree

6 files changed

+113
-142
lines changed

6 files changed

+113
-142
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ ouroborusClient tracer nodeSocket networkId =
5454
{ follow = \startChainFrom observerHandler -> do
5555
traceWith tracer ConnectingToNode{nodeSocket, networkId}
5656
chainPoint <- case startChainFrom of
57-
Nothing -> queryTip networkId nodeSocket
57+
Nothing -> queryTip (connectInfo nodeSocket networkId)
5858
Just x -> pure x
5959
traceWith tracer StartObservingFrom{chainPoint}
6060
connectToLocalNode

hydra-cluster/src/Hydra/Generator.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Hydra.Prelude hiding (size)
55
import Test.Hydra.Prelude
66

77
import Cardano.Api.UTxO qualified as UTxO
8-
import CardanoClient (QueryPoint (QueryTip), mkGenesisTx, queryUTxOFor)
8+
import CardanoClient (QueryPoint (QueryTip), localNodeConnectInfo, mkGenesisTx, queryUTxOFor)
99
import Control.Monad (foldM)
1010
import Data.Aeson (object, withObject, (.:), (.=))
1111
import Hydra.Chain.Backend (buildTransaction)
@@ -177,7 +177,7 @@ generateDemoUTxODataset ::
177177
IO Dataset
178178
generateDemoUTxODataset network nodeSocket faucetSk nClients nTxs = do
179179
-- Query available funds
180-
faucetUTxO <- queryUTxOFor network nodeSocket QueryTip faucetVk
180+
faucetUTxO <- queryUTxOFor (localNodeConnectInfo network nodeSocket) QueryTip faucetVk
181181
let (Coin fundsAvailable) = UTxO.totalLovelace faucetUTxO
182182
-- Generate client datasets
183183
allPaymentKeys <- generate $ replicateM nClients genSigningKey

hydra-node/src/Hydra/Chain/CardanoClient.hs

Lines changed: 95 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ module Hydra.Chain.CardanoClient where
66

77
import Hydra.Prelude
88

9-
import Hydra.Cardano.Api hiding (Block, queryCurrentEra)
9+
import Hydra.Cardano.Api hiding (Block, UTxO, queryCurrentEra)
1010

11+
import Cardano.Api.Query qualified as Query
12+
import Cardano.Api.UTxO (UTxO)
1113
import Cardano.Api.UTxO qualified as UTxO
1214
import Data.Aeson (eitherDecode', encode)
1315
import Data.Set qualified as Set
@@ -26,7 +28,6 @@ data QueryException
2628
| QueryProtocolParamsEncodingFailureOnEra AnyCardanoEra Text
2729
| QueryEraNotInCardanoModeFailure AnyCardanoEra
2830
| QueryNotShelleyBasedEraException AnyCardanoEra
29-
| QueryNotConwayEraOnwardsException AnyCardanoEra
3031
deriving stock (Show, Eq)
3132

3233
instance Exception QueryException where
@@ -45,14 +46,12 @@ instance Exception QueryException where
4546
printf "Error while querying using era %s not in cardano mode." (show eraName :: Text)
4647
QueryNotShelleyBasedEraException eraName ->
4748
printf "Error while querying using era %s not in shelley based era." (show eraName :: Text)
48-
QueryNotConwayEraOnwardsException eraName ->
49-
printf "Error while querying using era %s not in conway based era." (show eraName :: Text)
5049

5150
-- * CardanoClient handle
5251

5352
-- | Handle interface for abstract querying of a cardano node.
5453
data CardanoClient = CardanoClient
55-
{ queryUTxOByAddress :: [Address ShelleyAddr] -> IO UTxO
54+
{ queryUTxOByAddress :: [Address ShelleyAddr] -> IO (UTxO ConwayEra)
5655
, networkId :: NetworkId
5756
}
5857

@@ -62,15 +61,12 @@ data CardanoClient = CardanoClient
6261
--
6362
-- Throws 'SubmitTransactionException' if submission fails.
6463
submitTransaction ::
65-
-- | Current network discriminant
66-
NetworkId ->
67-
-- | Filepath to the cardano-node's domain socket
68-
SocketPath ->
64+
LocalNodeConnectInfo ->
6965
-- | A signed transaction.
7066
Tx ->
7167
IO ()
72-
submitTransaction networkId socket tx =
73-
submitTxToNodeLocal (localNodeConnectInfo networkId socket) txInMode >>= \case
68+
submitTransaction connectInfo tx =
69+
submitTxToNodeLocal connectInfo txInMode >>= \case
7470
SubmitSuccess ->
7571
pure ()
7672
SubmitFail (TxValidationEraMismatch e) ->
@@ -101,19 +97,17 @@ instance Exception SubmitTransactionException
10197
-- Note that this function loops forever; hence, one probably wants to couple it
10298
-- with a surrounding timeout.
10399
awaitTransaction ::
104-
-- | Current network discriminant
105-
NetworkId ->
106-
-- | Filepath to the cardano-node's domain socket
107-
SocketPath ->
100+
IsShelleyBasedEra era =>
101+
LocalNodeConnectInfo ->
108102
-- | The transaction to watch / await
109103
Tx ->
110-
IO UTxO
111-
awaitTransaction networkId socket tx =
104+
IO (UTxO era)
105+
awaitTransaction connectInfo tx =
112106
go
113107
where
114108
ins = keys (UTxO.toMap $ utxoFromTx tx)
115109
go = do
116-
utxo <- queryUTxOByTxIn networkId socket QueryTip ins
110+
utxo <- queryUTxOByTxIn connectInfo QueryTip ins
117111
if UTxO.null utxo
118112
then go
119113
else pure utxo
@@ -125,51 +119,59 @@ data QueryPoint = QueryTip | QueryAt ChainPoint
125119
deriving stock (Eq, Show, Generic)
126120

127121
-- | Query the latest chain point aka "the tip".
128-
queryTip :: NetworkId -> SocketPath -> IO ChainPoint
129-
queryTip networkId socket =
130-
chainTipToChainPoint <$> getLocalChainTip (localNodeConnectInfo networkId socket)
122+
queryTip :: LocalNodeConnectInfo -> IO ChainPoint
123+
queryTip connectInfo =
124+
chainTipToChainPoint <$> getLocalChainTip connectInfo
131125

132126
-- | Query the system start parameter at given point.
133127
--
134128
-- Throws at least 'QueryException' if query fails.
135-
querySystemStart :: NetworkId -> SocketPath -> QueryPoint -> IO SystemStart
136-
querySystemStart networkId socket queryPoint =
137-
runQuery networkId socket queryPoint QuerySystemStart
129+
querySystemStart :: LocalNodeConnectInfo -> QueryPoint -> IO SystemStart
130+
querySystemStart connectInfo queryPoint =
131+
runQueryExpr connectInfo queryPoint $
132+
Query.querySystemStart >>= liftIO . throwOnUnsupportedNtcVersion
138133

139134
-- | Query the era history at given point.
140135
--
141136
-- Throws at least 'QueryException' if query fails.
142-
queryEraHistory :: NetworkId -> SocketPath -> QueryPoint -> IO EraHistory
143-
queryEraHistory networkId socket queryPoint =
144-
runQuery networkId socket queryPoint QueryEraHistory
137+
queryEraHistory :: LocalNodeConnectInfo -> QueryPoint -> IO EraHistory
138+
queryEraHistory connectInfo queryPoint =
139+
runQueryExpr connectInfo queryPoint $
140+
Query.queryEraHistory >>= liftIO . throwOnUnsupportedNtcVersion
145141

146142
-- | Query the current epoch number.
147143
--
148144
-- Throws at least 'QueryException' if query fails.
149145
queryEpochNo ::
150-
NetworkId ->
151-
SocketPath ->
146+
LocalNodeConnectInfo ->
152147
QueryPoint ->
153148
IO EpochNo
154-
queryEpochNo networkId socket queryPoint = do
155-
runQueryExpr networkId socket queryPoint $ do
156-
queryForCurrentEraInShelleyBasedEraExpr (`queryInShelleyBasedEraExpr` QueryEpoch)
149+
queryEpochNo connectInfo queryPoint =
150+
runQueryExpr connectInfo queryPoint $ do
151+
queryForCurrentEraInShelleyBasedEraExpr $ \sbe ->
152+
Query.queryEpoch sbe
153+
>>= liftIO
154+
. throwOnUnsupportedNtcVersion
155+
>>= liftIO
156+
. throwOnEraMismatch
157157

158158
-- | Query the protocol parameters at given point and convert them to Babbage
159159
-- era protocol parameters.
160160
--
161161
-- Throws at least 'QueryException' if query fails.
162162
queryProtocolParameters ::
163-
-- | Current network discriminant
164-
NetworkId ->
165-
-- | Filepath to the cardano-node's domain socket
166-
SocketPath ->
163+
LocalNodeConnectInfo ->
167164
QueryPoint ->
168165
IO (PParams LedgerEra)
169-
queryProtocolParameters networkId socket queryPoint =
170-
runQueryExpr networkId socket queryPoint $ do
166+
queryProtocolParameters connectInfo queryPoint =
167+
runQueryExpr connectInfo queryPoint $ do
171168
queryForCurrentEraInShelleyBasedEraExpr $ \sbe -> do
172-
eraPParams <- queryInShelleyBasedEraExpr sbe QueryProtocolParameters
169+
eraPParams <-
170+
Query.queryProtocolParameters sbe
171+
>>= liftIO
172+
. throwOnUnsupportedNtcVersion
173+
>>= liftIO
174+
. throwOnEraMismatch
173175
liftIO $ coercePParamsToLedgerEra (convert sbe) eraPParams
174176
where
175177
encodeToEra :: ToJSON a => CardanoEra era -> a -> IO (PParams LedgerEra)
@@ -193,46 +195,46 @@ queryProtocolParameters networkId socket queryPoint =
193195
--
194196
-- Throws at least 'QueryException' if query fails.
195197
queryGenesisParameters ::
196-
-- | Current network discriminant
197-
NetworkId ->
198-
-- | Filepath to the cardano-node's domain socket
199-
SocketPath ->
198+
LocalNodeConnectInfo ->
200199
QueryPoint ->
201200
IO (GenesisParameters ShelleyEra)
202-
queryGenesisParameters networkId socket queryPoint =
203-
runQueryExpr networkId socket queryPoint $ do
204-
queryForCurrentEraInShelleyBasedEraExpr (`queryInShelleyBasedEraExpr` QueryGenesisParameters)
201+
queryGenesisParameters connectInfo queryPoint =
202+
runQueryExpr connectInfo queryPoint $ do
203+
queryForCurrentEraInShelleyBasedEraExpr $ \sbe ->
204+
Query.queryGenesisParameters sbe
205+
>>= liftIO
206+
. throwOnUnsupportedNtcVersion
207+
>>= liftIO
208+
. throwOnEraMismatch
205209

206210
-- | Query UTxO for all given addresses at given point.
207211
--
208212
-- Throws at least 'QueryException' if query fails.
209-
queryUTxO :: NetworkId -> SocketPath -> QueryPoint -> [Address ShelleyAddr] -> IO UTxO
210-
queryUTxO networkId socket queryPoint addresses =
211-
runQueryExpr networkId socket queryPoint $ do
212-
queryForCurrentEraInConwayEraOnwardsExpr
213-
(`queryUTxOExpr` addresses)
214-
215-
queryUTxOExpr :: ConwayEraOnwards era -> [Address ShelleyAddr] -> LocalStateQueryExpr b p QueryInMode r IO UTxO
216-
queryUTxOExpr ceo addresses = case ceo of
217-
ConwayEraOnwardsConway -> queryInShelleyBasedEraExpr (convert ceo) $ QueryUTxO (QueryUTxOByAddress (Set.fromList $ map AddressShelley addresses))
213+
queryUTxO :: IsShelleyBasedEra era => LocalNodeConnectInfo -> QueryPoint -> [Address ShelleyAddr] -> IO (UTxO era)
214+
queryUTxO connectInfo queryPoint addresses =
215+
runQueryExpr connectInfo queryPoint $
216+
Query.queryUtxo shelleyBasedEra (QueryUTxOByAddress (Set.fromList $ map AddressShelley addresses))
217+
>>= liftIO
218+
. throwOnUnsupportedNtcVersion
219+
>>= liftIO
220+
. throwOnEraMismatch
218221

219222
-- | Query UTxO for given tx inputs at given point.
220223
--
221224
-- Throws at least 'QueryException' if query fails.
222225
queryUTxOByTxIn ::
223-
-- | Current network discriminant
224-
NetworkId ->
225-
-- | Filepath to the cardano-node's domain socket
226-
SocketPath ->
226+
IsShelleyBasedEra era =>
227+
LocalNodeConnectInfo ->
227228
QueryPoint ->
228229
[TxIn] ->
229-
IO UTxO
230-
queryUTxOByTxIn networkId socket queryPoint inputs =
231-
runQueryExpr networkId socket queryPoint $
232-
queryForCurrentEraInConwayEraOnwardsExpr
233-
( \(ceo :: ConwayEraOnwards era) -> case ceo of
234-
ConwayEraOnwardsConway -> queryInShelleyBasedEraExpr (convert ceo) (QueryUTxO (QueryUTxOByTxIn (Set.fromList inputs)))
235-
)
230+
IO (UTxO era)
231+
queryUTxOByTxIn connectInfo queryPoint inputs =
232+
runQueryExpr connectInfo queryPoint $
233+
Query.queryUtxo shelleyBasedEra (QueryUTxOByTxIn (Set.fromList inputs))
234+
>>= liftIO
235+
. throwOnUnsupportedNtcVersion
236+
>>= liftIO
237+
. throwOnEraMismatch
236238

237239
queryForCurrentEraInEonExpr ::
238240
Eon eon =>
@@ -248,96 +250,65 @@ queryForCurrentEraInShelleyBasedEraExpr ::
248250
LocalStateQueryExpr b p QueryInMode r IO a
249251
queryForCurrentEraInShelleyBasedEraExpr = queryForCurrentEraInEonExpr (throwIO . QueryNotShelleyBasedEraException)
250252

251-
queryForCurrentEraInConwayEraOnwardsExpr ::
252-
Eon eon =>
253-
(forall era. eon era -> LocalStateQueryExpr b p QueryInMode r IO a) ->
254-
LocalStateQueryExpr b p QueryInMode r IO a
255-
queryForCurrentEraInConwayEraOnwardsExpr = queryForCurrentEraInEonExpr (throwIO . QueryNotConwayEraOnwardsException)
256-
257253
-- | Query the whole UTxO from node at given point. Useful for debugging, but
258254
-- should obviously not be used in production code.
259255
--
260256
-- Throws at least 'QueryException' if query fails.
261257
queryUTxOWhole ::
262-
-- | Current network discriminant
263-
NetworkId ->
264-
-- | Filepath to the cardano-node's domain socket
265-
SocketPath ->
258+
IsShelleyBasedEra era =>
259+
LocalNodeConnectInfo ->
266260
QueryPoint ->
267-
IO UTxO
268-
queryUTxOWhole networkId socket queryPoint = do
269-
runQueryExpr networkId socket queryPoint $ do
270-
queryForCurrentEraInConwayEraOnwardsExpr
271-
( \(ceo :: ConwayEraOnwards era) -> case ceo of
272-
ConwayEraOnwardsConway -> queryInShelleyBasedEraExpr (convert ceo) (QueryUTxO QueryUTxOWhole)
273-
)
261+
IO (UTxO era)
262+
queryUTxOWhole connectInfo queryPoint =
263+
runQueryExpr connectInfo queryPoint $
264+
Query.queryUtxo shelleyBasedEra QueryUTxOWhole
265+
>>= liftIO
266+
. throwOnUnsupportedNtcVersion
267+
>>= liftIO
268+
. throwOnEraMismatch
274269

275270
-- | Query UTxO for the address of given verification key at point.
276271
--
277272
-- Throws at least 'QueryException' if query fails.
278-
queryUTxOFor :: NetworkId -> SocketPath -> QueryPoint -> VerificationKey PaymentKey -> IO UTxO
279-
queryUTxOFor networkId nodeSocket queryPoint vk =
280-
case mkVkAddress networkId vk of
273+
queryUTxOFor :: IsShelleyBasedEra era => LocalNodeConnectInfo -> QueryPoint -> VerificationKey PaymentKey -> IO (UTxO era)
274+
queryUTxOFor connectInfo queryPoint vk =
275+
case mkVkAddress (localNodeNetworkId connectInfo) vk of
281276
ShelleyAddressInEra addr ->
282-
queryUTxO networkId nodeSocket queryPoint [addr]
277+
queryUTxO connectInfo queryPoint [addr]
283278
ByronAddressInEra{} ->
284279
fail "impossible: mkVkAddress returned Byron address."
285280

286281
-- | Query the current set of registered stake pools.
287282
--
288283
-- Throws at least 'QueryException' if query fails.
289284
queryStakePools ::
290-
-- | Current network discriminant
291-
NetworkId ->
292-
-- | Filepath to the cardano-node's domain socket
293-
SocketPath ->
285+
LocalNodeConnectInfo ->
294286
QueryPoint ->
295287
IO (Set PoolId)
296-
queryStakePools networkId socket queryPoint =
297-
runQueryExpr networkId socket queryPoint $ do
298-
queryForCurrentEraInShelleyBasedEraExpr (`queryInShelleyBasedEraExpr` QueryStakePools)
288+
queryStakePools connectInfo queryPoint =
289+
runQueryExpr connectInfo queryPoint $ do
290+
queryForCurrentEraInShelleyBasedEraExpr $ \sbe ->
291+
Query.queryStakePools sbe
292+
>>= liftIO
293+
. throwOnUnsupportedNtcVersion
294+
>>= liftIO
295+
. throwOnEraMismatch
299296

300297
-- * Helpers
301298

302299
-- | Monadic query expression to get current era.
303300
queryCurrentEraExpr :: LocalStateQueryExpr b p QueryInMode r IO AnyCardanoEra
304301
queryCurrentEraExpr =
305-
queryExpr QueryCurrentEra >>= liftIO . throwOnUnsupportedNtcVersion
306-
307-
-- | Monadic query expression for a 'QueryInShelleyBasedEra'.
308-
queryInShelleyBasedEraExpr ::
309-
-- | The current running era we can use to query the node
310-
ShelleyBasedEra era ->
311-
QueryInShelleyBasedEra era a ->
312-
LocalStateQueryExpr b p QueryInMode r IO a
313-
queryInShelleyBasedEraExpr sbe query =
314-
queryExpr (QueryInEra $ QueryInShelleyBasedEra sbe query)
315-
>>= liftIO
316-
. throwOnUnsupportedNtcVersion
317-
>>= liftIO
318-
. throwOnEraMismatch
319-
320-
-- | Throws at least 'QueryException' if query fails.
321-
runQuery :: NetworkId -> SocketPath -> QueryPoint -> QueryInMode a -> IO a
322-
runQuery networkId socket point query =
323-
runExceptT (queryNodeLocalState (localNodeConnectInfo networkId socket) queryTarget query) >>= \case
324-
Left err -> throwIO $ QueryAcquireException err
325-
Right result -> pure result
326-
where
327-
queryTarget =
328-
case point of
329-
QueryTip -> VolatileTip
330-
QueryAt cp -> SpecificPoint cp
302+
Query.queryCurrentEra >>= liftIO . throwOnUnsupportedNtcVersion
331303

332304
-- | Throws at least 'QueryException' if query fails.
333305
runQueryExpr ::
334-
NetworkId ->
335-
SocketPath ->
306+
LocalNodeConnectInfo ->
336307
QueryPoint ->
337308
LocalStateQueryExpr BlockInMode ChainPoint QueryInMode () IO a ->
338309
IO a
339-
runQueryExpr networkId socket point query =
340-
executeLocalStateQueryExpr (localNodeConnectInfo networkId socket) queryTarget query >>= \case
310+
runQueryExpr connectInfo point query =
311+
executeLocalStateQueryExpr connectInfo queryTarget query >>= \case
341312
Left err -> throwIO $ QueryAcquireException err
342313
Right result -> pure result
343314
where

0 commit comments

Comments
 (0)