Skip to content

Commit cbab13f

Browse files
committed
testnet: use global ConnIdSupply
A global `ConnIdSupply` allows to to distinguish all connections based on their `ConnId`.
1 parent 615371c commit cbab13f

File tree

8 files changed

+117
-76
lines changed

8 files changed

+117
-76
lines changed

ouroboros-network-framework/demo/connection-manager.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import Network.TypedProtocol.ReqResp.Type (ReqResp)
5858

5959
import Ouroboros.Network.ConnectionHandler
6060
import Ouroboros.Network.ConnectionManager.Core qualified as CM
61+
import Ouroboros.Network.ConnectionManager.State qualified as CM
6162
import Ouroboros.Network.ConnectionManager.InformationChannel
6263
(newInformationChannel)
6364
import Ouroboros.Network.ConnectionManager.Types
@@ -187,6 +188,7 @@ withBidirectionalConnectionManager
187188
-> Mux.MakeBearer m socket
188189
-> socket
189190
-- ^ listening socket
191+
-> CM.ConnStateIdSupply m
190192
-> DiffTime -- protocol idle timeout
191193
-> DiffTime -- wait time timeout
192194
-> Maybe peerAddr
@@ -201,6 +203,7 @@ withBidirectionalConnectionManager
201203
-> m a)
202204
-> m a
203205
withBidirectionalConnectionManager snocket makeBearer socket
206+
connStateIdSupply
204207
protocolIdleTimeout
205208
timeWaitTimeout
206209
localAddress
@@ -244,7 +247,8 @@ withBidirectionalConnectionManager snocket makeBearer socket
244247
acceptedConnectionsSoftLimit = maxBound,
245248
acceptedConnectionsDelay = 0
246249
},
247-
CM.updateVersionData = \a _ -> a
250+
CM.updateVersionData = \a _ -> a,
251+
CM.connStateIdSupply
248252
}
249253
(makeConnectionHandler
250254
muxTracer
@@ -458,8 +462,9 @@ bidirectionalExperiment
458462
localAddr remoteAddr
459463
clientAndServerData = do
460464
stdGen <- Random.newStdGen
465+
connStateIdSupply <- atomically $ CM.newConnStateIdSupply (Proxy @IO)
461466
withBidirectionalConnectionManager
462-
snocket makeBearer socket0
467+
snocket makeBearer socket0 connStateIdSupply
463468
protocolIdleTimeout timeWaitTimeout
464469
(Just localAddr) stdGen clientAndServerData $
465470
\connectionManager _serverAddr -> forever' $ do

ouroboros-network-framework/sim-tests/Test/Ouroboros/Network/ConnectionManager.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Data.List (intercalate, sortOn)
4444
import Data.Map (Map)
4545
import Data.Map.Strict qualified as Map
4646
import Data.Monoid (All (..))
47+
import Data.Proxy (Proxy (..))
4748
import Data.Text.Lazy qualified as Text
4849
import Data.Void (Void)
4950
import Quiet
@@ -731,6 +732,7 @@ prop_valid_transitions (Fixed rnd) (SkewedBool bindToLocalAddress) scheduleMap =
731732
experiment = do
732733
labelThisThread "th-main"
733734
snocket <- mkSnocket scheduleMap
735+
connStateIdSupply <- atomically $ CM.newConnStateIdSupply Proxy
734736
let tracer :: Tracer (IOSim s) TestConnectionManagerTrace
735737
tracer = Tracer (say . show)
736738
{--
@@ -775,7 +777,8 @@ prop_valid_transitions (Fixed rnd) (SkewedBool bindToLocalAddress) scheduleMap =
775777
},
776778
CM.timeWaitTimeout = testTimeWaitTimeout,
777779
CM.outboundIdleTimeout = testOutboundIdleTimeout,
778-
CM.updateVersionData = \a _ -> a
780+
CM.updateVersionData = \a _ -> a,
781+
CM.connStateIdSupply
779782
}
780783
connectionHandler
781784
(\_ -> HandshakeFailure)

