diff options
author | stratospher <44024636+stratospher@users.noreply.github.com> | 2023-10-03 19:37:56 +0530 |
---|---|---|
committer | stratospher <44024636+stratospher@users.noreply.github.com> | 2023-10-04 08:53:51 +0530 |
commit | e6e444c06cbf09380f9924dff3d21c1be15d1753 (patch) | |
tree | 996404bb43c2c5481a437431404afde07be9b215 /src/rpc/net.cpp | |
parent | bf589a50a0d6a7b94f1ba1ddf24a1497fd35ad44 (diff) |
refactor: add and use EnsureAnyAddrman in rpc
Diffstat (limited to 'src/rpc/net.cpp')
-rw-r--r-- | src/rpc/net.cpp | 35 |
1 files changed, 13 insertions, 22 deletions
diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 70d1cc715f..5a68bf9e1f 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -949,10 +949,7 @@ static RPCHelpMan addpeeraddress() }, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { - NodeContext& node = EnsureAnyNodeContext(request.context); - if (!node.addrman) { - throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled"); - } + AddrMan& addrman = EnsureAnyAddrman(request.context); const std::string& addr_string{request.params[0].get_str()}; const auto port{request.params[1].getInt<uint16_t>()}; @@ -968,11 +965,11 @@ static RPCHelpMan addpeeraddress() address.nTime = Now<NodeSeconds>(); // The source address is set equal to the address. This is equivalent to the peer // announcing itself. - if (node.addrman->Add({address}, address)) { + if (addrman.Add({address}, address)) { success = true; if (tried) { // Attempt to move the address to the tried addresses table. - node.addrman->Good(address); + addrman.Good(address); } } } @@ -1048,25 +1045,22 @@ static RPCHelpMan getaddrmaninfo() }}, RPCExamples{HelpExampleCli("getaddrmaninfo", "") + HelpExampleRpc("getaddrmaninfo", "")}, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { - NodeContext& node = EnsureAnyNodeContext(request.context); - if (!node.addrman) { - throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled"); - } + AddrMan& addrman = EnsureAnyAddrman(request.context); UniValue ret(UniValue::VOBJ); for (int n = 0; n < NET_MAX; ++n) { enum Network network = static_cast<enum Network>(n); if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue; UniValue obj(UniValue::VOBJ); - obj.pushKV("new", node.addrman->Size(network, true)); - obj.pushKV("tried", node.addrman->Size(network, false)); - obj.pushKV("total", node.addrman->Size(network)); + obj.pushKV("new", addrman.Size(network, true)); + obj.pushKV("tried", addrman.Size(network, false)); + obj.pushKV("total", addrman.Size(network)); ret.pushKV(GetNetworkName(network), obj); } UniValue obj(UniValue::VOBJ); - obj.pushKV("new", node.addrman->Size(std::nullopt, true)); - obj.pushKV("tried", node.addrman->Size(std::nullopt, false)); - obj.pushKV("total", node.addrman->Size()); + obj.pushKV("new", addrman.Size(std::nullopt, true)); + obj.pushKV("tried", addrman.Size(std::nullopt, false)); + obj.pushKV("total", addrman.Size()); ret.pushKV("all_networks", obj); return ret; }, @@ -1128,14 +1122,11 @@ static RPCHelpMan getrawaddrman() + HelpExampleRpc("getrawaddrman", "") }, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { - NodeContext& node = EnsureAnyNodeContext(request.context); - if (!node.addrman) { - throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled"); - } + AddrMan& addrman = EnsureAnyAddrman(request.context); UniValue ret(UniValue::VOBJ); - ret.pushKV("new", AddrmanTableToJSON(node.addrman->GetEntries(false))); - ret.pushKV("tried", AddrmanTableToJSON(node.addrman->GetEntries(true))); + ret.pushKV("new", AddrmanTableToJSON(addrman.GetEntries(false))); + ret.pushKV("tried", AddrmanTableToJSON(addrman.GetEntries(true))); return ret; }, }; |