Skip to content

Consolidate UTXOs, start-up wait for the asset rebuild, GUI asset fixes, CompressAmount range - #295

Merged
cdonnachie merged 6 commits into
mainfrom
fix/gui-followups-507
Nov 28, 2026
Merged

cdonnachie merged 6 commits into
mainfrom
fix/gui-followups-507

Conversation

@cdonnachie

Copy link
Copy Markdown
Member

Follow-ups for 5.0.7: the Consolidate UTXOs dialog, a start-up race with the asset database rebuild, the open findings from the comparison of the 5.x GUI with 4.2.1, and the range of CompressAmount. Six commits, one per subject.

Consolidate UTXOs

Checked by driving the dialog on a regtest wallet. It built valid transactions before this change; what it got wrong:

Before Now
Locked coins Spent like any other. Left alone.
Unconfirmed coins The output of one batch could be taken up by the next, building a chain of large transactions that the mempool refuses. Left for a later run.
Inputs Coin selection was allowed to add inputs to the ones chosen for the batch. Only the chosen ones.
A coin too large for the batch Ended the batch. Passed over.
Remaining coins worth less than 0.001 AVN in all The run looped until cancelled. The run ends.
Wallet without private keys Not checked for. Refused at the start: the dialog signs and sends, with nothing to hand to another signer.
Normal finish Shown as a warning. Shown as information.

A transaction pays 1 sat/byte, the minimum a 4.x node relays, to one output.

Start-up waits for the asset database rebuild

-reindexassets and the balance rebuild run in the initload thread, under cs_main. AppInitMain did not wait for them: whichever thread took the lock first decided whether the window, the RPC server and the network came up on a half-built asset database. Quitting during the rebuild (q on the splash screen) then hung or crashed the GUI.

Start-up now waits for the rebuild and stops if a shutdown was requested meanwhile. The next start reports that an earlier rebuild did not finish and runs it again.

GUI findings from the 4.2.1 comparison

Before Now
Locks over message boxes Six handlers on the Create, Manage, Transfer and Restricted Assets pages held cs_wallet while a message box was open, and the two Check handlers of the restricted page held cs_main. The wallet, or the node, stood still until the box was answered. No lock is held over a box.
Asset amount field Took eight decimals whatever the asset. An amount finer than the asset's units was refused by the node after the confirmation. Follows the decimals of the asset selected: step, display and what may be typed.
Change of a restricted asset Sent to a fresh address without the qualifier tags the asset requires, so a transfer of part of a coin was refused (bad-txns-null-verifier-address-failed-verification). Also transfer and the other asset RPCs. Goes back to an address the asset was spent from, if the fresh one does not pass the verifier. A change address that was named and does not pass is refused with a message that says why.
Custom change on Transfer Assets Used only when coins were selected as well. Used whenever it is set.
Custom change address that is not P2PKH Accepted on Create and Manage Assets, though an asset can only go to P2PKH. Warned about as on the Send page; the same check on Transfer.
Transaction list An asset transaction showed as a coin payment of 0, or not at all. Rows "Asset issued", "Asset reissued", "Asset sent to" and "Asset received with", carrying amount and name; a "Transaction fee" row for an asset transaction with no coin payment; an "Assets" entry in the type filter.
Asset lists The Transfer and Create pages kept the list they were opened with. They follow the wallet's asset model. Manage Assets still builds its list when it is shown.
Out of sync No warning on Create, Manage and the restricted pages. The warning the Send and Transfer pages give.
PSBT dialog, NONE and SINGLE Signed without a word. Asks first, saying what the signature leaves open.

Not in this PR:

  • The list of the wallet's restricted addresses is still fed only by what this GUI submits. 4.x filled it from connected blocks; that needs a scan on the wallet side, a database write and a signal to the GUI.
  • The asset pages cannot build a PSBT on a wallet without private keys: the three builders in wallet/asset_tx.cpp sign unconditionally.
  • The restricted pages keep confirming with the fee before sending, where 4.x had Check, then Submit.

CompressAmount

CompressAmount() turns an amount that does not end in a zero into about nine times the amount. With Avian's MAX_MONEY that no longer fits 64 bits: from 20,496,382,304.12172403 AVN up, the amount read back from the coins database is another one.

No output of a valid chain can hold that much: the subsidy schedule issues under 10.5 billion in all. The database format is therefore left alone. There is no unused value to mark a wide amount with, and a new format would mean rewriting every chainstate for a case that cannot occur.

