Skip to content

Commit

Permalink
Merge #862 #864
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.


864: nix: Provide derivations for Daedalus installer r=KtorZ a=rvl

Relates to #863.
Based on #828.

# Overview

- @disassembler @cleverca22 It's not exactly the same as before but should work ok I think.
- Adds source filtering to avoid unnecessary rebuilds.

# Comments

To build:

```
nix-build -A cardano-wallet-jormungandr
nix-build release.nix -A daedalus-jormungandr.windows -o daedalus-jormungandr-windows
nix-build release.nix -A daedalus-jormungandr.macos -o daedalus-jormungandr-macos
nix-build release.nix -A daedalus-jormungandr.linux -o daedalus-jormungandr-linux
```

Note that `daedalus-jormungandr.{windows,macos,linux}` from `release.nix` reference the same `cardano-wallet-jormungandr` derivation, only with different `system` or `crossSystem` arguments. So Daedalus may also import from `default.nix` rather than `release.nix`.

Co-authored-by: Rodney Lorrimar <rodney.lorrimar@iohk.io>
Co-authored-by: KtorZ <matthias.benkort@gmail.com>
  • Loading branch information
3 people committed Oct 22, 2019
3 parents c3f7cef + bcf292f + cc7b2c2 commit 6ba5bfd
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 195 deletions.
16 changes: 10 additions & 6 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ let
haskell = iohkLib.nix-tools.haskell { inherit pkgs; };
src = iohkLib.cleanSourceHaskell ./.;

inherit (import ./nix/jormungandr.nix { inherit iohkLib pkgs; })
jormungandr jormungandr-cli;
jmPkgs = import ./nix/jormungandr.nix { inherit iohkLib pkgs; };
inherit (jmPkgs) jormungandr jormungandr-cli;

cardano-http-bridge = iohkLib.rust-packages.pkgs.callPackage
./nix/cardano-http-bridge.nix { inherit pkgs; };
Expand All @@ -26,15 +26,19 @@ let
inherit (iohkLib.nix-tools) iohk-extras iohk-module;
};

inherit (haskellPackages.cardano-wallet-core.identifier) version;
in {
inherit pkgs iohkLib src haskellPackages;
inherit pkgs iohkLib src haskellPackages version;
inherit cardano-http-bridge cardano-sl-node jormungandr jormungandr-cli;
inherit (haskellPackages.cardano-wallet-core.identifier) version;

inherit (haskellPackages.cardano-wallet-http-bridge.components.exes)
cardano-wallet-http-bridge;
inherit (haskellPackages.cardano-wallet-jormungandr.components.exes)
cardano-wallet-jormungandr;

cardano-wallet-jormungandr = import ./nix/package-jormungandr.nix {
inherit (haskellPackages.cardano-wallet-jormungandr.components.exes)
cardano-wallet-jormungandr;
inherit pkgs jmPkgs version;
};

tests = collectComponents "tests" isCardanoWallet haskellPackages;
benchmarks = collectComponents "benchmarks" isCardanoWallet haskellPackages;
Expand Down
62 changes: 49 additions & 13 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 @@ -87,22 +89,24 @@ import Options.Applicative
, Parser
, argument
, command
, footer
, footerDoc
, help
, helper
, info
, long
, many
, metavar
, progDesc
, some
, str
)
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 +167,34 @@ 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."
<> footer
"Please note that launch will generate a configuration for Jörmungandr \
\in a folder specified by '--state-dir'."
<> footerDoc (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'"
<> D.line
<> D.line
<> D.text "Please also note that 'launch' will define a 'rest' and" <> D.line
<> D.text "'storage' configuration for Jörmungandr so in case you" <> D.line
<> D.text "provide a configuration file as extra arguments, make sure" <> D.line
<> D.text "not to define any these configuration settings."
)
where
cmd = fmap exec $ LaunchArgs
<$> hostPreferenceOption
Expand All @@ -186,8 +207,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 +217,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 +305,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örmungandr@0.3.999
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 6ba5bfd

Please sign in to comment.