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

Fix for multi-threaded getburninfo #1775

Merged
merged 9 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
24 changes: 16 additions & 8 deletions src/masternodes/balances.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ struct CBalances {
return Res::Ok();
}
auto current = CTokenAmount{amount.nTokenId, balances[amount.nTokenId]};
Require(current.Add(amount.nValue));
if (auto res = current.Add(amount.nValue); !res) {
return res;
}
if (current.nValue == 0) {
balances.erase(amount.nTokenId);
} else {
Expand All @@ -32,7 +34,9 @@ struct CBalances {
return Res::Ok();
}
auto current = CTokenAmount{amount.nTokenId, balances[amount.nTokenId]};
Require(current.Sub(amount.nValue));
if (auto res = current.Sub(amount.nValue); !res) {
return res;
}

if (current.nValue == 0) {
balances.erase(amount.nTokenId);
Expand All @@ -57,9 +61,11 @@ struct CBalances {
}

Res SubBalances(const TAmounts &other) {
for (const auto &[tokenId, amount] : other)
Require(Sub(CTokenAmount{tokenId, amount}));

for (const auto &[tokenId, amount] : other) {
if (auto res = Sub(CTokenAmount{tokenId, amount}); !res) {
return res;
}
}
return Res::Ok();
}

Expand All @@ -75,9 +81,11 @@ struct CBalances {
}

Res AddBalances(const TAmounts &other) {
for (const auto &[tokenId, amount] : other)
Require(Add(CTokenAmount{tokenId, amount}));

for (const auto &[tokenId, amount] : other) {
if (auto res = Add(CTokenAmount{tokenId, amount}); !res) {
return res;
}
}
return Res::Ok();
}

Expand Down
41 changes: 28 additions & 13 deletions src/masternodes/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2028,27 +2028,39 @@ UniValue getburninfo(const JSONRPCRequest& request) {
}
}

const auto nWorkers = DfTxTaskPool->GetAvailableThreads();
auto nWorkers = DfTxTaskPool->GetAvailableThreads();
if (height < nWorkers) {
nWorkers = height;
}

const auto chunks = height / nWorkers;
const auto chunksRemainder = height % nWorkers;

TaskGroup g;
std::vector<std::shared_ptr<BalanceResults>> workerResults;

for (size_t i{}; i < nWorkers; ++i) {
uint32_t startHeight = (i + 1) * chunks;
uint32_t stopHeight = startHeight - chunks;

if (i + 1 == nWorkers) {
startHeight += chunksRemainder;
stopHeight -= chunksRemainder;
}

std::vector<std::shared_ptr<BalanceResults>> workerResults;
// Note this creates a massive amount of chunks as we go in mem.
// But this is fine for now. Most optimal impl is to return the future val
// and add it on receive. It requires a bit more changes, but for now
// this should do.
// However reserve in one-go to prevent numerous reallocations
workerResults.reserve(chunks);

for (auto i = 0; i <= chunks; i++) {
auto result = std::make_shared<BalanceResults>();
workerResults.push_back(result);
}

auto &pool = DfTxTaskPool->pool;
auto processedHeight = 0;
auto i = 0;
while (processedHeight < height)
{
auto startHeight = (chunks * (i + 1));
auto stopHeight = (chunks * (i));
auto result = workerResults[i];

g.AddTask();
auto &pool = DfTxTaskPool->pool;
boost::asio::post(pool, [result, startHeight, stopHeight, &g] {
pburnHistoryDB->ForEachAccountHistory([result, stopHeight](const AccountHistoryKey &key, const AccountHistoryValue &value) {

Expand Down Expand Up @@ -2122,6 +2134,10 @@ UniValue getburninfo(const JSONRPCRequest& request) {
}, {}, startHeight, std::numeric_limits<uint32_t>::max());
g.RemoveTask();
});

// perfect accuracy: processedHeight += (startHeight > height) ? chunksRemainder : chunks;
processedHeight += chunks;
i++;
}

g.WaitForCompletion();
Expand Down Expand Up @@ -2173,7 +2189,6 @@ UniValue getburninfo(const JSONRPCRequest& request) {
.Set(request, result);
}


UniValue HandleSendDFIP2201DFIInput(const JSONRPCRequest& request, CWalletCoinsUnlocker pwallet,
const std::pair<std::string, CScript>& contractPair, CTokenAmount amount) {
CUtxosToAccountMessage msg{};
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/threadpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TaskPool {
public:
explicit TaskPool(size_t size);
void Shutdown();
[[nodiscard]] size_t GetAvailableThreads() const { return size; }
[[nodiscard]] size_t GetAvailableThreads() { return size; }
boost::asio::thread_pool pool;

private:
Expand Down