forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RPC: Add new getzmqnotifications method.
This adds a new RPC method "getzmqnotifications", which returns information about all active ZMQ notification endpoints. This is useful for software that layers on top of bitcoind, so it can verify that ZeroMQ is enabled and also figure out where it should listen. See bitcoin#13526. Github-Pull: bitcoin#13570 Rebased-From: 76c04a84d4e02acbd4ca78af3d4d7b9e854c1160
- Loading branch information
Showing
8 changed files
with
128 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2018 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <zmq/zmqrpc.h> | ||
|
||
#include <rpc/server.h> | ||
#include <zmq/zmqabstractnotifier.h> | ||
#include <zmq/zmqnotificationinterface.h> | ||
|
||
#include <univalue.h> | ||
|
||
namespace { | ||
|
||
UniValue getzmqnotifications(const JSONRPCRequest& request) | ||
{ | ||
if (request.fHelp || request.params.size() != 0) { | ||
throw std::runtime_error( | ||
"getzmqnotifications\n" | ||
"\nReturns information about the active ZeroMQ notifications.\n" | ||
"\nResult:\n" | ||
"[\n" | ||
" { (json object)\n" | ||
" \"type\": \"pubhashtx\", (string) Type of notification\n" | ||
" \"address\": \"...\" (string) Address of the publisher\n" | ||
" },\n" | ||
" ...\n" | ||
"]\n" | ||
"\nExamples:\n" | ||
+ HelpExampleCli("getzmqnotifications", "") | ||
+ HelpExampleRpc("getzmqnotifications", "") | ||
); | ||
} | ||
|
||
UniValue result(UniValue::VARR); | ||
if (g_zmq_notification_interface != nullptr) { | ||
for (const auto* n : g_zmq_notification_interface->GetActiveNotifiers()) { | ||
UniValue obj(UniValue::VOBJ); | ||
obj.pushKV("type", n->GetType()); | ||
obj.pushKV("address", n->GetAddress()); | ||
result.push_back(obj); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
const CRPCCommand commands[] = | ||
{ // category name actor (function) argNames | ||
// ----------------- ------------------------ ----------------------- ---------- | ||
{ "zmq", "getzmqnotifications", &getzmqnotifications, {} }, | ||
}; | ||
|
||
} // anonymous namespace | ||
|
||
void RegisterZMQRPCCommands(CRPCTable& t) | ||
{ | ||
for (const auto& c : commands) { | ||
t.appendCommand(c.name, &c); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) 2018 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BITCOIN_ZMQ_ZMQRPC_H | ||
#define BITCOIN_ZMQ_ZMQRPC_H | ||
|
||
class CRPCTable; | ||
|
||
void RegisterZMQRPCCommands(CRPCTable& t); | ||
|
||
#endif // BITCOIN_ZMQ_ZMRRPC_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) 2018 The Bitcoin Core developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
"""Test for the ZMQ RPC methods.""" | ||
|
||
from test_framework.test_framework import BitcoinTestFramework | ||
from test_framework.util import assert_equal | ||
|
||
|
||
class RPCZMQTest(BitcoinTestFramework): | ||
|
||
address = "tcp://127.0.0.1:28332" | ||
|
||
def set_test_params(self): | ||
self.num_nodes = 1 | ||
self.setup_clean_chain = True | ||
|
||
def run_test(self): | ||
self._test_getzmqnotifications() | ||
|
||
def _test_getzmqnotifications(self): | ||
self.restart_node(0, extra_args=[]) | ||
assert_equal(self.nodes[0].getzmqnotifications(), []) | ||
|
||
self.restart_node(0, extra_args=["-zmqpubhashtx=%s" % self.address]) | ||
assert_equal(self.nodes[0].getzmqnotifications(), [ | ||
{"type": "pubhashtx", "address": self.address}, | ||
]) | ||
|
||
|
||
if __name__ == '__main__': | ||
RPCZMQTest().main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters