Add a GetMinimumFeeRate function which is wrapped by GetMinimumFee

This commit is contained in:
Andrew Chow 2018-03-05 16:39:48 -05:00
parent cd927ff328
commit fab04887c2
2 changed files with 43 additions and 21 deletions

View file

@ -20,6 +20,22 @@ CAmount GetRequiredFee(unsigned int nTxBytes)
CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc) CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc)
{
CAmount fee_needed = GetMinimumFeeRate(coin_control, pool, estimator, feeCalc).GetFee(nTxBytes);
// Always obey the maximum
if (fee_needed > maxTxFee) {
fee_needed = maxTxFee;
if (feeCalc) feeCalc->reason = FeeReason::MAXTXFEE;
}
return fee_needed;
}
CFeeRate GetRequiredFeeRate()
{
return std::max(CWallet::minTxFee, ::minRelayTxFee);
}
CFeeRate GetMinimumFeeRate(const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc)
{ {
/* User control of how to calculate fee uses the following parameter precedence: /* User control of how to calculate fee uses the following parameter precedence:
1. coin_control.m_feerate 1. coin_control.m_feerate
@ -28,15 +44,15 @@ CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, c
4. nTxConfirmTarget (user-set global variable) 4. nTxConfirmTarget (user-set global variable)
The first parameter that is set is used. The first parameter that is set is used.
*/ */
CAmount fee_needed; CFeeRate feerate_needed ;
if (coin_control.m_feerate) { // 1. if (coin_control.m_feerate) { // 1.
fee_needed = coin_control.m_feerate->GetFee(nTxBytes); feerate_needed = *(coin_control.m_feerate);
if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE; if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
// Allow to override automatic min/max check over coin control instance // Allow to override automatic min/max check over coin control instance
if (coin_control.fOverrideFeeRate) return fee_needed; if (coin_control.fOverrideFeeRate) return feerate_needed;
} }
else if (!coin_control.m_confirm_target && ::payTxFee != CFeeRate(0)) { // 3. TODO: remove magic value of 0 for global payTxFee else if (!coin_control.m_confirm_target && ::payTxFee != CFeeRate(0)) { // 3. TODO: remove magic value of 0 for global payTxFee
fee_needed = ::payTxFee.GetFee(nTxBytes); feerate_needed = ::payTxFee;
if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE; if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
} }
else { // 2. or 4. else { // 2. or 4.
@ -48,38 +64,32 @@ CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, c
if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE) conservative_estimate = true; if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE) conservative_estimate = true;
else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL) conservative_estimate = false; else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL) conservative_estimate = false;
fee_needed = estimator.estimateSmartFee(target, feeCalc, conservative_estimate).GetFee(nTxBytes); feerate_needed = estimator.estimateSmartFee(target, feeCalc, conservative_estimate);
if (fee_needed == 0) { if (feerate_needed == CFeeRate(0)) {
// if we don't have enough data for estimateSmartFee, then use fallbackFee // if we don't have enough data for estimateSmartFee, then use fallbackFee
fee_needed = CWallet::fallbackFee.GetFee(nTxBytes); feerate_needed = CWallet::fallbackFee;
if (feeCalc) feeCalc->reason = FeeReason::FALLBACK; if (feeCalc) feeCalc->reason = FeeReason::FALLBACK;
// directly return if fallback fee is disabled (feerate 0 == disabled) // directly return if fallback fee is disabled (feerate 0 == disabled)
if (CWallet::fallbackFee.GetFee(1000) == 0) return fee_needed; if (CWallet::fallbackFee == CFeeRate(0)) return feerate_needed;
} }
// Obey mempool min fee when using smart fee estimation // Obey mempool min fee when using smart fee estimation
CAmount min_mempool_fee = pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(nTxBytes); CFeeRate min_mempool_feerate = pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
if (fee_needed < min_mempool_fee) { if (feerate_needed < min_mempool_feerate) {
fee_needed = min_mempool_fee; feerate_needed = min_mempool_feerate;
if (feeCalc) feeCalc->reason = FeeReason::MEMPOOL_MIN; if (feeCalc) feeCalc->reason = FeeReason::MEMPOOL_MIN;
} }
} }
// prevent user from paying a fee below minRelayTxFee or minTxFee // prevent user from paying a fee below minRelayTxFee or minTxFee
CAmount required_fee = GetRequiredFee(nTxBytes); CFeeRate required_feerate = GetRequiredFeeRate();
if (required_fee > fee_needed) { if (required_feerate > feerate_needed) {
fee_needed = required_fee; feerate_needed = required_feerate;
if (feeCalc) feeCalc->reason = FeeReason::REQUIRED; if (feeCalc) feeCalc->reason = FeeReason::REQUIRED;
} }
// But always obey the maximum return feerate_needed;
if (fee_needed > maxTxFee) {
fee_needed = maxTxFee;
if (feeCalc) feeCalc->reason = FeeReason::MAXTXFEE;
}
return fee_needed;
} }
CFeeRate GetDiscardRate(const CBlockPolicyEstimator& estimator) CFeeRate GetDiscardRate(const CBlockPolicyEstimator& estimator)
{ {
unsigned int highest_target = estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE); unsigned int highest_target = estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);

View file

@ -26,6 +26,18 @@ CAmount GetRequiredFee(unsigned int nTxBytes);
*/ */
CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc); CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc);
/**
* Return the minimum required feerate taking into account the
* floating relay feerate and user set minimum transaction feerate
*/
CFeeRate GetRequiredFeeRate();
/**
* Estimate the minimum fee rate considering user set parameters
* and the required fee
*/
CFeeRate GetMinimumFeeRate(const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc);
/** /**
* Return the maximum feerate for discarding change. * Return the maximum feerate for discarding change.
*/ */