Skip to content

Commit

Permalink
Merge #862
Browse files Browse the repository at this point in the history
862: jormungandr launch: use --rest-listen instead of generating config.yaml r=KtorZ a=rvl

Relates to #832 and #848.
Supersedes #850.

# Overview

`cardano-wallet-jormungandr launch` specifies the REST API port and storage directory. The user provides the rest of the Jörmungandr configuration (e.g. trusted peers). 

# Comments

The Jörmungandr P2P listen address, port, and log level are no longer configured by cardano-wallet.


Co-authored-by: Rodney Lorrimar <[email protected]>
Co-authored-by: KtorZ <[email protected]>
  • Loading branch information
3 people authored Oct 21, 2019
2 parents d87024c + 782de4c commit fc07f2a
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 125 deletions.
63 changes: 51 additions & 12 deletions lib/jormungandr/exe/cardano-wallet-jormungandr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ import Cardano.Wallet.Version
( showVersion, version )
import Control.Applicative
( optional, (<|>) )
import Data.List
( isPrefixOf )
import Data.Maybe
( fromMaybe )
import Data.Text
Expand All @@ -88,21 +90,25 @@ import Options.Applicative
, argument
, command
, footer
, header
, help
, helper
, info
, long
, many
, metavar
, progDesc
, some
, str
, progDescDoc
)
import Options.Applicative.Types
( readerAsk, readerError )
import System.Exit
( exitWith )
import System.FilePath
( (</>) )

import qualified Data.Text as T
import qualified Options.Applicative.Help.Pretty as D

{-------------------------------------------------------------------------------
Main entry point
Expand Down Expand Up @@ -163,17 +169,35 @@ data LaunchArgs = LaunchArgs

data JormungandrArgs = JormungandrArgs
{ genesisBlock :: Either (Hash "Genesis") FilePath
, extraJormungandrArgs :: [Text]
, extraJormungandrArgs :: [String]
}

cmdLaunch
:: FilePath
-> Mod CommandFields (IO ())
cmdLaunch dataDir = command "launch" $ info (helper <*> cmd) $ mempty
<> progDesc "Launch and monitor a wallet server and its chain producers."
<> header "Launch and monitor a wallet server and its chain producers."
<> progDescDoc (Just $ D.empty
<> D.text "Examples:"
<> D.line
<> D.text "1) Minimal setup, relying on sensible defaults:" <> D.line
<> D.text " launch --genesis-block block0.bin" <> D.line
<> D.line
<> D.text "2) Launching a full node: " <> D.line
<> D.text " launch --genesis-block block0.bin -- --secret secret.yaml" <> D.line
<> D.line
<> D.text "3) Bootstrapping from trusted peers*:" <> D.line
<> D.text " launch --genesis-block-hash 4c05c5bb -- --config config.yaml" <> D.line
<> D.line
<> D.text "(*) assuming 'trusted_peers' is defined in 'config.yaml'"
)
<> footer
"Please note that launch will generate a configuration for Jörmungandr \
\in a folder specified by '--state-dir'."
"Please note that launch will generate a configuration for \
\Jörmungandr in a folder specified by '--state-dir'. You can still \
\supply your own configuration file by using '--config' when \
\passing extra arguments to Jörmungandr but be careful to \
\a) NOT provide a 'rest' configuration and \
\b) NOT provide a 'storage' configuration."
where
cmd = fmap exec $ LaunchArgs
<$> hostPreferenceOption
Expand All @@ -186,8 +210,7 @@ cmdLaunch dataDir = command "launch" $ info (helper <*> cmd) $ mempty
<$> genesisBlockOption
<*> extraArguments)
exec (LaunchArgs hostPreference listen nodePort mStateDir logCfg verbosity jArgs) = do
let minSeverity = verbosityToMinSeverity verbosity
withLogging logCfg minSeverity $ \(cfg, tr) -> do
withLogging logCfg (verbosityToMinSeverity verbosity) $ \(cfg, tr) -> do
case genesisBlock jArgs of
Right block0File -> requireFilePath block0File
Left _ -> pure ()
Expand All @@ -197,7 +220,6 @@ cmdLaunch dataDir = command "launch" $ info (helper <*> cmd) $ mempty
{ _stateDir = stateDir
, _genesisBlock = genesisBlock jArgs
, _restApiPort = fromIntegral . getPort <$> nodePort
, _minSeverity = minSeverity
, _outputStream = Inherit
, _extraArgs = extraJormungandrArgs jArgs
}
Expand Down Expand Up @@ -286,7 +308,24 @@ genesisHashOption = optionT $ mempty
<> help "Blake2b_256 hash of the genesis block, in base 16."

