{-# LANGUAGE DeriveTraversable   #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE NamedFieldPuns      #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Ouroboros.Network.ConnectionManager.ConnMap
  ( ConnMap (..)
  , LocalAddr (..)
  , toList
  , toMap
  , empty
  , insert
  , insertUnknownLocalAddr
  , delete
    -- , deleteUnknownLocalAddr
  , deleteAtRemoteAddr
  , lookup
  , lookupByRemoteAddr
  , updateLocalAddr
  , traverseMaybe
  ) where

import Prelude hiding (lookup)

import Data.Foldable qualified as Foldable
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import System.Random (RandomGen, uniformR)

import Ouroboros.Network.ConnectionId

data LocalAddr peerAddr =
    -- | A reserved slot for an outbound connection which is being created.  The
    -- outbound connection must be in the `ReservedOutbound` state.
    UnknownLocalAddr
    -- | All connections which are not in the `ReservedOutbound` state use
    -- `LocalAddr`, since for them the local address is known.
  | LocalAddr peerAddr
  deriving (Int -> LocalAddr peerAddr -> ShowS
[LocalAddr peerAddr] -> ShowS
LocalAddr peerAddr -> String
(Int -> LocalAddr peerAddr -> ShowS)
-> (LocalAddr peerAddr -> String)
-> ([LocalAddr peerAddr] -> ShowS)
-> Show (LocalAddr peerAddr)
forall peerAddr.
Show peerAddr =>
Int -> LocalAddr peerAddr -> ShowS
forall peerAddr. Show peerAddr => [LocalAddr peerAddr] -> ShowS
forall peerAddr. Show peerAddr => LocalAddr peerAddr -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall peerAddr.
Show peerAddr =>
Int -> LocalAddr peerAddr -> ShowS
showsPrec :: Int -> LocalAddr peerAddr -> ShowS
$cshow :: forall peerAddr. Show peerAddr => LocalAddr peerAddr -> String
show :: LocalAddr peerAddr -> String
$cshowList :: forall peerAddr. Show peerAddr => [LocalAddr peerAddr] -> ShowS
showList :: [LocalAddr peerAddr] -> ShowS
Show, LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
(LocalAddr peerAddr -> LocalAddr peerAddr -> Bool)
-> (LocalAddr peerAddr -> LocalAddr peerAddr -> Bool)
-> Eq (LocalAddr peerAddr)
forall peerAddr.
Eq peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall peerAddr.
Eq peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
== :: LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
$c/= :: forall peerAddr.
Eq peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
/= :: LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
Eq, Eq (LocalAddr peerAddr)
Eq (LocalAddr peerAddr) =>
(LocalAddr peerAddr -> LocalAddr peerAddr -> Ordering)
-> (LocalAddr peerAddr -> LocalAddr peerAddr -> Bool)
-> (LocalAddr peerAddr -> LocalAddr peerAddr -> Bool)
-> (LocalAddr peerAddr -> LocalAddr peerAddr -> Bool)
-> (LocalAddr peerAddr -> LocalAddr peerAddr -> Bool)
-> (LocalAddr peerAddr -> LocalAddr peerAddr -> LocalAddr peerAddr)
-> (LocalAddr peerAddr -> LocalAddr peerAddr -> LocalAddr peerAddr)
-> Ord (LocalAddr peerAddr)
LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
LocalAddr peerAddr -> LocalAddr peerAddr -> Ordering
LocalAddr peerAddr -> LocalAddr peerAddr -> LocalAddr peerAddr
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall peerAddr. Ord peerAddr => Eq (LocalAddr peerAddr)
forall peerAddr.
Ord peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
forall peerAddr.
Ord peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> Ordering
forall peerAddr.
Ord peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> LocalAddr peerAddr
$ccompare :: forall peerAddr.
Ord peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> Ordering
compare :: LocalAddr peerAddr -> LocalAddr peerAddr -> Ordering
$c< :: forall peerAddr.
Ord peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
< :: LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
$c<= :: forall peerAddr.
Ord peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
<= :: LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
$c> :: forall peerAddr.
Ord peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
> :: LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
$c>= :: forall peerAddr.
Ord peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
>= :: LocalAddr peerAddr -> LocalAddr peerAddr -> Bool
$cmax :: forall peerAddr.
Ord peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> LocalAddr peerAddr
max :: LocalAddr peerAddr -> LocalAddr peerAddr -> LocalAddr peerAddr
$cmin :: forall peerAddr.
Ord peerAddr =>
LocalAddr peerAddr -> LocalAddr peerAddr -> LocalAddr peerAddr
min :: LocalAddr peerAddr -> LocalAddr peerAddr -> LocalAddr peerAddr
Ord)


-- | The outer map keys are remote addresses, the internal ones are local
-- addresses.
--
newtype ConnMap peerAddr a
  = ConnMap {
      forall peerAddr a.
ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
getConnMap ::
        Map peerAddr
          (Map (LocalAddr peerAddr) a)
    }
  deriving (Int -> ConnMap peerAddr a -> ShowS
[ConnMap peerAddr a] -> ShowS
ConnMap peerAddr a -> String
(Int -> ConnMap peerAddr a -> ShowS)
-> (ConnMap peerAddr a -> String)
-> ([ConnMap peerAddr a] -> ShowS)
-> Show (ConnMap peerAddr a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall peerAddr a.
(Show peerAddr, Show a) =>
Int -> ConnMap peerAddr a -> ShowS
forall peerAddr a.
(Show peerAddr, Show a) =>
[ConnMap peerAddr a] -> ShowS
forall peerAddr a.
(Show peerAddr, Show a) =>
ConnMap peerAddr a -> String
$cshowsPrec :: forall peerAddr a.
(Show peerAddr, Show a) =>
Int -> ConnMap peerAddr a -> ShowS
showsPrec :: Int -> ConnMap peerAddr a -> ShowS
$cshow :: forall peerAddr a.
(Show peerAddr, Show a) =>
ConnMap peerAddr a -> String
show :: ConnMap peerAddr a -> String
$cshowList :: forall peerAddr a.
(Show peerAddr, Show a) =>
[ConnMap peerAddr a] -> ShowS
showList :: [ConnMap peerAddr a] -> ShowS
Show, (forall a b. (a -> b) -> ConnMap peerAddr a -> ConnMap peerAddr b)
-> (forall a b. a -> ConnMap peerAddr b -> ConnMap peerAddr a)
-> Functor (ConnMap peerAddr)
forall a b. a -> ConnMap peerAddr b -> ConnMap peerAddr a
forall a b. (a -> b) -> ConnMap peerAddr a -> ConnMap peerAddr b
forall peerAddr a b. a -> ConnMap peerAddr b -> ConnMap peerAddr a
forall peerAddr a b.
(a -> b) -> ConnMap peerAddr a -> ConnMap peerAddr b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall peerAddr a b.
(a -> b) -> ConnMap peerAddr a -> ConnMap peerAddr b
fmap :: forall a b. (a -> b) -> ConnMap peerAddr a -> ConnMap peerAddr b
$c<$ :: forall peerAddr a b. a -> ConnMap peerAddr b -> ConnMap peerAddr a
<$ :: forall a b. a -> ConnMap peerAddr b -> ConnMap peerAddr a
Functor, (forall m. Monoid m => ConnMap peerAddr m -> m)
-> (forall m a. Monoid m => (a -> m) -> ConnMap peerAddr a -> m)
-> (forall m a. Monoid m => (a -> m) -> ConnMap peerAddr a -> m)
-> (forall a b. (a -> b -> b) -> b -> ConnMap peerAddr a -> b)
-> (forall a b. (a -> b -> b) -> b -> ConnMap peerAddr a -> b)
-> (forall b a. (b -> a -> b) -> b -> ConnMap peerAddr a -> b)
-> (forall b a. (b -> a -> b) -> b -> ConnMap peerAddr a -> b)
-> (forall a. (a -> a -> a) -> ConnMap peerAddr a -> a)
-> (forall a. (a -> a -> a) -> ConnMap peerAddr a -> a)
-> (forall a. ConnMap peerAddr a -> [a])
-> (forall a. ConnMap peerAddr a -> Bool)
-> (forall a. ConnMap peerAddr a -> Int)
-> (forall a. Eq a => a -> ConnMap peerAddr a -> Bool)
-> (forall a. Ord a => ConnMap peerAddr a -> a)
-> (forall a. Ord a => ConnMap peerAddr a -> a)
-> (forall a. Num a => ConnMap peerAddr a -> a)
-> (forall a. Num a => ConnMap peerAddr a -> a)
-> Foldable (ConnMap peerAddr)
forall a. Eq a => a -> ConnMap peerAddr a -> Bool
forall a. Num a => ConnMap peerAddr a -> a
forall a. Ord a => ConnMap peerAddr a -> a
forall m. Monoid m => ConnMap peerAddr m -> m
forall a. ConnMap peerAddr a -> Bool
forall a. ConnMap peerAddr a -> Int
forall a. ConnMap peerAddr a -> [a]
forall a. (a -> a -> a) -> ConnMap peerAddr a -> a
forall peerAddr a. Eq a => a -> ConnMap peerAddr a -> Bool
forall peerAddr a. Num a => ConnMap peerAddr a -> a
forall peerAddr a. Ord a => ConnMap peerAddr a -> a
forall peerAddr m. Monoid m => ConnMap peerAddr m -> m
forall m a. Monoid m => (a -> m) -> ConnMap peerAddr a -> m
forall peerAddr a. ConnMap peerAddr a -> Bool
forall peerAddr a. ConnMap peerAddr a -> Int
forall peerAddr a. ConnMap peerAddr a -> [a]
forall b a. (b -> a -> b) -> b -> ConnMap peerAddr a -> b
forall a b. (a -> b -> b) -> b -> ConnMap peerAddr a -> b
forall peerAddr a. (a -> a -> a) -> ConnMap peerAddr a -> a
forall peerAddr m a.
Monoid m =>
(a -> m) -> ConnMap peerAddr a -> m
forall peerAddr b a. (b -> a -> b) -> b -> ConnMap peerAddr a -> b
forall peerAddr a b. (a -> b -> b) -> b -> ConnMap peerAddr a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall peerAddr m. Monoid m => ConnMap peerAddr m -> m
fold :: forall m. Monoid m => ConnMap peerAddr m -> m
$cfoldMap :: forall peerAddr m a.
Monoid m =>
(a -> m) -> ConnMap peerAddr a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> ConnMap peerAddr a -> m
$cfoldMap' :: forall peerAddr m a.
Monoid m =>
(a -> m) -> ConnMap peerAddr a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> ConnMap peerAddr a -> m
$cfoldr :: forall peerAddr a b. (a -> b -> b) -> b -> ConnMap peerAddr a -> b
foldr :: forall a b. (a -> b -> b) -> b -> ConnMap peerAddr a -> b
$cfoldr' :: forall peerAddr a b. (a -> b -> b) -> b -> ConnMap peerAddr a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> ConnMap peerAddr a -> b
$cfoldl :: forall peerAddr b a. (b -> a -> b) -> b -> ConnMap peerAddr a -> b
foldl :: forall b a. (b -> a -> b) -> b -> ConnMap peerAddr a -> b
$cfoldl' :: forall peerAddr b a. (b -> a -> b) -> b -> ConnMap peerAddr a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> ConnMap peerAddr a -> b
$cfoldr1 :: forall peerAddr a. (a -> a -> a) -> ConnMap peerAddr a -> a
foldr1 :: forall a. (a -> a -> a) -> ConnMap peerAddr a -> a
$cfoldl1 :: forall peerAddr a. (a -> a -> a) -> ConnMap peerAddr a -> a
foldl1 :: forall a. (a -> a -> a) -> ConnMap peerAddr a -> a
$ctoList :: forall peerAddr a. ConnMap peerAddr a -> [a]
toList :: forall a. ConnMap peerAddr a -> [a]
$cnull :: forall peerAddr a. ConnMap peerAddr a -> Bool
null :: forall a. ConnMap peerAddr a -> Bool
$clength :: forall peerAddr a. ConnMap peerAddr a -> Int
length :: forall a. ConnMap peerAddr a -> Int
$celem :: forall peerAddr a. Eq a => a -> ConnMap peerAddr a -> Bool
elem :: forall a. Eq a => a -> ConnMap peerAddr a -> Bool
$cmaximum :: forall peerAddr a. Ord a => ConnMap peerAddr a -> a
maximum :: forall a. Ord a => ConnMap peerAddr a -> a
$cminimum :: forall peerAddr a. Ord a => ConnMap peerAddr a -> a
minimum :: forall a. Ord a => ConnMap peerAddr a -> a
$csum :: forall peerAddr a. Num a => ConnMap peerAddr a -> a
sum :: forall a. Num a => ConnMap peerAddr a -> a
$cproduct :: forall peerAddr a. Num a => ConnMap peerAddr a -> a
product :: forall a. Num a => ConnMap peerAddr a -> a
Foldable)

instance  Traversable (ConnMap peerAddr) where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ConnMap peerAddr a -> f (ConnMap peerAddr b)
traverse a -> f b
f (ConnMap Map peerAddr (Map (LocalAddr peerAddr) a)
m) = Map peerAddr (Map (LocalAddr peerAddr) b) -> ConnMap peerAddr b
forall peerAddr a.
Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
ConnMap (Map peerAddr (Map (LocalAddr peerAddr) b) -> ConnMap peerAddr b)
-> f (Map peerAddr (Map (LocalAddr peerAddr) b))
-> f (ConnMap peerAddr b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Map (LocalAddr peerAddr) a -> f (Map (LocalAddr peerAddr) b))
-> Map peerAddr (Map (LocalAddr peerAddr) a)
-> f (Map peerAddr (Map (LocalAddr peerAddr) b))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Map peerAddr a -> f (Map peerAddr b)
traverse ((a -> f b)
-> Map (LocalAddr peerAddr) a -> f (Map (LocalAddr peerAddr) b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b)
-> Map (LocalAddr peerAddr) a -> f (Map (LocalAddr peerAddr) b)
traverse a -> f b
f) Map peerAddr (Map (LocalAddr peerAddr) a)
m


toList :: ConnMap m a -> [a]
toList :: forall peerAddr a. ConnMap peerAddr a -> [a]
toList = ConnMap m a -> [a]
forall a. ConnMap m a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList


-- | Create a map of all connections with a known `ConnectionId`.
--
toMap :: forall peerAddr a.
         Ord peerAddr
      => ConnMap peerAddr a
      -> Map (ConnectionId peerAddr) a
toMap :: forall peerAddr a.
Ord peerAddr =>
ConnMap peerAddr a -> Map (ConnectionId peerAddr) a
toMap =
    -- We can use `fromAscList` because of the `Ord` instance of `ConnectionId`.
    -- /NOTE:/ if `fromAscList` is used on input which doesn't satisfy its
    -- precondition, then `Map.lookup` might fail when it shouldn't.
    [(ConnectionId peerAddr, a)] -> Map (ConnectionId peerAddr) a
forall k a. Eq k => [(k, a)] -> Map k a
Map.fromAscList
  ([(ConnectionId peerAddr, a)] -> Map (ConnectionId peerAddr) a)
-> (ConnMap peerAddr a -> [(ConnectionId peerAddr, a)])
-> ConnMap peerAddr a
-> Map (ConnectionId peerAddr) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (peerAddr
 -> Map (LocalAddr peerAddr) a
 -> [(ConnectionId peerAddr, a)]
 -> [(ConnectionId peerAddr, a)])
-> [(ConnectionId peerAddr, a)]
-> Map peerAddr (Map (LocalAddr peerAddr) a)
-> [(ConnectionId peerAddr, a)]
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey
      (\peerAddr
remoteAddress Map (LocalAddr peerAddr) a
st [(ConnectionId peerAddr, a)]
conns ->
        (LocalAddr peerAddr
 -> a
 -> [(ConnectionId peerAddr, a)]
 -> [(ConnectionId peerAddr, a)])
-> [(ConnectionId peerAddr, a)]
-> Map (LocalAddr peerAddr) a
-> [(ConnectionId peerAddr, a)]
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey
          (\LocalAddr peerAddr
localAddr a
conn [(ConnectionId peerAddr, a)]
conns' ->
            case LocalAddr peerAddr
localAddr of
              LocalAddr peerAddr
UnknownLocalAddr -> [(ConnectionId peerAddr, a)]
conns'
              LocalAddr peerAddr
localAddress ->
                (ConnectionId { peerAddr
remoteAddress :: peerAddr
remoteAddress :: peerAddr
remoteAddress, peerAddr
localAddress :: peerAddr
localAddress :: peerAddr
localAddress }, a
conn) (ConnectionId peerAddr, a)
-> [(ConnectionId peerAddr, a)] -> [(ConnectionId peerAddr, a)]
forall a. a -> [a] -> [a]
: [(ConnectionId peerAddr, a)]
conns'
          )
          [(ConnectionId peerAddr, a)]
conns
          Map (LocalAddr peerAddr) a
st
      )
      []
  (Map peerAddr (Map (LocalAddr peerAddr) a)
 -> [(ConnectionId peerAddr, a)])
-> (ConnMap peerAddr a
    -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> ConnMap peerAddr a
-> [(ConnectionId peerAddr, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
forall peerAddr a.
ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
getConnMap


empty :: ConnMap peerAddr a
empty :: forall peerAddr a. ConnMap peerAddr a
empty = Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
forall peerAddr a.
Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
ConnMap Map peerAddr (Map (LocalAddr peerAddr) a)
forall k a. Map k a
Map.empty


insert :: Ord peerAddr
       => ConnectionId peerAddr
       -> a
       -> ConnMap peerAddr a
       -> ConnMap peerAddr a
insert :: forall peerAddr a.
Ord peerAddr =>
ConnectionId peerAddr
-> a -> ConnMap peerAddr a -> ConnMap peerAddr a
insert ConnectionId { peerAddr
remoteAddress :: forall addr. ConnectionId addr -> addr
remoteAddress :: peerAddr
remoteAddress, peerAddr
localAddress :: forall addr. ConnectionId addr -> addr
localAddress :: peerAddr
localAddress } a
a =
    Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
forall peerAddr a.
Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
ConnMap
  (Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a)
-> (ConnMap peerAddr a
    -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> ConnMap peerAddr a
-> ConnMap peerAddr a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe (Map (LocalAddr peerAddr) a)
 -> Maybe (Map (LocalAddr peerAddr) a))
-> peerAddr
-> Map peerAddr (Map (LocalAddr peerAddr) a)
-> Map peerAddr (Map (LocalAddr peerAddr) a)
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter
      (\case
        Maybe (Map (LocalAddr peerAddr) a)
Nothing -> Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a)
forall a. a -> Maybe a
Just (Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a))
-> Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a)
forall a b. (a -> b) -> a -> b
$! LocalAddr peerAddr -> a -> Map (LocalAddr peerAddr) a
forall k a. k -> a -> Map k a
Map.singleton (peerAddr -> LocalAddr peerAddr
forall peerAddr. peerAddr -> LocalAddr peerAddr
LocalAddr peerAddr
localAddress) a
a
        Just Map (LocalAddr peerAddr) a
st -> Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a)
forall a. a -> Maybe a
Just (Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a))
-> Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a)
forall a b. (a -> b) -> a -> b
$! LocalAddr peerAddr
-> a -> Map (LocalAddr peerAddr) a -> Map (LocalAddr peerAddr) a
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (peerAddr -> LocalAddr peerAddr
forall peerAddr. peerAddr -> LocalAddr peerAddr
LocalAddr peerAddr
localAddress) a
a Map (LocalAddr peerAddr) a
st)
    peerAddr
