Skip to content

tapdb: keep the genesis anchor on the mint transaction - #11

Draft
SadiqJaf wants to merge 4 commits into
satscrip/v0.8.5-supplycommit-basefrom
satscrip/supplycommit-anchor-fix
Draft

SadiqJaf wants to merge 4 commits into
satscrip/v0.8.5-supplycommit-basefrom
satscrip/supplycommit-anchor-fix

Conversation

@SadiqJaf

@SadiqJaf SadiqJaf commented Oct 10, 2026 •

Copy link
Copy Markdown

Why this exists

Draft in Satscryption/taproot-assets only. Nothing here is filed against lightninglabs/taproot-assets.

A transfer proof upsert retargets genesis_points.anchor_tx_id from the mint transaction to the transfer transaction. FetchUnspentMintSupplyPreCommits then joins that column for the minting tx bytes while still using mint_supply_pre_commits.tx_output_index. newRootCommitment builds input 0 as transfer_txid:precommit_idx and signs it with the mint pre-commit key. Bitcoin Core rejects the Schnorr signature. The supply-commit state machine sits in CommitBroadcastState, later fetches return "no block info available", and later burns become dangling updates.

Reproduced on v0.8.0, v0.8.5, and upstream main as of the lightninglabs#2331 head: mint with supply commitments, confirm, send, UpdateSupplyCommit.

This branch is layer 1, cut from stock v0.8.5 (bb3f1c6b, tag v0.8.5). Head is ac101ed0. Layer 2 is the lightninglabs#2331 clone (satscrip/supplycommit-burn-provenance, #12), based on this branch. Fork PR #10 (satscrip/v0.8.5-candidate, the 721 redesign) is planned as layer 3 on top of layer 2. This PR does not rebase or modify #9 or #10.

Root cause

upsertAssetGen (tapdb/universe.go) runs for every proof type from universeUpsertProofLeaf. It always calls AnchorGenesisPoint, an unconditional UPDATE genesis_points SET anchor_tx_id. The schema comment on genesis_points says anchor_tx_id is the transaction that spends prev_out, which is the mint, not a later transfer.

The local mint path CommitSignedGenesisTx sets the same column correctly. mint_supply_pre_commits.outpoint and tx_output_index are stored from the real mint tx at funding time and are not retargeted. Only the join key is wrong.

Changing only the pre-commit query would leave the other readers of genesis_points.anchor_tx_id pointed at the transfer:

  • ConfirmChainTx stamps block info onto whatever anchor_tx_id points at. If a transfer retargets the pointer before the mint confirms, the mint tx never receives its block. The reported repro confirms first, so ConfirmChainTx has already written the mint row.
  • genesis_info_view exposes anchor_txid and block_height for universe leaves, balances, and stats.
  • FetchGenesisPointByAnchorTx.

The guard is therefore on the write: AnchorGenesisPoint runs only when the proof's anchor transaction spends asset.Genesis.FirstPrevOut. Transfer transactions are still stored with UpsertChainTx. Re-upserting the real issuance proof heals a pointer a transfer previously overwrote, because only the mint spends that outpoint.

QuerySyncStats parsed genesis_info_view.anchor_txid with chainhash.NewHash and failed with invalid hash length of 0 once that column could stay NULL. 8f772951 leaves AnchorPoint empty until a 32-byte mint txid is stored. A present but wrong-length txid is still an error.

Repair

Migration 61 (000061_repair_genesis_anchor.up.sql plus repairGenesisAnchorMigration) is programmatic, same pattern as migrations 50 and 51.

  • For each genesis point, scan chain_txns for the transaction that spends prev_out. One spender replaces a pointer that does not already spend the outpoint. Zero spenders (transfer arrived before the issuance tx was stored) leaves the pointer. Two or more spenders returns an error rather than guessing. An anchor that already spends the outpoint is a no-op, including a healthy custom-genesis mint.
  • Block metadata that ConfirmChainTx wrote onto the wrong transaction before the mint confirmed is not moved. Confirm-then-send leaves the mint's own chain_txns row with its block fields.
  • Never-confirmed CommitBroadcastState commitments whose inputs match none of the stored mint pre-commit outpoints, remote pre-commit outpoints, or confirmed supply-commitment outpoints are abandoned. The transition's new_commitment_id and pending_commit_txn_id are cleared, the transition is unfrozen, spent_by foreign keys are cleared, the supply_commitments row is deleted, and the state machine returns to UpdatesPendingState. Supply update events are kept. A valid unconfirmed commitment spends a real pre-commit or prior commitment outpoint and is not abandoned, so it is not double-spent by a rebuild. If no known outpoints exist, nothing is abandoned. A confirmed commitment (block_hash set) is not in the candidate set, even if its input is unknown.
  • startAssetSM copies the transition's pending updates onto UpdatesPendingState when that state was loaded empty. The next UpdateSupplyCommit tick rebuilds the tree from those events and broadcasts a commitment that spends the caller-selected pre-commit. A valid CommitBroadcastState is not rehydrated into a second broadcast. Migration runs before RPC, so a restarted node sees the repaired database before the state machine starts. A process already stuck in memory still holds the rejected transaction until restart.

