Skip to content

Commit

Permalink
Fixed broken spark state (#1307)
Browse files Browse the repository at this point in the history
* Fixed broken spark state when a block with more than one spark spend fails lTag check

* Stop CSparkState::AddSpend from throwing an exception

* Adding cover set  existance check

* Failing test fixed

---------

Co-authored-by: levonpetrosyan93 <petrosyan.levon93@gmail.com>
  • Loading branch information
psolstice and levonpetrosyan93 authored Aug 20, 2023
1 parent a06df90 commit 3a77e84
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
26 changes: 16 additions & 10 deletions src/spark/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,17 @@ bool ConnectBlockSpark(
)) {
return false;
}
}

if (!fJustCheck) {
if (!fJustCheck) {
BOOST_FOREACH(auto& lTag, pblock->sparkTxInfo->spentLTags) {
pindexNew->spentLTags.insert(lTag);
sparkState.AddSpend(lTag.first, lTag.second);
}
}

if (fJustCheck)
else {
return true;
}

const auto& params = ::Params().GetConsensus();
CHash256 hash;
Expand Down Expand Up @@ -633,9 +635,16 @@ bool CheckSparkSpendTransaction(
spend->setCoverSets(cover_set_data);
spend->setVout(Vout);

const std::vector<uint64_t>& ids = spend->getCoinGroupIds();
for (const auto& id : ids) {
if (!cover_sets.count(id) || !cover_set_data.count(id))
return state.DoS(100,
error("CheckSparkSpendTransaction: No cover set found."));
}

BatchProofContainer* batchProofContainer = BatchProofContainer::get_instance();
bool useBatching = batchProofContainer->fCollectProofs && !isVerifyDB && !isCheckWallet && sparkTxInfo && !sparkTxInfo->fInfoIsComplete;

// if we are collecting proofs, skip verification and collect proofs
// add proofs into container
if (useBatching) {
Expand All @@ -651,7 +660,6 @@ bool CheckSparkSpendTransaction(

if (passVerify) {
const std::vector<GroupElement>& lTags = spend->getUsedLTags();
const std::vector<uint64_t>& ids = spend->getCoinGroupIds();

if (lTags.size() != ids.size()) {
return state.DoS(100,
Expand Down Expand Up @@ -1003,12 +1011,10 @@ void CSparkState::AddMintsToStateAndBlockIndex(
}

void CSparkState::AddSpend(const GroupElement& lTag, int coinGroupId) {
if (!mintMetaInfo.count(coinGroupId)) {
throw std::invalid_argument("group id doesn't exist");
if (mintMetaInfo.count(coinGroupId) > 0) {
usedLTags[lTag] = coinGroupId;
spendMetaInfo[coinGroupId] += 1;
}

usedLTags[lTag] = coinGroupId;
spendMetaInfo[coinGroupId] += 1;
}

void CSparkState::RemoveSpend(const GroupElement& lTag) {
Expand Down
3 changes: 0 additions & 3 deletions src/test/spark_state_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,6 @@ BOOST_AUTO_TEST_CASE(lTag_adding)
BOOST_CHECK(!sparkState->IsUsedLTag(lTag2));
BOOST_CHECK(!sparkState->IsUsedLTagHash(receivedLTag, lTagHash2));

// add lTags to group that doesn't exist, should fail
BOOST_CHECK_THROW(sparkState->AddSpend(GroupElement(), 100), std::invalid_argument);

sparkState->Reset();
mempool.clear();
}
Expand Down

0 comments on commit 3a77e84

Please sign in to comment.