Skip to content

Commit

Permalink
Mainnet automatically runs for cardano-node run
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Sep 15, 2020
1 parent a996a17 commit e0274cc
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 57 deletions.
8 changes: 4 additions & 4 deletions cardano-node/src/Cardano/Node/Configuration/Logging.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import Cardano.BM.Trace (Trace, appendName, traceNamedObject)
import qualified Cardano.BM.Trace as Trace

import Cardano.Config.Git.Rev (gitRev)
import Cardano.Node.Configuration.POM (NodeConfigurationF (..))
import Cardano.Node.Configuration.POM (NodeConfiguration (..))
import Cardano.Node.Types

--------------------------------
Expand Down Expand Up @@ -125,7 +125,7 @@ loggingCLIConfiguration = maybe emptyConfig readConfig
-- | Create logging feature for `cardano-node`
createLoggingLayer
:: Text
-> NodeConfigurationF
-> NodeConfiguration
-> ExceptT ConfigError IO LoggingLayer
createLoggingLayer ver nodeConfig' = do

Expand Down Expand Up @@ -158,7 +158,7 @@ createLoggingLayer ver nodeConfig' = do
pure $ mkLogLayer logConfig switchBoard trace
where
loggingPreInit
:: NodeConfigurationF
:: NodeConfiguration
-> Configuration
-> Switchboard Text
-> Trace IO Text
Expand Down Expand Up @@ -188,7 +188,7 @@ createLoggingLayer ver nodeConfig' = do
-- Record node metrics, if configured
startCapturingMetrics trace

