Skip to content

Commit

Permalink
Implement SocketPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Nov 28, 2019
1 parent 7e511da commit 6d60295
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 53 deletions.
8 changes: 4 additions & 4 deletions cardano-config/src/Cardano/Config/CommonCLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ parseSigningKey =
<> help "Path to the signing key."
)

parseSocketDir :: Parser FilePath
parseSocketDir :: Parser SocketPath
parseSocketDir =
strOption
SocketDir <$> strOption
( long "socket-dir"
<> metavar "FILEPATH"
<> help "Directory with local sockets:\
Expand All @@ -164,9 +164,9 @@ parseSocketDirLast =
\ ${dir}/node-{core,relay}-${node-id}.socket"
)

parseSocketPath :: Parser FilePath
parseSocketPath :: Parser SocketPath
parseSocketPath =
strOption
SocketFile <$> strOption
( long "socket-path"
<> help "Path to a cardano-node socket"
<> completer (bashCompleter "file")
Expand Down
10 changes: 5 additions & 5 deletions cardano-config/src/Cardano/Config/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Cardano.Config.Types
, NodeRealCLI (..)
, NodeProtocolMode (..)
, SigningKeyFile (..)
, SocketFile (..)
, SocketPath (..)
, TopologyFile( ..)
-- * specific for @Core@
, NodeProtocol (..)
Expand Down Expand Up @@ -157,7 +157,7 @@ data MiscellaneousFilepaths = MiscellaneousFilepaths
, genesisFile :: !GenesisFile
, delegCertFile :: !(Maybe DelegationCertFile)
, signKeyFile :: !(Maybe SigningKeyFile)
, socketFile :: !SocketFile
, socketFile :: !SocketPath
} deriving Show

newtype TopologyFile = TopologyFile
Expand All @@ -176,9 +176,9 @@ newtype DelegationCertFile = DelegationCertFile
{ unDelegationCert :: FilePath }
deriving Show

newtype SocketFile = SocketFile
{ unSocket :: FilePath }
deriving Show
data SocketPath = SocketFile FilePath
| SocketDir FilePath
deriving Show

newtype SigningKeyFile = SigningKeyFile
{ unSigningKey :: FilePath }
Expand Down
8 changes: 4 additions & 4 deletions cardano-node/app/chairman.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Cardano.Config.CommonCLI
import Cardano.Config.Protocol (SomeProtocol(..), fromProtocol)
import Cardano.Config.Types (ConfigYamlFilePath(..), DelegationCertFile(..),
GenesisFile (..), NodeConfiguration(..),
SigningKeyFile(..), SocketFile(..), parseNodeConfiguration)
SigningKeyFile(..), SocketPath(..), parseNodeConfiguration)
import Cardano.Common.Parsers
import Cardano.Chairman (runChairman)

Expand Down Expand Up @@ -54,7 +54,7 @@ main = do
let run = runChairman p caCoreNodeIds
caSecurityParam
caMaxBlockNo
(unSocket caSocketDir)
caSocketDir
stdoutTracer

case caTimeout of
Expand Down Expand Up @@ -89,7 +89,7 @@ data ChairmanArgs = ChairmanArgs {
, caTimeout :: !(Maybe Int)
, caTimeoutType :: !TimeoutType
, caGenesisFile :: !GenesisFile
, caSocketDir :: !SocketFile
, caSocketDir :: !SocketPath
, caConfigYaml :: !ConfigYamlFilePath
, caSigningKeyFp :: !(Maybe SigningKeyFile)
, caDelegationCertFp :: !(Maybe DelegationCertFile)
Expand Down Expand Up @@ -132,7 +132,7 @@ parseChairmanArgs =
<*> parseFlag' FailureTimeout SuccessTimeout
"timeout-is-success" "Exit successfully on timeout."
<*> (GenesisFile <$> parseGenesisPath)
<*> (SocketFile <$> parseSocketDir)
<*> parseSocketDir
<*> (ConfigYamlFilePath <$> parseConfigFile)
<*> (optional $ SigningKeyFile <$> parseSigningKey)
<*> (optional $ DelegationCertFile <$> parseDelegationCert)
Expand Down
2 changes: 1 addition & 1 deletion cardano-node/src/Cardano/CLI/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import Cardano.Common.Orphans ()
import Cardano.Config.Protocol
import Cardano.Config.Types (CardanoConfiguration(..), Core(..),
DelegationCertFile(..), GenesisFile(..),
SigningKeyFile(..), SocketFile(..))
SigningKeyFile(..), SocketPath(..))
import Cardano.Config.Logging (LoggingLayer (..))
import Cardano.Config.Topology (NodeAddress(..), TopologyInfo(..))

Expand Down
4 changes: 2 additions & 2 deletions cardano-node/src/Cardano/CLI/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import Cardano.CLI.Ops
import Cardano.CLI.Tx.Submission
import Cardano.Config.Protocol
import Cardano.Config.Types (DelegationCertFile, GenesisFile,
SigningKeyFile, SocketFile, Update)
SigningKeyFile, SocketPath, Update)
import Cardano.Config.Topology
import Cardano.Common.Orphans ()

Expand Down Expand Up @@ -299,7 +299,7 @@ nodeSubmitTx
-> Maybe Double
-> Maybe DelegationCertFile
-> Maybe SigningKeyFile
-> SocketFile
-> SocketPath
-> Update
-> Protocol
-> GenTx ByronBlock
Expand Down
10 changes: 5 additions & 5 deletions cardano-node/src/Cardano/CLI/Tx/Generation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import qualified Cardano.Chain.Genesis as CC.Genesis
import qualified Cardano.Chain.MempoolPayload as CC.Mempool
import qualified Cardano.Chain.UTxO as CC.UTxO
import Cardano.Config.Logging (LoggingLayer (..), Trace)
import Cardano.Config.Types (SocketFile)
import Cardano.Config.Types (SocketPath)
import qualified Cardano.Crypto as Crypto
import Cardano.Config.Topology (NodeAddress (..),
NodeHostAddress(..))
Expand Down Expand Up @@ -142,7 +142,7 @@ data TxGenError = CurrentlyCannotSendTxToRelayNode FilePath
-----------------------------------------------------------------------------------------
genesisBenchmarkRunner
:: LoggingLayer
-> SocketFile
-> SocketPath
-> Consensus.Protocol ByronBlock
-> NonEmpty NodeAddress
-> NumberOfTxs
Expand Down Expand Up @@ -389,7 +389,7 @@ extractGenesisFunds genesisConfig signingKeys =
-- (latter corresponds to 'targetAddress' here) and "remember" it in 'availableFunds'.
prepareInitialFunds
:: Tracer IO String
-> SocketFile
-> SocketPath
-> CC.Genesis.Config
-> NodeConfig ByronConsensusProtocol
-> Map Int ((CC.UTxO.TxIn, CC.UTxO.TxOut), Crypto.SigningKey)
Expand Down Expand Up @@ -642,7 +642,7 @@ runBenchmark
-> Tracer IO SendRecvConnect
-> Tracer IO (SendRecvTxSubmission ByronBlock)
-> Tracer IO String
-> SocketFile
-> SocketPath
-> NodeConfig ByronConsensusProtocol
-> Crypto.SigningKey
-> CC.Common.Address
Expand Down Expand Up @@ -760,7 +760,7 @@ runBenchmark benchTracer
-- Technically all splitting transactions will send money back to 'sourceAddress'.
createMoreFundCoins
:: Tracer IO String
-> SocketFile
-> SocketPath
-> NodeConfig ByronConsensusProtocol
-> Crypto.SigningKey
-> FeePerTx
Expand Down
8 changes: 4 additions & 4 deletions cardano-node/src/Cardano/CLI/Tx/Submission.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import qualified Ouroboros.Network.NodeToClient as NodeToClient

import Cardano.Config.Topology
import Cardano.Common.LocalSocket
import Cardano.Config.Types (SocketFile(..))
import Cardano.Config.Types (SocketPath(..))



