Skip to content
This repository was archived by the owner on Jun 13, 2018. It is now read-only.

Commit e8add39

Browse files
committed
dapp testnet: easily spawn a PoA testnet (demo)
This introduces two things: * dapp --nix-run which can run programs via Nix. For maximum convenience and reliability, we run Geth via Nix, so it's downloaded on first run. * dapp testnet which runs a 1-node PoA 5s blocktime testnet. It uses a private hidden folder in ~/.dapp/testnet which is autodeleted when you C-c the `dapp testnet` command. I've verified that while the testnet is running, I can use `dapp create` with the exposed localhost RPC. (You have to set $ETH_FROM to the address of the testnet genesis account.) Let's build more convenience features for interacting with local testnets!
1 parent 93f9759 commit e8add39

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

libexec/dapp/dapp---nix-run

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env bash
2+
# Usage: dapp --nix-run PKG COMMAND...
3+
# Example:
4+
# $ dapp --nix-run go-ethereum geth --version
5+
#
6+
# Runs a command with the binaries from a named Nix package in PATH.
7+
8+
# It's basically an implementation of nix-shell(1) but simpler and
9+
# without actually starting a subshell, and using ~/.dapp/nix as an
10+
# "indirect GC root."
11+
12+
set -e
13+
14+
have() { command -v "$1" >/dev/null; }
15+
{ have nix-channel && have nix-env && have nix-shell; } || {
16+
echo >&2 "${0##*/}: error: The Nix package manager is required."
17+
echo >&2 "${0##*/}: error: See https://dapp.tools for installation instructions."
18+
exit 1
19+
}
20+
21+
channel=$HOME/.nix-defexpr/channels/dapphub
22+
23+
if [[ ! -d "$channel" ]]; then
24+
echo >&2 "${0##*/}: DappHub Nix channel not present; adding..."
25+
( set -x; nix-channel --add https://nix.dapphub.com/pkgs/dapphub )
26+
( set -x; nix-channel --update )
27+
fi
28+
29+
joinpaths() { printf "%s" "$*" | sed 's/ /:/g' ; }
30+
31+
# Let's have a custom directory for semi-temporary files related to
32+
# Nix packages. It will contain symlinks to store paths.
33+
pkgs=$HOME/.dapp/nix/pkgs
34+
mkdir -p "$pkgs"
35+
36+
attr="$1"; shift
37+
38+
# First step in Nix jargon: instantiate the package as a
39+
# derivation file.
40+
#
41+
# A package is a fully evaluated Nix expression which describes a
42+
# build product -- a "derivation" which is then saved as a file in
43+
# the store.
44+
drvpath=$(nix-instantiate --indirect --add-root "$pkgs"/"$attr".drv \
45+
"$channel" -A "$attr")
46+
drvpath=$(sed 's/!.*$//' <<<"$drvpath")
47+
48+
# The derivation will have a number of output paths, either of which
49+
# might contain the ./bin that we're interested in, so we'll accumulate
50+
# an array of PATH entries to use.
51+
paths=()
52+
53+
# This while loop gets its stdin from a Nix command; see below.
54+
while read output; do
55+
56+
name=$(basename "$output")
57+
path="$HOME/.dapp/nix/pkgs/$name"
58+
paths+=("$path"/bin)
59+
60+
if [[ ! -d "$path" ]]; then
61+
# The derivation's output paths must be "realised", which in our
62+
# case should mean downloading them from the binary cache.
63+
echo >&2 "${0##*/}: Need Nix package: $name"
64+
nix-store \
65+
-Q --indirect --add-root ~/.dapp/nix/pkgs/"$name" \
66+
--realise "$output" >/dev/null \
67+
2> >(sed 's/^/nix: /' >&2)
68+
fi
69+
70+
done < <(nix-store --query "$drvpath")
71+
72+
PATH=$(joinpaths "${paths[@]}"):$PATH "$@"

libexec/dapp/dapp---testnet-launch

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
gethdir=$HOME/.dapp/testnet
5+
chainid=2000
6+
while true; do
7+
if [[ ! -d "$gethdir/$chainid" ]]; then break; fi
8+
chainid=$((chainid + 1))
9+
done
10+
11+
chaindir="$gethdir/$chainid"
12+
13+
echo >&2 "dapp-testnet: Chain ID: $chainid"
14+
echo >&2 "dapp-testnet: Database: $chaindir"
15+
16+
mkdir -p "$chaindir"
17+
18+
function clean() { rm -rf "$chaindir"; }
19+
trap clean EXIT
20+
21+
address=$(
22+
geth 2>/dev/null account new --datadir "$chaindir" --password=<(exit) 2>/dev/null \
23+
| grep Address | sed 's/Address: {\(.*\)}/\1/')
24+
25+
echo >&2 "dapp-testnet: Whale address: $address"
26+
echo >&2 "dapp-testnet: RPC URL: http://127.0.0.1:$chainid"
27+
28+
jshon >"$chaindir/genesis.json" \
29+
-n {} \
30+
-n {} \
31+
-n "$chainid" -i chainId \
32+
-n 0 -i homesteadBlock \
33+
-n 0 -i eip155Block \
34+
-n 0 -i eip158Block \
35+
-n 0 -i byzantiumBlock \
36+
-n {} -n 5 -i period -n 3000 -i epoch -i clique \
37+
-i config \
38+
-s "0x1" -i difficulty \
39+
-s "0x3938700" -i gaslimit \
40+
-s "0x3132333400000000000000000000000000000000000000000000000000000000""$address""0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" -i extraData \
41+
-n {} \
42+
-n {} -s "0x21e19e0c9bab2400000" -i balance \
43+
-i "$address" \
44+
-i alloc
45+
46+
geth 2>/dev/null --datadir "$chaindir" init "$chaindir/genesis.json"
47+
48+
geth \
49+
--datadir "$chaindir" --mine --rpc --networkid "$chainid" \
50+
--minerthreads=1 \
51+
--rpcport="$chainid" --unlock="$address" --password=<(exit)

libexec/dapp/dapp-testnet

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
dapp --nix-run go-ethereum dapp --testnet-launch

0 commit comments

Comments
 (0)