Skip to content

Commit 44147b7

Browse files
committed
Add queryTimeHandle to BackendOps and start withDirectChain port
Signed-off-by: Sasha Bogicevic <[email protected]>
1 parent 540aa74 commit 44147b7

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

hydra-node/src/Hydra/Chain/Blockfrost/TimeHandle.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import Hydra.Cardano.Api (
1111
SystemStart (..),
1212
)
1313
import Hydra.Cardano.Api.Prelude (ChainPoint (ChainPoint, ChainPointAtGenesis))
14-
import Hydra.Chain.Blockfrost.Client (queryEraHistory, queryGenesisParameters, queryTip, runBlockfrostM)
14+
import Hydra.Chain.Blockfrost.Client (queryEraHistory, queryGenesisParameters, queryTip)
1515
import Hydra.Chain.Direct.TimeHandle (TimeHandle, mkTimeHandle)
1616

1717
-- | Query node for system start and era history before constructing a
1818
-- 'TimeHandle' using the slot at the tip of the network.
19-
queryTimeHandle :: Blockfrost.Project -> IO TimeHandle
20-
queryTimeHandle prj = runBlockfrostM prj $ do
19+
queryTimeHandle :: Blockfrost.BlockfrostClientT IO TimeHandle
20+
queryTimeHandle = do
2121
tip <- queryTip
2222

