Skip to content

Instantly share code, notes, and snippets.

@jprupp
Last active June 17, 2020 23:39
Show Gist options
  • Save jprupp/007be6617bbf87b2b72c05f5c8eb592a to your computer and use it in GitHub Desktop.
Save jprupp/007be6617bbf87b2b72c05f5c8eb592a to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
SEED=$(( RANDOM + 30000 ))
BITCOIN_DIRS=(/tmp/regtest-{$(( SEED + 50 )),$(( SEED + 51 ))})
BITCOIN_PORTS=($(( SEED + 10 )) $(( SEED + 11 )))
BITCOIND_RPC=($(( SEED + 20 )) $(( SEED + 21)))
export DIR=/tmp/regtest-haskoin-$(( SEED + 40 ))
export NET=bchreg
export PORT=$(( SEED + 30 ))
HASKOIN="http://localhost:$PORT"
client()
{
local N=$1
shift
bitcoin-cli -datadir="${BITCOIN_DIRS[N]}" \
-rpcport=${BITCOIND_RPC[N]} \
-regtest \
"$@"
}
connect-wait()
{
local CONNECT_PORT=$1
echo "** Waiting for port $CONNECT_PORT to open..." >&2
while ! nc -z localhost $CONNECT_PORT
do
sleep 2
done
}
server()
{
local N=$1
echo "** Starting server $N..." >&2
rm -rf "${BITCOIN_DIRS[N]}"
mkdir -p "${BITCOIN_DIRS[N]}"
bitcoind -datadir="${BITCOIN_DIRS[N]}" \
-rpcport=${BITCOIND_RPC[N]} \
-port=${BITCOIN_PORTS[N]} \
-regtest &>/dev/null &
BITCOIND_PID[N]=$!
connect-wait ${BITCOIND_RPC[N]}
connect-wait ${BITCOIN_PORTS[N]}
sleep 2
}
get-peers()
{
for PORT in "${BITCOIN_PORTS[@]}"
do
echo -n "--peer localhost:$PORT "
done
}
haskoin()
{
echo "** Starting Haskoin..." >&2
haskoin-store $(get-peers) &>/dev/null &
HASKOIN_PID=$!
connect-wait $PORT
sleep 2
}
address-txs-length()
{
local ADDR="$1"
echo "** Getting number of txs for $ADDR..." >&2
curl -s "$HASKOIN/address/$ADDR/transactions?limit=0" | jq length
}
address-txs-count()
{
local ADDR="$1"
echo "** Getting balance tx count for $ADDR..." >&2
curl -s "$HASKOIN/address/$ADDR/balance" | jq .txs
}
address-balance-confirmed()
{
local ADDR="$1"
echo "** Getting confirmed balance for $ADDR..." >&2
curl -s "$HASKOIN/address/$ADDR/balance" | jq .confirmed
}
address-balance-unconfirmed()
{
local ADDR="$1"
echo "** Getting unconfirmed balance for $ADDR..." >&2
curl -s "$HASKOIN/address/$ADDR/balance" | jq .unconfirmed
}
block-height()
{
echo "** Getting best block..." >&2
curl -s "$HASKOIN/block/best" | jq .height
}
wait-for()
{
CALL="$1"
EXPECT="$2"
echo "** Waiting for '$CALL' to return '$EXPECT'..." >&2
for ((i = 0; i < 10; i++ ))
do
if [[ $($CALL) == $EXPECT ]]
then
break
else
sleep 6
fi
done
}
wait-block-height()
{
EXPECT=$1
echo "** Waiting for block height to be at least $EXPECT..."
for ((i = 0; i < 10; i++ ))
do
if [[ $(block-height) -ge $EXPECT ]]
then
break
else
sleep 6
fi
done
}
report()
{
local TEXT="$1"
local EXPECTED="$2"
local GOT="$3"
echo "** Testing: $TEXT..." >&2
if [[ $GOT = $EXPECTED ]]
then
echo "-- OK: $GOT = $EXPECTED"
else
echo "-- FAIL: $GOT ≠ $EXPECTED"
fi
}
stop-server()
{
N=$1
if [[ -n $BITCOIND_PID[N] ]]
then
echo "** Killing server $N..." >&2
kill ${BITCOIND_PID[N]}
wait ${BITCOIND_PID[N]} || true
sleep 2
else
echo "Cannot kill unstarted server $N" >&2
exit 1
fi
}
stop-haskoin()
{
if [[ -n $HASKOIN_PID ]]
then
echo "** Killing Haskoin..." >&2
kill ${HASKOIN_PID}
wait ${HASKOIN_PID} || true
sleep 2
else
echo "Cannot kill unstarted Haskoin" >&2
exit 1
fi
}
#
# Initial generation
#
server 0
ADDR1=$(client 0 getnewaddress)
report "Generate blocks for ADDR1" \
110 \
$(client 0 generatetoaddress 110 $ADDR1 | jq length)
sleep 5
haskoin
wait-block-height 110
report "Transaction count for ADDR1" \
110 \
$(address-txs-length $ADDR1)
report "Balance for ADDR1" \
550000000000 \
$(address-balance-confirmed $ADDR1)
ADDR2=$(client 0 getnewaddress)
for ((i = 0; i < 10; i++))
do
client 0 sendtoaddress $ADDR2 1 >&/dev/null
done
wait-for "address-txs-length $ADDR2" 10
report "Transaction count for ADDR2" \
10 \
$(address-txs-count $ADDR2)
report "Confirmed balance for ADDR2" \
0 \
$(address-balance-confirmed $ADDR2)
report "Unconfirmed balance for ADDR2" \
1000000000 \
$(address-balance-unconfirmed $ADDR2)
stop-server 0
stop-haskoin
#
# Reorg
#
rm -r ${BITCOIN_DIRS[0]}
server 0
ADDR3=$(client 0 getnewaddress)
report "Generate blocks for ADDR3" \
120 \
$(client 0 generatetoaddress 120 $ADDR3 | jq length)
sleep 5
haskoin
wait-block-height 120
report "Confirmed balance for ADDR3" \
600000000000 \
$(address-balance-confirmed $ADDR3)
report "Transaction count for ADDR1" \
0 \
$(address-txs-length $ADDR1)
report "Balance for ADDR1" \
0 \
$(address-balance-confirmed $ADDR1)
report "Transaction count for ADDR2" \
0 \
$(address-txs-count $ADDR2)
report "Confirmed balance for ADDR2" \
0 \
$(address-balance-confirmed $ADDR2)
report "Unconfirmed balance for ADDR2" \
0 \
$(address-balance-unconfirmed $ADDR2)
# report "Confirmed balance for ADDR3" \
# $(( 25 * 100 * 1000 * 1000 )) \
# $(address-balance-confirmed "$ADDR3")
# pkill -P $$
# server 1
# haskoin
# ADDR4="$(client 1 getnewaddress)"
# client 1 generatetoaddress 2 "$ADDR4" &>/dev/null
# sleep 20
# report "Confirmed balance for ADDR3" \
# 0 \
# $(address-balance-confirmed "$ADDR3")
# report "Confirmed balance for ADDR4" \
# $(( 2 * 25 * 100 * 1000 * 1000 )) \
# $(address-balance-confirmed "$ADDR4")
pkill -P $$
rm -rf "$DIR"
rm -rf "${BITCOIN_DIRS[@]}"
echo "** Done" >&2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment