Skip to content

Commit f69adc1

Browse files
committed
Add ZMQ interface for BitcoinD
1 parent d571b7b commit f69adc1

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/lib.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ pub struct ConnectParams {
6363
pub rpc_socket: SocketAddrV4,
6464
/// p2p connection url, is some if the node started with p2p enabled
6565
pub p2p_socket: Option<SocketAddrV4>,
66+
/// zmq pub raw block connection url
67+
pub zmq_pub_raw_block_socket: Option<SocketAddrV4>,
68+
/// zmq pub raw tx connection Url
69+
pub zmq_pub_raw_tx_socket: Option<SocketAddrV4>,
6670
}
6771

6872
pub struct CookieValues {
@@ -217,6 +221,9 @@ pub struct Conf<'a> {
217221
/// happen they are used at the time the process is spawn. When retrying other available ports
218222
/// are returned reducing the probability of conflicts to negligible.
219223
pub attempts: u8,
224+
225+
/// Enable the ZMQ interface to be accessible.
226+
pub enable_zmq: bool,
220227
}
221228

222229
impl Default for Conf<'_> {
@@ -229,6 +236,7 @@ impl Default for Conf<'_> {
229236
tmpdir: None,
230237
staticdir: None,
231238
attempts: 3,
239+
enable_zmq: false,
232240
}
233241
}
234242
}
@@ -284,6 +292,25 @@ impl BitcoinD {
284292
(args, Some(p2p_socket))
285293
}
286294
};
295+
296+
let (zmq_args, zmq_pub_raw_tx_socket, zmq_pub_raw_block_socket) = match conf.enable_zmq {
297+
true => {
298+
let zmq_pub_raw_tx_port = get_available_port()?;
299+
let zmq_pub_raw_tx_socket = SocketAddrV4::new(LOCAL_IP, zmq_pub_raw_tx_port);
300+
let zmq_pub_raw_block_port = get_available_port()?;
301+
let zmq_pub_raw_block_socket = SocketAddrV4::new(LOCAL_IP, zmq_pub_raw_block_port);
302+
let zmqpubrawblock_arg =
303+
format!("-zmqpubrawblock=tcp://0.0.0.0:{}", zmq_pub_raw_block_port);
304+
let zmqpubrawtx_arg = format!("-zmqpubrawtx=tcp://0.0.0.0:{}", zmq_pub_raw_tx_port);
305+
(
306+
vec![zmqpubrawtx_arg, zmqpubrawblock_arg],
307+
Some(zmq_pub_raw_tx_socket),
308+
Some(zmq_pub_raw_block_socket),
309+
)
310+
}
311+
false => (vec![], None, None),
312+
};
313+
287314
let stdout = if conf.view_stdout {
288315
Stdio::inherit()
289316
} else {
@@ -307,6 +334,7 @@ impl BitcoinD {
307334
.args(&default_args)
308335
.args(&p2p_args)
309336
.args(&conf_args)
337+
.args(&zmq_args)
310338
.stdout(stdout)
311339
.spawn()
312340
.with_context(|| format!("Error while executing {:?}", exe.as_ref()))?;
@@ -365,6 +393,8 @@ impl BitcoinD {
365393
cookie_file,
366394
rpc_socket,
367395
p2p_socket,
396+
zmq_pub_raw_block_socket,
397+
zmq_pub_raw_tx_socket,
368398
},
369399
})
370400
}
@@ -767,6 +797,25 @@ mod test {
767797
assert_eq!(password, result_values.password);
768798
}
769799

800+
#[test]
801+
fn zmq_interface_enabled() {
802+
let mut conf = Conf::default();
803+
conf.enable_zmq = true;
804+
let bitcoind = BitcoinD::with_conf(exe_path().unwrap(), &conf).unwrap();
805+
806+
assert!(bitcoind.params.zmq_pub_raw_tx_socket.is_some());
807+
assert!(bitcoind.params.zmq_pub_raw_block_socket.is_some());
808+
}
809+
810+
#[test]
811+
fn zmq_interface_disabled() {
812+
let exe = init();
813+
let bitcoind = BitcoinD::new(exe).unwrap();
814+
815+
assert!(bitcoind.params.zmq_pub_raw_tx_socket.is_none());
816+
assert!(bitcoind.params.zmq_pub_raw_block_socket.is_none());
817+
}
818+
770819
fn peers_connected(client: &Client) -> usize {
771820
let result: Vec<Value> = client.call("getpeerinfo", &[]).unwrap();
772821
result.len()

0 commit comments

Comments
 (0)