Expand All @@ -65,7 +65,7 @@ handleTxSubmission :: forall blk.
( RunNode blk
, Show (ApplyTxErr blk)
)
=> SocketFile
=> SocketPath
-> Consensus.Protocol blk
-> TopologyInfo
-> GenTx blk
Expand All @@ -80,14 +80,14 @@ handleTxSubmission socketFp ptcl tinfo tx tracer = do
submitTx :: ( RunNode blk
, Show (ApplyTxErr blk)
)
=> SocketFile
=> SocketPath
-> NodeConfig (BlockProtocol blk)
-> NodeId
-> GenTx blk
-> Tracer IO String
-> IO ()
submitTx socketFp protoInfoConfig nId tx tracer = do
socketPath <- localSocketAddrInfo (Just nId) (unSocket $ socketFp) NoMkdirIfMissing
socketPath <- localSocketAddrInfo (Just nId) socketFp NoMkdirIfMissing
NodeToClient.connectTo
NetworkConnectTracers {
nctMuxTracer = nullTracer,
Expand Down
5 changes: 3 additions & 2 deletions cardano-node/src/Cardano/Chairman.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import Ouroboros.Network.Protocol.Handshake.Version
import Ouroboros.Network.NodeToClient

import Cardano.Common.LocalSocket
import Cardano.Config.Types (SocketPath)
import Cardano.Tracing.Tracers (TraceConstraints)

-- | Run chairman: connect with all the core nodes. Chairman will store the
Expand All @@ -81,7 +82,7 @@ runChairman :: forall blk.
-- will throw an exception.
-> Maybe BlockNo
-- ^ finish after that many blocks, if 'Nothing' run continuously.
-> FilePath
-> SocketPath
-- ^ local socket dir
-> Tracer IO String
-> IO ()
Expand Down Expand Up @@ -127,7 +128,7 @@ createConnection
-> Maybe BlockNo
-> Tracer IO String
-> NodeConfig (BlockProtocol blk)
-> FilePath
-> SocketPath
-> IO ()
createConnection
coreNodeId
Expand Down
33 changes: 25 additions & 8 deletions cardano-node/src/Cardano/Common/LocalSocket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import System.FilePath ((</>))
import System.IO.Error (isDoesNotExistError)
import Network.Socket as Socket

import Cardano.Config.Types (SocketPath(..))

import Ouroboros.Consensus.NodeId (NodeId(..))

data MkdirIfMissing
Expand All @@ -26,18 +28,22 @@ localSocketFilePath (RelayId n) = "node-relay-" ++ show n ++ ".socket"

-- | Provide an AF_UNIX address for a socket situated in 'socketDir', with its name
-- derived from the node ID. When 'mkdir' is 'MkdirIfMissing', the directory is created.
localSocketAddrInfo :: Maybe NodeId -> FilePath -> MkdirIfMissing -> IO Socket.AddrInfo
localSocketAddrInfo Nothing socketPath _ = do
localSocketAddrInfo :: Maybe NodeId -> SocketPath -> MkdirIfMissing -> IO Socket.AddrInfo
localSocketAddrInfo (Just _) (SocketFile _) _ =
panic $ "Cardano.Common.LocalSocket.localSocketAddrInfo: "
<> "You specified a socket filepath and a NodeId. "
<> "NodeIds should be used with a specified socket directory."
localSocketAddrInfo Nothing (SocketFile fp) _ = do
pure $
Socket.AddrInfo
[]
Socket.AF_UNIX
Socket.Stream
Socket.defaultProtocol
(Socket.SockAddrUnix socketPath)
(Socket.SockAddrUnix fp)
Nothing
localSocketAddrInfo (Just nodeId) socketDir mkdir = do
dir <- canonicalizePath =<< makeAbsolute socketDir
localSocketAddrInfo (Just nodeId) (SocketDir sDir) mkdir = do
dir <- canonicalizePath =<< makeAbsolute sDir
when (mkdir == MkdirIfMissing)
$ createDirectoryIfMissing True dir
pure $
Expand All @@ -48,19 +54,30 @@ localSocketAddrInfo (Just nodeId) socketDir mkdir = do
Socket.defaultProtocol
(Socket.SockAddrUnix $ dir </> localSocketFilePath nodeId)
Nothing
localSocketAddrInfo Nothing (SocketDir _) _ =
panic $ "Cardano.Common.LocalSocket.localSocketAddrInfo: "
<> "You specified a socket directory without specifying a NodeId "

-- TODO: Convert to ExceptT
-- | Remove the socket established with 'localSocketAddrInfo'.
removeStaleLocalSocket :: Maybe NodeId -> FilePath -> IO ()
removeStaleLocalSocket Nothing socketFp = do
removeStaleLocalSocket :: Maybe NodeId -> SocketPath -> IO ()
removeStaleLocalSocket Nothing (SocketFile socketFp) = do
removeFile socketFp
`catch` \e ->
if isDoesNotExistError e
then return ()
else throwIO e
removeStaleLocalSocket (Just nodeId) socketDir = do
removeStaleLocalSocket (Just nodeId) (SocketDir socketDir) = do
dir <- canonicalizePath =<< makeAbsolute socketDir
removeFile (dir </> localSocketFilePath nodeId)
`catch` \e ->
if isDoesNotExistError e
then return ()
else throwIO e
removeStaleLocalSocket Nothing (SocketDir _) =
panic $ "Cardano.Common.LocalSocket.removeStaleLocalSocket: "
<> "You specified a socket directory without specifying a NodeId "
removeStaleLocalSocket (Just _) (SocketFile _) =
panic $ "Cardano.Common.LocalSocket.removeStaleLocalSocket: "
<> "You specified a socket filepath and a NodeId. "
<> "NodeIds should be used with a specified socket directory."
4 changes: 2 additions & 2 deletions cardano-node/src/Cardano/Common/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ nodeMockParser = do
(GenesisFile genFp)
(DelegationCertFile <$> delCertFp)
(SigningKeyFile <$> sKeyFp)
(SocketFile socketFp)
socketFp
)
nAddress
(ConfigYamlFilePath nodeConfigFp)
Expand Down Expand Up @@ -148,7 +148,7 @@ nodeRealParser = do
(GenesisFile genFp)
(DelegationCertFile <$> delCertFp)
(SigningKeyFile <$> sKeyFp)
(SocketFile socketFp)
socketFp
)
nAddress
(ConfigYamlFilePath nodeConfigFp)
Expand Down
14 changes: 6 additions & 8 deletions cardano-node/src/Cardano/Node/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ import Cardano.Common.LocalSocket
import Cardano.Config.Protocol (SomeProtocol(..), fromProtocol)
import Cardano.Config.Topology
import Cardano.Config.Types (ConfigYamlFilePath(..), DbFile(..), NodeMockCLI(..),
NodeProtocolMode (..), NodeRealCLI(..),
SocketFile(..), TopologyFile(..),
NodeProtocolMode (..), NodeRealCLI(..), TopologyFile(..),
parseNodeConfiguration)
import Cardano.Tracing.Tracers
#ifdef UNIX
Expand Down Expand Up @@ -167,16 +166,15 @@ handleSimpleNode p trace nodeTracers npm = do
let tracer = contramap pack $ toLogObject trace
traceWith tracer $ unlines
[ "**************************************"
, "Hostname: " <> hn
, "Node IP: " <> naHostAddress rNodeAddr
, "My producers are " --TODO: Should depend on the jq version of top file
, "**************************************"
]


