Skip to content

Commit a012087

Browse files
committed
Return correct error codes in setban().
The setban() RPC was returning misleading or incorrect error codes (for example RPC_CLIENT_NODE_ALREADY_ADDED when an invalid IP address was entered). This commit fixes those error codes: - RPC_CLIENT_INVALID_IP_OR_SUBNET should be returned if the client enters an invalid IP address or subnet. This commit also updates the test cases to explicitly test the error code. This commit also adds a testcase for trying to setban on an invalid subnet.
1 parent 960bc7f commit a012087

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

qa/rpc-tests/nodehandling.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ def run_test(self):
2929
assert_equal(len(self.nodes[2].listbanned()), 0)
3030
self.nodes[2].setban("127.0.0.0/24", "add")
3131
assert_equal(len(self.nodes[2].listbanned()), 1)
32-
try:
33-
self.nodes[2].setban("127.0.0.1", "add") #throws exception because 127.0.0.1 is within range 127.0.0.0/24
34-
except:
35-
pass
32+
# This will throw an exception because 127.0.0.1 is within range 127.0.0.0/24
33+
assert_raises_jsonrpc(-23, "IP/Subnet already banned", self.nodes[2].setban, "127.0.0.1", "add")
34+
# This will throw an exception because 127.0.0.1/42 is not a real subnet
35+
assert_raises_jsonrpc(-30, "Error: Invalid IP/Subnet", self.nodes[2].setban, "127.0.0.1/42", "add")
3636
assert_equal(len(self.nodes[2].listbanned()), 1) #still only one banned ip because 127.0.0.1 is within the range of 127.0.0.0/24
37-
try:
38-
self.nodes[2].setban("127.0.0.1", "remove")
39-
except:
40-
pass
37+
# This will throw an exception because 127.0.0.1 was not added above
38+
assert_raises_jsonrpc(-30, "Error: Unban failed", self.nodes[2].setban, "127.0.0.1", "remove")
4139
assert_equal(len(self.nodes[2].listbanned()), 1)
4240
self.nodes[2].setban("127.0.0.0/24", "remove")
4341
assert_equal(len(self.nodes[2].listbanned()), 0)

src/rpc/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ UniValue setban(const JSONRPCRequest& request)
506506
LookupSubNet(request.params[0].get_str().c_str(), subNet);
507507

508508
if (! (isSubnet ? subNet.IsValid() : netAddr.IsValid()) )
509-
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Invalid IP/Subnet");
509+
throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Invalid IP/Subnet");
510510

511511
if (strCommand == "add")
512512
{
@@ -526,7 +526,7 @@ UniValue setban(const JSONRPCRequest& request)
526526
else if(strCommand == "remove")
527527
{
528528
if (!( isSubnet ? g_connman->Unban(subNet) : g_connman->Unban(netAddr) ))
529-
throw JSONRPCError(RPC_MISC_ERROR, "Error: Unban failed");
529+
throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Unban failed. Requested address/subnet was not previously banned.");
530530
}
531531
return NullUniValue;
532532
}

0 commit comments

Comments
 (0)