mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-06-02 15:32:34 +02:00
Merge bitcoin/bitcoin#22079: zmq: Add support to listen on IPv6 addresses
e6998838e5
doc: Add IPv6 address to zmq example (nthumann)8abe5703a9
test: Add IPv6 test to zmq (nthumann)ded449b726
zmq: Enable IPv6 on listening socket (nthumann) Pull request description: This PR adds support for listening on IPv6 addresses with bitcoinds ZMQ interface, just like the RPC server. Currently, it is not possible to specify an IPv6 address, as the `ZMQ_IPV6` [socket option](http://api.zeromq.org/master:zmq-setsockopt#toc27) is not set and therefore the ZMQ initialization fails, if one does so. The absence of this option has also been noted [here](https://github.com/bitcoin/bitcoin/issues/15198#issuecomment-617378512). With this PR one can e.g. set `-zmqpubhashblock=tcp://[::1]:28333` to listen on the IPv6 loopback address. ACKs for top commit: laanwj: Code review ACKe6998838e5
theStack: Tested ACKe6998838e5
🌱 Tree-SHA512: 43c3043d8d5c79794d475926259c1be975b694db4fcc1f7750a9a28e242f0fa1b531735a63ea5777498003aa5834f6243f39742d0f3941f2f37593d0c7890700
This commit is contained in:
commit
b05d3e76e7
@ -84,6 +84,7 @@ For instance:
|
|||||||
|
|
||||||
$ bitcoind -zmqpubhashtx=tcp://127.0.0.1:28332 \
|
$ bitcoind -zmqpubhashtx=tcp://127.0.0.1:28332 \
|
||||||
-zmqpubhashtx=tcp://192.168.1.2:28332 \
|
-zmqpubhashtx=tcp://192.168.1.2:28332 \
|
||||||
|
-zmqpubhashblock="tcp://[::1]:28333" \
|
||||||
-zmqpubrawtx=ipc:///tmp/bitcoind.tx.raw \
|
-zmqpubrawtx=ipc:///tmp/bitcoind.tx.raw \
|
||||||
-zmqpubhashtxhwm=10000
|
-zmqpubhashtxhwm=10000
|
||||||
|
|
||||||
@ -125,6 +126,9 @@ Setting the keepalive values appropriately for your operating environment may
|
|||||||
improve connectivity in situations where long-lived connections are silently
|
improve connectivity in situations where long-lived connections are silently
|
||||||
dropped by network middle boxes.
|
dropped by network middle boxes.
|
||||||
|
|
||||||
|
Also, the socket's ZMQ_IPV6 option is enabled to accept connections from IPv6
|
||||||
|
hosts as well. If needed, this option has to be set on the client side too.
|
||||||
|
|
||||||
## Remarks
|
## Remarks
|
||||||
|
|
||||||
From the perspective of bitcoind, the ZeroMQ socket is write-only; PUB
|
From the perspective of bitcoind, the ZeroMQ socket is write-only; PUB
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <chain.h>
|
#include <chain.h>
|
||||||
#include <chainparams.h>
|
#include <chainparams.h>
|
||||||
|
#include <netbase.h>
|
||||||
#include <node/blockstorage.h>
|
#include <node/blockstorage.h>
|
||||||
#include <rpc/server.h>
|
#include <rpc/server.h>
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
@ -73,6 +74,20 @@ static int zmq_send_multipart(void *sock, const void* data, size_t size, ...)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool IsZMQAddressIPV6(const std::string &zmq_address)
|
||||||
|
{
|
||||||
|
const std::string tcp_prefix = "tcp://";
|
||||||
|
const size_t tcp_index = zmq_address.rfind(tcp_prefix);
|
||||||
|
const size_t colon_index = zmq_address.rfind(":");
|
||||||
|
if (tcp_index == 0 && colon_index != std::string::npos) {
|
||||||
|
const std::string ip = zmq_address.substr(tcp_prefix.length(), colon_index - tcp_prefix.length());
|
||||||
|
CNetAddr addr;
|
||||||
|
LookupHost(ip, addr, false);
|
||||||
|
if (addr.IsIPv6()) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool CZMQAbstractPublishNotifier::Initialize(void *pcontext)
|
bool CZMQAbstractPublishNotifier::Initialize(void *pcontext)
|
||||||
{
|
{
|
||||||
assert(!psocket);
|
assert(!psocket);
|
||||||
@ -107,6 +122,15 @@ bool CZMQAbstractPublishNotifier::Initialize(void *pcontext)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// On some systems (e.g. OpenBSD) the ZMQ_IPV6 must not be enabled, if the address to bind isn't IPv6
|
||||||
|
const int enable_ipv6 { IsZMQAddressIPV6(address) ? 1 : 0};
|
||||||
|
rc = zmq_setsockopt(psocket, ZMQ_IPV6, &enable_ipv6, sizeof(enable_ipv6));
|
||||||
|
if (rc != 0) {
|
||||||
|
zmqError("Failed to set ZMQ_IPV6");
|
||||||
|
zmq_close(psocket);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
rc = zmq_bind(psocket, address.c_str());
|
rc = zmq_bind(psocket, address.c_str());
|
||||||
if (rc != 0)
|
if (rc != 0)
|
||||||
{
|
{
|
||||||
|
@ -24,6 +24,7 @@ from test_framework.util import (
|
|||||||
assert_equal,
|
assert_equal,
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
)
|
)
|
||||||
|
from test_framework.netutil import test_ipv6_local
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
@ -119,6 +120,7 @@ class ZMQTest (BitcoinTestFramework):
|
|||||||
self.test_mempool_sync()
|
self.test_mempool_sync()
|
||||||
self.test_reorg()
|
self.test_reorg()
|
||||||
self.test_multiple_interfaces()
|
self.test_multiple_interfaces()
|
||||||
|
self.test_ipv6()
|
||||||
finally:
|
finally:
|
||||||
# Destroy the ZMQ context.
|
# Destroy the ZMQ context.
|
||||||
self.log.debug("Destroying ZMQ context")
|
self.log.debug("Destroying ZMQ context")
|
||||||
@ -126,10 +128,12 @@ class ZMQTest (BitcoinTestFramework):
|
|||||||
|
|
||||||
# Restart node with the specified zmq notifications enabled, subscribe to
|
# Restart node with the specified zmq notifications enabled, subscribe to
|
||||||
# all of them and return the corresponding ZMQSubscriber objects.
|
# all of them and return the corresponding ZMQSubscriber objects.
|
||||||
def setup_zmq_test(self, services, *, recv_timeout=60, sync_blocks=True):
|
def setup_zmq_test(self, services, *, recv_timeout=60, sync_blocks=True, ipv6=False):
|
||||||
subscribers = []
|
subscribers = []
|
||||||
for topic, address in services:
|
for topic, address in services:
|
||||||
socket = self.ctx.socket(zmq.SUB)
|
socket = self.ctx.socket(zmq.SUB)
|
||||||
|
if ipv6:
|
||||||
|
socket.setsockopt(zmq.IPV6, 1)
|
||||||
subscribers.append(ZMQSubscriber(socket, topic.encode()))
|
subscribers.append(ZMQSubscriber(socket, topic.encode()))
|
||||||
|
|
||||||
self.restart_node(0, [f"-zmqpub{topic}={address}" for topic, address in services] +
|
self.restart_node(0, [f"-zmqpub{topic}={address}" for topic, address in services] +
|
||||||
@ -568,5 +572,22 @@ class ZMQTest (BitcoinTestFramework):
|
|||||||
assert_equal(self.nodes[0].getbestblockhash(), subscribers[0].receive().hex())
|
assert_equal(self.nodes[0].getbestblockhash(), subscribers[0].receive().hex())
|
||||||
assert_equal(self.nodes[0].getbestblockhash(), subscribers[1].receive().hex())
|
assert_equal(self.nodes[0].getbestblockhash(), subscribers[1].receive().hex())
|
||||||
|
|
||||||
|
def test_ipv6(self):
|
||||||
|
if not test_ipv6_local():
|
||||||
|
self.log.info("Skipping IPv6 test, because IPv6 is not supported.")
|
||||||
|
return
|
||||||
|
self.log.info("Testing IPv6")
|
||||||
|
# Set up subscriber using IPv6 loopback address
|
||||||
|
subscribers = self.setup_zmq_test([
|
||||||
|
("hashblock", "tcp://[::1]:28332")
|
||||||
|
], ipv6=True)
|
||||||
|
|
||||||
|
# Generate 1 block in nodes[0]
|
||||||
|
self.nodes[0].generatetoaddress(1, ADDRESS_BCRT1_UNSPENDABLE)
|
||||||
|
|
||||||
|
# Should receive the same block hash
|
||||||
|
assert_equal(self.nodes[0].getbestblockhash(), subscribers[0].receive().hex())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
ZMQTest().main()
|
ZMQTest().main()
|
||||||
|
Loading…
Reference in New Issue
Block a user