-
Notifications
You must be signed in to change notification settings - Fork 86
/
Diffusion.hs
405 lines (385 loc) · 13.1 KB
/
Diffusion.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NamedFieldPuns #-}
module Ouroboros.Network.Diffusion
(
-- * Common API
DiffusionTracers
, DiffusionArguments
, getDiffusionArguments
, daDiffusionMode
, DiffusionApplications
, runDataDiffusion
, DiffusionInitializationTracer(..)
, DiffusionFailure
-- * Non P2P API
, nullTracersNonP2P
, mkDiffusionTracersNonP2P
, mkDiffusionArgumentsNonP2P
, mkDiffusionApplicationsNonP2P
-- * P2P API
, nullTracersP2P
, mkDiffusionTracersP2P
, mkDiffusionArgumentsP2P
, mkDiffusionApplicationsP2P
)
where
import Control.Monad.Class.MonadSTM (STM)
import Data.ByteString.Lazy (ByteString)
import Data.Void (Void)
import Data.Map.Strict (Map)
import Data.Time (DiffTime)
import Data.Functor (void)
import Control.Exception (IOException)
import Control.Tracer (Tracer)
import Network.Mux (WithMuxBearer, MuxTrace)
import Network.Socket (SockAddr, Socket)
import Ouroboros.Network.PeerSelection.RootPeersDNS
( TracePublicRootPeers
, TraceLocalRootPeers
)
import Ouroboros.Network.PeerSelection.Governor.Types
( TracePeerSelection
, DebugPeerSelection
, PeerSelectionCounters
)
import Ouroboros.Network.ConnectionManager.Types
( ConnectionManagerTrace
)
import Ouroboros.Network.PeerSelection.PeerStateActions
( PeerSelectionActionsTrace
)
import Ouroboros.Network.ConnectionHandler
( ConnectionHandlerTrace
)
import Ouroboros.Network.Server2
( ServerTrace
)
import Ouroboros.Network.InboundGovernor
( InboundGovernorTrace
)
import Ouroboros.Network.NodeToNode
( RemoteAddress
, NodeToNodeVersionData
, DiffusionMode
, NodeToNodeVersion
, MiniProtocolParameters
, AcceptedConnectionsLimit
, IPSubscriptionTarget
, DnsSubscriptionTarget
)
import qualified Ouroboros.Network.NodeToNode as NTN
import Ouroboros.Network.NodeToClient
( LocalAddress
, LocalSocket
, NodeToClientVersionData
, Versions
, ConnectionId
, NodeToClientVersion
)
import qualified Ouroboros.Network.NodeToClient as NTC
import Ouroboros.Network.RethrowPolicy (RethrowPolicy)
import Ouroboros.Network.BlockFetch (FetchMode)
import Ouroboros.Network.Mux
( Bundle
, MiniProtocol
, MuxMode (..)
, OuroborosApplication
, ControlMessage
)
import Ouroboros.Network.PeerSelection.LedgerPeers
( LedgerPeersConsensusInterface
, TraceLedgerPeers
, RelayAddress
, UseLedgerAfter
)
import Ouroboros.Network.PeerSelection.PeerMetric (PeerMetrics)
import qualified Ouroboros.Network.Diffusion.Common as Common
import Ouroboros.Network.Diffusion.Common
( DiffusionInitializationTracer
, DiffusionFailure
, daDiffusionMode
, dtExtra
, daExtra
, dapExtra
)
import qualified Ouroboros.Network.Diffusion.P2P as P2P
import qualified Ouroboros.Network.Diffusion.NonP2P as NonP2P
-- | DiffusionTracers for either P2P or Non-P2P node
--
newtype DiffusionTracers =
DiffusionTracers
(Common.DiffusionTracers
(Either NonP2P.DiffusionTracersExtra
(P2P.DiffusionTracersExtra
RemoteAddress NodeToNodeVersion NodeToNodeVersionData
LocalAddress NodeToClientVersion NodeToClientVersionData
IOException IO))
RemoteAddress NodeToNodeVersion
LocalAddress NodeToClientVersion
IO)
-- | NonP2P null tracers
--
nullTracersNonP2P :: DiffusionTracers
nullTracersNonP2P =
DiffusionTracers (Common.nullTracers (Left NonP2P.nullTracers))
-- | P2P null tracers
--
nullTracersP2P :: DiffusionTracers
nullTracersP2P =
DiffusionTracers (Common.nullTracers (Right P2P.nullTracers))
-- | DiffusionArguments for either P2P or Non-P2P node
--
newtype DiffusionArguments m =
DiffusionArguments
{ getDiffusionArguments
:: Common.DiffusionArguments
(Either NonP2P.DiffusionArgumentsExtra
(P2P.DiffusionArgumentsExtra m))
Socket RemoteAddress
LocalSocket LocalAddress
}
-- | DiffusionApplications for either P2P or Non-P2P node
--
newtype DiffusionApplications =
DiffusionApplications
(Common.DiffusionApplications
(Either
NonP2P.DiffusionApplicationsExtra
(P2P.DiffusionApplicationsExtra RemoteAddress IO))
RemoteAddress NodeToNodeVersion NodeToNodeVersionData
LocalAddress NodeToClientVersion NodeToClientVersionData
IO
)
-- | Construct a value of NonP2P DiffusionArguments data type.
-- 'ouroboros-consensus' needs access to this constructor so we export this
-- function in order to avoid exporting the P2P and NonP2P internal modules.
--
mkDiffusionArgumentsNonP2P
:: Maybe (Either Socket RemoteAddress)
-> Maybe (Either Socket RemoteAddress)
-> Maybe (Either LocalSocket LocalAddress)
-> AcceptedConnectionsLimit
-> DiffusionMode
-> IPSubscriptionTarget
-> [DnsSubscriptionTarget]
-> DiffusionArguments m
mkDiffusionArgumentsNonP2P
a1 a2 a3 a4 a5 a6
a7 =
DiffusionArguments
$ Common.DiffusionArguments
a1 a2 a3 a4 a5
$ Left
$ NonP2P.DiffusionArgumentsExtra
a6 a7
-- | Construct a value of P2P DiffusionArguments data type.
-- 'ouroboros-consensus' needs access to this constructor so we export this
-- function in order to avoid exporting the P2P and NonP2P internal modules.
--
mkDiffusionArgumentsP2P
:: Maybe (Either Socket RemoteAddress)
-> Maybe (Either Socket RemoteAddress)
-> Maybe (Either LocalSocket LocalAddress)
-> AcceptedConnectionsLimit
-> DiffusionMode
-> NTN.PeerSelectionTargets
-> STM m [(Int, Map RelayAddress NTN.PeerAdvertise)]
-> STM m [RelayAddress]
-> STM m UseLedgerAfter
-> DiffTime
-> DiffTime
-> DiffusionArguments m
mkDiffusionArgumentsP2P
a1 a2 a3 a4 a5 a6
a7 a8 a9 a10 a11
=
DiffusionArguments
$ Common.DiffusionArguments
a1 a2 a3 a4 a5
$ Right
$ P2P.DiffusionArgumentsExtra
a6 a7 a8 a9 a10 a11
-- | Construct a value of NonP2P DiffusionApplications data type.
-- 'ouroboros-consensus' needs access to this constructor so we export this
-- function in order to avoid exporting the P2P and NonP2P internal modules.
--
mkDiffusionApplicationsNonP2P
:: Versions
NodeToNodeVersion
NodeToNodeVersionData
(Bundle
(ConnectionId RemoteAddress
-> STM IO ControlMessage
-> [MiniProtocol 'InitiatorMode ByteString IO () Void]))
-> Versions
NodeToNodeVersion
NodeToNodeVersionData
(Bundle
(ConnectionId RemoteAddress
-> STM IO ControlMessage
-> [MiniProtocol 'InitiatorResponderMode ByteString IO () ()]))
-> Versions
NodeToClientVersion
NodeToClientVersionData
(OuroborosApplication 'ResponderMode LocalAddress ByteString IO Void ())
-> LedgerPeersConsensusInterface IO
-> NTC.ErrorPolicies
-> DiffusionApplications
mkDiffusionApplicationsNonP2P
a1 a2 a3 a4 a5 =
DiffusionApplications
$ Common.DiffusionApplications
a1 a2 a3 a4
$ Left
$ NonP2P.DiffusionApplicationsExtra
a5
-- | Construct a value of P2P DiffusionApplications data type.
-- 'ouroboros-consensus' needs access to this constructor so we export this
-- function in order to avoid exporting the P2P and NonP2P internal modules.
--
mkDiffusionApplicationsP2P
:: Versions
NodeToNodeVersion
NodeToNodeVersionData
(Bundle
(ConnectionId RemoteAddress
-> STM IO ControlMessage
-> [MiniProtocol 'InitiatorMode ByteString IO () Void]))
-> Versions
NodeToNodeVersion
NodeToNodeVersionData
(Bundle
(ConnectionId RemoteAddress
-> STM IO ControlMessage
-> [MiniProtocol 'InitiatorResponderMode ByteString IO () ()]))
-> Versions
NodeToClientVersion
NodeToClientVersionData
(OuroborosApplication 'ResponderMode LocalAddress ByteString IO Void ())
-> LedgerPeersConsensusInterface IO
-> MiniProtocolParameters
-> RethrowPolicy
-> RethrowPolicy
-> PeerMetrics IO RemoteAddress
-> STM IO FetchMode
-> DiffusionApplications
mkDiffusionApplicationsP2P
a1 a2 a3 a4 a5 a6
a7 a8 a9 =
DiffusionApplications
$ Common.DiffusionApplications
a1 a2 a3 a4
$ Right
$ P2P.DiffusionApplicationsExtra
a5 a6 a7 a8 a9
-- | Construct a value of NonP2P DiffusionTracers data type.
-- 'ouroboros-consensus' needs access to this constructor so we export this
-- function in order to avoid exporting the P2P and NonP2P internal modules.
--
mkDiffusionTracersNonP2P
:: Tracer IO (WithMuxBearer (ConnectionId SockAddr) MuxTrace)
-> Tracer IO (NTN.HandshakeTr RemoteAddress NodeToNodeVersion)
-> Tracer IO (WithMuxBearer (ConnectionId LocalAddress) MuxTrace)
-> Tracer IO (NTC.HandshakeTr LocalAddress NodeToClientVersion)
-> Tracer IO (DiffusionInitializationTracer SockAddr LocalAddress)
-> Tracer IO TraceLedgerPeers
-> Tracer IO (NTN.WithIPList (NTC.SubscriptionTrace SockAddr))
-> Tracer IO (NTN.WithDomainName (NTC.SubscriptionTrace SockAddr))
-> Tracer IO (NTN.WithDomainName NTN.DnsTrace)
-> Tracer IO (NTC.WithAddr SockAddr NTC.ErrorPolicyTrace)
-> Tracer IO (NTC.WithAddr LocalAddress NTC.ErrorPolicyTrace)
-> Tracer IO NTN.AcceptConnectionsPolicyTrace
-> DiffusionTracers
mkDiffusionTracersNonP2P
a1 a2 a3 a4 a5 a6 a7 a8 a9
a10 a11 a12 =
DiffusionTracers
$ Common.DiffusionTracers
a1 a2 a3 a4 a5 a6
$ Left $ NonP2P.DiffusionTracersExtra
a7 a8 a9 a10 a11 a12
-- | Construct a value of P2P DiffusionTracers data type.
-- ouroboros-consensus needs access to this constructor so we export this
-- function in order to avoid exporting the P2P and NonP2P internal modules.
--
mkDiffusionTracersP2P
:: Tracer IO (WithMuxBearer (ConnectionId SockAddr) MuxTrace)
-> Tracer IO (NTN.HandshakeTr RemoteAddress NodeToNodeVersion)
-> Tracer IO (WithMuxBearer (ConnectionId LocalAddress) MuxTrace)
-> Tracer IO (NTC.HandshakeTr LocalAddress NodeToClientVersion)
-> Tracer IO (DiffusionInitializationTracer SockAddr LocalAddress)
-> Tracer IO TraceLedgerPeers
-> Tracer IO (TraceLocalRootPeers SockAddr IOException)
-> Tracer IO TracePublicRootPeers
-> Tracer IO (TracePeerSelection SockAddr)
-> Tracer
IO
(DebugPeerSelection
SockAddr (P2P.NodeToNodePeerConnectionHandle 'InitiatorMode SockAddr IO Void))
-> Tracer
IO
(DebugPeerSelection
SockAddr
(P2P.NodeToNodePeerConnectionHandle 'InitiatorResponderMode SockAddr IO ()))
-> Tracer IO PeerSelectionCounters
-> Tracer IO (PeerSelectionActionsTrace SockAddr)
-> Tracer
IO
(ConnectionManagerTrace
SockAddr
(ConnectionHandlerTrace
NodeToNodeVersion NodeToNodeVersionData))
-> Tracer IO (ServerTrace SockAddr)
-> Tracer IO (InboundGovernorTrace SockAddr)
-> Tracer
IO
(ConnectionManagerTrace
LocalAddress
(ConnectionHandlerTrace
NodeToClientVersion NodeToClientVersionData))
-> Tracer IO (ServerTrace LocalAddress)
-> Tracer IO (InboundGovernorTrace LocalAddress)
-> DiffusionTracers
mkDiffusionTracersP2P
a1 a2 a3 a4 a5 a6 a7 a8 a9
a10 a11 a12 a13 a14 a15 a16
a17 a18 a19 =
DiffusionTracers
$ Common.DiffusionTracers
a1 a2 a3 a4 a5 a6
$ Right
$ P2P.DiffusionTracersExtra
a7 a8 a9 a10 a11 a12
a13 a14 a15 a16 a17
a18 a19
-- | runDataDiffusion for either P2P or Non-P2P node
--
runDataDiffusion
:: DiffusionTracers
-> DiffusionArguments IO
-> DiffusionApplications
-> IO ()
runDataDiffusion
(DiffusionTracers
tr@Common.DiffusionTracers
{ dtExtra })
(DiffusionArguments
dargs@Common.DiffusionArguments
{ daExtra })
(DiffusionApplications
dapps@Common.DiffusionApplications
{ dapExtra }) =
case (dtExtra, daExtra, dapExtra) of
(Left t, Left da, Left dapp) ->
NonP2P.runDataDiffusion
(tr { dtExtra = t})
(dargs { daExtra = da })
(dapps { dapExtra = dapp })
(Right t, Right da, Right dapp) ->
void
$ P2P.runDataDiffusion
(tr { dtExtra = t})
(dargs { daExtra = da })
(dapps { dapExtra = dapp })
_ ->
error "Non-matching arguments, every argument should be on the same side!"