diff --git a/bitcoin.conf b/bitcoin.conf index 8c8a62065..3d3e5b882 100644 --- a/bitcoin.conf +++ b/bitcoin.conf @@ -1,9 +1,9 @@ server=1 rpcuser=username rpcpassword=password -testnet4=1 +regtest=1 -[testnet4] +[regtest] sv2=1 sv2port=8442 debug=sv2 \ No newline at end of file diff --git a/devenv.nix b/devenv.nix index d7fade714..94ae4ab78 100644 --- a/devenv.nix +++ b/devenv.nix @@ -57,6 +57,7 @@ in { redis = {exec = withLogging "mkdir -p ${config.devenv.root}/.devenv/state/redis && redis-server --dir ${config.devenv.root}/.devenv/state/redis --port ${toString poolConfig.redis.port}" "redis.log";}; pool = { exec = withLogging '' + DEVENV_ROOT=${config.devenv.root} BITCOIND_DATADIR=${bitcoindDataDir} ${config.devenv.root}/scripts/bitcoind-setup.sh echo "Waiting for Mint..." while ! nc -z localhost ${toString poolConfig.mint.port}; do sleep 1 @@ -95,7 +96,7 @@ in { exec = withLogging '' mkdir -p ${bitcoindDataDir} bitcoind -datadir=${bitcoindDataDir} -conf=${config.devenv.root}/bitcoin.conf - '' "bitcoind-testnet.log"; + '' "bitcoind-regtest.log"; }; miner = { exec = withLogging '' diff --git a/justfile b/justfile index f46cf9f47..69ec0d8c2 100644 --- a/justfile +++ b/justfile @@ -40,3 +40,8 @@ update-bitcoind: sed -i "s|rev = \".*\";|rev = \"$LATEST_COMMIT\";|" bitcoind.nix && \ sed -i "s|hash = \".*\";|hash = \"$HASH\";|" bitcoind.nix && \ echo "Done! bitcoind updated to commit $LATEST_COMMIT\nYou are now ready to test and commit" + +# generate blocks in regtest +generate-blocks COUNT="1": + @echo "Generating {{COUNT}} blocks in regtest..." + @bitcoin-cli -datadir=.devenv/state/bitcoind -conf=$(pwd)/bitcoin.conf -rpcuser=username -rpcpassword=password -regtest -rpcwallet=regtest -generate {{COUNT}} diff --git a/protocols/v2/roles-logic-sv2/src/job_creator.rs b/protocols/v2/roles-logic-sv2/src/job_creator.rs index 1ed653762..9a4d3243e 100644 --- a/protocols/v2/roles-logic-sv2/src/job_creator.rs +++ b/protocols/v2/roles-logic-sv2/src/job_creator.rs @@ -524,7 +524,7 @@ pub mod tests { } const PRIVATE_KEY_BTC: [u8; 32] = [34; 32]; - const NETWORK: Network = Network::Testnet; + const NETWORK: Network = Network::Regtest; #[cfg(feature = "prop_test")] const BLOCK_REWARD: u64 = 625_000_000_000; diff --git a/roles/jd-server/config-examples/jds-config-local-example.toml b/roles/jd-server/config-examples/jds-config-local-example.toml index dc8ce0555..d9c0b55f8 100644 --- a/roles/jd-server/config-examples/jds-config-local-example.toml +++ b/roles/jd-server/config-examples/jds-config-local-example.toml @@ -22,7 +22,7 @@ coinbase_outputs = [ listen_jd_address = "127.0.0.1:34264" # RPC config for mempool (it can be also the same TP if correctly configured) core_rpc_url = "http://127.0.0.1" -core_rpc_port = 48332 +core_rpc_port = 18443 core_rpc_user = "username" core_rpc_pass = "password" # Time interval used for JDS mempool update diff --git a/scripts/bitcoind-setup.sh b/scripts/bitcoind-setup.sh new file mode 100755 index 000000000..523ed273b --- /dev/null +++ b/scripts/bitcoind-setup.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash + +# bitcoind-setup.sh - Initialize regtest environment +# This script waits for bitcoind to be ready, then checks if we need to initialize +# the regtest environment with a wallet and some initial blocks. This should only +# need to be run once per dev environment setup or if you've reset your state. It's +# idempotent, so you can safely run it multiple times. + +set -e + +BITCOIN_CONF="${DEVENV_ROOT:-$(pwd)}/bitcoin.conf" +DATADIR="${BITCOIND_DATADIR:-$(pwd)/.devenv/state/bitcoind}" +RPC_ARGS="-datadir=${DATADIR} -conf=${BITCOIN_CONF} -rpcuser=username -rpcpassword=password -regtest" + +create_and_load_wallet() { + echo "Creating/loading regtest wallet..." + if ! bitcoin-cli $RPC_ARGS createwallet "regtest" 2>/dev/null; then + echo "Wallet exists, attempting to load..." + bitcoin-cli $RPC_ARGS loadwallet "regtest" 2>/dev/null || echo "Wallet already loaded" + fi +} + +echo "Waiting for bitcoind to be ready..." + +# Wait for bitcoind RPC to be available +max_attempts=30 +attempt=0 +while [ $attempt -lt $max_attempts ]; do + if bitcoin-cli $RPC_ARGS getblockchaininfo >/dev/null 2>&1; then + echo "bitcoind is ready!" + break + fi + attempt=$((attempt + 1)) + echo "Attempt $attempt/$max_attempts: bitcoind not ready yet, waiting..." + sleep 2 +done + +if [ $attempt -eq $max_attempts ]; then + echo "ERROR: bitcoind failed to become ready after $max_attempts attempts" + exit 1 +fi + +BLOCK_HEIGHT=$(bitcoin-cli $RPC_ARGS getblockcount 2>/dev/null || echo "0") +echo "Current block height: $BLOCK_HEIGHT" + +# Ensure we have at least 16 blocks and a wallet +if [ "$BLOCK_HEIGHT" -lt "16" ]; then + BLOCKS_NEEDED=$((16 - BLOCK_HEIGHT)) + echo "Block height is $BLOCK_HEIGHT, need to generate $BLOCKS_NEEDED more blocks..." + + create_and_load_wallet + + echo "Generating $BLOCKS_NEEDED blocks to reach height 16..." + bitcoin-cli $RPC_ARGS -rpcwallet=regtest -generate $BLOCKS_NEEDED + + NEW_HEIGHT=$(bitcoin-cli $RPC_ARGS getblockcount) + echo "✅ Regtest environment initialized! New block height: $NEW_HEIGHT" +else + echo "✅ Regtest environment already has sufficient blocks (height: $BLOCK_HEIGHT)" + + create_and_load_wallet +fi + +echo "bitcoind setup complete!"