Skip to content

Commit

Permalink
Do not delete and recalculate accumulators on initial block verificat… (
Browse files Browse the repository at this point in the history
PIVX-Project#46)

* Do not delete and recalculate accumulators on initial block verification.

* Close unclosed statement.
  • Loading branch information
presstab authored and Fuzzbawls committed Oct 4, 2017
1 parent c163d80 commit 4386edd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,17 +1317,21 @@ bool AppInit2(boost::thread_group& threadGroup)
CAccumulators::getInstance().ClearAccCheckpointsNoDB();

uiInterface.InitMessage(_("Verifying blocks..."));
fVerifyingBlocks = true;
if (!CVerifyDB().VerifyDB(pcoinsdbview, GetArg("-checklevel", 4), // Zerocoin must check at level 4
GetArg("-checkblocks", 500))) {
strLoadError = _("Corrupted block database detected");
fVerifyingBlocks = false;
break;
}
} catch (std::exception& e) {
if (fDebug) LogPrintf("%s\n", e.what());
strLoadError = _("Error opening block database");
fVerifyingBlocks = false;
break;
}

fVerifyingBlocks = false;
fLoaded = true;
} while (false);

Expand Down
24 changes: 14 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ bool fReindex = false;
bool fTxIndex = true;
bool fIsBareMultisigStd = true;
bool fCheckBlockIndex = false;
bool fVerifyingBlocks = false;
unsigned int nCoinCacheSize = 5000;
bool fAlerts = DEFAULT_ALERTS;

Expand Down Expand Up @@ -2625,14 +2626,15 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex
if (tx.ContainsZerocoins()) {
if (tx.IsZerocoinSpend()) {
//erase all zerocoinspends in this transaction
for (const CTxIn txin : tx.vin){
for (const CTxIn txin : tx.vin) {
if (txin.scriptSig.IsZerocoinSpend()) {
CoinSpend spend = TxInToZerocoinSpend(txin);
if(!CAccumulators::getInstance().EraseCoinSpend(spend.getCoinSerialNumber()))
if (!CAccumulators::getInstance().EraseCoinSpend(spend.getCoinSerialNumber()))
return error("failed to erase spent zerocoin in block");
}
}
} else if (tx.IsZerocoinMint()) {
}
if (tx.IsZerocoinMint()) {
//erase all zerocoinmints in this transaction
for (const CTxOut txout : tx.vout) {
if (txout.scriptPubKey.empty() || !txout.scriptPubKey.IsZerocoinMint())
Expand Down Expand Up @@ -2705,11 +2707,13 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex
// move best block pointer to prevout block
view.SetBestBlock(pindex->pprev->GetBlockHash());

//if block is an accumulator checkpoint block, remove checkpoint and checksums from db
uint256 nCheckpoint = pindex->nAccumulatorCheckpoint;
if(nCheckpoint != pindex->pprev->nAccumulatorCheckpoint) {
if(!CAccumulators::getInstance().EraseAccumulatorValues(nCheckpoint, pindex->pprev->nAccumulatorCheckpoint))
return error("DisconnectBlock(): failed to erase checkpoint");
if (!fVerifyingBlocks) {
//if block is an accumulator checkpoint block, remove checkpoint and checksums from db
uint256 nCheckpoint = pindex->nAccumulatorCheckpoint;
if(nCheckpoint != pindex->pprev->nAccumulatorCheckpoint) {
if(!CAccumulators::getInstance().EraseAccumulatorValues(nCheckpoint, pindex->pprev->nAccumulatorCheckpoint))
return error("DisconnectBlock(): failed to erase checkpoint");
}
}

if (pfClean) {
Expand Down Expand Up @@ -2946,7 +2950,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
}

// zerocoin accumulator: if a new accumulator checkpoint was generated, check that it is the correct value
if (block.nVersion >= Params().Zerocoin_HeaderVersion() && pindex->nHeight % 10 == 0) {
if (!fVerifyingBlocks && block.nVersion >= Params().Zerocoin_HeaderVersion() && pindex->nHeight % 10 == 0) {
uint256 nCheckpointCalculated = 0;
if (!CAccumulators::getInstance().GetCheckpoint(pindex->nHeight, nCheckpointCalculated))
return state.DoS(100, error("ConnectBlock() : failed to calculate accumulator checkpoint"));
Expand All @@ -2955,7 +2959,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
LogPrintf("%s: block=%d calculated: %s\n block: %s\n", __func__, pindex->nHeight, nCheckpointCalculated.GetHex(), block.nAccumulatorCheckpoint.GetHex());
return state.DoS(100, error("ConnectBlock() : accumulator does not match calculated value"));
}
} else {
} else if (!fVerifyingBlocks) {
if (block.nAccumulatorCheckpoint != pindex->pprev->nAccumulatorCheckpoint) {
return state.DoS(100, error("ConnectBlock() : new accumulator checkpoint generated on a block that is not multiple of 10"));
}
Expand Down
1 change: 1 addition & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ extern bool fCheckBlockIndex;
extern unsigned int nCoinCacheSize;
extern CFeeRate minRelayTxFee;
extern bool fAlerts;
extern bool fVerifyingBlocks;

extern bool fLargeWorkForkFound;
extern bool fLargeWorkInvalidChainFound;
Expand Down

0 comments on commit 4386edd

Please sign in to comment.