Skip to content

Commit

Permalink
Optimize getting sector
Browse files Browse the repository at this point in the history
  • Loading branch information
levonpetrosyan93 committed Dec 17, 2024
1 parent b50f94b commit 600da33
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
13 changes: 3 additions & 10 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<unsigned char> vch(serializedCoin.begin(), serializedCoin.end());
Expand All @@ -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));
Expand Down
14 changes: 12 additions & 2 deletions src/spark/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,8 @@ void CSparkState::GetCoinsForRecovery(
CChain *chain,
int maxHeight,
int coinGroupID,
int startIndex,
int endIndex,
uint256& blockHash,
std::vector<std::pair<spark::Coin, std::pair<uint256, std::vector<unsigned char>>>>& coins) {
coins.clear();
Expand All @@ -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) {
Expand All @@ -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<uint256, std::vector<unsigned char>> 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 ;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/spark/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ class CSparkState {
CChain *chain,
int maxHeight,
int coinGroupID,
int startIndex,
int endIndex,
uint256& blockHash,
std::vector<std::pair<spark::Coin, std::pair<uint256, std::vector<unsigned char>>>>& coins);

Expand Down

0 comments on commit 600da33

Please sign in to comment.