From 76b08b810cf6b102bd0ca98d544a16975d03f513 Mon Sep 17 00:00:00 2001 From: Lakshya Singh Date: Wed, 20 Sep 2023 19:48:41 +0530 Subject: [PATCH 1/5] chore: config assets for multiple chains - lightning config - env vars - bitcoin.conf for different networks - copy configs in lightningd repo Signed-off-by: Lakshya Singh --- assets/bitcoin.conf | 28 +++++++++++++++++++++------- assets/main-config | 9 +++++++++ assets/main-env | 4 ++++ assets/regtest-config | 9 +++++++++ assets/regtest-env | 4 ++++ assets/testnet-config | 3 ++- lightningd/Dockerfile | 2 ++ lightningd/entrypoint.sh | 2 ++ 8 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 assets/main-config create mode 100644 assets/main-env create mode 100644 assets/regtest-config create mode 100644 assets/regtest-env diff --git a/assets/bitcoin.conf b/assets/bitcoin.conf index f4aa604..0a3978c 100644 --- a/assets/bitcoin.conf +++ b/assets/bitcoin.conf @@ -2,15 +2,29 @@ ## bitcoin.conf configuration file. Lines beginning with # are comments. ## -[test] server=1 +rpcallowip=0.0.0.0/0 +whitelist=0.0.0.0/0 +txindex=1 +blockfilterindex=1 + +[main] +rpcbind=0.0.0.0 +rpcuser=rpcuser +rpcpassword=VLSsigner1 +rpcport=8332 +port=8333 + +[test] +rpcbind=0.0.0.0 rpcuser=rpcuser rpcpassword=VLSsigner1 rpcport=18332 +port=18333 + +[regtest] rpcbind=0.0.0.0 -rpcallowip=0.0.0.0/0 -whitelist=0.0.0.0/0 -zmqpubrawblock=tcp://bitcoind:38332 -zmqpubrawtx=tcp://bitcoind:38333 -txindex=1 -blockfilterindex=1 \ No newline at end of file +rpcuser=rpcuser +rpcpassword=VLSsigner1 +rpcport=38332 +port=38333 \ No newline at end of file diff --git a/assets/main-config b/assets/main-config new file mode 100644 index 0000000..95d6daa --- /dev/null +++ b/assets/main-config @@ -0,0 +1,9 @@ +network=regtest +bitcoin-rpcuser=rpcuser +bitcoin-rpcpassword=VLSsigner1 +bitcoin-rpcport=8332 +log-level=info +max-locktime-blocks=288 +important-plugin=/usr/bin/clboss +clboss-auto-close=true +bind-addr=0.0.0.0:9735 \ No newline at end of file diff --git a/assets/main-env b/assets/main-env new file mode 100644 index 0000000..bad59e7 --- /dev/null +++ b/assets/main-env @@ -0,0 +1,4 @@ +VLS_PORT=17701 +VLS_NETWORK=regtest +BITCOIND_RPC_URL=http://rpcuser:VLSsigner1@bitcoind:8332 +RUST_LOG=info \ No newline at end of file diff --git a/assets/regtest-config b/assets/regtest-config new file mode 100644 index 0000000..545c024 --- /dev/null +++ b/assets/regtest-config @@ -0,0 +1,9 @@ +network=regtest +bitcoin-rpcuser=rpcuser +bitcoin-rpcpassword=VLSsigner1 +bitcoin-rpcport=38332 +log-level=info +max-locktime-blocks=288 +important-plugin=/usr/bin/clboss +clboss-auto-close=true +bind-addr=0.0.0.0:19846 \ No newline at end of file diff --git a/assets/regtest-env b/assets/regtest-env new file mode 100644 index 0000000..a21ee38 --- /dev/null +++ b/assets/regtest-env @@ -0,0 +1,4 @@ +VLS_PORT=17701 +VLS_NETWORK=regtest +BITCOIND_RPC_URL=http://rpcuser:VLSsigner1@bitcoind:38332 +RUST_LOG=info \ No newline at end of file diff --git a/assets/testnet-config b/assets/testnet-config index 3980616..06f71f8 100644 --- a/assets/testnet-config +++ b/assets/testnet-config @@ -5,4 +5,5 @@ bitcoin-rpcport=18332 log-level=info max-locktime-blocks=288 important-plugin=/usr/bin/clboss -clboss-auto-close=true \ No newline at end of file +clboss-auto-close=true +bind-addr=0.0.0.0:19735 \ No newline at end of file diff --git a/lightningd/Dockerfile b/lightningd/Dockerfile index d8df184..5c64431 100644 --- a/lightningd/Dockerfile +++ b/lightningd/Dockerfile @@ -97,6 +97,8 @@ RUN mkdir -p "${BITCOIND_HOME}" && \ COPY assets/bitcoin.conf "${BITCOIND_HOME}"/bitcoin.conf COPY assets/testnet-config /testnet-config +COPY assets/regtest-config /regtest-config +COPY assets/main-config /main-config USER lightning diff --git a/lightningd/entrypoint.sh b/lightningd/entrypoint.sh index dcb89b1..a9fcbfa 100755 --- a/lightningd/entrypoint.sh +++ b/lightningd/entrypoint.sh @@ -2,6 +2,8 @@ set -e cp -u /testnet-config ${LIGHTNINGD_DATA}/testnet-config +cp -u /regtest-config ${LIGHTNINGD_DATA}/regtest-config +cp -u /main-config ${LIGHTNINGD_DATA}/main-config export GREENLIGHT_VERSION=$(lightningd --version) From 0484ff64757a585cf3f8f3580ac246e956b3f77a Mon Sep 17 00:00:00 2001 From: Lakshya Singh Date: Tue, 26 Sep 2023 00:37:51 +0530 Subject: [PATCH 2/5] refactor: use ARG instead of ENV - keep environment variables clean Signed-off-by: Lakshya Singh --- bitcoind/Dockerfile | 4 ++-- lightningd/Dockerfile | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bitcoind/Dockerfile b/bitcoind/Dockerfile index d0753d3..07f761c 100644 --- a/bitcoind/Dockerfile +++ b/bitcoind/Dockerfile @@ -46,8 +46,8 @@ RUN set -ex \ done && \ wget -O- https://raw.githubusercontent.com/Kvaciral/kvaciral/main/kvaciral.asc | gpg --import -ENV BITCOIN_VERSION=23.0 -ENV BITCOIN_PREFIX=/opt/bitcoin-${BITCOIN_VERSION} +ARG BITCOIN_VERSION=23.0 +ARG BITCOIN_PREFIX=/opt/bitcoin-${BITCOIN_VERSION} ARG SHA256SUMS_HASH=aaff81ea001f499e8f6f3221387d7db960d71a3b7a4a2b1aaf2c8060bc94a391 RUN wget https://bitcoincore.org/bin/bitcoin-core-${BITCOIN_VERSION}/SHA256SUMS diff --git a/lightningd/Dockerfile b/lightningd/Dockerfile index 5c64431..4dc327b 100644 --- a/lightningd/Dockerfile +++ b/lightningd/Dockerfile @@ -68,8 +68,8 @@ RUN apk update && \ sqlite-dev ARG LIGHTNINGD_UID=101 -ENV LIGHTNINGD_USER=lightning -ENV LIGHTNINGD_HOME=/home/${LIGHTNINGD_USER} +ARG LIGHTNINGD_USER=lightning +ARG LIGHTNINGD_HOME=/home/${LIGHTNINGD_USER} ENV LIGHTNINGD_DATA=${LIGHTNINGD_HOME}/.lightning \ LIGHTNINGD_RPC_PORT=9835 \ LIGHTNINGD_PORT=9735 \ From 5b0cb901cb8422a7a03599b8e349679e46d24d60 Mon Sep 17 00:00:00 2001 From: Lakshya Singh Date: Tue, 26 Sep 2023 00:40:53 +0530 Subject: [PATCH 3/5] feat: allow docker compose overrides - use file override to handle multiple networks - expose and map only required ports - name containers differently - add environment for chain variable - dont expose ports in image Signed-off-by: Lakshya Singh --- bitcoind/Dockerfile | 2 -- docker-compose.mainnet.yml | 25 +++++++++++++++++++++++++ docker-compose.regtest.yml | 25 +++++++++++++++++++++++++ docker-compose.testnet.yml | 10 ++++++++++ docker-compose.yml | 34 ++++++++-------------------------- 5 files changed, 68 insertions(+), 28 deletions(-) create mode 100644 docker-compose.mainnet.yml create mode 100644 docker-compose.regtest.yml create mode 100644 docker-compose.testnet.yml diff --git a/bitcoind/Dockerfile b/bitcoind/Dockerfile index 07f761c..feb830d 100644 --- a/bitcoind/Dockerfile +++ b/bitcoind/Dockerfile @@ -121,8 +121,6 @@ RUN chmod +x /healthcheck.sh VOLUME ["${BITCOIN_DATA}"] -EXPOSE 8332 8333 18332 18333 18444 - RUN mkdir -p "${BITCOIN_DATA}" RUN chown -R bitcoin:bitcoin "${BITCOIN_DATA}" diff --git a/docker-compose.mainnet.yml b/docker-compose.mainnet.yml new file mode 100644 index 0000000..6806566 --- /dev/null +++ b/docker-compose.mainnet.yml @@ -0,0 +1,25 @@ +version: "3.8" +name: main +services: + bitcoin-core: + container_name: bitcoind-main + expose: + - 8332 + ports: + - 8333:8333 + environment: + - BITCOIN_CHAIN=main + + core-lightning: + container_name: lightningd-main + command: + - --conf=/home/lightning/.lightning/main-config + - --bitcoin-rpcconnect=bitcoind + expose: + - 9735 + ports: + - 9735:9735 + env_file: + - ./assets/main-env + environment: + - BITCOIN_CHAIN=main \ No newline at end of file diff --git a/docker-compose.regtest.yml b/docker-compose.regtest.yml new file mode 100644 index 0000000..46134c6 --- /dev/null +++ b/docker-compose.regtest.yml @@ -0,0 +1,25 @@ +version: "3.8" +name: regtest +services: + bitcoin-core: + container_name: bitcoind-regtest + expose: + - 38332 + ports: + - 38333:38333 + environment: + - BITCOIN_CHAIN=regtest + + core-lightning: + container_name: lightningd-regtest + command: + - --conf=/home/lightning/.lightning/regtest-config + - --bitcoin-rpcconnect=bitcoind + expose: + - 19846 + ports: + - 19846:19846 + env_file: + - ./assets/regtest-env + environment: + - BITCOIN_CHAIN=regtest \ No newline at end of file diff --git a/docker-compose.testnet.yml b/docker-compose.testnet.yml new file mode 100644 index 0000000..250495a --- /dev/null +++ b/docker-compose.testnet.yml @@ -0,0 +1,10 @@ +version: "3.8" +name: testnet +services: + bitcoin-core: + ports: + - 18333:18333 + + core-lightning: + ports: + - 19735:19735 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index b1e6ddb..4ef7ef5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,22 +4,11 @@ services: build: dockerfile: ./bitcoind/Dockerfile image: bitcoind - container_name: bitcoind - command: - - --testnet - - -pid=/home/bitcoin/.bitcoin/testnet3/bitcoind-testnet.pid + container_name: bitcoind-test volumes: - data:/home/bitcoin/.bitcoin expose: - - 8332 - - 8333 - 18332 - - 18333 - - 18444 - ports: - - 8333:8333 - - 18333:18333 - - 38333:38333 networks: LN_testing: aliases: @@ -31,31 +20,24 @@ services: build: dockerfile: ./lightningd/Dockerfile image: lightningd - container_name: lightningd + container_name: lightningd-test command: - - --conf=/home/lightning/.lightning/testnet-config + - --conf=/home/lightning/.lightning/test-config - --bitcoin-rpcconnect=bitcoind - - --bind-addr=core-lightning:19735 - - --announce-addr=core-lightning:19735 volumes: - clightning:/home/lightning/.lightning expose: - - 9735 - - 9835 - ports: - - 9735:9735 - - 19735:19735 + - 19735 networks: - LN_testing - links: - - bitcoin-core:bitcoind depends_on: bitcoin-core: condition: service_healthy restart: true env_file: - - ./assets/testnet-env - + - ./assets/test-env + environment: + - BITCOIN_CHAIN=test volumes: data: @@ -66,4 +48,4 @@ volumes: external: true networks: - LN_testing: \ No newline at end of file + LN_testing: From 5c2d225be23801f80dfc814dd9ee1c336163ff4c Mon Sep 17 00:00:00 2001 From: Lakshya Singh Date: Tue, 26 Sep 2023 00:41:49 +0530 Subject: [PATCH 4/5] feat: update scripts to use env vars - bitcoind entrypoint set chain value - bitcoind healthcheck fix cli parameter - lightning entrypoint script - lightning health script - prepend chain in bitcoin.conf Signed-off-by: Lakshya Singh --- bitcoind/entrypoint.sh | 11 +++++++++-- bitcoind/healthcheck.sh | 2 +- docker-compose.yml | 4 ++-- lightningd/Dockerfile | 17 +++++++++++------ lightningd/healthcheck.sh | 15 +++++++++++++++ 5 files changed, 38 insertions(+), 11 deletions(-) create mode 100755 lightningd/healthcheck.sh diff --git a/bitcoind/entrypoint.sh b/bitcoind/entrypoint.sh index 1e49f65..9f01595 100755 --- a/bitcoind/entrypoint.sh +++ b/bitcoind/entrypoint.sh @@ -1,7 +1,8 @@ #!/bin/sh set -e -cp -u /bitcoin.conf $BITCOIN_DATA/ +cp /bitcoin.conf $BITCOIN_DATA/ +sed -i "1s/^/chain=$BITCOIN_CHAIN\n/" $BITCOIN_DATA/bitcoin.conf if [ $(echo "$1" | cut -c1) = "-" ]; then echo "$0: assuming arguments for bitcoind" @@ -9,5 +10,11 @@ if [ $(echo "$1" | cut -c1) = "-" ]; then set -- bitcoind "$@" fi +if [ $(echo "$1" | cut -c1) = "-" ] || [ "$1" = "bitcoind" ]; then + echo "$0: setting chain to $BITCOIN_CHAIN" + + set -- "$@" -chain=$BITCOIN_CHAIN +fi + echo -exec "$@" \ No newline at end of file +exec "$@" diff --git a/bitcoind/healthcheck.sh b/bitcoind/healthcheck.sh index 593bf1b..d8b421a 100755 --- a/bitcoind/healthcheck.sh +++ b/bitcoind/healthcheck.sh @@ -1 +1 @@ -bitcoin-cli --chain=$BITCOIN_CHAIN getblockchaininfo \ No newline at end of file +bitcoin-cli -chain=$BITCOIN_CHAIN getblockchaininfo \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 4ef7ef5..8760f56 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,7 +22,7 @@ services: image: lightningd container_name: lightningd-test command: - - --conf=/home/lightning/.lightning/test-config + - --conf=/home/lightning/.lightning/testnet-config - --bitcoin-rpcconnect=bitcoind volumes: - clightning:/home/lightning/.lightning @@ -35,7 +35,7 @@ services: condition: service_healthy restart: true env_file: - - ./assets/test-env + - ./assets/testnet-env environment: - BITCOIN_CHAIN=test diff --git a/lightningd/Dockerfile b/lightningd/Dockerfile index 4dc327b..e831aee 100644 --- a/lightningd/Dockerfile +++ b/lightningd/Dockerfile @@ -75,8 +75,6 @@ ENV LIGHTNINGD_DATA=${LIGHTNINGD_HOME}/.lightning \ LIGHTNINGD_PORT=9735 \ BITCOIND_HOME=/root/.bitcoin -COPY lightningd/entrypoint.sh /entrypoint.sh - COPY --from=builder /usr/bin/lightningd /usr/bin/ COPY --from=builder /usr/bin/lightning-cli /usr/bin/ COPY --from=builder /usr/bin/lightning-hsmtool /usr/bin/ @@ -87,8 +85,15 @@ COPY --from=builder /usr/bin/clboss /usr/bin/clboss RUN addgroup -S lightning && adduser -S lightning -G lightning && \ mkdir -p ${LIGHTNINGD_DATA} && \ - touch ${LIGHTNINGD_DATA}/config && \ - chown -R lightning:lightning "${LIGHTNINGD_DATA}" + chown -R lightning:lightning ${LIGHTNINGD_DATA} + +COPY assets/bitcoin.conf "${BITCOIND_HOME}"/bitcoin.conf + +COPY lightningd/entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +COPY lightningd/healthcheck.sh /healthcheck.sh +RUN chmod +x /healthcheck.sh VOLUME ["${LIGHTNINGD_DATA}"] @@ -102,8 +107,8 @@ COPY assets/main-config /main-config USER lightning -HEALTHCHECK --interval=30s --timeout=10s --start-period=30s \ - CMD lighting-cli --testnet getinfo +HEALTHCHECK --interval=10s --timeout=10s --start-period=15s \ + CMD ["/bin/sh", "-c", "/healthcheck.sh"] ENTRYPOINT ["/entrypoint.sh"] CMD ["lightningd"] diff --git a/lightningd/healthcheck.sh b/lightningd/healthcheck.sh new file mode 100755 index 0000000..e753eec --- /dev/null +++ b/lightningd/healthcheck.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +set -ex + +LIGHTNING_NETWORK="" + +case $BITCOIN_CHAIN in + "test") LIGHTNING_NETWORK=testnet ;; + "regtest") LIGHTNING_NETWORK=regtest ;; + "main") LIGHTNING_NETWORK=bitcoin ;; + "signet") LIGHTNING_NETWORK=signet ;; + *) echo "Invalid BITCOIN_CHAIN value: $BITCOIN_CHAIN" && exit 1 ;; +esac + +lightning-cli --network $LIGHTNING_NETWORK getinfo \ No newline at end of file From cbb68c442bb0bef82bb329bf2c097b06e1e5add8 Mon Sep 17 00:00:00 2001 From: Lakshya Singh Date: Fri, 22 Sep 2023 20:18:52 +0530 Subject: [PATCH 5/5] docs: setting up env variable Signed-off-by: Lakshya Singh --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index 54d10df..006529b 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,40 @@ docker volume create lightning_data docker compose up --build ``` +## Using Bitcoin Chains + +We have three possible overrides over the default `testnet` configuration in `docker-compose.yml`: +- `docker-compose.testnet.yml` +- `docker-compose.regtest.yml` +- `docker-compose.mainnet.yml` + +To use override we have to pass it down both the config using `-f` flag: +``` +docker compose -f docker-compose.yml -f up --build +``` + +__Note__: Even while using `testnet` running using the override is recommended as that will expose the `P2P` port for `bitcoind` and `P2P` port for `lightningd` on the host. + +## Additional Regtest Commands + +We have to run these commands after bitcoind is up and running. + +Create Wallet: +``` +docker container exec -it bitcoind bitcoin-cli createwallet default +``` + +Generate Address for node: +``` +docker container exec -it bitcoind bitcoin-cli getnewaddress +``` + +Generate Blocks +``` +docker container exec -it bitcoind bitcoin-cli generatetoaddress 50 $NODE_ADDRESS + +``` + ### References - [bitcoind](https://github.com/ruimarinho/docker-bitcoin-core/blob/master/23/alpine/Dockerfile) by @ruimarinho