summaryrefslogtreecommitdiff
path: root/src/Network/Haskoin/Node/Node.hs
blob: cb6da4b9c167b232095770eccac798eeb0eb2589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE GADTs                 #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE TemplateHaskell       #-}
module Network.Haskoin.Node.Node
    ( node
    ) where

import           Control.Concurrent.NQE
import           Control.Monad.Logger
import           Network.Haskoin.Node.Chain
import           Network.Haskoin.Node.Common
import           Network.Haskoin.Node.Manager
import           UnliftIO

node ::
       ( MonadLoggerIO m
       , MonadUnliftIO m
       )
    => NodeConfig m
    -> m ()
node cfg = do
    psup <- Inbox <$> newTQueueIO
    $(logInfoS) "Node" "Starting..."
    supervisor
        KillAll
        (nodeSupervisor cfg)
        [chain chCfg, manager (mgrCfg psup), peerSup psup]
  where
    peerSup psup = supervisor (Notify deadPeer) psup []
    chCfg =
        ChainConfig
        { chainConfDB = database cfg
        , chainConfListener = nodeEvents cfg . ChainEvent
        , chainConfManager = nodeManager cfg
        , chainConfChain = nodeChain cfg
        , chainConfNetwork = nodeNet cfg
        }
    mgrCfg psup =
        ManagerConfig
        { mgrConfMaxPeers = maxPeers cfg
        , mgrConfDB = database cfg
        , mgrConfDiscover = discover cfg
        , mgrConfMgrListener = nodeEvents cfg . ManagerEvent
        , mgrConfPeerListener = nodeEvents cfg . PeerEvent
        , mgrConfNetAddr = netAddress cfg
        , mgrConfPeers = initPeers cfg
        , mgrConfManager = nodeManager cfg
        , mgrConfChain = nodeChain cfg
        , mgrConfPeerSupervisor = psup
        , mgrConfNetwork = nodeNet cfg
        }
    deadPeer ex = PeerStopped ex `sendSTM` nodeManager cfg