-
Notifications
You must be signed in to change notification settings - Fork 38.7k
RPC: Add new "getzmqnotifications" method #13570
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| } | ||
| } |
| 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 |
| 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() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test should be skipped similar to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is indeed a good point! Since this was already merged, should I create a new PR with this fix?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll work on a fix and send in a PR with it as soon as I'm ready. Thanks again for spotting this!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created #13646. |
||
|
|
||
| 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() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could just
return notifiers?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No because
notifiersis a list of non-constCZMQAbstractNotifier, and I'd rather not return the mutable objects. That's the reason why I originally had this "add to JSON" method - but I think that the additional copy done here is probably not relevant in practice.