Skip to content

[upstream draft] tapdb: keep the genesis anchor on the mint transaction - #13

Open
SadiqJaf wants to merge 6 commits into
mainfrom
satscrip/supplycommit-anchor-fix-upstream-main
Open

SadiqJaf wants to merge 6 commits into
mainfrom
satscrip/supplycommit-anchor-fix-upstream-main

Conversation

@SadiqJaf

@SadiqJaf SadiqJaf commented Oct 11, 2026 •

Copy link
Copy Markdown

Draft for review in Satscryption/taproot-assets only. This is not filed on lightninglabs/taproot-assets: no upstream issue, pull request, comment, or push.

Base is the fork's main, fast-forwarded to lightninglabs/taproot-assets main at 9db270f988fc359233fe09f15ed2d041aea3c1f0 (Merge pull request lightninglabs#2329, 2026-10-09). Head satscrip/supplycommit-anchor-fix-upstream-main is 3cca2127415dd9332a5fdb23ff3eddcbc5685da5. It carries the anchor-retarget write guard and migration 74, a supply-commit manager hydration fix for pending updates before the restart tick, and the regression tests.

The issue text below is the upstream-ready draft. It is in this pull request body and is not part of the code commits.


Title

Supply commitment spends the transfer outpoint after a transfer proof retargets the genesis anchor

Affected versions

  • v0.8.0. The same upsertAssetGen path calls AnchorGenesisPoint on every proof, including a transfer proof.
  • v0.8.5 (bb3f1c6b552109dcd3f24c2fb3939ec051bfd9e6, tag v0.8.5). Reproduced with the new supply commit send before commit itest on a tree that has that test and no write guard.
  • main at 9db270f988fc359233fe09f15ed2d041aea3c1f0. upsertAssetGen still calls AnchorGenesisPoint unconditionally (tapdb/universe.go:643). LatestMigrationVersion is 73.

lightninglabs#2331 does not fix this. Its diff does not change tapdb/universe.go, AnchorGenesisPoint, or FetchUnspentMintSupplyPreCommits. On v0.8.5, a tree with lightninglabs#2331's cherry-picks and the v0.8.5 adaptations, and without the write guard (9f72c9e5), fails supply_commit_send_before_commit the same way stock v0.8.5 does.

Summary

UpdateSupplyCommit can build a commitment whose input 0 spends a transfer output instead of the mint supply pre-commit. The pre-commit output index is still the mint's index, so the commitment spends <transfer txid>:<pre-commit index> and is signed with the mint pre-commit key. The node rejects that transaction. The commitment never confirms, so later supply updates, including burns, wait on a commitment that cannot finalize. Confirming the first supply commitment before any transfer proof is upserted spends <mint txid>:<pre-commit index> and does not hit this. That is what testSupplyCommitMintBurn does.

Minimal reproduction

  1. Mint a grouped asset with supply commitments enabled, and confirm the mint.
  2. Send some of the asset, and let the transfer proof be upserted on the issuer (proof delivery back to the issuer, or a universe sync that inserts the transfer leaf).
  3. Call the first UpdateSupplyCommit for that group key.
  4. The commitment transaction's input 0 spends the send transaction at the pre-commit output index. The mint pre-commit output is still unspent.
  5. The commitment is rejected and never appears in the mempool, so it never confirms.

What the rejection looks like

v0.8.5 itest, stock tree plus only the new test (0ad85408), no write guard. The send succeeds. The harness then finds no commitment in the mempool:

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)

btcd (output_btcd_chainbackend.log). Commitment input 0 references the transfer at output 1:

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

lnd:

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

The same test on the lightninglabs#2331-only tree (9f72c9e5) fails the same way (93.25s). btcd accepts the transfer (f818ead5, 2 inputs, 3 outputs), mines it, and rejects the commitment:

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:

Transaction 92ff6c6e6c2100a4561aeaee4253cbaeebab734c97f705421b3facc29a6cd2d7 not accepted by mempool: failed to validate input 92ff6c6e6c2100a4561aeaee4253cbaeebab734c97f705421b3facc29a6cd2d7:0 which references output f818ead5eb3f87f48798db12aaf8f079e35002f097b93d043699ed3e15927c1c:1