-- Socket directory
myLocalAddr <- localSocketAddrInfo
Nothing
(unSocket $ socketFile rMscFp)
(socketFile rMscFp)
MkdirIfMissing

addrs <- nodeAddressInfo rNodeAddr
Expand Down Expand Up @@ -221,7 +219,7 @@ handleSimpleNode p trace nodeTracers npm = do
}


removeStaleLocalSocket Nothing (unSocket $ socketFile rMscFp )
removeStaleLocalSocket Nothing (socketFile rMscFp)
dbPath <- canonicalizePath =<< makeAbsolute (unDB $ dBFile rMscFp)
varTip <- atomically $ newTVar GenesisPoint

Expand Down Expand Up @@ -274,7 +272,7 @@ handleSimpleNode p trace nodeTracers npm = do
-- Socket directory
myLocalAddr <- localSocketAddrInfo
(ncNodeId nc)
(unSocket $ socketFile mMscFp)
(socketFile mMscFp)
MkdirIfMissing

addrs <- nodeAddressInfo mockNodeAddr
Expand Down Expand Up @@ -312,7 +310,7 @@ handleSimpleNode p trace nodeTracers npm = do
, dstValency = raValency ra
}

removeStaleLocalSocket (ncNodeId nc) (unSocket $ socketFile mMscFp)
removeStaleLocalSocket (ncNodeId nc) (socketFile mMscFp)
dbPath <- canonicalizePath =<< makeAbsolute (unDB $ dBFile mMscFp)

varTip <- atomically $ newTVar GenesisPoint
Expand Down
3 changes: 2 additions & 1 deletion cardano-node/src/Cardano/Wallet/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ import Ouroboros.Network.Protocol.Handshake.Version
import Ouroboros.Network.NodeToClient

import Cardano.Common.LocalSocket
import Cardano.Config.Types (SocketPath)
import Cardano.Tracing.Tracers (TraceConstraints)

runWalletClient :: forall blk.
( RunNode blk
, TraceConstraints blk
)
=> Protocol blk
-> FilePath
-> SocketPath
-> CoreNodeId
-> Tracer IO String
-> IO ()
Expand Down
Loading

0 comments on commit 6d60295

Please sign in to comment.