Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create and extract fork script #94

Merged
merged 8 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ go.work.sum
out

contrib/bitcoind-data

contrib/fork-*
24 changes: 22 additions & 2 deletions contrib/bitcoin-mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,37 @@ docker exec -it bitcoind-node /bin/bash
Then you can generate a block:

```sh
bitcoin-cli -regtest generate <number-block>
bitcoin-cli -regtest -generate <number-block>
```

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 <number-block>
```

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 <fork-name> <number-block>
```

#### 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 <fork-name> <start> <end>
```

### Reference

- [Running Bitcoind with ZMQ](https://bitcoindev.network/accessing-bitcoins-zeromq-interface/)
Expand Down
42 changes: 42 additions & 0 deletions contrib/create-fork.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion contrib/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading