Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Add "local" network to launcher, and --state-dir for the database #377

Merged
merged 4 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ executable cardano-wallet-launcher
build-depends:
base
, cardano-wallet-cli
, cardano-wallet-http-bridge
, cardano-wallet-launcher
, directory
, docopt
, filepath
, fmt
, process
, say
Expand Down
70 changes: 44 additions & 26 deletions exe/launcher/Main.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE QuasiQuotes #-}

module Main where
Expand All @@ -14,12 +15,12 @@ import Cardano.Launcher
, installSignalHandlers
, launch
)
import Cardano.Wallet.HttpBridge.Environment
( Network )
import Control.Concurrent
( threadDelay )
import Control.Monad
( when )
import Data.Maybe
( fromMaybe )
import Data.Text.Class
( FromText (..), ToText (..) )
import Data.Version
Expand All @@ -29,21 +30,26 @@ import Fmt
import Paths_cardano_wallet
( version )
import Say
( sayErr )
( sayErr, sayString )
import System.Console.Docopt
( Arguments
, Docopt
, Option
, docopt
, getArg
, isPresent
, longOption
, parseArgsOrExit
, shortOption
)
import System.Directory
( createDirectory, doesDirectoryExist )
import System.Environment
( getArgs )
import System.Exit
( exitSuccess, exitWith )
import System.FilePath
( (</>) )

import qualified Data.Text as T

Expand All @@ -66,9 +72,10 @@ Usage:
cardano-wallet-launcher --version

Options:
--network <STRING> testnet, staging, or mainnet [default: testnet]
--network <STRING> testnet, staging, mainnet, or local [default: testnet]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is local coming back?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of Daedalus integrating with the wallet backend. They also need to be able to spin-up a local cluster for their own testing.

--wallet-server-port <PORT> port used for serving the wallet API [default: 8090]
--http-bridge-port <PORT> port used for communicating with the http-bridge [default: 8080]
--state-dir <DIR> write wallet state (blockchain and database) to this directory
|]

main :: IO ()
Expand All @@ -80,15 +87,17 @@ main = do
putStrLn (showVersion version)
exitSuccess

network <- args `parseArg` longOption "network"
let stateDir = args `getArg` (longOption "state-dir")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to add integration test that'd check if the directory and contents are created correctly.
Could be here -> https://github.com/input-output-hk/cardano-wallet/tree/master/lib/http-bridge/test/integration/Test/Integration/Scenario/CLI

let network = fromMaybe "testnet" $ args `getArg` (longOption "network")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small point, but parentheses are not actually needed in the above two lines.

bridgePort <- args `parseArg` longOption "http-bridge-port"
walletPort <- args `parseArg` longOption "wallet-server-port"

sayErr "Starting..."
installSignalHandlers
maybe (pure ()) setupStateDir stateDir
let commands =
[ nodeHttpBridgeOn bridgePort network
, walletOn walletPort bridgePort
[ nodeHttpBridgeOn stateDir bridgePort network
, walletOn stateDir walletPort bridgePort network
]
sayErr $ fmt $ blockListF commands
(ProcessHasExited name code) <- launch commands
Expand All @@ -98,24 +107,33 @@ main = do
parseArg :: FromText a => Arguments -> Option -> IO a
parseArg = parseArgWith cli

nodeHttpBridgeOn :: Port "Node" -> Network -> Command
nodeHttpBridgeOn port net = Command
"cardano-http-bridge"
[ "start"
, "--port", T.unpack (toText port)
, "--template", T.unpack (toText net)
]
(return ())
Inherit

walletOn :: Port "Wallet" -> Port "Node" -> Command
walletOn wp np = Command
"cardano-wallet"
[ "server"
, "--port", T.unpack (toText wp)
, "--bridge-port", T.unpack (toText np)
]
(threadDelay oneSecond)
Inherit
nodeHttpBridgeOn :: Maybe FilePath -> Port "Node" -> String -> Command
nodeHttpBridgeOn stateDir port net =
Command "cardano-http-bridge" args (return ()) Inherit
where
args =
[ "start"
, "--port", T.unpack (toText port)
, "--template", net
] ++ networkArg
networkArg = maybe [] (\d -> ["--networks-dir", d]) stateDir

walletOn :: Maybe FilePath -> Port "Wallet" -> Port "Node" -> String -> Command
walletOn stateDir wp np net =
Command "cardano-wallet" args (threadDelay oneSecond) Inherit
where
args =
[ "server"
, "--network", if net == "local" then "testnet" else net
, "--port", T.unpack (toText wp)
, "--bridge-port", T.unpack (toText np)
] ++ dbArg
dbArg = maybe [] (\d -> ["--database", d </> "wallet.db"]) stateDir
oneSecond = 1000000

setupStateDir :: FilePath -> IO ()
setupStateDir dir = doesDirectoryExist dir >>= \case
True -> sayString $ "Using state directory: " ++ dir
False -> do
sayString $ "Creating state directory: " ++ dir
createDirectory dir
9 changes: 6 additions & 3 deletions exe/wallet/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import System.Console.Docopt
, command
, docopt
, exitWithUsage
, getArg
, isPresent
, longOption
, parseArgsOrExit
Expand All @@ -110,7 +111,7 @@ import System.IO
( BufferMode (NoBuffering), hSetBuffering, stderr, stdout )

import qualified Cardano.Wallet.Api.Server as Server
import qualified Cardano.Wallet.DB.MVar as MVar
import qualified Cardano.Wallet.DB.Sqlite as Sqlite
import qualified Cardano.Wallet.HttpBridge.Network as HttpBridge
import qualified Cardano.Wallet.HttpBridge.Transaction as HttpBridge
import qualified Data.Aeson as Aeson
Expand All @@ -135,7 +136,7 @@ and can be run "offline". (e.g. 'generate mnemonic')
⚠️ Options are positional (--a --b is not equivalent to --b --a) ! ⚠️

Usage:
cardano-wallet server [--network=STRING] [--port=INT] [--bridge-port=INT]
cardano-wallet server [--network=STRING] [--port=INT] [--bridge-port=INT] [--database=FILE]
cardano-wallet mnemonic generate [--size=INT]
cardano-wallet wallet list [--port=INT]
cardano-wallet wallet create [--port=INT] <name> [--address-pool-gap=INT]
Expand All @@ -154,6 +155,7 @@ Options:
--size <INT> number of mnemonic words to generate [default: 15]
--payment <PAYMENT> address to send to and amount to send separated by @: '<amount>@<address>'
--network <STRING> testnet, staging, or mainnet [default: testnet]
--database <FILE> use this file for storing wallet state

Examples:
# Create a transaction and send 22 lovelace from wallet-id to specified address
Expand Down Expand Up @@ -339,7 +341,8 @@ execHttpBridge args _ = do
<- args `parseArg` longOption "port"
(bridgePort :: Int)
<- args `parseArg` longOption "bridge-port"
db <- MVar.newDBLayer
let dbFile = args `getArg` longOption "database"
(_, db) <- Sqlite.newDBLayer dbFile
nw <- HttpBridge.newNetworkLayer @n bridgePort
waitForConnection nw defaultRetryPolicy
let tl = HttpBridge.newTransactionLayer @n
Expand Down