Files
stacks-puppet-node/bin/blockstack-genesis-block
2018-07-05 18:46:48 -04:00

77 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
GENESIS_BLOCK_REPO="$1"
START_BLOCK="$2"
UNLOCK_PERIOD="$3"
UNLOCK_INTERVAL="$4"
WHITELISTED_COMMITS="$5"
if [ -z "$GENESIS_BLOCK_REPO" ] || [ -z "$START_BLOCK" ] || [ -z "$UNLOCK_PERIOD" ] || [ -z "$UNLOCK_INTERVAL" ]; then
echo "Usage: $0 GENESIS_BLOCK_REPO START_BLOCK UNLOCK_PERIOD UNLOCK_INTERVAL [WHITELISTED_COMMITS]"
exit 1
fi
if [ -n "$WHITELISTED_COMMITS" ] && [ -f "$WHITELISTED_COMMITS" ]; then
WHITELISTED_COMMITS="$(realpath "$WHITELISTED_COMMITS")"
fi
# make sure our tools exist
TOOLS="./genesis-block"
if ! [ -d "$TOOLS" ]; then
echo "Fatal: could not find genesis block tools directory. Re-run this program in the blockstack-core/bin directory."
exit 1
fi
TOOLS="$(realpath "$TOOLS")"
set -e
WORKDIR="$(mktemp -d)"
GENESIS_BLOCK_FILE="genesis_block.json"
pushd "$WORKDIR" >/dev/null
git clone "$GENESIS_BLOCK_REPO" genesis-block >/dev/null 2>&1
if ! [ -f "genesis-block/$GENESIS_BLOCK_FILE" ]; then
echo "No genesis block found at $(pwd)/genesis-block/$GENESIS_BLOCK_FILE"
exit 1
fi
pushd "genesis-block" >/dev/null
set -e
# sanitize genesis block
cat "$GENESIS_BLOCK_FILE" | python2 -c 'from ast import literal_eval; import sys; import json; print json.dumps(literal_eval(sys.stdin.read()))' > '.sanitized-genesis-block.json'
# expand genesis block
cat ".sanitized-genesis-block.json" | "$TOOLS/blockstack-genesis-block-expand" "$START_BLOCK" "$UNLOCK_PERIOD" "$UNLOCK_INTERVAL" > ".genesis-block-expanded.json"
# create and genesis block history
"$TOOLS/blockstack-genesis-block-history-extract" "." "$WHITELISTED_COMMITS" > ".genesis-block-history.json"
"$TOOLS/blockstack-genesis-block-history-verify" ".genesis-block-history.json"
# merge them together into a single Python 2.x source file
echo '#!/usr/bin/env python2'
echo ""
echo "# Generated by blockstack-genesis-block"
echo "# DO NOT EDIT"
echo ""
echo "import json"
echo ""
echo "# javascript compat"
echo "false = False"
echo "true = True"
echo ""
echo -n "GENESIS_BLOCK_ROWS="
echo -n "$(cat .genesis-block-expanded.json)"
echo ""
echo -n "GENESIS_BLOCK_HISTORY="
echo -n "$(cat .genesis-block-history.json)"
echo ""
echo 'GENESIS_BLOCK={"rows": GENESIS_BLOCK_ROWS, "history": GENESIS_BLOCK_HISTORY}'
echo ""
popd >/dev/null
popd >/dev/null
exit 0