mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-15 16:38:23 +01:00
Merge bitcoin/bitcoin#33430: rpc: addpeeraddress: throw on invalid IP
316a0c5132rpc: addpeeraddress: throw on invalid IP (John Moffett) Pull request description: Right now we return an opaque `{"success" : false}` in `addpeeraddress` for an empty or invalid IP. This changes it to throw `RPC_CLIENT_INVALID_IP_OR_SUBNET` with the error message `Invalid IP address`. Tests updated to match. ACKs for top commit: sipa: utACK316a0c5132achow101: ACK316a0c5132vasild: ACK316a0c5132pablomartin4btc: tACK316a0c5132Tree-SHA512: 79a8ce127d0a24b2eb1f31bc3294b895d0c6424032a6b49168259e0e94aff69723d067adf1b4dc3c9b79e597531e5b65e4b8fc5a8e21fba0b81f99168de12b96
This commit is contained in:
@@ -1004,26 +1004,28 @@ static RPCHelpMan addpeeraddress()
|
|||||||
|
|
||||||
UniValue obj(UniValue::VOBJ);
|
UniValue obj(UniValue::VOBJ);
|
||||||
std::optional<CNetAddr> net_addr{LookupHost(addr_string, false)};
|
std::optional<CNetAddr> net_addr{LookupHost(addr_string, false)};
|
||||||
|
if (!net_addr.has_value()) {
|
||||||
|
throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Invalid IP address");
|
||||||
|
}
|
||||||
|
|
||||||
bool success{false};
|
bool success{false};
|
||||||
|
|
||||||
if (net_addr.has_value()) {
|
CService service{net_addr.value(), port};
|
||||||
CService service{net_addr.value(), port};
|
CAddress address{MaybeFlipIPv6toCJDNS(service), ServiceFlags{NODE_NETWORK | NODE_WITNESS}};
|
||||||
CAddress address{MaybeFlipIPv6toCJDNS(service), ServiceFlags{NODE_NETWORK | NODE_WITNESS}};
|
address.nTime = Now<NodeSeconds>();
|
||||||
address.nTime = Now<NodeSeconds>();
|
// The source address is set equal to the address. This is equivalent to the peer
|
||||||
// The source address is set equal to the address. This is equivalent to the peer
|
// announcing itself.
|
||||||
// announcing itself.
|
if (addrman.Add({address}, address)) {
|
||||||
if (addrman.Add({address}, address)) {
|
success = true;
|
||||||
success = true;
|
if (tried) {
|
||||||
if (tried) {
|
// Attempt to move the address to the tried addresses table.
|
||||||
// Attempt to move the address to the tried addresses table.
|
if (!addrman.Good(address)) {
|
||||||
if (!addrman.Good(address)) {
|
success = false;
|
||||||
success = false;
|
obj.pushKV("error", "failed-adding-to-tried");
|
||||||
obj.pushKV("error", "failed-adding-to-tried");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
obj.pushKV("error", "failed-adding-to-new");
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
obj.pushKV("error", "failed-adding-to-new");
|
||||||
}
|
}
|
||||||
|
|
||||||
obj.pushKV("success", success);
|
obj.pushKV("success", success);
|
||||||
|
|||||||
@@ -346,9 +346,12 @@ class NetTest(BitcoinTestFramework):
|
|||||||
assert "unknown command: addpeeraddress" not in node.help("addpeeraddress")
|
assert "unknown command: addpeeraddress" not in node.help("addpeeraddress")
|
||||||
|
|
||||||
self.log.debug("Test that adding an empty address fails")
|
self.log.debug("Test that adding an empty address fails")
|
||||||
assert_equal(node.addpeeraddress(address="", port=8333), {"success": False})
|
assert_raises_rpc_error(-30, "Invalid IP address", node.addpeeraddress, address="", port=8333)
|
||||||
assert_equal(node.getnodeaddresses(count=0), [])
|
assert_equal(node.getnodeaddresses(count=0), [])
|
||||||
|
|
||||||
|
self.log.debug("Test that adding a non-IP/hostname fails (no DNS lookup allowed)")
|
||||||
|
assert_raises_rpc_error(-30, "Invalid IP address", node.addpeeraddress, address="not_an_ip", port=8333)
|
||||||
|
|
||||||
self.log.debug("Test that non-bool tried fails")
|
self.log.debug("Test that non-bool tried fails")
|
||||||
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type bool", self.nodes[0].addpeeraddress, address="1.2.3.4", tried="True", port=1234)
|
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type bool", self.nodes[0].addpeeraddress, address="1.2.3.4", tried="True", port=1234)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user