Skip to content

Commit

Permalink
add reorg tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedberg committed Dec 18, 2024
1 parent 4ac727f commit 10cf79f
Show file tree
Hide file tree
Showing 8 changed files with 931 additions and 161 deletions.
89 changes: 89 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rand = "0.8.5"
rstest = "0.19.0"
rstest_reuse = "0.6.0"
serde_yaml = "0.9"
serial_test = "3.2.0"
strum = { version = "0.26.2", features = ["derive"] }
strum_macros = "0.26.2"
time = "0.3.34"
Expand Down
64 changes: 57 additions & 7 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
version: '3.2'

services:
bitcoind:
bitcoind_1:
image: registry.gitlab.com/hashbeam/docker/bitcoind:25.0
profiles: [electrum]
command: "-fallbackfee=0.0002"
electrs:
bitcoind_2:
image: registry.gitlab.com/hashbeam/docker/bitcoind:25.0
profiles: [electrum]
command: "-fallbackfee=0.0002"
bitcoind_3:
image: registry.gitlab.com/hashbeam/docker/bitcoind:25.0
profiles: [electrum]
command: "-fallbackfee=0.0002"
electrs_1:
image: registry.gitlab.com/hashbeam/docker/electrs:0.9.14
profiles: [electrum]
environment:
BTCHOST: bitcoind_1
ports:
- 50001:50001
depends_on:
- bitcoind
esplora:
- bitcoind_1
electrs_2:
image: registry.gitlab.com/hashbeam/docker/electrs:0.9.14
profiles: [electrum]
environment:
BTCHOST: bitcoind_2
ports:
- 50002:50001
depends_on:
- bitcoind_2
electrs_3:
image: registry.gitlab.com/hashbeam/docker/electrs:0.9.14
profiles: [electrum]
environment:
BTCHOST: bitcoind_3
ports:
- 50003:50001
depends_on:
- bitcoind_3
esplora_1:
image: blockstream/esplora:956c74f42eb6ad803d8aedc272ba83d3aa6dcf5c
profiles: [esplora]
command: /srv/explorer/run.sh bitcoin-regtest explorer
Expand All @@ -22,5 +48,29 @@ services:
NO_ADDRESS_SEARCH: 1
NO_REGTEST_MINING: 1
ports:
- 50002:50001
- 50004:50001
- 8094:80
esplora_2:
image: blockstream/esplora:956c74f42eb6ad803d8aedc272ba83d3aa6dcf5c
profiles: [esplora]
command: /srv/explorer/run.sh bitcoin-regtest explorer
environment:
DEBUG: verbose
NO_PRECACHE: 1
NO_ADDRESS_SEARCH: 1
NO_REGTEST_MINING: 1
ports:
- 50005:50001
- 8095:80
esplora_3:
image: blockstream/esplora:956c74f42eb6ad803d8aedc272ba83d3aa6dcf5c
profiles: [esplora]
command: /srv/explorer/run.sh bitcoin-regtest explorer
environment:
DEBUG: verbose
NO_PRECACHE: 1
NO_ADDRESS_SEARCH: 1
NO_REGTEST_MINING: 1
ports:
- 50006:50001
- 8096:80
141 changes: 101 additions & 40 deletions tests/start_services.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,128 @@
set -eu

_die () {
echo "ERR: $*"
echo "ERR: $*" >&2
exit 1
}

COMPOSE_BASE="docker compose"
if ! $COMPOSE_BASE >/dev/null; then
echo "could not call docker compose (hint: install docker compose plugin)"
exit 1
_prepare_bitcoin_nodes() {
$BCLI_1 createwallet miner
$BCLI_2 createwallet miner
$BCLI_3 createwallet miner
$BCLI_1 -rpcwallet=miner -generate 103
$BCLI_2 -rpcwallet=miner -generate 103
# connect the 2 bitcoin services for the reorg
if [ "$PROFILE" == "esplora" ]; then
$BCLI_2 addnode "esplora_3:18444" "onetry"
$BCLI_3 addnode "esplora_2:18444" "onetry"
elif [ "$PROFILE" == "electrum" ]; then
$BCLI_2 addnode "bitcoind_3:18444" "onetry"
$BCLI_3 addnode "bitcoind_2:18444" "onetry"
fi
}

_wait_for_bitcoind() {
# wait for bitcoind to be up
bitcoind_service_name="$1"
until $COMPOSE logs $bitcoind_service_name |grep -q 'Bound to'; do
sleep 1
done
}

_wait_for_electrs() {
# wait for electrs to have completed startup
electrs_service_name="$1"
until $COMPOSE logs $electrs_service_name |grep -q 'finished full compaction'; do
sleep 1
done
}

_wait_for_esplora() {
# wait for esplora to have completed startup
esplora_service_name="$1"
until $COMPOSE logs $esplora_service_name |grep -q 'run: nginx:'; do
sleep 1
done
}

_stop_esplora() {
# stop an esplora sub service
esplora_service_name="$1"
esplora_sub_service_name="${2:-electrs}"
if $COMPOSE ps |grep -q $esplora_service_name; then
for SRV in socat $esplora_sub_service_name; do
$COMPOSE exec $esplora_service_name bash -c "sv -w 60 force-stop /etc/service/$SRV"
done
fi
}

_stop_services() {
if [ "$PROFILE" == "esplora" ]; then
_stop_esplora esplora_1
_stop_esplora esplora_2
_stop_esplora esplora_3
fi
# bring all services down
$COMPOSE --profile '*' down -v --remove-orphans
}

_start_services() {
_stop_services
mkdir -p $TEST_DATA_DIR
for port in "${EXPOSED_PORTS[@]}"; do
if [ -n "$(ss -HOlnt "sport = :$port")" ];then
_die "port $port is already bound, services can't be started"
fi
done
$COMPOSE up -d
}

COMPOSE="docker compose"
if ! $COMPOSE >/dev/null; then
_die "could not call docker compose (hint: install docker compose plugin)"
fi
COMPOSE_BASE="$COMPOSE_BASE -f tests/docker-compose.yml"
COMPOSE="$COMPOSE -f tests/docker-compose.yml"
PROFILE=${PROFILE:-"esplora"}
COMPOSE="$COMPOSE_BASE --profile $PROFILE"
COMPOSE="$COMPOSE --profile $PROFILE"
TEST_DATA_DIR="./test-data"

# see docker-compose.yml for the exposed ports
if [ "$PROFILE" == "esplora" ]; then
BCLI="$COMPOSE exec -T esplora cli"
EXPOSED_PORTS=(8094 50002)
BCLI_1="$COMPOSE exec -T esplora_1 cli"
BCLI_2="$COMPOSE exec -T esplora_2 cli"
BCLI_3="$COMPOSE exec -T esplora_3 cli"
EXPOSED_PORTS=(8094 8095 8096 50004 50005 50006)
elif [ "$PROFILE" == "electrum" ]; then
BCLI="$COMPOSE exec -T -u blits bitcoind bitcoin-cli -regtest"
EXPOSED_PORTS=(50001)
BCLI_1="$COMPOSE exec -T -u blits bitcoind_1 bitcoin-cli -regtest"
BCLI_2="$COMPOSE exec -T -u blits bitcoind_2 bitcoin-cli -regtest"
BCLI_3="$COMPOSE exec -T -u blits bitcoind_3 bitcoin-cli -regtest"
EXPOSED_PORTS=(50001 50002 50003)
else
_die "invalid profile"
fi

# restart services (down + up) checking for ports availability
$COMPOSE_BASE --profile '*' down -v --remove-orphans
mkdir -p $TEST_DATA_DIR
for port in "${EXPOSED_PORTS[@]}"; do
if [ -n "$(ss -HOlnt "sport = :$port")" ];then
_die "port $port is already bound, services can't be started"
fi
done
$COMPOSE up -d
_start_services

# wait for services (pre-mining)
if [ "$PROFILE" == "esplora" ]; then
# wait for esplora to have completed setup
until $COMPOSE logs esplora |grep -q 'Bootstrapped 100%'; do
sleep 1
done
_wait_for_esplora esplora_1
_wait_for_esplora esplora_2
_wait_for_esplora esplora_3
_stop_esplora esplora_1 tor
_stop_esplora esplora_2 tor
_stop_esplora esplora_3 tor
elif [ "$PROFILE" == "electrum" ]; then
# wait for bitcoind to be up
until $COMPOSE logs bitcoind |grep 'Bound to'; do
sleep 1
done
_wait_for_bitcoind bitcoind_1
_wait_for_bitcoind bitcoind_2
_wait_for_bitcoind bitcoind_3
fi

# prepare bitcoin funds
$BCLI createwallet miner
$BCLI -rpcwallet=miner -generate 103
_prepare_bitcoin_nodes

# wait for services (post-mining)
if [ "$PROFILE" == "esplora" ]; then
# wait for esplora to have completed setup
until $COMPOSE logs esplora |grep -q 'Electrum RPC server running'; do
sleep 1
done
elif [ "$PROFILE" == "electrum" ]; then
# wait for electrs to have completed startup
until $COMPOSE logs electrs |grep 'finished full compaction'; do
sleep 1
done
if [ "$PROFILE" == "electrum" ]; then
_wait_for_electrs electrs_1
_wait_for_electrs electrs_2
_wait_for_electrs electrs_3
fi
Loading

0 comments on commit 10cf79f

Please sign in to comment.