aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/net.cpp
diff options
context:
space:
mode:
authorAndrew Chow <github@achow101.com>2023-09-21 06:04:05 -0400
committerAndrew Chow <github@achow101.com>2023-09-21 06:35:16 -0400
commit5027d41988d4a92be5a9fc846a680e805fc71383 (patch)
tree76f8eadfa6f6ef9ba8edbc72c4197f51eddb98cd /src/rpc/net.cpp
parent1d4846a8443be901b8a5deb0e357481af22838d0 (diff)
parentf52cb02f700b58bca921a7aa24bfeee04760262b (diff)
Merge bitcoin/bitcoin#26366: rpc, test: `addnode` improv + add test coverage for invalid command
f52cb02f700b58bca921a7aa24bfeee04760262b doc: make it clear that `node` in `addnode` refers to the node's address (brunoerg) effd1efefb53c58f0e43fec4f019a19f97795553 test: `addnode` with an invalid command should throw an error (brunoerg) 56b27b84877376ffc32b3bad09f1047b23de4ba1 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 f52cb02f700b58bca921a7aa24bfeee04760262b jonatack: ACK f52cb02f700b58bca921a7aa24bfeee04760262b theStack: re-ACK f52cb02f700b58bca921a7aa24bfeee04760262b Tree-SHA512: e4a69e58b784e233463945b4d55a401957f9fe4562c129f59216a44f44fb3221d3449ac578fb35e665ca654c6ade2e741b72c3df78040f7527229c77b6c5b82e
Diffstat (limited to 'src/rpc/net.cpp')
-rw-r--r--src/rpc/net.cpp22
1 files changed, 10 insertions, 12 deletions
diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp
index e9e9ad62ae..6af62641bd 100644
--- a/src/rpc/net.cpp
+++ b/src/rpc/net.cpp
@@ -287,7 +287,7 @@ static RPCHelpMan addnode()
strprintf("Addnode connections are limited to %u at a time", MAX_ADDNODE_CONNECTIONS) +
" and are counted separately from the -maxconnections limit.\n",
{
- {"node", RPCArg::Type::STR, RPCArg::Optional::NO, "The node (see getpeerinfo for nodes)"},
+ {"node", RPCArg::Type::STR, RPCArg::Optional::NO, "The address of the peer to connect to"},
{"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"},
},
RPCResult{RPCResult::Type::NONE, "", ""},
@@ -297,10 +297,8 @@ static RPCHelpMan addnode()
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
- std::string strCommand;
- if (!request.params[1].isNull())
- strCommand = request.params[1].get_str();
- if (strCommand != "onetry" && strCommand != "add" && strCommand != "remove") {
+ const std::string command{request.params[1].get_str()};
+ if (command != "onetry" && command != "add" && command != "remove") {
throw std::runtime_error(
self.ToString());
}
@@ -308,24 +306,24 @@ static RPCHelpMan addnode()
NodeContext& node = EnsureAnyNodeContext(request.context);
CConnman& connman = EnsureConnman(node);
- std::string strNode = request.params[0].get_str();
+ const std::string node_arg{request.params[0].get_str()};
- if (strCommand == "onetry")
+ if (command == "onetry")
{
CAddress addr;
- connman.OpenNetworkConnection(addr, false, nullptr, strNode.c_str(), ConnectionType::MANUAL);
+ connman.OpenNetworkConnection(addr, /*fCountFailure=*/false, /*grantOutbound=*/nullptr, node_arg.c_str(), ConnectionType::MANUAL);
return UniValue::VNULL;
}
- if (strCommand == "add")
+ if (command == "add")
{
- if (!connman.AddNode(strNode)) {
+ if (!connman.AddNode(node_arg)) {
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
}
}
- else if(strCommand == "remove")
+ else if (command == "remove")
{
- if (!connman.RemoveAddedNode(strNode)) {
+ if (!connman.RemoveAddedNode(node_arg)) {
throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node could not be removed. It has not been added previously.");
}
}