diff --git a/.gitignore b/.gitignore index 331fae5..7d317d3 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ go.work.sum out contrib/bitcoind-data + +contrib/fork-* diff --git a/contrib/bitcoin-mock.md b/contrib/bitcoin-mock.md index d8f57b9..2d520eb 100644 --- a/contrib/bitcoin-mock.md +++ b/contrib/bitcoin-mock.md @@ -53,17 +53,37 @@ docker exec -it bitcoind-node /bin/bash Then you can generate a block: ```sh -bitcoin-cli -regtest generate +bitcoin-cli -regtest -generate ``` If RPC params are required, you can provide them: ```sh -bitcoin-cli -regtest -rpcuser=user -rpcpassword=password generate +bitcoin-cli -regtest -rpcuser=user -rpcpassword=password -generate ``` More information in [developer.bitcoin.org -> testing](https://developer.bitcoin.org/examples/testing.html). +### Create BTC fork for testing + +In a few cases, we must create a BTC fork for testing. The `create-fork.sh` script helps you do this. We provide two functions: + +#### Create fork + +Creates a new fork starting at the latest block in the snapshot data. + +```sh +./create-fork.sh create +``` + +#### Extract fork + +Extracts any block between a specified range. This command below returns the list of block headers in this range. + +```sh +./create-fork.sh extract +``` + ### Reference - [Running Bitcoind with ZMQ](https://bitcoindev.network/accessing-bitcoins-zeromq-interface/) diff --git a/contrib/create-fork.sh b/contrib/create-fork.sh new file mode 100755 index 0000000..c44db73 --- /dev/null +++ b/contrib/create-fork.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +# Create fork +function createFork() { + forkName="./fork-"$1 + forkLength=$2 + + cp -rf ./bitcoind-snapshot/ $forkName + BITCOIND_DATA=$forkName docker-compose up -d + docker exec -it bitcoind-node bitcoin-cli -generate $forkLength + docker-compose down +} + +# Extract block between range +function extractFork() { + forkName="./fork-"$1 + start=$2 + end=$3 + + BITCOIND_DATA=$forkName docker-compose up -d + docker exec -it bitcoind-node bitcoin-cli -generate $forkLength + + for ((i=$start; i<=$end; i++)); do + hash=$(docker exec -it bitcoind-node bitcoin-cli getblockhash $i | tr -d "\r\n") + docker exec -it bitcoind-node bitcoin-cli getblockheader $hash false + done + docker-compose down +} + + +case "$1" in + create) + createFork $2 $3 + ;; + extract) + extractFork $2 $3 $4 + ;; + *) + echo "Invalid option." + exit 1 + ;; +esac diff --git a/contrib/docker-compose.yaml b/contrib/docker-compose.yaml index c755c6e..c155b22 100644 --- a/contrib/docker-compose.yaml +++ b/contrib/docker-compose.yaml @@ -5,7 +5,7 @@ services: image: kylemanna/bitcoind@sha256:2400e64960457b22be55299a2d7fa2aaa217f3dfc4afb84387e5511fe8ce5055 # restart: unless-stopped volumes: - - ./bitcoind-data:/bitcoin/.bitcoin + - ${BITCOIND_DATA:-./bitcoind-data}:/bitcoin/.bitcoin ports: # regtest ports - 127.0.0.1:28331:28331 # zmq sequence