mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 23:09:44 -04:00
refactor: add and use EnsureAnyAddrman in rpc
This commit is contained in:
parent
bf589a50a0
commit
e6e444c06c
3 changed files with 29 additions and 22 deletions
|
@ -949,10 +949,7 @@ static RPCHelpMan addpeeraddress()
|
||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||||
{
|
{
|
||||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
AddrMan& addrman = EnsureAnyAddrman(request.context);
|
||||||
if (!node.addrman) {
|
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& addr_string{request.params[0].get_str()};
|
const std::string& addr_string{request.params[0].get_str()};
|
||||||
const auto port{request.params[1].getInt<uint16_t>()};
|
const auto port{request.params[1].getInt<uint16_t>()};
|
||||||
|
@ -968,11 +965,11 @@ static RPCHelpMan addpeeraddress()
|
||||||
address.nTime = Now<NodeSeconds>();
|
address.nTime = Now<NodeSeconds>();
|
||||||
// The source address is set equal to the address. This is equivalent to the peer
|
// The source address is set equal to the address. This is equivalent to the peer
|
||||||
// announcing itself.
|
// announcing itself.
|
||||||
if (node.addrman->Add({address}, address)) {
|
if (addrman.Add({address}, address)) {
|
||||||
success = true;
|
success = true;
|
||||||
if (tried) {
|
if (tried) {
|
||||||
// Attempt to move the address to the tried addresses table.
|
// 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", "")},
|
RPCExamples{HelpExampleCli("getaddrmaninfo", "") + HelpExampleRpc("getaddrmaninfo", "")},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
|
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
|
||||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
AddrMan& addrman = EnsureAnyAddrman(request.context);
|
||||||
if (!node.addrman) {
|
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
|
|
||||||
}
|
|
||||||
|
|
||||||
UniValue ret(UniValue::VOBJ);
|
UniValue ret(UniValue::VOBJ);
|
||||||
for (int n = 0; n < NET_MAX; ++n) {
|
for (int n = 0; n < NET_MAX; ++n) {
|
||||||
enum Network network = static_cast<enum Network>(n);
|
enum Network network = static_cast<enum Network>(n);
|
||||||
if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
|
if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
|
||||||
UniValue obj(UniValue::VOBJ);
|
UniValue obj(UniValue::VOBJ);
|
||||||
obj.pushKV("new", node.addrman->Size(network, true));
|
obj.pushKV("new", addrman.Size(network, true));
|
||||||
obj.pushKV("tried", node.addrman->Size(network, false));
|
obj.pushKV("tried", addrman.Size(network, false));
|
||||||
obj.pushKV("total", node.addrman->Size(network));
|
obj.pushKV("total", addrman.Size(network));
|
||||||
ret.pushKV(GetNetworkName(network), obj);
|
ret.pushKV(GetNetworkName(network), obj);
|
||||||
}
|
}
|
||||||
UniValue obj(UniValue::VOBJ);
|
UniValue obj(UniValue::VOBJ);
|
||||||
obj.pushKV("new", node.addrman->Size(std::nullopt, true));
|
obj.pushKV("new", addrman.Size(std::nullopt, true));
|
||||||
obj.pushKV("tried", node.addrman->Size(std::nullopt, false));
|
obj.pushKV("tried", addrman.Size(std::nullopt, false));
|
||||||
obj.pushKV("total", node.addrman->Size());
|
obj.pushKV("total", addrman.Size());
|
||||||
ret.pushKV("all_networks", obj);
|
ret.pushKV("all_networks", obj);
|
||||||
return ret;
|
return ret;
|
||||||
},
|
},
|
||||||
|
@ -1128,14 +1122,11 @@ static RPCHelpMan getrawaddrman()
|
||||||
+ HelpExampleRpc("getrawaddrman", "")
|
+ HelpExampleRpc("getrawaddrman", "")
|
||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
|
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
|
||||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
AddrMan& addrman = EnsureAnyAddrman(request.context);
|
||||||
if (!node.addrman) {
|
|
||||||
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
|
|
||||||
}
|
|
||||||
|
|
||||||
UniValue ret(UniValue::VOBJ);
|
UniValue ret(UniValue::VOBJ);
|
||||||
ret.pushKV("new", AddrmanTableToJSON(node.addrman->GetEntries(false)));
|
ret.pushKV("new", AddrmanTableToJSON(addrman.GetEntries(false)));
|
||||||
ret.pushKV("tried", AddrmanTableToJSON(node.addrman->GetEntries(true)));
|
ret.pushKV("tried", AddrmanTableToJSON(addrman.GetEntries(true)));
|
||||||
return ret;
|
return ret;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -108,3 +108,16 @@ PeerManager& EnsurePeerman(const NodeContext& node)
|
||||||
}
|
}
|
||||||
return *node.peerman;
|
return *node.peerman;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AddrMan& EnsureAddrman(const NodeContext& node)
|
||||||
|
{
|
||||||
|
if (!node.addrman) {
|
||||||
|
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
|
||||||
|
}
|
||||||
|
return *node.addrman;
|
||||||
|
}
|
||||||
|
|
||||||
|
AddrMan& EnsureAnyAddrman(const std::any& context)
|
||||||
|
{
|
||||||
|
return EnsureAddrman(EnsureAnyNodeContext(context));
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include <any>
|
#include <any>
|
||||||
|
|
||||||
|
class AddrMan;
|
||||||
class ArgsManager;
|
class ArgsManager;
|
||||||
class CBlockPolicyEstimator;
|
class CBlockPolicyEstimator;
|
||||||
class CConnman;
|
class CConnman;
|
||||||
|
@ -31,5 +32,7 @@ CBlockPolicyEstimator& EnsureFeeEstimator(const node::NodeContext& node);
|
||||||
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context);
|
CBlockPolicyEstimator& EnsureAnyFeeEstimator(const std::any& context);
|
||||||
CConnman& EnsureConnman(const node::NodeContext& node);
|
CConnman& EnsureConnman(const node::NodeContext& node);
|
||||||
PeerManager& EnsurePeerman(const node::NodeContext& node);
|
PeerManager& EnsurePeerman(const node::NodeContext& node);
|
||||||
|
AddrMan& EnsureAddrman(const node::NodeContext& node);
|
||||||
|
AddrMan& EnsureAnyAddrman(const std::any& context);
|
||||||
|
|
||||||
#endif // BITCOIN_RPC_SERVER_UTIL_H
|
#endif // BITCOIN_RPC_SERVER_UTIL_H
|
||||||
|
|
Loading…
Add table
Reference in a new issue