2323
Blockfrost.Genesis{_genesisSystemStart} <- queryGenesisParameters

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

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import Control.Concurrent.Class.MonadSTM (
2323
import Control.Exception (IOException)
2424
import Control.Monad.Trans.Except (runExcept)
2525
import Hydra.Cardano.Api (
26-
AnyCardanoEra (..),
2726
BlockInMode (..),
2827
CardanoEra (..),
2928
ChainPoint,
@@ -35,9 +34,6 @@ import Hydra.Cardano.Api (
3534
LocalChainSyncClient (..),
3635
LocalNodeClientProtocols (..),
3736
LocalNodeConnectInfo (..),
38-
NetworkId,
39-
QueryInShelleyBasedEra (..),
40-
SocketPath,
4137
Tx,
4238
TxInMode (..),
4339
TxValidationErrorInCardanoMode,
@@ -56,11 +52,7 @@ import Hydra.Chain (
5652
currentState,
5753
)
5854
import Hydra.Chain.CardanoClient (
59-
QueryException (..),
6055
QueryPoint (..),
61-
queryCurrentEraExpr,
62-
queryInShelleyBasedEraExpr,
63-
runQueryExpr,
6456
)
6557
import Hydra.Chain.Direct.Handlers (
6658
ChainSyncHandler,
@@ -75,7 +67,6 @@ import Hydra.Chain.Direct.State (
7567
ChainContext (..),
7668
ChainStateAt (..),
7769
)
78-
import Hydra.Chain.Direct.TimeHandle (queryTimeHandle)
7970
import Hydra.Chain.Direct.Wallet (
8071
TinyWallet (..),
8172
WalletInfoOnChain (..),
@@ -84,9 +75,8 @@ import Hydra.Chain.Direct.Wallet (
8475
import Hydra.Logging (Tracer, traceWith)
8576
import Hydra.Node (BackendOps (..))
8677
import Hydra.Node.Util (readKeyPair)
87-
import Hydra.Options (CardanoChainConfig (..), DirectChainConfig (..))
78+
import Hydra.Options (CardanoChainConfig (..), ChainBackend (..))
8879
import Hydra.Tx (Party)
89-
import Ouroboros.Consensus.Cardano.Block (EraMismatch (..))
9080
import Ouroboros.Consensus.HardFork.History qualified as Consensus
9181
import Ouroboros.Network.Magic (NetworkMagic (..))
9282
import Ouroboros.Network.Protocol.ChainSync.Client (
@@ -156,7 +146,7 @@ mkTinyWallet tracer config = do
156146

157147
withDirectChain ::
158148
Tracer IO DirectChainLog ->
159-
DirectChainConfig ->
149+
CardanoChainConfig ->
160150
ChainContext ->
161151
TinyWallet IO ->
162152
-- | Chain state loaded from persistence.
@@ -167,12 +157,12 @@ withDirectChain tracer config ctx wallet chainStateHistory callback action = do
167157
let persistedPoint = recordedAt (currentState chainStateHistory)
168158
queue <- newTQueueIO
169159
-- Select a chain point from which to start synchronizing
170-
chainPoint <- maybe (queryTip networkId nodeSocket) pure $ do
160+
chainPoint <- maybe (queryTip chainBackend) pure $ do
171161
(max <$> startChainFrom <*> persistedPoint)
172162
<|> persistedPoint
173163
<|> startChainFrom
174164

175-
let getTimeHandle = queryTimeHandle networkId nodeSocket
165+
let getTimeHandle = queryTimeHandle chainBackend
176166
localChainState <- newLocalChainState chainStateHistory
177167
let chainHandle =
178168
mkChain
@@ -187,18 +177,21 @@ withDirectChain tracer config ctx wallet chainStateHistory callback action = do
187177
res <-
188178
race
189179
( handle onIOException $
190-
connectToLocalNode
191-
connectInfo
192-
(clientProtocols chainPoint queue handler)
180+
case chainBackend of
181+
DirectBackend{networkId, nodeSocket} ->
182+
connectToLocalNode
183+
(connectInfo networkId nodeSocket)
184+
(clientProtocols chainPoint queue handler)
185+
BlockfrostBackend{} -> undefined
193186
)
194187
(action chainHandle)
195188
case res of
196189
Left () -> error "'connectTo' cannot terminate but did?"
197190
Right a -> pure a
198191
where
199-
DirectChainConfig{networkId, nodeSocket, startChainFrom} = config
192+
CardanoChainConfig{chainBackend, startChainFrom} = config
200193

201-
connectInfo =
194+
connectInfo networkId nodeSocket =
202195
LocalNodeConnectInfo
203196
{ -- REVIEW: This was 432000 before, but all usages in the
204197
-- cardano-node repository are using this value. This is only
@@ -229,14 +222,10 @@ withDirectChain tracer config ctx wallet chainStateHistory callback action = do
229222
throwIO $
230223
ConnectException
231224
{ ioException
232-
, nodeSocket
233-
, networkId
234225
}
235226

236-
data ConnectException = ConnectException
227+
newtype ConnectException = ConnectException
237228
{ ioException :: IOException
238-
, nodeSocket :: SocketPath
239-
, networkId :: NetworkId
240229
}
241230
deriving stock (Show)
242231

hydra-node/src/Hydra/Node.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ import Hydra.Chain (
5252
initHistory,
5353
)
5454
import Hydra.Chain.Blockfrost.Client qualified as Blockfrost
55+
import Hydra.Chain.Blockfrost.TimeHandle qualified as Blockfrost
5556
import Hydra.Chain.CardanoClient qualified as CardanoClient
5657
import Hydra.Chain.ChainState (ChainStateType, IsChainState)
58+
import Hydra.Chain.Direct.TimeHandle qualified as TimeHandle
5759
import Hydra.Chain.ScriptRegistry qualified as ScriptRegistry
5860
import Hydra.Events (EventId, EventSink (..), EventSource (..), StateEvent (..), getEventId, putEventsToSinks, stateChanged)
5961
import Hydra.HeadLogic (
@@ -464,8 +466,10 @@ class BackendOps a where
464466
queryEraHistory :: (MonadIO m, MonadThrow m) => a -> CardanoClient.QueryPoint -> m EraHistory
465467
querySystemStart :: (MonadIO m, MonadThrow m) => a -> CardanoClient.QueryPoint -> m SystemStart
466468
queryProtocolParameters :: (MonadIO m, MonadThrow m) => a -> CardanoClient.QueryPoint -> m (PParams LedgerEra)
469+
queryTimeHandle :: (MonadIO m, MonadThrow m) => a -> m TimeHandle.TimeHandle
467470

468471
-- TODO: Perhaps use Reader monad for fetching configuration?
472+
-- we could also use a carrier type and define this instance on this type instead
469473
instance BackendOps ChainBackend where
470474
queryGenesisParameters = \case
471475
DirectBackend{networkId, nodeSocket} ->
@@ -526,3 +530,9 @@ instance BackendOps ChainBackend where
526530
BlockfrostBackend{projectPath} -> do
527531
prj <- liftIO $ Blockfrost.projectFromFile projectPath
528532
Blockfrost.runBlockfrostM prj Blockfrost.queryProtocolParameters
533+
queryTimeHandle = \case
534+
DirectBackend{networkId, nodeSocket} ->
535+
liftIO $ TimeHandle.queryTimeHandle networkId nodeSocket
536+
BlockfrostBackend{projectPath} -> do
537+
prj <- liftIO $ Blockfrost.projectFromFile projectPath
538+
Blockfrost.runBlockfrostM prj Blockfrost.queryTimeHandle

hydra-node/src/Hydra/Node/Run.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import Hydra.Node (
4040
import Hydra.Node.Network (NetworkConfiguration (..), withNetwork)
4141
import Hydra.Options (
4242
CardanoChainConfig (..),
43-
ChainBackend (..),
4443
ChainConfig (..),
4544
DirectChainConfig (..),
4645
InvalidOptions (..),

0 commit comments

Comments
 (0)