/
NodeToNode.hs
713 lines (653 loc) · 28.1 KB
/
NodeToNode.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
-- | Intended for qualified import
module Ouroboros.Consensus.Network.NodeToNode (
-- * Handlers
Handlers (..)
, mkHandlers
-- * Codecs
, Codecs (..)
, defaultCodecs
, identityCodecs
-- * Tracers
, Tracers
, Tracers' (..)
, nullTracers
, showTracers
-- * Applications
, Apps (..)
, ClientApp
, ServerApp
, mkApps
-- ** Projections
, initiator
, responder
-- * Re-exports
, ChainSyncTimeout (..)
) where
import Codec.CBOR.Decoding (Decoder)
import Codec.CBOR.Encoding (Encoding)
import Control.Monad (forever)
import Control.Monad.Class.MonadTime (MonadTime)
import Control.Monad.Class.MonadTimer (MonadTimer)
import Control.Tracer
import Data.ByteString.Lazy (ByteString)
import Data.Map.Strict (Map)
import Data.Void (Void)
import Ouroboros.Network.AnchoredFragment (AnchoredFragment)
import Ouroboros.Network.AnchoredFragment.Completeness
import Ouroboros.Network.Block (Serialised (..), decodePoint,
decodeTip, encodePoint, encodeTip)
import Ouroboros.Network.BlockFetch
import Ouroboros.Network.BlockFetch.Client (BlockFetchClient,
blockFetchClient)
import Ouroboros.Network.Channel
import Ouroboros.Network.Codec
import Ouroboros.Network.DeltaQ
import Ouroboros.Network.Driver
import Ouroboros.Network.KeepAlive
import Ouroboros.Network.Mux
import Ouroboros.Network.NodeToNode
import Ouroboros.Network.Protocol.BlockFetch.Codec
import Ouroboros.Network.Protocol.BlockFetch.Server (BlockFetchServer,
blockFetchServerPeer)
import Ouroboros.Network.Protocol.BlockFetch.Type (BlockFetch (..))
import Ouroboros.Network.Protocol.ChainSync.ClientPipelined
import Ouroboros.Network.Protocol.ChainSync.Codec
import Ouroboros.Network.Protocol.ChainSync.PipelineDecision
import Ouroboros.Network.Protocol.ChainSync.Server
import Ouroboros.Network.Protocol.ChainSync.Type
import Ouroboros.Network.Protocol.KeepAlive.Client
import Ouroboros.Network.Protocol.KeepAlive.Codec
import Ouroboros.Network.Protocol.KeepAlive.Server
import Ouroboros.Network.Protocol.KeepAlive.Type
import qualified Ouroboros.Network.Protocol.Trans.Hello.Util as Hello
import Ouroboros.Network.Protocol.TxSubmission.Client
import Ouroboros.Network.Protocol.TxSubmission.Codec
import Ouroboros.Network.Protocol.TxSubmission.Server
import Ouroboros.Network.Protocol.TxSubmission.Type
import Ouroboros.Network.Protocol.TxSubmission2.Codec
import Ouroboros.Network.Protocol.TxSubmission2.Type
import Ouroboros.Network.TxSubmission.Inbound
import Ouroboros.Network.TxSubmission.Mempool.Reader
(mapTxSubmissionMempoolReader)
import Ouroboros.Network.TxSubmission.Outbound
import Ouroboros.Consensus.Block
import Ouroboros.Consensus.Ledger.SupportsMempool
import Ouroboros.Consensus.Ledger.SupportsProtocol
import Ouroboros.Consensus.MiniProtocol.BlockFetch.Server
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client
import Ouroboros.Consensus.MiniProtocol.ChainSync.Server
import Ouroboros.Consensus.Node.NetworkProtocolVersion
import Ouroboros.Consensus.Node.Run
import Ouroboros.Consensus.Node.Serialisation
import qualified Ouroboros.Consensus.Node.Tracers as Node
import Ouroboros.Consensus.NodeKernel
import qualified Ouroboros.Consensus.Storage.ChainDB.API as ChainDB
import Ouroboros.Consensus.Util (ShowProxy)
import Ouroboros.Consensus.Util.IOLike
import Ouroboros.Consensus.Util.Orphans ()
import Ouroboros.Consensus.Util.ResourceRegistry
import Ouroboros.Consensus.Storage.Serialisation (SerialisedHeader)
{-------------------------------------------------------------------------------
Handlers
-------------------------------------------------------------------------------}
-- | Protocol handlers for node-to-node (remote) communication
data Handlers m peer blk = Handlers {
hChainSyncClient
:: peer
-> NodeToNodeVersion
-> ControlMessageSTM m
-> StrictTVar m (AnchoredFragment (Header blk), FragmentCompleteness)
-> ChainSyncClientPipelined (Header blk) (Point blk) (Tip blk) m ChainSyncClientResult
-- TODO: we should consider either bundling these context parameters
-- into a record, or extending the protocol handler representation
-- to support bracket-style initialisation so that we could have the
-- closure include these and not need to be explicit about them here.
, hChainSyncServer
:: NodeToNodeVersion
-> ChainDB.Follower m blk (ChainDB.WithPoint blk (SerialisedHeader blk))
-> ChainSyncServer (SerialisedHeader blk) (Point blk) (Tip blk) m ()
-- TODO block fetch client does not have GADT view of the handlers.
, hBlockFetchClient
:: NodeToNodeVersion
-> ControlMessageSTM m
-> BlockFetchClient (Header blk) blk m ()
, hBlockFetchServer
:: NodeToNodeVersion
-> ResourceRegistry m
-> BlockFetchServer (Serialised blk) (Point blk) m ()
, hTxSubmissionClient
:: NodeToNodeVersion
-> ControlMessageSTM m
-> peer
-> TxSubmissionClient (GenTxId blk) (GenTx blk) m ()
, hTxSubmissionServer
:: NodeToNodeVersion
-> peer
-> TxSubmissionServerPipelined (GenTxId blk) (GenTx blk) m ()
, hKeepAliveClient
:: NodeToNodeVersion
-> ControlMessageSTM m
-> peer
-> StrictTVar m (Map peer PeerGSV)
-> KeepAliveInterval
-> KeepAliveClient m ()
, hKeepAliveServer
:: NodeToNodeVersion
-> peer
-> KeepAliveServer m ()
}
mkHandlers
:: forall m blk remotePeer localPeer.
( IOLike m
, MonadTime m
, MonadTimer m
, LedgerSupportsMempool blk
, HasTxId (GenTx blk)
, LedgerSupportsProtocol blk
, Ord remotePeer
)
=> NodeKernelArgs m remotePeer localPeer blk
-> NodeKernel m remotePeer localPeer blk
-> Handlers m remotePeer blk
mkHandlers
NodeKernelArgs {keepAliveRng, miniProtocolParameters}
NodeKernel {getChainDB, getMempool, getTopLevelConfig, getTracers = tracers} =
Handlers {
hChainSyncClient = \peer ->
chainSyncClient
(pipelineDecisionLowHighMark
(chainSyncPipeliningLowMark miniProtocolParameters)
(chainSyncPipeliningHighMark miniProtocolParameters))
(contramap (TraceLabelPeer peer) (Node.chainSyncClientTracer tracers))
getTopLevelConfig
(defaultChainDbView getChainDB)
, hChainSyncServer = \_version ->
chainSyncHeadersServer
(Node.chainSyncServerHeaderTracer tracers)
getChainDB
, hBlockFetchClient =
blockFetchClient
, hBlockFetchServer = \version ->
blockFetchServer
(Node.blockFetchServerTracer tracers)
getChainDB
version
, hTxSubmissionClient = \version controlMessageSTM peer ->
txSubmissionOutbound
(contramap (TraceLabelPeer peer) (Node.txOutboundTracer tracers))
(txSubmissionMaxUnacked miniProtocolParameters)
(mapTxSubmissionMempoolReader txForgetValidated $ getMempoolReader getMempool)
version
controlMessageSTM
, hTxSubmissionServer = \version peer ->
txSubmissionInbound
(contramap (TraceLabelPeer peer) (Node.txInboundTracer tracers))
(txSubmissionMaxUnacked miniProtocolParameters)
(mapTxSubmissionMempoolReader txForgetValidated $ getMempoolReader getMempool)
(getMempoolWriter getMempool)
version
, hKeepAliveClient = \_version -> keepAliveClient (Node.keepAliveClientTracer tracers) keepAliveRng
, hKeepAliveServer = \_version _peer -> keepAliveServer
}
{-------------------------------------------------------------------------------
Codecs
-------------------------------------------------------------------------------}
-- | Node-to-node protocol codecs needed to run 'Handlers'.
data Codecs blk e m bCS bSCS bBF bSBF bTX bTX2 bKA = Codecs {
cChainSyncCodec :: Codec (ChainSync (Header blk) (Point blk) (Tip blk)) e m bCS
, cChainSyncCodecSerialised :: Codec (ChainSync (SerialisedHeader blk) (Point blk) (Tip blk)) e m bSCS
, cBlockFetchCodec :: Codec (BlockFetch blk (Point blk)) e m bBF
, cBlockFetchCodecSerialised :: Codec (BlockFetch (Serialised blk) (Point blk)) e m bSBF
, cTxSubmissionCodec :: Codec (TxSubmission (GenTxId blk) (GenTx blk)) e m bTX
, cTxSubmission2Codec :: Codec (TxSubmission2 (GenTxId blk) (GenTx blk)) e m bTX2
, cKeepAliveCodec :: Codec KeepAlive e m bKA
}
-- | Protocol codecs for the node-to-node protocols
defaultCodecs :: forall m blk. (IOLike m, SerialiseNodeToNodeConstraints blk,
ShowProxy (GenTxId blk), ShowProxy (GenTx blk))
=> CodecConfig blk
-> BlockNodeToNodeVersion blk
-> NodeToNodeVersion
-> Codecs blk DeserialiseFailure m
ByteString ByteString ByteString ByteString ByteString ByteString ByteString
defaultCodecs ccfg version nodeToNodeVersion = Codecs {
cChainSyncCodec =
codecChainSync
enc
dec
(encodePoint (encodeRawHash p))
(decodePoint (decodeRawHash p))
(encodeTip (encodeRawHash p))
(decodeTip (decodeRawHash p))
, cChainSyncCodecSerialised =
codecChainSync
enc
dec
(encodePoint (encodeRawHash p))
(decodePoint (decodeRawHash p))
(encodeTip (encodeRawHash p))
(decodeTip (decodeRawHash p))
, cBlockFetchCodec =
codecBlockFetch
enc
dec
(encodePoint (encodeRawHash p))
(decodePoint (decodeRawHash p))
, cBlockFetchCodecSerialised =
codecBlockFetch
enc
dec
(encodePoint (encodeRawHash p))
(decodePoint (decodeRawHash p))
, cTxSubmissionCodec =
codecTxSubmission
enc
dec
enc
dec
, cTxSubmission2Codec =
codecTxSubmission2
enc
dec
enc
dec
, cKeepAliveCodec =
if nodeToNodeVersion <= NodeToNodeV_6
then codecKeepAlive
else codecKeepAlive_v2
}
where
p :: Proxy blk
p = Proxy
enc :: SerialiseNodeToNode blk a => a -> Encoding
enc = encodeNodeToNode ccfg version
dec :: SerialiseNodeToNode blk a => forall s. Decoder s a
dec = decodeNodeToNode ccfg version
-- | Identity codecs used in tests.
identityCodecs :: Monad m
=> Codecs blk CodecFailure m
(AnyMessage (ChainSync (Header blk) (Point blk) (Tip blk)))
(AnyMessage (ChainSync (SerialisedHeader blk) (Point blk) (Tip blk)))
(AnyMessage (BlockFetch blk (Point blk)))
(AnyMessage (BlockFetch (Serialised blk) (Point blk)))
(AnyMessage (TxSubmission (GenTxId blk) (GenTx blk)))
(AnyMessage (TxSubmission2 (GenTxId blk) (GenTx blk)))
(AnyMessage KeepAlive)
identityCodecs = Codecs {
cChainSyncCodec = codecChainSyncId
, cChainSyncCodecSerialised = codecChainSyncId
, cBlockFetchCodec = codecBlockFetchId
, cBlockFetchCodecSerialised = codecBlockFetchId
, cTxSubmissionCodec = codecTxSubmissionId
, cTxSubmission2Codec = codecTxSubmission2Id
, cKeepAliveCodec = codecKeepAliveId
}
{-------------------------------------------------------------------------------
Tracers
-------------------------------------------------------------------------------}
-- | A record of 'Tracer's for the different protocols.
type Tracers m peer blk e =
Tracers' peer blk e (Tracer m)
data Tracers' peer blk e f = Tracers {
tChainSyncTracer :: f (TraceLabelPeer peer (TraceSendRecv (ChainSync (Header blk) (Point blk) (Tip blk))))
, tChainSyncSerialisedTracer :: f (TraceLabelPeer peer (TraceSendRecv (ChainSync (SerialisedHeader blk) (Point blk) (Tip blk))))
, tBlockFetchTracer :: f (TraceLabelPeer peer (TraceSendRecv (BlockFetch blk (Point blk))))
, tBlockFetchSerialisedTracer :: f (TraceLabelPeer peer (TraceSendRecv (BlockFetch (Serialised blk) (Point blk))))
, tTxSubmissionTracer :: f (TraceLabelPeer peer (TraceSendRecv (TxSubmission (GenTxId blk) (GenTx blk))))
, tTxSubmission2Tracer :: f (TraceLabelPeer peer (TraceSendRecv (TxSubmission2 (GenTxId blk) (GenTx blk))))
}
instance (forall a. Semigroup (f a)) => Semigroup (Tracers' peer blk e f) where
l <> r = Tracers {
tChainSyncTracer = f tChainSyncTracer
, tChainSyncSerialisedTracer = f tChainSyncSerialisedTracer
, tBlockFetchTracer = f tBlockFetchTracer
, tBlockFetchSerialisedTracer = f tBlockFetchSerialisedTracer
, tTxSubmissionTracer = f tTxSubmissionTracer
, tTxSubmission2Tracer = f tTxSubmission2Tracer
}
where
f :: forall a. Semigroup a
=> (Tracers' peer blk e f -> a)
-> a
f prj = prj l <> prj r
-- | Use a 'nullTracer' for each protocol.
nullTracers :: Monad m => Tracers m peer blk e
nullTracers = Tracers {
tChainSyncTracer = nullTracer
, tChainSyncSerialisedTracer = nullTracer
, tBlockFetchTracer = nullTracer
, tBlockFetchSerialisedTracer = nullTracer
, tTxSubmissionTracer = nullTracer
, tTxSubmission2Tracer = nullTracer
}
showTracers :: ( Show blk
, Show peer
, Show (Header blk)
, Show (GenTx blk)
, Show (GenTxId blk)
, HasHeader blk
, HasNestedContent Header blk
)
=> Tracer m String -> Tracers m peer blk e
showTracers tr = Tracers {
tChainSyncTracer = showTracing tr
, tChainSyncSerialisedTracer = showTracing tr
, tBlockFetchTracer = showTracing tr
, tBlockFetchSerialisedTracer = showTracing tr
, tTxSubmissionTracer = showTracing tr
, tTxSubmission2Tracer = showTracing tr
}
{-------------------------------------------------------------------------------
Applications
-------------------------------------------------------------------------------}
-- | A node-to-node application
type ClientApp m peer bytes a =
NodeToNodeVersion
-> ControlMessageSTM m
-> peer
-> Channel m bytes
-> m (a, Maybe bytes)
type ServerApp m peer bytes a =
NodeToNodeVersion
-> peer
-> Channel m bytes
-> m (a, Maybe bytes)
-- | Applications for the node-to-node protocols
--
-- See 'Network.Mux.Types.MuxApplication'
data Apps m peer bCS bBF bTX bTX2 bKA a = Apps {
-- | Start a chain sync client that communicates with the given upstream
-- node.
aChainSyncClient :: ClientApp m peer bCS a
-- | Start a chain sync server.
, aChainSyncServer :: ServerApp m peer bCS a
-- | Start a block fetch client that communicates with the given
-- upstream node.
, aBlockFetchClient :: ClientApp m peer bBF a
-- | Start a block fetch server.
, aBlockFetchServer :: ServerApp m peer bBF a
-- | Start a transaction submission client that communicates with the
-- given upstream node.
, aTxSubmissionClient :: ClientApp m peer bTX a
-- | Start a transaction submission server.
, aTxSubmissionServer :: ServerApp m peer bTX a
-- | Start a transaction submission v2 client that communicates with the
-- given upstream node.
, aTxSubmission2Client :: ClientApp m peer bTX2 a
-- | Start a transaction submission v2 server.
, aTxSubmission2Server :: ServerApp m peer bTX2 a
-- | Start a keep-alive client.
, aKeepAliveClient :: ClientApp m peer bKA a
-- | Start a keep-alive server.
, aKeepAliveServer :: ServerApp m peer bKA a
}
-- | Construct the 'NetworkApplication' for the node-to-node protocols
mkApps
:: forall m remotePeer localPeer blk e bCS bBF bTX bTX2 bKA.
( IOLike m
, MonadTimer m
, Ord remotePeer
, Exception e
, LedgerSupportsProtocol blk
, ShowProxy blk
, ShowProxy (Header blk)
, ShowProxy (TxId (GenTx blk))
, ShowProxy (GenTx blk)
)
=> NodeKernel m remotePeer localPeer blk -- ^ Needed for bracketing only
-> Tracers m remotePeer blk e
-> (NodeToNodeVersion -> Codecs blk e m bCS bCS bBF bBF bTX bTX2 bKA)
-> m ChainSyncTimeout
-> Handlers m remotePeer blk
-> Apps m remotePeer bCS bBF bTX bTX2 bKA ()
mkApps kernel Tracers {..} mkCodecs genChainSyncTimeout Handlers {..} =
Apps {..}
where
aChainSyncClient
:: NodeToNodeVersion
-> ControlMessageSTM m
-> remotePeer
-> Channel m bCS
-> m ((), Maybe bCS)
aChainSyncClient version controlMessageSTM them channel = do
labelThisThread "ChainSyncClient"
-- Note that it is crucial that we sync with the fetch client "outside"
-- of registering the state for the sync client. This is needed to
-- maintain a state invariant required by the block fetch logic: that for
-- each candidate chain there is a corresponding block fetch client that
-- can be used to fetch blocks for that chain.
bracketSyncWithFetchClient
(getFetchClientRegistry kernel) them $
bracketChainSyncClient
(contramap (TraceLabelPeer them) (Node.chainSyncClientTracer (getTracers kernel)))
(defaultChainDbView (getChainDB kernel))
(getNodeCandidates kernel)
them $ \varCandidate -> do
chainSyncTimeout <- genChainSyncTimeout
(_, trailing) <-
runPipelinedPeerWithLimits
(contramap (TraceLabelPeer them) tChainSyncTracer)
(cChainSyncCodec (mkCodecs version))
(byteLimitsChainSync (const 0)) -- TODO: Real Bytelimits, see #1727
(timeLimitsChainSync chainSyncTimeout)
channel
$ chainSyncClientPeerPipelined
$ hChainSyncClient them version controlMessageSTM varCandidate
return ((), trailing)
aChainSyncServer
:: NodeToNodeVersion
-> remotePeer
-> Channel m bCS
-> m ((), Maybe bCS)
aChainSyncServer version them channel = do
labelThisThread "ChainSyncServer"
chainSyncTimeout <- genChainSyncTimeout
bracketWithPrivateRegistry
(chainSyncHeaderServerFollower (getChainDB kernel))
ChainDB.followerClose
$ \flr ->
runPeerWithLimits
(contramap (TraceLabelPeer them) tChainSyncSerialisedTracer)
(cChainSyncCodecSerialised (mkCodecs version))
(byteLimitsChainSync (const 0)) -- TODO: Real Bytelimits, see #1727
(timeLimitsChainSync chainSyncTimeout)
channel
$ chainSyncServerPeer
$ hChainSyncServer version flr
aBlockFetchClient
:: NodeToNodeVersion
-> ControlMessageSTM m
-> remotePeer
-> Channel m bBF
-> m ((), Maybe bBF)
aBlockFetchClient version controlMessageSTM them channel = do
labelThisThread "BlockFetchClient"
bracketFetchClient (getFetchClientRegistry kernel) them $ \clientCtx ->
runPipelinedPeerWithLimits
(contramap (TraceLabelPeer them) tBlockFetchTracer)
(cBlockFetchCodec (mkCodecs version))
(byteLimitsBlockFetch (const 0)) -- TODO: Real Bytelimits, see #1727
timeLimitsBlockFetch
channel
$ hBlockFetchClient version controlMessageSTM clientCtx
aBlockFetchServer
:: NodeToNodeVersion
-> remotePeer
-> Channel m bBF
-> m ((), Maybe bBF)
aBlockFetchServer version them channel = do
labelThisThread "BlockFetchServer"
withRegistry $ \registry ->
runPeerWithLimits
(contramap (TraceLabelPeer them) tBlockFetchSerialisedTracer)
(cBlockFetchCodecSerialised (mkCodecs version))
(byteLimitsBlockFetch (const 0)) -- TODO: Real Bytelimits, see #1727
timeLimitsBlockFetch
channel
$ blockFetchServerPeer
$ hBlockFetchServer version registry
aTxSubmissionClient
:: NodeToNodeVersion
-> ControlMessageSTM m
-> remotePeer
-> Channel m bTX
-> m ((), Maybe bTX)
aTxSubmissionClient version controlMessageSTM them channel = do
labelThisThread "TxSubmissionClient"
runPeerWithLimits
(contramap (TraceLabelPeer them) tTxSubmissionTracer)
(cTxSubmissionCodec (mkCodecs version))
(byteLimitsTxSubmission (const 0)) -- TODO: Real Bytelimits, see #1727
timeLimitsTxSubmission
channel
(txSubmissionClientPeer (hTxSubmissionClient version controlMessageSTM them))
aTxSubmissionServer
:: NodeToNodeVersion
-> remotePeer
-> Channel m bTX
-> m ((), Maybe bTX)
aTxSubmissionServer version them channel = do
labelThisThread "TxSubmissionServer"
runPipelinedPeerWithLimits
(contramap (TraceLabelPeer them) tTxSubmissionTracer)
(cTxSubmissionCodec (mkCodecs version))
(byteLimitsTxSubmission (const 0)) -- TODO: Real Bytelimits, see #1727
timeLimitsTxSubmission
channel
(txSubmissionServerPeerPipelined (hTxSubmissionServer version them))
aTxSubmission2Client
:: NodeToNodeVersion
-> ControlMessageSTM m
-> remotePeer
-> Channel m bTX2
-> m ((), Maybe bTX2)
aTxSubmission2Client version controlMessageSTM them channel = do
labelThisThread "TxSubmissionClient"
runPeerWithLimits
(contramap (TraceLabelPeer them) tTxSubmission2Tracer)
(cTxSubmission2Codec (mkCodecs version))
(byteLimitsTxSubmission2 (const 0)) -- TODO: Real Bytelimits, see #1727
timeLimitsTxSubmission2
channel
(Hello.wrapClientPeer (txSubmissionClientPeer (hTxSubmissionClient version controlMessageSTM them)))
aTxSubmission2Server
:: NodeToNodeVersion
-> remotePeer
-> Channel m bTX2
-> m ((), Maybe bTX2)
aTxSubmission2Server version them channel = do
labelThisThread "TxSubmissionServer"
runPipelinedPeerWithLimits
(contramap (TraceLabelPeer them) tTxSubmission2Tracer)
(cTxSubmission2Codec (mkCodecs version))
(byteLimitsTxSubmission2 (const 0)) -- TODO: Real Bytelimits, see #1727
timeLimitsTxSubmission2
channel
(Hello.wrapServerPeerPipelined (txSubmissionServerPeerPipelined (hTxSubmissionServer version them)))
aKeepAliveClient
:: NodeToNodeVersion
-> ControlMessageSTM m
-> remotePeer
-> Channel m bKA
-> m ((), Maybe bKA)
aKeepAliveClient version _controlMessageSTM them channel = do
labelThisThread "KeepAliveClient"
let kacApp = case version of
-- Version 1 doesn't support keep alive protocol but Blockfetch
-- still requires a PeerGSV per peer.
NodeToNodeV_1 -> \_ -> forever (threadDelay 1000) >> return ((), Nothing)
NodeToNodeV_2 -> \_ -> forever (threadDelay 1000) >> return ((), Nothing)
_ -> \dqCtx -> do
runPeerWithLimits
nullTracer
(cKeepAliveCodec (mkCodecs version))
(byteLimitsKeepAlive (const 0)) -- TODO: Real Bytelimits, see #1727
timeLimitsKeepAlive
channel
$ keepAliveClientPeer
$ hKeepAliveClient version (continueForever (Proxy :: Proxy m)) them dqCtx
(KeepAliveInterval 10)
bracketKeepAliveClient (getFetchClientRegistry kernel) them kacApp
aKeepAliveServer
:: NodeToNodeVersion
-> remotePeer
-> Channel m bKA
-> m ((), Maybe bKA)
aKeepAliveServer version _them channel = do
labelThisThread "KeepAliveServer"
runPeerWithLimits
nullTracer
(cKeepAliveCodec (mkCodecs version))
(byteLimitsKeepAlive (const 0)) -- TODO: Real Bytelimits, see #1727
timeLimitsKeepAlive
channel
$ keepAliveServerPeer
$ keepAliveServer
{-------------------------------------------------------------------------------
Projections from 'Apps'
-------------------------------------------------------------------------------}
-- | A projection from 'NetworkApplication' to a client-side
-- 'OuroborosApplication' for the node-to-node protocols.
--
-- Implementation note: network currently doesn't enable protocols conditional
-- on the protocol version, but it eventually may; this is why @_version@ is
-- currently unused.
initiator
:: MiniProtocolParameters
-> NodeToNodeVersion
-> Apps m (ConnectionId peer) b b b b b a
-> OuroborosApplication 'InitiatorMode peer b m a Void
initiator miniProtocolParameters version Apps {..} =
nodeToNodeProtocols
miniProtocolParameters
-- TODO: currently consensus is using 'ConnectionId' for its 'peer' type.
-- This is currently ok, as we might accept multiple connections from the
-- same ip address, however this will change when we will switch to
-- p2p-governor & connection-manager. Then consenus can use peer's ip
-- address & port number, rather than 'ConnectionId' (which is
-- a quadruple uniquely determinaing a connection).
(\them controlMessageSTM -> NodeToNodeProtocols {
chainSyncProtocol =
(InitiatorProtocolOnly (MuxPeerRaw (aChainSyncClient version controlMessageSTM them))),
blockFetchProtocol =
(InitiatorProtocolOnly (MuxPeerRaw (aBlockFetchClient version controlMessageSTM them))),
txSubmissionProtocol =
if version >= NodeToNodeV_6
then InitiatorProtocolOnly (MuxPeerRaw (aTxSubmission2Client version controlMessageSTM them))
else InitiatorProtocolOnly (MuxPeerRaw (aTxSubmissionClient version controlMessageSTM them)),
keepAliveProtocol =
(InitiatorProtocolOnly (MuxPeerRaw (aKeepAliveClient version controlMessageSTM them)))
})
version
-- | A projection from 'NetworkApplication' to a server-side
-- 'OuroborosApplication' for the node-to-node protocols.
--
-- See 'initiatorNetworkApplication' for rationale for the @_version@ arg.
responder
:: MiniProtocolParameters
-> NodeToNodeVersion
-> Apps m (ConnectionId peer) b b b b b a
-> OuroborosApplication 'ResponderMode peer b m Void a
responder miniProtocolParameters version Apps {..} =
nodeToNodeProtocols
miniProtocolParameters
(\them _controlMessageSTM -> NodeToNodeProtocols {
chainSyncProtocol =
(ResponderProtocolOnly (MuxPeerRaw (aChainSyncServer version them))),
blockFetchProtocol =
(ResponderProtocolOnly (MuxPeerRaw (aBlockFetchServer version them))),
txSubmissionProtocol =
if version >= NodeToNodeV_6
then ResponderProtocolOnly (MuxPeerRaw (aTxSubmission2Server version them))
else ResponderProtocolOnly (MuxPeerRaw (aTxSubmissionServer version them)),
keepAliveProtocol =
(ResponderProtocolOnly (MuxPeerRaw (aKeepAliveServer version them)))
})
version