From fe287c32c72cd9c25723c71d6545e191ea4863eb Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 13 Jul 2026 13:00:26 -0400 Subject: [PATCH] Switch block ZMQ notifications to hashblock --- .env.example | 3 ++- README.md | 4 ++-- full-setup/bitcoin-mainnet.conf | 2 +- full-setup/bitcoin-regtest.conf | 2 +- full-setup/bitcoin-testnet.conf | 2 +- full-setup/public-pool-mainnet.env | 1 + full-setup/public-pool-regtest.env | 1 + full-setup/public-pool-testnet.env | 1 + src/services/bitcoin-rpc.service.ts | 12 +++++++++++- 9 files changed, 21 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index 9a3c264..f6a267a 100644 --- a/.env.example +++ b/.env.example @@ -22,7 +22,8 @@ BLOCK_TEMPLATE_RETRY_MAX_MS=1000 #BITCOIN_RPC_AUX_URLS=http://bitcoin-core-2,http://bitcoin-core-3 # Enable in bitcoin.conf with: -# zmqpubrawblock=tcp://0.0.0.0:3000 +# zmqpubhashblock=tcp://0.0.0.0:3000 +BITCOIN_ZMQ_TOPIC=hashblock BITCOIN_ZMQ_HOST=tcp://192.168.1.100:3000 API_PORT=3334 diff --git a/README.md b/README.md index 061a6fe..c22d77b 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ reserve two namespace values per configured Stratum worker. ### New-block notification path The master keeps an authoritative Bitcoin Core `getblocktemplate` longpoll open; -rawblock ZMQ remains a watchdog and duplicate results are discarded. Optional +hashblock ZMQ remains a watchdog and duplicate results are discarded. Optional endpoints in `BITCOIN_RPC_AUX_URLS` keep independent longpolls open and race the primary source, reducing dependence on one node's block-relay peers. An auxiliary template is only eligible after the primary Core's `getbestblockhash` exactly @@ -234,7 +234,7 @@ RPC block template table. ``` rpcallowip=172.16.0.0/12 -zmqpubrawblock=tcp://0.0.0.0:3000 +zmqpubhashblock=tcp://0.0.0.0:3000 ``` to your bitcoin.conf. diff --git a/full-setup/bitcoin-mainnet.conf b/full-setup/bitcoin-mainnet.conf index 39da218..1a7f761 100644 --- a/full-setup/bitcoin-mainnet.conf +++ b/full-setup/bitcoin-mainnet.conf @@ -19,7 +19,7 @@ rpcuser=bitcoin server=1 # notify pool master about new blocks -zmqpubrawblock=tcp://0.0.0.0:3000 +zmqpubhashblock=tcp://0.0.0.0:3000 [main] [test] diff --git a/full-setup/bitcoin-regtest.conf b/full-setup/bitcoin-regtest.conf index 7c6522d..56612fe 100644 --- a/full-setup/bitcoin-regtest.conf +++ b/full-setup/bitcoin-regtest.conf @@ -22,4 +22,4 @@ rpcuser=bitcoin prune=550 # notify pool master about new blocks -zmqpubrawblock=tcp://0.0.0.0:3000 +zmqpubhashblock=tcp://0.0.0.0:3000 diff --git a/full-setup/bitcoin-testnet.conf b/full-setup/bitcoin-testnet.conf index 39da218..1a7f761 100644 --- a/full-setup/bitcoin-testnet.conf +++ b/full-setup/bitcoin-testnet.conf @@ -19,7 +19,7 @@ rpcuser=bitcoin server=1 # notify pool master about new blocks -zmqpubrawblock=tcp://0.0.0.0:3000 +zmqpubhashblock=tcp://0.0.0.0:3000 [main] [test] diff --git a/full-setup/public-pool-mainnet.env b/full-setup/public-pool-mainnet.env index 555a560..cdd7a35 100644 --- a/full-setup/public-pool-mainnet.env +++ b/full-setup/public-pool-mainnet.env @@ -4,6 +4,7 @@ BITCOIN_RPC_PASSWORD=bitcoin BITCOIN_RPC_PORT=8332 BITCOIN_RPC_TIMEOUT=10000 +BITCOIN_ZMQ_TOPIC=hashblock BITCOIN_ZMQ_HOST=tcp://bitcoin:3000 API_PORT=3334 diff --git a/full-setup/public-pool-regtest.env b/full-setup/public-pool-regtest.env index b5cd7b0..d93cde2 100644 --- a/full-setup/public-pool-regtest.env +++ b/full-setup/public-pool-regtest.env @@ -4,6 +4,7 @@ BITCOIN_RPC_PASSWORD=bitcoin BITCOIN_RPC_PORT=28332 BITCOIN_RPC_TIMEOUT=10000 +BITCOIN_ZMQ_TOPIC=hashblock BITCOIN_ZMQ_HOST=tcp://bitcoin-regtest:3000 API_PORT=23334 diff --git a/full-setup/public-pool-testnet.env b/full-setup/public-pool-testnet.env index a633103..75581b4 100644 --- a/full-setup/public-pool-testnet.env +++ b/full-setup/public-pool-testnet.env @@ -4,6 +4,7 @@ BITCOIN_RPC_PASSWORD=bitcoin BITCOIN_RPC_PORT=18332 BITCOIN_RPC_TIMEOUT=10000 +BITCOIN_ZMQ_TOPIC=hashblock BITCOIN_ZMQ_HOST=tcp://bitcoin-testnet:3000 API_PORT=13334 diff --git a/src/services/bitcoin-rpc.service.ts b/src/services/bitcoin-rpc.service.ts index b72f4eb..60fe28d 100644 --- a/src/services/bitcoin-rpc.service.ts +++ b/src/services/bitcoin-rpc.service.ts @@ -249,8 +249,18 @@ export class BitcoinRpcService implements OnModuleInit { console.error('ZMQ Unable to connect, Retrying'); }); + const zmqTopics = (this.configService.get('BITCOIN_ZMQ_TOPIC') + ?? process.env.BITCOIN_ZMQ_TOPIC + ?? 'hashblock') + .split(',') + .map(topic => topic.trim()) + .filter(topic => topic.length > 0); + if (zmqTopics.length === 0) { + zmqTopics.push('hashblock'); + } sock.connect(this.configService.get('BITCOIN_ZMQ_HOST')); - sock.subscribe('rawblock'); + zmqTopics.forEach(topic => sock.subscribe(topic)); + console.log(`ZMQ subscribed to ${zmqTopics.join(',')}`); // Don't await this, otherwise it will block the rest of the program this.listenForNewBlocks(sock);