From 339223e091822895fa563e62ea03786b1debf9a9 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Wed, 8 Mar 2023 18:44:32 +0800 Subject: [PATCH 1/5] Add sleep in batchvote rpc --- src/masternodes/rpc_proposals.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/masternodes/rpc_proposals.cpp b/src/masternodes/rpc_proposals.cpp index 30e61cf59f..8cccc6c34a 100644 --- a/src/masternodes/rpc_proposals.cpp +++ b/src/masternodes/rpc_proposals.cpp @@ -739,6 +739,11 @@ UniValue votegovbatch(const JSONRPCRequest &request) { execTestTx(CTransaction(rawTx), targetHeight, optAuthTx); ret.push_back(signsend(rawTx, pwallet, optAuthTx)->GetHash().GetHex()); + // Sleep the RPC worker thread a bit, so that the node can + // relay the TXs as it works through. Otherwise, the main + // chain can be locked for too long that prevent broadcasting of + // TXs + std::this_thread::sleep_for(std::chrono::seconds(1)); } return ret; From aeb267f2aa554b43d68b82cfc7db87796ac86bf2 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Wed, 15 Mar 2023 10:47:45 +0800 Subject: [PATCH 2/5] Reduce time and minor refactor --- src/masternodes/rpc_proposals.cpp | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/masternodes/rpc_proposals.cpp b/src/masternodes/rpc_proposals.cpp index 8cccc6c34a..86e8b64459 100644 --- a/src/masternodes/rpc_proposals.cpp +++ b/src/masternodes/rpc_proposals.cpp @@ -614,14 +614,14 @@ UniValue votegovbatch(const JSONRPCRequest &request) { int targetHeight; - struct MasternodeMultiVote { + struct VotingState { uint256 propId; uint256 mnId; CTxDestination dest; CProposalVoteType type; }; - std::vector mnMultiVotes; + std::vector voteList; { CCustomCSView view(*pcustomcsview); @@ -629,18 +629,23 @@ UniValue votegovbatch(const JSONRPCRequest &request) { const auto &votes{keys[i].get_array()}; if (votes.size() != 3) { - throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Incorrect number of items, three expected, proposal ID, masternode ID and vote expected. %d entries provided.", votes.size())); + throw JSONRPCError(RPC_INVALID_PARAMETER, + strprintf("Incorrect number of items, three expected, proposal ID, masternode ID and vote expected. %d entries provided.", + votes.size())); } const auto propId = ParseHashV(votes[0].get_str(), "proposalId"); const auto prop = view.GetProposal(propId); if (!prop) { - throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Proposal <%s> does not exist", propId.GetHex())); + throw JSONRPCError(RPC_INVALID_PARAMETER, + strprintf("Proposal <%s> does not exist", + propId.GetHex())); } if (prop->status != CProposalStatusType::Voting) { throw JSONRPCError(RPC_INVALID_PARAMETER, - strprintf("Proposal <%s> is not in voting period", propId.GetHex())); + strprintf("Proposal <%s> is not in voting period", + propId.GetHex())); } uint256 mnId; @@ -652,7 +657,7 @@ UniValue votegovbatch(const JSONRPCRequest &request) { const CTxDestination dest = DecodeDestination(id); if (!IsValidDestination(dest)) { throw JSONRPCError(RPC_INVALID_PARAMETER, - strprintf("The masternode id or address is not valid: %s", id)); + strprintf("The masternode id or address is not valid: %s", id)); } CKeyID ckeyId; if (dest.index() == PKHashType) { @@ -660,7 +665,8 @@ UniValue votegovbatch(const JSONRPCRequest &request) { } else if (dest.index() == WitV0KeyHashType) { ckeyId = CKeyID(std::get(dest)); } else { - throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s does not refer to a P2PKH or P2WPKH address", id)); + throw JSONRPCError(RPC_INVALID_PARAMETER, + strprintf("%s does not refer to a P2PKH or P2WPKH address", id)); } if (auto masterNodeIdByOwner = view.GetMasternodeIdByOwner(ckeyId)) { mnId = masterNodeIdByOwner.value(); @@ -672,7 +678,7 @@ UniValue votegovbatch(const JSONRPCRequest &request) { const auto node = view.GetMasternode(mnId); if (!node) { throw JSONRPCError(RPC_INVALID_PARAMETER, - strprintf("The masternode does not exist or the address doesn't own a masternode: %s", id)); + strprintf("The masternode does not exist or the address doesn't own a masternode: %s", id)); } auto vote = CProposalVoteType::VoteNeutral; @@ -688,10 +694,11 @@ UniValue votegovbatch(const JSONRPCRequest &request) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Decision supports yes or no. Neutral is currently disabled because of issue https://github.com/DeFiCh/ain/issues/1704"); } - mnMultiVotes.push_back({ + voteList.push_back({ propId, mnId, - node->ownerType == 1 ? CTxDestination(PKHash(node->ownerAuthAddress)) : CTxDestination(WitnessV0KeyHash(node->ownerAuthAddress)), + node->ownerType == 1 ? CTxDestination(PKHash(node->ownerAuthAddress)) : + CTxDestination(WitnessV0KeyHash(node->ownerAuthAddress)), vote }); } @@ -701,7 +708,7 @@ UniValue votegovbatch(const JSONRPCRequest &request) { UniValue ret(UniValue::VARR); - for (const auto& [propId, mnId, ownerDest, vote] : mnMultiVotes) { + for (const auto& [propId, mnId, ownerDest, vote] : voteList) { CProposalVoteMessage msg; msg.propId = propId; @@ -743,7 +750,7 @@ UniValue votegovbatch(const JSONRPCRequest &request) { // relay the TXs as it works through. Otherwise, the main // chain can be locked for too long that prevent broadcasting of // TXs - std::this_thread::sleep_for(std::chrono::seconds(1)); + std::this_thread::sleep_for(std::chrono::seconds(0.5)); } return ret; From a6b096919f3a530f1f665717f911b89bc37771fe Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Wed, 15 Mar 2023 14:24:36 +0800 Subject: [PATCH 3/5] Update rpc_proposals.cpp --- src/masternodes/rpc_proposals.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/masternodes/rpc_proposals.cpp b/src/masternodes/rpc_proposals.cpp index 86e8b64459..4338efe28c 100644 --- a/src/masternodes/rpc_proposals.cpp +++ b/src/masternodes/rpc_proposals.cpp @@ -750,7 +750,7 @@ UniValue votegovbatch(const JSONRPCRequest &request) { // relay the TXs as it works through. Otherwise, the main // chain can be locked for too long that prevent broadcasting of // TXs - std::this_thread::sleep_for(std::chrono::seconds(0.5)); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); } return ret; From 8056f39485bf3ed1608f54faf45b298b38b32218 Mon Sep 17 00:00:00 2001 From: cedric ogire Date: Fri, 17 Mar 2023 12:20:28 +0800 Subject: [PATCH 4/5] Add sleep time parameter for voteGovBatch --- src/masternodes/rpc_proposals.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/masternodes/rpc_proposals.cpp b/src/masternodes/rpc_proposals.cpp index ef1127c474..910afc1c45 100644 --- a/src/masternodes/rpc_proposals.cpp +++ b/src/masternodes/rpc_proposals.cpp @@ -4,6 +4,7 @@ #include const bool DEFAULT_RPC_GOV_NEUTRAL = false; +const int SLEEP_TIME_MILLIS = 500; struct VotingInfo { int32_t votesPossible = 0; @@ -610,6 +611,7 @@ UniValue votegovbatch(const JSONRPCRequest &request) { RPCTypeCheck(request.params, {UniValue::VARR}, false); const auto &keys = request.params[0].get_array(); + auto sleepTime = gArgs.GetBoolArg("-sleep-time", SLEEP_TIME_MILLIS); auto neutralVotesAllowed = gArgs.GetBoolArg("-rpc-governance-accept-neutral", DEFAULT_RPC_GOV_NEUTRAL); int targetHeight; @@ -750,7 +752,7 @@ UniValue votegovbatch(const JSONRPCRequest &request) { // relay the TXs as it works through. Otherwise, the main // chain can be locked for too long that prevent broadcasting of // TXs - std::this_thread::sleep_for(std::chrono::milliseconds(500)); + std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime)); } return ret; From 2c7bec5d899b954a642a5b4699e26227cfeb9832 Mon Sep 17 00:00:00 2001 From: cedric ogire Date: Fri, 17 Mar 2023 14:51:36 +0800 Subject: [PATCH 5/5] Add description for sleep-time parameter --- src/init.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/init.cpp b/src/init.cpp index b3c49ec789..3cc88ef6e3 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -637,6 +637,7 @@ void SetupServerArgs() gArgs.AddArg("-dftxworkers=", strprintf("No. of parallel workers associated with the DfTx related work pool. Stock splits, parallel processing of the chain where appropriate, etc use this worker pool (default: %d)", DEFAULT_DFTX_WORKERS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); gArgs.AddArg("-maxaddrratepersecond=", strprintf("Sets MAX_ADDR_RATE_PER_SECOND limit for ADDR messages(default: %f)", MAX_ADDR_RATE_PER_SECOND), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); gArgs.AddArg("-maxaddrprocessingtokenbucket=", strprintf("Sets MAX_ADDR_PROCESSING_TOKEN_BUCKET limit for ADDR messages(default: %d)", MAX_ADDR_PROCESSING_TOKEN_BUCKET), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); + gArgs.AddArg("-sleep-time=", "Sets sleeping time for voteGovBatch, by default the value is set to 500ms", ArgsManager::ALLOW_ANY, OptionsCategory::HIDDEN); #if HAVE_DECL_DAEMON gArgs.AddArg("-daemon", "Run in the background as a daemon and accept commands", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);