Skip to content

Commit

Permalink
Added diffusionMode to local root peers group configuration
Browse files Browse the repository at this point in the history
"diffusionMode" can be either `"InitiatorOnly"` or
`"InitiatorAndResponder"`.  If not given, the latter is the default
- for backward compatibility.  When "InitiatorOnly" is used, the
connections to these local roots will only negotiate `InitiatorOnly`
mode (it won't be possible to be reused by the other side) and will not
bind to the server address, e.g. it will be made from an ephemeral port.
See IntersectMBO/ouroboros-network#5020 why
this feature was requested by some SPOs.

An example configuration:
```json
{ "localRoots":
    [ { "accessPoints":
          [ { "address": "10.0.0.1"
            , "port": 3001
            }
          ]
      , "advertise": false
      , "diffusionMode": "InitiatorOnly"
      , "warmValency": 1
      , "hotValency": 1
      }
    , { "accessPoints":
          [ { "address": "10.0.0.2"
            , "port": 3001
            }
          ]
      , "advertise": true
      , "diffusionMode": "InititiatorAndResponder"
      , "warmValency": 1
      , "hotValency": 1
      }
    ]
, "publicRoots": []
, "useLedgerAfterSlot": -1
}
```
  • Loading branch information
coot committed Dec 19, 2024
1 parent b22f1a0 commit af697b5
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 23 deletions.
7 changes: 7 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,10 @@ source-repository-package
location: https://github.com/neilmayhew/ekg-forward.git
tag: 4ba8bb693093f6cf54d43d6e9bbce1e08b0457dd
--sha256: sha256-g0gYqzRGjmZwxEzihJ4JifJe+GRfdLIMzaot7rUcjlI=

source-repository-package
type: git
location: https://github.com/input-output-hk/io-sim
tag: 586ddf6b52be1bbc78f9c659907f8e3fa13ccffe
--sha256: sha256-HaCWLHhseTQsTgShuh6tM52NpS558jcPgpQZ0YTBgj4=
subdir: io-sim
16 changes: 12 additions & 4 deletions cardano-node/src/Cardano/Node/Configuration/TopologyP2P.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Cardano.Node.Configuration.Topology (TopologyError (..))
import Cardano.Node.Types
import Cardano.Tracing.OrphanInstances.Network ()
import Ouroboros.Network.ConsensusMode
import Ouroboros.Network.NodeToNode (PeerAdvertise (..))
import Ouroboros.Network.NodeToNode (DiffusionMode (..), PeerAdvertise (..))
import Ouroboros.Network.PeerSelection.Bootstrap (UseBootstrapPeers (..))
import Ouroboros.Network.PeerSelection.LedgerPeers.Type (LedgerPeerSnapshot (..),
UseLedgerPeers (..))
Expand Down Expand Up @@ -109,7 +109,7 @@ instance ToJSON RootConfig where
rootConfigToRelayAccessPoint
:: RootConfig
-> [(RelayAccessPoint, PeerAdvertise)]
rootConfigToRelayAccessPoint RootConfig { rootAccessPoints, rootAdvertise } =
rootConfigToRelayAccessPoint RootConfig { rootAccessPoints, rootAdvertise } =
[ (ap, rootAdvertise) | ap <- rootAccessPoints ]


Expand All @@ -126,6 +126,8 @@ data LocalRootPeersGroup = LocalRootPeersGroup
, trustable :: PeerTrustable
-- ^ 'trustable' configures whether the root should be trusted in fallback
-- state.
, rootDiffusionMode :: DiffusionMode
-- ^ diffusion mode; used for local root peers.
} deriving (Eq, Show)

-- | Does not use the 'FromJSON' instance of 'RootConfig', so that
Expand All @@ -140,6 +142,9 @@ instance FromJSON LocalRootPeersGroup where
<*> pure hv
<*> o .:? "warmValency" .!= WarmValency v
<*> o .:? "trustable" .!= IsNotTrustable
-- deserialise via NodeDiffusionMode
<*> (maybe InitiatorAndResponderDiffusionMode getDiffusionMode
<$> o .:? "diffusionMode")

instance ToJSON LocalRootPeersGroup where
toJSON lrpg =
Expand All @@ -149,6 +154,8 @@ instance ToJSON LocalRootPeersGroup where
, "hotValency" .= hotValency lrpg
, "warmValency" .= warmValency lrpg
, "trustable" .= trustable lrpg
-- serialise via NodeDiffusionMode
, "diffusionMode" .= NodeDiffusionMode (rootDiffusionMode lrpg)
]

newtype LocalRootPeersGroups = LocalRootPeersGroups
Expand Down Expand Up @@ -277,10 +284,11 @@ isValidTrustedPeerConfiguration (RealNodeTopology (LocalRootPeersGroups lprgs) _
UseBootstrapPeers (_:_) -> True
where
anyTrustable =
any (\(LocalRootPeersGroup lr _ _ pt) -> case pt of
any (\LocalRootPeersGroup {localRoots, trustable} ->
case trustable of
IsNotTrustable -> False
IsTrustable -> not
. null
. rootAccessPoints
$ lr
$ localRoots
) lprgs
34 changes: 24 additions & 10 deletions cardano-node/src/Cardano/Node/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ import Ouroboros.Network.PeerSelection.LedgerPeers.Type (LedgerPeerSna
import Ouroboros.Network.PeerSelection.PeerSharing (PeerSharing (..))
import Ouroboros.Network.PeerSelection.PeerTrustable (PeerTrustable)
import Ouroboros.Network.PeerSelection.RelayAccessPoint (RelayAccessPoint (..))
import Ouroboros.Network.PeerSelection.State.LocalRootPeers (HotValency, WarmValency)
import Ouroboros.Network.PeerSelection.State.LocalRootPeers (HotValency, LocalRootConfig (..), WarmValency)
import Ouroboros.Network.Protocol.ChainSync.Codec
import Ouroboros.Network.Subscription (DnsSubscriptionTarget (..),
IPSubscriptionTarget (..))
Expand Down Expand Up @@ -678,7 +678,7 @@ installP2PSigHUPHandler :: Tracer IO (StartupTrace blk)
-> Api.BlockType blk
-> NodeConfiguration
-> NodeKernel IO RemoteAddress (ConnectionId LocalAddress) blk
-> StrictTVar IO [(HotValency, WarmValency, Map RelayAccessPoint (PeerAdvertise, PeerTrustable))]
-> StrictTVar IO [(HotValency, WarmValency, Map RelayAccessPoint LocalRootConfig)]
-> StrictTVar IO (Map RelayAccessPoint PeerAdvertise)
-> StrictTVar IO UseLedgerPeers
-> StrictTVar IO UseBootstrapPeers
Expand Down Expand Up @@ -780,7 +780,7 @@ updateBlockForging startupTracer blockType nodeKernel nc = do

updateTopologyConfiguration :: Tracer IO (StartupTrace blk)
-> NodeConfiguration
-> StrictTVar IO [(HotValency, WarmValency, Map RelayAccessPoint (PeerAdvertise, PeerTrustable))]
-> StrictTVar IO [(HotValency, WarmValency, Map RelayAccessPoint LocalRootConfig)]
-> StrictTVar IO (Map RelayAccessPoint PeerAdvertise)
-> StrictTVar IO UseLedgerPeers
-> StrictTVar IO UseBootstrapPeers
Expand Down Expand Up @@ -882,7 +882,7 @@ checkVRFFilePermissions (File vrfPrivKey) = do

mkP2PArguments
:: NodeConfiguration
-> STM IO [(HotValency, WarmValency, Map RelayAccessPoint (PeerAdvertise, PeerTrustable))]
-> STM IO [(HotValency, WarmValency, Map RelayAccessPoint LocalRootConfig)]
-- ^ non-overlapping local root peers groups; the 'Int' denotes the
-- valency of its group.
-> STM IO (Map RelayAccessPoint PeerAdvertise)
Expand Down Expand Up @@ -980,18 +980,32 @@ producerAddressesNonP2P nt =

producerAddresses
:: NetworkTopology
-> ( [(HotValency, WarmValency, Map RelayAccessPoint (PeerAdvertise, PeerTrustable))]
, Map RelayAccessPoint PeerAdvertise)
-> ( [(HotValency, WarmValency, Map RelayAccessPoint LocalRootConfig)]
, Map RelayAccessPoint PeerAdvertise
)
-- ^ local roots & public roots
producerAddresses RealNodeTopology { ntLocalRootPeersGroups
, ntPublicRootPeers
} =
( map (\lrp -> ( hotValency lrp
, warmValency lrp
, Map.fromList $ map (fmap (, trustable lrp))
$ rootConfigToRelayAccessPoint
$ localRoots lrp
, Map.fromList
. map (\(addr, peerAdvertise) ->
( addr
, LocalRootConfig {
diffusionMode = rootDiffusionMode lrp,
peerAdvertise,
peerTrustable = trustable lrp
}
)
)
. rootConfigToRelayAccessPoint
$ localRoots lrp
)
)
(groups ntLocalRootPeersGroups)
, foldMap (Map.fromList . rootConfigToRelayAccessPoint . publicRoots) ntPublicRootPeers
, foldMap ( Map.fromList
. rootConfigToRelayAccessPoint
. publicRoots
) ntPublicRootPeers
)
5 changes: 2 additions & 3 deletions cardano-node/src/Cardano/Node/Startup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ import Ouroboros.Network.NodeToClient (LocalAddress (..), LocalSocket,
NodeToClientVersion)
import Ouroboros.Network.NodeToNode (DiffusionMode (..), NodeToNodeVersion, PeerAdvertise)
import Ouroboros.Network.PeerSelection.LedgerPeers.Type (UseLedgerPeers)
import Ouroboros.Network.PeerSelection.PeerTrustable (PeerTrustable)
import Ouroboros.Network.PeerSelection.RelayAccessPoint (RelayAccessPoint)
import Ouroboros.Network.PeerSelection.State.LocalRootPeers (HotValency, WarmValency)
import Ouroboros.Network.PeerSelection.State.LocalRootPeers (HotValency, LocalRootConfig, WarmValency)
import Ouroboros.Network.Subscription.Dns (DnsSubscriptionTarget (..))
import Ouroboros.Network.Subscription.Ip (IPSubscriptionTarget (..))

Expand Down Expand Up @@ -108,7 +107,7 @@ data StartupTrace blk =
-- | Log peer-to-peer network configuration, either on startup or when its
-- updated.
--
| NetworkConfig [(HotValency, WarmValency, Map RelayAccessPoint (PeerAdvertise, PeerTrustable))]
| NetworkConfig [(HotValency, WarmValency, Map RelayAccessPoint LocalRootConfig)]
(Map RelayAccessPoint PeerAdvertise)
UseLedgerPeers
(Maybe PeerSnapshotFile)
Expand Down
6 changes: 4 additions & 2 deletions cardano-node/src/Cardano/Node/Tracing/Tracers/P2P.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,15 +1212,17 @@ instance (Show addr, Show versionNumber, Show agreedOptions, LogFormatting addr,
, "remoteAddress" .= toJSON connId
, "provenance" .= String (pack . show $ prov)
]
forMachine _dtal (TrConnect (Just localAddress) remoteAddress) =
forMachine _dtal (TrConnect (Just localAddress) remoteAddress diffusionMode) =
mconcat
[ "kind" .= String "ConnectTo"
, "connectionId" .= toJSON ConnectionId { localAddress, remoteAddress }
, "diffusionMode" .= toJSON diffusionMode
]
forMachine dtal (TrConnect Nothing remoteAddress) =
forMachine dtal (TrConnect Nothing remoteAddress diffusionMode) =
mconcat
[ "kind" .= String "ConnectTo"
, "remoteAddress" .= forMachine dtal remoteAddress
, "diffusionMode" .= toJSON diffusionMode
]
forMachine _dtal (TrConnectError (Just localAddress) remoteAddress err) =
mconcat
Expand Down
11 changes: 9 additions & 2 deletions cardano-node/src/Cardano/Node/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,18 @@ instance FromJSON NodeConsensusMode where
_ -> fail "Parsing NodeConsensusMode failed: can be either 'Genesis' or 'Praos'"
parseJSON _ = fail "Parsing NodeConsensusMode failed"

-- | Newtype wrapper which provides 'FromJSON' instance for 'DiffusionMode'.
-- | Newtype wrapper which provides 'ToJSON' and 'FromJSON' instances for
-- 'DiffusionMode'.
--
newtype NodeDiffusionMode
= NodeDiffusionMode { getDiffusionMode :: DiffusionMode }
deriving newtype Show
deriving newtype (Eq, Show)

instance ToJSON NodeDiffusionMode where
toJSON (NodeDiffusionMode InitiatorOnlyDiffusionMode)
= String "InitiatorOnly"
toJSON (NodeDiffusionMode InitiatorAndResponderDiffusionMode)
= String "InitiatorAndResponder"

instance FromJSON NodeDiffusionMode where
parseJSON (String str) =
Expand Down
27 changes: 25 additions & 2 deletions cardano-node/src/Cardano/Tracing/OrphanInstances/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Ouroboros.Network.ConnectionHandler (ConnectionHandlerTrace (..
import Ouroboros.Network.ConnectionId (ConnectionId (..))
import Ouroboros.Network.ConnectionManager.Core as ConnMgr (Trace (..))
import Ouroboros.Network.ConnectionManager.ConnMap (ConnMap (..), LocalAddr (..))
import Ouroboros.Network.ConnectionManager.State (ConnStateId (..))
import Ouroboros.Network.ConnectionManager.Types (AbstractState (..),
ConnectionManagerCounters (..),
OperationResult (..))
Expand Down Expand Up @@ -81,6 +82,7 @@ import Ouroboros.Network.PeerSelection.State.KnownPeers (KnownPeerInfo
import qualified Ouroboros.Network.PeerSelection.State.KnownPeers as KnownPeers
import Ouroboros.Network.PeerSelection.State.LocalRootPeers (HotValency (..),
LocalRootPeers, WarmValency (..))
import Ouroboros.Network.PeerSelection.State.LocalRootPeers (LocalRootConfig (..))
import qualified Ouroboros.Network.PeerSelection.State.LocalRootPeers as LocalRootPeers
import Ouroboros.Network.PeerSelection.Types (PeerStatus (..))
import Ouroboros.Network.Protocol.BlockFetch.Type (BlockFetch, Message (..))
Expand Down Expand Up @@ -1579,6 +1581,16 @@ instance FromJSON HotValency where
instance FromJSON WarmValency where
parseJSON v = WarmValency <$> parseJSON v

instance ToJSON LocalRootConfig where
toJSON LocalRootConfig { peerAdvertise,
peerTrustable,
diffusionMode } =
Aeson.object
[ "peerAdvertise" .= peerAdvertise
, "peerTrustable" .= peerTrustable
, "diffusionMode" .= show diffusionMode
]

instance Show exception => ToObject (TraceLocalRootPeers RemoteAddress exception) where
toObject _verb (TraceLocalRootDomains groups) =
mconcat [ "kind" .= String "LocalRootDomains"
Expand Down Expand Up @@ -2334,6 +2346,15 @@ instance ToJSON addr => ToJSON (LocalAddr addr) where
toJSON (LocalAddr addr) = toJSON addr
toJSON UnknownLocalAddr = Null

instance ToJSON NtN.DiffusionMode where
toJSON = String . pack . show

instance ToJSON ConnStateId where
toJSON (ConnStateId connStateId) = toJSON connStateId

instance ToObject ConnStateId where
toObject _ connStateId = mconcat [ "connStateId" .= toJSON connStateId ]

instance (Show addr, Show versionNumber, Show agreedOptions, ToObject addr,
ToJSON addr, ToJSON versionNumber, ToJSON agreedOptions)
=> ToObject (ConnMgr.Trace addr (ConnectionHandlerTrace versionNumber agreedOptions)) where
Expand All @@ -2351,15 +2372,17 @@ instance (Show addr, Show versionNumber, Show agreedOptions, ToObject addr,
, "remoteAddress" .= toJSON connId
, "provenance" .= String (pack . show $ prov)
]
TrConnect (Just localAddress) remoteAddress ->
TrConnect (Just localAddress) remoteAddress diffusionMode ->
mconcat
[ "kind" .= String "ConnectTo"
, "connectionId" .= toJSON ConnectionId { localAddress, remoteAddress }
, "diffusionMode" .= toJSON diffusionMode
]
TrConnect Nothing remoteAddress ->
TrConnect Nothing remoteAddress diffusionMode ->
mconcat
[ "kind" .= String "ConnectTo"
, "remoteAddress" .= toObject verb remoteAddress
, "diffusionMode" .= toJSON diffusionMode
]
TrConnectError (Just localAddress) remoteAddress err ->
mconcat
Expand Down
1 change: 1 addition & 0 deletions cardano-node/src/Cardano/Tracing/Tracers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import Cardano.Slotting.Slot (EpochNo (..), SlotNo (..), WithOrigin (.
import Cardano.Tracing.Config
import Cardano.Tracing.HasIssuer (BlockIssuerVerificationKeyHash (..), HasIssuer (..))
import Cardano.Tracing.Metrics
import Cardano.Tracing.OrphanInstances.Network ()
import Cardano.Tracing.Render (renderChainHash, renderHeaderHash)
import Cardano.Tracing.Shutdown ()
import Cardano.Tracing.Startup ()
Expand Down
2 changes: 2 additions & 0 deletions cardano-testnet/src/Testnet/Components/Configuration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis)
import Cardano.Ledger.Conway.Genesis (ConwayGenesis)
import qualified Cardano.Node.Configuration.Topology as NonP2P
import qualified Cardano.Node.Configuration.TopologyP2P as P2P
import Ouroboros.Network.NodeToNode (DiffusionMode (..))
import Ouroboros.Network.PeerSelection.Bootstrap
import Ouroboros.Network.PeerSelection.LedgerPeers
import Ouroboros.Network.PeerSelection.PeerTrustable
Expand Down Expand Up @@ -225,6 +226,7 @@ mkTopologyConfig numNodes allPorts port True = A.encodePretty topologyP2P
(HotValency (numNodes - 1))
(WarmValency (numNodes - 1))
IsNotTrustable
InitiatorAndResponderDiffusionMode
]

topologyP2P :: P2P.NetworkTopology
Expand Down

0 comments on commit af697b5

Please sign in to comment.