message: [Wallet] Refactor ListMints to more quickly detect orphan/failed stakes
diff: @@ -380,23 +380,31 @@ std::set<CMintMeta> CzPIVTracker::ListMints(bool fUnusedOnly, bool fMatureOnly,
  //See if there is internal record of spending this mint (note this is memory only, would reset on restart)
  bool isPendingSpend = static_cast<bool>(mapPendingSpends.count(mint.hashSerial));
  +
  + // See if there is a blockchain record of spending this mint
  + uint256 txidSpend;
  + bool isConfirmedSpend = zerocoinDB->ReadCoinSpend(mint.hashSerial, txidSpend);
  +
  + // Double check the mempool for pending spend
  + bool fRemovedPending = false;
  if (isPendingSpend) {
  uint256 txidPendingSpend = mapPendingSpends.at(mint.hashSerial);
  if (!setMempool.count(txidPendingSpend)) {
  + fRemovedPending = true;
  RemovePending(txidPendingSpend);
  LogPrintf("%s: pending txid %s removed because not in mempool\n", __func__, txidPendingSpend.GetHex());
  }
  -
  }
  - //See if there is blockchain record of spending this mint
  - uint256 txidSpend;
  - bool isConfirmedSpend = zerocoinDB->ReadCoinSpend(mint.hashSerial, txidSpend);
  -
  //If a pending spend got confirmed, then remove it from the pendingspend map
  if (isPendingSpend && isConfirmedSpend)
  mapPendingSpends.erase(mint.hashSerial);
  + // If the pending spend was removed from the mempool, and there is no confirmed
  + // spend on the blockchain, then it should no longer be treated as a pending spend
  + if (fRemovedPending && !isConfirmedSpend)
  + isPendingSpend = false;
  +
  bool isUsed = isPendingSpend || isConfirmedSpend;
  if (fUnusedOnly && isUsed)
  @@ -416,14 +424,14 @@ std::set<CMintMeta> CzPIVTracker::ListMints(bool fUnusedOnly, bool fMatureOnly,
  mint.txid = txid;
  }
  - if (!IsInitialBlockDownload() && GetTransaction(mint.txid, tx, hashBlock, true)) {
  + if (!IsInitialBlockDownload() && !GetTransaction(mint.txid, tx, hashBlock, true)) {
  LogPrintf("%s failed to find tx for mint txid=%s\n", __func__, mint.txid.GetHex());
  Archive(mint);
  continue;
  }
  //if not in the block index, most likely is unconfirmed tx
  - if (mapBlockIndex.count(hashBlock) && !chainActive.Contains(mapBlockIndex[hashBlock])) {
  + if (mapBlockIndex.count(hashBlock) && !chainActive.Contains(mapBlockIndex.at(hashBlock))) {
  if (mint.isUsed != isUsed) {
  mint.isUsed = isUsed;
  LogPrintf("%s: set mint %s isUsed to %d\n", __func__, mint.hashPubcoin.GetHex(), isUsed);
  