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

Commit f30a720

Browse files
committed
[CO-390] Create a 'demo' executable for cardano-sl-wallet-new
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
1 parent 70a7ca2 commit f30a720

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

wallet-new/cardano-sl-wallet-new.cabal

+18
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,21 @@ benchmark cardano-sl-wallet-new-bench
714714
BangPatterns
715715
TemplateHaskell
716716
ScopedTypeVariables
717+
718+
test-suite demo
719+
type: exitcode-stdio-1.0
720+
721+
default-language: Haskell2010
722+
default-extensions: NoImplicitPrelude
723+
FlexibleContexts
724+
OverloadedStrings
725+
RecordWildCards
726+
ScopedTypeVariables
727+
728+
hs-source-dirs: demo
729+
main-is: Main.hs
730+
731+
build-depends: base
732+
, async
733+
, cardano-sl-wallet-new
734+
, universum

wallet-new/demo/Main.hs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{-| Demo cluster of wallet nodes. See demo/README.md -}
2+
3+
{-# LANGUAGE LambdaCase #-}
4+
5+
module Main where
6+
7+
import Universum
8+
9+
import Control.Concurrent.Async (waitAny)
10+
import System.IO (BufferMode (..), hSetBuffering, stdout)
11+
12+
import Cardano.Wallet.Demo (MaxWaitingTime (..), NodeName (..),
13+
NodeType (..), RunningNode (..), startCluster,
14+
waitForNode)
15+
16+
17+
-- | Cluster configuration can be tweaked via ENV vars. Each ENV var is prefixed
18+
-- with the following.
19+
--
20+
-- (e.g. `DEMO_NO_CLIENT_AUTH=True`)
21+
prefix :: String
22+
prefix = "DEMO_"
23+
24+
25+
main :: IO ()
26+
main = void $ do
27+
hSetBuffering stdout NoBuffering -- Instead of LineBuffering
28+
29+
putTextLn "Cluster is starting..."
30+
cluster <- startCluster prefix
31+
[ ("core0", NodeCore)
32+
, ("core1", NodeCore)
33+
, ("core2", NodeCore)
34+
, ("core3", NodeCore)
35+
, ("relay", NodeRelay)
36+
, ("wallet", NodeEdge)
37+
]
38+
handles <- forM cluster $ \case
39+
RunningCoreNode (NodeName nodeId) handle -> do
40+
putTextLn $ "..." <> nodeId <> " skipped!"
41+
return handle
42+
43+
RunningRelayNode (NodeName nodeId) handle -> do
44+
putTextLn $ "..." <> nodeId <> " skipped!"
45+
return handle
46+
47+
RunningWalletNode (NodeName nodeId) client handle -> do
48+
putText "..." >> waitForNode client (MaxWaitingTime 10)
49+
putTextLn $ nodeId <> " OK!"
50+
return handle
51+
putTextLn "Cluster is ready!"
52+
53+
waitAny handles

wallet-new/demo/README.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Demo
2+
3+
## Getting Started
4+
5+
This module provides an executable for starting a demo cluster of wallet nodes.
6+
It is designed to remove all the overhead of setting up a configuration
7+
and an environment and to _just work_, out-of-the-box. Minor configuration
8+
adjustments are however possible via environment variables.
9+
10+
```
11+
stack test cardano-sl-wallet-new:demo
12+
```
13+
14+
This spawns three wallet-nodes running respectively on:
15+
16+
| NodeId | Address | API Address | API Doc Address |
17+
| --- | --- | --- | --- |
18+
| node0 | localhost:3000 | localhost:8090 | localhost:8190 |
19+
| node1 | localhost:3001 | localhost:8091 | localhost:8191 |
20+
| node2 | localhost:3002 | localhost:8092 | localhost:8192 |
21+
22+
23+
## Configuring Nodes
24+
25+
Almost _anything_ from the normal CLI arguments of a node or a wallet node can be
26+
configured via an ENV variable using an `UPPER_SNAKE_CASE` naming, correctly
27+
prefixed with `DEMO_` with a few gotchas:
28+
29+
- Flags need an explicit boolean value
30+
31+
- There's no ENV vars mapping to (i.e. you can't configure):
32+
- `--topology`
33+
- `--tlscert`
34+
- `--tlsca`
35+
- `--tlskey`
36+
- `--logger-config`
37+
- `--node-id`
38+
- `--db-path`
39+
- `--wallet-db-path`
40+
41+
- There's an extra `LOG_SEVERITY` variable that can be set to `Debug`, `Info`
42+
and so forth to ajust logging severity for _all_ nodes.
43+
44+
- When it make senses, variable values are automatically incremented by the
45+
node index. For instance, if you provide `LISTEN=127.0.0.1:3000`, then
46+
- node0 will receive "127.0.0.1:3000"
47+
- node1 will receive "127.0.0.1:3001"
48+
- node2 will receive "127.0.0.1:3002"
49+
50+
This is the case for:
51+
- `--listen`
52+
- `--wallet-address`
53+
- `--wallet-doc-address`
54+
55+
For instance, one can disable TLS client authentication doing:
56+
57+
```
58+
DEMO_NO_CLIENT_AUTH=True stack test cardano-sl-wallet-new:demo
59+
```
60+
61+
62+
### Relative FilePath
63+
64+
One can provide relative filepath as values for ENV vars. They are computed from
65+
the `wallet-new` folder, so for instance, providing `./state-demo` will point
66+
to the directory `$(git rev-parse --show-toplevel)/wallet-new/state-demo`.
67+
68+
69+
### State Directory
70+
71+
By default, each node receives a temporary state directory from the system;
72+
probably somewhere in `/tmp`. This location can always be overriden by
73+
providing an extra `DEMO_STATE_DIR` variable with a custom location.
74+
75+
Note that, each default has been choosen in such way that they won't conflict
76+
if all nodes were to share the same state directory :)

0 commit comments

Comments
 (0)