-- | -- [ARGUMENTS...]
extraArguments :: Parser [Text]
extraArguments = some $ argument str $ mempty
extraArguments :: Parser [String]
extraArguments = many $ argument jmArg $ mempty
<> metavar "[-- ARGUMENTS...]"
<> help "Extra arguments to be passed to jormungandr."
<> help "Extra arguments to be passed to Jörmungandr."
where
jmArg = do
arg <- readerAsk
case validate arg of
Just err -> readerError err
Nothing -> pure arg
validate arg
| "--genesis-block" `isPrefixOf` arg = Just $
"The " <> arg <> " option must be placed before the --"
| "--rest-listen" `isPrefixOf` arg = Just $
suggestion "--rest-listen"
| "--storage" `isPrefixOf` arg = Just $
suggestion "--storage"
| otherwise = Nothing
suggestion arg = "The " <> arg <> " argument is used by the launch command."
<> "\nIf you need this level of flexibility, run \"jormungandr\" "
<> "separately and use \"cardano-wallet-jormungandr serve\"."
31 changes: 0 additions & 31 deletions lib/jormungandr/src/Cardano/Wallet/Jormungandr/Compatibility.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ module Cardano.Wallet.Jormungandr.Compatibility
-- * Node's Configuration
, BaseUrl (..)
, Scheme (..)
, genConfigFile
, localhostBaseUrl
, baseUrlToText
) where
Expand All @@ -39,8 +38,6 @@ import Cardano.Wallet.Jormungandr.Environment
( KnownNetwork (..), Network (..) )
import Cardano.Wallet.Jormungandr.Primitive.Types
( Tx (..) )
import Cardano.Wallet.Network.Ports
( PortNumber )
import Cardano.Wallet.Primitive.AddressDerivation
( KeyToAddress (..), getRawKey )
import Cardano.Wallet.Primitive.AddressDerivation.Random
Expand All @@ -60,8 +57,6 @@ import Control.Arrow
( second )
import Control.Monad
( when )
import Data.Aeson
( Value (..), object, (.=) )
import Data.ByteString
( ByteString )
import Data.ByteString.Base58
Expand All @@ -78,14 +73,11 @@ import Data.Word
( Word16 )
import Servant.Client.Core
( BaseUrl (..), Scheme (..), showBaseUrl )
import System.FilePath
( FilePath, (</>) )

import qualified Cardano.Byron.Codec.Cbor as CBOR
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Codec.Binary.Bech32 as Bech32
import qualified Codec.CBOR.Write as CBOR
import qualified Data.Aeson as Aeson
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as B8
import qualified Data.Text as T
Expand Down Expand Up @@ -223,29 +215,6 @@ instance KnownNetwork n => DecodeAddress (Jormungandr n) where
<> B8.unpack (BS.pack [discriminant])
<> "."

-- | Generate a configuration file for Jö[email protected]
genConfigFile
:: FilePath
-> PortNumber
-> BaseUrl
-> Aeson.Value
genConfigFile stateDir addressPort (BaseUrl _ host port _) = object
[ "storage" .= (stateDir </> "chain")
, "rest" .= object
[ "listen" .= String listen ]
, "p2p" .= object
[ "trusted_peers" .= ([] :: [()])
, "topics_of_interest" .= object
[ "messages" .= String "low"
, "blocks" .= String "normal"
]
, "public_address" .= String publicAddress
]
]
where
listen = T.pack $ mconcat [host, ":", show port]
publicAddress = T.pack $ mconcat ["/ip4/127.0.0.1/tcp/", show addressPort]

{-------------------------------------------------------------------------------
Base URL
-------------------------------------------------------------------------------}
Expand Down
45 changes: 11 additions & 34 deletions lib/jormungandr/src/Cardano/Wallet/Jormungandr/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ module Cardano.Wallet.Jormungandr.Network

import Prelude

