Skip to content

Commit

Permalink
Tidy up network configuration and startup
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-iohk committed Nov 21, 2023
1 parent 51a3278 commit e24b580
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
5 changes: 3 additions & 2 deletions hydra-node/exe/hydra-node/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import Hydra.Node (
runHydraNode,
)
import Hydra.Node.EventQueue (EventQueue (..), createEventQueue)
import Hydra.Node.Network (withNetwork)
import Hydra.Node.Network (NetworkConfiguration (..), withNetwork)
import Hydra.Options (
ChainConfig (..),
Command (GenHydraKey, Publish, Run),
Expand Down Expand Up @@ -102,7 +102,8 @@ main = do
apiPersistence <- createPersistenceIncremental $ persistenceDir <> "/server-output"
withAPIServer apiHost apiPort party apiPersistence (contramap APIServer tracer) chain pparams (putEvent . ClientEvent) $ \server -> do
-- Network
withNetwork tracer persistenceDir (connectionMessages server) signingKey otherParties host port peers nodeId putNetworkEvent $ \hn -> do
let networkConfiguration = NetworkConfiguration{persistenceDir, signingKey, otherParties, host, port, peers, nodeId}
withNetwork tracer (connectionMessages server) networkConfiguration putNetworkEvent $ \hn -> do
-- Main loop
runHydraNode (contramap Node tracer) $
HydraNode
Expand Down
45 changes: 29 additions & 16 deletions hydra-node/src/Hydra/Node/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@
-- │ ▼
--
-- @
module Hydra.Node.Network (withNetwork, withFlipHeartbeats) where
module Hydra.Node.Network (
NetworkConfiguration (..),
withNetwork,
withFlipHeartbeats,
) where

import Hydra.Prelude hiding (fromList, replicate)

Expand All @@ -80,29 +84,36 @@ import Hydra.Persistence (createPersistence, createPersistenceIncremental)
-- The type is made complicated because the various subsystems use part of the tracer only.
type LogEntry tx msg = HydraLog tx (WithHost (TraceOuroborosNetwork (Signed (ReliableMsg (Heartbeat msg)))))

-- | Configuration for a `Node` network layer.
data NetworkConfiguration m = NetworkConfiguration
{ persistenceDir :: FilePath
-- ^ Persistence directory
, signingKey :: SigningKey HydraKey
-- ^ This node's signing key. This is used to sign messages sent to peers.
, otherParties :: [Party]
-- ^ The list of peers `Party` known to this node.
, host :: IP
-- ^ IP address to listen on for incoming connections.
, port :: PortNumber
-- ^ Port to listen on.
, peers :: [Host]
-- ^ Addresses and ports of remote peers.
, nodeId :: NodeId
-- ^ This node's id.
}

-- | Starts the network layer of a node, passing configured `Network` to its continuation.
withNetwork ::
(ToCBOR msg, ToJSON msg, FromJSON msg, FromCBOR msg) =>
-- | Tracer to use for logging messages.
Tracer IO (LogEntry tx msg) ->
-- | Persistence directory
FilePath ->
-- | Callback/observer for connectivity changes in peers.
ConnectionMessages IO ->
-- | This node's signing key. This is used to sign messages sent to peers.
SigningKey HydraKey ->
-- | The list of peers `Party` known to this node.
[Party] ->
-- | IP address to listen on for incoming connections.
IP ->
-- | Port to listen on.
PortNumber ->
-- | Addresses and ports of remote peers.
[Host] ->
-- | This node's id.
NodeId ->
-- | The network configuration
NetworkConfiguration IO ->
-- | Produces a `NetworkComponent` that can send `msg` and consumes `Authenticated` @msg@.
NetworkComponent IO (Authenticated msg) msg ()
withNetwork tracer persistenceDir connectionMessages signingKey otherParties host port peers nodeId callback action = do
withNetwork tracer connectionMessages configuration callback action = do
let localhost = Host{hostname = show host, port}
me = deriveParty signingKey
numberOfParties = length $ me : otherParties
Expand All @@ -117,6 +128,8 @@ withNetwork tracer persistenceDir connectionMessages signingKey otherParties hos

withHeartbeat nodeId connectionMessages reliability callback $ \network ->
action network
where
NetworkConfiguration{persistenceDir, signingKey, otherParties, host, port, peers, nodeId} = configuration

withFlipHeartbeats ::
NetworkComponent m (Authenticated (Heartbeat msg)) msg1 a ->
Expand Down

0 comments on commit e24b580

Please sign in to comment.