| 982 | } |
| 983 | |
| 984 | static RPCMethod addpeeraddress() |
| 985 | { |
| 986 | return RPCMethod{"addpeeraddress", |
| 987 | "Add the address of a potential peer to an address manager table. This RPC is for testing only.", |
| 988 | { |
| 989 | {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP address of the peer"}, |
| 990 | {"port", RPCArg::Type::NUM, RPCArg::Optional::NO, "The port of the peer"}, |
| 991 | {"tried", RPCArg::Type::BOOL, RPCArg::Default{false}, "If true, attempt to add the peer to the tried addresses table"}, |
| 992 | }, |
| 993 | RPCResult{ |
| 994 | RPCResult::Type::OBJ, "", "", |
| 995 | { |
| 996 | {RPCResult::Type::BOOL, "success", "whether the peer address was successfully added to the address manager table"}, |
| 997 | {RPCResult::Type::STR, "error", /*optional=*/true, "error description, if the address could not be added"}, |
| 998 | }, |
| 999 | }, |
| 1000 | RPCExamples{ |
| 1001 | HelpExampleCli("addpeeraddress", "\"1.2.3.4\" 8333 true") |
| 1002 | + HelpExampleRpc("addpeeraddress", "\"1.2.3.4\", 8333, true") |
| 1003 | }, |
| 1004 | [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue |
| 1005 | { |
| 1006 | AddrMan& addrman = EnsureAnyAddrman(request.context); |
| 1007 | |
| 1008 | const std::string& addr_string{request.params[0].get_str()}; |
| 1009 | const auto port{request.params[1].getInt<uint16_t>()}; |
| 1010 | const bool tried{request.params[2].isNull() ? false : request.params[2].get_bool()}; |
| 1011 | |
| 1012 | UniValue obj(UniValue::VOBJ); |
| 1013 | std::optional<CNetAddr> net_addr{LookupHost(addr_string, false)}; |
| 1014 | if (!net_addr.has_value()) { |
| 1015 | throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Invalid IP address"); |
| 1016 | } |
| 1017 | |
| 1018 | bool success{false}; |
| 1019 | |
| 1020 | CService service{net_addr.value(), port}; |
| 1021 | CAddress address{MaybeFlipIPv6toCJDNS(service), ServiceFlags{NODE_NETWORK | NODE_WITNESS}}; |
| 1022 | address.nTime = Now<NodeSeconds>(); |
| 1023 | // The source address is set equal to the address. This is equivalent to the peer |
| 1024 | // announcing itself. |
| 1025 | if (addrman.Add({address}, address)) { |
| 1026 | success = true; |
| 1027 | if (tried) { |
| 1028 | // Attempt to move the address to the tried addresses table. |
| 1029 | if (!addrman.Good(address)) { |
| 1030 | success = false; |
| 1031 | obj.pushKV("error", "failed-adding-to-tried"); |
| 1032 | } |
| 1033 | } |
| 1034 | } else { |
| 1035 | obj.pushKV("error", "failed-adding-to-new"); |
| 1036 | } |
| 1037 | |
| 1038 | obj.pushKV("success", success); |
| 1039 | return obj; |
| 1040 | }, |
| 1041 | }; |
nothing calls this directly
no test coverage detected