What changes: the limit has a name, MAX_COMPRESSIBLE_AMOUNT, and a unit test holds the schedule of each network to it. The integer fuzz target, which asserted the property up to MAX_MONEY and was excluded from CI for failing, asserts it up to the limit and runs again.

How it was checked

Built and run in WSL (clang 18, Qt 6.4). Nothing was run on Windows or macOS.

  • Unit tests: 167 of 168 suites pass; script_assets_tests is skipped locally for want of its data directory.

  • Qt tests (test_avian-qt, offscreen):

    • consolidateDialogTests: 520 coins of 0.04 AVN become transactions of 500 and 20 inputs; block rewards go 16 to a batch with a locked one left alone. Each transaction is in the mempool, has one output, pays 1 sat/byte and is mined. The locked coin check fails without the fix.
    • assetAmountFieldTests: what the field shows and what it refuses for 0, 2 and 8 decimals.
    • assetHistoryTests: issue and transfer of an asset with one decimal give the rows above.
    • Three older tests of that binary (URI, nested RPC, wallet) fail before and after; they still expect Bitcoin's values.
  • Start-up, on a copy of mainnet: SIGTERM sent during the rebuild. Before, the GUI's window and network threads had come up during the rebuild and the process did not exit. Now GUI and daemon log "Shutdown done" within a second; the next start logs that an earlier rebuild did not finish, rebuilds, and reaches "Done loading" only after it.

  • Restricted change, regtest: transfer of 100 out of a coin of 1000 of a restricted asset. Before: refused. Now: 100 to the buyer, 900 back to the tagged holder, in the mempool. A named change address without the tag is refused with the new message.

  • Fuzz: integer, 1,027,642 runs under ASan and UBSan, clean.

  • Functional: mining_sv2.py, the one functional test CI runs, passes.

Compiled, not run: the sync warning, the NONE/SINGLE question and the list refresh. Nothing was looked at on screen. A manual pass before release: consolidate with a locked coin in the wallet; quit on the splash screen during -reindexassets; type an amount for an asset with fewer than eight decimals; send part of a restricted asset coin; look at the transaction list after an issue and a transfer; sign a PSBT with SINGLE.

The dialog took its coins from listCoins(), which includes locked coins, and
selected them like any other, so a coin the user had locked was spent. Coins
are now taken by one rule in the list and in the run: within the input
amounts, confirmed, not locked.

Unconfirmed coins are left for a later run. The coin a batch made was listed
again at once, and when it fell within the input amounts the next batch
spent it. Two batches of 500 inputs chained that way are more than the
mempool takes as one family, and the wallet keeps a transaction the mempool
refused.

Also:

- a batch spends the coins selected for it and no others
  (m_allow_other_inputs was left on);
- a coin too large for what is left of a batch is passed over for a smaller
  one, where the selection used to stop at it within an address;
- the coins are counted from the wallet for each batch, not kept in a
  counter, and the progress goes by coins: the estimate of the batches to
  come went by the coins-per-batch limit alone, and the amount limit is
  often the one that binds;
- a selection worth 0.001 AVN or less ends the run; it was skipped with
  nothing changed, so the loop went round until it was cancelled;
- a run that ends normally is reported with an information box, not a
  warning;
- the dialog does not run on a wallet without private keys. From reading: it
  would have committed transactions without signatures.

A test drives the dialog on a regtest wallet: 520 small coins go into a
transaction of 500 inputs and one of 20, and block rewards go 16 to a batch
under an amount limit. Each transaction is accepted by the mempool, pays one
output to the address, costs 1000 per 1000 bytes, which is the least a 4.x
node relays, and is taken by a block. Before the change the test failed on
the locked coin, which was spent; everything else it checks passed then too.
The rebuild runs in the initload thread under cs_main. AppInitMain did not
wait for it: with a chain tip already loaded it went straight on to start
the node, and needed cs_main on the way. Which thread took the lock first
decided what happened:

- AppInitMain first: "Done loading", the network threads and the GUI window
  came up while the rebuild had only begun, on an asset database half
  rebuilt.
- the rebuild first: AppInitMain waited for the lock, past its last check
  for a shutdown request. When the rebuild was interrupted it got the lock
  and finished the start-up as if nothing had happened.

