{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs     #-}

module Cardano.Streaming.Callbacks where

import Control.Exception (throw)
import Data.Word (Word32)

import Cardano.Api qualified as C
import Cardano.Slotting.Slot (WithOrigin (At, Origin))
import Network.TypedProtocol.Pipelined (N (Z), Nat (Succ, Zero))
import Ouroboros.Network.Protocol.ChainSync.Client qualified as CS
import Ouroboros.Network.Protocol.ChainSync.ClientPipelined qualified as CSP
import Ouroboros.Network.Protocol.ChainSync.PipelineDecision (PipelineDecision (Collect), pipelineDecisionMax)

import Cardano.Streaming.Helpers qualified as H

-- * Raw chain-sync clients using callback

blocksCallbackPipelined
  :: Word32 -> C.LocalNodeConnectInfo C.CardanoMode -> C.ChainPoint
  -> (H.ChainSyncEvent (C.BlockInMode C.CardanoMode) -> IO ())
  -> IO ()
blocksCallbackPipelined :: Word32
-> LocalNodeConnectInfo CardanoMode
-> ChainPoint
-> (ChainSyncEvent (BlockInMode CardanoMode) -> IO ())
-> IO ()
blocksCallbackPipelined Word32
n LocalNodeConnectInfo CardanoMode
con ChainPoint
point ChainSyncEvent (BlockInMode CardanoMode) -> IO ()
callback =
  LocalNodeConnectInfo CardanoMode
-> LocalNodeClientProtocolsInMode CardanoMode -> IO ()
forall mode.
LocalNodeConnectInfo mode
-> LocalNodeClientProtocolsInMode mode -> IO ()
C.connectToLocalNode LocalNodeConnectInfo CardanoMode
con (LocalNodeClientProtocolsInMode CardanoMode -> IO ())
-> LocalNodeClientProtocolsInMode CardanoMode -> IO ()
forall a b. (a -> b) -> a -> b
$ LocalNodeClientProtocols :: forall block point tip slot tx txid txerr (query :: * -> *)
       (m :: * -> *).
LocalChainSyncClient block point tip m
-> Maybe (LocalTxSubmissionClient tx txerr m ())
-> Maybe (LocalStateQueryClient block point query m ())
-> Maybe (LocalTxMonitorClient txid tx slot m ())
-> LocalNodeClientProtocols
     block point tip slot tx txid txerr query m
C.LocalNodeClientProtocols
    { localChainSyncClient :: LocalChainSyncClient
  (BlockInMode CardanoMode) ChainPoint ChainTip IO
C.localChainSyncClient    = ChainSyncClientPipelined
  (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> LocalChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO
forall block point tip (m :: * -> *).
ChainSyncClientPipelined block point tip m ()
-> LocalChainSyncClient block point tip m
C.LocalChainSyncClientPipelined (ChainSyncClientPipelined
   (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
 -> LocalChainSyncClient
      (BlockInMode CardanoMode) ChainPoint ChainTip IO)
-> ChainSyncClientPipelined
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> LocalChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO
forall a b. (a -> b) -> a -> b
$ IO
  (ClientPipelinedStIdle
     'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> ChainSyncClientPipelined
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall header point tip (m :: * -> *) a.
m (ClientPipelinedStIdle 'Z header point tip m a)
-> ChainSyncClientPipelined header point tip m a
CSP.ChainSyncClientPipelined (IO
   (ClientPipelinedStIdle
      'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
 -> ChainSyncClientPipelined
      (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> IO
     (ClientPipelinedStIdle
        'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> ChainSyncClientPipelined
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall a b. (a -> b) -> a -> b
$ ClientPipelinedStIdle
  'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientPipelinedStIdle
        'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ClientPipelinedStIdle
   'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
 -> IO
      (ClientPipelinedStIdle
         'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ()))
-> ClientPipelinedStIdle
     'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientPipelinedStIdle
        'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall a b. (a -> b) -> a -> b
$ [ChainPoint]
-> ClientPipelinedStIntersect
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> ClientPipelinedStIdle
     'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall point header tip (m :: * -> *) a.
[point]
-> ClientPipelinedStIntersect header point tip m a
-> ClientPipelinedStIdle 'Z header point tip m a
CSP.SendMsgFindIntersect [ChainPoint
point] ClientPipelinedStIntersect
  (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
onIntersect
    , localTxSubmissionClient :: Maybe
  (LocalTxSubmissionClient
     (TxInMode CardanoMode) (TxValidationErrorInMode CardanoMode) IO ())
C.localTxSubmissionClient = Maybe
  (LocalTxSubmissionClient
     (TxInMode CardanoMode) (TxValidationErrorInMode CardanoMode) IO ())
forall a. Maybe a
Nothing
    , localStateQueryClient :: Maybe
  (LocalStateQueryClient
     (BlockInMode CardanoMode)
     ChainPoint
     (QueryInMode CardanoMode)
     IO
     ())
C.localStateQueryClient   = Maybe
  (LocalStateQueryClient
     (BlockInMode CardanoMode)
     ChainPoint
     (QueryInMode CardanoMode)
     IO
     ())
forall a. Maybe a
Nothing
    , localTxMonitoringClient :: Maybe
  (LocalTxMonitorClient
     (TxIdInMode CardanoMode) (TxInMode CardanoMode) SlotNo IO ())
C.localTxMonitoringClient = Maybe
  (LocalTxMonitorClient
     (TxIdInMode CardanoMode) (TxInMode CardanoMode) SlotNo IO ())
forall a. Maybe a
Nothing
    }
  where
    onIntersect :: ClientPipelinedStIntersect
  (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
onIntersect =
      ClientPipelinedStIntersect :: forall header point tip (m :: * -> *) a.
(point -> tip -> m (ClientPipelinedStIdle 'Z header point tip m a))
-> (tip -> m (ClientPipelinedStIdle 'Z header point tip m a))
-> ClientPipelinedStIntersect header point tip m a
CSP.ClientPipelinedStIntersect
        { recvMsgIntersectFound :: ChainPoint
-> ChainTip
-> IO
     (ClientPipelinedStIdle
        'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
CSP.recvMsgIntersectFound = \ChainPoint
_ ChainTip
_ -> ClientPipelinedStIdle
  'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientPipelinedStIdle
        'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ClientPipelinedStIdle
   'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
 -> IO
      (ClientPipelinedStIdle
         'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ()))
-> ClientPipelinedStIdle
     'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientPipelinedStIdle
        'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall a b. (a -> b) -> a -> b
$ Word32
-> ClientPipelinedStIdle
     'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
work Word32
n
        , recvMsgIntersectNotFound :: ChainTip
-> IO
     (ClientPipelinedStIdle
        'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
CSP.recvMsgIntersectNotFound = ChainSyncEventException
-> ChainTip
-> IO
     (ClientPipelinedStIdle
        'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall a e. Exception e => e -> a
throw ChainSyncEventException
H.NoIntersectionFound
        }

    work :: Word32 -> CSP.ClientPipelinedStIdle 'Z (C.BlockInMode C.CardanoMode) C.ChainPoint C.ChainTip IO ()
    work :: Word32
-> ClientPipelinedStIdle
     'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
work Word32
pipelineSize = WithOrigin BlockNo
-> WithOrigin BlockNo
-> Nat 'Z
-> ClientPipelinedStIdle
     'Z (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall (n :: N).
WithOrigin BlockNo
-> WithOrigin BlockNo
-> Nat n
-> ClientPipelinedStIdle
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
requestMore WithOrigin BlockNo
forall t. WithOrigin t
Origin WithOrigin BlockNo
forall t. WithOrigin t
Origin Nat 'Z
forall (n :: N). ('Z ~ n) => Nat n
Zero
      where
          requestMore -- was clientIdle_RequestMoreN
            :: WithOrigin C.BlockNo -> WithOrigin C.BlockNo -> Nat n
            -> CSP.ClientPipelinedStIdle n (C.BlockInMode C.CardanoMode) C.ChainPoint C.ChainTip IO ()
          requestMore :: WithOrigin BlockNo
-> WithOrigin BlockNo
-> Nat n
-> ClientPipelinedStIdle
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
requestMore WithOrigin BlockNo
clientTip WithOrigin BlockNo
serverTip Nat n
rqsInFlight = let
            in case Word32
-> Nat n
-> WithOrigin BlockNo
-> WithOrigin BlockNo
-> PipelineDecision n
forall (n :: N).
Word32
-> Nat n
-> WithOrigin BlockNo
-> WithOrigin BlockNo
-> PipelineDecision n
pipelineDecisionMax Word32
pipelineSize Nat n
rqsInFlight WithOrigin BlockNo
clientTip WithOrigin BlockNo
serverTip of
                -- handle a response
                PipelineDecision n
Collect -> case Nat n
rqsInFlight of
                  Succ Nat n
predN -> Maybe
  (IO
     (ClientPipelinedStIdle
        ('S n) (BlockInMode CardanoMode) ChainPoint ChainTip IO ()))
-> ClientStNext
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> ClientPipelinedStIdle
     ('S n) (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall (m :: * -> *) (n1 :: N) header point tip a.
Maybe (m (ClientPipelinedStIdle ('S n1) header point tip m a))
-> ClientStNext n1 header point tip m a
-> ClientPipelinedStIdle ('S n1) header point tip m a
CSP.CollectResponse Maybe
  (IO
     (ClientPipelinedStIdle
        ('S n) (BlockInMode CardanoMode) ChainPoint ChainTip IO ()))
forall a. Maybe a
Nothing (Nat n
-> ClientStNext
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall (n :: N).
Nat n
-> ClientStNext
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
clientNextN Nat n
predN)
                -- fire more requests
                PipelineDecision n
_ -> ClientPipelinedStIdle
  ('S n) (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> ClientPipelinedStIdle
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall (n :: N) header point tip (m :: * -> *) a.
ClientPipelinedStIdle ('S n) header point tip m a
-> ClientPipelinedStIdle n header point tip m a
CSP.SendMsgRequestNextPipelined (WithOrigin BlockNo
-> WithOrigin BlockNo
-> Nat ('S n)
-> ClientPipelinedStIdle
     ('S n) (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall (n :: N).
WithOrigin BlockNo
-> WithOrigin BlockNo
-> Nat n
-> ClientPipelinedStIdle
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
requestMore WithOrigin BlockNo
clientTip WithOrigin BlockNo
serverTip (Nat n -> Nat ('S n)
forall (m :: N) (n :: N). (m ~ 'S n) => Nat n -> Nat m
Succ Nat n
rqsInFlight))

          clientNextN
            :: Nat n
            -> CSP.ClientStNext n (C.BlockInMode C.CardanoMode) C.ChainPoint C.ChainTip IO ()
          clientNextN :: Nat n
-> ClientStNext
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
clientNextN Nat n
rqsInFlight = ClientStNext :: forall (n :: N) header point tip (m :: * -> *) a.
(header -> tip -> m (ClientPipelinedStIdle n header point tip m a))
-> (point
    -> tip -> m (ClientPipelinedStIdle n header point tip m a))
-> ClientStNext n header point tip m a
CSP.ClientStNext
            { recvMsgRollForward :: BlockInMode CardanoMode
-> ChainTip
-> IO
     (ClientPipelinedStIdle
        n (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
CSP.recvMsgRollForward = \BlockInMode CardanoMode
bim ChainTip
ct -> do
                ChainSyncEvent (BlockInMode CardanoMode) -> IO ()
callback (ChainSyncEvent (BlockInMode CardanoMode) -> IO ())
-> ChainSyncEvent (BlockInMode CardanoMode) -> IO ()
forall a b. (a -> b) -> a -> b
$ BlockInMode CardanoMode
-> ChainTip -> ChainSyncEvent (BlockInMode CardanoMode)
forall a. a -> ChainTip -> ChainSyncEvent a
H.RollForward BlockInMode CardanoMode
bim ChainTip
ct
                ClientPipelinedStIdle
  n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientPipelinedStIdle
        n (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall (m :: * -> *) a. Monad m => a -> m a
return (ClientPipelinedStIdle
   n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
 -> IO
      (ClientPipelinedStIdle
         n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()))
-> ClientPipelinedStIdle
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientPipelinedStIdle
        n (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall a b. (a -> b) -> a -> b
$ WithOrigin BlockNo
-> WithOrigin BlockNo
-> Nat n
-> ClientPipelinedStIdle
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall (n :: N).
WithOrigin BlockNo
-> WithOrigin BlockNo
-> Nat n
-> ClientPipelinedStIdle
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
requestMore (BlockNo -> WithOrigin BlockNo
forall t. t -> WithOrigin t
At (BlockNo -> WithOrigin BlockNo) -> BlockNo -> WithOrigin BlockNo
forall a b. (a -> b) -> a -> b
$ BlockInMode CardanoMode -> BlockNo
H.bimBlockNo BlockInMode CardanoMode
bim) (ChainTip -> WithOrigin BlockNo
H.fromChainTip ChainTip
ct) Nat n
rqsInFlight
            , recvMsgRollBackward :: ChainPoint
-> ChainTip
-> IO
     (ClientPipelinedStIdle
        n (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
CSP.recvMsgRollBackward = \ChainPoint
cp ChainTip
ct -> do
                ChainSyncEvent (BlockInMode CardanoMode) -> IO ()
callback (ChainSyncEvent (BlockInMode CardanoMode) -> IO ())
-> ChainSyncEvent (BlockInMode CardanoMode) -> IO ()
forall a b. (a -> b) -> a -> b
$ ChainPoint -> ChainTip -> ChainSyncEvent (BlockInMode CardanoMode)
forall a. ChainPoint -> ChainTip -> ChainSyncEvent a
H.RollBackward ChainPoint
cp ChainTip
ct
                ClientPipelinedStIdle
  n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientPipelinedStIdle
        n (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall (m :: * -> *) a. Monad m => a -> m a
return (ClientPipelinedStIdle
   n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
 -> IO
      (ClientPipelinedStIdle
         n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()))
-> ClientPipelinedStIdle
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientPipelinedStIdle
        n (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall a b. (a -> b) -> a -> b
$ WithOrigin BlockNo
-> WithOrigin BlockNo
-> Nat n
-> ClientPipelinedStIdle
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall (n :: N).
WithOrigin BlockNo
-> WithOrigin BlockNo
-> Nat n
-> ClientPipelinedStIdle
     n (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
requestMore WithOrigin BlockNo
forall t. WithOrigin t
Origin (ChainTip -> WithOrigin BlockNo
H.fromChainTip ChainTip
ct) Nat n
rqsInFlight
            }

blocksCallback
  :: C.LocalNodeConnectInfo C.CardanoMode -> C.ChainPoint
  -> (H.ChainSyncEvent (C.BlockInMode C.CardanoMode) -> IO ())
  -> IO ()
blocksCallback :: LocalNodeConnectInfo CardanoMode
-> ChainPoint
-> (ChainSyncEvent (BlockInMode CardanoMode) -> IO ())
-> IO ()
blocksCallback LocalNodeConnectInfo CardanoMode
con ChainPoint
point ChainSyncEvent (BlockInMode CardanoMode) -> IO ()
callback =
  LocalNodeConnectInfo CardanoMode
-> LocalNodeClientProtocolsInMode CardanoMode -> IO ()
forall mode.
LocalNodeConnectInfo mode
-> LocalNodeClientProtocolsInMode mode -> IO ()
C.connectToLocalNode LocalNodeConnectInfo CardanoMode
con (LocalNodeClientProtocolsInMode CardanoMode -> IO ())
-> LocalNodeClientProtocolsInMode CardanoMode -> IO ()
forall a b. (a -> b) -> a -> b
$ LocalNodeClientProtocols :: forall block point tip slot tx txid txerr (query :: * -> *)
       (m :: * -> *).
LocalChainSyncClient block point tip m
-> Maybe (LocalTxSubmissionClient tx txerr m ())
-> Maybe (LocalStateQueryClient block point query m ())
-> Maybe (LocalTxMonitorClient txid tx slot m ())
-> LocalNodeClientProtocols
     block point tip slot tx txid txerr query m
C.LocalNodeClientProtocols
    { localChainSyncClient :: LocalChainSyncClient
  (BlockInMode CardanoMode) ChainPoint ChainTip IO
C.localChainSyncClient    = ChainSyncClient (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> LocalChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO
forall block point tip (m :: * -> *).
ChainSyncClient block point tip m ()
-> LocalChainSyncClient block point tip m
C.LocalChainSyncClient (ChainSyncClient
   (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
 -> LocalChainSyncClient
      (BlockInMode CardanoMode) ChainPoint ChainTip IO)
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> LocalChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO
forall a b. (a -> b) -> a -> b
$ IO
  (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall header point tip (m :: * -> *) a.
m (ClientStIdle header point tip m a)
-> ChainSyncClient header point tip m a
CS.ChainSyncClient (IO
   (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
 -> ChainSyncClient
      (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> IO
     (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall a b. (a -> b) -> a -> b
$ ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
 -> IO
      (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ()))
-> ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall a b. (a -> b) -> a -> b
$ [ChainPoint]
-> ClientStIntersect
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall point header tip (m :: * -> *) a.
[point]
-> ClientStIntersect header point tip m a
-> ClientStIdle header point tip m a
CS.SendMsgFindIntersect [ChainPoint
point] ClientStIntersect
  (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
onIntersect
    , localTxSubmissionClient :: Maybe
  (LocalTxSubmissionClient
     (TxInMode CardanoMode) (TxValidationErrorInMode CardanoMode) IO ())
C.localTxSubmissionClient = Maybe
  (LocalTxSubmissionClient
     (TxInMode CardanoMode) (TxValidationErrorInMode CardanoMode) IO ())
forall a. Maybe a
Nothing
    , localStateQueryClient :: Maybe
  (LocalStateQueryClient
     (BlockInMode CardanoMode)
     ChainPoint
     (QueryInMode CardanoMode)
     IO
     ())
C.localStateQueryClient   = Maybe
  (LocalStateQueryClient
     (BlockInMode CardanoMode)
     ChainPoint
     (QueryInMode CardanoMode)
     IO
     ())
forall a. Maybe a
Nothing
    , localTxMonitoringClient :: Maybe
  (LocalTxMonitorClient
     (TxIdInMode CardanoMode) (TxInMode CardanoMode) SlotNo IO ())
C.localTxMonitoringClient = Maybe
  (LocalTxMonitorClient
     (TxIdInMode CardanoMode) (TxInMode CardanoMode) SlotNo IO ())
forall a. Maybe a
Nothing
    }
  where
    onIntersect :: ClientStIntersect
  (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
onIntersect =
      ClientStIntersect :: forall header point tip (m :: * -> *) a.
(point -> tip -> ChainSyncClient header point tip m a)
-> (tip -> ChainSyncClient header point tip m a)
-> ClientStIntersect header point tip m a
CS.ClientStIntersect
        { recvMsgIntersectFound :: ChainPoint
-> ChainTip
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
CS.recvMsgIntersectFound = \ChainPoint
_ ChainTip
_ -> IO
  (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall header point tip (m :: * -> *) a.
m (ClientStIdle header point tip m a)
-> ChainSyncClient header point tip m a
CS.ChainSyncClient IO
  (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
sendRequestNext
        , recvMsgIntersectNotFound :: ChainTip
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
CS.recvMsgIntersectNotFound = ChainSyncEventException
-> ChainTip
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall a e. Exception e => e -> a
throw ChainSyncEventException
H.NoIntersectionFound
        }
    sendRequestNext :: IO
  (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
sendRequestNext = ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
 -> IO
      (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ()))
-> ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall a b. (a -> b) -> a -> b
$ ClientStNext (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientStNext (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall header point tip (m :: * -> *) a.
ClientStNext header point tip m a
-> m (ClientStNext header point tip m a)
-> ClientStIdle header point tip m a
CS.SendMsgRequestNext ClientStNext (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
onNext (ClientStNext (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
-> IO
     (ClientStNext (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure ClientStNext (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
onNext)
      where
        onNext :: ClientStNext (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
onNext = ClientStNext :: forall header point tip (m :: * -> *) a.
(header -> tip -> ChainSyncClient header point tip m a)
-> (point -> tip -> ChainSyncClient header point tip m a)
-> ClientStNext header point tip m a
CS.ClientStNext
            { recvMsgRollForward :: BlockInMode CardanoMode
-> ChainTip
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
CS.recvMsgRollForward = \BlockInMode CardanoMode
bim ChainTip
ct -> IO
  (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall header point tip (m :: * -> *) a.
m (ClientStIdle header point tip m a)
-> ChainSyncClient header point tip m a
CS.ChainSyncClient (IO
   (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
 -> ChainSyncClient
      (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> IO
     (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall a b. (a -> b) -> a -> b
$ do
                ChainSyncEvent (BlockInMode CardanoMode) -> IO ()
callback (ChainSyncEvent (BlockInMode CardanoMode) -> IO ())
-> ChainSyncEvent (BlockInMode CardanoMode) -> IO ()
forall a b. (a -> b) -> a -> b
$ BlockInMode CardanoMode
-> ChainTip -> ChainSyncEvent (BlockInMode CardanoMode)
forall a. a -> ChainTip -> ChainSyncEvent a
H.RollForward BlockInMode CardanoMode
bim ChainTip
ct
                IO
  (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
sendRequestNext
            , recvMsgRollBackward :: ChainPoint
-> ChainTip
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
CS.recvMsgRollBackward = \ChainPoint
cp ChainTip
ct -> IO
  (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall header point tip (m :: * -> *) a.
m (ClientStIdle header point tip m a)
-> ChainSyncClient header point tip m a
CS.ChainSyncClient (IO
   (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
 -> ChainSyncClient
      (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> IO
     (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
-> ChainSyncClient
     (BlockInMode CardanoMode) ChainPoint ChainTip IO ()
forall a b. (a -> b) -> a -> b
$ do
                ChainSyncEvent (BlockInMode CardanoMode) -> IO ()
callback (ChainSyncEvent (BlockInMode CardanoMode) -> IO ())
-> ChainSyncEvent (BlockInMode CardanoMode) -> IO ()
forall a b. (a -> b) -> a -> b
$ ChainPoint -> ChainTip -> ChainSyncEvent (BlockInMode CardanoMode)
forall a. ChainPoint -> ChainTip -> ChainSyncEvent a
H.RollBackward ChainPoint
cp ChainTip
ct
                IO
  (ClientStIdle (BlockInMode CardanoMode) ChainPoint ChainTip IO ())
sendRequestNext
            }