Skip to content

Commit

Permalink
Added P2PDecisionType and Refactored requestPublicRootPeers type
Browse files Browse the repository at this point in the history
  • Loading branch information
bolt12 committed Jan 7, 2025
1 parent ca290f4 commit 4d398a4
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,11 @@ import Ouroboros.Cardano.PeerSelection.PeerSelectionActions
import Ouroboros.Network.BlockFetch (FetchMode (..), PraosFetchMode (..),
TraceFetchClientState, TraceLabelPeer (..))
import Ouroboros.Network.Diffusion.Common qualified as Common
import Ouroboros.Network.PeerSelection.Governor.Types
(BootstrapPeersCriticalTimeoutError)
import Ouroboros.Network.PeerSelection.PeerAdvertise (PeerAdvertise (..))
import Ouroboros.Network.PeerSelection.PeerSharing (PeerSharing)
import Ouroboros.Network.PeerSelection.RelayAccessPoint (DomainAccessPoint (..),
PortNumber, RelayAccessPoint (..))
import Ouroboros.Network.PeerSelection.RootPeersDNS (PeerActionsDNS (..))
import Ouroboros.Network.PeerSelection.RootPeersDNS.DNSActions (DNSLookupType)
import Ouroboros.Network.PeerSelection.RootPeersDNS.DNSSemaphore
(newLedgerAndPublicRootDNSSemaphore)
import Ouroboros.Network.PeerSelection.RootPeersDNS.LocalRootPeers
(TraceLocalRootPeers)
import Ouroboros.Network.PeerSelection.RootPeersDNS.PublicRootPeers
Expand Down Expand Up @@ -1116,19 +1111,8 @@ diffusionSimulation
onlyOutboundConnectionsStateVar <- newTVarIO UntrustedState
useBootstrapPeersScriptVar <- newTVarIO bootstrapPeers
churnModeVar <- newTVarIO ChurnModeNormal
dnsSemaphore <- newLedgerAndPublicRootDNSSemaphore
dnsTimeoutScriptVar <- newTVarIO dnsTimeout
dnsLookupDelayScriptVar <- newTVarIO dnsLookupDelay

let dnsActions :: PeerActionsDNS (TestAddress NtNAddr_) () BootstrapPeersCriticalTimeoutError m
dnsActions =
PeerActionsDNS {
paToPeerAddr = (\a b -> TestAddress (IPAddr a b))
, paDnsActions = mockDNSActions dMapVar
dnsTimeoutScriptVar
dnsLookupDelayScriptVar
}
readUseBootstrapPeers = stepScriptSTM' useBootstrapPeersScriptVar

let readUseBootstrapPeers = stepScriptSTM' useBootstrapPeersScriptVar
(bgaRng, rng) = Random.split $ mkStdGen seed
acceptedConnectionsLimit =
AcceptedConnectionsLimit maxBound maxBound 0
Expand Down Expand Up @@ -1285,10 +1269,7 @@ diffusionSimulation
requestPublicRootPeers (Common.dtTracePublicRootPeersTracer tracersExtraAddr)
(caeReadUseBootstrapPeers cardanoExtraArgs)
(pure TooOld)
(\a b -> TestAddress (IPAddr a b))
dnsSemaphore
readPublicRootPeers
(paDnsActions dnsActions)

run blockGeneratorArgs
limitsAndTimeouts
Expand All @@ -1298,7 +1279,7 @@ diffusionSimulation
(cardanoExtraArgsToPeerSelectionActions cardanoExtraArgs)
CPSV.empty
CPRP.cardanoPublicRootPeersAPI
(cardanoPeerSelectionGovernorArgs readUseLedgerPeers peerSharing (iLedgerPeersConsensusInterface interfaces))
(cardanoPeerSelectionGovernorArgs readUseLedgerPeers peerSharing (clpciUpdateOutboundConnectionsState $ lpExtraAPI $ iLedgerPeersConsensusInterface interfaces))
CPSV.cardanoPeerSelectionStatetoCounters
requestPublicRootPeers'
peerChurnGovernor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ import Ouroboros.Network.PeerSelection.PeerStateActions (PeerConnectionHandle)
import Ouroboros.Network.PeerSelection.PublicRootPeers (PublicRootPeers)
import Ouroboros.Network.PeerSelection.RelayAccessPoint (DomainAccessPoint,
RelayAccessPoint)
import Ouroboros.Network.PeerSelection.RootPeersDNS (PeerActionsDNS)
import Ouroboros.Network.PeerSelection.RootPeersDNS.DNSActions (DNSLookupType)
import Ouroboros.Network.PeerSelection.RootPeersDNS.DNSSemaphore (DNSSemaphore)
import Ouroboros.Network.PeerSelection.State.LocalRootPeers (HotValency,
LocalRootConfig, WarmValency)
import Ouroboros.Network.PeerSelection.Types (PublicExtraPeersAPI (..))
Expand Down Expand Up @@ -170,7 +172,7 @@ type ResolverException = SomeException

