Full stack Bitcoin software

Wallets

The libbitcoin C++ libraries, from Bitcoin primitives to a complete server.

1. Unconfirmed conflicts#

Bitcoin Server (bs) reports every transaction it has seen that meets its fee rate policy and can still confirm, including unconfirmed transactions that conflict with each other. This section sets out the choices a wallet has for presenting and spending around such conflicts, and what each one costs.

1.1 Why wallets see conflicts#

A single Bitcoin Core node holds one linearization of the transactions it has seen: when two transactions spend the same coin, it keeps at most one of them. A wallet behind such a server can come to assume it will never be shown a conflict. That assumption does not hold:

So every wallet that takes unconfirmed transactions from a server has to deal with conflicts somehow, whichever server it uses. Most wallets were not written to expect them.

bs keeps every transaction it has seen that meets its fee rate policy and can still be mined in a future block. Transactions that conflict cannot all confirm, but until a block decides, each is a valid candidate and each is reported: in address history, in pool listings, and as spenders of the coins they share. When a block confirms one, every transaction that conflicts with it can no longer confirm and leaves the wallet-facing answers.

1.2 Conflicts are not just pairs#

Two transactions conflict when they spend a common coin. A transaction also inherits every conflict of its ancestors: if a parent cannot confirm, neither can its child. Beyond that simple case, conflicts take many shapes:

So the possible outcomes are not "one side or the other". They are the sets of transactions that can confirm together, and there can be many of them. Everything below is stated in those terms.

1.3 A worked example#

The simplest case. A wallet holds one confirmed coin of 1.00000000 BTC. It pays 0.30000000 to a merchant with a 500 sat fee (tx A), then signs the same payment again with a 3,000 sat fee (tx B). Both spend the same coin and both are unconfirmed.

confirmed coin                    1.00000000
tx A  pays 0.30000000, change     0.69999500
tx B  pays 0.30000000, change     0.69997000

if A confirms, balance            0.69999500
if B confirms, balance            0.69997000
counting both changes             1.39996500   (cannot happen)
merchant counting both payments   0.60000000   (cannot happen)

The outcomes the chain can actually reach are listed in green. A figure that adds up transactions which cannot confirm together is not one of them.

1.4 Find the conflicts#

Everything else depends on knowing which transactions compete. A wallet already downloads its transactions to build history, so it can find conflicts without any new server call:

  1. Index the inputs of every unconfirmed transaction the wallet holds, by the coin each one spends.
  2. Two transactions that spend the same coin conflict.
  3. Each transaction inherits the conflicts of every unconfirmed transaction whose outputs it spends.
  4. Transactions joined by any chain of conflicts form one conflict group. Transactions outside every group are unaffected.

This finds every conflict among transactions that touch the wallet's own scripts. The case it cannot see is covered under incoming payments.

1.5 Balance#

A balance is a single number, so here the wallet has to choose among outcomes. There are three reasonable choices.

Option What it shows Cost
Worst outcome The lowest balance any set of transactions that can confirm together produces. In the example, 0.69997000. Never overstates. Understates until a block decides, by as much as the outcomes differ.
Predicted outcome The balance if the transactions the wallet expects to confirm do, for example those with the higher fee rate, or the ones the wallet itself signed last. Matches what users usually expect. Wrong if the prediction fails, for example when a miner already has another transaction in its template, or when the conflict comes from someone else.
Range Confirmed balance plus a separate pending figure, shown as its lowest and highest value. Most accurate. Takes more space, and users need to understand it.

The balance answers of bs use the worst outcome: Electrum blockchain.scripthash.get_balance reports it in unconfirmed, and the Esplora address unconfirmed totals (its mempool_stats field) do the same. A wallet that takes its balance from these calls gets that choice already made.

These answers are per script. When conflicting transactions pay different addresses in the same wallet, each script's worst case is taken on its own, and adding them up can come out lower than any real outcome. A wallet that wants an exact wallet-wide figure computes it from its own conflict groups.

1.6 Spending#

Coin selection is where one rule is not optional: a new transaction must never spend outputs of two transactions that conflict with each other. Such a transaction can never be valid, because one of its parents can never confirm. A node that holds only one of the parents does not have, and cannot get, the other's output, so it sees the new transaction as an orphan, not a conflict. The wallet is the only place this can be caught. Within that rule there are three choices.

Option Behaviour Cost
Hold Outputs of any transaction in a conflict group are not spendable until a block decides. Never builds a transaction that can fail. Those funds wait for a confirmation.
Predicted outcome Outputs of the transactions the wallet expects to confirm are spendable; outputs of anything that conflicts with them are not. Use the same choice as the balance. Funds are usable right away. If a different outcome confirms, the new transaction fails with its parent and has to be rebuilt.
Spend the contested coins Spend the coins the transactions compete for, not their outputs. Valid whichever way the earlier conflict would have gone. The new transaction joins the group as one more candidate.

"Send max" and any spendable-amount figure follow the same option, so the number shown is one the wallet can actually send.

1.7 History#

A transaction list does not force a choice. Every candidate is real until a block decides, and showing all of them is accurate. What matters is that conflicting transactions are shown as alternatives, not as independent payments.

1.8 When a block decides#

Once a transaction confirms, everything that conflicts with it can no longer confirm, and bs stops including those transactions in history, pool listings and balances. The same applies to anything that spends their outputs.

A wallet that caches transactions locally needs to drop entries the server no longer reports. Kept forever, they stay in the balance and in coin selection after the question is settled.

1.9 Incoming payments#

When a conflicting transaction also pays the wallet, both appear in its history and everything above applies. When it pays someone else instead, only the original touches the wallet's scripts. The wallet sees one unconfirmed incoming payment and no conflict.

1.10 Summary#

Decision Required Wallet's choice
Conflicts Group transactions that spend a common coin, including inherited conflicts. None.
Balance Show an outcome the chain can reach. Worst outcome, predicted outcome, or range.
Spending Never combine outputs of conflicting transactions. Hold, predicted outcome, or spend the contested coins.
History Show conflicting transactions as alternatives. Mark, group, or show the expected outcome.
Resolution Drop what the server stops reporting. None.

Copyright © 2011-2026 libbitcoin developers