From f94360305b9008a3509a5d600762b45b04e95fb3 Mon Sep 17 00:00:00 2001 From: furszy <5377650+furszy@users.noreply.github.com> Date: Wed, 27 Mar 2019 13:22:57 +0100 Subject: [PATCH] zpiv already precomputed validation + witness height refactor --- src/wallet.cpp | 13 ++++++++----- src/zpiv/accumulators.cpp | 39 ++++++++++++++++++--------------------- src/zpiv/accumulators.h | 7 +++++++ 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/wallet.cpp b/src/wallet.cpp index 5bbadb4f4c461..0e5578a5983e0 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -4742,11 +4742,14 @@ bool CWallet::MintsToInputVector(std::map& 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. diff --git a/src/zpiv/accumulators.cpp b/src/zpiv/accumulators.cpp index fa4b98614d162..30d25b51b6b9b 100644 --- a/src/zpiv/accumulators.cpp +++ b/src/zpiv/accumulators.cpp @@ -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); @@ -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; @@ -930,3 +911,19 @@ map 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 + } +} \ No newline at end of file diff --git a/src/zpiv/accumulators.h b/src/zpiv/accumulators.h index 7089961b76cc7..5b349372adbb5 100644 --- a/src/zpiv/accumulators.h +++ b/src/zpiv/accumulators.h @@ -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