module LibP2P.Protocol.GossipSub.PartialMessages
( PartialMessagesExtension
, PartialMessagesConfig (..)
, PartialCallbackResult (..)
, PartialPublishAction (..)
, PartialPublishPlan
, PartialMessagesError (..)
, defaultPartialMessagesConfig
, newPartialMessagesExtension
, installPartialMessages
, partialMeshPeers
, partialPeerRequests
, publishPartial
) where
import Control.Concurrent.MVar
import Control.Concurrent.STM
import Control.Exception (Exception)
import Control.Monad (foldM, forM, forM_, unless)
import Data.ByteString (ByteString)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe, isJust, isNothing)
import qualified Data.Set as Set
import LibP2P.Crypto.PeerId (PeerId)
import LibP2P.Protocol.GossipSub.Extensions
( localPartialMode
, peerRequestsPartial
, peerSupportsPartialExtension
, peerSupportsSendingPartial
)
import LibP2P.Protocol.GossipSub.Types
data PartialMessagesConfig state = PartialMessagesConfig
{ forall state. PartialMessagesConfig state -> state
partialInitialPeerState :: !state
, forall state.
PartialMessagesConfig state
-> PeerId
-> Map PeerId state
-> PartialMessage
-> IO (PartialCallbackResult state)
partialOnIncoming ::
!(PeerId -> Map PeerId state -> PartialMessage -> IO (PartialCallbackResult state))
, forall state.
PartialMessagesConfig state
-> Topic
-> ByteString
-> [PeerId]
-> Map PeerId state
-> IO (PartialCallbackResult state)
partialOnEmitGossip ::
!(Topic -> ByteString -> [PeerId] -> Map PeerId state -> IO (PartialCallbackResult state))
, forall state. PartialMessagesConfig state -> Int
partialGroupTTLHeartbeats :: !Int
, forall state. PartialMessagesConfig state -> Int
partialPeerGroupLimitPerTopic :: !Int
, forall state. PartialMessagesConfig state -> Int
partialPeerGroupLimitPerTopicPerPeer :: !Int
}
data PartialCallbackResult state = PartialCallbackResult
{ forall state. PartialCallbackResult state -> Map PeerId state
partialUpdatedPeerStates :: !(Map PeerId state)
, forall state. PartialCallbackResult state -> [PartialPublishAction]
partialPublishActions :: ![PartialPublishAction]
}
data PartialPublishAction = PartialPublishAction
{ PartialPublishAction -> PeerId
partialActionPeer :: !PeerId
, PartialPublishAction -> Maybe ByteString
partialActionMessage :: !(Maybe ByteString)
, PartialPublishAction -> Maybe ByteString
partialActionMetadata :: !(Maybe ByteString)
}
deriving (Int -> PartialPublishAction -> ShowS
[PartialPublishAction] -> ShowS
PartialPublishAction -> String
(Int -> PartialPublishAction -> ShowS)
-> (PartialPublishAction -> String)
-> ([PartialPublishAction] -> ShowS)
-> Show PartialPublishAction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PartialPublishAction -> ShowS
showsPrec :: Int -> PartialPublishAction -> ShowS
$cshow :: PartialPublishAction -> String
show :: PartialPublishAction -> String
$cshowList :: [PartialPublishAction] -> ShowS
showList :: [PartialPublishAction] -> ShowS
Show, PartialPublishAction -> PartialPublishAction -> Bool
(PartialPublishAction -> PartialPublishAction -> Bool)
-> (PartialPublishAction -> PartialPublishAction -> Bool)
-> Eq PartialPublishAction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PartialPublishAction -> PartialPublishAction -> Bool
== :: PartialPublishAction -> PartialPublishAction -> Bool
$c/= :: PartialPublishAction -> PartialPublishAction -> Bool
/= :: PartialPublishAction -> PartialPublishAction -> Bool
Eq)
type PartialPublishPlan state =
Map PeerId state ->
(PeerId -> Bool) ->
IO (PartialCallbackResult state)
data PartialMessagesError
= PartialMessagesAlreadyInstalled
| PartialMessagesNodeStarted
| PartialMessagesNotInstalled
| PartialMessagesNotEnabledForTopic
deriving (Int -> PartialMessagesError -> ShowS
[PartialMessagesError] -> ShowS
PartialMessagesError -> String
(Int -> PartialMessagesError -> ShowS)
-> (PartialMessagesError -> String)
-> ([PartialMessagesError] -> ShowS)
-> Show PartialMessagesError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PartialMessagesError -> ShowS
showsPrec :: Int -> PartialMessagesError -> ShowS
$cshow :: PartialMessagesError -> String
show :: PartialMessagesError -> String
$cshowList :: [PartialMessagesError] -> ShowS
showList :: [PartialMessagesError] -> ShowS
Show, PartialMessagesError -> PartialMessagesError -> Bool
(PartialMessagesError -> PartialMessagesError -> Bool)
-> (PartialMessagesError -> PartialMessagesError -> Bool)
-> Eq PartialMessagesError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PartialMessagesError -> PartialMessagesError -> Bool
== :: PartialMessagesError -> PartialMessagesError -> Bool
$c/= :: PartialMessagesError -> PartialMessagesError -> Bool
/= :: PartialMessagesError -> PartialMessagesError -> Bool
Eq)
instance Exception PartialMessagesError
data PartialMessagesExtension state = PartialMessagesExtension
{ forall state.
PartialMessagesExtension state -> PartialMessagesConfig state
pmeConfig :: !(PartialMessagesConfig state)
, forall state.
PartialMessagesExtension state -> MVar (PartialRuntimeState state)
pmeState :: !(MVar (PartialRuntimeState state))
, forall state.
PartialMessagesExtension state -> MVar (Maybe GossipSubRouter)
pmeRouter :: !(MVar (Maybe GossipSubRouter))
}
data PartialRuntimeState state = PartialRuntimeState
{ forall state.
PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
prsGroups :: !(Map Topic (Map ByteString (PartialGroupState state)))
, forall state.
PartialRuntimeState state -> Map Topic PeerGroupCounter
prsCounters :: !(Map Topic PeerGroupCounter)
}
data PartialGroupState state = PartialGroupState
{ forall state. PartialGroupState state -> Map PeerId state
pgsPeerStates :: !(Map PeerId state)
, forall state. PartialGroupState state -> Int
pgsTTL :: !Int
, forall state. PartialGroupState state -> Maybe PeerId
pgsInitiatedBy :: !(Maybe PeerId)
}
data PeerGroupCounter = PeerGroupCounter
{ PeerGroupCounter -> Int
pgcTotal :: !Int
, PeerGroupCounter -> Map PeerId Int
pgcPerPeer :: !(Map PeerId Int)
}
defaultPartialMessagesConfig ::
state ->
(PeerId -> Map PeerId state -> PartialMessage -> IO (PartialCallbackResult state)) ->
(Topic -> ByteString -> [PeerId] -> Map PeerId state -> IO (PartialCallbackResult state)) ->
PartialMessagesConfig state
defaultPartialMessagesConfig :: forall state.
state
-> (PeerId
-> Map PeerId state
-> PartialMessage
-> IO (PartialCallbackResult state))
-> (Topic
-> ByteString
-> [PeerId]
-> Map PeerId state
-> IO (PartialCallbackResult state))
-> PartialMessagesConfig state
defaultPartialMessagesConfig state
initial PeerId
-> Map PeerId state
-> PartialMessage
-> IO (PartialCallbackResult state)
onIncoming Topic
-> ByteString
-> [PeerId]
-> Map PeerId state
-> IO (PartialCallbackResult state)
onGossip =
PartialMessagesConfig
{ partialInitialPeerState :: state
partialInitialPeerState = state
initial
, partialOnIncoming :: PeerId
-> Map PeerId state
-> PartialMessage
-> IO (PartialCallbackResult state)
partialOnIncoming = PeerId
-> Map PeerId state
-> PartialMessage
-> IO (PartialCallbackResult state)
onIncoming
, partialOnEmitGossip :: Topic
-> ByteString
-> [PeerId]
-> Map PeerId state
-> IO (PartialCallbackResult state)
partialOnEmitGossip = Topic
-> ByteString
-> [PeerId]
-> Map PeerId state
-> IO (PartialCallbackResult state)
onGossip
, partialGroupTTLHeartbeats :: Int
partialGroupTTLHeartbeats = Int
3
, partialPeerGroupLimitPerTopic :: Int
partialPeerGroupLimitPerTopic = Int
255
, partialPeerGroupLimitPerTopicPerPeer :: Int
partialPeerGroupLimitPerTopicPerPeer = Int
8
}
newPartialMessagesExtension ::
PartialMessagesConfig state -> IO (PartialMessagesExtension state)
newPartialMessagesExtension :: forall state.
PartialMessagesConfig state -> IO (PartialMessagesExtension state)
newPartialMessagesExtension PartialMessagesConfig state
config =
PartialMessagesConfig state
-> MVar (PartialRuntimeState state)
-> MVar (Maybe GossipSubRouter)
-> PartialMessagesExtension state
forall state.
PartialMessagesConfig state
-> MVar (PartialRuntimeState state)
-> MVar (Maybe GossipSubRouter)
-> PartialMessagesExtension state
PartialMessagesExtension PartialMessagesConfig state
config
(MVar (PartialRuntimeState state)
-> MVar (Maybe GossipSubRouter) -> PartialMessagesExtension state)
-> IO (MVar (PartialRuntimeState state))
-> IO
(MVar (Maybe GossipSubRouter) -> PartialMessagesExtension state)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PartialRuntimeState state -> IO (MVar (PartialRuntimeState state))
forall a. a -> IO (MVar a)
newMVar (Map Topic (Map ByteString (PartialGroupState state))
-> Map Topic PeerGroupCounter -> PartialRuntimeState state
forall state.
Map Topic (Map ByteString (PartialGroupState state))
-> Map Topic PeerGroupCounter -> PartialRuntimeState state
PartialRuntimeState Map Topic (Map ByteString (PartialGroupState state))
forall k a. Map k a
Map.empty Map Topic PeerGroupCounter
forall k a. Map k a
Map.empty)
IO (MVar (Maybe GossipSubRouter) -> PartialMessagesExtension state)
-> IO (MVar (Maybe GossipSubRouter))
-> IO (PartialMessagesExtension state)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe GossipSubRouter -> IO (MVar (Maybe GossipSubRouter))
forall a. a -> IO (MVar a)
newMVar Maybe GossipSubRouter
forall a. Maybe a
Nothing
installPartialMessages ::
GossipSubRouter ->
PartialMessagesExtension state ->
IO (Either PartialMessagesError ())
installPartialMessages :: forall state.
GossipSubRouter
-> PartialMessagesExtension state
-> IO (Either PartialMessagesError ())
installPartialMessages GossipSubRouter
router PartialMessagesExtension state
extension = MVar (Maybe GossipSubRouter)
-> (Maybe GossipSubRouter
-> IO (Maybe GossipSubRouter, Either PartialMessagesError ()))
-> IO (Either PartialMessagesError ())
forall a b. MVar a -> (a -> IO (a, b)) -> IO b
modifyMVar (PartialMessagesExtension state -> MVar (Maybe GossipSubRouter)
forall state.
PartialMessagesExtension state -> MVar (Maybe GossipSubRouter)
pmeRouter PartialMessagesExtension state
extension) ((Maybe GossipSubRouter
-> IO (Maybe GossipSubRouter, Either PartialMessagesError ()))
-> IO (Either PartialMessagesError ()))
-> (Maybe GossipSubRouter
-> IO (Maybe GossipSubRouter, Either PartialMessagesError ()))
-> IO (Either PartialMessagesError ())
forall a b. (a -> b) -> a -> b
$ \Maybe GossipSubRouter
installed ->
case Maybe GossipSubRouter
installed of
Just GossipSubRouter
_ -> (Maybe GossipSubRouter, Either PartialMessagesError ())
-> IO (Maybe GossipSubRouter, Either PartialMessagesError ())
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe GossipSubRouter
installed, PartialMessagesError -> Either PartialMessagesError ()
forall a b. a -> Either a b
Left PartialMessagesError
PartialMessagesAlreadyInstalled)
Maybe GossipSubRouter
Nothing -> do
claimed <- STM Bool -> IO Bool
forall a. STM a -> IO a
atomically (STM Bool -> IO Bool) -> STM Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$ GossipSubRouter -> PartialMessagesHooks -> STM Bool
claimHooks GossipSubRouter
router PartialMessagesHooks
hooks
if claimed
then pure (Just router, Right ())
else pure (Nothing, Left PartialMessagesAlreadyInstalled)
where
hooks :: PartialMessagesHooks
hooks =
PartialMessagesHooks
{ pmhHandleRPC :: PeerId -> PartialMessage -> IO Bool
pmhHandleRPC = PartialMessagesExtension state
-> GossipSubRouter -> PeerId -> PartialMessage -> IO Bool
forall state.
PartialMessagesExtension state
-> GossipSubRouter -> PeerId -> PartialMessage -> IO Bool
handleIncoming PartialMessagesExtension state
extension GossipSubRouter
router
, pmhHeartbeat :: IO ()
pmhHeartbeat = PartialMessagesExtension state -> IO ()
forall state. PartialMessagesExtension state -> IO ()
heartbeat PartialMessagesExtension state
extension
, pmhEmitGossip :: Topic -> [PeerId] -> IO ()
pmhEmitGossip = PartialMessagesExtension state
-> GossipSubRouter -> Topic -> [PeerId] -> IO ()
forall state.
PartialMessagesExtension state
-> GossipSubRouter -> Topic -> [PeerId] -> IO ()
emitGossip PartialMessagesExtension state
extension GossipSubRouter
router
, pmhPeerDisconnected :: PeerId -> IO ()
pmhPeerDisconnected = PartialMessagesExtension state -> PeerId -> IO ()
forall state. PartialMessagesExtension state -> PeerId -> IO ()
peerDisconnected PartialMessagesExtension state
extension
}
claimHooks :: GossipSubRouter -> PartialMessagesHooks -> STM Bool
claimHooks :: GossipSubRouter -> PartialMessagesHooks -> STM Bool
claimHooks GossipSubRouter
router PartialMessagesHooks
hooks = do
current <- TVar (Maybe PartialMessagesHooks)
-> STM (Maybe PartialMessagesHooks)
forall a. TVar a -> STM a
readTVar (GossipSubRouter -> TVar (Maybe PartialMessagesHooks)
gsPartialMessagesHooks GossipSubRouter
router)
if isJust current
then pure False
else writeTVar (gsPartialMessagesHooks router) (Just hooks) >> pure True
partialMeshPeers ::
PartialMessagesExtension state ->
Topic ->
IO (Either PartialMessagesError [PeerId])
partialMeshPeers :: forall state.
PartialMessagesExtension state
-> Topic -> IO (Either PartialMessagesError [PeerId])
partialMeshPeers PartialMessagesExtension state
extension Topic
topic = do
installed <- MVar (Maybe GossipSubRouter) -> IO (Maybe GossipSubRouter)
forall a. MVar a -> IO a
readMVar (PartialMessagesExtension state -> MVar (Maybe GossipSubRouter)
forall state.
PartialMessagesExtension state -> MVar (Maybe GossipSubRouter)
pmeRouter PartialMessagesExtension state
extension)
case installed of
Maybe GossipSubRouter
Nothing -> Either PartialMessagesError [PeerId]
-> IO (Either PartialMessagesError [PeerId])
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PartialMessagesError -> Either PartialMessagesError [PeerId]
forall a b. a -> Either a b
Left PartialMessagesError
PartialMessagesNotInstalled)
Just GossipSubRouter
router -> [PeerId] -> Either PartialMessagesError [PeerId]
forall a b. b -> Either a b
Right ([PeerId] -> Either PartialMessagesError [PeerId])
-> IO [PeerId] -> IO (Either PartialMessagesError [PeerId])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GossipSubRouter -> Topic -> IO [PeerId]
eligiblePeers GossipSubRouter
router Topic
topic
partialPeerRequests ::
PartialMessagesExtension state -> PeerId -> Topic -> IO Bool
partialPeerRequests :: forall state.
PartialMessagesExtension state -> PeerId -> Topic -> IO Bool
partialPeerRequests PartialMessagesExtension state
extension PeerId
pid Topic
topic = do
installed <- MVar (Maybe GossipSubRouter) -> IO (Maybe GossipSubRouter)
forall a. MVar a -> IO a
readMVar (PartialMessagesExtension state -> MVar (Maybe GossipSubRouter)
forall state.
PartialMessagesExtension state -> MVar (Maybe GossipSubRouter)
pmeRouter PartialMessagesExtension state
extension)
maybe (pure False) (\GossipSubRouter
router -> GossipSubRouter -> PeerId -> Topic -> IO Bool
peerRequestsPartial GossipSubRouter
router PeerId
pid Topic
topic) installed
publishPartial ::
PartialMessagesExtension state ->
Topic ->
ByteString ->
PartialPublishPlan state ->
IO (Either PartialMessagesError ())
publishPartial :: forall state.
PartialMessagesExtension state
-> Topic
-> ByteString
-> PartialPublishPlan state
-> IO (Either PartialMessagesError ())
publishPartial PartialMessagesExtension state
extension Topic
topic ByteString
groupId PartialPublishPlan state
plan = do
installed <- MVar (Maybe GossipSubRouter) -> IO (Maybe GossipSubRouter)
forall a. MVar a -> IO a
readMVar (PartialMessagesExtension state -> MVar (Maybe GossipSubRouter)
forall state.
PartialMessagesExtension state -> MVar (Maybe GossipSubRouter)
pmeRouter PartialMessagesExtension state
extension)
case installed of
Maybe GossipSubRouter
Nothing -> Either PartialMessagesError ()
-> IO (Either PartialMessagesError ())
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PartialMessagesError -> Either PartialMessagesError ()
forall a b. a -> Either a b
Left PartialMessagesError
PartialMessagesNotInstalled)
Just GossipSubRouter
router -> do
mode <- GossipSubRouter -> Topic -> IO (Maybe PartialTopicMode)
localPartialMode GossipSubRouter
router Topic
topic
case mode of
Maybe PartialTopicMode
Nothing -> Either PartialMessagesError ()
-> IO (Either PartialMessagesError ())
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PartialMessagesError -> Either PartialMessagesError ()
forall a b. a -> Either a b
Left PartialMessagesError
PartialMessagesNotEnabledForTopic)
Just PartialTopicMode
_ -> do
eligible <- GossipSubRouter -> Topic -> IO [PeerId]
eligiblePeers GossipSubRouter
router Topic
topic
requesting <- filterMFor eligible $ \PeerId
pid ->
GossipSubRouter -> PeerId -> Topic -> IO Bool
peerRequestsPartial GossipSubRouter
router PeerId
pid Topic
topic
actions <- modifyMVar (pmeState extension) $ \PartialRuntimeState state
runtime ->
PartialMessagesExtension state
-> PartialRuntimeState state
-> Topic
-> ByteString
-> [PeerId]
-> [PeerId]
-> PartialPublishPlan state
-> IO (PartialRuntimeState state, [PartialPublishAction])
forall state.
PartialMessagesExtension state
-> PartialRuntimeState state
-> Topic
-> ByteString
-> [PeerId]
-> [PeerId]
-> PartialPublishPlan state
-> IO (PartialRuntimeState state, [PartialPublishAction])
publishGroup PartialMessagesExtension state
extension PartialRuntimeState state
runtime Topic
topic ByteString
groupId [PeerId]
eligible [PeerId]
requesting PartialPublishPlan state
plan
sendActions router topic groupId actions
pure (Right ())
publishGroup ::
PartialMessagesExtension state ->
PartialRuntimeState state ->
Topic ->
ByteString ->
[PeerId] ->
[PeerId] ->
PartialPublishPlan state ->
IO (PartialRuntimeState state, [PartialPublishAction])
publishGroup :: forall state.
PartialMessagesExtension state
-> PartialRuntimeState state
-> Topic
-> ByteString
-> [PeerId]
-> [PeerId]
-> PartialPublishPlan state
-> IO (PartialRuntimeState state, [PartialPublishAction])
publishGroup PartialMessagesExtension state
extension PartialRuntimeState state
runtime Topic
topic ByteString
groupId [PeerId]
eligible [PeerId]
requesting PartialPublishPlan state
plan = do
let config :: PartialMessagesConfig state
config = PartialMessagesExtension state -> PartialMessagesConfig state
forall state.
PartialMessagesExtension state -> PartialMessagesConfig state
pmeConfig PartialMessagesExtension state
extension
(PartialRuntimeState state
runtime', PartialGroupState state
group) = PartialMessagesConfig state
-> PartialRuntimeState state
-> Topic
-> ByteString
-> (PartialRuntimeState state, PartialGroupState state)
forall state.
PartialMessagesConfig state
-> PartialRuntimeState state
-> Topic
-> ByteString
-> (PartialRuntimeState state, PartialGroupState state)
localGroup PartialMessagesConfig state
config PartialRuntimeState state
runtime Topic
topic ByteString
groupId
peers :: Map PeerId state
peers = PartialMessagesConfig state
-> [PeerId] -> Map PeerId state -> Map PeerId state
forall state.
PartialMessagesConfig state
-> [PeerId] -> Map PeerId state -> Map PeerId state
initializePeers PartialMessagesConfig state
config [PeerId]
eligible (PartialGroupState state -> Map PeerId state
forall state. PartialGroupState state -> Map PeerId state
pgsPeerStates PartialGroupState state
group)
requested :: PeerId -> Bool
requested PeerId
pid = PeerId
pid PeerId -> [PeerId] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [PeerId]
requesting
result <- PartialPublishPlan state
plan Map PeerId state
peers PeerId -> Bool
requested
let updated =
PartialGroupState state
group
{ pgsPeerStates = partialUpdatedPeerStates result
, pgsTTL = groupTTL config
, pgsInitiatedBy = Nothing
}
finalState = Topic
-> ByteString
-> PartialGroupState state
-> PartialRuntimeState state
-> PartialRuntimeState state
forall state.
Topic
-> ByteString
-> PartialGroupState state
-> PartialRuntimeState state
-> PartialRuntimeState state
putGroup Topic
topic ByteString
groupId PartialGroupState state
updated PartialRuntimeState state
runtime'
pure (finalState, partialPublishActions result)
handleIncoming ::
PartialMessagesExtension state ->
GossipSubRouter ->
PeerId ->
PartialMessage ->
IO Bool
handleIncoming :: forall state.
PartialMessagesExtension state
-> GossipSubRouter -> PeerId -> PartialMessage -> IO Bool
handleIncoming PartialMessagesExtension state
extension GossipSubRouter
router PeerId
sender PartialMessage
partial = do
violation <- GossipSubRouter -> PartialMessage -> IO Bool
invalidIncoming GossipSubRouter
router PartialMessage
partial
if violation
then pure True
else do
eligible <- eligiblePeers router (partialTopicId partial)
outcome <- modifyMVar (pmeState extension) $ \PartialRuntimeState state
runtime ->
PartialMessagesExtension state
-> PartialRuntimeState state
-> PeerId
-> [PeerId]
-> PartialMessage
-> IO (PartialRuntimeState state, Either () [PartialPublishAction])
forall state.
PartialMessagesExtension state
-> PartialRuntimeState state
-> PeerId
-> [PeerId]
-> PartialMessage
-> IO (PartialRuntimeState state, Either () [PartialPublishAction])
receiveGroup PartialMessagesExtension state
extension PartialRuntimeState state
runtime PeerId
sender [PeerId]
eligible PartialMessage
partial
case outcome of
Left () -> Bool -> IO Bool
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
Right [PartialPublishAction]
actions -> do
GossipSubRouter
-> Topic -> ByteString -> [PartialPublishAction] -> IO ()
sendActions GossipSubRouter
router (PartialMessage -> Topic
partialTopicId PartialMessage
partial) (PartialMessage -> ByteString
partialGroupId PartialMessage
partial) [PartialPublishAction]
actions
Bool -> IO Bool
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
invalidIncoming :: GossipSubRouter -> PartialMessage -> IO Bool
invalidIncoming :: GossipSubRouter -> PartialMessage -> IO Bool
invalidIncoming GossipSubRouter
router PartialMessage
partial = do
mode <- GossipSubRouter -> Topic -> IO (Maybe PartialTopicMode)
localPartialMode GossipSubRouter
router (PartialMessage -> Topic
partialTopicId PartialMessage
partial)
pure $ case mode of
Maybe PartialTopicMode
Nothing -> Bool
True
Just PartialTopicMode
RequestsPartialMessages -> Bool
False
Just PartialTopicMode
SupportsPartialMessages -> Maybe ByteString -> Bool
forall a. Maybe a -> Bool
isJust (PartialMessage -> Maybe ByteString
partialMessageData PartialMessage
partial)
receiveGroup ::
PartialMessagesExtension state ->
PartialRuntimeState state ->
PeerId ->
[PeerId] ->
PartialMessage ->
IO (PartialRuntimeState state, Either () [PartialPublishAction])
receiveGroup :: forall state.
PartialMessagesExtension state
-> PartialRuntimeState state
-> PeerId
-> [PeerId]
-> PartialMessage
-> IO (PartialRuntimeState state, Either () [PartialPublishAction])
receiveGroup PartialMessagesExtension state
extension PartialRuntimeState state
runtime PeerId
sender [PeerId]
eligible PartialMessage
partial =
case PartialMessagesConfig state
-> PartialRuntimeState state
-> Topic
-> ByteString
-> PeerId
-> Either () (PartialRuntimeState state, PartialGroupState state)
forall state.
PartialMessagesConfig state
-> PartialRuntimeState state
-> Topic
-> ByteString
-> PeerId
-> Either () (PartialRuntimeState state, PartialGroupState state)
remoteGroup PartialMessagesConfig state
config PartialRuntimeState state
runtime Topic
topic ByteString
groupId PeerId
sender of
Left () -> (PartialRuntimeState state, Either () [PartialPublishAction])
-> IO (PartialRuntimeState state, Either () [PartialPublishAction])
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PartialRuntimeState state
runtime, () -> Either () [PartialPublishAction]
forall a b. a -> Either a b
Left ())
Right (PartialRuntimeState state
runtime', PartialGroupState state
group) -> do
let peers :: Map PeerId state
peers =
(state -> state -> state)
-> PeerId -> state -> Map PeerId state -> Map PeerId state
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith
(\state
_ state
old -> state
old)
PeerId
sender
(PartialMessagesConfig state -> state
forall state. PartialMessagesConfig state -> state
partialInitialPeerState PartialMessagesConfig state
config)
(PartialMessagesConfig state
-> [PeerId] -> Map PeerId state -> Map PeerId state
forall state.
PartialMessagesConfig state
-> [PeerId] -> Map PeerId state -> Map PeerId state
initializePeers PartialMessagesConfig state
config [PeerId]
eligible (PartialGroupState state -> Map PeerId state
forall state. PartialGroupState state -> Map PeerId state
pgsPeerStates PartialGroupState state
group))
result <- PartialMessagesConfig state
-> PeerId
-> Map PeerId state
-> PartialMessage
-> IO (PartialCallbackResult state)
forall state.
PartialMessagesConfig state
-> PeerId
-> Map PeerId state
-> PartialMessage
-> IO (PartialCallbackResult state)
partialOnIncoming PartialMessagesConfig state
config PeerId
sender Map PeerId state
peers PartialMessage
partial
let updated = PartialGroupState state
group{pgsPeerStates = partialUpdatedPeerStates result}
pure (putGroup topic groupId updated runtime', Right (partialPublishActions result))
where
config :: PartialMessagesConfig state
config = PartialMessagesExtension state -> PartialMessagesConfig state
forall state.
PartialMessagesExtension state -> PartialMessagesConfig state
pmeConfig PartialMessagesExtension state
extension
topic :: Topic
topic = PartialMessage -> Topic
partialTopicId PartialMessage
partial
groupId :: ByteString
groupId = PartialMessage -> ByteString
partialGroupId PartialMessage
partial
emitGossip ::
PartialMessagesExtension state ->
GossipSubRouter ->
Topic ->
[PeerId] ->
IO ()
emitGossip :: forall state.
PartialMessagesExtension state
-> GossipSubRouter -> Topic -> [PeerId] -> IO ()
emitGossip PartialMessagesExtension state
extension GossipSubRouter
router Topic
topic [PeerId]
candidates = do
eligible <- GossipSubRouter -> Topic -> [PeerId] -> IO [PeerId]
filterEligible GossipSubRouter
router Topic
topic [PeerId]
candidates
actions <- modifyMVar (pmeState extension) $ \PartialRuntimeState state
runtime ->
PartialMessagesExtension state
-> PartialRuntimeState state
-> Topic
-> [PeerId]
-> IO
(PartialRuntimeState state, [(ByteString, [PartialPublishAction])])
forall state.
PartialMessagesExtension state
-> PartialRuntimeState state
-> Topic
-> [PeerId]
-> IO
(PartialRuntimeState state, [(ByteString, [PartialPublishAction])])
gossipGroups PartialMessagesExtension state
extension PartialRuntimeState state
runtime Topic
topic [PeerId]
eligible
forM_ actions (uncurry (sendActions router topic))
gossipGroups ::
PartialMessagesExtension state ->
PartialRuntimeState state ->
Topic ->
[PeerId] ->
IO (PartialRuntimeState state, [(ByteString, [PartialPublishAction])])
gossipGroups :: forall state.
PartialMessagesExtension state
-> PartialRuntimeState state
-> Topic
-> [PeerId]
-> IO
(PartialRuntimeState state, [(ByteString, [PartialPublishAction])])
gossipGroups PartialMessagesExtension state
extension PartialRuntimeState state
runtime Topic
topic [PeerId]
eligible = do
let groups :: Map ByteString (PartialGroupState state)
groups = Map ByteString (PartialGroupState state)
-> Topic
-> Map Topic (Map ByteString (PartialGroupState state))
-> Map ByteString (PartialGroupState state)
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Map ByteString (PartialGroupState state)
forall k a. Map k a
Map.empty Topic
topic (PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
forall state.
PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
prsGroups PartialRuntimeState state
runtime)
(updated, sends) <-
((Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
-> (ByteString, PartialGroupState state)
-> IO
(Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])]))
-> (Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
-> [(ByteString, PartialGroupState state)]
-> IO
(Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM
(PartialMessagesExtension state
-> Topic
-> [PeerId]
-> (Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
-> (ByteString, PartialGroupState state)
-> IO
(Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
forall state.
PartialMessagesExtension state
-> Topic
-> [PeerId]
-> (Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
-> (ByteString, PartialGroupState state)
-> IO
(Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
gossipGroup PartialMessagesExtension state
extension Topic
topic [PeerId]
eligible)
(Map ByteString (PartialGroupState state)
groups, [])
(Map ByteString (PartialGroupState state)
-> [(ByteString, PartialGroupState state)]
forall k a. Map k a -> [(k, a)]
Map.toList Map ByteString (PartialGroupState state)
groups)
pure (runtime{prsGroups = Map.insert topic updated (prsGroups runtime)}, reverse sends)
gossipGroup ::
PartialMessagesExtension state ->
Topic ->
[PeerId] ->
(Map ByteString (PartialGroupState state), [(ByteString, [PartialPublishAction])]) ->
(ByteString, PartialGroupState state) ->
IO (Map ByteString (PartialGroupState state), [(ByteString, [PartialPublishAction])])
gossipGroup :: forall state.
PartialMessagesExtension state
-> Topic
-> [PeerId]
-> (Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
-> (ByteString, PartialGroupState state)
-> IO
(Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
gossipGroup PartialMessagesExtension state
extension Topic
topic [PeerId]
eligible (Map ByteString (PartialGroupState state)
groups, [(ByteString, [PartialPublishAction])]
sends) (ByteString
groupId, PartialGroupState state
group)
| Maybe PeerId -> Bool
forall a. Maybe a -> Bool
isJust (PartialGroupState state -> Maybe PeerId
forall state. PartialGroupState state -> Maybe PeerId
pgsInitiatedBy PartialGroupState state
group) = (Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
-> IO
(Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map ByteString (PartialGroupState state)
groups, [(ByteString, [PartialPublishAction])]
sends)
| [PeerId] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [PeerId]
newPeers = (Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
-> IO
(Map ByteString (PartialGroupState state),
[(ByteString, [PartialPublishAction])])
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map ByteString (PartialGroupState state)
groups, [(ByteString, [PartialPublishAction])]
sends)
| Bool
otherwise = do
let config :: PartialMessagesConfig state
config = PartialMessagesExtension state -> PartialMessagesConfig state
forall state.
PartialMessagesExtension state -> PartialMessagesConfig state
pmeConfig PartialMessagesExtension state
extension
peers :: Map PeerId state
peers = PartialMessagesConfig state
-> [PeerId] -> Map PeerId state -> Map PeerId state
forall state.
PartialMessagesConfig state
-> [PeerId] -> Map PeerId state -> Map PeerId state
initializePeers PartialMessagesConfig state
config [PeerId]
newPeers (PartialGroupState state -> Map PeerId state
forall state. PartialGroupState state -> Map PeerId state
pgsPeerStates PartialGroupState state
group)
result <- PartialMessagesConfig state
-> Topic
-> ByteString
-> [PeerId]
-> Map PeerId state
-> IO (PartialCallbackResult state)
forall state.
PartialMessagesConfig state
-> Topic
-> ByteString
-> [PeerId]
-> Map PeerId state
-> IO (PartialCallbackResult state)
partialOnEmitGossip PartialMessagesConfig state
config Topic
topic ByteString
groupId [PeerId]
newPeers Map PeerId state
peers
let updated = PartialGroupState state
group{pgsPeerStates = partialUpdatedPeerStates result}
pure
( Map.insert groupId updated groups
, (groupId, partialPublishActions result) : sends
)
where
newPeers :: [PeerId]
newPeers = (PeerId -> Bool) -> [PeerId] -> [PeerId]
forall a. (a -> Bool) -> [a] -> [a]
filter (PeerId -> Map PeerId state -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`Map.notMember` PartialGroupState state -> Map PeerId state
forall state. PartialGroupState state -> Map PeerId state
pgsPeerStates PartialGroupState state
group) [PeerId]
eligible
heartbeat :: PartialMessagesExtension state -> IO ()
heartbeat :: forall state. PartialMessagesExtension state -> IO ()
heartbeat PartialMessagesExtension state
extension = MVar (PartialRuntimeState state)
-> (PartialRuntimeState state -> IO (PartialRuntimeState state))
-> IO ()
forall a. MVar a -> (a -> IO a) -> IO ()
modifyMVar_ (PartialMessagesExtension state -> MVar (PartialRuntimeState state)
forall state.
PartialMessagesExtension state -> MVar (PartialRuntimeState state)
pmeState PartialMessagesExtension state
extension) ((PartialRuntimeState state -> IO (PartialRuntimeState state))
-> IO ())
-> (PartialRuntimeState state -> IO (PartialRuntimeState state))
-> IO ()
forall a b. (a -> b) -> a -> b
$ \PartialRuntimeState state
runtime ->
PartialRuntimeState state -> IO (PartialRuntimeState state)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PartialRuntimeState state -> PartialRuntimeState state
forall state.
PartialRuntimeState state -> PartialRuntimeState state
ageRuntime PartialRuntimeState state
runtime)
peerDisconnected :: PartialMessagesExtension state -> PeerId -> IO ()
peerDisconnected :: forall state. PartialMessagesExtension state -> PeerId -> IO ()
peerDisconnected PartialMessagesExtension state
extension PeerId
pid = MVar (PartialRuntimeState state)
-> (PartialRuntimeState state -> IO (PartialRuntimeState state))
-> IO ()
forall a. MVar a -> (a -> IO a) -> IO ()
modifyMVar_ (PartialMessagesExtension state -> MVar (PartialRuntimeState state)
forall state.
PartialMessagesExtension state -> MVar (PartialRuntimeState state)
pmeState PartialMessagesExtension state
extension) ((PartialRuntimeState state -> IO (PartialRuntimeState state))
-> IO ())
-> (PartialRuntimeState state -> IO (PartialRuntimeState state))
-> IO ()
forall a b. (a -> b) -> a -> b
$ \PartialRuntimeState state
runtime ->
PartialRuntimeState state -> IO (PartialRuntimeState state)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PeerId -> PartialRuntimeState state -> PartialRuntimeState state
forall state.
PeerId -> PartialRuntimeState state -> PartialRuntimeState state
dropDisconnectedPeer PeerId
pid PartialRuntimeState state
runtime)
dropDisconnectedPeer ::
PeerId -> PartialRuntimeState state -> PartialRuntimeState state
dropDisconnectedPeer :: forall state.
PeerId -> PartialRuntimeState state -> PartialRuntimeState state
dropDisconnectedPeer PeerId
pid PartialRuntimeState state
runtime =
((Topic, Map ByteString (PartialGroupState state))
-> PartialRuntimeState state -> PartialRuntimeState state)
-> PartialRuntimeState state
-> [(Topic, Map ByteString (PartialGroupState state))]
-> PartialRuntimeState state
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Topic, Map ByteString (PartialGroupState state))
-> PartialRuntimeState state -> PartialRuntimeState state
forall state.
(Topic, Map ByteString (PartialGroupState state))
-> PartialRuntimeState state -> PartialRuntimeState state
cleanTopic PartialRuntimeState state
withoutPeer (Map Topic (Map ByteString (PartialGroupState state))
-> [(Topic, Map ByteString (PartialGroupState state))]
forall k a. Map k a -> [(k, a)]
Map.toList (PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
forall state.
PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
prsGroups PartialRuntimeState state
withoutPeer))
where
withoutPeer :: PartialRuntimeState state
withoutPeer =
PartialRuntimeState state
runtime
{ prsGroups = Map.map (Map.map dropPeer) (prsGroups runtime)
, prsCounters = Map.map (removePeerCount pid) (prsCounters runtime)
}
dropPeer :: PartialGroupState state -> PartialGroupState state
dropPeer PartialGroupState state
group = PartialGroupState state
group{pgsPeerStates = Map.delete pid (pgsPeerStates group)}
cleanTopic ::
(Topic, Map ByteString (PartialGroupState state)) ->
PartialRuntimeState state ->
PartialRuntimeState state
cleanTopic :: forall state.
(Topic, Map ByteString (PartialGroupState state))
-> PartialRuntimeState state -> PartialRuntimeState state
cleanTopic (Topic
topic, Map ByteString (PartialGroupState state)
groups) PartialRuntimeState state
runtime =
let (PartialRuntimeState state
runtime', Map ByteString (PartialGroupState state)
live) =
(ByteString
-> PartialGroupState state
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state)))
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
-> Map ByteString (PartialGroupState state)
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey
(Topic
-> ByteString
-> PartialGroupState state
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
forall state.
Topic
-> ByteString
-> PartialGroupState state
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
keepNonEmpty Topic
topic)
(PartialRuntimeState state
runtime, Map ByteString (PartialGroupState state)
forall k a. Map k a
Map.empty)
Map ByteString (PartialGroupState state)
groups
in if Map ByteString (PartialGroupState state) -> Bool
forall k a. Map k a -> Bool
Map.null Map ByteString (PartialGroupState state)
live
then
PartialRuntimeState state
runtime'
{ prsGroups = Map.delete topic (prsGroups runtime')
, prsCounters = Map.delete topic (prsCounters runtime')
}
else PartialRuntimeState state
runtime'{prsGroups = Map.insert topic live (prsGroups runtime')}
keepNonEmpty ::
Topic ->
ByteString ->
PartialGroupState state ->
(PartialRuntimeState state, Map ByteString (PartialGroupState state)) ->
(PartialRuntimeState state, Map ByteString (PartialGroupState state))
keepNonEmpty :: forall state.
Topic
-> ByteString
-> PartialGroupState state
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
keepNonEmpty Topic
topic ByteString
groupId PartialGroupState state
group (PartialRuntimeState state
runtime, Map ByteString (PartialGroupState state)
live)
| Map PeerId state -> Bool
forall k a. Map k a -> Bool
Map.null (PartialGroupState state -> Map PeerId state
forall state. PartialGroupState state -> Map PeerId state
pgsPeerStates PartialGroupState state
group) =
(PartialRuntimeState state
-> (PeerId -> PartialRuntimeState state)
-> Maybe PeerId
-> PartialRuntimeState state
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PartialRuntimeState state
runtime (\PeerId
pid -> Topic
-> PeerId -> PartialRuntimeState state -> PartialRuntimeState state
forall state.
Topic
-> PeerId -> PartialRuntimeState state -> PartialRuntimeState state
decrementCounter Topic
topic PeerId
pid PartialRuntimeState state
runtime) (PartialGroupState state -> Maybe PeerId
forall state. PartialGroupState state -> Maybe PeerId
pgsInitiatedBy PartialGroupState state
group), Map ByteString (PartialGroupState state)
live)
| Bool
otherwise = (PartialRuntimeState state
runtime, ByteString
-> PartialGroupState state
-> Map ByteString (PartialGroupState state)
-> Map ByteString (PartialGroupState state)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert ByteString
groupId PartialGroupState state
group Map ByteString (PartialGroupState state)
live)
eligiblePeers :: GossipSubRouter -> Topic -> IO [PeerId]
eligiblePeers :: GossipSubRouter -> Topic -> IO [PeerId]
eligiblePeers GossipSubRouter
router Topic
topic = do
mesh <- TVar (Map Topic (Set PeerId)) -> IO (Map Topic (Set PeerId))
forall a. TVar a -> IO a
readTVarIO (GossipSubRouter -> TVar (Map Topic (Set PeerId))
gsMesh GossipSubRouter
router)
fanout <- readTVarIO (gsFanout router)
let meshPeers = Set PeerId -> Topic -> Map Topic (Set PeerId) -> Set PeerId
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Set PeerId
forall a. Set a
Set.empty Topic
topic Map Topic (Set PeerId)
mesh
candidates =
if Set PeerId -> Bool
forall a. Set a -> Bool
Set.null Set PeerId
meshPeers
then Set PeerId -> Topic -> Map Topic (Set PeerId) -> Set PeerId
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Set PeerId
forall a. Set a
Set.empty Topic
topic Map Topic (Set PeerId)
fanout
else Set PeerId
meshPeers
filterEligible router topic (Set.toList candidates)
filterEligible :: GossipSubRouter -> Topic -> [PeerId] -> IO [PeerId]
filterEligible :: GossipSubRouter -> Topic -> [PeerId] -> IO [PeerId]
filterEligible GossipSubRouter
router Topic
topic [PeerId]
peers = do
mode <- GossipSubRouter -> Topic -> IO (Maybe PartialTopicMode)
localPartialMode GossipSubRouter
router Topic
topic
filterMFor peers $ \PeerId
pid -> do
extension <- GossipSubRouter -> PeerId -> IO Bool
peerSupportsPartialExtension GossipSubRouter
router PeerId
pid
relation <- case mode of
Just PartialTopicMode
RequestsPartialMessages -> GossipSubRouter -> PeerId -> Topic -> IO Bool
peerSupportsSendingPartial GossipSubRouter
router PeerId
pid Topic
topic
Just PartialTopicMode
SupportsPartialMessages -> GossipSubRouter -> PeerId -> Topic -> IO Bool
peerRequestsPartial GossipSubRouter
router PeerId
pid Topic
topic
Maybe PartialTopicMode
Nothing -> Bool -> IO Bool
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
pure (extension && relation)
filterMFor :: [a] -> (a -> IO Bool) -> IO [a]
filterMFor :: forall a. [a] -> (a -> IO Bool) -> IO [a]
filterMFor [a]
values a -> IO Bool
predicate = do
pairs <- [a] -> (a -> IO (a, Bool)) -> IO [(a, Bool)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [a]
values ((a -> IO (a, Bool)) -> IO [(a, Bool)])
-> (a -> IO (a, Bool)) -> IO [(a, Bool)]
forall a b. (a -> b) -> a -> b
$ \a
value -> do
keep <- a -> IO Bool
predicate a
value
pure (value, keep)
pure [value | (value, keep) <- pairs, keep]
sendActions ::
GossipSubRouter -> Topic -> ByteString -> [PartialPublishAction] -> IO ()
sendActions :: GossipSubRouter
-> Topic -> ByteString -> [PartialPublishAction] -> IO ()
sendActions GossipSubRouter
router Topic
topic ByteString
groupId [PartialPublishAction]
actions =
[PartialPublishAction] -> (PartialPublishAction -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [PartialPublishAction]
actions ((PartialPublishAction -> IO ()) -> IO ())
-> (PartialPublishAction -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \PartialPublishAction
action -> do
extension <- GossipSubRouter -> PeerId -> IO Bool
peerSupportsPartialExtension GossipSubRouter
router (PartialPublishAction -> PeerId
partialActionPeer PartialPublishAction
action)
requests <- peerRequestsPartial router (partialActionPeer action) topic
supports <- peerSupportsSendingPartial router (partialActionPeer action) topic
let message = if Bool
extension Bool -> Bool -> Bool
&& Bool
requests then PartialPublishAction -> Maybe ByteString
partialActionMessage PartialPublishAction
action else Maybe ByteString
forall a. Maybe a
Nothing
metadata = if Bool
extension Bool -> Bool -> Bool
&& Bool
supports then PartialPublishAction -> Maybe ByteString
partialActionMetadata PartialPublishAction
action else Maybe ByteString
forall a. Maybe a
Nothing
unless (isNothing message && isNothing metadata) $
gsSendRPC
router
(partialActionPeer action)
emptyRPC
{ rpcPartial =
Just
PartialMessage
{ partialTopicId = topic
, partialGroupId = groupId
, partialMessageData = message
, partialPartsMetadata = metadata
}
}
localGroup ::
PartialMessagesConfig state ->
PartialRuntimeState state ->
Topic ->
ByteString ->
(PartialRuntimeState state, PartialGroupState state)
localGroup :: forall state.
PartialMessagesConfig state
-> PartialRuntimeState state
-> Topic
-> ByteString
-> (PartialRuntimeState state, PartialGroupState state)
localGroup PartialMessagesConfig state
config PartialRuntimeState state
runtime Topic
topic ByteString
groupId =
case PartialRuntimeState state
-> Topic -> ByteString -> Maybe (PartialGroupState state)
forall state.
PartialRuntimeState state
-> Topic -> ByteString -> Maybe (PartialGroupState state)
lookupGroup PartialRuntimeState state
runtime Topic
topic ByteString
groupId of
Just PartialGroupState state
group
| Just PeerId
initiator <- PartialGroupState state -> Maybe PeerId
forall state. PartialGroupState state -> Maybe PeerId
pgsInitiatedBy PartialGroupState state
group ->
(Topic
-> PeerId -> PartialRuntimeState state -> PartialRuntimeState state
forall state.
Topic
-> PeerId -> PartialRuntimeState state -> PartialRuntimeState state
decrementCounter Topic
topic PeerId
initiator PartialRuntimeState state
runtime, PartialGroupState state
group)
Just PartialGroupState state
group -> (PartialRuntimeState state
runtime, PartialGroupState state
group)
Maybe (PartialGroupState state)
Nothing -> (PartialRuntimeState state
runtime, PartialMessagesConfig state
-> Maybe PeerId -> PartialGroupState state
forall state.
PartialMessagesConfig state
-> Maybe PeerId -> PartialGroupState state
emptyGroup PartialMessagesConfig state
config Maybe PeerId
forall a. Maybe a
Nothing)
remoteGroup ::
PartialMessagesConfig state ->
PartialRuntimeState state ->
Topic ->
ByteString ->
PeerId ->
Either () (PartialRuntimeState state, PartialGroupState state)
remoteGroup :: forall state.
PartialMessagesConfig state
-> PartialRuntimeState state
-> Topic
-> ByteString
-> PeerId
-> Either () (PartialRuntimeState state, PartialGroupState state)
remoteGroup PartialMessagesConfig state
config PartialRuntimeState state
runtime Topic
topic ByteString
groupId PeerId
sender =
case PartialRuntimeState state
-> Topic -> ByteString -> Maybe (PartialGroupState state)
forall state.
PartialRuntimeState state
-> Topic -> ByteString -> Maybe (PartialGroupState state)
lookupGroup PartialRuntimeState state
runtime Topic
topic ByteString
groupId of
Just PartialGroupState state
group -> (PartialRuntimeState state, PartialGroupState state)
-> Either () (PartialRuntimeState state, PartialGroupState state)
forall a b. b -> Either a b
Right (PartialRuntimeState state
runtime, PartialGroupState state
group)
Maybe (PartialGroupState state)
Nothing -> do
counter <-
PartialMessagesConfig state
-> PeerId -> PeerGroupCounter -> Either () PeerGroupCounter
forall state.
PartialMessagesConfig state
-> PeerId -> PeerGroupCounter -> Either () PeerGroupCounter
incrementCounter
PartialMessagesConfig state
config
PeerId
sender
(PeerGroupCounter
-> Topic -> Map Topic PeerGroupCounter -> PeerGroupCounter
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault PeerGroupCounter
emptyCounter Topic
topic (PartialRuntimeState state -> Map Topic PeerGroupCounter
forall state.
PartialRuntimeState state -> Map Topic PeerGroupCounter
prsCounters PartialRuntimeState state
runtime))
let updated =
PartialRuntimeState state
runtime
{ prsCounters = Map.insert topic counter (prsCounters runtime)
}
Right (updated, emptyGroup config (Just sender))
lookupGroup ::
PartialRuntimeState state -> Topic -> ByteString -> Maybe (PartialGroupState state)
lookupGroup :: forall state.
PartialRuntimeState state
-> Topic -> ByteString -> Maybe (PartialGroupState state)
lookupGroup PartialRuntimeState state
runtime Topic
topic ByteString
groupId =
Topic
-> Map Topic (Map ByteString (PartialGroupState state))
-> Maybe (Map ByteString (PartialGroupState state))
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Topic
topic (PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
forall state.
PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
prsGroups PartialRuntimeState state
runtime) Maybe (Map ByteString (PartialGroupState state))
-> (Map ByteString (PartialGroupState state)
-> Maybe (PartialGroupState state))
-> Maybe (PartialGroupState state)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString
-> Map ByteString (PartialGroupState state)
-> Maybe (PartialGroupState state)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ByteString
groupId
putGroup ::
Topic ->
ByteString ->
PartialGroupState state ->
PartialRuntimeState state ->
PartialRuntimeState state
putGroup :: forall state.
Topic
-> ByteString
-> PartialGroupState state
-> PartialRuntimeState state
-> PartialRuntimeState state
putGroup Topic
topic ByteString
groupId PartialGroupState state
group PartialRuntimeState state
runtime =
PartialRuntimeState state
runtime
{ prsGroups = Map.alter insertTopic topic (prsGroups runtime)
}
where
insertTopic :: Maybe (Map ByteString (PartialGroupState state))
-> Maybe (Map ByteString (PartialGroupState state))
insertTopic = Map ByteString (PartialGroupState state)
-> Maybe (Map ByteString (PartialGroupState state))
forall a. a -> Maybe a
Just (Map ByteString (PartialGroupState state)
-> Maybe (Map ByteString (PartialGroupState state)))
-> (Maybe (Map ByteString (PartialGroupState state))
-> Map ByteString (PartialGroupState state))
-> Maybe (Map ByteString (PartialGroupState state))
-> Maybe (Map ByteString (PartialGroupState state))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString
-> PartialGroupState state
-> Map ByteString (PartialGroupState state)
-> Map ByteString (PartialGroupState state)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert ByteString
groupId PartialGroupState state
group (Map ByteString (PartialGroupState state)
-> Map ByteString (PartialGroupState state))
-> (Maybe (Map ByteString (PartialGroupState state))
-> Map ByteString (PartialGroupState state))
-> Maybe (Map ByteString (PartialGroupState state))
-> Map ByteString (PartialGroupState state)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map ByteString (PartialGroupState state)
-> Maybe (Map ByteString (PartialGroupState state))
-> Map ByteString (PartialGroupState state)
forall a. a -> Maybe a -> a
fromMaybe Map ByteString (PartialGroupState state)
forall k a. Map k a
Map.empty
emptyGroup ::
PartialMessagesConfig state -> Maybe PeerId -> PartialGroupState state
emptyGroup :: forall state.
PartialMessagesConfig state
-> Maybe PeerId -> PartialGroupState state
emptyGroup PartialMessagesConfig state
config = Map PeerId state -> Int -> Maybe PeerId -> PartialGroupState state
forall state.
Map PeerId state -> Int -> Maybe PeerId -> PartialGroupState state
PartialGroupState Map PeerId state
forall k a. Map k a
Map.empty (PartialMessagesConfig state -> Int
forall state. PartialMessagesConfig state -> Int
groupTTL PartialMessagesConfig state
config)
groupTTL :: PartialMessagesConfig state -> Int
groupTTL :: forall state. PartialMessagesConfig state -> Int
groupTTL = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
3 (Int -> Int)
-> (PartialMessagesConfig state -> Int)
-> PartialMessagesConfig state
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PartialMessagesConfig state -> Int
forall state. PartialMessagesConfig state -> Int
partialGroupTTLHeartbeats
initializePeers ::
PartialMessagesConfig state -> [PeerId] -> Map PeerId state -> Map PeerId state
initializePeers :: forall state.
PartialMessagesConfig state
-> [PeerId] -> Map PeerId state -> Map PeerId state
initializePeers PartialMessagesConfig state
config [PeerId]
peers Map PeerId state
states = (PeerId -> Map PeerId state -> Map PeerId state)
-> Map PeerId state -> [PeerId] -> Map PeerId state
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr PeerId -> Map PeerId state -> Map PeerId state
forall {k}. Ord k => k -> Map k state -> Map k state
insertInitial Map PeerId state
states [PeerId]
peers
where
insertInitial :: k -> Map k state -> Map k state
insertInitial k
pid = (state -> state -> state)
-> k -> state -> Map k state -> Map k state
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith (\state
_ state
old -> state
old) k
pid (PartialMessagesConfig state -> state
forall state. PartialMessagesConfig state -> state
partialInitialPeerState PartialMessagesConfig state
config)
incrementCounter ::
PartialMessagesConfig state -> PeerId -> PeerGroupCounter -> Either () PeerGroupCounter
incrementCounter :: forall state.
PartialMessagesConfig state
-> PeerId -> PeerGroupCounter -> Either () PeerGroupCounter
incrementCounter PartialMessagesConfig state
config PeerId
pid PeerGroupCounter
counter
| PeerGroupCounter -> Int
pgcTotal PeerGroupCounter
counter Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (PartialMessagesConfig state -> Int
forall state. PartialMessagesConfig state -> Int
partialPeerGroupLimitPerTopic PartialMessagesConfig state
config) = () -> Either () PeerGroupCounter
forall a b. a -> Either a b
Left ()
| Int
peerCount Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (PartialMessagesConfig state -> Int
forall state. PartialMessagesConfig state -> Int
partialPeerGroupLimitPerTopicPerPeer PartialMessagesConfig state
config) = () -> Either () PeerGroupCounter
forall a b. a -> Either a b
Left ()
| Bool
otherwise =
PeerGroupCounter -> Either () PeerGroupCounter
forall a b. b -> Either a b
Right
PeerGroupCounter
counter
{ pgcTotal = pgcTotal counter + 1
, pgcPerPeer = Map.insert pid (peerCount + 1) (pgcPerPeer counter)
}
where
peerCount :: Int
peerCount = Int -> PeerId -> Map PeerId Int -> Int
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Int
0 PeerId
pid (PeerGroupCounter -> Map PeerId Int
pgcPerPeer PeerGroupCounter
counter)
decrementCounter ::
Topic -> PeerId -> PartialRuntimeState state -> PartialRuntimeState state
decrementCounter :: forall state.
Topic
-> PeerId -> PartialRuntimeState state -> PartialRuntimeState state
decrementCounter Topic
topic PeerId
pid PartialRuntimeState state
runtime =
PartialRuntimeState state
runtime
{ prsCounters = Map.adjust (removeOne pid) topic (prsCounters runtime)
}
removeOne :: PeerId -> PeerGroupCounter -> PeerGroupCounter
removeOne :: PeerId -> PeerGroupCounter -> PeerGroupCounter
removeOne PeerId
pid PeerGroupCounter
counter =
case PeerId -> Map PeerId Int -> Maybe Int
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup PeerId
pid (PeerGroupCounter -> Map PeerId Int
pgcPerPeer PeerGroupCounter
counter) of
Maybe Int
Nothing -> PeerGroupCounter
counter
Just Int
count ->
PeerGroupCounter
counter
{ pgcTotal = max 0 (pgcTotal counter - 1)
, pgcPerPeer =
if count <= 1
then Map.delete pid (pgcPerPeer counter)
else Map.insert pid (count - 1) (pgcPerPeer counter)
}
removePeerCount :: PeerId -> PeerGroupCounter -> PeerGroupCounter
removePeerCount :: PeerId -> PeerGroupCounter -> PeerGroupCounter
removePeerCount PeerId
pid PeerGroupCounter
counter =
PeerGroupCounter
counter
{ pgcTotal = max 0 (pgcTotal counter - count)
, pgcPerPeer = Map.delete pid (pgcPerPeer counter)
}
where
count :: Int
count = Int -> PeerId -> Map PeerId Int -> Int
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Int
0 PeerId
pid (PeerGroupCounter -> Map PeerId Int
pgcPerPeer PeerGroupCounter
counter)
ageRuntime :: PartialRuntimeState state -> PartialRuntimeState state
ageRuntime :: forall state.
PartialRuntimeState state -> PartialRuntimeState state
ageRuntime PartialRuntimeState state
runtime = ((Topic, Map ByteString (PartialGroupState state))
-> PartialRuntimeState state -> PartialRuntimeState state)
-> PartialRuntimeState state
-> [(Topic, Map ByteString (PartialGroupState state))]
-> PartialRuntimeState state
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Topic, Map ByteString (PartialGroupState state))
-> PartialRuntimeState state -> PartialRuntimeState state
forall state.
(Topic, Map ByteString (PartialGroupState state))
-> PartialRuntimeState state -> PartialRuntimeState state
ageTopic PartialRuntimeState state
runtime (Map Topic (Map ByteString (PartialGroupState state))
-> [(Topic, Map ByteString (PartialGroupState state))]
forall k a. Map k a -> [(k, a)]
Map.toList (PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
forall state.
PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
prsGroups PartialRuntimeState state
runtime))
ageTopic ::
(Topic, Map ByteString (PartialGroupState state)) ->
PartialRuntimeState state ->
PartialRuntimeState state
ageTopic :: forall state.
(Topic, Map ByteString (PartialGroupState state))
-> PartialRuntimeState state -> PartialRuntimeState state
ageTopic (Topic
topic, Map ByteString (PartialGroupState state)
groups) PartialRuntimeState state
runtime =
let (PartialRuntimeState state
runtime', Map ByteString (PartialGroupState state)
live) = (ByteString
-> PartialGroupState state
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state)))
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
-> Map ByteString (PartialGroupState state)
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey (Topic
-> ByteString
-> PartialGroupState state
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
forall state.
Topic
-> ByteString
-> PartialGroupState state
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
ageGroup Topic
topic) (PartialRuntimeState state
runtime, Map ByteString (PartialGroupState state)
forall k a. Map k a
Map.empty) Map ByteString (PartialGroupState state)
groups
groupMap :: Map Topic (Map ByteString (PartialGroupState state))
groupMap =
if Map ByteString (PartialGroupState state) -> Bool
forall k a. Map k a -> Bool
Map.null Map ByteString (PartialGroupState state)
live
then Topic
-> Map Topic (Map ByteString (PartialGroupState state))
-> Map Topic (Map ByteString (PartialGroupState state))
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete Topic
topic (PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
forall state.
PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
prsGroups PartialRuntimeState state
runtime')
else Topic
-> Map ByteString (PartialGroupState state)
-> Map Topic (Map ByteString (PartialGroupState state))
-> Map Topic (Map ByteString (PartialGroupState state))
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Topic
topic Map ByteString (PartialGroupState state)
live (PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
forall state.
PartialRuntimeState state
-> Map Topic (Map ByteString (PartialGroupState state))
prsGroups PartialRuntimeState state
runtime')
counters :: Map Topic PeerGroupCounter
counters =
if Map ByteString (PartialGroupState state) -> Bool
forall k a. Map k a -> Bool
Map.null Map ByteString (PartialGroupState state)
live
then Topic -> Map Topic PeerGroupCounter -> Map Topic PeerGroupCounter
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete Topic
topic (PartialRuntimeState state -> Map Topic PeerGroupCounter
forall state.
PartialRuntimeState state -> Map Topic PeerGroupCounter
prsCounters PartialRuntimeState state
runtime')
else PartialRuntimeState state -> Map Topic PeerGroupCounter
forall state.
PartialRuntimeState state -> Map Topic PeerGroupCounter
prsCounters PartialRuntimeState state
runtime'
in PartialRuntimeState state
runtime'{prsGroups = groupMap, prsCounters = counters}
ageGroup ::
Topic ->
ByteString ->
PartialGroupState state ->
(PartialRuntimeState state, Map ByteString (PartialGroupState state)) ->
(PartialRuntimeState state, Map ByteString (PartialGroupState state))
ageGroup :: forall state.
Topic
-> ByteString
-> PartialGroupState state
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
-> (PartialRuntimeState state,
Map ByteString (PartialGroupState state))
ageGroup Topic
topic ByteString
groupId PartialGroupState state
group (PartialRuntimeState state
runtime, Map ByteString (PartialGroupState state)
live)
| PartialGroupState state -> Int
forall state. PartialGroupState state -> Int
pgsTTL PartialGroupState state
group Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 Bool -> Bool -> Bool
|| Map PeerId state -> Bool
forall k a. Map k a -> Bool
Map.null (PartialGroupState state -> Map PeerId state
forall state. PartialGroupState state -> Map PeerId state
pgsPeerStates PartialGroupState state
group) =
(PartialRuntimeState state
-> (PeerId -> PartialRuntimeState state)
-> Maybe PeerId
-> PartialRuntimeState state
forall b a. b -> (a -> b) -> Maybe a -> b
maybe PartialRuntimeState state
runtime (\PeerId
pid -> Topic
-> PeerId -> PartialRuntimeState state -> PartialRuntimeState state
forall state.
Topic
-> PeerId -> PartialRuntimeState state -> PartialRuntimeState state
decrementCounter Topic
topic PeerId
pid PartialRuntimeState state
runtime) (PartialGroupState state -> Maybe PeerId
forall state. PartialGroupState state -> Maybe PeerId
pgsInitiatedBy PartialGroupState state
group), Map ByteString (PartialGroupState state)
live)
| Bool
otherwise =
(PartialRuntimeState state
runtime, ByteString
-> PartialGroupState state
-> Map ByteString (PartialGroupState state)
-> Map ByteString (PartialGroupState state)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert ByteString
groupId PartialGroupState state
group{pgsTTL = pgsTTL group - 1} Map ByteString (PartialGroupState state)
live)
emptyCounter :: PeerGroupCounter
emptyCounter :: PeerGroupCounter
emptyCounter = Int -> Map PeerId Int -> PeerGroupCounter
PeerGroupCounter Int
0 Map PeerId Int
forall k a. Map k a
Map.empty