Skip to content

Commit e6e444c

Browse files
committed
refactor: add and use EnsureAnyAddrman in rpc
1 parent bf589a5 commit e6e444c

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/rpc/net.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -949,10 +949,7 @@ static RPCHelpMan addpeeraddress()
949949
},
950950
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
951951
{
952-
NodeContext& node = EnsureAnyNodeContext(request.context);
953-
if (!node.addrman) {
954-
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
955-
}
952+
AddrMan& addrman = EnsureAnyAddrman(request.context);
956953

957954
const std::string& addr_string{request.params[0].get_str()};
958955
const auto port{request.params[1].getInt<uint16_t>()};
@@ -968,11 +965,11 @@ static RPCHelpMan addpeeraddress()
968965
address.nTime = Now<NodeSeconds>();
969966
// The source address is set equal to the address. This is equivalent to the peer
970967
// announcing itself.
971-
if (node.addrman->Add({address}, address)) {
968+
if (addrman.Add({address}, address)) {
972969
success = true;
973970
if (tried) {
974971
// Attempt to move the address to the tried addresses table.
975-
node.addrman->Good(address);
972+
addrman.Good(address);
976973
}
977974
}
978975
}
@@ -1048,25 +1045,22 @@ static RPCHelpMan getaddrmaninfo()
10481045
}},
10491046
RPCExamples{HelpExampleCli("getaddrmaninfo", "") + HelpExampleRpc("getaddrmaninfo", "")},
10501047
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
1051-
NodeContext& node = EnsureAnyNodeContext(request.context);
1052-
if (!node.addrman) {
1053-
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
1054-
}
1048+
AddrMan& addrman = EnsureAnyAddrman(request.context);
10551049

10561050
UniValue ret(UniValue::VOBJ);
10571051
for (int n = 0; n < NET_MAX; ++n) {
10581052
enum Network network = static_cast<enum Network>(n);
10591053
if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
10601054
UniValue obj(UniValue::VOBJ);
1061-
obj.pushKV("new", node.addrman->Size(network, true));
1062-
obj.pushKV("tried", node.addrman->Size(network, false));
1063-
obj.pushKV("total", node.addrman->Size(network));
1055+
obj.pushKV("new", addrman.Size(network, true));
1056+
obj.pushKV("tried", addrman.Size(network, false));
1057+
obj.pushKV("total", addrman.Size(network));
10641058
ret.pushKV(GetNetworkName(network), obj);
10651059
}
10661060
UniValue obj(UniValue::VOBJ);
1067-
obj.pushKV("new", node.addrman->Size(std::nullopt, true));
1068-
obj.pushKV("tried", node.addrman->Size(std::nullopt, false));
1069-
obj.pushKV("total", node.addrman->Size());
1061+
obj.pushKV("new", addrman.Size(std::nullopt, true));
1062+
obj.pushKV("tried", addrman.Size(std::nullopt, false));
1063+
obj.pushKV("total", addrman.Size());
10701064
ret.pushKV("all_networks", obj);
10711065
return ret;
10721066
},
@@ -1128,14 +1122,11 @@ static RPCHelpMan getrawaddrman()
11281122
+ HelpExampleRpc("getrawaddrman", "")
11291123
},
11301124
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
1131-
NodeContext& node = EnsureAnyNodeContext(request.context);
1132-
if (!node.addrman) {
1133-
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
1134-
}
1125+
AddrMan& addrman = EnsureAnyAddrman(request.context);
11351126

11361127
UniValue ret(UniValue::VOBJ);
1137-
ret.pushKV("new", AddrmanTableToJSON(node.addrman->GetEntries(false)));
1138-
ret.pushKV("tried", AddrmanTableToJSON(node.addrman->GetEntries(true)));
1128+
ret.pushKV("new", AddrmanTableToJSON(addrman.GetEntries(false)));
1129+
ret.pushKV("tried", AddrmanTableToJSON(addrman.GetEntries(true)));
11391130
return ret;
11401131
},
11411132
};

src/rpc/server_util.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,16 @@ PeerManager& EnsurePeerman(const NodeContext& node)
108108
}
109109
return *node.peerman;
110110
}
111+
112+
AddrMan& EnsureAddrman(const NodeContext& node)
113+
{
114+
if (!node.addrman) {
115+
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
116+
}
117+
return *node.addrman;
118+
}
119+
120+
AddrMan& EnsureAnyAddrman(const std::any& context)
121+
{
122+
return EnsureAddrman(EnsureAnyNodeContext(context));
123+
}

src/rpc/server_util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <any>
99

10+
class AddrMan;
1011
class ArgsManager;
1112
class CBlockPolicyEstimator;
1213
class CConnman;
@@ -31,5 +32,7 @@ CBlockPolicyEstimator& EnsureFeeEstimator(const node::NodeContext& node);
3132
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context);
3233
CConnman& EnsureConnman(const node::NodeContext& node);
3334
PeerManager& EnsurePeerman(const node::NodeContext& node);
35+
AddrMan& EnsureAddrman(const node::NodeContext& node);
36+
AddrMan& EnsureAnyAddrman(const std::any& context);
3437

3538
#endif // BITCOIN_RPC_SERVER_UTIL_H

0 commit comments

Comments
 (0)