{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Ouroboros.Consensus.Mock.Ledger.Address (
    Addr
  , AddrDist
  , mkAddrDist
  ) where

import           Codec.Serialise (Serialise)
import           Control.DeepSeq (NFData)
import           Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import           Data.String
import           NoThunks.Class (NoThunks)

import           Ouroboros.Consensus.Node.ProtocolInfo
import           Ouroboros.Consensus.NodeId (NodeId (..))
import           Ouroboros.Consensus.Util.Condense

-- | Mock address
newtype Addr = Addr String
  deriving (
      Int -> Addr -> ShowS
[Addr] -> ShowS
Addr -> String
(Int -> Addr -> ShowS)
-> (Addr -> String) -> ([Addr] -> ShowS) -> Show Addr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Addr] -> ShowS
$cshowList :: [Addr] -> ShowS
show :: Addr -> String
$cshow :: Addr -> String
showsPrec :: Int -> Addr -> ShowS
$cshowsPrec :: Int -> Addr -> ShowS
Show
    , Addr -> Addr -> Bool
(Addr -> Addr -> Bool) -> (Addr -> Addr -> Bool) -> Eq Addr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Addr -> Addr -> Bool
$c/= :: Addr -> Addr -> Bool
== :: Addr -> Addr -> Bool
$c== :: Addr -> Addr -> Bool
Eq
    , Eq Addr
Eq Addr
-> (Addr -> Addr -> Ordering)
-> (Addr -> Addr -> Bool)
-> (Addr -> Addr -> Bool)
-> (Addr -> Addr -> Bool)
-> (Addr -> Addr -> Bool)
-> (Addr -> Addr -> Addr)
-> (Addr -> Addr -> Addr)
-> Ord Addr
Addr -> Addr -> Bool
Addr -> Addr -> Ordering
Addr -> Addr -> Addr
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
min :: Addr -> Addr -> Addr
$cmin :: Addr -> Addr -> Addr
max :: Addr -> Addr -> Addr
$cmax :: Addr -> Addr -> Addr
>= :: Addr -> Addr -> Bool
$c>= :: Addr -> Addr -> Bool
> :: Addr -> Addr -> Bool
$c> :: Addr -> Addr -> Bool
<= :: Addr -> Addr -> Bool
$c<= :: Addr -> Addr -> Bool
< :: Addr -> Addr -> Bool
$c< :: Addr -> Addr -> Bool
compare :: Addr -> Addr -> Ordering
$ccompare :: Addr -> Addr -> Ordering
$cp1Ord :: Eq Addr
Ord
    , String -> Addr
(String -> Addr) -> IsString Addr
forall a. (String -> a) -> IsString a
fromString :: String -> Addr
$cfromString :: String -> Addr
IsString
    , Decoder s Addr
Decoder s [Addr]
[Addr] -> Encoding
Addr -> Encoding
(Addr -> Encoding)
-> (forall s. Decoder s Addr)
-> ([Addr] -> Encoding)
-> (forall s. Decoder s [Addr])
-> Serialise Addr
forall s. Decoder s [Addr]
forall s. Decoder s Addr
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [Addr]
$cdecodeList :: forall s. Decoder s [Addr]
encodeList :: [Addr] -> Encoding
$cencodeList :: [Addr] -> Encoding
decode :: Decoder s Addr
$cdecode :: forall s. Decoder s Addr
encode :: Addr -> Encoding
$cencode :: Addr -> Encoding
Serialise
    , Addr -> ()
(Addr -> ()) -> NFData Addr
forall a. (a -> ()) -> NFData a
rnf :: Addr -> ()
$crnf :: Addr -> ()
NFData
    , Context -> Addr -> IO (Maybe ThunkInfo)
Proxy Addr -> String
(Context -> Addr -> IO (Maybe ThunkInfo))
-> (Context -> Addr -> IO (Maybe ThunkInfo))
-> (Proxy Addr -> String)
-> NoThunks Addr
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy Addr -> String
$cshowTypeOf :: Proxy Addr -> String
wNoThunks :: Context -> Addr -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> Addr -> IO (Maybe ThunkInfo)
noThunks :: Context -> Addr -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> Addr -> IO (Maybe ThunkInfo)
NoThunks
    )

instance Condense Addr where
  condense :: Addr -> String
condense (Addr String
addr) = String
addr

-- | Mapping from addresses to node IDs
--
-- This is needed in order to assign stake to nodes.
type AddrDist = Map Addr NodeId

-- | Construct address to node ID mapping
mkAddrDist :: NumCoreNodes -> AddrDist
mkAddrDist :: NumCoreNodes -> AddrDist
mkAddrDist NumCoreNodes
numCoreNodes =
    [(Addr, NodeId)] -> AddrDist
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Addr, NodeId)] -> AddrDist) -> [(Addr, NodeId)] -> AddrDist
forall a b. (a -> b) -> a -> b
$ [Addr] -> [NodeId] -> [(Addr, NodeId)]
forall a b. [a] -> [b] -> [(a, b)]
zip [ String -> Addr
forall a. IsString a => String -> a
fromString [Char
addr] | Char
addr <- [Char
'a'..] ]
                       [ CoreNodeId -> NodeId
CoreId CoreNodeId
nid
                       | CoreNodeId
nid <- NumCoreNodes -> [CoreNodeId]
enumCoreNodes NumCoreNodes
numCoreNodes
                       ]