Skip to content

Commit e38993b

Browse files
jonlsluke-jr
authored andcommitted
RPC: Add "togglenetwork" method to toggle network activity temporarily
RPC command "togglenetwork" toggles network and returns new state after command. RPC command "getinfo" returns "networkactive" field in output.
1 parent 7c9a98a commit e38993b

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class CConnman
131131
bool Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError, Options options);
132132
void Stop();
133133
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
134+
bool GetNetworkActive() const { return fNetworkActive; };
134135
void SetNetworkActive(bool active);
135136
bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false, bool fFeeler = false);
136137
bool CheckIncomingNonce(uint64_t nonce);

src/rpc/misc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ UniValue getinfo(const UniValue& params, bool fHelp)
8989
#endif
9090
obj.push_back(Pair("blocks", (int)chainActive.Height()));
9191
obj.push_back(Pair("timeoffset", GetTimeOffset()));
92-
if(g_connman)
92+
if (g_connman) {
93+
obj.push_back(Pair("networkactive", g_connman->GetNetworkActive()));
9394
obj.push_back(Pair("connections", (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL)));
95+
}
9496
obj.push_back(Pair("proxy", (proxy.IsValid() ? proxy.proxy.ToStringIPPort() : string())));
9597
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
9698
obj.push_back(Pair("testnet", Params().TestnetToBeDeprecatedFieldRPC()));

src/rpc/net.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,24 @@ UniValue clearbanned(const UniValue& params, bool fHelp)
571571
return NullUniValue;
572572
}
573573

574+
UniValue togglenetwork(const JSONRPCRequest& request)
575+
{
576+
if (request.fHelp || request.params.size() != 0) {
577+
throw runtime_error(
578+
"togglenetwork\n"
579+
"Toggle all network activity temporarily."
580+
);
581+
}
582+
583+
if (!g_connman) {
584+
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
585+
}
586+
587+
g_connman->SetNetworkActive(!g_connman->GetNetworkActive());
588+
589+
return g_connman->GetNetworkActive();
590+
}
591+
574592
static const CRPCCommand commands[] =
575593
{ // category name actor (function) okSafeMode
576594
// --------------------- ------------------------ ----------------------- ----------
@@ -585,6 +603,7 @@ static const CRPCCommand commands[] =
585603
{ "network", "setban", &setban, true },
586604
{ "network", "listbanned", &listbanned, true },
587605
{ "network", "clearbanned", &clearbanned, true },
606+
{ "network", "togglenetwork", &togglenetwork, true, },
588607
};
589608

590609
void RegisterNetRPCCommands(CRPCTable &t)

0 commit comments

Comments
 (0)