Skip to content

Commit 6dc45a9

Browse files
committed
outbound-governor: represent unknown values explicitly
The `PeerSharing` & `PeerAdvertise` flags in `KnownPeerInfo` might be unknown. It's much better to represent unknown values using a `Maybe` type rather than using default values. We use a `StrictMaybe`.
1 parent aad59d9 commit 6dc45a9

File tree

1 file changed

+13
-10
lines changed
  • ouroboros-network/src/Ouroboros/Network/PeerSelection/State

1 file changed

+13
-10
lines changed

ouroboros-network/src/Ouroboros/Network/PeerSelection/State/KnownPeers.hs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ module Ouroboros.Network.PeerSelection.State.KnownPeers
4343
import Data.List qualified as List
4444
import Data.Map.Strict (Map)
4545
import Data.Map.Strict qualified as Map
46+
import Data.Maybe.Strict
4647
import Data.OrdPSQ (OrdPSQ)
4748
import Data.OrdPSQ qualified as PSQ
4849
import Data.Set (Set)
4950
import Data.Set qualified as Set
5051

52+
import Control.Applicative ((<|>))
5153
import Control.Exception (assert)
5254
import Control.Monad.Class.MonadTime.SI
5355

54-
import Data.Maybe (fromMaybe)
5556
import Ouroboros.Network.PeerSelection.PeerAdvertise (PeerAdvertise (..))
5657
import Ouroboros.Network.PeerSelection.PeerSharing (PeerSharing (..))
5758

@@ -114,7 +115,7 @@ data KnownPeerInfo = KnownPeerInfo {
114115
--
115116
-- It is used by the Peer Sharing logic to decide if we should share/ask
116117
-- about/to this peer's address to others.
117-
knownPeerSharing :: !PeerSharing,
118+
knownPeerSharing :: !(StrictMaybe PeerSharing),
118119

119120
-- | Indicates current local Peer Willingness information.
120121
--
@@ -123,7 +124,7 @@ data KnownPeerInfo = KnownPeerInfo {
123124
--
124125
-- It is used by the Peer Sharing logic to decide if we should share
125126
-- about this peer's address to others.
126-
knownPeerAdvertise :: !PeerAdvertise,
127+
knownPeerAdvertise :: !(StrictMaybe PeerAdvertise),
127128

128129
-- | Indicates if the node managed to connect to the peer at some point
129130
-- in time.
@@ -165,14 +166,16 @@ alterKnownPeerInfo (peerSharing, peerAdvertise) peerLookupResult =
165166
KnownPeerInfo {
166167
knownPeerFailCount = 0
167168
, knownPeerTepid = False
168-
, knownPeerSharing = fromMaybe PeerSharingDisabled peerSharing
169-
, knownPeerAdvertise = fromMaybe DoNotAdvertisePeer peerAdvertise
169+
, knownPeerSharing = maybeToStrictMaybe peerSharing
170+
, knownPeerAdvertise = maybeToStrictMaybe peerAdvertise
170171
, knownSuccessfulConnection = False
171172
}
172173
Just kpi -> Just $
173174
kpi {
174-
knownPeerSharing = fromMaybe (knownPeerSharing kpi) peerSharing
175-
, knownPeerAdvertise = fromMaybe (knownPeerAdvertise kpi) peerAdvertise
175+
knownPeerSharing = maybeToStrictMaybe peerSharing
176+
<|> knownPeerSharing kpi
177+
, knownPeerAdvertise = maybeToStrictMaybe peerAdvertise
178+
<|> knownPeerAdvertise kpi
176179
}
177180

178181
-------------------------------
@@ -437,7 +440,7 @@ canPeerShareRequest :: Ord peeraddr => peeraddr -> KnownPeers peeraddr -> Bool
437440
canPeerShareRequest pa KnownPeers { allPeers } =
438441
case Map.lookup pa allPeers of
439442
Just KnownPeerInfo
440-
{ knownPeerSharing = PeerSharingEnabled
443+
{ knownPeerSharing = SJust PeerSharingEnabled
441444
} -> True
442445
_ -> False
443446

@@ -448,7 +451,7 @@ canSharePeers :: Ord peeraddr => peeraddr -> KnownPeers peeraddr -> Bool
448451
canSharePeers pa KnownPeers { allPeers } =
449452
case Map.lookup pa allPeers of
450453
Just KnownPeerInfo
451-
{ knownPeerAdvertise = DoAdvertisePeer
454+
{ knownPeerAdvertise = SJust DoAdvertisePeer
452455
, knownSuccessfulConnection = True
453456
, knownPeerFailCount = 0
454457
} -> True
@@ -473,7 +476,7 @@ getPeerSharingResponsePeers knownPeers =
473476
Map.keysSet
474477
$ Map.filter (\case
475478
KnownPeerInfo
476-
{ knownPeerAdvertise = DoAdvertisePeer
479+
{ knownPeerAdvertise = SJust DoAdvertisePeer
477480
, knownSuccessfulConnection = True
478481
, knownPeerFailCount = 0
479482
} -> True

0 commit comments

Comments
 (0)