module Test.ThreadNet.Util.NodeToNodeVersion (
    genVersion
  , genVersionFiltered
  , newestVersion
  ) where

import qualified Data.Map.Strict as Map
import           Data.Proxy (Proxy (..))
import           Ouroboros.Consensus.Node.NetworkProtocolVersion
import           Test.QuickCheck (Gen)
import           Test.Util.QuickCheck

genVersion ::
     SupportedNetworkProtocolVersion blk
  => Proxy blk -> Gen (NodeToNodeVersion, BlockNodeToNodeVersion blk)
genVersion :: forall blk.
SupportedNetworkProtocolVersion blk =>
Proxy blk -> Gen (NodeToNodeVersion, BlockNodeToNodeVersion blk)
genVersion = (BlockNodeToNodeVersion blk -> Bool)
-> Proxy blk -> Gen (NodeToNodeVersion, BlockNodeToNodeVersion blk)
forall blk.
SupportedNetworkProtocolVersion blk =>
(BlockNodeToNodeVersion blk -> Bool)
-> Proxy blk -> Gen (NodeToNodeVersion, BlockNodeToNodeVersion blk)
genVersionFiltered (Bool -> BlockNodeToNodeVersion blk -> Bool
forall a b. a -> b -> a
const Bool
True)

genVersionFiltered ::
    SupportedNetworkProtocolVersion blk
  => (BlockNodeToNodeVersion blk -> Bool)
  -> Proxy blk
  -> Gen (NodeToNodeVersion, BlockNodeToNodeVersion blk)
genVersionFiltered :: forall blk.
SupportedNetworkProtocolVersion blk =>
(BlockNodeToNodeVersion blk -> Bool)
-> Proxy blk -> Gen (NodeToNodeVersion, BlockNodeToNodeVersion blk)
genVersionFiltered BlockNodeToNodeVersion blk -> Bool
f =
      [(NodeToNodeVersion, BlockNodeToNodeVersion blk)]
-> Gen (NodeToNodeVersion, BlockNodeToNodeVersion blk)
forall a. HasCallStack => [a] -> Gen a
elements
    ([(NodeToNodeVersion, BlockNodeToNodeVersion blk)]
 -> Gen (NodeToNodeVersion, BlockNodeToNodeVersion blk))
-> (Proxy blk -> [(NodeToNodeVersion, BlockNodeToNodeVersion blk)])
-> Proxy blk
-> Gen (NodeToNodeVersion, BlockNodeToNodeVersion blk)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((NodeToNodeVersion, BlockNodeToNodeVersion blk) -> Bool)
-> [(NodeToNodeVersion, BlockNodeToNodeVersion blk)]
-> [(NodeToNodeVersion, BlockNodeToNodeVersion blk)]
forall a. (a -> Bool) -> [a] -> [a]
filter (BlockNodeToNodeVersion blk -> Bool
f (BlockNodeToNodeVersion blk -> Bool)
-> ((NodeToNodeVersion, BlockNodeToNodeVersion blk)
    -> BlockNodeToNodeVersion blk)
-> (NodeToNodeVersion, BlockNodeToNodeVersion blk)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NodeToNodeVersion, BlockNodeToNodeVersion blk)
-> BlockNodeToNodeVersion blk
forall a b. (a, b) -> b
snd)
    ([(NodeToNodeVersion, BlockNodeToNodeVersion blk)]
 -> [(NodeToNodeVersion, BlockNodeToNodeVersion blk)])
-> (Proxy blk -> [(NodeToNodeVersion, BlockNodeToNodeVersion blk)])
-> Proxy blk
-> [(NodeToNodeVersion, BlockNodeToNodeVersion blk)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map NodeToNodeVersion (BlockNodeToNodeVersion blk)
-> [(NodeToNodeVersion, BlockNodeToNodeVersion blk)]
forall k a. Map k a -> [(k, a)]
Map.toList
    (Map NodeToNodeVersion (BlockNodeToNodeVersion blk)
 -> [(NodeToNodeVersion, BlockNodeToNodeVersion blk)])
-> (Proxy blk
    -> Map NodeToNodeVersion (BlockNodeToNodeVersion blk))
-> Proxy blk
-> [(NodeToNodeVersion, BlockNodeToNodeVersion blk)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy blk -> Map NodeToNodeVersion (BlockNodeToNodeVersion blk)
forall blk.
SupportedNetworkProtocolVersion blk =>
Proxy blk -> Map NodeToNodeVersion (BlockNodeToNodeVersion blk)
supportedNodeToNodeVersions

-- | Return the newest version, i.e., the version with the highest
-- 'NodeToNodeVersion'. This can be used when you don't care about the
-- versioning of a block.
newestVersion ::
     SupportedNetworkProtocolVersion blk
  => Proxy blk -> (NodeToNodeVersion, BlockNodeToNodeVersion blk)
newestVersion :: forall blk.
SupportedNetworkProtocolVersion blk =>
Proxy blk -> (NodeToNodeVersion, BlockNodeToNodeVersion blk)
newestVersion = Map NodeToNodeVersion (BlockNodeToNodeVersion blk)
-> (NodeToNodeVersion, BlockNodeToNodeVersion blk)
forall k a. Map k a -> (k, a)
Map.findMax (Map NodeToNodeVersion (BlockNodeToNodeVersion blk)
 -> (NodeToNodeVersion, BlockNodeToNodeVersion blk))
-> (Proxy blk
    -> Map NodeToNodeVersion (BlockNodeToNodeVersion blk))
-> Proxy blk
-> (NodeToNodeVersion, BlockNodeToNodeVersion blk)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy blk -> Map NodeToNodeVersion (BlockNodeToNodeVersion blk)
forall blk.
SupportedNetworkProtocolVersion blk =>
Proxy blk -> Map NodeToNodeVersion (BlockNodeToNodeVersion blk)
supportedNodeToNodeVersions