Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Fees, replace by fee and pinning

After this lesson: I can decide whether a node accepts a replacement transaction.

60 min lesson3 commandsBitcoin Script lab: timelocks, multisig, Miniscript and Taproot on a regtest node

A payment stuck in the queue

A block has room for only so much weight. When more transactions wait than fit, miners pick the ones that pay most for their size.

If your payment pays too little, it waits. This lesson shows how a node measures a fee and how a payment can be replaced. It also shows how somebody can make replacing hard.

By the end of this lesson

Note

I can decide whether a node accepts a replacement transaction.

Key words

WordWhat it meansExample
Mempoolthe transactions a node holds before they are mined
Fee ratethe fee divided by the virtual size2.0 sat/vB
Replace by feesending a new version that spends the same input and pays more
Incremental relay feethe extra rate a replacement must add
Pinningmaking a transaction costly to replace

The cells students fill in are left blank in this preview.

Miners compare fee rates

A miner fills a block by weight, so a transaction is judged by its fee per virtual byte, not its total fee. Weight divided by 4, rounded up, is the virtual size.

When the lab started it left one payment from Bob in the mempool. It pays 220 sats for 110 virtual bytes, which is 2 sat/vB.

Look at the waiting payment and two replacements

Command
docker exec bitcoind sh steps/fees.sh

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: what the node said

TransactionFeevsizeThe node
The waiting payment220 sats110in the mempool
Replacement, same fee220 sats110insufficient fee
Replacement, twice the fee440 sats110allowed

The cells students fill in are left blank in this preview.

Try it: which pays more per byte?

Code: python
# Two transactions waiting for the next block. Which would a miner pick first?
waiting = [
    {"name": "big", "fee": 3000, "vsize": 1500},
    {"name": "small", "fee": 900, "vsize": 150},
]

for tx in waiting:
    rate = tx["fee"] / tx["vsize"]
    print(tx["name"], "pays", tx["fee"], "sats, which is", rate, "sat/vB")
# Change the big one's fee to 12000. Which is first now?

A replacement must pay for itself

A node takes a replacement only if it pays more than what it pushes out. Its fee must cover the old fee plus the incremental relay fee for its own size. This node's incremental fee is 100 sats per 1000 virtual bytes, which is 0.1 sat/vB.

So replacing 220 sats on 110 virtual bytes needs at least 220 plus 11, which is 231 sats.

Work the same numbers in Python

Command
docker exec jupyter python3 steps/feerate.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A common mistake

Warning

Many people think the transaction with the biggest total fee is mined first. A miner has limited weight to fill. A small transaction paying a high rate beats a large one paying more in total.

Pinning makes replacing expensive

Anybody who can spend an output of a waiting transaction can hang a child on it. A large, cheap child means a replacement must pay for pushing out both.

That matters for channels, where a closing transaction may need to be replaced in time. Newer relay rules, such as small TRUC transactions, limit children to make pinning harder.

See what a large child costs

Command
docker exec jupyter python3 steps/pinning.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

About this lab and its source: Bitcoin Script lab: timelocks, multisig, Miniscript and Taproot on a regtest node

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.