With the write guard, the same itest passes: 77.46s on the v0.8.5 anchor-fix branch (ac101ed0), and it is one of the 117 cases in the passing make itest-parallel on the stacked branch (a307dc0d). On this main-based branch it passes in ~59s (see verification below).

Root cause

On main at 9db270f9:

  • tapdb/sqlc/assets.sql.go AnchorGenesisPoint (lines 153-170) is an unconditional UPDATE genesis_points SET anchor_tx_id = $2 for the row whose prev_out is the genesis outpoint.
  • tapdb/universe.go universeUpsertProofLeaf (line 1138) calls upsertAssetGen (line 1259) for every proof type.
  • upsertAssetGen (line 572) stores the proof's AnchorTx and then calls AnchorGenesisPoint (line 643) with that chain-tx row. For an issuance proof that transaction is the mint. For a transfer proof it is the send. The genesis prevout stays the mint's input.
  • FetchUnspentMintSupplyPreCommits (tapdb/sqlc/queries/supply_commit.sql:296) joins chain_txns mint_txn ON gp.anchor_tx_id = mint_txn.txn_id (line 316) and returns that raw transaction with mint_supply_pre_commits.tx_output_index. The pre-commit row is not retargeted. Only the join key is.
  • newRootCommitment (universe/supplycommit/transitions.go:518) adds each pre-commit as an input and sets the witness UTXO to preCommit.MintingTxn.TxOut[preCommit.OutIdx] (line 548). After the retarget, MintingTxn is the transfer and OutIdx is still the mint pre-commit index.

Relation to other issues

Proposed fix

On satscrip/supplycommit-anchor-fix-upstream-main (this pull request), against the fork main above:

  • Call AnchorGenesisPoint only when the proof's anchor transaction spends asset.Genesis.FirstPrevOut. A transfer is still stored with UpsertChainTx. Re-upserting the issuance proof restores a pointer a transfer previously overwrote, because only the mint spends that outpoint.
  • Migration 74 (main is at 73) repairs genesis_points.anchor_tx_id when the current pointer does not spend the genesis outpoint and a unique spender is known. It only moves the anchor pointer; it does not move block-confirmation metadata that ConfirmChainTx may have written onto a retargeted chain_txns row (the mint row keeps its block fields; rebuild paths use raw_tx). When several stored chain transactions spend the same genesis outpoint, the migration prefers the mint batch's recorded mint transaction (from mint_supply_pre_commit outpoints), else the sole confirmed spender, else logs the ambiguity and leaves that genesis unchanged while continuing other rows (it never fails the whole migration for this case). It abandons a never-confirmed supply commitment whose inputs match none of the known pre-commit or confirmed-commitment outpoints, and leaves confirmed commitments and in-mempool spends of a real pre-commit or confirmed commitment outpoint in place. 000074_repair_genesis_anchor.down.sql is comment-only, matching other programmatic migrations (the repair is irreversible). After restart, pending updates are hydrated onto UpdatesPendingState and the next tick rebuilds a commitment that spends the mint pre-commit.
  • QuerySyncStats leaves AnchorPoint empty when anchor_txid is NULL, instead of failing chainhash.NewHash with invalid hash length of 0.

Regression coverage:

  • TestTransferProofKeepsCustomGenesisPreCommit upserts a mint proof and then a transfer proof. The genesis anchor and FetchUnspentMintSupplyPreCommits still return the mint transaction and the pre-commit index.
  • TestRepairGenesisAnchor covers mint-batch preference, confirmed-spender preference, and ambiguous multi-spender cases that leave the pointer unchanged.
  • TestMigration74CommitmentSafety and TestMigration74RebuildsValidCommitment cover a confirmed commitment, a mempool spend of a confirmed commitment, a custom-genesis pre-commit at caller index 1, and a rebuild that broadcasts a transaction spending that pre-commit.
  • supply commit send before commit is the itest above.

Verification on this branch

Head 3cca2127415dd9332a5fdb23ff3eddcbc5685da5.

make lint via the repo taproot-assets-tools Docker image (upstream make lint).

