Quick and dirty build and test jormungandr ###################
# HERE BE DRAGONS -- last updated ~ Sun Jun 9 08:56:43 UTC 2019
###################

# FYI This is alpha code (jormungandr) and NOT ready for an actual public testnet.
# Please don't consider this as any official announcement/post.
# I'm just some keyboard cowboy community member who got bored one night.

# Ok so you want to build a BFT testnet...
# Here are some quick and dirty intructions
# NOTE -- spacing in html is crap and i am crap at html
# You will probably need to fix the spacing of the yaml config files

# Some helpful links:
# https://input-output-hk.github.io/jormungandr/quickstart/introduction.html
# https://github.com/input-output-hk/jormungandr


# Get pre-reqs (Ubuntu)
sudo apt-get install build-essential pkg-config git curl libssl-dev

# install rust the easy way
curl https://sh.rustup.rs -sSf | sh
source $HOME/.cargo/env
rustup install stable
rustup default stable

# get the source, checkout a working commit hash
git clone https://github.com/input-output-hk/jormungandr.git
cd jormungandr
git pull # for good measure
git submodule update --init --recursive
# build the binaries (force will be reqired if you've built before)
cargo install --path jcli --force;cargo install --path jormungandr --force

# create a working dir to play in
mkdir -p ~/testnet/storage
cd ~/testnet

# now its time to create some pub and private keys as well as accounts
# some will be used to define the validator nodes
# the others will be used for prefunded wallets

# generate private/public keys https://input-output-hk.github.io/jormungandr/jcli/key.html
# generate addresses https://input-output-hk.github.io/jormungandr/jcli/address.html

# these for loops will create 3 keys for nodes and 3 keys for wallets

for node in `seq 1 3`
do
  jcli key generate --type=Ed25519Extended > node${node}.key
  cat node${node}.key | jcli key to-public > node${node}.pub
done

for acct in `seq 1 3`
do
  jcli key generate --type=Ed25519Extended > acct${acct}.key
  cat acct${acct}.key | jcli key to-public > acct${acct}.pub
  jcli address account --testing `cat acct${acct}.pub` > acct${acct}.addr
done

# now you create the genesis config for your testnet
# NOTE: I ran into an issue using a genesis init template file from an old commit
# you should do this every time you rebuild a new version

jcli genesis init > genesis.yaml

# you will need to edit the file and change a few things
# firstly you will need to replace and add the values of the node#.pub files
# to the consensus_leader_ids array

# then you will need to replace/add the values of the acct#.addr file
# to the initial_funds array, and adding a value (number of ada to prefund)

# now i dont know how to create the certs yet so i just removed that
# so delete initital_certs
# i also updated the linear_fee array and set certificate: 0

# i also dont think we need legacy support so i deleted legacy_funds
# but heh what do i know

vi genesis.yaml

# ok its now time to create the genesis block
# this will tell the testnet that your validators are valid :)
# and will prefund your wallets

jcli genesis encode --input genesis.yaml --output block-0.bin

# now create the config file replace $USER with your username
# replace UNIQUENUMBER with a ... unique number :)
# this will be used by other validator nodes to identify this node

vi config.yaml

storage: "/home/$USER/testnet/storage"
logger:
  verbosity: 3
  format: json
rest:
  listen: "127.0.0.1:8443"
  prefix: "api"
peer_2_peer:
  public_id: UNIQUENUMBER
  trusted_peers:
  public_address: "/ip4/127.0.0.1/tcp/8299"
  topics_of_interests:
    messages: high
    blocks: high


# now create a secrets file for your node you need the contents of node1.key file
# the node2/3 key files aren't used just yet

vi node_secret.yaml
bft:
  signing_key: ed25519e_sk1

# now start the node:

jormungandr --genesis-block block-0.bin --config config.yaml --secret node_secret.yaml

# Enjoy!

# TODO: add instructions for connecting multiple nodes together

# The Lovelace Community Pool is a no fee staking pool run by @kyleo
# Join us on Telegram

# If this was useful and you can/want to support us, our donation address is:

Ae2tdPwUPEYw3rz8KGHbnTusd9QWQ8ePhogEWkm1agugTtW51skA59DrKe8