mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-08 10:31:50 -05:00
Merge bitcoin/bitcoin#27069: net: add Ensure{any}Banman
2d955ff006
net: add `Ensure{any}Banman` (brunoerg) Pull request description: This PR adds `Ensure{any}Banman` functions to avoid code repetition and make it cleaner. Same approach as done with argsman, chainman, connman and others. ACKs for top commit: davidgumberg: ACK [2d955ff
](2d955ff006
) Tree-SHA512: 0beb7125312168a3df130c1793a1412ab423ef0f46023bfe2a121630c79df7e55d3d143fcf053bd09e2d96e9385a7a04594635da3e5c6be0c5d3a9cafbe3b631
This commit is contained in:
commit
e0d8378f2d
3 changed files with 26 additions and 17 deletions
|
@ -702,9 +702,7 @@ static RPCHelpMan setban()
|
||||||
throw std::runtime_error(help.ToString());
|
throw std::runtime_error(help.ToString());
|
||||||
}
|
}
|
||||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||||
if (!node.banman) {
|
BanMan& banman = EnsureBanman(node);
|
||||||
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
|
|
||||||
}
|
|
||||||
|
|
||||||
CSubNet subNet;
|
CSubNet subNet;
|
||||||
CNetAddr netAddr;
|
CNetAddr netAddr;
|
||||||
|
@ -726,7 +724,7 @@ static RPCHelpMan setban()
|
||||||
|
|
||||||
if (strCommand == "add")
|
if (strCommand == "add")
|
||||||
{
|
{
|
||||||
if (isSubnet ? node.banman->IsBanned(subNet) : node.banman->IsBanned(netAddr)) {
|
if (isSubnet ? banman.IsBanned(subNet) : banman.IsBanned(netAddr)) {
|
||||||
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP/Subnet already banned");
|
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP/Subnet already banned");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -741,12 +739,12 @@ static RPCHelpMan setban()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isSubnet) {
|
if (isSubnet) {
|
||||||
node.banman->Ban(subNet, banTime, absolute);
|
banman.Ban(subNet, banTime, absolute);
|
||||||
if (node.connman) {
|
if (node.connman) {
|
||||||
node.connman->DisconnectNode(subNet);
|
node.connman->DisconnectNode(subNet);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
node.banman->Ban(netAddr, banTime, absolute);
|
banman.Ban(netAddr, banTime, absolute);
|
||||||
if (node.connman) {
|
if (node.connman) {
|
||||||
node.connman->DisconnectNode(netAddr);
|
node.connman->DisconnectNode(netAddr);
|
||||||
}
|
}
|
||||||
|
@ -754,7 +752,7 @@ static RPCHelpMan setban()
|
||||||
}
|
}
|
||||||
else if(strCommand == "remove")
|
else if(strCommand == "remove")
|
||||||
{
|
{
|
||||||
if (!( isSubnet ? node.banman->Unban(subNet) : node.banman->Unban(netAddr) )) {
|
if (!( isSubnet ? banman.Unban(subNet) : banman.Unban(netAddr) )) {
|
||||||
throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Unban failed. Requested address/subnet was not previously manually banned.");
|
throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Unban failed. Requested address/subnet was not previously manually banned.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -785,13 +783,10 @@ static RPCHelpMan listbanned()
|
||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||||
{
|
{
|
||||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
BanMan& banman = EnsureAnyBanman(request.context);
|
||||||
if(!node.banman) {
|
|
||||||
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
|
|
||||||
}
|
|
||||||
|
|
||||||
banmap_t banMap;
|
banmap_t banMap;
|
||||||
node.banman->GetBanned(banMap);
|
banman.GetBanned(banMap);
|
||||||
const int64_t current_time{GetTime()};
|
const int64_t current_time{GetTime()};
|
||||||
|
|
||||||
UniValue bannedAddresses(UniValue::VARR);
|
UniValue bannedAddresses(UniValue::VARR);
|
||||||
|
@ -825,12 +820,9 @@ static RPCHelpMan clearbanned()
|
||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||||
{
|
{
|
||||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
BanMan& banman = EnsureAnyBanman(request.context);
|
||||||
if (!node.banman) {
|
|
||||||
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
|
|
||||||
}
|
|
||||||
|
|
||||||
node.banman->ClearBanned();
|
banman.ClearBanned();
|
||||||
|
|
||||||
return UniValue::VNULL;
|
return UniValue::VNULL;
|
||||||
},
|
},
|
||||||
|
|
|
@ -39,6 +39,20 @@ CTxMemPool& EnsureAnyMemPool(const std::any& context)
|
||||||
return EnsureMemPool(EnsureAnyNodeContext(context));
|
return EnsureMemPool(EnsureAnyNodeContext(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BanMan& EnsureBanman(const NodeContext& node)
|
||||||
|
{
|
||||||
|
if (!node.banman) {
|
||||||
|
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
|
||||||
|
}
|
||||||
|
return *node.banman;
|
||||||
|
}
|
||||||
|
|
||||||
|
BanMan& EnsureAnyBanman(const std::any& context)
|
||||||
|
{
|
||||||
|
return EnsureBanman(EnsureAnyNodeContext(context));
|
||||||
|
}
|
||||||
|
|
||||||
ArgsManager& EnsureArgsman(const NodeContext& node)
|
ArgsManager& EnsureArgsman(const NodeContext& node)
|
||||||
{
|
{
|
||||||
if (!node.args) {
|
if (!node.args) {
|
||||||
|
|
|
@ -13,6 +13,7 @@ class CConnman;
|
||||||
class CTxMemPool;
|
class CTxMemPool;
|
||||||
class ChainstateManager;
|
class ChainstateManager;
|
||||||
class PeerManager;
|
class PeerManager;
|
||||||
|
class BanMan;
|
||||||
namespace node {
|
namespace node {
|
||||||
struct NodeContext;
|
struct NodeContext;
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
@ -20,6 +21,8 @@ struct NodeContext;
|
||||||
node::NodeContext& EnsureAnyNodeContext(const std::any& context);
|
node::NodeContext& EnsureAnyNodeContext(const std::any& context);
|
||||||
CTxMemPool& EnsureMemPool(const node::NodeContext& node);
|
CTxMemPool& EnsureMemPool(const node::NodeContext& node);
|
||||||
CTxMemPool& EnsureAnyMemPool(const std::any& context);
|
CTxMemPool& EnsureAnyMemPool(const std::any& context);
|
||||||
|
BanMan& EnsureBanman(const node::NodeContext& node);
|
||||||
|
BanMan& EnsureAnyBanman(const std::any& context);
|
||||||
ArgsManager& EnsureArgsman(const node::NodeContext& node);
|
ArgsManager& EnsureArgsman(const node::NodeContext& node);
|
||||||
ArgsManager& EnsureAnyArgsman(const std::any& context);
|
ArgsManager& EnsureAnyArgsman(const std::any& context);
|
||||||
ChainstateManager& EnsureChainman(const node::NodeContext& node);
|
ChainstateManager& EnsureChainman(const node::NodeContext& node);
|
||||||
|
|
Loading…
Add table
Reference in a new issue