Command Result
make lint PASS
make migration-check PASS
make sqlc-check PASS
make unit pkg=tapdb nocache=1 (SQLite) PASS (ok tapdb 89.574s)
make unit dbbackend=postgres pkg=tapdb nocache=1 PASS (ok tapdb 385.228s)
make unit pkg=universe/supplycommit nocache=1 (SQLite) PASS (ok universe/supplycommit 0.251s)
make unit dbbackend=postgres pkg=universe/supplycommit nocache=1 PASS (ok universe/supplycommit 0.228s)
make itest icase=supply_commit_send_before_commit PASS (ok itest 58.936s)
--- PASS: TestTaprootAssetsDaemon (58.86s)
    --- PASS: TestTaprootAssetsDaemon/tranche00/115-of-128/supply_commit_send_before_commit (57.91s)
ok  	github.com/lightninglabs/taproot-assets/itest	58.936s

The itest minted, sent, published the first commitment, minted a second tranche, sent again, and published the next commitment. Both commitments confirmed. The mempool-empty failure did not occur.

Open in Web Open in Cursor 

@cursor
cursor Bot force-pushed the satscrip/supplycommit-anchor-fix-upstream-main branch 2 times, most recently from 7a2b70f to 3cca212 Compare October 11, 2026 15:32

Copy link
Copy Markdown
Author

@macroscope-app review this PR

@macroscopeapp

macroscopeapp Bot commented Oct 11, 2026

Copy link
Copy Markdown

Manual reviews triggered for commit 3cca212:

All prior checks · these links stay valid even if you push more commits.

@macroscopeapp

macroscopeapp Bot commented Oct 11, 2026

Copy link
Copy Markdown

Review started; results will be posted as check runs.

@SadiqJaf
SadiqJaf marked this pull request as ready for review October 11, 2026 20:21

Copy link
Copy Markdown
Author

/review-security

@SadiqJaf
SadiqJaf changed the base branch from cursor/upstream-main-mirror-ad51 to main October 12, 2026 07:12
@SadiqJaf SadiqJaf closed this Oct 12, 2026
@SadiqJaf SadiqJaf reopened this Oct 12, 2026
Port the v0.8.5 genesis-anchor repair onto current main. Main's latest
migration is 73, so this lands as migration 74.

A proof updates genesis_points.anchor_tx_id only when its anchor
transaction spends the genesis outpoint. Migration 74 retargets a
pointer a transfer already moved, and abandons a never-confirmed
commitment only when its inputs match neither a real pre-commit nor a
confirmed commitment outpoint. A restarted manager then rebuilds and
broadcasts a commitment that spends the caller-selected pre-commit,
including a custom-genesis mint at output index 1.

Signed-off-by: Sadiq Jaffer <sadiq.jaffer@satscryption.io>
startAssetSM copies a parked transition onto UpdatesPendingState, so
the restart tick commits that batch without fetching the record a
second time. The restart test expected the old empty-memory fetch.

Signed-off-by: Sadiq Jaffer <sadiq.jaffer@satscryption.io>
Mint, transfer, and only then publish the supply commitment. The
commitment must spend the mint pre-commit, not the transfer outpoint
at the same index.

Signed-off-by: Sadiq Jaffer <sadiq.jaffer@satscryption.io>
Signed-off-by: Sadiq Jaffer <sadiq.jaffer@satscryption.io>
@SadiqJaf
SadiqJaf force-pushed the satscrip/supplycommit-anchor-fix-upstream-main branch from 6e5b6ca to bd9f47f Compare October 12, 2026 08:39
…ycommit-anchor-fix-upstream-main

# Conflicts:
#	docs/release-notes/release-notes-0.9.0.md
#	tapdb/sqlc/querier.go
@SadiqJaf SadiqJaf closed this Nov 11, 2026
@SadiqJaf SadiqJaf reopened this Nov 11, 2026
@SadiqJaf SadiqJaf closed this Nov 12, 2026
@SadiqJaf SadiqJaf reopened this Nov 12, 2026
@SadiqJaf SadiqJaf closed this Nov 12, 2026
@SadiqJaf SadiqJaf reopened this Nov 12, 2026
Use a type conversion instead of a struct literal in the genesis anchor
rebuild test (staticcheck S1016), and add the missing testCaseSeconds
entry for the "supply commit send before commit" itest case.
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