ouroboros-network-framework/sim-tests/Test/Ouroboros/Network/Server2/Sim.hs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import Data.Monoid (Sum (..))
5555
import Data.Monoid.Synchronisation (FirstToFinish (..))
5656
import Data.OrdPSQ (OrdPSQ)
5757
import Data.OrdPSQ qualified as OrdPSQ
58+
import Data.Proxy (Proxy (..))
5859
import Data.Set (Set)
5960
import Data.Set qualified as Set
6061
import Data.Typeable (Typeable)
@@ -654,64 +655,66 @@ multinodeExperiment inboundTrTracer trTracer inboundTracer debugTracer cmTracer
654655
(MultiNodeScript script _) =
655656
withJobPool $ \jobpool -> do
656657
stdGenVar <- newTVarIO stdGen0
657-
cc <- startServerConnectionHandler stdGenVar MainServer dataFlow0 [accInit] serverAddr jobpool
658-
loop stdGenVar (Map.singleton serverAddr [accInit]) (Map.singleton serverAddr cc) script jobpool
658+
connStateIdSupply <- atomically $ CM.newConnStateIdSupply (Proxy @m)
659+
cc <- startServerConnectionHandler stdGenVar connStateIdSupply MainServer dataFlow0 [accInit] serverAddr jobpool
660+
loop stdGenVar connStateIdSupply (Map.singleton serverAddr [accInit]) (Map.singleton serverAddr cc) script jobpool
659661
where
660662

661663
loop :: StrictTVar m StdGen
664+
-> CM.ConnStateIdSupply m
662665
-> Map.Map peerAddr acc
663666
-> Map.Map peerAddr (StrictTQueue m (ConnectionHandlerMessage peerAddr req))
664667
-> [ConnectionEvent req peerAddr]
665668
-> JobPool () m ()
666669
-> m ()
667-
loop _ _ _ [] _ = threadDelay 3600
668-
loop stdGenVar nodeAccs servers (event : events) jobpool =
670+
loop _ _ _ _ [] _ = threadDelay 3600
671+
loop stdGenVar connStateIdSupply nodeAccs servers (event : events) jobpool =
669672
case event of
670673

671674
StartClient delay localAddr -> do
672675
threadDelay delay
673-
cc <- startClientConnectionHandler stdGenVar (Client localAddr) localAddr jobpool
674-
loop stdGenVar nodeAccs (Map.insert localAddr cc servers) events jobpool
676+
cc <- startClientConnectionHandler stdGenVar connStateIdSupply (Client localAddr) localAddr jobpool
677+
loop stdGenVar connStateIdSupply nodeAccs (Map.insert localAddr cc servers) events jobpool
675678

676679
StartServer delay localAddr nodeAcc -> do
677680
threadDelay delay
678-
cc <- startServerConnectionHandler stdGenVar (Node localAddr) Duplex [nodeAcc] localAddr jobpool
679-
loop stdGenVar (Map.insert localAddr [nodeAcc] nodeAccs) (Map.insert localAddr cc servers) events jobpool
681+
cc <- startServerConnectionHandler stdGenVar connStateIdSupply (Node localAddr) Duplex [nodeAcc] localAddr jobpool
682+
loop stdGenVar connStateIdSupply (Map.insert localAddr [nodeAcc] nodeAccs) (Map.insert localAddr cc servers) events jobpool
680683

681684
InboundConnection delay nodeAddr -> do
682685
threadDelay delay
683686
sendMsg nodeAddr $ NewConnection serverAddr
684-
loop stdGenVar nodeAccs servers events jobpool
687+
loop stdGenVar connStateIdSupply nodeAccs servers events jobpool
685688

686689
OutboundConnection delay nodeAddr -> do
687690
threadDelay delay
688691
sendMsg serverAddr $ NewConnection nodeAddr
689-
loop stdGenVar nodeAccs servers events jobpool
692+
loop stdGenVar connStateIdSupply nodeAccs servers events jobpool
690693

