user-1:   "mempoolminfee" : n,       (numeric) Minimum fee rate in BTC/kB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee
  "minrelaytxfee" : n,       (numeric) Current minimum relay fee for transactions

https://bitcoincore.org/en/doc/0.21.0/rpc/blockchain/getmempoolinfo/
I am trying to understand how is mempoolminfee calculated. Based on above documentation it should be max of minrelaytxfee and minimum mempool fee. I understand minrelaytxfee is a config option which can be set to anything by user. What is the other fee: 'minimum mempool fee'?
Looking at the code for getmempoolinfo RPC I found this comment in one header file which explains the function that is used for calculation:
    ret.pushKV("mempoolminfee", ValueFromAmount(std::max(pool.GetMinFee(maxmempool), ::minRelayTxFee).GetFeePerK()));


https://github.com/bitcoin/bitcoin/blob/fdd80b0a53b4af0b29cb6e03118e2456d053a757/src/rpc/blockchain.cpp#L1655
    /** The minimum fee to get into the mempool, which may itself not be enough
      *  for larger-sized transactions.
      *  The incrementalRelayFee policy variable is used to bound the time it
      *  takes the fee rate to go back down all the way to 0. When the feerate
      *  would otherwise be half of this, it is set to 0 instead.
      */

https://github.com/bitcoin/bitcoin/blob/fdd80b0a53b4af0b29cb6e03118e2456d053a757/src/txmempool.h#L731
CFeeRate CTxMemPool::GetMinFee(size_t sizelimit) const {
    LOCK(cs);
    if (!blockSinceLastRollingFeeBump || rollingMinimumFeeRate == 0)
        return CFeeRate(llround(rollingMinimumFeeRate));

    int64_t time = GetTime();
    if (time > lastRollingFeeUpdate + 10) {
        double halflife = ROLLING_FEE_HALFLIFE;
        if (DynamicMemoryUsage() < sizelimit / 4)
            halflife /= 4;
        else if (DynamicMemoryUsage() < sizelimit / 2)
            halflife /= 2;

        rollingMinimumFeeRate = rollingMinimumFeeRate / pow(2.0, (time - lastRollingFeeUpdate) / halflife);
        lastRollingFeeUpdate = time;

        if (rollingMinimumFeeRate < (double)incrementalRelayFee.GetFeePerK() / 2) {
            rollingMinimumFeeRate = 0;
            return CFeeRate(0);
        }
    }
    return std::max(CFeeRate(llround(rollingMinimumFeeRate)), incrementalRelayFee);
}

https://github.com/bitcoin/bitcoin/blob/fdd80b0a53b4af0b29cb6e03118e2456d053a757/src/txmempool.cpp#L1080
I still don't understand what GetMinFee() does. Can someone explain this is in a different way or use some examples?
If I save minrelaytxfee=0.0002 and incrementalrelayfee=0.0003, mempoolminfee is 0.0002 which isn't max of both.

user48884: From the mempool's perspective, the rollingMinimumFeeRate is the actual minimum fee rate (the minrelayfee pertains to other parts of the codebase). It starts at 0, and is updated whenever a transaction package is evicted from the mempool due to size constraints. When the mempool reaches its configured maximum, it removes the lowest feerate package to free up space, and sets the rollingMinimumFeeRate to the feerate the removed package paid.
But then you might wonder how the rollingMinimumFeeRate goes down? What happens if the maximum mempool size is reached, but then transactions get confirmed and the mempool's size is reduced? The rollingMinimumFeeRate needs to decrease eventually, and that is what GetMinFee is doing.
GetMinFee is the second part of updating rollingMinimumFeeRate. It reduces it exponentially according to a schedule. If it has been more than 10 seconds since rollingMinimumFeeRate has changed (either up or down), then it will be divided by some power of 2.
If the mempool is more than half full, and no packages are removed to increase rollingMinimumFeeRate again, then after 12 hours, it will be half of what its original value was. That is to say, rollingMinimumFeeRate has a half life of 12 hours when the mempool is at least half full.
This half life decreases when the mempool is between a quarter full and half full. In that case, it is 6 hours. And when the mempool is less than a quarter full, the half life is 3 hours. This is what this section does:
    double halflife = ROLLING_FEE_HALFLIFE;
    if (DynamicMemoryUsage() < sizelimit / 4)
        halflife /= 4;
    else if (DynamicMemoryUsage() < sizelimit / 2)
        halflife /= 2;

To compute the reduced rollingMinimumFeeRate, we want to figure out how much time has passed since the last update and figure out how much lower it should be given the half life that we want. That is what this line does:
    rollingMinimumFeeRate = rollingMinimumFeeRate / pow(2.0, (time - lastRollingFeeUpdate) / halflife);

