Skip to content

Commit e736149

Browse files
committed
don't hold cd_spendcache for the entire loop
1 parent 6e2cf8d commit e736149

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

src/wallet.cpp

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,39 +3112,49 @@ bool CWallet::CreateCoinStake(
31123112
if (nHeightCacheStop % 10)
31133113
nHeightCacheStop -= nHeightCacheStop % 10;
31143114
if (!fKernelFound) {
3115-
TRY_LOCK(cs_spendcache, fLocked);
3116-
if (!fLocked)
3117-
return false;
31183115

31193116
//Do some precomputing of zerocoin spend knowledge proofs
31203117
int nCompute = 50000 / listInputs.size();
31213118
for (std::unique_ptr<CStakeInput>& stakeInput : listInputs) {
3122-
if (ShutdownRequested())
3123-
return false;
3124-
if (!stakeInput->IsZPIV())
3125-
continue;
3119+
{
3120+
TRY_LOCK(cs_spendcache, fLocked);
3121+
if (!fLocked) {
3122+
LogPrintf("%s : Cannot get lock on cs_spendcache\n", __func__);
3123+
continue;
3124+
}
31263125

3127-
CoinWitnessData* witnessData = zpivTracker->GetSpendCache(stakeInput->GetSerialHash());
3128-
int nHeightStop = nHeightCacheStop;
3129-
if (witnessData->nHeightAccStart == 0) {
3130-
// This has no cache, so initialize it
3131-
CZerocoinMint mint;
3132-
if (!GetMintFromStakeHash(stakeInput->GetSerialHash(), mint))
3126+
if (ShutdownRequested())
3127+
return false;
3128+
if (!stakeInput->IsZPIV())
31333129
continue;
3134-
*witnessData = CoinWitnessData(mint);
3135-
nHeightStop = std::min(chainActive.Height() - 210, mint.GetHeight() + nCompute);
3136-
} else {
3137-
nHeightStop = std::min(chainActive.Height() - 210, witnessData->nHeightAccEnd + nCompute);
3138-
}
31393130

3140-
if (nHeightStop - witnessData->nHeightAccEnd < 20)
3141-
continue;
3131+
CoinWitnessData *witnessData = zpivTracker->GetSpendCache(stakeInput->GetSerialHash());
3132+
int nHeightStop = nHeightCacheStop;
3133+
if (!witnessData->nHeightAccStart) {
3134+
// This has no cache, so initialize it
3135+
CZerocoinMint mint;
3136+
if (!GetMintFromStakeHash(stakeInput->GetSerialHash(), mint))
3137+
continue;
3138+
*witnessData = CoinWitnessData(mint);
3139+
nHeightStop = std::min(chainActive.Height() - 210, mint.GetHeight() + 1000);
3140+
} else {
3141+
nHeightStop = std::min(chainActive.Height() - 210, (witnessData->nHeightAccEnd > 0 ? witnessData->nHeightAccEnd : witnessData->nHeightAccStart) + 1000);
3142+
}
3143+
3144+
if (nHeightStop - witnessData->nHeightAccEnd < 20)
3145+
continue;
31423146

3143-
CBlockIndex* pindexStop = chainActive[nHeightStop];
3144-
AccumulatorMap mapAccumulators(Params().Zerocoin_Params(false));
3145-
LogPrintf("%s: caching mint %s\n start=%d\n end=%d\n", __func__, witnessData->coin->getValue().GetHex().substr(0, 6), witnessData->nHeightAccStart, nHeightStop);
3146-
if (!GenerateAccumulatorWitness(witnessData, mapAccumulators, 100, pindexStop)) {
3147-
LogPrintf("%s: caching of witness failed!\n", __func__);
3147+
CBlockIndex *pindexStop = chainActive[nHeightStop];
3148+
AccumulatorMap mapAccumulators(Params().Zerocoin_Params(false));
3149+
LogPrintf("%s: caching mint %s\n start=%d\n end=%d\n", __func__,
3150+
witnessData->coin->getValue().GetHex().substr(0, 6), witnessData->nHeightAccStart,
3151+
nHeightStop);
3152+
if (!GenerateAccumulatorWitness(witnessData, mapAccumulators, pindexStop)) {
3153+
LogPrintf("%s: caching of witness failed!\n", __func__);
3154+
} else {
3155+
LogPrintf("%s : caching of mint %s success!\n", __func__,
3156+
witnessData->coin->getValue().GetHex().substr(0, 6));
3157+
}
31483158
}
31493159
}
31503160
return false;
@@ -4758,7 +4768,21 @@ bool CWallet::MintsToInputVector(std::map<CBigNum, CZerocoinMint>& mapMintsSelec
47584768
libzerocoin::ZerocoinParams* paramsAccumulator = Params().Zerocoin_Params(false);
47594769
AccumulatorMap mapAccumulators(paramsAccumulator);
47604770

4761-
LOCK(cs_spendcache);
4771+
int nLockAttempts = 0;
4772+
while (nLockAttempts < 100) {
4773+
TRY_LOCK(cs_spendcache, lockSpendcache);
4774+
if (!lockSpendcache) {
4775+
MilliSleep(100);
4776+
++nLockAttempts;
4777+
continue;
4778+
}
4779+
break;
4780+
}
4781+
if (nLockAttempts == 100) {
4782+
LogPrintf("%s : could not get lock on cs_spendcache\n");
4783+
receipt.SetStatus(_("could not get lock on cs_spendcache"), ZPIV_TXMINT_GENERAL);
4784+
return false;
4785+
}
47624786

47634787
int64_t nTimeStart = GetTimeMicros();
47644788
for (auto& it : mapMintsSelected) {

0 commit comments

Comments
 (0)