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

zpiv up to date precomputing validation + witness calculation height #1

Closed
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: 8 additions & 5 deletions src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4742,11 +4742,14 @@ bool CWallet::MintsToInputVector(std::map<CBigNum, CZerocoinMint>& mapMintsSelec
coinWitness->SetHeightMintAdded(mint.GetHeight());
}

// Generate the witness for each mint being spent
if (!GenerateAccumulatorWitness(coinWitness, mapAccumulators, pindexCheckpoint)) {
receipt.SetStatus(_("Try to spend with a higher security level to include more coins"),
ZPIV_FAILED_ACCUMULATOR_INITIALIZATION);
return error("%s : %s", __func__, receipt.GetStatusMessage());
// Check if it's already precomputed
if (CalWitUpToChainHeight(pindexCheckpoint) > coinWitness->nHeightAccEnd){
// Generate the witness for each mint being spent
if (!GenerateAccumulatorWitness(coinWitness, mapAccumulators, pindexCheckpoint)) {
receipt.SetStatus(_("Try to spend with a higher security level to include more coins"),
ZPIV_FAILED_ACCUMULATOR_INITIALIZATION);
return error("%s : %s", __func__, receipt.GetStatusMessage());
}
}

// Construct the CoinSpend object. This acts like a signature on the transaction.
Expand Down
39 changes: 18 additions & 21 deletions src/zpiv/accumulators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,21 +555,8 @@ bool GenerateAccumulatorWitness(CoinWitnessData* coinWitness, AccumulatorMap& ma
coinWitness->pAccumulator->setValue(witnessAccumulator.getValue());
}

//add the pubcoins from the blockchain up to the next checksum starting from the block
int nChainHeight = chainActive.Height();
int nHeightMax = nChainHeight % 10;
nHeightMax = nChainHeight - nHeightMax - 20; // at least two checkpoints deep

// Determine the height to stop at
int nHeightStop;
if (pindexCheckpoint) {
nHeightStop = pindexCheckpoint->nHeight - 10;
nHeightStop -= nHeightStop % 10;
LogPrint("zero", "%s: using checkpoint height %d\n", __func__, pindexCheckpoint->nHeight);
} else {
nHeightStop = nHeightMax;
}

int nHeightStop = CalWitUpToChainHeight(pindexCheckpoint);
if (nHeightStop <= coinWitness->nHeightAccEnd)
return error("%s: trying to accumulate bad block range, start=%d end=%d", __func__, coinWitness->nHeightAccEnd, nHeightStop);

Expand Down Expand Up @@ -832,13 +819,7 @@ bool GenerateAccumulatorWitness(

//add the pubcoins from the blockchain up to the next checksum starting from the block
CBlockIndex *pindex = chainActive[nHeightCheckpoint - 10];
int nChainHeight = chainActive.Height();
int nHeightStop = nChainHeight % 10;
nHeightStop = nChainHeight - nHeightStop - 20; // at least two checkpoints deep

//If looking for a specific checkpoint
if (pindexCheckpoint)
nHeightStop = pindexCheckpoint->nHeight - 10;
int nHeightStop = CalWitUpToChainHeight(pindexCheckpoint);

//Iterate through the chain and calculate the witness
int nCheckpointsAdded = 0;
Expand Down Expand Up @@ -930,3 +911,19 @@ map<CoinDenomination, int> GetMintMaturityHeight()

return mapRet;
}

/**
* If pindex is null then it will return the chain head mature checkpoint height
* @param pindex
* @return
*/
int CalWitUpToChainHeight(CBlockIndex* pindex){
if (pindex){
int nHeightStop = pindex->nHeight - 10;
nHeightStop -= nHeightStop % 10;
return nHeightStop;
}else{
int nChainHeight = chainActive.Height();
return nChainHeight - (nChainHeight % 10) - 20; // at least two checkpoints deep
}
}
7 changes: 7 additions & 0 deletions src/zpiv/accumulators.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ int GetChecksumHeight(uint32_t nChecksum, libzerocoin::CoinDenomination denomina
bool InvalidCheckpointRange(int nHeight);
bool ValidateAccumulatorCheckpoint(const CBlock& block, CBlockIndex* pindex, AccumulatorMap& mapAccumulators);

/**
* If pindex is null then it will return the last mature checkpoint height
* @param pindex
* @return
*/
int CalWitUpToChainHeight(CBlockIndex* pindex = nullptr);


// Exceptions

Expand Down