From 600da33369de52812c83fe574d4470ba3aaa5114 Mon Sep 17 00:00:00 2001 From: levonpetrosyan93 Date: Tue, 17 Dec 2024 18:04:34 +0400 Subject: [PATCH] Optimize getting sector --- src/rpc/misc.cpp | 13 +++---------- src/spark/state.cpp | 14 ++++++++++++-- src/spark/state.h | 2 ++ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index fc1f518b06..c7c58adb2d 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -1418,6 +1418,8 @@ UniValue getsparkanonymitysetsector(const JSONRPCRequest& request) &chainActive, chainActive.Height() - (ZC_MINT_CONFIRMATIONS - 1), coinGroupId, + startIndex, + endIndex, blockHash, coins); } catch (std::exception & e) { @@ -1428,15 +1430,8 @@ UniValue getsparkanonymitysetsector(const JSONRPCRequest& request) UniValue ret(UniValue::VOBJ); UniValue mints(UniValue::VARR); - std::size_t counter = 0; + for (const auto& coin : coins) { - if (counter < startIndex) { - ++counter; - continue; - } - if (counter >= endIndex) { - break; - } CDataStream serializedCoin(SER_NETWORK, PROTOCOL_VERSION); serializedCoin << coin; std::vector vch(serializedCoin.begin(), serializedCoin.end()); @@ -1449,8 +1444,6 @@ UniValue getsparkanonymitysetsector(const JSONRPCRequest& request) UniValue entity(UniValue::VARR); entity.push_backV(data); mints.push_back(entity); - - ++counter; } ret.push_back(Pair("coins", mints)); diff --git a/src/spark/state.cpp b/src/spark/state.cpp index d80606fe20..dcfb63bbcc 100644 --- a/src/spark/state.cpp +++ b/src/spark/state.cpp @@ -1369,6 +1369,8 @@ void CSparkState::GetCoinsForRecovery( CChain *chain, int maxHeight, int coinGroupID, + int startIndex, + int endIndex, uint256& blockHash, std::vector>>>& coins) { coins.clear(); @@ -1384,7 +1386,7 @@ void CSparkState::GetCoinsForRecovery( if (index == coinGroup.firstBlock && coinGroup.firstBlock != coinGroup.lastBlock) throw std::runtime_error(std::string("Incorrect blockHash provided: " + blockHash.GetHex())); - + std::size_t counter = 0; for (CBlockIndex *block = index;; block = block->pprev) { // ignore block heigher than max height if (block->nHeight > maxHeight) { @@ -1401,14 +1403,22 @@ void CSparkState::GetCoinsForRecovery( if (id) { if (block->sparkMintedCoins.count(id) > 0) { for (const auto &coin : block->sparkMintedCoins[id]) { + if (counter < startIndex) { + ++counter; + continue; + } + if (counter >= endIndex) { + break; + } std::pair> txHashContext; if (block->sparkTxHashContext.count(coin.S)) txHashContext = block->sparkTxHashContext[coin.S]; coins.push_back({coin, txHashContext}); + ++counter; } } } - if (block == coinGroup.firstBlock) { + if (block == coinGroup.firstBlock || counter >= endIndex) { break ; } } diff --git a/src/spark/state.h b/src/spark/state.h index 03396a7f4b..13f72cc451 100644 --- a/src/spark/state.h +++ b/src/spark/state.h @@ -225,6 +225,8 @@ class CSparkState { CChain *chain, int maxHeight, int coinGroupID, + int startIndex, + int endIndex, uint256& blockHash, std::vector>>>& coins);