<blockquote>
  <p>If I pass in say <code>2000</code> as the <code>nBytes</code> argument, what will this function return as the fee <code>nFee</code>?</p>
</blockquote>

<p>It depends on the fee rate that the CFeeRate object represents. The fee rate is denoted in Bitcoin per Kilobyte. So you pass in the number of bytes into GetFee and and calculates the amount in Bitcoin for the fee to be paid for something of size <code>nBytes_</code>.</p>

<p>If, for example, your CFeeRate object represents 0.00020000 Bitcoin per Kilobyte (or 20 satoshis per byte), giving 2000 as nBytes means that your fee will be <code>2000 * 0.0002 / 1000 = 0.0004 BTC</code>.</p>

<blockquote>
  <p>I don't understand how <code>nSatoshisPerK</code> is defined.</p>
</blockquote>

<p>It is defined at the creation of the object. Whatever created the object specifies the fee rate to use.</p>

<blockquote>
  <p>Also, curious as to why this is used instead of letting miners determine this fee through selective mining.</p>
</blockquote>

<p>This is just an implementation detail, not an actual network thing that specifies the fee rate for everyone to use. CFeeRate is internal to Bitcoin Core and is simply just how fee rates are representing in internal logic. The fee rates themselves are constructed by the fee estimator which calculates fees based upon mined transactions.</p>

CC BY-SA 4.0
2018-07-03T19:45:31.993Z
Ok so this definition `CFeeRate(const I _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK)` in `feerate.h` looks like the one being used in `validation.cpp` here: `CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);`, which would set `nSatoshisPerK` to `DEFAULT_MIN_RELAY_TX_FEE`. Is that what you're saying?  Also, it seems that this is enforced in `validation.cpp` I thought these validations were run on all incoming blocks.
JBaczuk
CC BY-SA 4.0
2018-07-03T19:52:27.350Z
There are multiple fee rates used for different purposes. One of them is the minimum relay fee (which is what you are looking at) which is the minimum fee that a transaction must pay in order for it to be accepted to the node's mempool and relayed. Yes. that particular fee rate in validation.cpp sets nSatoshisPerK for that fee rate object to the DEFAULT_MIN_RELAY_TX_FEE.
Ava Chow
CC BY-SA 4.0
2018-07-03T19:13:35.593Z
2018-07-03T19:13:35.593Z
Ava Chow
<p>Referring to the function <code>CAmount CFeeRate::GetFee(size_t nBytes_)</code> in <code>feerate.cpp</code>: </p>

<p>If I pass in say <code>2000</code> as the <code>nBytes</code> argument, what will this function return as the fee <code>nFee</code>? I don't understand how <code>nSatoshisPerK</code> is defined. Also, curious as to why this is used instead of letting miners determine this fee through selective mining.</p>

<pre><code>CAmount CFeeRate::GetFee(size_t nBytes_) const
{
    assert(nBytes_ &lt;= uint64_t(std::numeric_limits&lt;int64_t&gt;::max()));
    int64_t nSize = int64_t(nBytes_);

    CAmount nFee = nSatoshisPerK * nSize / 1000;

    if (nFee == 0 &amp;&amp; nSize != 0) {
        if (nSatoshisPerK &gt; 0)
            nFee = CAmount(1);
        if (nSatoshisPerK &lt; 0)
            nFee = CAmount(-1);
    }

    return nFee;
}
</code></pre>

<mining-reward><bitcoin-core-development>
Please explain CFeeRate::GetFee(size_t nBytes)
JBaczuk
# Please explain CFeeRate::GetFee(size_t nBytes)

(Asked by: username_0 on 2018-07-03)

Referring to the function `CAmount CFeeRate::GetFee(size_t nBytes_)` in `feerate.cpp`:

If I pass in say `2000` as the `nBytes` argument, what will this function return as the fee `nFee`? I don't understand how `nSatoshisPerK` is defined. Also, curious as to why this is used instead of letting miners determine this fee through selective mining.

```
CAmount CFeeRate::GetFee(size_t nBytes_) const
{
    assert(nBytes_ <= uint64_t(std::numeric_limits<int64_t>::max()));
    int64_t nSize = int64_t(nBytes_);

    CAmount nFee = nSatoshisPerK * nSize / 1000;

    if (nFee == 0 && nSize != 0) {
        if (nSatoshisPerK > 0)
            nFee = CAmount(1);
        if (nSatoshisPerK < 0)
            nFee = CAmount(-1);
    }

    return nFee;
}

```

**Tags:** mining-reward, bitcoin-core-development


---

## 1 answers

### Answer by username_1 on 2018-07-03. Score: 1 | Accepted answer

> If I pass in say `2000` as the `nBytes` argument, what will this function return as the fee `nFee`?

It depends on the fee rate that the CFeeRate object represents. The fee rate is denoted in Bitcoin per Kilobyte. So you pass in the number of bytes into GetFee and and calculates the amount in Bitcoin for the fee to be paid for something of size `nBytes_`.

If, for example, your CFeeRate object represents 0.00020000 Bitcoin per Kilobyte (or 20 satoshis per byte), giving 2000 as nBytes means that your fee will be `2000 * 0.0002 / 1000 = 0.0004 BTC`.

> I don't understand how `nSatoshisPerK` is defined.

It is defined at the creation of the object. Whatever created the object specifies the fee rate to use.

> Also, curious as to why this is used instead of letting miners determine this fee through selective mining.

This is just an implementation detail, not an actual network thing that specifies the fee rate for everyone to use. CFeeRate is internal to Bitcoin Core and is simply just how fee rates are representing in internal logic. The fee rates themselves are constructed by the fee estimator which calculates fees based upon mined transactions.

> **Comments:**
> **username_0**: Ok so this definition `CFeeRate(const I _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK)` in `feerate.h` looks like the one being used in `validation.cpp` here: `CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);`, which would set `nSatoshisPerK` to `DEFAULT_MIN_RELAY_TX_FEE`. Is that what you're saying?  Also, it seems that this is enforced in `validation.cpp` I thought these validations were run on all incoming blocks.  
> **username_1**: There are multiple fee rates used for different purposes. One of them is the minimum relay fee (which is what you are looking at) which is the minimum fee that a transaction must pay in order for it to be accepted to the node's mempool and relayed. Yes. that particular fee rate in validation.cpp sets nSatoshisPerK for that fee rate object to the DEFAULT_MIN_RELAY_TX_FEE.  

---

