Skip to content

Commit d44ab83

Browse files
committed
Merge #295: add getzmqnotifications rpc
a9be191 add getzmqnotifications integration test (Antoni Spaanderman) b8fa99d add getzmqnotifications rpc (Antoni Spaanderman) Pull request description: this rpc gives information about the active zmq publishers of bitcoin core currently `get_zmq_notifications` returns a Vec directly from bitcoind, but a structure like getindexinfo could also be used because the possible values of the "type" field are known ACKs for top commit: apoelstra: ACK a9be191 tcharding: ACK a9be191 Tree-SHA512: b9c617de9c8f356e259eaf685be63f14b2608a6433d54ed28450c48c740b07af913a217802e8aec49b92c56be559e4b3c7be23a83163c0f8e4bc300c2107c047
2 parents 1b51e3d + a9be191 commit d44ab83

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

client/src/client.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,11 @@ pub trait RpcApi: Sized {
12611261
) -> Result<json::ScanTxOutResult> {
12621262
self.call("scantxoutset", &["start".into(), into_json(descriptors)?])
12631263
}
1264+
1265+
/// Returns information about the active ZeroMQ notifications
1266+
fn get_zmq_notifications(&self) -> Result<Vec<json::GetZmqNotificationsResult>> {
1267+
self.call("getzmqnotifications", &[])
1268+
}
12641269
}
12651270

12661271
/// Client implements a JSON-RPC client for the Bitcoin Core daemon or compatible APIs.

integration_test/run.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ bitcoind -regtest $BLOCKFILTERARG $FALLBACKFEEARG \
3434
-rpcport=12349 \
3535
-server=1 \
3636
-txindex=1 \
37-
-printtoconsole=0 &
37+
-printtoconsole=0 \
38+
-zmqpubrawblock=tcp://0.0.0.0:28332 \
39+
-zmqpubrawtx=tcp://0.0.0.0:28333 &
3840
PID2=$!
3941

4042
# Let it connect to the other node.

integration_test/src/main.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use bitcoin::{
3232
Transaction, TxIn, TxOut, Txid, Witness,
3333
};
3434
use bitcoincore_rpc::bitcoincore_rpc_json::{
35-
GetBlockTemplateModes, GetBlockTemplateRules, ScanTxOutRequest,
35+
GetBlockTemplateModes, GetBlockTemplateRules, GetZmqNotificationsResult, ScanTxOutRequest,
3636
};
3737

3838
lazy_static! {
@@ -226,6 +226,7 @@ fn main() {
226226
test_add_ban(&cl);
227227
test_set_network_active(&cl);
228228
test_get_index_info(&cl);
229+
test_get_zmq_notifications(&cl);
229230
test_stop(cl);
230231
}
231232

@@ -1426,6 +1427,36 @@ fn test_get_index_info(cl: &Client) {
14261427
}
14271428
}
14281429

1430+
fn test_get_zmq_notifications(cl: &Client) {
1431+
let mut zmq_info = cl.get_zmq_notifications().unwrap();
1432+
1433+
// it doesn't matter in which order Bitcoin Core returns the result,
1434+
// but checking it is easier if it has a known order
1435+
1436+
// sort_by_key does not allow returning references to parameters of the compare function
1437+
// (removing the lifetime from the return type mimics this behavior, but we don't want it)
1438+
fn compare_fn(result: &GetZmqNotificationsResult) -> impl Ord + '_ {
1439+
(&result.address, &result.notification_type, result.hwm)
1440+
}
1441+
zmq_info.sort_by(|a, b| compare_fn(a).cmp(&compare_fn(b)));
1442+
1443+
assert!(
1444+
zmq_info
1445+
== [
1446+
GetZmqNotificationsResult {
1447+
notification_type: "pubrawblock".to_owned(),
1448+
address: "tcp://0.0.0.0:28332".to_owned(),
1449+
hwm: 1000
1450+
},
1451+
GetZmqNotificationsResult {
1452+
notification_type: "pubrawtx".to_owned(),
1453+
address: "tcp://0.0.0.0:28333".to_owned(),
1454+
hwm: 1000
1455+
},
1456+
]
1457+
);
1458+
}
1459+
14291460
fn test_stop(cl: Client) {
14301461
println!("Stopping: '{}'", cl.stop().unwrap());
14311462
}

json/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,14 @@ pub struct GetIndexInfoResult {
21402140
pub basic_block_filter_index: Option<IndexStatus>,
21412141
}
21422142

2143+
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
2144+
pub struct GetZmqNotificationsResult {
2145+
#[serde(rename = "type")]
2146+
pub notification_type: String,
2147+
pub address: String,
2148+
pub hwm: u64,
2149+
}
2150+
21432151
impl<'a> serde::Serialize for PubKeyOrAddress<'a> {
21442152
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
21452153
where

0 commit comments

Comments
 (0)