691694
CloseInboundConnection delay remoteAddr -> do
692695
threadDelay delay
693696
sendMsg remoteAddr $ Disconnect serverAddr
694-
loop stdGenVar nodeAccs servers events jobpool
697+
loop stdGenVar connStateIdSupply nodeAccs servers events jobpool
695698

696699
CloseOutboundConnection delay remoteAddr -> do
697700
threadDelay delay
698701
sendMsg serverAddr $ Disconnect remoteAddr
699-
loop stdGenVar nodeAccs servers events jobpool
702+
loop stdGenVar connStateIdSupply nodeAccs servers events jobpool
700703

701704
InboundMiniprotocols delay nodeAddr reqs -> do
702705
threadDelay delay
703706
sendMsg nodeAddr $ RunMiniProtocols serverAddr reqs
704-
loop stdGenVar nodeAccs servers events jobpool
707+
loop stdGenVar connStateIdSupply nodeAccs servers events jobpool
705708

706709
OutboundMiniprotocols delay nodeAddr reqs -> do
707710
threadDelay delay
708711
sendMsg serverAddr $ RunMiniProtocols nodeAddr reqs
709-
loop stdGenVar nodeAccs servers events jobpool
712+
loop stdGenVar connStateIdSupply nodeAccs servers events jobpool
710713

711714
ShutdownClientServer delay nodeAddr -> do
712715
threadDelay delay
713716
sendMsg nodeAddr Shutdown
714-
loop stdGenVar nodeAccs servers events jobpool
717+
loop stdGenVar connStateIdSupply nodeAccs servers events jobpool
715718
where
716719
sendMsg :: peerAddr -> ConnectionHandlerMessage peerAddr req -> m ()
717720
sendMsg addr msg = atomically $
@@ -731,11 +734,12 @@ multinodeExperiment inboundTrTracer trTracer inboundTracer debugTracer cmTracer
731734
Just qs -> readTQueue (projectBundle tok qs)
732735

