Skip to content

Commit

Permalink
outbound-governor: configurable DiffusionMode for local root peers
Browse files Browse the repository at this point in the history
  • Loading branch information
coot committed Dec 5, 2024
1 parent 48ba3e3 commit cc72bb7
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 60 deletions.
5 changes: 3 additions & 2 deletions ouroboros-network-framework/demo/connection-manager.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import Ouroboros.Network.Context
import Ouroboros.Network.IOManager
import Ouroboros.Network.Mux
import Ouroboros.Network.MuxMode
import Ouroboros.Network.NodeToNode.Version (DiffusionMode (..))
import Ouroboros.Network.Protocol.Handshake
import Ouroboros.Network.Protocol.Handshake.Codec (timeLimitsHandshake)
import Ouroboros.Network.Protocol.Handshake.Unversioned
Expand Down Expand Up @@ -541,9 +542,9 @@ bidirectionalExperiment
Mux.InitiatorResponderMode
UnversionedProtocol))
connect n cm | n <= 1 =
acquireOutboundConnection cm remoteAddr
acquireOutboundConnection cm InitiatorAndResponderDiffusionMode remoteAddr
connect n cm =
acquireOutboundConnection cm remoteAddr
acquireOutboundConnection cm InitiatorAndResponderDiffusionMode remoteAddr
`catch` \(_ :: IOException) -> threadDelay 1
>> connect (pred n) cm
`catch` \(_ :: Mux.Error) -> threadDelay 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ test-suite sim-tests
monoidal-synchronisation,
network,
network-mux,
ouroboros-network-api,
ouroboros-network-framework,
ouroboros-network-framework:testlib,
ouroboros-network-testing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import Ouroboros.Network.ConnectionManager.Core qualified as CM
import Ouroboros.Network.ConnectionManager.Test.Utils (verifyAbstractTransition)
import Ouroboros.Network.ConnectionManager.Types
import Ouroboros.Network.MuxMode
import Ouroboros.Network.NodeToNode.Version (DiffusionMode (..))
import Ouroboros.Network.Server.RateLimiting
import Ouroboros.Network.Snocket (Accept (..), Accepted (..),
AddressFamily (TestFamily), Snocket (..), TestAddress (..))
Expand Down Expand Up @@ -811,7 +812,7 @@ prop_valid_transitions (Fixed rnd) (SkewedBool bindToLocalAddress) scheduleMap =
-- handshake negotiation.
timeout (1 + 5 + testTimeWaitTimeout)
(acquireOutboundConnection
connectionManager addr))
connectionManager InitiatorAndResponderDiffusionMode addr))
`catches`
[ Handler $ \(e :: IOException) -> return (Left (toException e))
, Handler $ \(e :: SomeConnectionManagerError) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import Ouroboros.Network.InboundGovernor.State (ConnectionState (..))
import Ouroboros.Network.InboundGovernor.State qualified as IG
import Ouroboros.Network.Mux
import Ouroboros.Network.MuxMode
import Ouroboros.Network.NodeToNode.Version (DiffusionMode (..))
import Ouroboros.Network.Protocol.Handshake.Codec (noTimeLimitsHandshake,
timeLimitsHandshake)
import Ouroboros.Network.Protocol.Handshake.Unversioned
Expand Down Expand Up @@ -869,7 +870,7 @@ multinodeExperiment inboundTrTracer trTracer inboundTracer debugTracer cmTracer
case fromException e of
Just SomeAsyncException {} -> Nothing
_ -> Just e)
$ acquireOutboundConnection cm remoteAddr
$ acquireOutboundConnection cm InitiatorAndResponderDiffusionMode remoteAddr
case connHandle of
Left _ ->
go connMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import Ouroboros.Network.ConnectionManager.State qualified as State
import Ouroboros.Network.ConnectionManager.Types
import Ouroboros.Network.InboundGovernor.Event (NewConnectionInfo (..))
import Ouroboros.Network.MuxMode
import Ouroboros.Network.NodeToNode.Version (DiffusionMode (..))
import Ouroboros.Network.Server.RateLimiting (AcceptedConnectionsLimit (..))
import Ouroboros.Network.Snocket

