Skip to content

Commit 22eca7d

Browse files
committed
Add smart fee estimation functions
These are more useful fee and priority estimation functions. If there is no fee/pri high enough for the target you are aiming for, it will give you the estimate for the lowest target that you can reliably obtain. This is better than defaulting to the minimum. It will also pass back the target for which it returned an answer.
1 parent e54ebbf commit 22eca7d

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

src/policy/fees.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,28 @@ CFeeRate CBlockPolicyEstimator::estimateFee(int confTarget)
504504
return CFeeRate(median);
505505
}
506506

507+
CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, int *answerFoundAtTarget)
508+
{
509+
if (answerFoundAtTarget)
510+
*answerFoundAtTarget = confTarget;
511+
// Return failure if trying to analyze a target we're not tracking
512+
if (confTarget <= 0 || (unsigned int)confTarget > feeStats.GetMaxConfirms())
513+
return CFeeRate(0);
514+
515+
double median = -1;
516+
while (median < 0 && (unsigned int)confTarget <= feeStats.GetMaxConfirms()) {
517+
median = feeStats.EstimateMedianVal(confTarget++, SUFFICIENT_FEETXS, MIN_SUCCESS_PCT, true, nBestSeenHeight);
518+
}
519+
520+
if (answerFoundAtTarget)
521+
*answerFoundAtTarget = confTarget - 1;
522+
523+
if (median < 0)
524+
return CFeeRate(0);
525+
526+
return CFeeRate(median);
527+
}
528+
507529
double CBlockPolicyEstimator::estimatePriority(int confTarget)
508530
{
509531
// Return failure if trying to analyze a target we're not tracking
@@ -513,6 +535,25 @@ double CBlockPolicyEstimator::estimatePriority(int confTarget)
513535
return priStats.EstimateMedianVal(confTarget, SUFFICIENT_PRITXS, MIN_SUCCESS_PCT, true, nBestSeenHeight);
514536
}
515537

538+
double CBlockPolicyEstimator::estimateSmartPriority(int confTarget, int *answerFoundAtTarget)
539+
{
540+
if (answerFoundAtTarget)
541+
*answerFoundAtTarget = confTarget;
542+
// Return failure if trying to analyze a target we're not tracking
543+
if (confTarget <= 0 || (unsigned int)confTarget > priStats.GetMaxConfirms())
544+
return -1;
545+
546+
double median = -1;
547+
while (median < 0 && (unsigned int)confTarget <= priStats.GetMaxConfirms()) {
548+
median = priStats.EstimateMedianVal(confTarget++, SUFFICIENT_PRITXS, MIN_SUCCESS_PCT, true, nBestSeenHeight);
549+
}
550+
551+
if (answerFoundAtTarget)
552+
*answerFoundAtTarget = confTarget - 1;
553+
554+
return median;
555+
}
556+
516557
void CBlockPolicyEstimator::Write(CAutoFile& fileout)
517558
{
518559
fileout << nBestSeenHeight;

src/policy/fees.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,21 @@ class CBlockPolicyEstimator
242242
/** Return a fee estimate */
243243
CFeeRate estimateFee(int confTarget);
244244

245+
/** Estimate fee rate needed to get be included in a block within
246+
* confTarget blocks. If no answer can be given at confTarget, return an
247+
* estimate at the lowest target where one can be given.
248+
*/
249+
CFeeRate estimateSmartFee(int confTarget, int *answerFoundAtTarget);
250+
245251
/** Return a priority estimate */
246252
double estimatePriority(int confTarget);
247253

254+
/** Estimate priority needed to get be included in a block within
255+
* confTarget blocks. If no answer can be given at confTarget, return an
256+
* estimate at the lowest target where one can be given.
257+
*/
258+
double estimateSmartPriority(int confTarget, int *answerFoundAtTarget);
259+
248260
/** Write estimation data to a file */
249261
void Write(CAutoFile& fileout);
250262

src/txmempool.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,11 +701,21 @@ CFeeRate CTxMemPool::estimateFee(int nBlocks) const
701701
LOCK(cs);
702702
return minerPolicyEstimator->estimateFee(nBlocks);
703703
}
704+
CFeeRate CTxMemPool::estimateSmartFee(int nBlocks, int *answerFoundAtBlocks) const
705+
{
706+
LOCK(cs);
707+
return minerPolicyEstimator->estimateSmartFee(nBlocks, answerFoundAtBlocks);
708+
}
704709
double CTxMemPool::estimatePriority(int nBlocks) const
705710
{
706711
LOCK(cs);
707712
return minerPolicyEstimator->estimatePriority(nBlocks);
708713
}
714+
double CTxMemPool::estimateSmartPriority(int nBlocks, int *answerFoundAtBlocks) const
715+
{
716+
LOCK(cs);
717+
return minerPolicyEstimator->estimateSmartPriority(nBlocks, answerFoundAtBlocks);
718+
}
709719

710720
bool
711721
CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const

src/txmempool.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,21 @@ class CTxMemPool
454454

455455
bool lookup(uint256 hash, CTransaction& result) const;
456456

457+
/** Estimate fee rate needed to get into the next nBlocks
458+
* If no answer can be given at nBlocks, return an estimate
459+
* at the lowest number of blocks where one can be given
460+
*/
461+
CFeeRate estimateSmartFee(int nBlocks, int *answerFoundAtBlocks = NULL) const;
462+
457463
/** Estimate fee rate needed to get into the next nBlocks */
458464
CFeeRate estimateFee(int nBlocks) const;
459465

466+
/** Estimate priority needed to get into the next nBlocks
467+
* If no answer can be given at nBlocks, return an estimate
468+
* at the lowest number of blocks where one can be given
469+
*/
470+
double estimateSmartPriority(int nBlocks, int *answerFoundAtBlocks = NULL) const;
471+
460472
/** Estimate priority needed to get into the next nBlocks */
461473
double estimatePriority(int nBlocks) const;
462474

0 commit comments

Comments
 (0)