Migration 61 does not stop a later unguarded upsert. The write guard is what keeps a transfer from retargeting the anchor after the migration has run.

Fork PR #9 and #10

Read-only. Neither branch is modified.

#9 (ta/721-slim, custom genesis PSBT, issue 721) does not modify tapdb/universe.go, upsertAssetGen, or the AnchorGenesisPoint SQL. CommitSignedGenesisTx is refactored into CommitSignedGenesisTxWithKey and still anchors TxIn[0].PreviousOutPoint with the mint chain tx. assets.sql changes the internal-key upsert, not anchor_tx_id. The planter stores a caller-selected PreCommitOutputIndex in the existing mint_supply_pre_commits row. anchoring_site.go watches mint-tx input scripts via customAnchorInputPrevOut, not genesis_points.anchor_tx_id. No conflict with the write guard. After this fix the pre-commit join still uses the mint tx bytes at that caller-selected index. Repair restores the tx that spends genesis input 0, which is the custom mint tx. A healthy custom-genesis anchor is a no-op.

#10 (satscrip/v0.8.5-candidate) adds FetchSupplySyncerPushedServers on BaseUniverseStore and does not touch upsertAssetGen or AnchorGenesisPoint. It does not touch asset_minting.go or assets.sql. supply_commit.sql adds QuerySupplyCommitmentByTxid and QuerySupplyCommitTransitionByNewCommitment and does not change the FetchUnspentMintSupplyPreCommits join. The caretaker confirmation watch uses signedTx.TxOut[anchorIdx].PkScript instead of TxOut[0]. That is the asset-anchor output watch, not genesis_points.anchor_tx_id. custom_anchor.go selects the caller pre-commit index on the mint tx. #10 also adds broadcast confirmation recovery (findCommitConfirmation / tryRecoverBroadcastConfirmation). A later textual merge can overlap universe/supplycommit/transitions.go, states.go, manager.go, and supply_commit.sql. Behaviourally, their recovery finalizes a CommitBroadcast tx that actually confirmed. This migration abandons only a never-confirmed tx that does not spend a real pre-commit or prior-commitment outpoint. A valid custom-genesis commitment spends mint_txid:caller_index and is not abandoned. On a future merge, abandon invalid commitments first, then run their confirmation recovery for valid in-flight transactions. #10 is planned as layer 3 on top of #12. This PR does not rebase it.

Tests