Expand Down Expand Up @@ -1311,9 +1312,10 @@ with args@Arguments {
-> StrictTMVar m (ConnectionManagerState peerAddr handle handleError version m)
-> StrictTVar m StdGen
-> ConnectionHandlerFn handlerTrace socket peerAddr handle handleError (version, versionData) m
-> DiffusionMode
-> peerAddr
-> m (Connected peerAddr handle handleError)
acquireOutboundConnectionImpl freshIdSupply stateVar stdGenVar handler peerAddr = do
acquireOutboundConnectionImpl freshIdSupply stateVar stdGenVar handler diffusionMode peerAddr = do
let provenance = Outbound
traceWith tracer (TrIncludeConnection provenance peerAddr)
(trace, mutableConnState@MutableConnState { connVar }
Expand Down Expand Up @@ -1510,14 +1512,20 @@ with args@Arguments {
Just IPv4Address -> ipv4Address
Just IPv6Address -> ipv6Address
configureSocket socket addr
-- only bind to the ip address if:
-- * the diffusion is given `ipv4/6` addresses;
-- * `diffusionMode` for this connection is
-- `InitiatorAndResponderMode`.
case addressType peerAddr of
Nothing -> pure ()
Just IPv4Address ->
Just IPv4Address | InitiatorAndResponderDiffusionMode
<- diffusionMode ->
traverse_ (bind snocket socket)
ipv4Address
Just IPv6Address ->
Just IPv6Address | InitiatorAndResponderDiffusionMode
<- diffusionMode ->
traverse_ (bind snocket socket)
ipv6Address
_ -> pure ()

traceWith tracer (TrConnect addr peerAddr)
connect snocket socket peerAddr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ import Network.Mux.Types qualified as Mux
import Ouroboros.Network.ConnectionId (ConnectionId (..))
import Ouroboros.Network.ConnectionManager.ConnMap (ConnMap)
import Ouroboros.Network.MuxMode
import Ouroboros.Network.NodeToNode.Version (DiffusionMode (..))


-- | Connection manager supports `IPv4` and `IPv6` addresses.
Expand Down Expand Up @@ -495,7 +496,7 @@ data Connected peerAddr handle handleError =


type AcquireOutboundConnection peerAddr handle handleError m
= peerAddr -> m (Connected peerAddr handle handleError)
= DiffusionMode -> peerAddr -> m (Connected peerAddr handle handleError)
type IncludeInboundConnection socket peerAddr handle handleError m
= Word32
-- ^ inbound connections hard limit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import Ouroboros.Network.Driver.Limits
import Ouroboros.Network.InboundGovernor qualified as InboundGovernor
import Ouroboros.Network.Mux
import Ouroboros.Network.MuxMode
import Ouroboros.Network.NodeToNode.Version (DiffusionMode (..))
import Ouroboros.Network.Protocol.Handshake
import Ouroboros.Network.Protocol.Handshake.Codec (cborTermVersionDataCodec,
noTimeLimitsHandshake, timeLimitsHandshake)
Expand Down Expand Up @@ -735,7 +736,7 @@ unidirectionalExperiment stdGen timeouts snocket makeBearer confSock socket clie
replicateM
(numberOfRounds clientAndServerData)
(bracket
(acquireOutboundConnection connectionManager serverAddr)
(acquireOutboundConnection connectionManager InitiatorOnlyDiffusionMode serverAddr)
(\case
Connected connId _ _ -> releaseOutboundConnection connectionManager connId
Disconnected {} -> error "unidirectionalExperiment: impossible happened")
Expand Down Expand Up @@ -836,6 +837,7 @@ bidirectionalExperiment
(withLock useLock lock
(acquireOutboundConnection
connectionManager0
InitiatorAndResponderDiffusionMode
localAddr1))
(\case
Connected connId _ _ ->
Expand All @@ -861,6 +863,7 @@ bidirectionalExperiment
(withLock useLock lock
(acquireOutboundConnection
connectionManager1
InitiatorAndResponderDiffusionMode
localAddr0))
(\case
Connected connId _ _ ->
Expand Down
1 change: 1 addition & 0 deletions ouroboros-network/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Addapted to `network-mux` changes in https://github.com/IntersectMBO/ouroboros-network/pull/4999
* Addapted to `network-mux` changes in https://github.com/IntersectMBO/ouroboros-network/pull/4997
* Use `LocalRootConfig` instead of a tuple.
* Extended `LocalRootConfig` with `diffusionMode :: DiffusionMode` field.

### Non-Breaking changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import Network.Socket (SockAddr)

import Ouroboros.Network.ConsensusMode
import Ouroboros.Network.ExitPolicy (RepromoteDelay (..))
import Ouroboros.Network.NodeToNode.Version (DiffusionMode (..))
import Ouroboros.Network.PeerSelection.Bootstrap (UseBootstrapPeers (..),
requiresBootstrapPeers)
import Ouroboros.Network.PeerSelection.Governor hiding (PeerSelectionState (..),
Expand Down Expand Up @@ -3875,8 +3876,8 @@ prop_issue_3550 = prop_governor_target_established_below defaultMaxTime $
(PeerAddr 29,[],GovernorScripts {peerShareScript = Script (Nothing :| []), peerSharingScript = Script (PeerSharingDisabled :| []), connectionScript = Script ((ToWarm,NoDelay) :| [(ToCold,NoDelay),(Noop,NoDelay)])})
],
localRootPeers = LocalRootPeers.fromGroups
[ (1, 1, Map.fromList [(PeerAddr 16, LocalRootConfig DoAdvertisePeer IsNotTrustable)])
, (1, 1, Map.fromList [(PeerAddr 4, LocalRootConfig DoAdvertisePeer IsNotTrustable)])
[ (1, 1, Map.fromList [(PeerAddr 16, LocalRootConfig DoAdvertisePeer IsNotTrustable InitiatorAndResponderDiffusionMode)])
, (1, 1, Map.fromList [(PeerAddr 4, LocalRootConfig DoAdvertisePeer IsNotTrustable InitiatorAndResponderDiffusionMode)])
],
publicRootPeers = PublicRootPeers.fromPublicRootPeers
(Map.fromList [ (PeerAddr 14, DoNotAdvertisePeer)
Expand Down Expand Up @@ -3923,7 +3924,7 @@ prop_issue_3515 = prop_governor_nolivelock $
peerSharingScript = Script (PeerSharingDisabled :| []),
connectionScript = Script ((ToCold,NoDelay) :| [(Noop,NoDelay)])
})],
localRootPeers = LocalRootPeers.fromGroups [(1,1,Map.fromList [(PeerAddr 10, LocalRootConfig DoAdvertisePeer IsNotTrustable)])],
localRootPeers = LocalRootPeers.fromGroups [(1,1,Map.fromList [(PeerAddr 10, LocalRootConfig DoAdvertisePeer IsNotTrustable InitiatorAndResponderDiffusionMode)])],
publicRootPeers = PublicRootPeers.empty,
targets = Script . NonEmpty.fromList $ targets'',
pickKnownPeersForPeerShare = Script (PickFirst :| []),
Expand Down Expand Up @@ -3964,7 +3965,7 @@ prop_issue_3494 = prop_governor_nofail $
peerSharingScript = Script (PeerSharingDisabled :| []),
connectionScript = Script ((ToCold,NoDelay) :| [(Noop,NoDelay)])
})],
localRootPeers = LocalRootPeers.fromGroups [(1,1,Map.fromList [(PeerAddr 64, LocalRootConfig DoAdvertisePeer IsNotTrustable)])],
localRootPeers = LocalRootPeers.fromGroups [(1,1,Map.fromList [(PeerAddr 64, LocalRootConfig DoAdvertisePeer IsNotTrustable InitiatorAndResponderDiffusionMode)])],
publicRootPeers = PublicRootPeers.empty,
targets = Script . NonEmpty.fromList $ targets'',
pickKnownPeersForPeerShare = Script (PickFirst :| []),
Expand Down Expand Up @@ -4013,8 +4014,8 @@ prop_issue_3233 = prop_governor_nolivelock $
(PeerAddr 15,[],GovernorScripts {peerShareScript = Script (Just ([],PeerShareTimeSlow) :| []), peerSharingScript = Script (PeerSharingDisabled :| []), connectionScript = Script ((Noop,NoDelay) :| [])})
],
localRootPeers = LocalRootPeers.fromGroups
[ (1, 1, Map.fromList [(PeerAddr 15, LocalRootConfig DoAdvertisePeer IsNotTrustable)])
, (1, 1, Map.fromList [(PeerAddr 13, LocalRootConfig DoAdvertisePeer IsNotTrustable)])
[ (1, 1, Map.fromList [(PeerAddr 15, LocalRootConfig DoAdvertisePeer IsNotTrustable InitiatorAndResponderDiffusionMode)])
, (1, 1, Map.fromList [(PeerAddr 13, LocalRootConfig DoAdvertisePeer IsNotTrustable InitiatorAndResponderDiffusionMode)])
],
publicRootPeers = PublicRootPeers.fromPublicRootPeers
(Map.fromList [(PeerAddr 4, DoNotAdvertisePeer)]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ module Test.Ouroboros.Network.PeerSelection.Instances
, prop_shrink_PeerSelectionTargets
) where

import Data.Hashable
import Data.IP qualified as IP
import Data.Text.Encoding (encodeUtf8)
import Data.Word (Word32, Word64)

import Cardano.Slotting.Slot (SlotNo (..))

import Ouroboros.Network.PeerSelection.Governor

import Data.Hashable
import Data.IP qualified as IP
import Ouroboros.Network.ConsensusMode
import Ouroboros.Network.NodeToNode.Version (DiffusionMode (..))
import Ouroboros.Network.PeerSelection.Bootstrap (UseBootstrapPeers (..))
import Ouroboros.Network.PeerSelection.Governor
import Ouroboros.Network.PeerSelection.LedgerPeers.Type (AfterSlot (..),
UseLedgerPeers (..))
import Ouroboros.Network.PeerSelection.PeerAdvertise (PeerAdvertise (..))
Expand Down Expand Up @@ -192,12 +192,18 @@ prop_shrink_PeerSelectionTargets x =


instance Arbitrary LocalRootConfig where
arbitrary = LocalRootConfig <$> arbitrary <*> arbitrary
shrink a@LocalRootConfig { peerAdvertise, peerTrustable } =
arbitrary = LocalRootConfig <$> arbitrary <*> arbitrary <*> elements [InitiatorAndResponderDiffusionMode, InitiatorOnlyDiffusionMode]
shrink a@LocalRootConfig { peerAdvertise, peerTrustable, diffusionMode } =
[ a { peerTrustable = peerTrustable' }
| peerTrustable' <- shrink peerTrustable
]
++
[ a { peerAdvertise = peerAdvertise' }
| peerAdvertise' <- shrink peerAdvertise
]
++
[ a { diffusionMode = diffusionMode' }
| diffusionMode' <- case diffusionMode of
InitiatorOnlyDiffusionMode -> []
InitiatorAndResponderDiffusionMode -> [InitiatorOnlyDiffusionMode]
]
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import Control.Monad.IOSim
import Control.Tracer (Tracer (..), contramap, traceWith)

import Ouroboros.Network.ExitPolicy
import Ouroboros.Network.NodeToNode.Version (DiffusionMode)
import Ouroboros.Network.PeerSelection.Governor hiding (PeerSelectionState (..))
import Ouroboros.Network.PeerSelection.Governor qualified as Governor
import Ouroboros.Network.PeerSelection.State.LocalRootPeers qualified as LocalRootPeers
Expand Down Expand Up @@ -515,8 +516,8 @@ mockPeerSelectionActions' tracer
traceWith tracer (TraceEnvPeerShareResult addr peeraddrs)
return (PeerSharingResult peeraddrs)

establishPeerConnection :: IsBigLedgerPeer -> PeerAddr -> m (PeerConn m)
establishPeerConnection _ peeraddr = do
establishPeerConnection :: IsBigLedgerPeer -> DiffusionMode -> PeerAddr -> m (PeerConn m)
establishPeerConnection _ _ peeraddr = do
--TODO: add support for variable delays and synchronous failure
traceWith tracer (TraceEnvEstablishConn peeraddr)
threadDelay 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import Control.Tracer (Tracer (Tracer), contramap, nullTracer, traceWith)
import Control.Concurrent.Class.MonadSTM qualified as LazySTM
import Data.List (intercalate)
import Data.List.NonEmpty (NonEmpty (..))
import Ouroboros.Network.NodeToNode.Version (DiffusionMode (..))
import Ouroboros.Network.PeerSelection.LedgerPeers
import Ouroboros.Network.PeerSelection.PeerAdvertise (PeerAdvertise (..))
import Ouroboros.Network.PeerSelection.PeerTrustable (PeerTrustable (..))
Expand Down Expand Up @@ -238,10 +239,10 @@ simpleMockRoots = MockRoots localRootPeers dnsMap Map.empty (singletonScript Map
[ ( 2, 2
, Map.fromList
[ ( RelayAccessAddress (read "192.0.2.1") (read "3333")
, LocalRootConfig DoAdvertisePeer IsNotTrustable
, LocalRootConfig DoAdvertisePeer IsNotTrustable InitiatorAndResponderDiffusionMode
)
, ( RelayAccessDomain "test.domain" (read "4444")
, LocalRootConfig DoNotAdvertisePeer IsNotTrustable
, LocalRootConfig DoNotAdvertisePeer IsNotTrustable InitiatorAndResponderDiffusionMode
)
]
)
Expand Down
Loading

0 comments on commit cc72bb7

Please sign in to comment.