This is really just the half life formula: N(t) = N_0 * (1/2) ^ (t/t_1/2).
N_0 is the initial quantity (rollingMinimumFeeRate), t is the time that has passed (time - lastRollingFeeUpdate), and t_1/2 is the half life we want (halflife). If you move the exponent into the fraction, reduce the formula, and write it out in code, you get the aforementioned computation line.
The last piece to this update is to check whether the rollingMinimumFeeRate is less than half of the incrementalRelayFee. If it is, then rollingMinimumFeeRate is set to 0. This is done because exponential decay means that rollingMinimumFeeRate would technically never reach 0 (that's how the exponential decay function works). So instead we just bound it to be no less than half of the incrementalRelayFee. Once it reaches that, it just resets to 0 because at that point, it will be incredibly low anyways.
We use the incrementalRelayFee as the lower bound because that is the minimum feerate a transaction needs to pay after accounting for conflicts in order to be relayed. So a transaction must be paying at least incrementalRelayFee, so we can drop the rollingMinimumFeeRate after it is less than incrementalRelayFee, but to be safe, we do it at half of that value.
That last piece of GetMinFee is at the top of the function, which is simply that the current rollingMinimumFeeRate does not change until we find a block, and an optimization to return it when it is already 0. This first part is important because we don't want to be reducing the mempool's minimum fee when things aren't being removed from the mempool.

-1: <pre><code>  &quot;mempoolminfee&quot; : n,       (numeric) Minimum fee rate in BTC/kB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee
  &quot;minrelaytxfee&quot; : n,       (numeric) Current minimum relay fee for transactions
</code></pre>
<p><a href="https://bitcoincore.org/en/doc/0.21.0/rpc/blockchain/getmempoolinfo/" rel="nofollow noreferrer">https://bitcoincore.org/en/doc/0.21.0/rpc/blockchain/getmempoolinfo/</a></p>
<p>I am trying to understand how is <code>mempoolminfee</code> calculated. Based on above documentation it should be max of <code>minrelaytxfee</code> and minimum mempool fee. I understand <code>minrelaytxfee</code> is a config option which can be set to anything by user. What is the other fee: 'minimum mempool fee'?</p>
<p>Looking at the code for <code>getmempoolinfo</code> RPC I found this comment in one header file which explains the function that is used for calculation:</p>
<pre><code>    ret.pushKV(&quot;mempoolminfee&quot;, ValueFromAmount(std::max(pool.GetMinFee(maxmempool), ::minRelayTxFee).GetFeePerK()));

</code></pre>
<p><a href="https://github.com/bitcoin/bitcoin/blob/fdd80b0a53b4af0b29cb6e03118e2456d053a757/src/rpc/blockchain.cpp#L1655" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/fdd80b0a53b4af0b29cb6e03118e2456d053a757/src/rpc/blockchain.cpp#L1655</a></p>
<pre><code>    /** The minimum fee to get into the mempool, which may itself not be enough
      *  for larger-sized transactions.
      *  The incrementalRelayFee policy variable is used to bound the time it
      *  takes the fee rate to go back down all the way to 0. When the feerate
      *  would otherwise be half of this, it is set to 0 instead.
      */
</code></pre>
<p><a href="https://github.com/bitcoin/bitcoin/blob/fdd80b0a53b4af0b29cb6e03118e2456d053a757/src/txmempool.h#L731" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/fdd80b0a53b4af0b29cb6e03118e2456d053a757/src/txmempool.h#L731</a></p>
<pre><code>CFeeRate CTxMemPool::GetMinFee(size_t sizelimit) const {
    LOCK(cs);
    if (!blockSinceLastRollingFeeBump || rollingMinimumFeeRate == 0)
        return CFeeRate(llround(rollingMinimumFeeRate));

    int64_t time = GetTime();
    if (time &gt; lastRollingFeeUpdate + 10) {
        double halflife = ROLLING_FEE_HALFLIFE;
        if (DynamicMemoryUsage() &lt; sizelimit / 4)
            halflife /= 4;
        else if (DynamicMemoryUsage() &lt; sizelimit / 2)
            halflife /= 2;

        rollingMinimumFeeRate = rollingMinimumFeeRate / pow(2.0, (time - lastRollingFeeUpdate) / halflife);
        lastRollingFeeUpdate = time;

        if (rollingMinimumFeeRate &lt; (double)incrementalRelayFee.GetFeePerK() / 2) {
            rollingMinimumFeeRate = 0;
            return CFeeRate(0);
        }
    }
    return std::max(CFeeRate(llround(rollingMinimumFeeRate)), incrementalRelayFee);
}
</code></pre>
<p><a href="https://github.com/bitcoin/bitcoin/blob/fdd80b0a53b4af0b29cb6e03118e2456d053a757/src/txmempool.cpp#L1080" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/fdd80b0a53b4af0b29cb6e03118e2456d053a757/src/txmempool.cpp#L1080</a></p>
<p>I still don't understand what GetMinFee() does. Can someone explain this is in a different way or use some examples?</p>
<p>If I save <code>minrelaytxfee=0.0002</code> and <code>incrementalrelayfee=0.0003</code>, <code>mempoolminfee</code> is 0.0002 which isn't max of both.</p>
 48884: <p>From the mempool's perspective, the <code>rollingMinimumFeeRate</code> is the actual minimum fee rate (the minrelayfee pertains to other parts of the codebase). It starts at 0, and is updated whenever a transaction package is evicted from the mempool due to size constraints. When the mempool reaches its configured maximum, it removes the lowest feerate package to free up space, and sets the <code>rollingMinimumFeeRate</code> to the feerate the removed package paid.</p>
<p>But then you might wonder how the <code>rollingMinimumFeeRate</code> goes down? What happens if the maximum mempool size is reached, but then transactions get confirmed and the mempool's size is reduced? The <code>rollingMinimumFeeRate</code> needs to decrease eventually, and that is what <code>GetMinFee</code> is doing.</p>
<p><code>GetMinFee</code> is the second part of updating <code>rollingMinimumFeeRate</code>. It reduces it exponentially according to a schedule. If it has been more than 10 seconds since <code>rollingMinimumFeeRate</code> has changed (either up or down), then it will be divided by some power of 2.</p>
<p>If the mempool is more than half full, and no packages are removed to increase <code>rollingMinimumFeeRate</code> again, then after 12 hours, it will be half of what its original value was. That is to say, <code>rollingMinimumFeeRate</code> has a half life of 12 hours when the mempool is at least half full.</p>
<p>This half life decreases when the mempool is between a quarter full and half full. In that case, it is 6 hours. And when the mempool is less than a quarter full, the half life is 3 hours. This is what this section does:</p>
<pre><code>    double halflife = ROLLING_FEE_HALFLIFE;
    if (DynamicMemoryUsage() &lt; sizelimit / 4)
        halflife /= 4;
    else if (DynamicMemoryUsage() &lt; sizelimit / 2)
        halflife /= 2;
</code></pre>
<p>To compute the reduced <code>rollingMinimumFeeRate</code>, we want to figure out how much time has passed since the last update and figure out how much lower it should be given the half life that we want. That is what this line does:</p>
<pre><code>    rollingMinimumFeeRate = rollingMinimumFeeRate / pow(2.0, (time - lastRollingFeeUpdate) / halflife);
</code></pre>
<p>This is really just the <a href="https://en.wikipedia.org/wiki/Half-life#Formulas_for_half-life_in_exponential_decay" rel="nofollow noreferrer">half life formula</a>: <code>N(t) = N_0 * (1/2) ^ (t/t_1/2)</code>.</p>
<p><code>N_0</code> is the initial quantity (<code>rollingMinimumFeeRate</code>), <code>t</code> is the time that has passed (<code>time - lastRollingFeeUpdate</code>), and <code>t_1/2</code> is the half life we want (<code>halflife</code>). If you move the exponent into the fraction, reduce the formula, and write it out in code, you get the aforementioned computation line.</p>
<p>The last piece to this update is to check whether the <code>rollingMinimumFeeRate</code> is less than half of the incrementalRelayFee. If it is, then <code>rollingMinimumFeeRate</code> is set to 0. This is done because exponential decay means that <code>rollingMinimumFeeRate</code> would technically never reach 0 (that's how the exponential decay function works). So instead we just bound it to be no less than half of the <code>incrementalRelayFee</code>. Once it reaches that, it just resets to 0 because at that point, it will be incredibly low anyways.</p>
<p>We use the <code>incrementalRelayFee</code> as the lower bound because that is the minimum feerate a transaction needs to pay after accounting for conflicts in order to be relayed. So a transaction must be paying at least <code>incrementalRelayFee</code>, so we can drop the <code>rollingMinimumFeeRate</code> after it is less than <code>incrementalRelayFee</code>, but to be safe, we do it at half of that value.</p>
<p>That last piece of <code>GetMinFee</code> is at the top of the function, which is simply that the current <code>rollingMinimumFeeRate</code> does not change until we find a block, and an optimization to return it when it is already 0. This first part is important because we don't want to be reducing the mempool's minimum fee when things aren't being removed from the mempool.</p>