import Cardano.BM.Data.Severity
( Severity (..) )
import Cardano.BM.Trace
( Trace, logInfo )
( Trace )
import Cardano.Launcher
( Command (..)
, ProcessHasExited
Expand Down Expand Up @@ -95,7 +93,7 @@ import Cardano.Wallet.Jormungandr.Api.Client
import Cardano.Wallet.Jormungandr.Binary
( runGetOrFail )
import Cardano.Wallet.Jormungandr.Compatibility
( Jormungandr, genConfigFile, localhostBaseUrl )
( Jormungandr, localhostBaseUrl )
import Cardano.Wallet.Network
( Cursor, NetworkLayer (..), NextBlocksResult (..), defaultRetryPolicy )
import Cardano.Wallet.Network.BlockHeaders
Expand All @@ -118,7 +116,7 @@ import Cardano.Wallet.Primitive.Types
import Control.Concurrent.MVar.Lifted
( MVar, modifyMVar, newMVar, readMVar )
import Control.Exception
( Exception, bracket )
( Exception )
import Control.Monad.IO.Class
( liftIO )
import Control.Monad.Trans.Class
Expand All @@ -131,8 +129,6 @@ import Data.ByteArray.Encoding
( Base (Base16), convertToBase )
import Data.Coerce
( coerce )
import Data.Function
( (&) )
import Data.Map.Strict
( Map )
import Data.Quantity
Expand All @@ -141,18 +137,13 @@ import Data.Text
( Text )
import Data.Word
( Word32, Word64 )
import System.Directory
( removeFile )
import System.FilePath
( (</>) )

import qualified Cardano.Wallet.Jormungandr.Binary as J
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy as BL
import qualified Data.Char as C
import qualified Data.Map as Map
import qualified Data.Text as T
import qualified Data.Yaml as Yaml

-- | Whether to start Jormungandr with the given config, or to connect to an
-- already running Jormungandr REST API using the given parameters.
Expand All @@ -173,9 +164,8 @@ data JormungandrConfig = JormungandrConfig
{ _stateDir :: FilePath
, _genesisBlock :: Either (Hash "Genesis") FilePath
, _restApiPort :: Maybe PortNumber
, _minSeverity :: Severity
, _outputStream :: StdStream
, _extraArgs :: [Text]
, _extraArgs :: [String]
} deriving (Show, Eq)

-- | Starts the network layer and runs the given action with a
Expand Down Expand Up @@ -477,28 +467,15 @@ withJormungandr
-> (JormungandrConnParams -> IO a)
-- ^ Action to run while node is running.
-> IO (Either ErrStartup a)
withJormungandr tr (JormungandrConfig stateDir block0 mPort logSeverity output extraArgs) cb =
bracket setupConfig cleanupConfig startBackend
where
nodeConfigFile = stateDir </> "jormungandr-config.yaml"
setupConfig = do
apiPort <- maybe getRandomPort pure mPort
p2pPort <- getRandomPort
let baseUrl = localhostBaseUrl $ fromIntegral apiPort
genConfigFile stateDir p2pPort baseUrl
& Yaml.encodeFile nodeConfigFile
logInfo tr $ mempty
<> "Generated Jörmungandr's configuration to: "
<> T.pack nodeConfigFile
pure (apiPort, baseUrl)
cleanupConfig _ = removeFile nodeConfigFile

startBackend (apiPort, baseUrl) = getGenesisBlockArg block0 >>= \case
withJormungandr tr (JormungandrConfig stateDir block0 mPort output extraArgs) cb = do
apiPort <- maybe getRandomPort pure mPort
let baseUrl = localhostBaseUrl $ fromIntegral apiPort
getGenesisBlockArg block0 >>= \case
Right (block0H, genesisBlockArg) -> do
let args = genesisBlockArg ++
[ "--config", nodeConfigFile
, "--log-level", C.toLower <$> show logSeverity
] ++ map T.unpack extraArgs
[ "--rest-listen", "127.0.0.1:" <> show apiPort
, "--storage", stateDir </> "chain"
] ++ extraArgs
let cmd = Command "jormungandr" args (return ()) output
let tr' = transformLauncherTrace tr
res <- withBackendProcess tr' cmd $
Expand Down
Loading

0 comments on commit fc07f2a

Please sign in to comment.