Skip to content

Commit

Permalink
Merge #295: add getzmqnotifications rpc
Browse files Browse the repository at this point in the history
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
  • Loading branch information
apoelstra committed Apr 25, 2024
2 parents 1b51e3d + a9be191 commit d44ab83
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
5 changes: 5 additions & 0 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,11 @@ pub trait RpcApi: Sized {
) -> Result<json::ScanTxOutResult> {
self.call("scantxoutset", &["start".into(), into_json(descriptors)?])
}

/// Returns information about the active ZeroMQ notifications
fn get_zmq_notifications(&self) -> Result<Vec<json::GetZmqNotificationsResult>> {
self.call("getzmqnotifications", &[])
}
}

/// Client implements a JSON-RPC client for the Bitcoin Core daemon or compatible APIs.
Expand Down
4 changes: 3 additions & 1 deletion integration_test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ bitcoind -regtest $BLOCKFILTERARG $FALLBACKFEEARG \
-rpcport=12349 \
-server=1 \
-txindex=1 \
-printtoconsole=0 &
-printtoconsole=0 \
-zmqpubrawblock=tcp://0.0.0.0:28332 \
-zmqpubrawtx=tcp://0.0.0.0:28333 &
PID2=$!

# Let it connect to the other node.
Expand Down
33 changes: 32 additions & 1 deletion integration_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use bitcoin::{
Transaction, TxIn, TxOut, Txid, Witness,
};
use bitcoincore_rpc::bitcoincore_rpc_json::{
GetBlockTemplateModes, GetBlockTemplateRules, ScanTxOutRequest,
GetBlockTemplateModes, GetBlockTemplateRules, GetZmqNotificationsResult, ScanTxOutRequest,
};

lazy_static! {
Expand Down Expand Up @@ -226,6 +226,7 @@ fn main() {
test_add_ban(&cl);
test_set_network_active(&cl);
test_get_index_info(&cl);
test_get_zmq_notifications(&cl);
test_stop(cl);
}

Expand Down Expand Up @@ -1426,6 +1427,36 @@ fn test_get_index_info(cl: &Client) {
}
}

fn test_get_zmq_notifications(cl: &Client) {
let mut zmq_info = cl.get_zmq_notifications().unwrap();

// it doesn't matter in which order Bitcoin Core returns the result,
// but checking it is easier if it has a known order

// sort_by_key does not allow returning references to parameters of the compare function
// (removing the lifetime from the return type mimics this behavior, but we don't want it)
fn compare_fn(result: &GetZmqNotificationsResult) -> impl Ord + '_ {
(&result.address, &result.notification_type, result.hwm)
}
zmq_info.sort_by(|a, b| compare_fn(a).cmp(&compare_fn(b)));

assert!(
zmq_info
== [
GetZmqNotificationsResult {
notification_type: "pubrawblock".to_owned(),
address: "tcp://0.0.0.0:28332".to_owned(),
hwm: 1000
},
GetZmqNotificationsResult {
notification_type: "pubrawtx".to_owned(),
address: "tcp://0.0.0.0:28333".to_owned(),
hwm: 1000
},
]
);
}

fn test_stop(cl: Client) {
println!("Stopping: '{}'", cl.stop().unwrap());
}
8 changes: 8 additions & 0 deletions json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2140,6 +2140,14 @@ pub struct GetIndexInfoResult {
pub basic_block_filter_index: Option<IndexStatus>,
}

#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct GetZmqNotificationsResult {
#[serde(rename = "type")]
pub notification_type: String,
pub address: String,
pub hwm: u64,
}

impl<'a> serde::Serialize for PubKeyOrAddress<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down

0 comments on commit d44ab83

Please sign in to comment.