In this article we will study what is Replace-By-Fee transactions and how to use them. Surprise, with RBF you can try to do doublespends!

Imagine situation when Alice sends coins to Bob. She creates and signs the transaction, and broadcasts it to the network for confirmation. Unfortunately, the transaction hangs unconfirmed for a long time, as it has low fee (or an ok fee under other circumstances, but at this point of time unconfirmed transactions queue is too big and miners prefer transactions with higher fees). What can Alice do? Her options: a) wait for a confirmation (might take a while) b) wait till transaction gets cancelled (most miners will exclude it from mempool) and freed coins would be spendable again (i.e. create transactions using those coins and most pools won’t reject it as doublespend, as old transaction got cleared from mempool) c) replace transaction, increasing the fee (RBF).

Thus, RBF — replace of existing transaction with a new one, increasing the fee. “Replace” means that new transaction will use same inputs (i.e. coins) as old one, and this would not be considered as doublespend (i.e. cheating). As result, only one transaction will be confirmed, mined and added to blockchain (most probably the one with higher fee).

RBF was introduced in BIP-0125

But kind of transaction is potentially replaceable? Good question. Transaction replacement was introduced by the Bitcoin designer himself Satoshi Nakamoto, but disabled at some point. It was then improved/upgraded to RBF and shipped in Bitcoin Core 0.12+. For replaceability, there’s a designated int field called nSequence, which signals which transaction is an older version and which one is younger. So, for the transaction to be replaceable nSequence should be lower than MAX (0xffffffff — 1). By default, most wallets set nSequence to maximum and you have to manually enable transactions replacement ability in settings. Here’s how it looks in Electrum:

Enable RBF in Electrum wallet

An example of decoded transaction:

...

"inputs": [

{

"addresses": [ "...." ],

"output_index": 0,

"output_value": 1010000,

"prev_hash": "...",

"script": "...",

"script_type":

"pay-to-pubkey-hash",

"sequence": 0

},

...

Sequence is indicated for each transaction input.

In Electrum after you enable RBF you’ll be able to increase transaction fee right in GUI. Right click the transaction and you’ll see it. But now we are going to manually create RBF transaction. Why? First of all, to better understand what’s going on. Second, for more versatility. In case of manual RBF we can specify totally different destination addresses, which can be considered a doublespend.

This is how it works. Alice wants to pay Bob for goods. Alice creates, signs and broadcasts transaction (which transfers coins to Bob), but intentionally sets low (or zero) fee. If Bob is not cautious enough he accepts unconfirmed transaction as payment and sends goods to Alice. After that Alice replaces the transaction with output addresses which Alice controls. Basically, Alice returns her funds. Bob got scammed. How can Bob avoid that? First, if Bob wants to accept unconfirmed transactions (for speed), don’t accept replaceable transactions (where nSequence is lower than MAX). Of course, ideal case when Bob waits for enough confirmations.