733736
startClientConnectionHandler :: StrictTVar m StdGen
737+
-> CM.ConnStateIdSupply m
734738
-> Name peerAddr
735739
-> peerAddr
736740
-> JobPool () m ()
737741
-> m (StrictTQueue m (ConnectionHandlerMessage peerAddr req))
738-
startClientConnectionHandler stdGenVar name localAddr jobpool = do
742+
startClientConnectionHandler stdGenVar connStateIdSupply name localAddr jobpool = do
739743
cc <- atomically newTQueue
740744
labelTQueueIO cc $ "cc/" ++ show name
741745
connVar <- newTVarIO Map.empty
@@ -746,7 +750,8 @@ multinodeExperiment inboundTrTracer trTracer inboundTracer debugTracer cmTracer
746750
$ Job
747751
( withInitiatorOnlyConnectionManager
748752
name simTimeouts nullTracer nullTracer stdGen
749-
snocket makeBearer (Just localAddr) (mkNextRequests connVar)
753+
snocket makeBearer connStateIdSupply
754+
(Just localAddr) (mkNextRequests connVar)
750755
timeLimitsHandshake acceptedConnLimit
751756
( \ connectionManager ->
752757
connectionLoop SingInitiatorMode localAddr cc connectionManager Map.empty connVar
@@ -758,13 +763,14 @@ multinodeExperiment inboundTrTracer trTracer inboundTracer debugTracer cmTracer
758763
return cc
759764

760765
startServerConnectionHandler :: StrictTVar m StdGen
766+
-> CM.ConnStateIdSupply m
761767
-> Name peerAddr
762768
-> DataFlow
763769
-> acc
764770
-> peerAddr
765771
-> JobPool () m ()
766772
-> m (StrictTQueue m (ConnectionHandlerMessage peerAddr req))
767-
startServerConnectionHandler stdGenVar name dataFlow serverAcc localAddr jobpool = do
773+
startServerConnectionHandler stdGenVar connStateIdSupply name dataFlow serverAcc localAddr jobpool = do
768774
fd <- Snocket.open snocket addrFamily
769775
Snocket.bind snocket fd localAddr
770776
Snocket.listen snocket fd
@@ -782,7 +788,8 @@ multinodeExperiment inboundTrTracer trTracer inboundTracer debugTracer cmTracer
782788
inboundTrTracer trTracer cmTracer
783789
inboundTracer debugTracer
784790
stdGen
785-
snocket makeBearer (\_ -> pure ()) fd (Just localAddr) serverAcc
791+
snocket makeBearer connStateIdSupply
792+
(\_ -> pure ()) fd (Just localAddr) serverAcc
786793
(mkNextRequests connVar)
787794
timeLimitsHandshake
788795
acceptedConnLimit
@@ -799,7 +806,8 @@ multinodeExperiment inboundTrTracer trTracer inboundTracer debugTracer cmTracer
799806
(show name)
800807
Unidirectional ->
801808
Job ( withInitiatorOnlyConnectionManager
802-
name simTimeouts trTracer cmTracer stdGen snocket makeBearer (Just localAddr)
809+
name simTimeouts trTracer cmTracer stdGen snocket makeBearer
810+
connStateIdSupply (Just localAddr)
803811
(mkNextRequests connVar)
804812
timeLimitsHandshake
805813
acceptedConnLimit
@@ -2182,13 +2190,15 @@ prop_server_accept_error (Fixed rnd) (AbsIOError ioerr) =
21822190
Snocket.bind snock socket0 addr
21832191
Snocket.listen snock socket0
21842192
nextRequests <- oneshotNextRequests pdata
2193+
connStateIdSupply <- atomically $ CM.newConnStateIdSupply Proxy
21852194
withBidirectionalConnectionManager "node-0" simTimeouts
21862195
nullTracer nullTracer
21872196
nullTracer nullTracer
21882197
nullTracer
21892198
(mkStdGen rnd)
21902199
snock
21912200
makeFDBearer
2201+
connStateIdSupply
21922202
(\_ -> pure ())
21932203
socket0 (Just addr)
21942204
[accumulatorInit pdata]

ouroboros-network-framework/src/Ouroboros/Network/ConnectionManager/Core.hs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ data Arguments handlerTrace socket peerAddr handle handleError versionNumber ver
149149

150150
connectionsLimits :: AcceptedConnectionsLimit,
151151

152-
updateVersionData :: versionData -> DiffusionMode -> versionData
152+
updateVersionData :: versionData -> DiffusionMode -> versionData,
153+
154+
-- | Supply for `ConnStateId`-s.
155+
--
156+
connStateIdSupply :: ConnStateIdSupply m
153157
}
154158

155159

@@ -397,17 +401,17 @@ with args@Arguments {
397401
connectionDataFlow,
398402
prunePolicy,
399403
connectionsLimits,
400-
updateVersionData
404+
updateVersionData,
405+
connStateIdSupply
401406
}
402407
ConnectionHandler {
403408
connectionHandler
404409
}
405410
classifyHandleError
406411
inboundGovernorInfoChannel
407412
k = do
408-
((connStateIdSupply, stateVar, stdGenVar)
409-
:: ( ConnStateIdSupply m
410-
, StrictTMVar m (ConnectionManagerState peerAddr handle handleError
413+
((stateVar, stdGenVar)
414+
:: ( StrictTMVar m (ConnectionManagerState peerAddr handle handleError
411415
version m)
412416
, StrictTVar m StdGen
413417
))
@@ -420,9 +424,8 @@ with args@Arguments {
420424
Just st -> Just <$> traverse (inspectTVar (Proxy :: Proxy m) . toLazyTVar . connVar) st
421425
return (TraceString (show st'))
422426

423-
connStateIdSupply <- State.newConnStateIdSupply (Proxy :: Proxy m)
424427
stdGenVar <- newTVar (stdGen args)
425-
return (connStateIdSupply, v, stdGenVar)
428+
return (v, stdGenVar)
426429

427430
let readState
428431
:: STM m (State.ConnMap peerAddr AbstractState)
@@ -459,8 +462,7 @@ with args@Arguments {
459462
WithInitiatorMode
460463
OutboundConnectionManager {
461464
ocmAcquireConnection =
462-
acquireOutboundConnectionImpl connStateIdSupply stateVar
463-
stdGenVar outboundHandler,
465+
acquireOutboundConnectionImpl stateVar stdGenVar outboundHandler,
464466
ocmReleaseConnection =
465467
releaseOutboundConnectionImpl stateVar stdGenVar
466468
},
@@ -474,8 +476,7 @@ with args@Arguments {
474476
WithResponderMode
475477
InboundConnectionManager {
476478
icmIncludeConnection =
477-
includeInboundConnectionImpl connStateIdSupply stateVar
478-
inboundHandler,
479+
includeInboundConnectionImpl stateVar inboundHandler,
479480
icmReleaseConnection =
480481
releaseInboundConnectionImpl stateVar,
481482
icmPromotedToWarmRemote =
@@ -495,15 +496,13 @@ with args@Arguments {
495496
WithInitiatorResponderMode
496497
OutboundConnectionManager {
497498
ocmAcquireConnection =
498-
acquireOutboundConnectionImpl connStateIdSupply stateVar
499-
stdGenVar outboundHandler,
499+
acquireOutboundConnectionImpl stateVar stdGenVar outboundHandler,
500500
ocmReleaseConnection =
501501
releaseOutboundConnectionImpl stateVar stdGenVar
502502
}
503503
InboundConnectionManager {
504504
icmIncludeConnection =
505-
includeInboundConnectionImpl connStateIdSupply stateVar
506-
inboundHandler,
505+
includeInboundConnectionImpl stateVar inboundHandler,
507506
icmReleaseConnection =
508507
releaseInboundConnectionImpl stateVar,
509508
icmPromotedToWarmRemote =
@@ -846,8 +845,7 @@ with args@Arguments {
846845

847846
includeInboundConnectionImpl
848847
:: HasCallStack
849-
=> ConnStateIdSupply m
850-
-> StrictTMVar m (ConnectionManagerState peerAddr handle handleError version m)
848+
=> StrictTMVar m (ConnectionManagerState peerAddr handle handleError version m)
851849
-> ConnectionHandlerFn handlerTrace socket peerAddr handle handleError version versionData m
852850
-> Word32
853851
-- ^ inbound connections hard limit
@@ -861,8 +859,7 @@ with args@Arguments {
861859
-> ConnectionId peerAddr
862860
-- ^ connection id used as an identifier of the resource
863861
-> m (Connected peerAddr handle handleError)
864-
includeInboundConnectionImpl connStateIdSupply
865-
stateVar
862+
includeInboundConnectionImpl stateVar
866863
handler
867864
hardLimit
868865
socket
@@ -1314,14 +1311,13 @@ with args@Arguments {
13141311

13151312
acquireOutboundConnectionImpl
13161313
:: HasCallStack
1317-
=> ConnStateIdSupply m
1318-
-> StrictTMVar m (ConnectionManagerState peerAddr handle handleError version m)
1314+
=> StrictTMVar m (ConnectionManagerState peerAddr handle handleError version m)
13191315
-> StrictTVar m StdGen
13201316
-> ConnectionHandlerFn handlerTrace socket peerAddr handle handleError version versionData m
13211317
-> DiffusionMode
13221318
-> peerAddr
13231319
-> m (Connected peerAddr handle handleError)
1324-
acquireOutboundConnectionImpl connStateIdSupply stateVar stdGenVar handler diffusionMode peerAddr = do
1320+
acquireOutboundConnectionImpl stateVar stdGenVar handler diffusionMode peerAddr = do
13251321
let provenance = Outbound
13261322
traceWith tracer (TrIncludeConnection provenance peerAddr)
13271323
(trace, mutableConnState@MutableConnState { connVar, connStateId }

0 commit comments

Comments
 (0)