remoteAddress
  (Map peerAddr (Map (LocalAddr peerAddr) a)
 -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> (ConnMap peerAddr a
    -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> ConnMap peerAddr a
-> Map peerAddr (Map (LocalAddr peerAddr) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
forall peerAddr a.
ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
getConnMap


insertUnknownLocalAddr
  :: Ord peerAddr
  => peerAddr
  -> a
  -> ConnMap peerAddr a
  -> ConnMap peerAddr a
insertUnknownLocalAddr :: forall peerAddr a.
Ord peerAddr =>
peerAddr -> a -> ConnMap peerAddr a -> ConnMap peerAddr a
insertUnknownLocalAddr peerAddr
remoteAddress a
a =
    Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
forall peerAddr a.
Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
ConnMap
  (Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a)
-> (ConnMap peerAddr a
    -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> ConnMap peerAddr a
-> ConnMap peerAddr a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe (Map (LocalAddr peerAddr) a)
 -> Maybe (Map (LocalAddr peerAddr) a))
-> peerAddr
-> Map peerAddr (Map (LocalAddr peerAddr) a)
-> Map peerAddr (Map (LocalAddr peerAddr) a)
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter
      (\case
        Maybe (Map (LocalAddr peerAddr) a)
Nothing -> Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a)
forall a. a -> Maybe a
Just (Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a))
-> Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a)
forall a b. (a -> b) -> a -> b
$! LocalAddr peerAddr -> a -> Map (LocalAddr peerAddr) a
forall k a. k -> a -> Map k a
Map.singleton LocalAddr peerAddr
forall peerAddr. LocalAddr peerAddr
UnknownLocalAddr a
a
        Just Map (LocalAddr peerAddr) a
st -> Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a)
forall a. a -> Maybe a
Just (Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a))
-> Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a)
forall a b. (a -> b) -> a -> b
$! LocalAddr peerAddr
-> a -> Map (LocalAddr peerAddr) a -> Map (LocalAddr peerAddr) a
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert LocalAddr peerAddr
forall peerAddr. LocalAddr peerAddr
UnknownLocalAddr a
a Map (LocalAddr peerAddr) a
st
      )
    peerAddr
