Skip to content

Commit 2d955ff

Browse files
committed
net: add Ensure{any}Banman
it adds `Ensure{any}Banman` functions to avoid code repetition and make it cleaner. Similar approach as done with argsman, chainman, connman and others.
1 parent fe86616 commit 2d955ff

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

src/rpc/net.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,7 @@ static RPCHelpMan setban()
702702
throw std::runtime_error(help.ToString());
703703
}
704704
NodeContext& node = EnsureAnyNodeContext(request.context);
705-
if (!node.banman) {
706-
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
707-
}
705+
BanMan& banman = EnsureBanman(node);
708706

709707
CSubNet subNet;
710708
CNetAddr netAddr;
@@ -726,7 +724,7 @@ static RPCHelpMan setban()
726724

727725
if (strCommand == "add")
728726
{
729-
if (isSubnet ? node.banman->IsBanned(subNet) : node.banman->IsBanned(netAddr)) {
727+
if (isSubnet ? banman.IsBanned(subNet) : banman.IsBanned(netAddr)) {
730728
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP/Subnet already banned");
731729
}
732730

@@ -741,20 +739,20 @@ static RPCHelpMan setban()
741739
}
742740

743741
if (isSubnet) {
744-
node.banman->Ban(subNet, banTime, absolute);
742+
banman.Ban(subNet, banTime, absolute);
745743
if (node.connman) {
746744
node.connman->DisconnectNode(subNet);
747745
}
748746
} else {
749-
node.banman->Ban(netAddr, banTime, absolute);
747+
banman.Ban(netAddr, banTime, absolute);
750748
if (node.connman) {
751749
node.connman->DisconnectNode(netAddr);
752750
}
753751
}
754752
}
755753
else if(strCommand == "remove")
756754
{
757-
if (!( isSubnet ? node.banman->Unban(subNet) : node.banman->Unban(netAddr) )) {
755+
if (!( isSubnet ? banman.Unban(subNet) : banman.Unban(netAddr) )) {
758756
throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Unban failed. Requested address/subnet was not previously manually banned.");
759757
}
760758
}
@@ -785,13 +783,10 @@ static RPCHelpMan listbanned()
785783
},
786784
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
787785
{
788-
NodeContext& node = EnsureAnyNodeContext(request.context);
789-
if(!node.banman) {
790-
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
791-
}
786+
BanMan& banman = EnsureAnyBanman(request.context);
792787

793788
banmap_t banMap;
794-
node.banman->GetBanned(banMap);
789+
banman.GetBanned(banMap);
795790
const int64_t current_time{GetTime()};
796791

797792
UniValue bannedAddresses(UniValue::VARR);
@@ -825,12 +820,9 @@ static RPCHelpMan clearbanned()
825820
},
826821
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
827822
{
828-
NodeContext& node = EnsureAnyNodeContext(request.context);
829-
if (!node.banman) {
830-
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
831-
}
823+
BanMan& banman = EnsureAnyBanman(request.context);
832824

833-
node.banman->ClearBanned();
825+
banman.ClearBanned();
834826

835827
return UniValue::VNULL;
836828
},

src/rpc/server_util.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ CTxMemPool& EnsureAnyMemPool(const std::any& context)
3939
return EnsureMemPool(EnsureAnyNodeContext(context));
4040
}
4141

42+
43+
BanMan& EnsureBanman(const NodeContext& node)
44+
{
45+
if (!node.banman) {
46+
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
47+
}
48+
return *node.banman;
49+
}
50+
51+
BanMan& EnsureAnyBanman(const std::any& context)
52+
{
53+
return EnsureBanman(EnsureAnyNodeContext(context));
54+
}
55+
4256
ArgsManager& EnsureArgsman(const NodeContext& node)
4357
{
4458
if (!node.args) {

src/rpc/server_util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ class CConnman;
1313
class CTxMemPool;
1414
class ChainstateManager;
1515
class PeerManager;
16+
class BanMan;
1617
namespace node {
1718
struct NodeContext;
1819
} // namespace node
1920

2021
node::NodeContext& EnsureAnyNodeContext(const std::any& context);
2122
CTxMemPool& EnsureMemPool(const node::NodeContext& node);
2223
CTxMemPool& EnsureAnyMemPool(const std::any& context);
24+
BanMan& EnsureBanman(const node::NodeContext& node);
25+
BanMan& EnsureAnyBanman(const std::any& context);
2326
ArgsManager& EnsureArgsman(const node::NodeContext& node);
2427
ArgsManager& EnsureAnyArgsman(const std::any& context);
2528
ChainstateManager& EnsureChainman(const node::NodeContext& node);

0 commit comments

Comments
 (0)