Skip to content

Commit

Permalink
zpiv already precomputed validation + witness height refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Mar 27, 2019
1 parent 8082f27 commit be5b47f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
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 ( ((pindexCheckpoint) ? CalWitUpToChainHeight(pindexCheckpoint) : CalWitUpToChainHeight()) > 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
37 changes: 17 additions & 20 deletions src/zpiv/accumulators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,20 +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 = pindexCheckpoint ? CalWitUpToChainHeight(pindexCheckpoint) : CalWitUpToChainHeight();

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 +820,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 = pindexCheckpoint ? CalWitUpToChainHeight(pindexCheckpoint) : CalWitUpToChainHeight();

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

return mapRet;
}

int CalWitUpToChainHeight(){
int nChainHeight = chainActive.Height();
int nHeightMax = nChainHeight % 10;
return nChainHeight - nHeightMax - 20; // at least two checkpoints deep
}

int CalWitUpToChainHeight(CBlockIndex* pindex){
if (pindex){
int nHeightStop = pindex->nHeight - 10;
nHeightStop -= nHeightStop % 10;
return nHeightStop;
}
throw std::runtime_error("CalWitUpToChainHeight() null pindex");
}
3 changes: 3 additions & 0 deletions src/zpiv/accumulators.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ int GetChecksumHeight(uint32_t nChecksum, libzerocoin::CoinDenomination denomina
bool InvalidCheckpointRange(int nHeight);
bool ValidateAccumulatorCheckpoint(const CBlock& block, CBlockIndex* pindex, AccumulatorMap& mapAccumulators);

int CalWitUpToChainHeight();
int CalWitUpToChainHeight(CBlockIndex* pindex);


// Exceptions

Expand Down

0 comments on commit be5b47f

Please sign in to comment.