edgecase
Author: StJohn Piano
Published: 2020-09-12
Datafeed Article 177
This article has been digitally signed by Edgecase Datafeed.
905 words - 257 lines - 7 pages





Parts


- Description
- Notes
- Recipe
- Example




Description


This recipe describes a method of managing the fee and broadcast of a Bitcoin transaction.

If a transaction is not mined, a new version can be constructed with a higher fee. Once the previous version has been cleared from memory pools of accessible Bitcoin nodes, the new version can be broadcast.

Previous version of this recipe:
Recipe for managing the fee and broadcast of a Bitcoin transaction




Notes


Various points:
- Bitcoin is stored in a Bitcoin address.
- An address holds "unspent outputs". Previous Bitcoin transactions created these outputs and sent them specifically to the address.
- The bitcoin balance of an address is the sum of the values in its unspent outputs.
- A new Bitcoin transaction will use unspent outputs as its inputs.
- When creating a new transaction, only complete inputs can be used. Inputs cannot be broken into smaller inputs prior to spending.
- In a new transaction, any change that you wish to keep can be sent in a new unspent output back to the original address or to another address that you control.

If a transaction disappears from the network (i.e. the memory pools of all public nodes), this doesn't mean that it has vanished permanently. Anyone who stored it can rebroadcast it later - it will still be valid. In order to make it invalid, create a new transaction that uses at least one of the same inputs.

An input in a new transaction is an as-yet-unspent output of a previous transaction. Each unspent output can only ever be spent once. If a transaction is broadcast that uses at least one input that has already been spent, this transaction is invalid and will not be mined.










Recipe



Objectives:
- Fine control of the transaction fee.
- Ability to rebroadcast a new transaction with a higher fee if the previous transaction is not mined within a reasonable time period. This new transaction, if mined, should invalidate the previous transaction.



Relevant links:
- Recipe for creating and signing a standard Bitcoin transaction #2
- Recipe for creating and signing a nonstandard Bitcoin transaction #2



The Other services page will hopefully include links to active services that can:
- display current Bitcoin fee rates.
- decode / validate a Bitcoin transaction.
- broadcast a transaction to the Bitcoin network.
- look up a transaction in the Bitcoin blockchain and count its confirmations.



1) Gather / choose the required data for a new transaction. Include at least one "change address" output. Inputs should be added as necessary so that the total input value is greater than the total output value. Add enough to pay for the as-yet-unknown transaction fee. Any bitcoin that is left over will be sent to the change address.



2) Look at the current Bitcoin network transaction fee rates. Choose a fee rate for the transaction.



3) Create and sign a transaction with the specified fee rate.



4) Broadcast the transaction to the Bitcoin network.



5) Wait for the transaction to be mined.

Explanation: A node will store unconfirmed transactions in its memory pools for a particular time period. If the transaction is not mined (confirmed) within this period, the node will delete it from its memory pool. This period will vary depending on the node. I have read that the default period is 72 hours (3 days). In my experience, transaction deletions occur in 24-48 hours.

After broadcasting the transaction, wait 3 days + a small delay. If the transaction is mined during this period, stop here, and wait for the transaction to receive 6 confirmations (i.e. be 6 blocks deep into the Bitcoin blockchain). Otherwise, confirm that it has been deleted from the relevant node's memory pool (by looking up the transaction's txid) and continue to step (6).



6) Look at the current Bitcoin network transaction fee rates. Choose a higher fee rate than the one chosen in step (2).



7) Repeat steps (2)-(6) until the transaction is mined. At least one of the inputs should be present in all of these transactions, so that when one of them is mined, the others are invalidated.










Example



I'll summarise a transaction sequence from the previous project Creating and signing a standard Bitcoin transaction with two inputs and two outputs that demonstrates the process of the creation and broadcast of a new version of a transaction with a higher fee.



Initial conditions:
- I created addresses A1, A2, A3.

Address 1 (A1):
1BB66Gx4833uKx8Lo2k8Mt4TRk42WTr7cZ


A2:
12YCFdpsRDvEHNcj5rsmvJ5G2XXkW1icJP


A3:
1H5SUR7Fv7x252WuJPqBjewwpeHuJ218Ky




Transaction sequence:

- Transaction T1: I used Solidi, an online exchange, to transfer 0.01 BTC to address A1. Solidi created a transaction that included this transfer.
-- Txid1:
9d64ba7c5afdb3caa79bb79c35dde09af70b27829241b2823e4ffb918c75d153


- T2_v1 (Transaction 2, version 1): I created a transaction that would transfer the bitcoin in A1 to addresses A2 and A3 (0.005 bitcoin to each, with the A2 output paying the fee). This transaction had a fee rate of 1 satoshi / byte.
-- Txid2_v1:
d03971f787c974bdae8b77fef474567cbc6ae2626d85c36872efc9ddea7383f2

-- Result: T2_v1 was not mined. Within 32 hours, it had been removed from the public nodes that I checked.

- T2_v2: I created another version of T2. This transaction had a fee rate of 100 satoshi / byte.
-- Txid2_v2:
585f054c01139bdae20ff8fa233c4cda47db6d891a387e37dfd56f9cb39d1bea

- Result: T2_v2 was mined with 6+ confirmations in about an hour.