A shutdown request during the rebuild (q on the splash screen) therefore
left a GUI running on a database that was not rebuilt. Seen on Linux with
the GUI and a copy of mainnet: the window was up, the request was not acted
on, and the process had to be killed. On Windows the same key press was
reported to crash the wallet with a heap corruption.

AppInitMain now waits until the initload thread is done with the asset
database and returns false if a shutdown was requested meanwhile. On the
same data the GUI stops within a second of the request ("Shutdown done"),
the next start says an earlier rebuild did not finish, rebuilds, and reports
"Done loading" only after it.
AssetAmountField::setUnit() stored the decimals and did nothing with them:
every asset was shown and entered with eight. The spin box now formats an
amount with the decimals of the asset and takes no amount finer than that.
The value stays in base units of 0.00000001 as before.

4.x did this through its own unit formatting.
…restricted asset

Locks. Eight places in the asset pages took cs_wallet or cs_main and then
opened a message box in the same scope: the freeze and tag handlers with
their confirmation, error and success boxes, the two Check buttons, and the
"sent" boxes of Create, Manage and Transfer Assets. With cs_wallet held the
wallet cannot take in a block, with cs_main the node cannot connect one,
until the user answers. The wallet functions called there take cs_wallet
themselves, so those locks are gone; the two Check handlers hold cs_main for
the lookup only.

Change of a restricted asset. A transfer of part of a restricted asset sent
the rest to a new address of the wallet, which has none of the tags the
verifier string asks for, and the node refused the transaction
(bad-txns-null-verifier-address-failed-verification). The change now goes
back to an address the asset is being spent from that passes the verifier,
which is what 4.x did. A change address named by the caller that fails is
refused with a message that says why. Checked on regtest: 100 of 1000 $RST
sent to a tagged address, 900 back to the tagged holder, in the mempool;
before the change the same call was rejected.

Custom change address. On Transfer Assets it was passed on for the asset
change only when coins were selected too. On all three pages a change
address that is not a key hash address is refused when it is entered: it is
given an asset output.
…allet, two warnings

Transaction list. An asset output was decomposed like a payment in coins and
read "Sent to" or "Received with" 0.00 AVN. The record now knows four more
kinds of row, as 4.x had: an asset issued, reissued, sent and received, with
the amount of the asset and its name in the amount column. An asset output
that stays in the wallet of a transaction the wallet made is not a payment
and has no debit row; a transfer between the wallet's own addresses has no
row at all. The fee of a transaction is put on its first payment in coins,
and a plain asset transfer has none, so it gets a fee row. The type filter
has an Assets entry. The amount role of an asset row stays 0: sorting, the
amount filter and the balloon work in coins.

Asset lists. Transfer Assets filled its list of assets on the first visit and
never again; 4.x refreshed it on every new transaction. It and Create Assets
now follow the asset model, which changes when the wallet's asset balances
do, and keep what is selected.

Warnings. Create Assets, Manage Assets and the two restricted asset actions
ask before sending while the node is in initial block download, as Send and
Transfer Assets do. The PSBT dialog asks before signing with NONE or SINGLE.

A test issues an asset with one decimal, sends 100.5 of it to a foreign
address and reads the rows of the table model: "Asset issued 1000", the
owner token, "Asset sent to -100.5", one fee row, no row for the 899.5 of
change. The warnings and the list refresh are compiled and not run.
CompressAmount() turns an amount that does not end in a zero into about
nine times the amount. With Bitcoin's MAX_MONEY that fits 64 bits with room
to spare; Avian's is a thousand times larger, and from
20,496,382,304.12172403 coins up the compressed form wraps around, so the
amount read back from the coins database would be another one.

No output of a valid chain can hold that much. The subsidy starts at 2500
and halves every 2,100,000 blocks, the founder payment is taken from it,
and the sum over all of it is under 10.5 billion. So the database format is
left alone: there is no unused value to mark a wide amount with, and a new
format would mean rewriting every chainstate for a case that cannot occur.

What changes is that the limit has a name, MAX_COMPRESSIBLE_AMOUNT, and a
test: the amount at the limit survives, the next one does not, and the
coins the subsidy schedule issues on each network stay under it. A change
to the schedule that broke this would fail the test.

The integer fuzz target asserted the property up to MAX_MONEY, failed, and
was excluded from the CI run. It asserts it up to the limit now and is back
in the run: a million runs of it under ASan and UBSan found nothing. The
coins simulation in coins_tests draws its amounts up to the limit instead
of a round ten billion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant