Merge bitcoin/bitcoin#22043: rpc, test: addpeeraddress test coverage, code simplify/constness

b36e0cd1b9 rpc: simplify addpeeraddress and improve code constness (Jon Atack)
6b1926cf1e 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`](b36e0cd1b9)
  MarcoFalke:
    review ACK b36e0cd1b9 💭

Tree-SHA512: 01773fb70f23db5abf46806bb27804e48feff27272b2e6582bd5b886e9715088eb2d84755106bce2ad6f88e21582f7f071a30a89d5b17286d899c3dd8553b4fc
This commit is contained in:
MarcoFalke
2021-05-25 09:39:55 +02:00
2 changed files with 37 additions and 16 deletions

View File

@@ -931,26 +931,22 @@ static RPCHelpMan addpeeraddress()
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
}
const std::string& addr_string{request.params[0].get_str()};
const uint16_t port{static_cast<uint16_t>(request.params[1].get_int())};
UniValue obj(UniValue::VOBJ);
std::string addr_string = request.params[0].get_str();
uint16_t port{static_cast<uint16_t>(request.params[1].get_int())};
CNetAddr net_addr;
if (!LookupHost(addr_string, net_addr, false)) {
obj.pushKV("success", false);
return obj;
}
CAddress address = CAddress({net_addr, port}, ServiceFlags(NODE_NETWORK|NODE_WITNESS));
address.nTime = GetAdjustedTime();
// The source address is set equal to the address. This is equivalent to the peer
// announcing itself.
if (!node.addrman->Add(address, address)) {
obj.pushKV("success", false);
return obj;
bool success{false};
if (LookupHost(addr_string, net_addr, false)) {
CAddress address{CAddress({net_addr, port}, ServiceFlags(NODE_NETWORK | NODE_WITNESS))};
address.nTime = GetAdjustedTime();
// The source address is set equal to the address. This is equivalent to the peer
// announcing itself.
if (node.addrman->Add(address, address)) success = true;
}
obj.pushKV("success", true);
obj.pushKV("success", success);
return obj;
},
};