TestTransferProofKeepsCustomGenesisPreCommit upserts a mint tx with an extra input and a pre-commit at output index 1, then a transfer proof that spends a mint output. The genesis anchor and FetchUnspentMintSupplyPreCommits must still return that mint txid and index. That is the custom-genesis (lightninglabs#2238 / lightninglabs#721) shape. A full custom-genesis itest is not on this branch: FundBatch / PrepareBatch / FinalizeBatch are not on stock v0.8.5 and live on #9 / #10, which this branch does not depend on.

TestRepairGenesisAnchor covers restore, no-spender, and multiple-spenders. TestAbandonInvalidUnconfirmedSupplyCommit abandons a signed tx that misses the real pre-commit and keeps one that spends it, including the pending update. Those two cases were not weakened.

TestMigration61CommitmentSafety (ac101ed0) adds three cases the abandon rule must not touch:

  • a confirmed CommitBroadcast commitment, even when its input is an unknown outpoint
  • an unconfirmed commitment that spends a confirmed commitment outpoint
  • an unconfirmed commitment that spends a custom-genesis pre-commit at caller index 1, after a transfer had retargeted the genesis anchor; the migration restores the mint tx and leaves the commitment in CommitBroadcast

TestMigration61RebuildsValidCommitment abandons a commitment whose input is neither a pre-commit nor a confirmed commitment outpoint, then starts the real supply-commit manager the way a restarted node does. The next tick rebuilds from the pending mint event and broadcasts a transaction that spends the caller-selected pre-commit at index 1. The stored chain tx matches the broadcast tx.

TestHydrateInitialState covers the restart copy of pending updates.

supply commit send before commit mints with supply commitments, sends to a second tapd before the first UpdateSupplyCommit, syncs the transfer leaf in (SYNC_FULL, then InsertProof if the sync did not land it), and asserts the confirmed commitment's input 0 is mint_txid:precommit_idx. It then reissues into the group, transfers again before the next commit, and asserts that commitment spends the new mint pre-commit and the previous commitment outpoint. The second send is the issuer's second outbound transfer and the recipient's second receive (edf285de).

Verification

make unit three times on ac101ed0, with -test.count=1 so the second and third runs re-execute (nocache=1). The first execution is the initial make unit (PASS, no cached packages). Two later invocations in the same loop were cache hits and are not counted. No package failed, so nothing was classified against bb3f1c6b.

Run Result
1 PASS
2 (nocache=1) PASS (exit 0)
3 (nocache=1) PASS (exit 0)

Integration, btcd, this branch's tapd-itest (v0.8.5-4-gac101ed0). make itest with the supply and universe cases. Go splits -test.run on a top-level |, so a first filter that listed universe_|supply_commit_|... ran only the universe alternative. That run is not the supply result. The re-run puts the alternation in parentheses: (^universe_|supply_commit_|supply_verify_peer_node|fetch_supply_leaves). Exit 0, 518.74s.

Case Result
universe REST API PASS (28.28s)
universe sync PASS (72.70s)
universe delete leaf PASS (11.34s)
universe sync manual insert PASS (71.69s)
universe federation PASS (53.17s)
universe pagination simple PASS (13.25s)
supply commit ignore asset PASS (76.85s)
supply commit mint burn PASS (11.83s)
supply commit send before commit PASS (77.46s)
supply verify peer node PASS (48.09s)
fetch supply leaves PASS (53.08s)

The full make itest-parallel suite, including burn acceptance, is on #12 (a307dc0d). See that PR.

Without the write guard, TestTransferProofKeepsCustomGenesisPreCommit fails: expected anchor chain txn id 1, actual 2. The same tree's send-before-commit itest stops in UpdateAndMineSupplyCommit with unable to find txns in mempool: wanted 1, found 0. lightninglabs#2331 does not change upsertAssetGen. The anchor fix has to land first.

Upstream-main variant

Prepared on the fork only, not pushed to lightninglabs/taproot-assets, and no upstream PR is open.

Branch satscrip/supplycommit-anchor-fix-upstream-main, head 8ae788eb, based on upstream main 9db270f9 (Merge pull request lightninglabs#2329). Main's LatestMigrationVersion is 73, so the port is migration 74, which is after 72 and after the migration already on that main. Signing on main persists through AnchoringWatcher.Register and ApplyCommitTxStake; the rebuild test drives that stake against the harness queries.

make unit nocache=1 on the first port commit failed one expectation: TestSupplyCommitRestart/anchoring_updates_pending_starts_cycle still expected the restart tick to FetchState a second time. startAssetSM now copies the parked updates onto UpdatesPendingState, so the tick commits that in-memory batch. 8ae788eb updates that expectation. The test still requires the machine to reach CommitBroadcastState with the mint event. The follow-up make unit nocache=1 exited 0. No other package failed.

Commits

Unsigned. This environment cannot produce a GPG signature (commit.gpgsign is set; commits were created with --no-gpg-sign). Signed-off-by: Cursor Agent <cursoragent@cursor.com>.

  • 187fe043 tapdb: keep the genesis anchor on the mint transaction
  • 8f772951 tapdb: allow universe stats without a mint anchor
  • edf285de itest: count both sends in supply commit send before commit
  • ac101ed0 tapdb: prove migration 61 leaves valid commitments

Control: lightninglabs#2331 alone, and stock without the fix

supply commit send before commit is a new test on this branch. git grep for testSupplyCommitSendBeforeCommit on bb3f1c6b is empty. It was added in 187fe043, and edf285de sets the second send's transfer counts to 1, 2, 2.

Two throwaway branches were built locally to run it without the write guard. They are not pushed and have no pull request.

Tree Head Contents
cursor/stock-send-before-commit-ad51 0ad85408 bb3f1c6b plus only the new itest
cursor/2331-alone-v085-ad51 9f72c9e5 lightninglabs#2331 cherry-picks and the v0.8.5 adaptations (c21e3386, 9a514248, 38d7f763, 19febe6c, d0c26945) plus the same itest. anchorSpendsGenesis is absent

tapd-itest for the lightninglabs#2331-alone run embeds v0.8.5-6-g9f72c9e5. Cases ran serially with make itest-only.

Case Stock + test only (0ad85408) lightninglabs#2331 alone (9f72c9e5) With the anchor fix
supply_commit_send_before_commit FAIL (93.27s) FAIL (93.25s) PASS. This branch (ac101ed0) 77.46s. #12 (a307dc0d) includes that fix; make itest-parallel there exited 0, 117/117
supply_commit_mint_burn not run on the stock throwaway PASS (12.10s) PASS on this branch (11.83s) and inside #12's suite
supply_commit_ignore_asset not run on the stock throwaway PASS (37.08s) PASS on this branch (76.85s) and inside #12's suite

Both send-before-commit failures happen after the send itself succeeds. The harness then looks for the commitment in the mempool and finds nothing, because Bitcoin Core rejected it.

Stock (0ad85408), itest/utils.go:

test_harness.go:179: Publishing the first supply commitment after the send
utils.go:204: unable to find txns in mempool: wanted 1, found 0 txs in mempool: []
--- FAIL: TestTaprootAssetsDaemon/tranche00/104-of-117/supply_commit_send_before_commit (93.27s)

Stock btcd, output_btcd_chainbackend.log. Commitment input 0 spends the transfer at the pre-commit index:

Rejected transaction cfb3002fe51827cc884233ec5209fa705203d6094bfc7933f2e9cc048b6111f6: failed to validate input cfb3002fe51827cc884233ec5209fa705203d6094bfc7933f2e9cc048b6111f6:0 which references output b63dde18b2b7a8d0d0dc729dc23d44426c6428b1d8b5dcbee958c55385a56c3a:1

Stock lnd Alice:

Transaction cfb3002fe51827cc884233ec5209fa705203d6094bfc7933f2e9cc048b6111f6 not accepted by mempool: failed to validate input cfb3002fe51827cc884233ec5209fa705203d6094bfc7933f2e9cc048b6111f6:0 which references output b63dde18b2b7a8d0d0dc729dc23d44426c6428b1d8b5dcbee958c55385a56c3a:1
[/walletrpc.WalletKit/PublishTransaction]: undefined: failed to validate input cfb3002fe51827cc884233ec5209fa705203d6094bfc7933f2e9cc048b6111f6:0 which references output b63dde18b2b7a8d0d0dc729dc23d44426c6428b1d8b5dcbee958c55385a56c3a:1

lightninglabs#2331 alone (9f72c9e5) is the same failure. Serial harness:

test_harness.go:179: Publishing the first supply commitment after the send
utils.go:204: unable to find txns in mempool: wanted 1, found 0 txs in mempool: []
--- FAIL: TestTaprootAssetsDaemon/tranche00/104-of-117/supply_commit_send_before_commit (93.25s)

The following case cleared itest/regtest, so the Core and lnd lines below are from an immediate rerun of the same commit. That rerun failed the same way (73.27s). btcd: f818ead5 was accepted as a witness transaction (2 inputs, 3 outputs) and mined, and the commitment spends output 1 of it:

Accepted transaction f818ead5eb3f87f48798db12aaf8f079e35002f097b93d043699ed3e15927c1c (pool size: 1)
Sending tx (hash f818ead5eb3f87f48798db12aaf8f079e35002f097b93d043699ed3e15927c1c, 2 inputs, 3 outputs, lock height 0)
Rejected transaction 92ff6c6e6c2100a4561aeaee4253cbaeebab734c97f705421b3facc29a6cd2d7: failed to validate input 92ff6c6e6c2100a4561aeaee4253cbaeebab734c97f705421b3facc29a6cd2d7:0 which references output f818ead5eb3f87f48798db12aaf8f079e35002f097b93d043699ed3e15927c1c:1
CheckMempoolAcceptance: failed to validate input 92ff6c6e6c2100a4561aeaee4253cbaeebab734c97f705421b3facc29a6cd2d7:0 which references output f818ead5eb3f87f48798db12aaf8f079e35002f097b93d043699ed3e15927c1c:1

lnd Alice on that tree:

Transaction 92ff6c6e6c2100a4561aeaee4253cbaeebab734c97f705421b3facc29a6cd2d7 not accepted by mempool: failed to validate input 92ff6c6e6c2100a4561aeaee4253cbaeebab734c97f705421b3facc29a6cd2d7:0 which references output f818ead5eb3f87f48798db12aaf8f079e35002f097b93d043699ed3e15927c1c:1
[/walletrpc.WalletKit/PublishTransaction]: undefined: failed to validate input 92ff6c6e6c2100a4561aeaee4253cbaeebab734c97f705421b3facc29a6cd2d7:0 which references output f818ead5eb3f87f48798db12aaf8f079e35002f097b93d043699ed3e15927c1c:1

The prevout script on that rejection is 51206dd5210dbecf8c8c0c056b54abb07cc75f14657778a904ce594c5308b2c38847. The commitment witness does not satisfy it. Input 0 is transfer_txid:1 because upsertAssetGen still retargets genesis_points.anchor_tx_id onto the transfer. lightninglabs#2331 does not change that write, so send-before-commit still blocks when lightninglabs#2331 sits on stock v0.8.5. It passes once the anchor fix is underneath, which is the #12 stack.

B1 tapd on #12

Build from satscrip/supplycommit-burn-provenance at a307dc0de1fe04fbdfa213af62d516a8ae074898 (v0.8.5-9-ga307dc0d). make build tapd-debug sha256 e23ca4d86c92709e9e98ae9e72a504f9bdacb6320b117e5798762030fa14aeef. The itest tapd-itest that passed the 117-case suite is the same commit, sha256 bd952fc5d54525449b05e978edcdfc2a4a6c3d43e57126d48f3084afb96fd626.

Open in Web Open in Cursor 

cursoragent and others added 3 commits October 10, 2026 10:59
Transfer proofs share a genesis with the mint but carry a later
anchor transaction. upsertAssetGen wrote that transaction into
genesis_points.anchor_tx_id for every proof type, so supply
pre-commit resolution built commitment inputs against the transfer
txid.

Only anchor the genesis when the proof transaction spends the
genesis outpoint. Migration 61 restores anchors from the chain
transaction that spends each genesis outpoint, and abandons
never-confirmed supply commitments that spend neither a real
pre-commit nor a confirmed commitment outpoint so a restarted
node can rebuild them.

Signed-off-by: Cursor Agent <cursoragent@cursor.com>

Co-authored-by: Sadiq Jaffer <SadiqJaf@users.noreply.github.com>
QuerySyncStats treated genesis_points.anchor_tx_id as always set.
After transfer proofs stop retargeting that column, a genesis
point can have no mint transaction yet, and the stats query
returned "invalid hash length of 0". Leave the anchor outpoint
empty until the mint transaction is known.

Signed-off-by: Cursor Agent <cursoragent@cursor.com>

Co-authored-by: Sadiq Jaffer <SadiqJaf@users.noreply.github.com>
The second transfer is the issuer's second outbound transfer
and the recipient's second receive. The assertion asked for a
single transfer and timed out.

Signed-off-by: Cursor Agent <cursoragent@cursor.com>

Co-authored-by: Sadiq Jaffer <SadiqJaf@users.noreply.github.com>
A confirmed commitment stays, and so does an unconfirmed commitment
that spends a real pre-commit or a confirmed commitment outpoint.
Only a commitment whose inputs match neither is abandoned.

After that abandon, a restarted supply-commit manager rebuilds from
the pending updates and broadcasts a commitment that spends the
caller-selected pre-commit output. A custom-genesis mint at output
index 1 is covered, including an anchor that a transfer had
retargeted.

Signed-off-by: Cursor Agent <cursoragent@cursor.com>

Co-authored-by: Sadiq Jaffer <SadiqJaf@users.noreply.github.com>
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.

2 participants