remoteAddress
  (Map peerAddr (Map (LocalAddr peerAddr) a)
 -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> (ConnMap peerAddr a
    -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> ConnMap peerAddr a
-> Map peerAddr (Map (LocalAddr peerAddr) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
forall peerAddr a.
ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
getConnMap


delete :: Ord peerAddr
       => ConnectionId peerAddr
       -> ConnMap peerAddr a
       -> ConnMap peerAddr a
delete :: forall peerAddr a.
Ord peerAddr =>
ConnectionId peerAddr -> ConnMap peerAddr a -> ConnMap peerAddr a
delete ConnectionId { peerAddr
remoteAddress :: forall addr. ConnectionId addr -> addr
remoteAddress :: peerAddr
remoteAddress, peerAddr
localAddress :: forall addr. ConnectionId addr -> addr
localAddress :: peerAddr
localAddress } =
    Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
forall peerAddr a.
Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
ConnMap
  (Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a)
-> (ConnMap peerAddr a
    -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> ConnMap peerAddr a
-> ConnMap peerAddr a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe (Map (LocalAddr peerAddr) a)
 -> Maybe (Map (LocalAddr peerAddr) a))
-> peerAddr
-> Map peerAddr (Map (LocalAddr peerAddr) a)
-> Map peerAddr (Map (LocalAddr peerAddr) a)
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter
      (\case
          Maybe (Map (LocalAddr peerAddr) a)
Nothing -> Maybe (Map (LocalAddr peerAddr) a)
forall a. Maybe a
Nothing
          Just Map (LocalAddr peerAddr) a
st ->
            let st' :: Map (LocalAddr peerAddr) a
st' = LocalAddr peerAddr
-> Map (LocalAddr peerAddr) a -> Map (LocalAddr peerAddr) a
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete (peerAddr -> LocalAddr peerAddr
forall peerAddr. peerAddr -> LocalAddr peerAddr
LocalAddr peerAddr
localAddress) Map (LocalAddr peerAddr) a
st
            in if Map (LocalAddr peerAddr) a -> Bool
forall k a. Map k a -> Bool
Map.null Map (LocalAddr peerAddr) a
st'
                 then Maybe (Map (LocalAddr peerAddr) a)
forall a. Maybe a
Nothing
                 else Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a)
forall a. a -> Maybe a
Just Map (LocalAddr peerAddr) a
st'
      )
      peerAddr
remoteAddress
  (Map peerAddr (Map (LocalAddr peerAddr) a)
 -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> (ConnMap peerAddr a
    -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> ConnMap peerAddr a
-> Map peerAddr (Map (LocalAddr peerAddr) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
forall peerAddr a.
ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
getConnMap


deleteAtRemoteAddr
  :: (Ord peerAddr, Eq a)
  => peerAddr
  -- ^ remoteAddr
  -> a
  -- ^ element to remove
  -> ConnMap peerAddr a
  -> ConnMap peerAddr a
deleteAtRemoteAddr :: forall peerAddr a.
(Ord peerAddr, Eq a) =>
peerAddr -> a -> ConnMap peerAddr a -> ConnMap peerAddr a
deleteAtRemoteAddr peerAddr
remoteAddress a
a =
    Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
forall peerAddr a.
Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
ConnMap
  (Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a)
-> (ConnMap peerAddr a
    -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> ConnMap peerAddr a
-> ConnMap peerAddr a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe (Map (LocalAddr peerAddr) a)
 -> Maybe (Map (LocalAddr peerAddr) a))
-> peerAddr
-> Map peerAddr (Map (LocalAddr peerAddr) a)
-> Map peerAddr (Map (LocalAddr peerAddr) a)
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter
      (\case
          Maybe (Map (LocalAddr peerAddr) a)
Nothing -> Maybe (Map (LocalAddr peerAddr) a)
forall a. Maybe a
Nothing
          Just Map (LocalAddr peerAddr) a
st ->
            let st' :: Map (LocalAddr peerAddr) a
st' = (a -> Bool)
-> Map (LocalAddr peerAddr) a -> Map (LocalAddr peerAddr) a
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/=a
a) Map (LocalAddr peerAddr) a
st in
            if Map (LocalAddr peerAddr) a -> Bool
forall k a. Map k a -> Bool
Map.null Map (LocalAddr peerAddr) a
st'
              then Maybe (Map (LocalAddr peerAddr) a)
forall a. Maybe a
Nothing
              else Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a)
forall a. a -> Maybe a
Just Map (LocalAddr peerAddr) a
st'
      )
      peerAddr
remoteAddress
  (Map peerAddr (Map (LocalAddr peerAddr) a)
 -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> (ConnMap peerAddr a
    -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> ConnMap peerAddr a
-> Map peerAddr (Map (LocalAddr peerAddr) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
forall peerAddr a.
ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
getConnMap



lookup :: Ord peerAddr
       => ConnectionId peerAddr
       -> ConnMap peerAddr a
       -> Maybe a
lookup :: forall peerAddr a.
Ord peerAddr =>
ConnectionId peerAddr -> ConnMap peerAddr a -> Maybe a
lookup ConnectionId { peerAddr
remoteAddress :: forall addr. ConnectionId addr -> addr
remoteAddress :: peerAddr
remoteAddress, peerAddr
localAddress :: forall addr. ConnectionId addr -> addr
localAddress :: peerAddr
localAddress } (ConnMap Map peerAddr (Map (LocalAddr peerAddr) a)
st) =
   case peerAddr
remoteAddress peerAddr
-> Map peerAddr (Map (LocalAddr peerAddr) a)
-> Maybe (Map (LocalAddr peerAddr) a)
forall k a. Ord k => k -> Map k a -> Maybe a
`Map.lookup` Map peerAddr (Map (LocalAddr peerAddr) a)
st of
     Maybe (Map (LocalAddr peerAddr) a)
Nothing  -> Maybe a
forall a. Maybe a
Nothing
     Just Map (LocalAddr peerAddr) a
st' -> peerAddr -> LocalAddr peerAddr
forall peerAddr. peerAddr -> LocalAddr peerAddr
LocalAddr peerAddr
localAddress LocalAddr peerAddr -> Map (LocalAddr peerAddr) a -> Maybe a
forall k a. Ord k => k -> Map k a -> Maybe a
`Map.lookup` Map (LocalAddr peerAddr) a
st'


-- | Find a random entry for a given remote address.
--
-- NOTE: the outbound governor will only ask for a connection to a peer if it
-- doesn't have one (and one isn't being created).  This property, simplifies
-- `lookupOutbound`: we can pick (randomly) one of the connections to the
-- remote peer.  When the outbound governor is asking, likely all of the
-- connections are in an inbound state.  The outbound governor will has a grace
-- period after demoting a peer, so a race (when a is being demoted and
-- promoted at the same time) is unlikely.
--
lookupByRemoteAddr
    :: forall rnd peerAddr a.
       ( Ord peerAddr
       , RandomGen rnd
       )
    => rnd
    -- ^ a fresh `rnd` (it must come from a `split`)
    -> peerAddr
    -- ^ remote address
    -> ConnMap peerAddr a
    -> (Maybe a)
lookupByRemoteAddr :: forall rnd peerAddr a.
(Ord peerAddr, RandomGen rnd) =>
rnd -> peerAddr -> ConnMap peerAddr a -> Maybe a
lookupByRemoteAddr rnd
rnd peerAddr
remoteAddress (ConnMap Map peerAddr (Map (LocalAddr peerAddr) a)
st) =
  case peerAddr
remoteAddress peerAddr
-> Map peerAddr (Map (LocalAddr peerAddr) a)
-> Maybe (Map (LocalAddr peerAddr) a)
forall k a. Ord k => k -> Map k a -> Maybe a
`Map.lookup` Map peerAddr (Map (LocalAddr peerAddr) a)
st of
    Maybe (Map (LocalAddr peerAddr) a)
Nothing -> Maybe a
forall a. Maybe a
Nothing
    Just Map (LocalAddr peerAddr) a
st' ->
      case LocalAddr peerAddr
forall peerAddr. LocalAddr peerAddr
UnknownLocalAddr LocalAddr peerAddr -> Map (LocalAddr peerAddr) a -> Maybe a
forall k a. Ord k => k -> Map k a -> Maybe a
`Map.lookup` Map (LocalAddr peerAddr) a
st' of
        Just a
a -> a -> Maybe a
forall a. a -> Maybe a
Just a
a
        Maybe a
Nothing ->
          if Map (LocalAddr peerAddr) a -> Bool
forall k a. Map k a -> Bool
Map.null Map (LocalAddr peerAddr) a
st'
          then Maybe a
forall a. Maybe a
Nothing
          else let (Int
indx, rnd
_rnd') = (Int, Int) -> rnd -> (Int, rnd)
forall g a. (RandomGen g, UniformRange a) => (a, a) -> g -> (a, g)
uniformR (Int
0, Map (LocalAddr peerAddr) a -> Int
forall k a. Map k a -> Int
Map.size Map (LocalAddr peerAddr) a
st' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) rnd
rnd
               in a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ (LocalAddr peerAddr, a) -> a
forall a b. (a, b) -> b
snd ((LocalAddr peerAddr, a) -> a) -> (LocalAddr peerAddr, a) -> a
forall a b. (a -> b) -> a -> b
$ Int -> Map (LocalAddr peerAddr) a -> (LocalAddr peerAddr, a)
forall k a. Int -> Map k a -> (k, a)
Map.elemAt Int
indx Map (LocalAddr peerAddr) a
st'


-- | Promote `UnknownLocalAddr` to `LocalAddr`.
--
updateLocalAddr
  :: Ord peerAddr
  => ConnectionId peerAddr
  -> ConnMap peerAddr a
  -> (Bool, ConnMap peerAddr a)
  -- ^ Return `True` iff the entry was updated.
updateLocalAddr :: forall peerAddr a.
Ord peerAddr =>
ConnectionId peerAddr
-> ConnMap peerAddr a -> (Bool, ConnMap peerAddr a)
updateLocalAddr ConnectionId { peerAddr
remoteAddress :: forall addr. ConnectionId addr -> addr
remoteAddress :: peerAddr
remoteAddress, peerAddr
localAddress :: forall addr. ConnectionId addr -> addr
localAddress :: peerAddr
localAddress } (ConnMap Map peerAddr (Map (LocalAddr peerAddr) a)
m) =
      Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
forall peerAddr a.
Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a
ConnMap
  (Map peerAddr (Map (LocalAddr peerAddr) a) -> ConnMap peerAddr a)
-> (Bool, Map peerAddr (Map (LocalAddr peerAddr) a))
-> (Bool, ConnMap peerAddr a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Maybe (Map (LocalAddr peerAddr) a)
 -> (Bool, Maybe (Map (LocalAddr peerAddr) a)))
-> peerAddr
-> Map peerAddr (Map (LocalAddr peerAddr) a)
-> (Bool, Map peerAddr (Map (LocalAddr peerAddr) a))
forall (f :: * -> *) k a.
(Functor f, Ord k) =>
(Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
Map.alterF
      (\case
        Maybe (Map (LocalAddr peerAddr) a)
Nothing -> (Bool
False, Maybe (Map (LocalAddr peerAddr) a)
forall a. Maybe a
Nothing)
        Just Map (LocalAddr peerAddr) a
m' ->
          let -- delete & lookup for entry in `UnknownLocalAddr`
              (Maybe a
ma, Map (LocalAddr peerAddr) a
m'') =
                (Maybe a -> (Maybe a, Maybe a))
-> LocalAddr peerAddr
-> Map (LocalAddr peerAddr) a
-> (Maybe a, Map (LocalAddr peerAddr) a)
forall (f :: * -> *) k a.
(Functor f, Ord k) =>
(Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
Map.alterF
                  (\Maybe a
x -> (Maybe a
x,Maybe a
forall a. Maybe a
Nothing))
                  LocalAddr peerAddr
forall peerAddr. LocalAddr peerAddr
UnknownLocalAddr
                  Map (LocalAddr peerAddr) a
m'
          in
            case Maybe a
ma of
              -- there was no entry, so no need to update the inner map
              Maybe a
Nothing -> (Bool
False, Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a)
forall a. a -> Maybe a
Just Map (LocalAddr peerAddr) a
m')
              -- we have an entry: put it in the `LocalAddr`, but only if it's
              -- not present in the map.
              Just {} ->
                  (Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a))
-> (Bool, Map (LocalAddr peerAddr) a)
-> (Bool, Maybe (Map (LocalAddr peerAddr) a))
forall a b. (a -> b) -> (Bool, a) -> (Bool, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Map (LocalAddr peerAddr) a -> Maybe (Map (LocalAddr peerAddr) a)
forall a. a -> Maybe a
Just
                ((Bool, Map (LocalAddr peerAddr) a)
 -> (Bool, Maybe (Map (LocalAddr peerAddr) a)))
-> (Map (LocalAddr peerAddr) a
    -> (Bool, Map (LocalAddr peerAddr) a))
-> Map (LocalAddr peerAddr) a
-> (Bool, Maybe (Map (LocalAddr peerAddr) a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe a -> (Bool, Maybe a))
-> LocalAddr peerAddr
-> Map (LocalAddr peerAddr) a
-> (Bool, Map (LocalAddr peerAddr) a)
forall (f :: * -> *) k a.
(Functor f, Ord k) =>
(Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
Map.alterF
                    (\case
                      Maybe a
Nothing  -> (Bool
True, Maybe a
ma)
                      a :: Maybe a
a@Just{} -> (Bool
False, Maybe a
a)
                    )
                    (peerAddr -> LocalAddr peerAddr
forall peerAddr. peerAddr -> LocalAddr peerAddr
LocalAddr peerAddr
localAddress)
                (Map (LocalAddr peerAddr) a
 -> (Bool, Maybe (Map (LocalAddr peerAddr) a)))
-> Map (LocalAddr peerAddr) a
-> (Bool, Maybe (Map (LocalAddr peerAddr) a))
forall a b. (a -> b) -> a -> b
$ Map (LocalAddr peerAddr) a
m''
      )
      peerAddr
remoteAddress
      Map peerAddr (Map (LocalAddr peerAddr) a)
m


traverseMaybe
  :: Applicative f
  => (a -> f (Maybe b))
  -> ConnMap peerAddr a
  -> f [b]
traverseMaybe :: forall (f :: * -> *) a b peerAddr.
Applicative f =>
(a -> f (Maybe b)) -> ConnMap peerAddr a -> f [b]
traverseMaybe a -> f (Maybe b)
fn =
    (Map peerAddr [b] -> [b]) -> f (Map peerAddr [b]) -> f [b]
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([[b]] -> [b]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[b]] -> [b])
-> (Map peerAddr [b] -> [[b]]) -> Map peerAddr [b] -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map peerAddr [b] -> [[b]]
forall k a. Map k a -> [a]
Map.elems)
  (f (Map peerAddr [b]) -> f [b])
-> (ConnMap peerAddr a -> f (Map peerAddr [b]))
-> ConnMap peerAddr a
-> f [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (peerAddr -> Map (LocalAddr peerAddr) a -> f (Maybe [b]))
-> Map peerAddr (Map (LocalAddr peerAddr) a)
-> f (Map peerAddr [b])
forall (f :: * -> *) k a b.
Applicative f =>
(k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)
Map.traverseMaybeWithKey
      (\peerAddr
_ Map (LocalAddr peerAddr) a
st ->
          (Map (LocalAddr peerAddr) b -> Maybe [b])
-> f (Map (LocalAddr peerAddr) b) -> f (Maybe [b])
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([b] -> Maybe [b]
forall a. a -> Maybe a
Just ([b] -> Maybe [b])
-> (Map (LocalAddr peerAddr) b -> [b])
-> Map (LocalAddr peerAddr) b
-> Maybe [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map (LocalAddr peerAddr) b -> [b]
forall k a. Map k a -> [a]
Map.elems)
        (f (Map (LocalAddr peerAddr) b) -> f (Maybe [b]))
-> (Map (LocalAddr peerAddr) a -> f (Map (LocalAddr peerAddr) b))
-> Map (LocalAddr peerAddr) a
-> f (Maybe [b])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LocalAddr peerAddr -> a -> f (Maybe b))
-> Map (LocalAddr peerAddr) a -> f (Map (LocalAddr peerAddr) b)
forall (f :: * -> *) k a b.
Applicative f =>
(k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)
Map.traverseMaybeWithKey (\LocalAddr peerAddr
_ -> a -> f (Maybe b)
fn)
        (Map (LocalAddr peerAddr) a -> f (Maybe [b]))
-> Map (LocalAddr peerAddr) a -> f (Maybe [b])
forall a b. (a -> b) -> a -> b
$ Map (LocalAddr peerAddr) a
st
      )
  (Map peerAddr (Map (LocalAddr peerAddr) a) -> f (Map peerAddr [b]))
-> (ConnMap peerAddr a
    -> Map peerAddr (Map (LocalAddr peerAddr) a))
-> ConnMap peerAddr a
-> f (Map peerAddr [b])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
forall peerAddr a.
ConnMap peerAddr a -> Map peerAddr (Map (LocalAddr peerAddr) a)
getConnMap