{-# LANGUAGE DeriveGeneric #-}

{-# OPTIONS_GHC -Wno-orphans #-}
{-# LANGUAGE InstanceSigs  #-}

module Ouroboros.Network.PeerSelection.PeerSharing (PeerSharing (..)) where

import GHC.Generics (Generic)

-- | Is a peer willing to participate in Peer Sharing? If yes are others allowed
-- to share this peer's address?
-- Information about the node comes from the configuration file, while information about
-- other nodes is received via handshake.
--
-- NOTE: This information is only useful if P2P flag is enabled.
--
data PeerSharing = PeerSharingDisabled -- ^ Peer does not participate in Peer Sharing
                                       -- at all
                 | PeerSharingEnabled -- ^ Peer participates in Peer Sharing
  deriving  (PeerSharing -> PeerSharing -> Bool
(PeerSharing -> PeerSharing -> Bool)
-> (PeerSharing -> PeerSharing -> Bool) -> Eq PeerSharing
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PeerSharing -> PeerSharing -> Bool
== :: PeerSharing -> PeerSharing -> Bool
$c/= :: PeerSharing -> PeerSharing -> Bool
/= :: PeerSharing -> PeerSharing -> Bool
Eq, Int -> PeerSharing -> ShowS
[PeerSharing] -> ShowS
PeerSharing -> String
(Int -> PeerSharing -> ShowS)
-> (PeerSharing -> String)
-> ([PeerSharing] -> ShowS)
-> Show PeerSharing
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PeerSharing -> ShowS
showsPrec :: Int -> PeerSharing -> ShowS
$cshow :: PeerSharing -> String
show :: PeerSharing -> String
$cshowList :: [PeerSharing] -> ShowS
showList :: [PeerSharing] -> ShowS
Show, ReadPrec [PeerSharing]
ReadPrec PeerSharing
Int -> ReadS PeerSharing
ReadS [PeerSharing]
(Int -> ReadS PeerSharing)
-> ReadS [PeerSharing]
-> ReadPrec PeerSharing
-> ReadPrec [PeerSharing]
-> Read PeerSharing
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS PeerSharing
readsPrec :: Int -> ReadS PeerSharing
$creadList :: ReadS [PeerSharing]
readList :: ReadS [PeerSharing]
$creadPrec :: ReadPrec PeerSharing
readPrec :: ReadPrec PeerSharing
$creadListPrec :: ReadPrec [PeerSharing]
readListPrec :: ReadPrec [PeerSharing]
Read, (forall x. PeerSharing -> Rep PeerSharing x)
-> (forall x. Rep PeerSharing x -> PeerSharing)
-> Generic PeerSharing
forall x. Rep PeerSharing x -> PeerSharing
forall x. PeerSharing -> Rep PeerSharing x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PeerSharing -> Rep PeerSharing x
from :: forall x. PeerSharing -> Rep PeerSharing x
$cto :: forall x. Rep PeerSharing x -> PeerSharing
to :: forall x. Rep PeerSharing x -> PeerSharing
Generic)

-- | The combination of two 'PeerSharing' values forms a Monoid where the unit
-- is 'PeerSharingEnabled'.
--
-- This operation is used in the connection handshake.
--
instance Semigroup PeerSharing where
  (<>) :: PeerSharing -> PeerSharing -> PeerSharing
  PeerSharing
PeerSharingDisabled <> :: PeerSharing -> PeerSharing -> PeerSharing
<> PeerSharing
_                   = PeerSharing
PeerSharingDisabled
  PeerSharing
_                   <> PeerSharing
PeerSharingDisabled = PeerSharing
PeerSharingDisabled
  PeerSharing
_                   <> PeerSharing
_                   = PeerSharing
PeerSharingEnabled

-- | The Monoid laws are witnessed by the following denotation function:
--
-- ⟦_⟧ :: PeerSharing -> All
-- ⟦ PeerSharingDisabled ⟧ = All False
-- ⟦ PeerSharingEnabled ⟧  = All True
--
instance Monoid PeerSharing where
  mempty :: PeerSharing
  mempty :: PeerSharing
mempty = PeerSharing
PeerSharingEnabled