Skip to content

Commit 5027d41

Browse files
committed
Merge bitcoin/bitcoin#26366: rpc, test: addnode improv + add test coverage for invalid command
f52cb02 doc: make it clear that `node` in `addnode` refers to the node's address (brunoerg) effd1ef test: `addnode` with an invalid command should throw an error (brunoerg) 56b27b8 rpc, refactor: clean-up `addnode` (brunoerg) Pull request description: This PR: - Adds test coverage for an invalid `command` in `addnode`. - Rename `test_getaddednodeinfo` to `test_addnode_getaddednodeinfo` and its log since this function also tests `addnode` and it doesn't worth to split into 2 ones. - Makes it clear in docs that `node` in `addnode` refers to the node's address. It seemed a little weird for me "The node (see getpeerinfo for nodes)", it could mean a lot of things e.g. the node id. - Some small improv/clean-up: use `const` where possible, rename some vars, and remove the check for nullance for `command` since it's a non-optional field. ACKs for top commit: achow101: ACK f52cb02 jonatack: ACK f52cb02 theStack: re-ACK f52cb02 Tree-SHA512: e4a69e58b784e233463945b4d55a401957f9fe4562c129f59216a44f44fb3221d3449ac578fb35e665ca654c6ade2e741b72c3df78040f7527229c77b6c5b82e
2 parents 1d4846a + f52cb02 commit 5027d41

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/rpc/net.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ static RPCHelpMan addnode()
287287
strprintf("Addnode connections are limited to %u at a time", MAX_ADDNODE_CONNECTIONS) +
288288
" and are counted separately from the -maxconnections limit.\n",
289289
{
290-
{"node", RPCArg::Type::STR, RPCArg::Optional::NO, "The node (see getpeerinfo for nodes)"},
290+
{"node", RPCArg::Type::STR, RPCArg::Optional::NO, "The address of the peer to connect to"},
291291
{"command", RPCArg::Type::STR, RPCArg::Optional::NO, "'add' to add a node to the list, 'remove' to remove a node from the list, 'onetry' to try a connection to the node once"},
292292
},
293293
RPCResult{RPCResult::Type::NONE, "", ""},
@@ -297,35 +297,33 @@ static RPCHelpMan addnode()
297297
},
298298
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
299299
{
300-
std::string strCommand;
301-
if (!request.params[1].isNull())
302-
strCommand = request.params[1].get_str();
303-
if (strCommand != "onetry" && strCommand != "add" && strCommand != "remove") {
300+
const std::string command{request.params[1].get_str()};
301+
if (command != "onetry" && command != "add" && command != "remove") {
304302
throw std::runtime_error(
305303
self.ToString());
306304
}
307305

308306
NodeContext& node = EnsureAnyNodeContext(request.context);
309307
CConnman& connman = EnsureConnman(node);
310308

311-
std::string strNode = request.params[0].get_str();
309+
const std::string node_arg{request.params[0].get_str()};
312310

313-
if (strCommand == "onetry")
311+
if (command == "onetry")
314312
{
315313
CAddress addr;
316-
connman.OpenNetworkConnection(addr, false, nullptr, strNode.c_str(), ConnectionType::MANUAL);
314+
connman.OpenNetworkConnection(addr, /*fCountFailure=*/false, /*grantOutbound=*/nullptr, node_arg.c_str(), ConnectionType::MANUAL);
317315
return UniValue::VNULL;
318316
}
319317

320-
if (strCommand == "add")
318+
if (command == "add")
321319
{
322-
if (!connman.AddNode(strNode)) {
320+
if (!connman.AddNode(node_arg)) {
323321
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
324322
}
325323
}
326-
else if(strCommand == "remove")
324+
else if (command == "remove")
327325
{
328-
if (!connman.RemoveAddedNode(strNode)) {
326+
if (!connman.RemoveAddedNode(node_arg)) {
329327
throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node could not be removed. It has not been added previously.");
330328
}
331329
}

test/functional/rpc_net.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def run_test(self):
6161
self.test_getpeerinfo()
6262
self.test_getnettotals()
6363
self.test_getnetworkinfo()
64-
self.test_getaddednodeinfo()
64+
self.test_addnode_getaddednodeinfo()
6565
self.test_service_flags()
6666
self.test_getnodeaddresses()
6767
self.test_addpeeraddress()
@@ -205,8 +205,8 @@ def test_getnetworkinfo(self):
205205
# Check dynamically generated networks list in getnetworkinfo help output.
206206
assert "(ipv4, ipv6, onion, i2p, cjdns)" in self.nodes[0].help("getnetworkinfo")
207207

208-
def test_getaddednodeinfo(self):
209-
self.log.info("Test getaddednodeinfo")
208+
def test_addnode_getaddednodeinfo(self):
209+
self.log.info("Test addnode and getaddednodeinfo")
210210
assert_equal(self.nodes[0].getaddednodeinfo(), [])
211211
# add a node (node2) to node0
212212
ip_port = "127.0.0.1:{}".format(p2p_port(2))
@@ -220,6 +220,8 @@ def test_getaddednodeinfo(self):
220220
# check that node can be removed
221221
self.nodes[0].addnode(node=ip_port, command='remove')
222222
assert_equal(self.nodes[0].getaddednodeinfo(), [])
223+
# check that an invalid command returns an error
224+
assert_raises_rpc_error(-1, 'addnode "node" "command"', self.nodes[0].addnode, node=ip_port, command='abc')
223225
# check that trying to remove the node again returns an error
224226
assert_raises_rpc_error(-24, "Node could not be removed", self.nodes[0].addnode, node=ip_port, command='remove')
225227
# check that a non-existent node returns an error

0 commit comments

Comments
 (0)