run :: forall extraArgs extraState extraDebugState extraActions extraAPI
extraPeers extraFlags extraChurnArgs extraCounters
exception resolver m.
exception resolver resolverError m.
( Alternative (STM m)
, MonadAsync m
, MonadDelay m
Expand All @@ -193,6 +195,7 @@ run :: forall extraArgs extraState extraDebugState extraActions extraAPI
, Exception exception

, resolver ~ ()
, resolverError ~ ResolverException
, forall a. Semigroup a => Semigroup (m a)
)
=> Node.BlockGeneratorArgs Block StdGen
Expand Down Expand Up @@ -226,7 +229,9 @@ run :: forall extraArgs extraState extraDebugState extraActions extraAPI
(PeerConnectionHandle
muxMode responderCtx NtNAddr ntnVersionData bytes m a b)
-> extraCounters)
-> ( (NumberOfPeers -> LedgerPeersKind -> m (Maybe (Set NtNAddr, DiffTime)))
-> ( PeerActionsDNS NtNAddr resolver resolverError m
-> DNSSemaphore m
-> (NumberOfPeers -> LedgerPeersKind -> m (Maybe (Set NtNAddr, DiffTime)))
-> LedgerPeersKind
-> Int
-> m (PublicRootPeers extraPeers NtNAddr, DiffTime))
Expand Down Expand Up @@ -453,7 +458,7 @@ run blockGeneratorArgs limits ni na
extraArgs extraState extraDebugState extraActions
extraAPI extraPeers extraFlags
extraChurnArgs extraCounters exception
NtNAddr m
NtNAddr resolver resolverError m
argsExtra = Common.ArgumentsExtra
{ Common.daPeerSelectionTargets = aPeerTargets na
, Common.daReadLocalRootPeers = aReadLocalRootPeers na
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import Ouroboros.Network.PeerSelection.Governor.Types (AssociationMode (..),
BootstrapPeersCriticalTimeoutError (..), ExtraGuardedDecisions (..),
PeerSelectionGovernorArgs (..), PeerSelectionSetsWithSizes,
PeerSelectionState (..), PeerSelectionView (..))
import Ouroboros.Network.PeerSelection.LedgerPeers.Type
(LedgerPeersConsensusInterface (..), UseLedgerPeers)
import Ouroboros.Network.PeerSelection.LedgerPeers.Type (UseLedgerPeers)
import Ouroboros.Network.PeerSelection.PeerSharing (PeerSharing)
import Ouroboros.Network.PeerSelection.PublicRootPeers qualified as PublicRootPeers
import Ouroboros.Network.PeerSelection.State.EstablishedPeers qualified as EstablishedPeers
Expand Down Expand Up @@ -185,7 +184,7 @@ cardanoPeerSelectionGovernorArgs
)
=> STM m UseLedgerPeers
-> PeerSharing
-> LedgerPeersConsensusInterface (CardanoLedgerPeersConsensusInterface m) m
-> (OutboundConnectionsState -> STM m ())
-> PeerSelectionGovernorArgs
CardanoPeerSelectionState
extraDebugState
Expand All @@ -198,7 +197,7 @@ cardanoPeerSelectionGovernorArgs
peerconn
BootstrapPeersCriticalTimeoutError
m
cardanoPeerSelectionGovernorArgs readUseLedgerPeers peerSharing lpsci =
cardanoPeerSelectionGovernorArgs readUseLedgerPeers peerSharing updateOutboundConnectionsState =
PeerSelectionGovernorArgs {
-- If by any chance the node takes more than 15 minutes to converge to a
-- clean state, we crash the node. This could happen in very rare
Expand All @@ -215,7 +214,7 @@ cardanoPeerSelectionGovernorArgs readUseLedgerPeers peerSharing lpsci =
associationMode <- readAssociationMode readUseLedgerPeers
peerSharing
(cpstBootstrapPeersFlag (extraState st))
clpciUpdateOutboundConnectionsState (lpExtraAPI lpsci)
updateOutboundConnectionsState
(outboundConnectionsState associationMode psv st)
, extraDecisions =
ExtraGuardedDecisions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ import Data.Map qualified as Map
import Data.Set (Set)
import Data.Set qualified as Set
import Network.DNS qualified as DNS
import Network.Socket (PortNumber)
import Ouroboros.Cardano.Network.PublicRootPeers (CardanoPublicRootPeers (..))
import Ouroboros.Network.PeerSelection.LedgerPeers hiding (getLedgerPeers)
import Ouroboros.Network.PeerSelection.PeerAdvertise (PeerAdvertise (..))
import Ouroboros.Network.PeerSelection.PeerSelectionActions (getPublicRootPeers)
import Ouroboros.Network.PeerSelection.PublicRootPeers (PublicRootPeers)
import Ouroboros.Network.PeerSelection.PublicRootPeers qualified as PublicRootPeers
import Ouroboros.Network.PeerSelection.RootPeersDNS.DNSActions (DNSActions)
import Ouroboros.Network.PeerSelection.RootPeersDNS (PeerActionsDNS (..))
import Ouroboros.Network.PeerSelection.RootPeersDNS.DNSSemaphore (DNSSemaphore)
import Ouroboros.Network.PeerSelection.RootPeersDNS.PublicRootPeers

Expand All @@ -48,17 +47,20 @@ requestPublicRootPeers
=> Tracer m TracePublicRootPeers
-> STM m UseBootstrapPeers
-> STM m LedgerStateJudgement
-> (IP -> PortNumber -> peeraddr)
-> DNSSemaphore m
-> STM m (Map RelayAccessPoint PeerAdvertise)
-> DNSActions resolver exception m
-> PeerActionsDNS peeraddr resolver exception m
-> DNSSemaphore m
-> (NumberOfPeers -> LedgerPeersKind -> m (Maybe (Set peeraddr, DiffTime)))
-> LedgerPeersKind
-> Int
-> m (PublicRootPeers (CardanoPublicRootPeers peeraddr) peeraddr, DiffTime)
requestPublicRootPeers
publicTracer useBootstrapped getLedgerStateJudgement
toPeerAddr dnsSemaphore readPublicRootPeers dnsActions
publicTracer useBootstrapped
getLedgerStateJudgement readPublicRootPeers
PeerActionsDNS { paToPeerAddr = toPeerAddr
, paDnsActions = dnsActions
}
dnsSemaphore
getLedgerPeers ledgerPeersKind n = do
-- Check if the node is in a sensitive state
usingBootstrapPeers <- atomically
Expand Down
22 changes: 17 additions & 5 deletions ouroboros-network/src/Ouroboros/Network/Diffusion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
module Ouroboros.Network.Diffusion
( -- * Common API
P2P (..)
, P2PDecision (..)
, ExtraTracers (..)
, ArgumentsExtra (..)
, Applications (..)
Expand All @@ -18,6 +19,7 @@ module Ouroboros.Network.Diffusion
import Control.Concurrent.Class.MonadSTM.Strict (StrictTVar)
import Control.Exception (Exception, IOException)
import Data.Functor (void)
import Network.DNS (Resolver)
import Network.Socket (Socket)
import Ouroboros.Network.Diffusion.Common (Arguments,
NodeToNodeConnectionManager, NodeToNodePeerConnectionHandle, Tracers)
Expand All @@ -37,6 +39,16 @@ data P2P = P2P -- ^ General P2P mode. Can be instantiated with custom
-- data types
| NonP2P -- ^ Cardano non-P2P mode. Deprecated

-- | Auxiliary type to define arbitrary decision types based on type level
-- P2P
--
data P2PDecision (p2p :: P2P) a b where
P2PDecision :: a
-> P2PDecision 'P2P a b
NonP2PDecision :: b
-> P2PDecision 'NonP2P a b


-- | Tracers which depend on p2p mode.
--
data ExtraTracers (p2p :: P2P) extraState extraDebugState extraFlags extraPeers extraCounters m where
Expand All @@ -57,20 +69,20 @@ data ExtraTracers (p2p :: P2P) extraState extraDebugState extraFlags extraPeers
--
data ArgumentsExtra
(p2p :: P2P) extraArgs extraState extraDebugState extraActions extraAPI
extraPeers extraFlags extraChurnArgs extraCounters exception ntnAddr m where
extraPeers extraFlags extraChurnArgs extraCounters exception ntnAddr resolver resolverError m where
P2PArguments
:: Common.ArgumentsExtra extraArgs extraState extraDebugState extraActions extraAPI
extraPeers extraFlags extraChurnArgs
extraCounters exception ntnAddr m
extraCounters exception ntnAddr resolver resolverError m
-> ArgumentsExtra 'P2P extraArgs extraState extraDebugState extraActions extraAPI
extraPeers extraFlags extraChurnArgs
extraCounters exception ntnAddr m
extraCounters exception ntnAddr resolver resolverError m

NonP2PArguments
:: NonP2P.ArgumentsExtra
-> ArgumentsExtra 'NonP2P extraArgs extraState extraDebugState extraActions extraAPI
extraPeers extraFlags extraChurnArgs
extraCounters exception ntnAddr m
extraCounters exception ntnAddr resolver resolverError m

-- | Application data which depend on p2p mode.
--
Expand Down Expand Up @@ -133,7 +145,7 @@ run :: forall (p2p :: P2P) extraArgs extraState extraDebugState extraActions ext
LocalSocket LocalAddress
-> ArgumentsExtra p2p extraArgs extraState extraDebugState extraActions extraAPI
extraPeers extraFlags extraChurnArgs extraCounters exception
RemoteAddress IO
RemoteAddress Resolver IOException IO
-> Applications p2p RemoteAddress LocalAddress NodeToNodeVersionData NodeToClientVersionData extraAPI IO a
-> ApplicationsExtra p2p RemoteAddress IO a
-> IO ()
Expand Down
8 changes: 6 additions & 2 deletions ouroboros-network/src/Ouroboros/Network/Diffusion/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ import Ouroboros.Network.PeerSelection.PeerStateActions (PeerConnectionHandle,
PeerSelectionActionsTrace (..))
import Ouroboros.Network.PeerSelection.PublicRootPeers (PublicRootPeers)
import Ouroboros.Network.PeerSelection.RelayAccessPoint (RelayAccessPoint)
import Ouroboros.Network.PeerSelection.RootPeersDNS (PeerActionsDNS)
import Ouroboros.Network.PeerSelection.RootPeersDNS.DNSActions (DNSActions,
DNSLookupType (..))
import Ouroboros.Network.PeerSelection.RootPeersDNS.DNSSemaphore (DNSSemaphore)
import Ouroboros.Network.PeerSelection.RootPeersDNS.LocalRootPeers
(TraceLocalRootPeers)
import Ouroboros.Network.PeerSelection.RootPeersDNS.PublicRootPeers
Expand Down Expand Up @@ -359,7 +361,7 @@ nullTracersExtra =
--
data ArgumentsExtra extraArgs extraState extraDebugState extraActions extraAPI extraPeers
extraFlags extraChurnArgs extraCounters exception
peeraddr m = ArgumentsExtra {
peeraddr resolver resolverError m = ArgumentsExtra {
-- | selection targets for the peer governor
--
daPeerSelectionTargets :: PeerSelectionTargets
Expand Down Expand Up @@ -450,7 +452,9 @@ data ArgumentsExtra extraArgs extraState extraDebugState extraActions extraAPI e
-- 'Ouroboros.Network.PeerSelection.PeerSelectionActions.getPublicRootPeers'
--
, daRequestPublicRootPeers
:: ( (NumberOfPeers -> LedgerPeersKind -> m (Maybe (Set peeraddr, DiffTime)))
:: PeerActionsDNS peeraddr resolver resolverError m
-> DNSSemaphore m
-> ( (NumberOfPeers -> LedgerPeersKind -> m (Maybe (Set peeraddr, DiffTime)))
-> LedgerPeersKind
-> Int
-> m (PublicRootPeers extraPeers peeraddr, DiffTime))
Expand Down
7 changes: 5 additions & 2 deletions ouroboros-network/src/Ouroboros/Network/Diffusion/P2P.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import Ouroboros.Network.PeerSelection.State.LocalRootPeers qualified as LocalRo
import Ouroboros.Network.PeerSharing (PeerSharingRegistry (..))
import Ouroboros.Network.RethrowPolicy
import Ouroboros.Network.Server2 qualified as Server
import Network.DNS (Resolver)

runM
:: forall m ntnFd ntnAddr ntnVersion ntnVersionData
Expand Down Expand Up @@ -155,7 +156,7 @@ runM
-> -- | p2p configuration
ArgumentsExtra extraArgs extraState extraDebugState extraActions extraAPI
extraPeers extraFlags extraChurnArgs extraCounters
exception ntnAddr m
exception ntnAddr resolver resolverError m

-> -- | protocol handlers
Applications ntnAddr ntnVersion ntnVersionData
Expand Down Expand Up @@ -637,7 +638,7 @@ runM Interfaces
peerConnToPeerSharing = pchPeerSharing diNtnPeerSharing,
requestPeerShare =
getPeerShare (readTVar (getPeerSharingRegistry daPeerSharingRegistry)),
requestPublicRootPeers = daRequestPublicRootPeers getLedgerPeers,
requestPublicRootPeers = daRequestPublicRootPeers dnsActions dnsSemaphore getLedgerPeers,
readInboundPeers =
case daOwnPeerSharing of
PeerSharingDisabled -> pure Map.empty
Expand Down Expand Up @@ -856,6 +857,8 @@ run :: ( Monoid extraPeers
extraCounters
exception
RemoteAddress
Resolver
IOException
IO
-> Applications
RemoteAddress
Expand Down

0 comments on commit 4d398a4

Please sign in to comment.