From 9c46c20f2806829dcea5cb7a4060d08d63de364d Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Wed, 17 Aug 2022 00:37:30 +0100 Subject: [PATCH 01/11] Tried with a setup/run combo. --- contrib/images/Makefile | 11 ++- contrib/images/bitcoind/Dockerfile | 36 ++++++++ contrib/images/bitcoind/README.md | 12 +++ contrib/images/bitcoind/btcwallet_create.exp | 73 +++++++++++++++ contrib/images/bitcoind/wrapper.sh | 94 ++++++++++++++++++++ 5 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 contrib/images/bitcoind/Dockerfile create mode 100644 contrib/images/bitcoind/README.md create mode 100644 contrib/images/bitcoind/btcwallet_create.exp create mode 100755 contrib/images/bitcoind/wrapper.sh diff --git a/contrib/images/Makefile b/contrib/images/Makefile index 998778ef6..a44ad027d 100644 --- a/contrib/images/Makefile +++ b/contrib/images/Makefile @@ -1,4 +1,4 @@ -all: babylond-env +all: babylond-env bitcoind babylond-env: babylond-rmi docker build --tag babylonchain/babylond -f babylond-env/Dockerfile \ @@ -11,4 +11,11 @@ babylond-dlv: babylond-rmi babylond-rmi: docker rmi babylonchain/babylond 2>/dev/null; true -.PHONY: all babylond-env babylond-dlv babylond-rmi +bitcoind: + docker build --tag babylonchain/bintcoind -f bitcoind/Dockerfile \ + $(shell git rev-parse --show-toplevel) + +bitcoind-rmi: + docker rmi babylonchain/bitcoind 2>/dev/null; true + +.PHONY: all babylond-env babylond-dlv babylond-rmi bitcoind bitcoind-rmi diff --git a/contrib/images/bitcoind/Dockerfile b/contrib/images/bitcoind/Dockerfile new file mode 100644 index 000000000..6ab0fbc48 --- /dev/null +++ b/contrib/images/bitcoind/Dockerfile @@ -0,0 +1,36 @@ +FROM golang:1.18-alpine AS build + +RUN apk add --no-cache openssh-client git make build-base linux-headers libc-dev + +WORKDIR /work + +ENV GO111MODULE=on + +RUN git clone https://github.com/btcsuite/btcd.git +RUN cd btcd && go install -v . ./cmd/... && cd - + +RUN git clone https://github.com/btcsuite/btcwallet.git +RUN cd btcwallet && go install -v . ./cmd/... && cd - + + +FROM alpine:3.14 AS run +RUN apk add bash curl jq expect expect-dev +WORKDIR /bitcoind + +COPY --from=build /go/bin/* /usr/bin/ +COPY contrib/images/bitcoind/wrapper.sh /bitcoind/wrapper.sh +COPY contrib/images/bitcoind/btcwallet_create.exp /bitcoind/btcwallet_create.exp + +ENV RPCUSER=rpcuser +ENV RPCPASS=rpcpass +ENV PASSPHRASE=pass +ENV GENERATE_INTERVAL_SECS=30 + +# Do the one time setup of the miner and the wallet. +RUN /bitcoind/wrapper.sh setup + +EXPOSE 18554 18555 18556 + +ENTRYPOINT ["/bitcoind/wrapper.sh"] +CMD ["run"] +STOPSIGNAL SIGTERM diff --git a/contrib/images/bitcoind/README.md b/contrib/images/bitcoind/README.md new file mode 100644 index 000000000..765ee8ad0 --- /dev/null +++ b/contrib/images/bitcoind/README.md @@ -0,0 +1,12 @@ +# bitcoind + +`bitcoind` is a Docker image we build to be able to run a single Bitcoin node in the local devnet that produces blocks at regular intervals without consuming CPU. + +To achieve this we use [btcd](https://github.com/btcsuite/btcd) in `--simnet` mode and make calls to the [generate](https://github.com/btcsuite/btcd/blob/v0.23.1/rpcserver.go#L886) API endpoint. + +The image also runs a [btcwallet](https://github.com/btcsuite/btcwallet) process which can be used to make transfers. To talk to `btcd` programmatically we can use the [rpcclient](https://github.com/btcsuite/btcd/blob/v0.23.1/rpcclient/mining.go#L54-L62), and for the wallet the [chain](https://github.com/btcsuite/btcwallet/tree/master/chain) client. + +See more at: +* https://gist.github.com/davecgh/2992ed85d41307e794f6 +* http://piotrpasich.com/how-to-setup-own-bitcoin-simulation-network/ +* https://blog.krybot.com/a?ID=00950-ef39d506-48ea-45df-81c5-2115e2f4a0f6 diff --git a/contrib/images/bitcoind/btcwallet_create.exp b/contrib/images/bitcoind/btcwallet_create.exp new file mode 100644 index 000000000..6fe0bd0ee --- /dev/null +++ b/contrib/images/bitcoind/btcwallet_create.exp @@ -0,0 +1,73 @@ +#!/usr/bin/expect -f +# +# This Expect script was generated by autoexpect on Tue Aug 16 22:44:36 2022 +# Expect and autoexpect were both written by Don Libes, NIST. +# +# Note that autoexpect does not guarantee a working script. It +# necessarily has to guess about certain things. Two reasons a script +# might fail are: +# +# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet, +# etc.) and devices discard or ignore keystrokes that arrive "too +# quickly" after prompts. If you find your new script hanging up at +# one spot, try adding a short sleep just before the previous send. +# Setting "force_conservative" to 1 (see below) makes Expect do this +# automatically - pausing briefly before sending each character. This +# pacifies every program I know of. The -c flag makes the script do +# this in the first place. The -C flag allows you to define a +# character to toggle this mode off and on. + +set force_conservative 0 ;# set to 1 to force conservative mode even if + ;# script wasn't run conservatively originally +if {$force_conservative} { + set send_slow {1 .1} + proc send {ignore arg} { + sleep .1 + exp_send -s -- $arg + } +} + +# +# 2) differing output - Some programs produce different output each time +# they run. The "date" command is an obvious example. Another is +# ftp, if it produces throughput statistics at the end of a file +# transfer. If this causes a problem, delete these patterns or replace +# them with wildcards. An alternative is to use the -p flag (for +# "prompt") which makes Expect only look for the last line of output +# (i.e., the prompt). The -P flag allows you to define a character to +# toggle this mode off and on. +# +# Read the man page for more info. +# +# -Don + +set RPCUSER [lindex $argv 0] +set RPCPASS [lindex $argv 1] +set PASSPHRASE [lindex $argv 2] + +set timeout -1 +spawn btcwallet --simnet -u $RPCUSER -P $RPCPASS --create +match_max 100000 +expect -exact "Enter the private passphrase for your new wallet: " +send -- "$PASSPHRASE\r" +expect -exact "\r +Confirm passphrase: " +send -- "$PASSPHRASE\r" +expect -exact "\r +Do you want to add an additional layer of encryption for public data? (n/no/y/yes) \[no\]: " +send -- "n\r" +expect -exact "n\r +Do you have an existing wallet seed you want to use? (n/no/y/yes) \[no\]: " +send -- "n\r" +expect "n\r +Your wallet generation seed is:\r +*\r +IMPORTANT: Keep the seed in a safe place as you\r +will NOT be able to restore your wallet without it.\r +Please keep in mind that anyone who has access\r +to the seed can also restore your wallet thereby\r +giving them access to all your funds, so it is\r +imperative that you keep it in a secure location.\r +Once you have stored the seed in a safe and secure location, enter \"OK\" to continue: " +send -- "OK\r" +expect eof diff --git a/contrib/images/bitcoind/wrapper.sh b/contrib/images/bitcoind/wrapper.sh new file mode 100755 index 000000000..fad895956 --- /dev/null +++ b/contrib/images/bitcoind/wrapper.sh @@ -0,0 +1,94 @@ +#!/usr/bin/env sh +set -euo pipefail +#set -x + +# btcctl will be looking for this file, but the wallet doesn't create it. +mkdir -p /root/.btcwallet && touch /root/.btcwallet/btcwallet.conf +mkdir -p /root/.btcd && touch /root/.btcd/btcd.conf + +# Create a wallet with and a miner address, then mine enough blocks for the miner to have some initial balance. +function setup { + + echo "Starting btcd..." + btcd --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18556 --listen=0.0.0.0:18555 2>&1 & + BTCD_PID=$! + + echo "Creating a wallet..." + # Used autoexpect to create the wallet in the first instance. + # https://stackoverflow.com/questions/4857702/how-to-provide-password-to-a-command-that-prompts-for-one-in-bash + expect btcwallet_create.exp $RPCUSER $RPCPASS $PASSPHRASE + + echo "Starting btcwallet..." + btcwallet --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18554 2>&1 & + BTCWALLET_PID=$! + + # Allow some time for the wallet to start + sleep 5 + + echo "Creating miner address..." + MINING_ADDR=$(btcctl --simnet --wallet -u $RPCUSER -P $RPCPASS getnewaddress) + echo $MINING_ADDR > mining.addr + + echo "Restarting btcd with mining address $MINING_ADDR..." + kill -9 $BTCD_PID + btcd --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18556 --listen=0.0.0.0:18555 --miningaddr=$MINING_ADDR 2>&1 & + BTCD_PID=$! + + # Allow btcd to start + sleep 5 + + echo "Generating enought blocks for the first coinbase to mature..." + btcctl --simnet -u $RPCUSER -P $RPCPASS generate 100 + + # Allow some time for the wallet to catch up. + sleep 5 + + echo "Checking balance..." + btcctl --simnet --wallet -u $RPCUSER -P $RPCPASS getbalance + + echo "Exiting..." + kill $BTCWALLET_PID + kill $BTCD_PID +} + +# Start the BTC node and the wallet in the background, then generate blocks at regular intervals. +function run { + if [ ! -f "mining.addr" ]; then + echo "Mining address not found. Please run setup first." + exit 1 + fi + MINING_ADDR=$(cat mining.addr) + + echo "Mining address: $MINING_ADDR" + + echo "Starting btcd..." + btcd --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18556 --listen=0.0.0.0:18555 --miningaddr=$MINING_ADDR 2>&1 & + sleep 5 + + echo "Starting btcwallet..." + btcwallet --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18554 2>&1 & + sleep 5 + + echo "Generating a block every ${GENERATE_INTERVAL_SECS} seconds." + echo "Press [CTRL+C] to stop..." + while true + do + btcctl --simnet -u $RPCUSER -P $RPCPASS generate 1 + + sleep ${GENERATE_INTERVAL_SECS} + done +} + +case "$1" in + setup) + setup + ;; + + run) + run + ;; + + *) + echo $"Usage: $0 {setup|run}" + exit 1 +esac From 5918e6983ec4d15ce204040ccc4e471f985c96a0 Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Wed, 17 Aug 2022 00:47:55 +0100 Subject: [PATCH 02/11] Perform the setup at each startup. --- contrib/images/bitcoind/Dockerfile | 5 +- contrib/images/bitcoind/README.md | 135 +++++++++++++++++++++++++++++ contrib/images/bitcoind/wrapper.sh | 107 ++++++++--------------- 3 files changed, 170 insertions(+), 77 deletions(-) diff --git a/contrib/images/bitcoind/Dockerfile b/contrib/images/bitcoind/Dockerfile index 6ab0fbc48..d5a7963fc 100644 --- a/contrib/images/bitcoind/Dockerfile +++ b/contrib/images/bitcoind/Dockerfile @@ -26,11 +26,8 @@ ENV RPCPASS=rpcpass ENV PASSPHRASE=pass ENV GENERATE_INTERVAL_SECS=30 -# Do the one time setup of the miner and the wallet. -RUN /bitcoind/wrapper.sh setup - EXPOSE 18554 18555 18556 ENTRYPOINT ["/bitcoind/wrapper.sh"] -CMD ["run"] +CMD [] STOPSIGNAL SIGTERM diff --git a/contrib/images/bitcoind/README.md b/contrib/images/bitcoind/README.md index 765ee8ad0..e70b61318 100644 --- a/contrib/images/bitcoind/README.md +++ b/contrib/images/bitcoind/README.md @@ -10,3 +10,138 @@ See more at: * https://gist.github.com/davecgh/2992ed85d41307e794f6 * http://piotrpasich.com/how-to-setup-own-bitcoin-simulation-network/ * https://blog.krybot.com/a?ID=00950-ef39d506-48ea-45df-81c5-2115e2f4a0f6 + + +## Build + +You can build the image with the following command: + +```bash +make bitcoind +``` + +## Test + +One way to see if it works is to run the container interactively: + +```bash +docker run -it --rm --name bitcoind babylonchain/bintcoind +``` + +The logs should show that a wallet is created with and mining is started: + +```console +$ docker run -it --rm babylonchain/bintcoind +Starting btcd... +Creating a wallet... +spawn btcwallet --simnet -u rpcuser -P rpcpass --create +2022-08-16 23:39:33.091 [INF] BTCD: Version 0.23.1-beta +2022-08-16 23:39:33.091 [INF] BTCD: Loading block database from '/root/.btcd/data/simnet/blocks_ffldb' +Enter the private passphrase for your new wallet: +Confirm passphrase: +Do you want to add an additional layer of encryption for public data? (n/no/y/yes) [no]: n +Do you have an existing wallet seed you want to use? (n/no/y/yes) [no]: n +Your wallet generation seed is: +b46944f9dd96792481ec71c4d945a3e79959e429c5b77fcda14e587cabebc672 +IMPORTANT: Keep the seed in a safe place as you +will NOT be able to restore your wallet without it. +Please keep in mind that anyone who has access +to the seed can also restore your wallet thereby +giving them access to all your funds, so it is +imperative that you keep it in a secure location. +Once you have stored the seed in a safe and secure location, enter "OK" to continue: OK +Creating the wallet... +2022-08-16 23:39:33.114 [INF] BTCD: Block database loaded +2022-08-16 23:39:33.116 [INF] INDX: Committed filter index is enabled +2022-08-16 23:39:33.116 [INF] INDX: Catching up indexes from height -1 to 0 +2022-08-16 23:39:33.116 [INF] INDX: Indexes caught up to height 0 +2022-08-16 23:39:33.116 [INF] CHAN: Chain state (height 0, hash 683e86bd5c6d110d91b94b97137ba6bfe02dbbdb8e3dff722a669b5d69d77af6, totaltx 1, work 2) +2022-08-16 23:39:33.117 [INF] RPCS: Generating TLS certificates... +2022-08-16 23:39:33.136 [INF] RPCS: Done generating TLS certificates +2022-08-16 23:39:33.141 [INF] AMGR: Loaded 0 addresses from file '/root/.btcd/data/simnet/peers.json' +2022-08-16 23:39:33.141 [INF] RPCS: RPC server listening on 0.0.0.0:18556 +2022-08-16 23:39:33.141 [INF] CMGR: Server listening on 0.0.0.0:18555 +2022-08-16 23:39:35.814 [INF] WLLT: Opened wallet +The wallet has been created successfully. +Starting btcwallet... +2022-08-16 23:39:35.822 [INF] BTCW: Version 0.15.1-alpha +2022-08-16 23:39:35.822 [INF] BTCW: Generating TLS certificates... +2022-08-16 23:39:35.846 [INF] BTCW: Done generating TLS certificates +2022-08-16 23:39:35.846 [INF] RPCS: Listening on 0.0.0.0:18554 +2022-08-16 23:39:35.846 [INF] BTCW: Attempting RPC client connection to localhost:18556 +2022-08-16 23:39:35.872 [INF] RPCS: New websocket client 127.0.0.1:49538 +2022-08-16 23:39:35.872 [INF] CHNS: Established connection to RPC server localhost:18556 +2022-08-16 23:39:36.768 [INF] WLLT: Opened wallet +2022-08-16 23:39:36.771 [INF] WLLT: RECOVERY MODE ENABLED -- rescanning for used addresses with recovery_window=250 +2022-08-16 23:39:36.790 [INF] WLLT: Started rescan from block 683e86bd5c6d110d91b94b97137ba6bfe02dbbdb8e3dff722a669b5d69d77af6 (height 0) for 0 addresses +2022-08-16 23:39:36.791 [INF] RPCS: Beginning rescan for 0 addresses +2022-08-16 23:39:36.791 [INF] RPCS: Skipping rescan as client has no addrs/utxos +2022-08-16 23:39:36.791 [INF] RPCS: Finished rescan +2022-08-16 23:39:36.791 [INF] WLLT: Catching up block hashes to height 0, this might take a while +2022-08-16 23:39:36.792 [INF] WLLT: Done catching up block hashes +2022-08-16 23:39:36.792 [INF] WLLT: Finished rescan for 0 addresses (synced to block 683e86bd5c6d110d91b94b97137ba6bfe02dbbdb8e3dff722a669b5d69d77af6, height 0) +Creating miner address... +Restarting btcd with mining address Sjfg9uCaTqSR7UvGRuuMRJtUtWRhoBA7YX... +2022-08-16 23:39:40.861 [ERR] CHNS: Websocket receive error from localhost:18556: websocket: close 1006 unexpected EOF +2022-08-16 23:39:40.862 [INF] CHNS: Failed to connect to localhost:18556: dial tcp 127.0.0.1:18556: connect: connection refused +2022-08-16 23:39:40.862 [INF] CHNS: Retrying connection to localhost:18556 in 5s +2022-08-16 23:39:40.873 [INF] BTCD: Version 0.23.1-beta +2022-08-16 23:39:40.873 [INF] BTCD: Loading block database from '/root/.btcd/data/simnet/blocks_ffldb' +2022-08-16 23:39:40.897 [INF] BCDB: Detected unclean shutdown - Repairing... +2022-08-16 23:39:40.900 [INF] BCDB: Database sync complete +2022-08-16 23:39:40.901 [INF] BTCD: Block database loaded +2022-08-16 23:39:40.910 [INF] INDX: Committed filter index is enabled +2022-08-16 23:39:40.913 [INF] INDX: Catching up indexes from height -1 to 0 +2022-08-16 23:39:40.913 [INF] INDX: Indexes caught up to height 0 +2022-08-16 23:39:40.913 [INF] CHAN: Chain state (height 0, hash 683e86bd5c6d110d91b94b97137ba6bfe02dbbdb8e3dff722a669b5d69d77af6, totaltx 1, work 2) +2022-08-16 23:39:40.927 [INF] AMGR: Loaded 0 addresses from file '/root/.btcd/data/simnet/peers.json' +2022-08-16 23:39:40.927 [INF] RPCS: RPC server listening on 0.0.0.0:18556 +2022-08-16 23:39:40.927 [INF] CMGR: Server listening on 0.0.0.0:18555 +Generating enought blocks for the first coinbase to mature... +2022-08-16 23:39:45.890 [INF] RPCS: New websocket client 127.0.0.1:49552 +2022-08-16 23:39:45.890 [INF] CHNS: Reestablished connection to RPC server localhost:18556 +2022-08-16 23:39:45.890 [INF] WLLT: RECOVERY MODE ENABLED -- rescanning for used addresses with recovery_window=250 +2022-08-16 23:39:45.891 [WRN] CHNS: Received unexpected reply: {"hash":"683e86bd5c6d110d91b94b97137ba6bfe02dbbdb8e3dff722a669b5d69d77af6","height":0} (id 16) +2022-08-16 23:39:45.896 [INF] MINR: Block submitted via CPU miner accepted (hash 3650f1363cc7fc4c3f01d1d904caa9f9687ef08927498658b6c50b585edb7872, amount 50 BTC) +... +2022-08-16 23:39:45.920 [INF] MINR: Block submitted via CPU miner accepted (hash 7a6dc189c7ffc38db73304e2993dacb241a932fc3980ca840da466b58e2d26d8, amount 50 BTC) +[ + "3650f1363cc7fc4c3f01d1d904caa9f9687ef08927498658b6c50b585edb7872", + "243d889cc77cbabcb4d28a3c5224dc3c0ef6970d0ea9f7e143e1216abb87c0d4", + "1577ee934812dace99bbab757ce6ccbed443da0863baab3b8ba7efe1b4453917", + ... + "63cb51a672732ec1175b1ed288d20cd8f1cb8653fdd7dc31f90fbdb9844c7998", + "7a6dc189c7ffc38db73304e2993dacb241a932fc3980ca840da466b58e2d26d8" +] +2022-08-16 23:39:46.148 [INF] WLLT: Catching up block hashes to height 32, this might take a while +2022-08-16 23:39:46.151 [INF] WLLT: Done catching up block hashes +2022-08-16 23:39:46.151 [INF] WLLT: Finished rescan for 1 address (synced to block 42ad6a44786a2c6edcfcca7098b3d18cc13c668a004c494ca87566c404f9ae34, height 32) +Checking balance... +50 +Generating a block every 30 seconds. +Press [CTRL+C] to stop... +2022-08-16 23:39:50.975 [INF] MINR: Block submitted via CPU miner accepted (hash 1e4931b5b590e297f736da282ab1367f1704ff5a21943c3883161d4f0a60dda2, amount 50 BTC) +[ + "1e4931b5b590e297f736da282ab1367f1704ff5a21943c3883161d4f0a60dda2" +] + +``` + +Then we can connect to this from another container to query the balance: + +```console +$ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +93e58681637d babylonchain/bintcoind "/bitcoind/wrapper.sh" 5 minutes ago Up 5 minutes 18554-18556/tcp bitcoind +$ docker exec -it bitcoind sh +/bitcoind # btcctl --simnet --wallet -u $RPCUSER -P $RPCPASS getbalance +600 +/bitcoind # + +``` + +The balance will go up as more blocks mature. The default coinbase maturity is 100 blocks. + +## Use in docker-compose + +The image has been added to the main `docker-compose.yml` file. It will build it if it's not built already and start running it as part of the local testnet. The other containers can use the default ports to connect to it, which are also exposed on the host. diff --git a/contrib/images/bitcoind/wrapper.sh b/contrib/images/bitcoind/wrapper.sh index fad895956..861a129bb 100755 --- a/contrib/images/bitcoind/wrapper.sh +++ b/contrib/images/bitcoind/wrapper.sh @@ -7,88 +7,49 @@ mkdir -p /root/.btcwallet && touch /root/.btcwallet/btcwallet.conf mkdir -p /root/.btcd && touch /root/.btcd/btcd.conf # Create a wallet with and a miner address, then mine enough blocks for the miner to have some initial balance. -function setup { - echo "Starting btcd..." - btcd --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18556 --listen=0.0.0.0:18555 2>&1 & - BTCD_PID=$! +echo "Starting btcd..." +btcd --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18556 --listen=0.0.0.0:18555 2>&1 & +BTCD_PID=$! - echo "Creating a wallet..." - # Used autoexpect to create the wallet in the first instance. - # https://stackoverflow.com/questions/4857702/how-to-provide-password-to-a-command-that-prompts-for-one-in-bash - expect btcwallet_create.exp $RPCUSER $RPCPASS $PASSPHRASE +echo "Creating a wallet..." +# Used autoexpect to create the wallet in the first instance. +# https://stackoverflow.com/questions/4857702/how-to-provide-password-to-a-command-that-prompts-for-one-in-bash +expect btcwallet_create.exp $RPCUSER $RPCPASS $PASSPHRASE - echo "Starting btcwallet..." - btcwallet --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18554 2>&1 & - BTCWALLET_PID=$! +echo "Starting btcwallet..." +btcwallet --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18554 2>&1 & +BTCWALLET_PID=$! - # Allow some time for the wallet to start - sleep 5 +# Allow some time for the wallet to start +sleep 5 - echo "Creating miner address..." - MINING_ADDR=$(btcctl --simnet --wallet -u $RPCUSER -P $RPCPASS getnewaddress) - echo $MINING_ADDR > mining.addr +echo "Creating miner address..." +MINING_ADDR=$(btcctl --simnet --wallet -u $RPCUSER -P $RPCPASS getnewaddress) +echo $MINING_ADDR > mining.addr - echo "Restarting btcd with mining address $MINING_ADDR..." - kill -9 $BTCD_PID - btcd --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18556 --listen=0.0.0.0:18555 --miningaddr=$MINING_ADDR 2>&1 & - BTCD_PID=$! +echo "Restarting btcd with mining address $MINING_ADDR..." +kill -9 $BTCD_PID +btcd --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18556 --listen=0.0.0.0:18555 --miningaddr=$MINING_ADDR 2>&1 & +BTCD_PID=$! - # Allow btcd to start - sleep 5 +# Allow btcd to start +sleep 5 - echo "Generating enought blocks for the first coinbase to mature..." - btcctl --simnet -u $RPCUSER -P $RPCPASS generate 100 +echo "Generating enought blocks for the first coinbase to mature..." +btcctl --simnet -u $RPCUSER -P $RPCPASS generate 100 - # Allow some time for the wallet to catch up. - sleep 5 +# Allow some time for the wallet to catch up. +sleep 5 - echo "Checking balance..." - btcctl --simnet --wallet -u $RPCUSER -P $RPCPASS getbalance +echo "Checking balance..." +btcctl --simnet --wallet -u $RPCUSER -P $RPCPASS getbalance - echo "Exiting..." - kill $BTCWALLET_PID - kill $BTCD_PID -} +echo "Generating a block every ${GENERATE_INTERVAL_SECS} seconds." +echo "Press [CTRL+C] to stop..." +while true +do + btcctl --simnet -u $RPCUSER -P $RPCPASS generate 1 -# Start the BTC node and the wallet in the background, then generate blocks at regular intervals. -function run { - if [ ! -f "mining.addr" ]; then - echo "Mining address not found. Please run setup first." - exit 1 - fi - MINING_ADDR=$(cat mining.addr) - - echo "Mining address: $MINING_ADDR" - - echo "Starting btcd..." - btcd --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18556 --listen=0.0.0.0:18555 --miningaddr=$MINING_ADDR 2>&1 & - sleep 5 - - echo "Starting btcwallet..." - btcwallet --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18554 2>&1 & - sleep 5 - - echo "Generating a block every ${GENERATE_INTERVAL_SECS} seconds." - echo "Press [CTRL+C] to stop..." - while true - do - btcctl --simnet -u $RPCUSER -P $RPCPASS generate 1 - - sleep ${GENERATE_INTERVAL_SECS} - done -} - -case "$1" in - setup) - setup - ;; - - run) - run - ;; - - *) - echo $"Usage: $0 {setup|run}" - exit 1 -esac + sleep ${GENERATE_INTERVAL_SECS} +done From 0c2d8899d1cc3a19b0d24d8d3b68845179dab5eb Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Wed, 17 Aug 2022 01:01:11 +0100 Subject: [PATCH 03/11] Added to docker-compose.yml --- contrib/images/bitcoind/README.md | 10 ++++++++++ docker-compose.yml | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/contrib/images/bitcoind/README.md b/contrib/images/bitcoind/README.md index e70b61318..2c1069c16 100644 --- a/contrib/images/bitcoind/README.md +++ b/contrib/images/bitcoind/README.md @@ -145,3 +145,13 @@ The balance will go up as more blocks mature. The default coinbase maturity is 1 ## Use in docker-compose The image has been added to the main `docker-compose.yml` file. It will build it if it's not built already and start running it as part of the local testnet. The other containers can use the default ports to connect to it, which are also exposed on the host. + +It can be started on its own like so: + +```bash +docker-compose up -d bitcoind +``` + +The ports are mapped to the default ports that `btcd` and `btcwallet` would use, so if those are already running on the host they will clash. + +Currently the image doesn't support restarting, the container has to be completely removed and recreated if it's stopped. diff --git a/docker-compose.yml b/docker-compose.yml index 866895e38..33a33fc74 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -89,6 +89,19 @@ services: localnet: ipv4_address: 192.168.10.5 + bitcoind: + build: + context: ./ + dockerfile: ./contrib/images/bitcoind/Dockerfile + image: babylonchain/bitcoind:latest + container_name: bitcoind + ports: + - 18554:18554 + - 18555:18555 + - 18556:18556 + networks: + - localnet + networks: localnet: driver: bridge From 0b0e61be154e9edad5bd3aa912aec3899f4ed1a7 Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Wed, 17 Aug 2022 01:12:32 +0100 Subject: [PATCH 04/11] Try different ports. --- contrib/images/bitcoind/README.md | 2 +- docker-compose.yml | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/contrib/images/bitcoind/README.md b/contrib/images/bitcoind/README.md index 2c1069c16..7e7cf4278 100644 --- a/contrib/images/bitcoind/README.md +++ b/contrib/images/bitcoind/README.md @@ -152,6 +152,6 @@ It can be started on its own like so: docker-compose up -d bitcoind ``` -The ports are mapped to the default ports that `btcd` and `btcwallet` would use, so if those are already running on the host they will clash. +The ports are mapped to the 10000 higher than the default ports that `btcd` and `btcwallet` would use, to avoid clashes if they are already running on the host. Currently the image doesn't support restarting, the container has to be completely removed and recreated if it's stopped. diff --git a/docker-compose.yml b/docker-compose.yml index 33a33fc74..92c1fa599 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -96,9 +96,9 @@ services: image: babylonchain/bitcoind:latest container_name: bitcoind ports: - - 18554:18554 - - 18555:18555 - - 18556:18556 + - 28554:18554 + - 28555:18555 + - 28556:18556 networks: - localnet From 723154e84a8e524c625a11f5307e413a30ba48d9 Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Wed, 17 Aug 2022 10:09:32 +0100 Subject: [PATCH 05/11] Fix typo. --- contrib/images/bitcoind/wrapper.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/images/bitcoind/wrapper.sh b/contrib/images/bitcoind/wrapper.sh index 861a129bb..b0761dcce 100755 --- a/contrib/images/bitcoind/wrapper.sh +++ b/contrib/images/bitcoind/wrapper.sh @@ -36,7 +36,7 @@ BTCD_PID=$! # Allow btcd to start sleep 5 -echo "Generating enought blocks for the first coinbase to mature..." +echo "Generating enough blocks for the first coinbase to mature..." btcctl --simnet -u $RPCUSER -P $RPCPASS generate 100 # Allow some time for the wallet to catch up. From cf422b8b86dbe37625250b014fccade931549bfa Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Wed, 17 Aug 2022 10:09:46 +0100 Subject: [PATCH 06/11] What if no ports are mapped? --- docker-compose.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 92c1fa599..6c368aa80 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -95,10 +95,10 @@ services: dockerfile: ./contrib/images/bitcoind/Dockerfile image: babylonchain/bitcoind:latest container_name: bitcoind - ports: - - 28554:18554 - - 28555:18555 - - 28556:18556 + # ports: + # - 28554:18554 + # - 28555:18555 + # - 28556:18556 networks: - localnet From f480a4ac2efac2fa0b946468cf67aaee7e34f20d Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Wed, 17 Aug 2022 10:13:16 +0100 Subject: [PATCH 07/11] Example of command to remove with docker-compose --- contrib/images/bitcoind/README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/contrib/images/bitcoind/README.md b/contrib/images/bitcoind/README.md index 7e7cf4278..a2d718bc2 100644 --- a/contrib/images/bitcoind/README.md +++ b/contrib/images/bitcoind/README.md @@ -152,6 +152,11 @@ It can be started on its own like so: docker-compose up -d bitcoind ``` -The ports are mapped to the 10000 higher than the default ports that `btcd` and `btcwallet` would use, to avoid clashes if they are already running on the host. +Currently the image doesn't support restarting, the container has to be completely removed and recreated if it's stopped. It can be removed with the following command: -Currently the image doesn't support restarting, the container has to be completely removed and recreated if it's stopped. +```bash +docker-compose stop bitcoind +docker-compose rm bitcoind +``` + +The ports are mapped to the 10000 higher than the default ports that `btcd` and `btcwallet` would use, to avoid clashes if they are already running on the host. From f62269e434c810e8a42587a4a56bf7ef047f2fd7 Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Wed, 17 Aug 2022 10:24:39 +0100 Subject: [PATCH 08/11] Use default ports again. --- contrib/images/bitcoind/README.md | 2 +- contrib/images/bitcoind/wrapper.sh | 1 + docker-compose.yml | 6 ++---- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/contrib/images/bitcoind/README.md b/contrib/images/bitcoind/README.md index a2d718bc2..aab974306 100644 --- a/contrib/images/bitcoind/README.md +++ b/contrib/images/bitcoind/README.md @@ -159,4 +159,4 @@ docker-compose stop bitcoind docker-compose rm bitcoind ``` -The ports are mapped to the 10000 higher than the default ports that `btcd` and `btcwallet` would use, to avoid clashes if they are already running on the host. +The ports are mapped to the same default ports that `btcd` and `btcwallet` would use, so if the host already runs these services they might clash. diff --git a/contrib/images/bitcoind/wrapper.sh b/contrib/images/bitcoind/wrapper.sh index b0761dcce..629dc65d6 100755 --- a/contrib/images/bitcoind/wrapper.sh +++ b/contrib/images/bitcoind/wrapper.sh @@ -30,6 +30,7 @@ echo $MINING_ADDR > mining.addr echo "Restarting btcd with mining address $MINING_ADDR..." kill -9 $BTCD_PID +sleep 1 btcd --simnet -u $RPCUSER -P $RPCPASS --rpclisten=0.0.0.0:18556 --listen=0.0.0.0:18555 --miningaddr=$MINING_ADDR 2>&1 & BTCD_PID=$! diff --git a/docker-compose.yml b/docker-compose.yml index 6c368aa80..dae4a8b7f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -95,10 +95,8 @@ services: dockerfile: ./contrib/images/bitcoind/Dockerfile image: babylonchain/bitcoind:latest container_name: bitcoind - # ports: - # - 28554:18554 - # - 28555:18555 - # - 28556:18556 + ports: + - 18554-18556:18554-18556 networks: - localnet From 0e7fd69e59abab00808d3485ee94d52df2e110a3 Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Wed, 17 Aug 2022 10:27:08 +0100 Subject: [PATCH 09/11] Fix typo in image name. --- contrib/images/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/images/Makefile b/contrib/images/Makefile index a44ad027d..3afb2e868 100644 --- a/contrib/images/Makefile +++ b/contrib/images/Makefile @@ -12,7 +12,7 @@ babylond-rmi: docker rmi babylonchain/babylond 2>/dev/null; true bitcoind: - docker build --tag babylonchain/bintcoind -f bitcoind/Dockerfile \ + docker build --tag babylonchain/bitcoind -f bitcoind/Dockerfile \ $(shell git rev-parse --show-toplevel) bitcoind-rmi: From b1cc24b26cc2b1fb52254a019d98d2a33d7ff9cc Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Wed, 17 Aug 2022 10:54:38 +0100 Subject: [PATCH 10/11] Set an IP address. --- docker-compose.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index dae4a8b7f..bc5c1ccf9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -98,7 +98,8 @@ services: ports: - 18554-18556:18554-18556 networks: - - localnet + localnet: + ipv4_address: 192.168.10.6 networks: localnet: From 49627b892e551dd2224ea60a86f5926729b039ac Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Wed, 17 Aug 2022 12:56:48 +0100 Subject: [PATCH 11/11] Rename to bitcoinsim. Remove curl and jq --- contrib/images/Makefile | 12 +++++----- .../{bitcoind => bitcoinsim}/Dockerfile | 10 ++++---- .../images/{bitcoind => bitcoinsim}/README.md | 24 +++++++++---------- .../btcwallet_create.exp | 0 .../{bitcoind => bitcoinsim}/wrapper.sh | 0 docker-compose.yml | 8 +++---- 6 files changed, 27 insertions(+), 27 deletions(-) rename contrib/images/{bitcoind => bitcoinsim}/Dockerfile (70%) rename contrib/images/{bitcoind => bitcoinsim}/README.md (92%) rename contrib/images/{bitcoind => bitcoinsim}/btcwallet_create.exp (100%) rename contrib/images/{bitcoind => bitcoinsim}/wrapper.sh (100%) diff --git a/contrib/images/Makefile b/contrib/images/Makefile index 3afb2e868..8da208d36 100644 --- a/contrib/images/Makefile +++ b/contrib/images/Makefile @@ -1,4 +1,4 @@ -all: babylond-env bitcoind +all: babylond-env bitcoinsim babylond-env: babylond-rmi docker build --tag babylonchain/babylond -f babylond-env/Dockerfile \ @@ -11,11 +11,11 @@ babylond-dlv: babylond-rmi babylond-rmi: docker rmi babylonchain/babylond 2>/dev/null; true -bitcoind: - docker build --tag babylonchain/bitcoind -f bitcoind/Dockerfile \ +bitcoinsim: + docker build --tag babylonchain/bitcoinsim -f bitcoinsim/Dockerfile \ $(shell git rev-parse --show-toplevel) -bitcoind-rmi: - docker rmi babylonchain/bitcoind 2>/dev/null; true +bitcoinsim-rmi: + docker rmi babylonchain/bitcoinsim 2>/dev/null; true -.PHONY: all babylond-env babylond-dlv babylond-rmi bitcoind bitcoind-rmi +.PHONY: all babylond-env babylond-dlv babylond-rmi bitcoinsim bitcoinsim-rmi diff --git a/contrib/images/bitcoind/Dockerfile b/contrib/images/bitcoinsim/Dockerfile similarity index 70% rename from contrib/images/bitcoind/Dockerfile rename to contrib/images/bitcoinsim/Dockerfile index d5a7963fc..df41df859 100644 --- a/contrib/images/bitcoind/Dockerfile +++ b/contrib/images/bitcoinsim/Dockerfile @@ -14,12 +14,12 @@ RUN cd btcwallet && go install -v . ./cmd/... && cd - FROM alpine:3.14 AS run -RUN apk add bash curl jq expect expect-dev -WORKDIR /bitcoind +RUN apk add bash expect expect-dev +WORKDIR /bitcoinsim COPY --from=build /go/bin/* /usr/bin/ -COPY contrib/images/bitcoind/wrapper.sh /bitcoind/wrapper.sh -COPY contrib/images/bitcoind/btcwallet_create.exp /bitcoind/btcwallet_create.exp +COPY contrib/images/bitcoinsim/wrapper.sh /bitcoinsim/wrapper.sh +COPY contrib/images/bitcoinsim/btcwallet_create.exp /bitcoinsim/btcwallet_create.exp ENV RPCUSER=rpcuser ENV RPCPASS=rpcpass @@ -28,6 +28,6 @@ ENV GENERATE_INTERVAL_SECS=30 EXPOSE 18554 18555 18556 -ENTRYPOINT ["/bitcoind/wrapper.sh"] +ENTRYPOINT ["/bitcoinsim/wrapper.sh"] CMD [] STOPSIGNAL SIGTERM diff --git a/contrib/images/bitcoind/README.md b/contrib/images/bitcoinsim/README.md similarity index 92% rename from contrib/images/bitcoind/README.md rename to contrib/images/bitcoinsim/README.md index aab974306..f9c66b03d 100644 --- a/contrib/images/bitcoind/README.md +++ b/contrib/images/bitcoinsim/README.md @@ -1,6 +1,6 @@ -# bitcoind +# bitcoinsim -`bitcoind` is a Docker image we build to be able to run a single Bitcoin node in the local devnet that produces blocks at regular intervals without consuming CPU. +`bitcoinsim` is a Docker image we build to be able to run a single Bitcoin node in the local devnet that produces blocks at regular intervals without consuming CPU. To achieve this we use [btcd](https://github.com/btcsuite/btcd) in `--simnet` mode and make calls to the [generate](https://github.com/btcsuite/btcd/blob/v0.23.1/rpcserver.go#L886) API endpoint. @@ -17,7 +17,7 @@ See more at: You can build the image with the following command: ```bash -make bitcoind +make bitcoinsim ``` ## Test @@ -25,13 +25,13 @@ make bitcoind One way to see if it works is to run the container interactively: ```bash -docker run -it --rm --name bitcoind babylonchain/bintcoind +docker run -it --rm --name bitcoinsim babylonchain/bitcoinsim ``` The logs should show that a wallet is created with and mining is started: ```console -$ docker run -it --rm babylonchain/bintcoind +$ docker run -it --rm babylonchain/bitcoinsim Starting btcd... Creating a wallet... spawn btcwallet --simnet -u rpcuser -P rpcpass --create @@ -132,11 +132,11 @@ Then we can connect to this from another container to query the balance: ```console $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -93e58681637d babylonchain/bintcoind "/bitcoind/wrapper.sh" 5 minutes ago Up 5 minutes 18554-18556/tcp bitcoind -$ docker exec -it bitcoind sh -/bitcoind # btcctl --simnet --wallet -u $RPCUSER -P $RPCPASS getbalance +93e58681637d babylonchain/bitcoinsim "/bitcoinsim/wrapper.sh" 5 minutes ago Up 5 minutes 18554-18556/tcp bitcoinsim +$ docker exec -it bitcoinsim sh +/bitcoinsim # btcctl --simnet --wallet -u $RPCUSER -P $RPCPASS getbalance 600 -/bitcoind # +/bitcoinsim # ``` @@ -149,14 +149,14 @@ The image has been added to the main `docker-compose.yml` file. It will build it It can be started on its own like so: ```bash -docker-compose up -d bitcoind +docker-compose up -d bitcoinsim ``` Currently the image doesn't support restarting, the container has to be completely removed and recreated if it's stopped. It can be removed with the following command: ```bash -docker-compose stop bitcoind -docker-compose rm bitcoind +docker-compose stop bitcoinsim +docker-compose rm bitcoinsim ``` The ports are mapped to the same default ports that `btcd` and `btcwallet` would use, so if the host already runs these services they might clash. diff --git a/contrib/images/bitcoind/btcwallet_create.exp b/contrib/images/bitcoinsim/btcwallet_create.exp similarity index 100% rename from contrib/images/bitcoind/btcwallet_create.exp rename to contrib/images/bitcoinsim/btcwallet_create.exp diff --git a/contrib/images/bitcoind/wrapper.sh b/contrib/images/bitcoinsim/wrapper.sh similarity index 100% rename from contrib/images/bitcoind/wrapper.sh rename to contrib/images/bitcoinsim/wrapper.sh diff --git a/docker-compose.yml b/docker-compose.yml index bc5c1ccf9..399dc2abb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -89,12 +89,12 @@ services: localnet: ipv4_address: 192.168.10.5 - bitcoind: + bitcoinsim: build: context: ./ - dockerfile: ./contrib/images/bitcoind/Dockerfile - image: babylonchain/bitcoind:latest - container_name: bitcoind + dockerfile: ./contrib/images/bitcoinsim/Dockerfile + image: babylonchain/bitcoinsim:latest + container_name: bitcoinsim ports: - 18554-18556:18554-18556 networks: