Skip to content

Commit 4f7ff3f

Browse files
feat: detects rbf and updates database
1 parent 8af5fa5 commit 4f7ff3f

5 files changed

Lines changed: 199 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ version = "0.23.0"
2929
[[example]]
3030
name = "bdk_sqlx_postgres"
3131
path = "examples/bdk_sqlx_postgres.rs"
32+
33+
[[example]]
34+
name = "regtest_bdk_sqlx_postgres"
35+
path = "examples/regtest_bdk_sqlx_postgres.rs"

Justfile

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Default task: Show available commands
2+
default:
3+
just --list
4+
5+
# Set environment variables for all commands
6+
export DATABASE_URL := "postgres://postgres:password@localhost:5432/mydatabase"
7+
export ELECTRUM_URL := "localhost:50001"
8+
export NODE_URL := "http://localhost:18443"
9+
export NODE_USERNAME := "custom_user"
10+
export NODE_PASSWORD := "custom_pass"
11+
export RUST_BACKTRACE := "1"
12+
13+
# Setup network
14+
setup-network:
15+
# Check if the network exists first
16+
docker network inspect bitcoin-network >/dev/null 2>&1 || docker network create bitcoin-network
17+
# Verify network was created
18+
docker network inspect bitcoin-network >/dev/null 2>&1 && echo "Bitcoin network is ready" || (echo "Failed to create bitcoin-network" && exit 1)
19+
20+
# PostgreSQL commands
21+
start-postgres:
22+
docker run -d --name postgres \
23+
-p 5432:5432 \
24+
-e POSTGRES_USER=postgres \
25+
-e POSTGRES_PASSWORD=password \
26+
-e POSTGRES_DB=mydatabase \
27+
postgres:15
28+
29+
test-postgres:
30+
PGPASSWORD=password psql -h localhost -p 5432 -U postgres -d mydatabase -c "SELECT 1"
31+
32+
# Bitcoin Core commands
33+
build-bitcoin:
34+
docker build -t bitcoin-nix \
35+
--build-arg NODE_USERNAME=custom_user \
36+
--build-arg NODE_PASSWORD=custom_pass \
37+
-f bitcoin.Dockerfile .
38+
39+
start-bitcoin: setup-network
40+
docker run -d --name bitcoin-node \
41+
--network bitcoin-network \
42+
-p 18443:18443 \
43+
bitcoin-nix
44+
# Add a delay or check to ensure bitcoin is ready
45+
echo "Waiting for Bitcoin node to start..."
46+
for i in {1..30}; do \
47+
if just test-bitcoin >/dev/null 2>&1; then \
48+
echo "Bitcoin node is ready!"; \
49+
break; \
50+
fi; \
51+
echo "Waiting... ($i/30)"; \
52+
sleep 1; \
53+
if [ $i -eq 30 ]; then \
54+
echo "Timed out waiting for Bitcoin node to start"; \
55+
exit 1; \
56+
fi; \
57+
done
58+
59+
test-bitcoin:
60+
curl --silent --user custom_user:custom_pass \
61+
--data-binary '{"jsonrpc": "1.0", "id":"bitcointest", "method": "getblockchaininfo", "params": []}' \
62+
-H 'content-type: text/plain;' http://localhost:18443
63+
64+
generate-blocks:
65+
curl --silent --user custom_user:custom_pass \
66+
--data-binary '{"jsonrpc": "1.0", "id":"bitcointest", "method": "generatetoaddress", "params": [101, "bcrt1prdfvfk2ddxe8y88qxhwkxn9cy0d2w6k98gj9smwz6tqjcnl4tdwsrf03aw"]}' \
67+
-H 'content-type: text/plain;' http://localhost:18443
68+
69+
# Electrs commands
70+
build-electrs:
71+
docker build -t electrs-nix \
72+
-f electrs.Dockerfile .
73+
74+
start-electrs: setup-network
75+
docker run -d --name electrs \
76+
--network bitcoin-network \
77+
-p 50001:50001 \
78+
electrs-nix
79+
echo "Electrs started"
80+
81+
check-electrs:
82+
docker logs electrs
83+
84+
85+
86+
# Kill any services that might be using the ports
87+
kill-services:
88+
# Kill any process using PostgreSQL port
89+
lsof -ti:5432 | xargs kill -9 || true
90+
# Kill any process using Bitcoin RPC port
91+
lsof -ti:18443 | xargs kill -9 || true
92+
# Kill any process using Electrs port
93+
lsof -ti:50001 | xargs kill -9 || true
94+
95+
# Stop and remove Docker containers
96+
stop-containers:
97+
docker stop postgres bitcoin-node electrs || true
98+
docker rm postgres bitcoin-node electrs || true
99+
100+
# Clean everything
101+
clean-all:
102+
just kill-services
103+
just stop-containers
104+
docker network rm bitcoin-network || true
105+
# Make sure all orphaned networks are also removed
106+
docker network prune -f
107+
echo "Environment cleaned successfully"
108+
109+
# Run the regtest example with all required services
110+
run-example-regtest:
111+
# Clean up any existing containers first
112+
just stop-containers
113+
# Start all required services
114+
just start-postgres
115+
just build-bitcoin
116+
just start-bitcoin
117+
just build-electrs
118+
just start-electrs
119+
# Generate initial blocks
120+
just generate-blocks
121+
# Run the regtest example
122+
cargo run --example regtest_bdk_sqlx_postgres

bitcoin.Dockerfile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Use the official Nix base image
2+
FROM nixos/nix:latest
3+
4+
# Define build arguments with defaults
5+
ARG NODE_USERNAME
6+
ARG NODE_PASSWORD
7+
8+
# Install bitcoind and basic tools via Nix
9+
RUN nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs && \
10+
nix-channel --update && \
11+
nix-env -iA nixpkgs.bitcoind nixpkgs.coreutils
12+
13+
# Create data directory
14+
RUN mkdir -p /data/bitcoin
15+
16+
# Expose Bitcoin RPC port and P2P port
17+
EXPOSE 18443
18+
EXPOSE 18444
19+
20+
# Set the entrypoint using shell form to enable variable interpolation
21+
ENTRYPOINT exec /root/.nix-profile/bin/bitcoind \
22+
"-datadir=/data/bitcoin" \
23+
"-printtoconsole" \
24+
"-regtest=1" \
25+
"-server=1" \
26+
"-txindex=1" \
27+
"-rpcbind=0.0.0.0:18443" \
28+
"-rpcallowip=0.0.0.0/0" \
29+
"-rpcuser=custom_user" \
30+
"-rpcpassword=custom_pass" \
31+
"-rpcworkqueue=64" \
32+
"-rpcthreads=8" \
33+
"-fallbackfee=0.0002" \
34+
"-debug=1"

electrs.Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use the official Nix base image
2+
FROM nixos/nix:latest
3+
4+
# Add the unstable channel
5+
RUN nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs && \
6+
nix-channel --update
7+
8+
# Install electrs from Nixpkgs
9+
RUN nix-env -iA nixpkgs.electrs
10+
11+
# Create data directory for electrs
12+
RUN mkdir -p /data/electrs/
13+
14+
# Expose electrs RPC port (default: 50001)
15+
EXPOSE 50001
16+
17+
COPY electrs.toml /etc/electrs/config.toml
18+
19+
# Command to run electrs with direct auth parameters
20+
CMD ["/root/.nix-profile/bin/electrs", "--conf=/etc/electrs/config.toml"]

electrs.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
auth = "custom_user:custom_pass"
2+
# The listening RPC address of bitcoind
3+
daemon_rpc_addr = "bitcoin-node:18443" # Use the Docker service name
4+
daemon_p2p_addr = "bitcoin-node:18444"
5+
# The listening P2P address of bitcoind
6+
# Note: Your Bitcoin node has P2P listening disabled (-listen=0)
7+
# so this connection will fail, but electrs needs P2P
8+
9+
# Network type
10+
network = "regtest"
11+
12+
# Database directory
13+
db_dir = "/data/electrs"
14+
15+
# Electrum server address
16+
electrum_rpc_addr = "0.0.0.0:50001" # Allow external connections
17+
18+
# Log level
19+
log_filters = "INFO"

0 commit comments

Comments
 (0)