Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc): introduce and use setmnthreadactive (regtest-only) #6286

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3486,8 +3486,7 @@ void CConnman::ThreadOpenMasternodeConnections(CDeterministicMNManager& dmnman,

didConnect = false;

if (!fNetworkActive || !mn_sync.IsBlockchainSynced())
continue;
if (!fNetworkActive || !m_masternode_thread_active || !mn_sync.IsBlockchainSynced()) continue;

std::set<CService> connectedNodes;
std::map<uint256 /*proTxHash*/, bool /*fInbound*/> connectedProRegTxHashes;
Expand Down
3 changes: 3 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,8 @@ friend class CNode;
bool GetNetworkActive() const { return fNetworkActive; };
bool GetUseAddrmanOutgoing() const { return m_use_addrman_outgoing; };
void SetNetworkActive(bool active, CMasternodeSync* const mn_sync);
bool GetMasternodeThreadActive() const { return m_masternode_thread_active; };
void SetMasternodeThreadActive(bool active) { m_masternode_thread_active = active; };
SocketEventsMode GetSocketEventsMode() const { return socketEventsMode; }

enum class MasternodeConn {
Expand Down Expand Up @@ -1721,6 +1723,7 @@ friend class CNode;

std::vector<ListenSocket> vhListenSocket;
std::atomic<bool> fNetworkActive{true};
std::atomic<bool> m_masternode_thread_active{true};
bool fAddressesInitialized{false};
AddrMan& addrman;
const NetGroupManager& m_netgroupman;
Expand Down
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "prioritisetransaction", 1, "fee_delta" },
{ "setban", 2, "bantime" },
{ "setban", 3, "absolute" },
{ "setmnthreadactive", 0, "state" },
{ "setnetworkactive", 0, "state" },
{ "setcoinjoinrounds", 0, "rounds" },
{ "setcoinjoinamount", 0, "amount" },
Expand Down
27 changes: 27 additions & 0 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,32 @@ static RPCHelpMan addpeeraddress()
};
}

static RPCHelpMan setmnthreadactive()
{
return RPCHelpMan{"setmnthreadactive",
"\nDisable/enable automatic masternode connections thread activity.\n",
{
{"state", RPCArg::Type::BOOL, RPCArg::Optional::NO, "true to enable the thread, false to disable"},
},
RPCResult{RPCResult::Type::BOOL, "", "The value that was passed in"},
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{

if (Params().NetworkIDString() != CBaseChainParams::REGTEST) {
throw std::runtime_error("setmnthreadactive is for regression testing (-regtest mode) only.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not it be JSONRPCError?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For whatever reason all such messages are thrown this way 🤷‍♂️
https://github.com/search?q=repo%3Abitcoin%2Fbitcoin+%22%28-regtest+mode%29%22&type=code

}

const NodeContext& node = EnsureAnyNodeContext(request.context);
CConnman& connman = EnsureConnman(node);

connman.SetMasternodeThreadActive(request.params[0].get_bool());

return connman.GetMasternodeThreadActive();
},
};
}

void RegisterNetRPCCommands(CRPCTable &t)
{
// clang-format off
Expand All @@ -1042,6 +1068,7 @@ static const CRPCCommand commands[] =

{ "hidden", &addconnection, },
{ "hidden", &addpeeraddress, },
{ "hidden", &setmnthreadactive },
};
// clang-format on
for (const auto& c : commands) {
Expand Down
1 change: 1 addition & 0 deletions src/test/fuzz/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ const std::vector<std::string> RPC_COMMANDS_SAFE_FOR_FUZZING{
"reconsiderblock",
"scantxoutset",
"sendrawtransaction",
"setmnthreadactive",
"setmocktime",
"setnetworkactive",
"signmessagewithprivkey",
Expand Down
24 changes: 15 additions & 9 deletions test/functional/test_framework/test_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,8 @@ def __init__(self, proTxHash, ownerAddr, votingAddr, rewards_address, operator_r
self.collateral_vout = collateral_vout
self.addr = addr
self.evo = evo
self.node = None
self.nodeIdx = None


class DashTestFramework(BitcoinTestFramework):
Expand All @@ -1097,6 +1099,15 @@ def run_test(self):
"""Tests must override this method to define test logic"""
raise NotImplementedError

def connect_nodes(self, a, b):
for mn2 in self.mninfo:
if mn2.node is not None:
mn2.node.setmnthreadactive(False)
super().connect_nodes(a, b)
for mn2 in self.mninfo:
if mn2.node is not None:
mn2.node.setmnthreadactive(True)

def set_dash_test_params(self, num_nodes, masterodes_count, extra_args=None, fast_dip3_enforcement=False, evo_count=0):
self.mn_count = masterodes_count
self.evo_count = evo_count
Expand Down Expand Up @@ -1435,17 +1446,12 @@ def do_connect(idx):
job.result()
jobs.clear()

# connect nodes in parallel
for idx in range(0, self.mn_count):
jobs.append(executor.submit(do_connect, idx))

# wait for all nodes to connect
for job in jobs:
job.result()
jobs.clear()

executor.shutdown()

# connect nodes
for idx in range(0, self.mn_count):
do_connect(idx)

def start_masternode(self, mninfo, extra_args=None):
args = ['-masternodeblsprivkey=%s' % mninfo.keyOperator] + self.extra_args[mninfo.nodeIdx]
if extra_args is not None:
Expand Down
Loading