adaptLogConfig :: NodeConfigurationF -> Configuration -> IO ()
adaptLogConfig :: NodeConfiguration -> Configuration -> IO ()
adaptLogConfig nodeConfig =
liveViewdisablesStdout (ncViewMode nodeConfig)
liveViewdisablesStdout SimpleView _ = pure ()
Expand Down
40 changes: 20 additions & 20 deletions cardano-node/src/Cardano/Node/Configuration/POM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{-# OPTIONS_GHC -Wno-orphans #-} -- TODO: REMOVE ME

module Cardano.Node.Configuration.POM
( NodeConfigurationF (..)
( NodeConfiguration (..)
, PartialNodeConfiguration(..)
, defaultPartialNodeConfiguration
, lastOption
Expand Down Expand Up @@ -37,8 +37,8 @@ import Cardano.Node.Types
import Cardano.Tracing.Config
import Ouroboros.Network.Block (MaxSlotNo (..))

data NodeConfigurationF
= NodeConfigurationF
data NodeConfiguration
= NodeConfiguration
{ ncNodeAddr :: !(Maybe NodeAddress)
-- | Filepath of the configuration yaml file. This file determines
-- all the configuration settings required for the cardano node
Expand Down Expand Up @@ -94,8 +94,8 @@ data PartialNodeConfiguration
, pncSocketPath :: !(Last SocketPath)

-- BlockFetch configuration
, pncMaxConcurrencyBulkSync :: !(Last (Maybe MaxConcurrencyBulkSync))
, pncMaxConcurrencyDeadline :: !(Last (Maybe MaxConcurrencyDeadline))
, pncMaxConcurrencyBulkSync :: !(Last MaxConcurrencyBulkSync)
, pncMaxConcurrencyDeadline :: !(Last MaxConcurrencyDeadline)

-- Logging parameters:
, pncViewMode :: !(Last ViewMode)
Expand Down Expand Up @@ -273,10 +273,16 @@ instance FromJSON PartialNodeConfiguration where
npcTestShelleyHardForkAtVersion,
npcShelleyHardForkNotBeforeEpoch
}

-- Default configuration is mainnet
defaultPartialNodeConfiguration :: PartialNodeConfiguration
defaultPartialNodeConfiguration = mempty
{ pncViewMode = Last $ Just SimpleView
{ pncConfigFile = Last . Just $ ConfigYamlFilePath "configuration/cardano/mainnet-config.json"
, pncDatabaseFile = Last . Just $ DbFile "mainnet/db/"
, pncLoggingSwitch = Last $ Just True
, pncSocketPath = Last $ Just "mainnet/socket/nodesocket"
, pncTopologyFile = Last . Just $ TopologyFile "configuration/cardano/mainnet-topology.json"
, pncViewMode = Last $ Just SimpleView
}

lastOption :: Parser a -> Parser (Last a)
Expand All @@ -285,27 +291,21 @@ lastOption parser = fmap Last $ optional parser
lastToEither :: String -> Last a -> Either String a
lastToEither errMsg (Last x) = maybe (Left errMsg) Right x

makeNodeConfiguration :: PartialNodeConfiguration -> Either String NodeConfigurationF
makeNodeConfiguration :: PartialNodeConfiguration -> Either String NodeConfiguration
makeNodeConfiguration pnc = do
configFile <- lastToEither "Missing ConfigFile" $ pncConfigFile pnc
configFile <- lastToEither "Missing YAML config file" $ pncConfigFile pnc
topologyFile <- lastToEither "Missing TopologyFile" $ pncTopologyFile pnc
databaseFile <- lastToEither "Missing DatabaseFile" $ pncDatabaseFile pnc
-- socketFile <- lastToEither "Missing SocketFile" $ pncSocketFile pnc
protocolFiles <- lastToEither "Missing ProtocolFiles" $ pncProtocolFiles pnc
validateDB <- lastToEither "Missing ValidateDB" $ pncValidateDB pnc
shutdownIPC <- lastToEither "Missing ShutdownIPC" $ pncShutdownIPC pnc
shutdownOnSlotSynced <- lastToEither "Missing ShutdownOnSlotSynced" $ pncShutdownOnSlotSynced pnc

--pncSocketPath

protocolConfig <- lastToEither "Missing ProtocolConfig" $ pncProtocolConfig pnc
maxConcurrencyBulkSync <- lastToEither "Missing MaxConcurrencyBulkSync" $ pncMaxConcurrencyBulkSync pnc
maxConcurrencyDeadline <- lastToEither "Missing MaxConcurrencyDeadline" $ pncMaxConcurrencyDeadline pnc
viewMode <- lastToEither "Missing ViewMode" $ pncViewMode pnc
loggingSwitch <- lastToEither "Missing LoggingSwitch" $ pncLoggingSwitch pnc
logMetrics <- lastToEither "Missing LogMetrics" $ pncLogMetrics pnc
traceConfig <- lastToEither "Missing TraceConfig" $ pncTraceConfig pnc
return $ NodeConfigurationF
return $ NodeConfiguration
{ ncNodeAddr = getLast $ pncNodeAddr pnc
, ncConfigFile = configFile
, ncTopologyFile = topologyFile
Expand All @@ -316,15 +316,15 @@ makeNodeConfiguration pnc = do
, ncShutdownOnSlotSynced = shutdownOnSlotSynced
, ncProtocolConfig = protocolConfig
, ncSocketPath = getLast $ pncSocketPath pnc
, ncMaxConcurrencyBulkSync = maxConcurrencyBulkSync
, ncMaxConcurrencyDeadline = maxConcurrencyDeadline
, ncMaxConcurrencyBulkSync = getLast $ pncMaxConcurrencyBulkSync pnc
, ncMaxConcurrencyDeadline = getLast $ pncMaxConcurrencyDeadline pnc
, ncViewMode = viewMode
, ncLoggingSwitch = loggingSwitch
, ncLogMetrics = logMetrics
, ncTraceConfig = traceConfig
}

ncProtocol :: NodeConfigurationF -> Protocol
ncProtocol :: NodeConfiguration -> Protocol
ncProtocol nc =
case ncProtocolConfig nc of
NodeProtocolConfigurationByron{} -> ByronProtocol
Expand All @@ -333,8 +333,8 @@ ncProtocol nc =


parseNodeConfigurationFP :: Maybe ConfigYamlFilePath -> IO PartialNodeConfiguration
parseNodeConfigurationFP Nothing = panic "No configuration yaml filepath provided"
parseNodeConfigurationFP Nothing = parseNodeConfigurationFP . getLast $ pncConfigFile defaultPartialNodeConfiguration
parseNodeConfigurationFP (Just (ConfigYamlFilePath fp)) = do
nc <- decodeFileThrow fp
-- Make all the files be relative to the location of the config file.
pure $ adjustFilePaths (takeDirectory fp </>) nc
pure $ adjustFilePaths (takeDirectory fp </>) nc <> defaultPartialNodeConfiguration
4 changes: 2 additions & 2 deletions cardano-node/src/Cardano/Node/Configuration/Socket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Control.Monad.Trans.Except.Extra (handleIOExceptT)
import Network.Socket (AddrInfo (..), AddrInfoFlag (..), Socket, SocketType (..),
defaultHints, getAddrInfo)

import Cardano.Node.Configuration.POM (NodeConfigurationF (..))
import Cardano.Node.Configuration.POM (NodeConfiguration (..))
import Cardano.Node.Types

#if defined(mingw32_HOST_OS)
Expand Down Expand Up @@ -91,7 +91,7 @@ renderSocketConfigError (GetAddrInfoError addr ex) =
-- * node cli
-- * systemd socket activation
--
gatherConfiguredSockets :: NodeConfigurationF
gatherConfiguredSockets :: NodeConfiguration
-> ExceptT SocketConfigError IO
(SocketOrSocketInfo [Socket] [AddrInfo],
SocketOrSocketInfo Socket SocketPath)
Expand Down
4 changes: 2 additions & 2 deletions cardano-node/src/Cardano/Node/Configuration/Topology.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import qualified Data.Text as Text
import Network.Socket (PortNumber, SockAddr (..))
import Text.Read (readMaybe)

import Cardano.Node.Configuration.POM (NodeConfigurationF (..))
import Cardano.Node.Configuration.POM (NodeConfiguration (..))
import Cardano.Node.Types

import Ouroboros.Consensus.Util.Condense (Condense (..))
Expand Down Expand Up @@ -129,7 +129,7 @@ instance ToJSON NetworkTopology where
-- | Read the `NetworkTopology` configuration from the specified file.
-- While running a real protocol, this gives your node its own address and
-- other remote peers it will attempt to connect to.
readTopologyFile :: NodeConfigurationF -> IO (Either Text NetworkTopology)
readTopologyFile :: NodeConfiguration -> IO (Either Text NetworkTopology)
readTopologyFile nc = do
eBs <- Exception.try $ BS.readFile (unTopology $ ncTopologyFile nc)

Expand Down
12 changes: 6 additions & 6 deletions cardano-node/src/Cardano/Node/Handlers/Shutdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import Ouroboros.Consensus.Util.ResourceRegistry (ResourceRegistry)
import Ouroboros.Consensus.Util.STM (onEachChange)
import Ouroboros.Network.Block (MaxSlotNo (..), SlotNo, pointSlot)

import Cardano.Node.Configuration.POM (NodeConfigurationF (..))
import Cardano.Node.Configuration.POM (NodeConfiguration (..))

-- | 'ShutdownFDs' mediate the graceful shutdown requests,
-- either external or internal to the process.
Expand Down Expand Up @@ -131,18 +131,18 @@ triggerShutdown (ShutdownDoorbell (Fd shutFd)) trace reason = do
-- external or internal, as requested by configuration in 'NodeCLI',
-- while allocating corresponding 'ShutdownFDs', and providing them to the 'action'.
withShutdownHandling
:: NodeConfigurationF
:: NodeConfiguration
-> Trace IO Text
-> (ShutdownFDs -> IO ())
-> IO ()
withShutdownHandling nc trace action = do
sfds <- decideShutdownFds nc
withShutdownHandler (sfdsListener sfds) trace (action sfds)
where
decideShutdownFds :: NodeConfigurationF -> IO ShutdownFDs
decideShutdownFds NodeConfigurationF{ncShutdownIPC = Just fd} =
decideShutdownFds :: NodeConfiguration -> IO ShutdownFDs
decideShutdownFds NodeConfiguration{ncShutdownIPC = Just fd} =
pure $ ExternalShutdown (ShutdownListener fd)
decideShutdownFds NodeConfigurationF{ncShutdownOnSlotSynced = MaxSlotNo{}} =
decideShutdownFds NodeConfiguration{ncShutdownOnSlotSynced = MaxSlotNo{}} =
mkInternalShutdown
decideShutdownFds _ = pure NoShutdownFDs

Expand All @@ -155,7 +155,7 @@ withShutdownHandling nc trace action = do
-- spawn a thread that would cause node to shutdown upon ChainDB reaching the
-- configuration-defined slot.
maybeSpawnOnSlotSyncedShutdownHandler
:: NodeConfigurationF
:: NodeConfiguration
-> ShutdownFDs
-> Trace IO Text
-> ResourceRegistry IO
Expand Down
6 changes: 3 additions & 3 deletions cardano-node/src/Cardano/Node/Protocol.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Cardano.Prelude
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Except.Extra (firstExceptT)

import Cardano.Node.Configuration.POM (NodeConfigurationF (..))
import Cardano.Node.Configuration.POM (NodeConfiguration (..))
import Cardano.Node.Types

import Cardano.Node.Protocol.Byron
Expand All @@ -25,9 +25,9 @@ import Cardano.Node.Protocol.Types (SomeConsensusProtocol (..))
--

mkConsensusProtocol
:: NodeConfigurationF
:: NodeConfiguration
-> ExceptT ProtocolInstantiationError IO SomeConsensusProtocol
mkConsensusProtocol NodeConfigurationF{ncProtocolConfig, ncProtocolFiles} =
mkConsensusProtocol NodeConfiguration{ncProtocolConfig, ncProtocolFiles} =
case ncProtocolConfig of

NodeProtocolConfigurationByron config ->
Expand Down
33 changes: 17 additions & 16 deletions cardano-node/src/Cardano/Node/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Data.Version (showVersion)
import GHC.Clock (getMonotonicTimeNSec)
import Network.HostName (getHostName)
import Network.Socket (AddrInfo, Socket)
import System.Directory (canonicalizePath, makeAbsolute)
import System.Directory (canonicalizePath, createDirectoryIfMissing, makeAbsolute)
import System.Environment (lookupEnv)

import Cardano.BM.Data.Aggregated (Measurable (..))
Expand All @@ -48,9 +48,9 @@ import Cardano.BM.Trace
import Cardano.Config.Git.Rev (gitRev)
import Cardano.Node.Configuration.Logging (LoggingLayer (..), Severity (..),
createLoggingLayer, shutdownLoggingLayer)
import Cardano.Node.Configuration.POM (NodeConfigurationF (..),
PartialNodeConfiguration (..), defaultPartialNodeConfiguration,
makeNodeConfiguration, parseNodeConfigurationFP)
import Cardano.Node.Configuration.POM (NodeConfiguration (..),
PartialNodeConfiguration (..), makeNodeConfiguration, ncProtocol,
parseNodeConfigurationFP)
import Cardano.Node.Types
import Cardano.Tracing.Config (TraceOptions (..), TraceSelection (..))

Expand Down Expand Up @@ -96,11 +96,10 @@ runNode
-> IO ()
runNode cmd@PartialNodeConfiguration{pncConfigFile} = do
configYamlPc <- parseNodeConfigurationFP $ getLast pncConfigFile

nc <- case makeNodeConfiguration $ cmd <> configYamlPc <> defaultPartialNodeConfiguration of
Left err -> panic $ Text.pack err
nc <- case makeNodeConfiguration $ cmd <> configYamlPc of
Left err -> panic $ "Error in creating the NodeConfiguration: " <> Text.pack err
Right nc' -> return nc'

print $ ncDatabaseFile nc
eLoggingLayer <- runExceptT $ createLoggingLayer
(Text.pack (showVersion version))
nc
Expand Down Expand Up @@ -176,7 +175,7 @@ runNode cmd@PartialNodeConfiguration{pncConfigFile} = do
#endif
shutdownLoggingLayer loggingLayer

logTracingVerbosity :: NodeConfigurationF -> Tracer IO String -> IO ()
logTracingVerbosity :: NodeConfiguration -> Tracer IO String -> IO ()
logTracingVerbosity nc tracer =
case ncTraceConfig nc of
TracingOff -> return ()
Expand Down Expand Up @@ -251,7 +250,7 @@ handleSimpleNode
=> Consensus.Protocol IO blk (BlockProtocol blk)
-> Trace IO Text
-> Tracers RemoteConnectionId LocalConnectionId blk
-> NodeConfigurationF
-> NodeConfiguration
-> (NodeKernel IO RemoteConnectionId LocalConnectionId blk -> IO ())
-- ^ Called on the 'NodeKernel' after creating it, but before the network
-- layer is initialised. This implies this function must not block,
Expand Down Expand Up @@ -351,12 +350,12 @@ handleSimpleNode p trace nodeTracers nc onKernel = do
}

createTracers
:: NodeConfigurationF
:: NodeConfiguration
-> Trace IO Text
-> Tracer IO Text
-> Consensus.TopLevelConfig blk
-> IO ()
createTracers ncf@NodeConfigurationF{ncNodeAddr, ncValidateDB}
createTracers ncf@NodeConfiguration{ncNodeAddr, ncValidateDB}
tr tracer cfg = do
eitherTopology <- readTopologyFile ncf
nt <- either
Expand All @@ -383,7 +382,7 @@ handleSimpleNode p trace nodeTracers nc onKernel = do
nTr = appendName "networkMagic" tr
vTr = appendName "version" tr
cTr = appendName "commit" tr
traceNamedObject rTr (meta, LogMessage ("THIS SHOULD BE THE PROTOCOL")) --TODO: Fix me
traceNamedObject rTr (meta, LogMessage . Text.pack . protocolName $ ncProtocol nc)
traceNamedObject nTr (meta, LogMessage ("NetworkMagic " <> show (unNetworkMagic . getNetworkMagic $ Consensus.configBlock cfg)))
traceNamedObject vTr (meta, LogMessage . pack . showVersion $ version)
traceNamedObject cTr (meta, LogMessage gitRev)
Expand All @@ -394,9 +393,11 @@ handleSimpleNode p trace nodeTracers nc onKernel = do
-- Helper functions
--------------------------------------------------------------------------------

canonDbPath :: NodeConfigurationF -> IO FilePath
canonDbPath NodeConfigurationF{ncDatabaseFile = DbFile dbFp} =
canonicalizePath =<< makeAbsolute dbFp
canonDbPath :: NodeConfiguration -> IO FilePath
canonDbPath NodeConfiguration{ncDatabaseFile = DbFile dbFp} = do
fp <- canonicalizePath =<< makeAbsolute dbFp
createDirectoryIfMissing True fp
return fp

createDiffusionArguments
:: SocketOrSocketInfo [Socket] [AddrInfo]
Expand Down
8 changes: 4 additions & 4 deletions cardano-node/src/Cardano/Node/TUI/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ import Cardano.Node.TUI.Drawing (LiveViewState (..), LiveViewThread (.
import Cardano.Node.TUI.EventHandler (LiveViewBackend (..))
import Cardano.Tracing.Peer (Peer (..))

import Cardano.Node.Configuration.POM (NodeConfigurationF (..))
import Cardano.Node.Configuration.POM (NodeConfiguration (..), ncProtocol)
import Cardano.Node.Types

-- | Change a few fields in the LiveViewState after it has been initialized above.
liveViewPostSetup :: NFData a => LiveViewBackend blk a -> NodeConfigurationF-> IO ()
liveViewPostSetup :: NFData a => LiveViewBackend blk a -> NodeConfiguration-> IO ()
liveViewPostSetup lvbe nc = do
modifyMVar_ (getbe lvbe) $ \lvs ->
pure lvs
{ lvsNodeId = nodeId
, lvsProtocol = panic "NEED PROTOCOL HERE" --TODO: FIX ME --ncProtocol nc
, lvsRelease = "NEED PROTOCOL NAME HERE" --TODO: FIX ME protocolName (ncProtocol nc)
, lvsProtocol = ncProtocol nc
, lvsRelease = protocolName (ncProtocol nc)
}
where
--TODO: this is meaningless. Nodes do not have ids. The port number is not
Expand Down

0 comments on commit e0274cc

Please sign in to comment.