Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getzmqnotifications rpc #295

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading