@@ -290,6 +290,7 @@ void TxConfirmStats::removeTx(unsigned int entryHeight, unsigned int nBestSeenHe
290
290
// of no harm to try to remove them again.
291
291
bool CBlockPolicyEstimator::removeTx (uint256 hash)
292
292
{
293
+ LOCK (cs_feeEstimator);
293
294
std::map<uint256, TxStatsInfo>::iterator pos = mapMemPoolTxs.find (hash);
294
295
if (pos != mapMemPoolTxs.end ()) {
295
296
feeStats.removeTx (pos->second .blockHeight , nBestSeenHeight, pos->second .bucketIndex );
@@ -315,6 +316,7 @@ CBlockPolicyEstimator::CBlockPolicyEstimator()
315
316
316
317
void CBlockPolicyEstimator::processTransaction (const CTxMemPoolEntry& entry, bool validFeeEstimate)
317
318
{
319
+ LOCK (cs_feeEstimator);
318
320
unsigned int txHeight = entry.GetHeight ();
319
321
uint256 hash = entry.GetTx ().GetHash ();
320
322
if (mapMemPoolTxs.count (hash)) {
@@ -374,6 +376,7 @@ bool CBlockPolicyEstimator::processBlockTx(unsigned int nBlockHeight, const CTxM
374
376
void CBlockPolicyEstimator::processBlock (unsigned int nBlockHeight,
375
377
std::vector<const CTxMemPoolEntry*>& entries)
376
378
{
379
+ LOCK (cs_feeEstimator);
377
380
if (nBlockHeight <= nBestSeenHeight) {
378
381
// Ignore side chains and re-orgs; assuming they are random
379
382
// they don't affect the estimate.
@@ -410,6 +413,7 @@ void CBlockPolicyEstimator::processBlock(unsigned int nBlockHeight,
410
413
411
414
CFeeRate CBlockPolicyEstimator::estimateFee (int confTarget)
412
415
{
416
+ LOCK (cs_feeEstimator);
413
417
// Return failure if trying to analyze a target we're not tracking
414
418
// It's not possible to get reasonable estimates for confTarget of 1
415
419
if (confTarget <= 1 || (unsigned int )confTarget > feeStats.GetMaxConfirms ())
@@ -427,18 +431,24 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, int *answerFoun
427
431
{
428
432
if (answerFoundAtTarget)
429
433
*answerFoundAtTarget = confTarget;
430
- // Return failure if trying to analyze a target we're not tracking
431
- if (confTarget <= 0 || (unsigned int )confTarget > feeStats.GetMaxConfirms ())
432
- return CFeeRate (0 );
433
-
434
- // It's not possible to get reasonable estimates for confTarget of 1
435
- if (confTarget == 1 )
436
- confTarget = 2 ;
437
434
438
435
double median = -1 ;
439
- while (median < 0 && (unsigned int )confTarget <= feeStats.GetMaxConfirms ()) {
440
- median = feeStats.EstimateMedianVal (confTarget++, SUFFICIENT_FEETXS, MIN_SUCCESS_PCT, true , nBestSeenHeight);
441
- }
436
+
437
+ {
438
+ LOCK (cs_feeEstimator);
439
+
440
+ // Return failure if trying to analyze a target we're not tracking
441
+ if (confTarget <= 0 || (unsigned int )confTarget > feeStats.GetMaxConfirms ())
442
+ return CFeeRate (0 );
443
+
444
+ // It's not possible to get reasonable estimates for confTarget of 1
445
+ if (confTarget == 1 )
446
+ confTarget = 2 ;
447
+
448
+ while (median < 0 && (unsigned int )confTarget <= feeStats.GetMaxConfirms ()) {
449
+ median = feeStats.EstimateMedianVal (confTarget++, SUFFICIENT_FEETXS, MIN_SUCCESS_PCT, true , nBestSeenHeight);
450
+ }
451
+ } // Must unlock cs_feeEstimator before taking mempool locks
442
452
443
453
if (answerFoundAtTarget)
444
454
*answerFoundAtTarget = confTarget - 1 ;
@@ -456,12 +466,14 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, int *answerFoun
456
466
457
467
void CBlockPolicyEstimator::Write (CAutoFile& fileout)
458
468
{
469
+ LOCK (cs_feeEstimator);
459
470
fileout << nBestSeenHeight;
460
471
feeStats.Write (fileout);
461
472
}
462
473
463
474
void CBlockPolicyEstimator::Read (CAutoFile& filein, int nFileVersion)
464
475
{
476
+ LOCK (cs_feeEstimator);
465
477
int nFileBestSeenHeight;
466
478
filein >> nFileBestSeenHeight;
467
479
feeStats.Read (filein);
0 commit comments