Skip to content

Commit

Permalink
Implement RealNodeTopology
Browse files Browse the repository at this point in the history
While running a real protocol, the node determines its ip address
and the peers it will connect to from a JSON configuration file.
  • Loading branch information
Jimbo4350 committed Nov 26, 2019
1 parent 02a9271 commit 09fa19d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
12 changes: 12 additions & 0 deletions cardano-config/src/Cardano/Config/Topology.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Cardano.Config.Topology
, NodeAddress(..)
, NodeHostAddress(..)
, NodeSetup(..)
, RealNodeTopology(..)
, RemoteAddress(..)
, createNodeAddress
, nodeAddressInfo
Expand Down Expand Up @@ -134,6 +135,17 @@ data NodeSetup = NodeSetup
, producers :: ![RemoteAddress]
} deriving Show

data RealNodeTopology = RealNodeTopology
{ rNodeAddress :: !NodeAddress
, rProducers :: ![RemoteAddress]
}

instance FromJSON RealNodeTopology where
parseJSON = withObject "RealNodeTopology" $ \v ->
RealNodeTopology
<$> v .: "rNodeAddress"
<*> (v .: "rProducers")

instance FromJSON NodeId where
parseJSON v = CoreId <$> parseJSON v

Expand Down
35 changes: 30 additions & 5 deletions cardano-node/src/Cardano/Node/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ import Cardano.Prelude hiding (ByteString, atomically, take, trace)
import Prelude (error, id, unlines)

import qualified Control.Concurrent.Async as Async
import Control.Exception (IOException)
import qualified Control.Exception as Exception
import Control.Tracer
import Data.Aeson (eitherDecode)
import qualified Data.ByteString.Char8 as BSC
import qualified Data.ByteString.Lazy as LB
import Data.Either (partitionEithers)
import Data.Functor.Contravariant (contramap)
import qualified Data.List as List
Expand Down Expand Up @@ -172,13 +176,21 @@ handleSimpleNode p trace nodeTracers npm = do
RealProtocolMode (NodeRealCLI rMscFp rNodeAddr _ _) -> do
let pInfo@ProtocolInfo{ pInfoConfig = cfg } = protocolInfo p

hn <- getHostName
-- Topology
eitherTopology <- readRealNodeTopology . unTopology $ topFile rMscFp
topology <- case eitherTopology of
--TODO: Convert handleSimpleNode to return `ExceptT`
Left err -> panic $ "Cardano.Node.Run.readRealNodeTopology: "
<> err
Right top -> pure top

-- Tracing
let tracer = contramap pack $ toLogObject trace
traceWith tracer $ unlines
[ "**************************************"
, "Hostname: " <> hn
, "My producers are " --TODO: Should depend on the jq version of top file
[ ""
, "**************************************"
, "Host node address: " <> (show $ rNodeAddress topology)
, "My producers are " <> (show $ rProducers topology)
, "**************************************"
]

Expand All @@ -196,7 +208,7 @@ handleSimpleNode p trace nodeTracers npm = do
dnsProducerAddrs :: [RemoteAddress]
(ipProducerAddrs, dnsProducerAddrs) = partitionEithers
[ maybe (Right ra) Left $ remoteAddressToNodeAddress ra
| ra <- [RemoteAddress "18.185.45.45" 3001 1] ]
| ra <- rProducers topology ]
ipProducers :: [SockAddr]
ipProducers = nodeAddressToSockAddr <$> ipProducerAddrs

Expand Down Expand Up @@ -350,3 +362,16 @@ handleSimpleNode p trace nodeTracers npm = do
Just (CoreId n) -> n
Just (RelayId _) -> error "Non-core nodes currently not supported"
Nothing -> 999

-- | Read the `RealNodeTopology` 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.
readRealNodeTopology :: FilePath -> IO (Either Text RealNodeTopology)
readRealNodeTopology fp = do
ebs <- Exception.try $ BSC.readFile fp :: IO (Either IOException BSC.ByteString)
case ebs of
Left e -> pure $ handler e
Right bs -> pure . first toS . eitherDecode $ LB.fromStrict bs
where
handler :: IOException -> Either Text RealNodeTopology
handler e = Left . pack $ show e
25 changes: 12 additions & 13 deletions configuration/topology-proxy-follower.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
[
{ "nodeId": 0
, "nodeAddress":
{ "addr": "127.0.0.1"
, "port": 7776
}
, "producers":
[ { "addr": "18.185.45.45"
, "port": 3001
, "valency": 1
}
]
{
"rNodeAddress": {
"addr": "127.0.0.1",
"port": 7776
},
"rProducers": [
{
"addr": "18.185.45.45",
"port": 3001,
"valency": 1
}
]
]
}
5 changes: 2 additions & 3 deletions scripts/mainnet-proxy-follower.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
set -e

RUNNER=${RUNNER:-cabal new-run --}
TOPOLOGY=${TOPOLOGY:-"configuration/topology-proxy-follower.json"}

ARGS=( real-protocol
--database-path "./db"
--genesis-file "configuration/mainnet-genesis.json"
--topology "${TOPOLOGY}"
--socket-dir "./socket/singlenode"
--topology "configuration/topology-proxy-follower.json"
--socket-path "./socket/singlenode"
--config "./configuration/mainnet-proxy-follower.yaml"
--port 7776
)
Expand Down

0 comments on commit 09fa19d

Please sign in to comment.