-
Notifications
You must be signed in to change notification settings - Fork 86
/
Common.hs
206 lines (178 loc) · 7.12 KB
/
Common.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
-- Common things between P2P and NonP2P Diffusion modules
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
module Ouroboros.Network.Diffusion.Common
( DiffusionTracer (..)
, Failure (..)
, Tracers (..)
, nullTracers
, Arguments (..)
, Applications (..)
) where
import Data.ByteString.Lazy (ByteString)
import Data.List.NonEmpty (NonEmpty)
import Data.Typeable (Typeable)
import Data.Void (Void)
import Control.Concurrent.Class.MonadSTM.Strict
import Control.Exception (Exception, SomeException)
import Control.Tracer (Tracer, nullTracer)
import Network.Mux (MuxMode (..), MuxTrace, WithMuxBearer)
import Ouroboros.Network.Mux (OuroborosApplicationWithMinimalCtx,
OuroborosBundleWithExpandedCtx)
import Ouroboros.Network.NodeToClient (Versions)
import Ouroboros.Network.NodeToClient qualified as NodeToClient
import Ouroboros.Network.NodeToNode (AcceptedConnectionsLimit, ConnectionId,
DiffusionMode)
import Ouroboros.Network.NodeToNode qualified as NodeToNode
import Ouroboros.Network.PeerSelection.Governor.Types (PublicPeerSelectionState)
import Ouroboros.Network.PeerSelection.LedgerPeers.Type
(LedgerPeersConsensusInterface)
import Ouroboros.Network.PeerSelection.LocalRootPeers (OutboundConnectionsState)
import Ouroboros.Network.Snocket (FileDescriptor)
import Ouroboros.Network.Socket (SystemdSocketTracer)
-- | The 'DiffusionTracer' logs
--
-- * diffusion initialisation messages
-- * terminal errors thrown by diffusion
--
data DiffusionTracer ntnAddr ntcAddr
= RunServer (NonEmpty ntnAddr)
| RunLocalServer ntcAddr
| UsingSystemdSocket ntcAddr
-- Rename as 'CreateLocalSocket'
| CreateSystemdSocketForSnocketPath ntcAddr
| CreatedLocalSocket ntcAddr
| ConfiguringLocalSocket ntcAddr FileDescriptor
| ListeningLocalSocket ntcAddr FileDescriptor
| LocalSocketUp ntcAddr FileDescriptor
-- Rename as 'CreateServerSocket'
| CreatingServerSocket ntnAddr
| ConfiguringServerSocket ntnAddr
| ListeningServerSocket ntnAddr
| ServerSocketUp ntnAddr
-- Rename as 'UnsupportedLocalSocketType'
| UnsupportedLocalSystemdSocket ntnAddr
-- Remove (this is impossible case), there's no systemd on Windows
| UnsupportedReadySocketCase
| DiffusionErrored SomeException
| SystemdSocketConfiguration SystemdSocketTracer
deriving Show
-- TODO: add a tracer for these misconfiguration
data Failure where
UnsupportedReadySocket :: Failure
UnexpectedIPv4Address :: forall ntnAddr. (Show ntnAddr, Typeable ntnAddr) => ntnAddr -> Failure
UnexpectedIPv6Address :: forall ntnAddr. (Show ntnAddr, Typeable ntnAddr) => ntnAddr -> Failure
NoSocket :: Failure
DiffusionError :: SomeException -> Failure
deriving instance Show Failure
instance Exception Failure
-- | Common DiffusionTracers interface between P2P and NonP2P
--
data Tracers ntnAddr ntnVersion ntcAddr ntcVersion m = Tracers {
-- | Mux tracer
dtMuxTracer
:: Tracer m (WithMuxBearer (ConnectionId ntnAddr) MuxTrace)
-- | Handshake protocol tracer
, dtHandshakeTracer
:: Tracer m (NodeToNode.HandshakeTr ntnAddr ntnVersion)
--
-- NodeToClient tracers
--
-- | Mux tracer for local clients
, dtLocalMuxTracer
:: Tracer m (WithMuxBearer (ConnectionId ntcAddr) MuxTrace)
-- | Handshake protocol tracer for local clients
, dtLocalHandshakeTracer
:: Tracer m (NodeToClient.HandshakeTr ntcAddr ntcVersion)
-- | Diffusion initialisation tracer
, dtDiffusionTracer
:: Tracer m (DiffusionTracer ntnAddr ntcAddr)
}
nullTracers :: Applicative m
=> Tracers ntnAddr ntnVersion
ntcAddr ntcVersion
m
nullTracers = Tracers {
dtMuxTracer = nullTracer
, dtHandshakeTracer = nullTracer
, dtLocalMuxTracer = nullTracer
, dtLocalHandshakeTracer = nullTracer
, dtDiffusionTracer = nullTracer
}
-- | Common DiffusionArguments interface between P2P and NonP2P
--
data Arguments m ntnFd ntnAddr ntcFd ntcAddr = Arguments {
-- | an @IPv4@ socket ready to accept connections or an @IPv4@ addresses
--
daIPv4Address :: Maybe (Either ntnFd ntnAddr)
-- | an @IPv6@ socket ready to accept connections or an @IPv6@ addresses
--
, daIPv6Address :: Maybe (Either ntnFd ntnAddr)
-- | an @AF_UNIX@ socket ready to accept connections or an @AF_UNIX@
-- socket path
, daLocalAddress :: Maybe (Either ntcFd ntcAddr)
-- | parameters for limiting number of accepted connections
--
, daAcceptedConnectionsLimit :: AcceptedConnectionsLimit
-- | run in initiator only mode
--
, daMode :: DiffusionMode
-- | public peer selection state
--
-- It is created outside of diffusion, since it is needed to create some
-- apps (e.g. peer sharing).
--
, daPublicPeerSelectionVar :: StrictTVar m (PublicPeerSelectionState ntnAddr)
}
-- | Versioned mini-protocol bundles run on a negotiated connection.
--
data Applications ntnAddr ntnVersion ntnVersionData
ntcAddr ntcVersion ntcVersionData
m a =
Applications {
-- | NodeToNode initiator applications for initiator only mode.
--
-- TODO: we should accept one or the other, but not both:
-- 'daApplicationInitiatorMode', 'daApplicationInitiatorResponderMode'.
--
-- Even in non-p2p mode we use p2p apps.
daApplicationInitiatorMode
:: Versions ntnVersion
ntnVersionData
(OuroborosBundleWithExpandedCtx
InitiatorMode ntnAddr
ByteString m a Void)
-- | NodeToNode initiator & responder applications for bidirectional mode.
--
, daApplicationInitiatorResponderMode
-- Peer Sharing result computation callback
:: Versions ntnVersion
ntnVersionData
(OuroborosBundleWithExpandedCtx
InitiatorResponderMode ntnAddr
ByteString m a ())
-- | NodeToClient responder application (server role)
--
-- Because p2p mode does not infect local connections we we use non-p2p
-- apps.
, daLocalResponderApplication
:: Versions ntcVersion
ntcVersionData
(OuroborosApplicationWithMinimalCtx
ResponderMode ntcAddr
ByteString m Void ())
-- | Interface used to get peers from the current ledger.
--
-- TODO: it should be in 'InterfaceExtra'
, daLedgerPeersCtx :: LedgerPeersConsensusInterface m
-- | Callback provided by consensus to inform it if the node is
-- connected to only local roots or also some external peers.
--
-- This is useful in order for the Bootstrap State Machine to
-- simply refuse to transition from TooOld to YoungEnough while
-- it only has local peers.
--
, daUpdateOutboundConnectionsState :: OutboundConnectionsState -> STM m ()
}