-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathOuroboros.hs
493 lines (465 loc) · 16.1 KB
/
Ouroboros.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
-- | Ouroboros-based implementation of 'Hydra.Network' interface.
-- This implements a dumb 'FireForget' protocol and maintains one connection to each peer.
-- Contrary to other protocols implemented in Ouroboros, this is a push-based protocol.
module Hydra.Network.Ouroboros (
withIOManager,
module Hydra.Network,
module Hydra.Network.Ouroboros,
module Hydra.Network.Ouroboros.VersionedProtocol,
) where
import Control.Monad.Class.MonadAsync (wait)
import Hydra.Network.Ouroboros.VersionedProtocol (
HydraNetworkConfig (..),
HydraVersionedProtocolData (..),
hydraVersionedProtocolCodec,
hydraVersionedProtocolDataCodec,
)
import Hydra.Prelude
import Codec.CBOR.Term (Term)
import Codec.CBOR.Term qualified as CBOR
import Control.Concurrent.STM (
TChan,
dupTChan,
newBroadcastTChanIO,
readTChan,
writeTChan,
)
import Control.Exception (IOException)
import Data.Aeson (object, (.=))
import Data.Aeson qualified as Aeson
import Data.Aeson.Types qualified as Aeson
import Data.Map.Strict as Map
import Data.Text qualified as T
import Hydra.Logging (Tracer (..), nullTracer)
import Hydra.Network (
Host (..),
Network (..),
NetworkCallback (..),
NetworkComponent,
PortNumber,
)
import Hydra.Network.Message (
HydraHandshakeRefused (..),
HydraVersionedProtocolNumber (..),
KnownHydraVersions (..),
)
import Hydra.Network.Ouroboros.Client as FireForget (
FireForgetClient (..),
fireForgetClientPeer,
)
import Hydra.Network.Ouroboros.Server as FireForget (
FireForgetServer (..),
fireForgetServerPeer,
)
import Hydra.Network.Ouroboros.Type (
FireForget (..),
Message (..),
codecFireForget,
)
import Network.Mux.Compat (
WithMuxBearer (..),
)
import Network.Socket (
AddrInfo (addrAddress),
NameInfoFlag (..),
SockAddr,
Socket,
defaultHints,
getAddrInfo,
getNameInfo,
getPeerName,
)
import Network.TypedProtocol.Codec (
AnyMessageAndAgency (..),
)
import Network.TypedProtocol.Pipelined ()
import Ouroboros.Network.Driver.Simple (
TraceSendRecv (..),
)
import Ouroboros.Network.ErrorPolicy (
ErrorPolicyTrace,
WithAddr (WithAddr),
nullErrorPolicies,
)
import Ouroboros.Network.IOManager (IOManager, withIOManager)
import Ouroboros.Network.Mux (
MiniProtocol (
MiniProtocol,
miniProtocolLimits,
miniProtocolNum,
miniProtocolRun
),
MiniProtocolCb,
MiniProtocolLimits (..),
MiniProtocolNum (MiniProtocolNum),
MuxMode (..),
OuroborosApplication (..),
OuroborosApplicationWithMinimalCtx,
RunMiniProtocol (..),
mkMiniProtocolCbFromPeer,
)
import Ouroboros.Network.Protocol.Handshake.Codec (codecHandshake, noTimeLimitsHandshake)
import Ouroboros.Network.Protocol.Handshake.Type (Handshake, HandshakeProtocolError (..), Message (..), RefuseReason (..))
import Ouroboros.Network.Protocol.Handshake.Version (acceptableVersion, queryVersion, simpleSingletonVersions)
import Ouroboros.Network.Server.Socket (AcceptedConnectionsLimit (AcceptedConnectionsLimit))
import Ouroboros.Network.Snocket (makeSocketBearer, socketSnocket)
import Ouroboros.Network.Socket (
AcceptConnectionsPolicyTrace,
ConnectionId (..),
HandshakeCallbacks (..),
NetworkConnectTracers (..),
NetworkServerTracers (..),
SomeResponderApplication (..),
connectToNodeSocket,
newNetworkMutableState,
withServerNode,
)
import Ouroboros.Network.Subscription (
IPSubscriptionTarget (IPSubscriptionTarget),
SubscriptionTrace,
WithIPList,
)
import Ouroboros.Network.Subscription qualified as Subscription
import Ouroboros.Network.Subscription.Ip (SubscriptionParams (..), WithIPList (WithIPList))
import Ouroboros.Network.Subscription.Worker (LocalAddresses (LocalAddresses))
withOuroborosNetwork ::
forall inbound outbound.
(ToCBOR outbound, FromCBOR outbound) =>
(ToCBOR inbound, FromCBOR inbound) =>
Tracer IO (WithHost (TraceOuroborosNetwork outbound)) ->
HydraNetworkConfig ->
(HydraHandshakeRefused -> IO ()) ->
NetworkComponent IO inbound outbound ()
withOuroborosNetwork
tracer
HydraNetworkConfig{protocolVersion, localHost, remoteHosts}
handshakeCallback
NetworkCallback{deliver}
between = do
bchan <- newBroadcastTChanIO
let newBroadcastChannel = atomically $ dupTChan bchan
-- NOTE: There should only be one `IOManager` instance per process. Should we
-- want to use ouroboros network framework in other places, we must factor out
-- this instantiation
withIOManager $ \iomgr -> do
withServerListening iomgr hydraServer $
race_ (connect iomgr newBroadcastChannel hydraClient) $ do
between $
Network
{ broadcast = atomically . writeTChan bchan
}
where
resolveSockAddr :: Host -> IO SockAddr
resolveSockAddr Host{hostname, port} = do
is <- getAddrInfo (Just defaultHints) (Just $ toString hostname) (Just $ show port)
case is of
(info : _) -> pure $ addrAddress info
_ -> error "getAdrrInfo failed.. do proper error handling"
getHost :: SockAddr -> IO Host
getHost sockAddr = do
(mHost, mPort) <- getNameInfo [NI_NUMERICHOST, NI_NUMERICSERV] True True sockAddr
maybe (error "getNameInfo failed.. do proper error handling") pure $ do
host <- T.pack <$> mHost
port <- readMaybe =<< mPort
pure $ Host host port
connect ::
IOManager ->
IO t ->
( t ->
OuroborosApplicationWithMinimalCtx
InitiatorMode
SockAddr
LByteString
IO
()
Void
) ->
IO Void
connect iomgr newBroadcastChannel app = do
-- REVIEW(SN): move outside to have this information available?
networkState <- newNetworkMutableState
-- Using port number 0 to let the operating system pick a random port
localAddr <- resolveSockAddr localHost{port = 0}
remoteAddrs <- forM remoteHosts resolveSockAddr
let sn = socketSnocket iomgr
Subscription.ipSubscriptionWorker
sn
(contramap (WithHost localHost . TraceSubscriptions) tracer)
(contramap (WithHost localHost . TraceErrorPolicy) tracer)
networkState
(subscriptionParams localAddr remoteAddrs)
( \sock ->
actualConnect iomgr newBroadcastChannel app sock `catch` \e -> do
host <- getHost =<< getPeerName sock
onHandshakeError host e
)
onHandshakeError :: Host -> HandshakeProtocolError HydraVersionedProtocolNumber -> IO ()
onHandshakeError remoteHost = \case
HandshakeError (VersionMismatch theirVersions _) -> do
handshakeCallback
HydraHandshakeRefused
{ ourVersion = protocolVersion
, theirVersions = KnownHydraVersions theirVersions
, remoteHost
}
_ ->
handshakeCallback
HydraHandshakeRefused
{ ourVersion = protocolVersion
, theirVersions = NoKnownHydraVersions
, remoteHost
}
subscriptionParams ::
SockAddr ->
[SockAddr] ->
SubscriptionParams a IPSubscriptionTarget
subscriptionParams localAddr remoteAddrs =
SubscriptionParams
{ spLocalAddresses = LocalAddresses (Just localAddr) Nothing Nothing
, spConnectionAttemptDelay = const Nothing
, spErrorPolicies = nullErrorPolicies
, spSubscriptionTarget = IPSubscriptionTarget remoteAddrs (length remoteAddrs)
}
actualConnect ::
IOManager ->
IO t ->
(t -> OuroborosApplicationWithMinimalCtx 'InitiatorMode SockAddr LByteString IO () Void) ->
Socket ->
IO ()
actualConnect iomgr newBroadcastChannel app sn = do
chan <- newBroadcastChannel
connectToNodeSocket
iomgr
(codecHandshake hydraVersionedProtocolCodec)
noTimeLimitsHandshake
hydraVersionedProtocolDataCodec
networkConnectTracers
(HandshakeCallbacks acceptableVersion queryVersion)
(simpleSingletonVersions protocolVersion MkHydraVersionedProtocolData (app chan))
sn
where
networkConnectTracers :: NetworkConnectTracers SockAddr HydraVersionedProtocolNumber
networkConnectTracers =
NetworkConnectTracers
{ nctMuxTracer = nullTracer
, nctHandshakeTracer = contramap (WithHost localHost . TraceHandshake) tracer
}
withServerListening ::
IOManager ->
OuroborosApplicationWithMinimalCtx 'ResponderMode SockAddr LByteString IO a b ->
IO b ->
IO ()
withServerListening iomgr app continuation = do
networkState <- newNetworkMutableState
localAddr <- resolveSockAddr localHost
-- TODO(SN): whats this? _ <- async $ cleanNetworkMutableState networkState
handle onIOException
$ withServerNode
(socketSnocket iomgr)
makeSocketBearer
notConfigureSocket
networkServerTracers
networkState
(AcceptedConnectionsLimit maxBound maxBound 0)
localAddr
(codecHandshake hydraVersionedProtocolCodec)
noTimeLimitsHandshake
hydraVersionedProtocolDataCodec
(HandshakeCallbacks acceptableVersion queryVersion)
(simpleSingletonVersions protocolVersion MkHydraVersionedProtocolData (SomeResponderApplication app))
nullErrorPolicies
$ \_addr serverAsync -> do
race_ (wait serverAsync) continuation
where
notConfigureSocket :: a -> b -> IO ()
notConfigureSocket _ _ = pure ()
networkServerTracers :: NetworkServerTracers SockAddr HydraVersionedProtocolNumber
networkServerTracers =
NetworkServerTracers
{ nstMuxTracer = nullTracer
, nstHandshakeTracer = contramap (WithHost localHost . TraceHandshake) tracer
, nstErrorPolicyTracer = contramap (WithHost localHost . TraceErrorPolicy) tracer
, nstAcceptPolicyTracer = contramap (WithHost localHost . TraceAcceptPolicy) tracer
}
onIOException :: IOException -> IO ()
onIOException ioException =
throwIO $
NetworkServerListenException
{ ioException
, localHost
}
hydraClient ::
TChan outbound ->
OuroborosApplicationWithMinimalCtx 'InitiatorMode addr LByteString IO () Void
hydraClient chan =
OuroborosApplication
[ MiniProtocol
{ miniProtocolNum = MiniProtocolNum 42
, miniProtocolLimits = maximumMiniProtocolLimits
, miniProtocolRun = InitiatorProtocolOnly initiator
}
]
where
initiator :: MiniProtocolCb ctx LByteString IO ()
initiator =
mkMiniProtocolCbFromPeer
( const
(nullTracer, codecFireForget, fireForgetClientPeer $ client chan)
)
hydraServer ::
OuroborosApplicationWithMinimalCtx 'ResponderMode addr LByteString IO Void ()
hydraServer =
OuroborosApplication
[ MiniProtocol
{ miniProtocolNum = MiniProtocolNum 42
, miniProtocolLimits = maximumMiniProtocolLimits
, miniProtocolRun = ResponderProtocolOnly responder
}
]
where
responder :: MiniProtocolCb ctx LByteString IO ()
responder = mkMiniProtocolCbFromPeer (const (nullTracer, codecFireForget, fireForgetServerPeer server))
-- TODO: provide sensible limits
-- https://github.com/input-output-hk/ouroboros-network/issues/575
maximumMiniProtocolLimits :: MiniProtocolLimits
maximumMiniProtocolLimits =
MiniProtocolLimits{maximumIngressQueue = maxBound}
client ::
TChan outbound ->
FireForgetClient outbound IO ()
client chan =
Idle $ do
atomically (readTChan chan) <&> \msg ->
SendMsg msg (pure $ client chan)
server :: FireForgetServer inbound IO ()
server =
FireForgetServer
{ recvMsg = \msg -> deliver msg $> server
, recvMsgDone = pure ()
}
data NetworkServerListenException = NetworkServerListenException
{ ioException :: IOException
, localHost :: Host
}
deriving stock (Show)
instance Exception NetworkServerListenException
data WithHost trace = WithHost Host trace
deriving stock (Show)
instance ToJSON trace => ToJSON (WithHost trace) where
toJSON (WithHost h tr) =
object
[ "host" .= h
, "data" .= tr
]
data TraceOuroborosNetwork msg
= TraceSubscriptions (WithIPList (SubscriptionTrace SockAddr))
| TraceErrorPolicy (WithAddr SockAddr ErrorPolicyTrace)
| TraceAcceptPolicy AcceptConnectionsPolicyTrace
| TraceHandshake (WithMuxBearer (ConnectionId SockAddr) (TraceSendRecv (Handshake HydraVersionedProtocolNumber CBOR.Term)))
| TraceSendRecv (TraceSendRecv (FireForget msg))
-- NOTE: cardano-node would have orphan ToObject instances for most of these
-- types, but we want to avoid that dependency.
instance ToJSON msg => ToJSON (TraceOuroborosNetwork msg) where
toJSON = \case
TraceSubscriptions withIpList ->
tagged "TraceSubscriptions" ["subscriptions" .= encodeWithIPList withIpList]
TraceErrorPolicy withAddr ->
tagged "TraceErrorPolicy" ["errors" .= encodeWithAddr withAddr]
TraceAcceptPolicy accept ->
tagged "TraceAcceptPolicy" ["accept" .= show @Text accept]
TraceHandshake handshake ->
tagged "TraceHandshake" ["handshake" .= encodeTraceSendRecvHandshake handshake]
TraceSendRecv sndRcv ->
tagged "TraceSendRecv" ["trace" .= encodeTraceSendRecvFireForget sndRcv]
tagged :: Text -> [Aeson.Pair] -> Aeson.Value
tagged tag pairs = object (("tag" .= tag) : pairs)
encodeWithIPList :: WithIPList (SubscriptionTrace SockAddr) -> Aeson.Value
encodeWithIPList (WithIPList src dsts ev) =
tagged
"WithIPList"
[ "src" .= show @Text src
, "dsts" .= show @Text dsts
, "event" .= show @Text ev
]
encodeWithAddr :: WithAddr SockAddr ErrorPolicyTrace -> Aeson.Value
encodeWithAddr (WithAddr addr ev) =
tagged
"WithAddr"
[ "addr" .= show @Text addr
, "event" .= show @Text ev
]
encodeTraceSendRecvHandshake ::
WithMuxBearer (ConnectionId SockAddr) (TraceSendRecv (Handshake HydraVersionedProtocolNumber CBOR.Term)) ->
[Aeson.Pair]
encodeTraceSendRecvHandshake = \case
WithMuxBearer peerId (TraceSendMsg (AnyMessageAndAgency agency msg)) ->
[ "event" .= ("send" :: String)
, "agency" .= (show agency :: Text)
, "peer" .= (show peerId :: Text)
]
++ encodeMsg msg
WithMuxBearer peerId (TraceRecvMsg (AnyMessageAndAgency agency msg)) ->
[ "event" .= ("receive" :: Text)
, "agency" .= (show agency :: Text)
, "peer" .= (show peerId :: Text)
]
++ encodeMsg msg
where
encodeMsg ::
Message (Handshake HydraVersionedProtocolNumber Term) from to ->
[Aeson.Pair]
encodeMsg = \case
MsgProposeVersions versions ->
[ "tag" .= ("ProposeVersions" :: String)
, "versions" .= (show <$> Map.keys versions :: [Text])
]
MsgAcceptVersion v _ ->
[ "tag" .= ("AcceptVersion" :: String)
, "version" .= (show v :: Text)
]
MsgRefuse reason ->
[ "tag" .= ("RefuseVersions" :: String)
, "reason" .= encodeRefuseReason reason
]
MsgReplyVersions versions ->
[ "tag" .= ("ReplyVersions" :: String)
, "versions" .= (show <$> Map.keys versions :: [Text])
]
MsgQueryReply versions ->
[ "tag" .= ("MsgQueryReply" :: String)
, "versions" .= (show <$> Map.keys versions :: [Text])
]
encodeRefuseReason ::
RefuseReason vNumber ->
Aeson.Value
encodeRefuseReason = \case
VersionMismatch{} -> Aeson.String "VersionMismatchOrUnknown"
HandshakeDecodeError{} -> Aeson.String "HandshakeDecodeError"
Refused{} -> Aeson.String "ServerRejected"
encodeTraceSendRecvFireForget ::
forall msg.
ToJSON msg =>
TraceSendRecv (FireForget msg) ->
[Aeson.Pair]
encodeTraceSendRecvFireForget = \case
TraceSendMsg (AnyMessageAndAgency agency msg) ->
[ "event" .= ("send" :: String)
, "agency" .= (show agency :: Text)
]
++ encodeMsg msg
TraceRecvMsg (AnyMessageAndAgency agency msg) ->
[ "event" .= ("receive" :: Text)
, "agency" .= (show agency :: Text)
]
++ encodeMsg msg
where
encodeMsg ::
Message (FireForget msg) from to ->
[Aeson.Pair]
encodeMsg = \case
MsgSend msg ->
[ "send" .= msg
]
MsgDone ->
[ "done" .= ()
]