Skip to content

Commit 40f7a28

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22043: rpc, test: addpeeraddress test coverage, code simplify/constness
b36e0cd rpc: simplify addpeeraddress and improve code constness (Jon Atack) 6b1926c test: addpeeraddress functional test coverage (Jon Atack) Pull request description: - Add functional test coverage for rpc addpeeraddress - Simplify addpeeraddress and improve code constness ACKs for top commit: klementtan: ACK [`b36e0cd`](bitcoin/bitcoin@b36e0cd) MarcoFalke: review ACK b36e0cd 💭 Tree-SHA512: 01773fb70f23db5abf46806bb27804e48feff27272b2e6582bd5b886e9715088eb2d84755106bce2ad6f88e21582f7f071a30a89d5b17286d899c3dd8553b4fc
2 parents db1aca0 + b36e0cd commit 40f7a28

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

src/rpc/net.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -931,26 +931,22 @@ static RPCHelpMan addpeeraddress()
931931
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
932932
}
933933

934-
UniValue obj(UniValue::VOBJ);
935-
936-
std::string addr_string = request.params[0].get_str();
937-
uint16_t port{static_cast<uint16_t>(request.params[1].get_int())};
934+
const std::string& addr_string{request.params[0].get_str()};
935+
const uint16_t port{static_cast<uint16_t>(request.params[1].get_int())};
938936

937+
UniValue obj(UniValue::VOBJ);
939938
CNetAddr net_addr;
940-
if (!LookupHost(addr_string, net_addr, false)) {
941-
obj.pushKV("success", false);
942-
return obj;
943-
}
944-
CAddress address = CAddress({net_addr, port}, ServiceFlags(NODE_NETWORK|NODE_WITNESS));
945-
address.nTime = GetAdjustedTime();
946-
// The source address is set equal to the address. This is equivalent to the peer
947-
// announcing itself.
948-
if (!node.addrman->Add(address, address)) {
949-
obj.pushKV("success", false);
950-
return obj;
939+
bool success{false};
940+
941+
if (LookupHost(addr_string, net_addr, false)) {
942+
CAddress address{CAddress({net_addr, port}, ServiceFlags(NODE_NETWORK | NODE_WITNESS))};
943+
address.nTime = GetAdjustedTime();
944+
// The source address is set equal to the address. This is equivalent to the peer
945+
// announcing itself.
946+
if (node.addrman->Add(address, address)) success = true;
951947
}
952948

953-
obj.pushKV("success", true);
949+
obj.pushKV("success", success);
954950
return obj;
955951
},
956952
};

test/functional/rpc_net.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def run_test(self):
6969
self.test_getaddednodeinfo()
7070
self.test_service_flags()
7171
self.test_getnodeaddresses()
72+
self.test_addpeeraddress()
7273

7374
def test_connection_count(self):
7475
self.log.info("Test getconnectioncount")
@@ -236,6 +237,30 @@ def test_getnodeaddresses(self):
236237
assert_raises_rpc_error(-8, "Address count out of range", self.nodes[0].getnodeaddresses, -1)
237238
assert_raises_rpc_error(-8, "Network not recognized: Foo", self.nodes[0].getnodeaddresses, 1, "Foo")
238239

240+
def test_addpeeraddress(self):
241+
self.log.info("Test addpeeraddress")
242+
node = self.nodes[1]
243+
244+
self.log.debug("Test that addpeerinfo is a hidden RPC")
245+
# It is hidden from general help, but its detailed help may be called directly.
246+
assert "addpeerinfo" not in node.help()
247+
assert "addpeerinfo" in node.help("addpeerinfo")
248+
249+
self.log.debug("Test that adding an empty address fails")
250+
assert_equal(node.addpeeraddress(address="", port=8333), {"success": False})
251+
assert_equal(node.getnodeaddresses(count=0), [])
252+
253+
self.log.debug("Test that adding a valid address succeeds")
254+
assert_equal(node.addpeeraddress(address="1.2.3.4", port=8333), {"success": True})
255+
addrs = node.getnodeaddresses(count=0)
256+
assert_equal(len(addrs), 1)
257+
assert_equal(addrs[0]["address"], "1.2.3.4")
258+
assert_equal(addrs[0]["port"], 8333)
259+
260+
self.log.debug("Test that adding the same address again when already present fails")
261+
assert_equal(node.addpeeraddress(address="1.2.3.4", port=8333), {"success": False})
262+
assert_equal(len(node.getnodeaddresses(count=0)), 1)
263+
239264

240265
if __name__ == '__main__':
241266
NetTest().main()

0 commit comments

Comments
 (0)