Skip to content

Commit 1c0a3d0

Browse files
Merge bitcoin#27069: net: add Ensure{any}Banman
* Merge bitcoin#27069: net: add `Ensure{any}Banman` 2d955ff 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](bitcoin@2d955ff) Tree-SHA512: 0beb7125312168a3df130c1793a1412ab423ef0f46023bfe2a121630c79df7e55d3d143fcf053bd09e2d96e9385a7a04594635da3e5c6be0c5d3a9cafbe3b631 * Apply suggestions from code review --------- Co-authored-by: merge-script <[email protected]>
1 parent e06a8b6 commit 1c0a3d0

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

src/rpc/net.cpp

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,7 @@ static RPCHelpMan setban()
741741
throw std::runtime_error(help.ToString());
742742
}
743743
const NodeContext& node = EnsureAnyNodeContext(request.context);
744-
if (!node.banman) {
745-
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
746-
}
744+
BanMan& banman = EnsureBanman(node);
747745

748746
CSubNet subNet;
749747
CNetAddr netAddr;
@@ -766,7 +764,7 @@ static RPCHelpMan setban()
766764

767765
if (strCommand == "add")
768766
{
769-
if (isSubnet ? node.banman->IsBanned(subNet) : node.banman->IsBanned(netAddr)) {
767+
if (isSubnet ? banman.IsBanned(subNet) : banman.IsBanned(netAddr)) {
770768
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP/Subnet already banned");
771769
}
772770

@@ -779,20 +777,20 @@ static RPCHelpMan setban()
779777
absolute = true;
780778

781779
if (isSubnet) {
782-
node.banman->Ban(subNet, banTime, absolute);
780+
banman.Ban(subNet, banTime, absolute);
783781
if (node.connman) {
784782
node.connman->DisconnectNode(subNet);
785783
}
786784
} else {
787-
node.banman->Ban(netAddr, banTime, absolute);
785+
banman.Ban(netAddr, banTime, absolute);
788786
if (node.connman) {
789787
node.connman->DisconnectNode(netAddr);
790788
}
791789
}
792790
}
793791
else if(strCommand == "remove")
794792
{
795-
if (!( isSubnet ? node.banman->Unban(subNet) : node.banman->Unban(netAddr) )) {
793+
if (!( isSubnet ? banman.Unban(subNet) : banman.Unban(netAddr) )) {
796794
throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Unban failed. Requested address/subnet was not previously manually banned.");
797795
}
798796
}
@@ -823,14 +821,10 @@ static RPCHelpMan listbanned()
823821
},
824822
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
825823
{
826-
827-
const NodeContext& node = EnsureAnyNodeContext(request.context);
828-
if(!node.banman) {
829-
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
830-
}
824+
BanMan& banman = EnsureAnyBanman(request.context);
831825

832826
banmap_t banMap;
833-
node.banman->GetBanned(banMap);
827+
banman.GetBanned(banMap);
834828
const int64_t current_time{GetTime()};
835829

836830
UniValue bannedAddresses(UniValue::VARR);
@@ -864,12 +858,9 @@ static RPCHelpMan clearbanned()
864858
},
865859
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
866860
{
867-
const NodeContext& node = EnsureAnyNodeContext(request.context);
868-
if (!node.banman) {
869-
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
870-
}
861+
BanMan& banman = EnsureAnyBanman(request.context);
871862

872-
node.banman->ClearBanned();
863+
banman.ClearBanned();
873864

874865
return NullUniValue;
875866
},
@@ -888,12 +879,9 @@ static RPCHelpMan cleardiscouraged()
888879
},
889880
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
890881
{
891-
const NodeContext& node = EnsureAnyNodeContext(request.context);
892-
if (!node.banman) {
893-
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
894-
}
882+
BanMan& banman = EnsureAnyBanman(request.context);
895883

896-
node.banman->ClearDiscouraged();
884+
banman.ClearDiscouraged();
897885

898886
return NullUniValue;
899887
},

src/rpc/server_util.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ CTxMemPool& EnsureAnyMemPool(const CoreContext& context)
5656
return EnsureMemPool(EnsureAnyNodeContext(context));
5757
}
5858

59+
60+
BanMan& EnsureBanman(const NodeContext& node)
61+
{
62+
if (!node.banman) {
63+
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
64+
}
65+
return *node.banman;
66+
}
67+
68+
BanMan& EnsureAnyBanman(const CoreContext& context)
69+
{
70+
return EnsureBanman(EnsureAnyNodeContext(context));
71+
}
72+
5973
ArgsManager& EnsureArgsman(const NodeContext& node)
6074
{
6175
if (!node.args) {

src/rpc/server_util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class CConnman;
1313
class CTxMemPool;
1414
class ChainstateManager;
1515
class PeerManager;
16+
class BanMan;
1617
struct LLMQContext;
1718
namespace node {
1819
struct NodeContext;
@@ -21,6 +22,8 @@ struct NodeContext;
2122
node::NodeContext& EnsureAnyNodeContext(const CoreContext& context);
2223
CTxMemPool& EnsureMemPool(const node::NodeContext& node);
2324
CTxMemPool& EnsureAnyMemPool(const CoreContext& context);
25+
BanMan& EnsureBanman(const node::NodeContext& node);
26+
BanMan& EnsureAnyBanman(const CoreContext& context);
2427
ArgsManager& EnsureArgsman(const node::NodeContext& node);
2528
ArgsManager& EnsureAnyArgsman(const CoreContext& context);
2629
ChainstateManager& EnsureChainman(const node::NodeContext& node);

0 commit comments

Comments
 (0)