Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
[CO-390] Create a 'demo' executable for cardano-sl-wallet-new
Browse files Browse the repository at this point in the history
This code makes use of the 'Cardano.Wallet.Demo' module and show
how it can be used to spawn a demo cluster of nodes.
More details in the README.md
  • Loading branch information
KtorZ committed Sep 25, 2018
1 parent 3485f7f commit 337b08f
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
18 changes: 18 additions & 0 deletions wallet-new/cardano-sl-wallet-new.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -712,3 +712,21 @@ benchmark cardano-sl-wallet-new-bench
BangPatterns
TemplateHaskell
ScopedTypeVariables

test-suite demo
type: exitcode-stdio-1.0

default-language: Haskell2010
default-extensions: NoImplicitPrelude
FlexibleContexts
OverloadedStrings
RecordWildCards
ScopedTypeVariables

hs-source-dirs: demo
main-is: Main.hs

build-depends: base
, async
, cardano-sl-wallet-new
, universum
53 changes: 53 additions & 0 deletions wallet-new/demo/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{-| Demo cluster of wallet nodes. See demo/README.md -}

{-# LANGUAGE LambdaCase #-}

module Main where

import Universum

import Control.Concurrent.Async (waitAny)
import System.IO (BufferMode (..), hSetBuffering, stdout)

import Cardano.Wallet.Demo (MaxWaitingTime (..), NodeName (..),
NodeType (..), RunningNode (..), startCluster,
waitForNode)


-- | Cluster configuration can be tweaked via ENV vars. Each ENV var is prefixed
-- with the following.
--
-- (e.g. `DEMO_NO_CLIENT_AUTH=True`)
prefix :: String
prefix = "DEMO_"


main :: IO ()
main = void $ do
hSetBuffering stdout NoBuffering -- Instead of LineBuffering

putTextLn "Cluster is starting..."
cluster <- startCluster prefix
[ ("core0", NodeCore)
, ("core1", NodeCore)
, ("core2", NodeCore)
, ("core3", NodeCore)
, ("relay", NodeRelay)
, ("wallet", NodeEdge)
]
handles <- forM cluster $ \case
RunningCoreNode (NodeName nodeId) handle -> do
putTextLn $ "..." <> nodeId <> " skipped!"
return handle

RunningRelayNode (NodeName nodeId) handle -> do
putTextLn $ "..." <> nodeId <> " skipped!"
return handle

RunningWalletNode (NodeName nodeId) client handle -> do
putText "..." >> waitForNode client (MaxWaitingTime 10)
putTextLn $ nodeId <> " OK!"
return handle
putTextLn "Cluster is ready!"

waitAny handles
76 changes: 76 additions & 0 deletions wallet-new/demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Demo

## Getting Started

This module provides an executable for starting a demo cluster of wallet nodes.
It is designed to remove all the overhead of setting up a configuration
and an environment and to _just work_, out-of-the-box. Minor configuration
adjustments are however possible via environment variables.

```
stack test cardano-sl-wallet-new:demo
```

This spawns three wallet-nodes running respectively on:

| NodeId | Address | API Address | API Doc Address |
| --- | --- | --- | --- |
| node0 | localhost:3000 | localhost:8090 | localhost:8190 |
| node1 | localhost:3001 | localhost:8091 | localhost:8191 |
| node2 | localhost:3002 | localhost:8092 | localhost:8192 |


## Configuring Nodes

Almost _anything_ from the normal CLI arguments of a node or a wallet node can be
configured via an ENV variable using an `UPPER_SNAKE_CASE` naming, correctly
prefixed with `DEMO_` with a few gotchas:

- Flags need an explicit boolean value

- There's no ENV vars mapping to (i.e. you can't configure):
- `--topology`
- `--tlscert`
- `--tlsca`
- `--tlskey`
- `--logger-config`
- `--node-id`
- `--db-path`
- `--wallet-db-path`

- There's an extra `LOG_SEVERITY` variable that can be set to `Debug`, `Info`
and so forth to ajust logging severity for _all_ nodes.

- When it make senses, variable values are automatically incremented by the
node index. For instance, if you provide `LISTEN=127.0.0.1:3000`, then
- node0 will receive "127.0.0.1:3000"
- node1 will receive "127.0.0.1:3001"
- node2 will receive "127.0.0.1:3002"

This is the case for:
- `--listen`
- `--wallet-address`
- `--wallet-doc-address`

For instance, one can disable TLS client authentication doing:

```
DEMO_NO_CLIENT_AUTH=True stack test cardano-sl-wallet-new:demo
```


### Relative FilePath

One can provide relative filepath as values for ENV vars. They are computed from
the `wallet-new` folder, so for instance, providing `./state-demo` will point
to the directory `$(git rev-parse --show-toplevel)/wallet-new/state-demo`.


### State Directory

By default, each node receives a temporary state directory from the system;
probably somewhere in `/tmp`. This location can always be overriden by
providing an extra `DEMO_STATE_DIR` variable with a custom location.

Note that, each default has been choosen in such way that they won't conflict
if all nodes were to share the same state directory :)

0 comments on commit 337b08f

Please sign in to comment.