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 assert during certain cases of invalidateblock #866

Merged
merged 2 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 7 additions & 6 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ class CDBTransaction {
}
};

template<typename Parent, typename CommitTarget>
template<typename Parent, typename CommitTarget, typename CommitGuard>
class CScopedDBTransaction {
public:
typedef CDBTransaction<Parent, CommitTarget> Transaction;
Expand All @@ -736,31 +736,32 @@ class CScopedDBTransaction {
std::function<void ()> commitHandler;
std::function<void ()> rollbackHandler;
bool didCommitOrRollback{};
CommitGuard & commitGuard;

public:
CScopedDBTransaction(Transaction &dbTx) : dbTransaction(dbTx) {}
CScopedDBTransaction(Transaction &dbTx, CommitGuard & commitGuard) : dbTransaction(dbTx), commitGuard(commitGuard) {}
~CScopedDBTransaction() {
if (!didCommitOrRollback)
Rollback();
}
void Commit() {
assert(!didCommitOrRollback);
didCommitOrRollback = true;
dbTransaction.Commit();
commitGuard.CommitTransaction(dbTransaction);
if (commitHandler)
commitHandler();
}
void Rollback() {
assert(!didCommitOrRollback);
didCommitOrRollback = true;
dbTransaction.Clear();
commitGuard.ClearTransaction(dbTransaction);
if (rollbackHandler)
rollbackHandler();
}

static std::unique_ptr<CScopedDBTransaction<Parent, CommitTarget>> Begin(Transaction &dbTx) {
static std::unique_ptr<CScopedDBTransaction<Parent, CommitTarget, CommitGuard>> Begin(Transaction &dbTx, CommitGuard & commitGuard) {
assert(dbTx.IsClean());
return std::make_unique<CScopedDBTransaction<Parent, CommitTarget>>(dbTx);
return std::make_unique<CScopedDBTransaction<Parent, CommitTarget, CommitGuard>>(dbTx, commitGuard);
}

void SetCommitHandler(const std::function<void ()> &h) {
Expand Down
10 changes: 10 additions & 0 deletions src/evo/evodb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ void CEvoDB::WriteBestBlock(const uint256& hash)
{
Write(EVODB_BEST_BLOCK, hash);
}

void CEvoDB::CommitTransaction(CurTransaction & tx) {
LOCK(cs);
tx.Commit();
}

void CEvoDB::ClearTransaction(CurTransaction & tx) {
LOCK(cs);
tx.Clear();
}
7 changes: 5 additions & 2 deletions src/evo/evodb.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CEvoDB

typedef CDBTransaction<CDBWrapper, CDBBatch> RootTransaction;
typedef CDBTransaction<RootTransaction, RootTransaction> CurTransaction;
typedef CScopedDBTransaction<RootTransaction, RootTransaction> ScopedTransaction;
typedef CScopedDBTransaction<RootTransaction, RootTransaction, CEvoDB> ScopedTransaction;

CDBBatch rootBatch;
RootTransaction rootDBTransaction;
Expand All @@ -33,7 +33,7 @@ class CEvoDB
std::unique_ptr<ScopedTransaction> BeginTransaction()
{
LOCK(cs);
auto t = ScopedTransaction::Begin(curDBTransaction);
auto t = ScopedTransaction::Begin(curDBTransaction, *this);
return t;
}

Expand Down Expand Up @@ -84,6 +84,9 @@ class CEvoDB

bool VerifyBestBlock(const uint256& hash);
void WriteBestBlock(const uint256& hash);

void CommitTransaction(CurTransaction & tx);
void ClearTransaction(CurTransaction & tx);
};

extern CEvoDB* evoDb;
Expand Down
36 changes: 24 additions & 12 deletions src/sigma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,11 @@ bool CheckSigmaSpendTransaction(
// This list of public coins is required by function "Verify" of CoinSpend.
std::vector<sigma::PublicCoin> anonymity_set;
while(true) {
BOOST_FOREACH(const sigma::PublicCoin& pubCoinValue,
index->sigmaMintedPubCoins[denominationAndId]) {
anonymity_set.push_back(pubCoinValue);
if (index->sigmaMintedPubCoins.count(denominationAndId) > 0) {
BOOST_FOREACH(const sigma::PublicCoin& pubCoinValue,
index->sigmaMintedPubCoins[denominationAndId]) {
anonymity_set.push_back(pubCoinValue);
}
}
if (index == coinGroup.firstBlock)
break;
Expand Down Expand Up @@ -847,6 +849,9 @@ void CSigmaState::AddMintsToStateAndBlockIndex(
const sigma::CoinDenomination denomination = it.first;
const std::vector<sigma::PublicCoin>& mintsWithThisDenom = it.second;

if (mintsWithThisDenom.empty())
continue;

if (latestCoinIds[denomination] < 1)
latestCoinIds[denomination] = 1;
auto mintCoinGroupId = latestCoinIds[denomination];
Expand Down Expand Up @@ -894,14 +899,16 @@ void CSigmaState::AddBlock(CBlockIndex *index) {
BOOST_FOREACH(
const PAIRTYPE(PAIRTYPE(sigma::CoinDenomination, int), vector<sigma::PublicCoin>) &pubCoins,
index->sigmaMintedPubCoins) {
if (!pubCoins.second.empty()) {
SigmaCoinGroupInfo& coinGroup = coinGroups[pubCoins.first];

if (coinGroup.firstBlock == NULL)
coinGroup.firstBlock = index;
coinGroup.lastBlock = index;
coinGroup.nCoins += pubCoins.second.size();
}
if (pubCoins.second.empty())
continue;

SigmaCoinGroupInfo& coinGroup = coinGroups[pubCoins.first];

if (coinGroup.firstBlock == NULL)
coinGroup.firstBlock = index;
coinGroup.lastBlock = index;
coinGroup.nCoins += pubCoins.second.size();

latestCoinIds[pubCoins.first.first] = pubCoins.first.second;
BOOST_FOREACH(const sigma::PublicCoin &coin, pubCoins.second) {
Expand All @@ -923,6 +930,9 @@ void CSigmaState::RemoveBlock(CBlockIndex *index) {
SigmaCoinGroupInfo &coinGroup = coinGroups[coin.first];
int nMintsToForget = coin.second.size();

if (nMintsToForget == 0)
continue;

assert(coinGroup.nCoins >= nMintsToForget);

if ((coinGroup.nCoins -= nMintsToForget) == 0) {
Expand All @@ -941,7 +951,8 @@ void CSigmaState::RemoveBlock(CBlockIndex *index) {
do {
assert(coinGroup.lastBlock != coinGroup.firstBlock);
coinGroup.lastBlock = coinGroup.lastBlock->pprev;
} while (coinGroup.lastBlock->sigmaMintedPubCoins.count(coin.first) == 0);
} while (coinGroup.lastBlock->sigmaMintedPubCoins.count(coin.first) == 0 ||
coinGroup.lastBlock->sigmaMintedPubCoins[coin.first].size() == 0);
}
}

Expand Down Expand Up @@ -1030,7 +1041,8 @@ int CSigmaState::GetCoinSetForSpend(
for (CBlockIndex *block = coinGroup.lastBlock;
;
block = block->pprev) {
if (block->sigmaMintedPubCoins[denomAndId].size() > 0) {
if (block->sigmaMintedPubCoins.count(denomAndId) > 0 &&
block->sigmaMintedPubCoins[denomAndId].size() > 0) {
if (block->nHeight <= maxHeight) {
if (numberOfCoins == 0) {
// latest block satisfying given conditions
Expand Down