From 9ce54ea57fa90f33483533818ef6d420628c047d Mon Sep 17 00:00:00 2001 From: Sadiq Jaffer Date: Mon, 12 Oct 2026 09:36:53 +0100 Subject: [PATCH 1/5] tapdb: keep the genesis anchor on the mint transaction 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 --- tapdb/genesis_anchor.go | 477 +++++++++++ tapdb/genesis_anchor_rebuild_test.go | 420 ++++++++++ tapdb/genesis_anchor_test.go | 783 ++++++++++++++++++ tapdb/migrations.go | 2 +- tapdb/programmatic_migrations.go | 8 + tapdb/sqlc/assets.sql.go | 73 ++ .../000074_repair_genesis_anchor.down.sql | 3 + .../000074_repair_genesis_anchor.up.sql | 5 + tapdb/sqlc/querier.go | 15 + tapdb/sqlc/queries/assets.sql | 11 + tapdb/sqlc/queries/supply_commit.sql | 85 ++ tapdb/sqlc/supply_commit.sql.go | 240 ++++++ tapdb/universe.go | 50 +- tapdb/universe_stats.go | 23 +- universe/supplycommit/hydrate_test.go | 36 + universe/supplycommit/manager.go | 28 + 16 files changed, 2240 insertions(+), 19 deletions(-) create mode 100644 tapdb/genesis_anchor.go create mode 100644 tapdb/genesis_anchor_rebuild_test.go create mode 100644 tapdb/genesis_anchor_test.go create mode 100644 tapdb/sqlc/migrations/000074_repair_genesis_anchor.down.sql create mode 100644 tapdb/sqlc/migrations/000074_repair_genesis_anchor.up.sql create mode 100644 universe/supplycommit/hydrate_test.go diff --git a/tapdb/genesis_anchor.go b/tapdb/genesis_anchor.go new file mode 100644 index 0000000000..e790d73b29 --- /dev/null +++ b/tapdb/genesis_anchor.go @@ -0,0 +1,477 @@ +package tapdb + +import ( + "bytes" + "context" + "database/sql" + "fmt" + + "github.com/btcsuite/btcd/chainhash/v2" + "github.com/btcsuite/btcd/wire/v2" + "github.com/lightninglabs/taproot-assets/tapdb/sqlc" + "github.com/lightninglabs/taproot-assets/universe/supplycommit" +) + +// storedChainTx is a decoded chain_txns row. +type storedChainTx struct { + id int64 + tx *wire.MsgTx + blockHeight sql.NullInt32 + blockHash []byte +} + +// repairGenesisAnchorMigration is migration 74. It restores +// genesis_points.anchor_tx_id after transfer proofs retargeted it, then +// drops never-confirmed supply commitments that spend neither a mint +// pre-commit nor a confirmed commitment outpoint. +func repairGenesisAnchorMigration(ctx context.Context, + db sqlc.Querier) error { + + if err := repairGenesisAnchors(ctx, db); err != nil { + return err + } + + return abandonInvalidUnconfirmedSupplyCommits(ctx, db) +} + +// repairGenesisAnchors points each genesis anchor at the chain +// transaction that spends the genesis outpoint. +// +// A genesis with no spending chain transaction is left unchanged: the +// issuance proof may not have been stored yet. When several chain +// transactions spend the genesis outpoint, the migration prefers the +// mint batch's recorded mint transaction, else the sole confirmed +// spender, else leaves the pointer unchanged and continues. An anchor +// that already spends the genesis outpoint is left in place. +// +// Block metadata that ConfirmChainTx wrote onto a retargeted anchor +// before the mint confirmed is not moved. Confirm-then-send leaves the +// mint's own chain_txns row with its block fields intact. +func repairGenesisAnchors(ctx context.Context, db sqlc.Querier) error { + points, err := db.GenesisPoints(ctx) + if err != nil { + return fmt.Errorf("listing genesis points: %w", err) + } + if len(points) == 0 { + return nil + } + + rows, err := db.FetchAllChainTxns(ctx) + if err != nil { + return fmt.Errorf("listing chain transactions: %w", err) + } + + byID := make(map[int64]*wire.MsgTx, len(rows)) + decoded := make([]storedChainTx, 0, len(rows)) + for _, row := range rows { + parsed := new(wire.MsgTx) + err := parsed.Deserialize(bytes.NewReader(row.RawTx)) + if err != nil { + return fmt.Errorf("decoding chain txn %d: %w", + row.TxnID, err) + } + + byID[row.TxnID] = parsed + decoded = append(decoded, storedChainTx{ + id: row.TxnID, + tx: parsed, + blockHeight: row.BlockHeight, + blockHash: row.BlockHash, + }) + } + + for _, point := range points { + err := repairOneGenesisAnchor(ctx, db, point, byID, decoded) + if err != nil { + return err + } + } + + return nil +} + +// repairOneGenesisAnchor repairs a single genesis point. +func repairOneGenesisAnchor(ctx context.Context, db sqlc.Querier, + point sqlc.GenesisPoint, byID map[int64]*wire.MsgTx, + decoded []storedChainTx) error { + + var genesis wire.OutPoint + err := readOutPoint(bytes.NewReader(point.PrevOut), 0, 0, &genesis) + if err != nil { + return fmt.Errorf("decoding genesis outpoint %d: %w", + point.GenesisID, err) + } + + if point.AnchorTxID.Valid { + current, ok := byID[point.AnchorTxID.Int64] + if ok && anchorSpendsGenesis(current, genesis) { + return nil + } + } + + var spenders []storedChainTx + for _, row := range decoded { + if anchorSpendsGenesis(row.tx, genesis) { + spenders = append(spenders, row) + } + } + + switch len(spenders) { + case 0: + return nil + + default: + spenderID, ok := resolveGenesisSpender( + ctx, db, point, genesis, spenders, + ) + if !ok { + return nil + } + + if point.AnchorTxID.Valid && + point.AnchorTxID.Int64 == spenderID { + + return nil + } + + err := db.AnchorGenesisPoint(ctx, sqlc.AnchorGenesisPointParams{ + PrevOut: point.PrevOut, + AnchorTxID: sql.NullInt64{ + Int64: spenderID, + Valid: true, + }, + }) + if err != nil { + return fmt.Errorf("retargeting genesis %d: %w", + point.GenesisID, err) + } + + log.Infof("Repaired genesis anchor %d to chain txn %d", + point.GenesisID, spenderID) + + return nil + } +} + +// resolveGenesisSpender picks the chain_txns row that should anchor a +// genesis outpoint when one or more stored transactions spend it. +func resolveGenesisSpender(ctx context.Context, db sqlc.Querier, + point sqlc.GenesisPoint, genesis wire.OutPoint, + spenders []storedChainTx) (int64, bool) { + + if len(spenders) == 1 { + return spenders[0].id, true + } + + batchHashes, err := mintBatchMintTxHashes(ctx, db, point.GenesisID) + if err != nil { + log.Warnf("Migration 74: genesis %d (%v): cannot read "+ + "mint batch mint tx: %v; leaving anchor unchanged", + point.GenesisID, genesis, err) + + return 0, false + } + + if len(batchHashes) > 0 { + var batchMatches []storedChainTx + for _, row := range spenders { + hash := row.tx.TxHash() + for _, want := range batchHashes { + if hash == want { + batchMatches = append( + batchMatches, row, + ) + break + } + } + } + + switch len(batchMatches) { + case 1: + return batchMatches[0].id, true + + case 0: + // Fall through to confirmed selection. + + default: + logAmbiguousGenesisSpenders( + point.GenesisID, genesis, spenders, + "several spenders match the mint batch mint tx", + ) + + return 0, false + } + } + + var confirmed []storedChainTx + for _, row := range spenders { + if chainTxConfirmed(row) { + confirmed = append(confirmed, row) + } + } + + switch len(confirmed) { + case 1: + return confirmed[0].id, true + + case 0: + logAmbiguousGenesisSpenders( + point.GenesisID, genesis, spenders, + "no unique mint-batch or confirmed spender", + ) + + return 0, false + + default: + logAmbiguousGenesisSpenders( + point.GenesisID, genesis, spenders, + "several confirmed spenders", + ) + + return 0, false + } +} + +// mintBatchMintTxHashes returns mint-transaction hashes recorded on +// mint_supply_pre_commit rows for the minting batch tied to genesisID. +func mintBatchMintTxHashes(ctx context.Context, db sqlc.Querier, + genesisID int64) ([]chainhash.Hash, error) { + + encoded, err := db.FetchMintPreCommitOutpointsByGenesisID( + ctx, sql.NullInt64{ + Int64: genesisID, + Valid: true, + }, + ) + if err != nil { + return nil, err + } + + seen := make(map[chainhash.Hash]struct{}) + var hashes []chainhash.Hash + for _, raw := range encoded { + var op wire.OutPoint + err := readOutPoint(bytes.NewReader(raw), 0, 0, &op) + if err != nil { + return nil, fmt.Errorf("decoding pre-commit "+ + "outpoint: %w", err) + } + + if _, ok := seen[op.Hash]; ok { + continue + } + + seen[op.Hash] = struct{}{} + hashes = append(hashes, op.Hash) + } + + return hashes, nil +} + +// chainTxConfirmed reports whether chain_txns block metadata is set. +func chainTxConfirmed(row storedChainTx) bool { + return row.blockHeight.Valid && len(row.blockHash) > 0 +} + +// logAmbiguousGenesisSpenders logs when migration 74 cannot pick a +// unique spender for a genesis outpoint. +func logAmbiguousGenesisSpenders(genesisID int64, genesis wire.OutPoint, + spenders []storedChainTx, reason string) { + + txids := make([]chainhash.Hash, len(spenders)) + for i, row := range spenders { + txids[i] = row.tx.TxHash() + } + + log.Warnf("Migration 74: genesis %d (%v) is spent by %d "+ + "chain transactions (%v); %s; leaving anchor_tx_id "+ + "unchanged", genesisID, genesis, len(spenders), txids, + reason) +} + +// abandonInvalidUnconfirmedSupplyCommits deletes never-confirmed +// CommitBroadcast commitments that do not spend a stored pre-commit +// outpoint or a confirmed supply-commitment outpoint. +// +// A valid unconfirmed commitment spends one of those outpoints and is +// left to be rebroadcast. When no known outpoints exist, nothing is +// abandoned: the migration cannot tell a bad input from a missing row. +// Supply update events on the transition are kept, and the state +// machine returns to UpdatesPendingState. +func abandonInvalidUnconfirmedSupplyCommits(ctx context.Context, + db sqlc.Querier) error { + + known, err := loadKnownSupplyOutpoints(ctx, db) + if err != nil { + return err + } + if len(known) == 0 { + return nil + } + + rows, err := db.FetchUnconfirmedBroadcastSupplyCommits(ctx) + if err != nil { + return fmt.Errorf("listing unconfirmed supply "+ + "commitments: %w", err) + } + + for _, row := range rows { + var tx wire.MsgTx + err := tx.Deserialize(bytes.NewReader(row.RawTx)) + if err != nil { + return fmt.Errorf("decoding supply commitment "+ + "%d: %w", row.CommitID, err) + } + if commitSpendsKnownOutpoint(&tx, known) { + continue + } + + err = abandonSupplyCommitment(ctx, db, row) + if err != nil { + return err + } + } + + return nil +} + +// loadKnownSupplyOutpoints returns mint pre-commit outpoints, remote +// pre-commit outpoints, and confirmed supply-commitment outpoints. +func loadKnownSupplyOutpoints(ctx context.Context, + db sqlc.Querier) (map[wire.OutPoint]struct{}, error) { + + known := make(map[wire.OutPoint]struct{}) + + mintOps, err := db.FetchMintSupplyPreCommitOutpoints(ctx) + if err != nil { + return nil, fmt.Errorf("listing mint pre-commit "+ + "outpoints: %w", err) + } + if err := addEncodedOutpoints(known, mintOps); err != nil { + return nil, err + } + + remoteOps, err := db.FetchRemoteSupplyPreCommitOutpoints(ctx) + if err != nil { + return nil, fmt.Errorf("listing remote pre-commit "+ + "outpoints: %w", err) + } + if err := addEncodedOutpoints(known, remoteOps); err != nil { + return nil, err + } + + confirmed, err := db.FetchConfirmedSupplyCommitOutpoints(ctx) + if err != nil { + return nil, fmt.Errorf("listing confirmed supply "+ + "commitments: %w", err) + } + for _, row := range confirmed { + if !row.OutputIndex.Valid || + len(row.Txid) != chainhash.HashSize { + + continue + } + + var hash chainhash.Hash + copy(hash[:], row.Txid) + op := wire.OutPoint{ + Hash: hash, + Index: uint32(row.OutputIndex.Int32), + } + known[op] = struct{}{} + } + + return known, nil +} + +// addEncodedOutpoints decodes wire outpoints into the set. +func addEncodedOutpoints(known map[wire.OutPoint]struct{}, + encoded [][]byte) error { + + for _, raw := range encoded { + var op wire.OutPoint + err := readOutPoint(bytes.NewReader(raw), 0, 0, &op) + if err != nil { + return fmt.Errorf("decoding outpoint: %w", err) + } + known[op] = struct{}{} + } + + return nil +} + +// commitSpendsKnownOutpoint reports whether any input spends a known +// supply outpoint. +func commitSpendsKnownOutpoint(tx *wire.MsgTx, + known map[wire.OutPoint]struct{}) bool { + + for _, in := range tx.TxIn { + if _, ok := known[in.PreviousOutPoint]; ok { + return true + } + } + + return false +} + +// abandonSupplyCommitment removes one never-confirmed commitment and +// returns its state machine to UpdatesPendingState. +func abandonSupplyCommitment(ctx context.Context, db sqlc.Querier, + row sqlc.FetchUnconfirmedBroadcastSupplyCommitsRow) error { + + commitID := sql.NullInt64{ + Int64: row.CommitID, + Valid: true, + } + + err := db.UnmarkMintPreCommitsSpentBy(ctx, commitID) + if err != nil { + return fmt.Errorf("clearing mint pre-commit spends: %w", err) + } + err = db.UnmarkRemotePreCommitsSpentBy(ctx, commitID) + if err != nil { + return fmt.Errorf("clearing remote pre-commit spends: %w", + err) + } + err = db.ClearSpentCommitmentRef(ctx, commitID) + if err != nil { + return fmt.Errorf("clearing spent commitment refs: %w", err) + } + err = db.ClearSupplyCommitmentRefs(ctx, commitID) + if err != nil { + return fmt.Errorf("detaching commitment %d: %w", + row.CommitID, err) + } + err = db.ClearPendingSupplyCommitTransition(ctx, row.TransitionID) + if err != nil { + return fmt.Errorf("clearing transition %d: %w", + row.TransitionID, err) + } + + stateName, err := stateToDBString( + &supplycommit.UpdatesPendingState{}, + ) + if err != nil { + return err + } + err = db.ResetSupplyCommitMachineForAbandon( + ctx, sqlc.ResetSupplyCommitMachineForAbandonParams{ + StateName: stateName, + LatestCommitmentID: commitID, + GroupKey: row.GroupKey, + }, + ) + if err != nil { + return fmt.Errorf("resetting state machine: %w", err) + } + + err = db.DeleteSupplyCommitment(ctx, row.CommitID) + if err != nil { + return fmt.Errorf("deleting supply commitment %d: %w", + row.CommitID, err) + } + + log.Infof("Abandoned unconfirmed supply commitment %d", row.CommitID) + + return nil +} diff --git a/tapdb/genesis_anchor_rebuild_test.go b/tapdb/genesis_anchor_rebuild_test.go new file mode 100644 index 0000000000..d7a5cc7386 --- /dev/null +++ b/tapdb/genesis_anchor_rebuild_test.go @@ -0,0 +1,420 @@ +package tapdb + +import ( + "bytes" + "context" + "net/url" + "testing" + "time" + + btcaddr "github.com/btcsuite/btcd/address/v2" + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcutil/v2/txsort" + "github.com/btcsuite/btcd/chaincfg/v2" + "github.com/btcsuite/btcd/chainhash/v2" + "github.com/btcsuite/btcd/psbt/v2" + "github.com/btcsuite/btcd/wire/v2" + "github.com/lightninglabs/taproot-assets/asset" + "github.com/lightninglabs/taproot-assets/fn" + "github.com/lightninglabs/taproot-assets/internal/test" + "github.com/lightninglabs/taproot-assets/proof" + "github.com/lightninglabs/taproot-assets/tapdb/sqlc" + "github.com/lightninglabs/taproot-assets/tapreorg" + "github.com/lightninglabs/taproot-assets/tapsend" + "github.com/lightninglabs/taproot-assets/universe/supplycommit" + "github.com/lightningnetwork/lnd/chainntnfs" + "github.com/lightningnetwork/lnd/keychain" + "github.com/lightningnetwork/lnd/lnwallet/chainfee" + "github.com/lightningnetwork/lnd/lnwire" + "github.com/stretchr/testify/require" +) + +// rebuildWallet funds and signs a commitment without dropping the +// pre-commit inputs the state machine already added. +type rebuildWallet struct{} + +func (rebuildWallet) FundPsbt(_ context.Context, packet *psbt.Packet, + _ uint32, _ chainfee.SatPerKWeight, _ int32) (*tapsend.FundedPsbt, + error) { + + funded := packet.UnsignedTx.Copy() + funded.AddTxIn(&wire.TxIn{ + PreviousOutPoint: wire.OutPoint{ + Hash: chainhash.Hash{0x01}, + Index: 0, + }, + }) + pkt, err := psbt.NewFromUnsignedTx(funded) + if err != nil { + return nil, err + } + + return &tapsend.FundedPsbt{ + Pkt: pkt, + ChangeOutputIndex: -1, + }, nil +} + +func (rebuildWallet) SignPsbt(_ context.Context, + pkt *psbt.Packet) (*psbt.Packet, error) { + + signed, err := psbt.NewFromUnsignedTx(pkt.UnsignedTx) + if err != nil { + return nil, err + } + // A one-element witness stack: item count, item length, item. + for i := range signed.Inputs { + signed.Inputs[i].FinalScriptWitness = []byte{0x01, 0x01, 0x01} + } + + txCopy := pkt.UnsignedTx.Copy() + txsort.InPlaceSort(txCopy) + signed.UnsignedTx = txCopy + + return signed, nil +} + +func (rebuildWallet) ImportTaprootOutput(_ context.Context, + pub *btcec.PublicKey) (btcaddr.Address, error) { + + return btcaddr.NewAddressTaproot( + pub.SerializeCompressed()[1:], &chaincfg.RegressionNetParams, + ) +} + +func (rebuildWallet) UnlockInput(context.Context, wire.OutPoint) error { + return nil +} + +type rebuildKeyRing struct{} + +func (rebuildKeyRing) DeriveNextTaprootAssetKey( + context.Context) (keychain.KeyDescriptor, error) { + + priv, err := btcec.NewPrivateKey() + if err != nil { + return keychain.KeyDescriptor{}, err + } + + return keychain.KeyDescriptor{ + KeyLocator: keychain.KeyLocator{ + Family: keychain.KeyFamily(212), + Index: 1, + }, + PubKey: priv.PubKey(), + }, nil +} + +// rebuildChain serves the two chain calls the commitment builder makes. +type rebuildChain struct{} + +func (rebuildChain) GenFileChainLookup(*proof.File) asset.ChainLookup { + return nil +} + +func (rebuildChain) GenProofChainLookup(*proof.Proof) (asset.ChainLookup, + error) { + + return nil, nil +} + +func (rebuildChain) RegisterConfirmationsNtfn(context.Context, + *chainhash.Hash, []byte, uint32, uint32, bool, + chan struct{}) (*chainntnfs.ConfirmationEvent, chan error, error) { + + return nil, nil, nil +} + +func (rebuildChain) RegisterBlockEpochNtfn(context.Context) (chan int32, + chan error, error) { + + return nil, nil, nil +} + +func (rebuildChain) GetBlock(context.Context, + chainhash.Hash) (*wire.MsgBlock, error) { + + return nil, nil +} + +func (rebuildChain) GetBlockByHeight(context.Context, + int64) (*wire.MsgBlock, error) { + + return nil, nil +} + +func (rebuildChain) GetBlockHash(context.Context, int64) (chainhash.Hash, + error) { + + return chainhash.Hash{}, nil +} + +func (rebuildChain) VerifyBlock(context.Context, wire.BlockHeader, + uint32) error { + + return nil +} + +func (rebuildChain) CurrentHeight(context.Context) (uint32, error) { + return 200, nil +} + +func (rebuildChain) GetBlockTimestamp(context.Context, uint32) (int64, + error) { + + return 0, nil +} + +func (rebuildChain) GetBlockHeaderByHeight(context.Context, + int64) (*wire.BlockHeader, error) { + + return nil, nil +} + +func (rebuildChain) PublishTransaction(context.Context, *wire.MsgTx, + string) error { + + return nil +} + +func (rebuildChain) EstimateFee(context.Context, + uint32) (chainfee.SatPerKWeight, error) { + + return chainfee.FeePerKwFloor, nil +} + +type rebuildDaemon struct { + broadcast chan *wire.MsgTx +} + +func (d rebuildDaemon) Start() error { return nil } + +func (d rebuildDaemon) Stop() error { return nil } + +func (d rebuildDaemon) BroadcastTransaction(tx *wire.MsgTx, + _ string) error { + + d.broadcast <- tx.Copy() + return nil +} + +func (d rebuildDaemon) RegisterConfirmationsNtfn(*chainhash.Hash, []byte, + uint32, uint32, ...chainntnfs.NotifierOption) ( + *chainntnfs.ConfirmationEvent, error) { + + return &chainntnfs.ConfirmationEvent{ + Confirmed: make(chan *chainntnfs.TxConfirmation), + }, nil +} + +func (d rebuildDaemon) RegisterSpendNtfn(*wire.OutPoint, []byte, + uint32) (*chainntnfs.SpendEvent, error) { + + return &chainntnfs.SpendEvent{ + Spend: make(chan *chainntnfs.SpendDetail), + }, nil +} + +func (d rebuildDaemon) SendMessages(btcec.PublicKey, + []lnwire.Message) error { + + return nil +} + +type rebuildIgnoreCache struct{} + +func (rebuildIgnoreCache) InvalidateCache(btcec.PublicKey) {} + +type rebuildAssetLookup struct{} + +func (rebuildAssetLookup) FetchSupplyCommitAssets(context.Context, + bool) ([]btcec.PublicKey, error) { + + return nil, nil +} + +func (rebuildAssetLookup) QueryAssetGroupByID(context.Context, + asset.ID) (*asset.AssetGroup, error) { + + return nil, nil +} + +func (rebuildAssetLookup) QueryAssetGroupByGroupKey(context.Context, + *btcec.PublicKey) (*asset.AssetGroup, error) { + + return nil, nil +} + +func (rebuildAssetLookup) FetchAssetMetaForAsset(context.Context, + asset.ID) (*proof.MetaReveal, error) { + + priv, err := btcec.NewPrivateKey() + if err != nil { + return nil, err + } + + return &proof.MetaReveal{ + UniverseCommitments: true, + DelegationKey: fn.Some(*priv.PubKey()), + }, nil +} + +func (rebuildAssetLookup) FetchInternalKeyLocator(context.Context, + *btcec.PublicKey) (keychain.KeyLocator, error) { + + return keychain.KeyLocator{}, nil +} + +type rebuildSyncer struct{} + +func (rebuildSyncer) PushSupplyCommitment(context.Context, asset.Specifier, + supplycommit.RootCommitment, supplycommit.SupplyLeaves, + supplycommit.ChainProof, []url.URL) (map[string]error, error) { + + return nil, nil +} + +// rebuildRegTx is the registration transaction the phase-1 stake +// writes through. The queries are the harness database, so the signed +// commitment lands where a restarted node would read it. +type rebuildRegTx struct { + q *sqlc.Queries +} + +func (r rebuildRegTx) Queries() *sqlc.Queries { + return r.q +} + +func (r rebuildRegTx) EnqueueEffect(context.Context, + tapreorg.OutboxEffect) error { + + return nil +} + +// rebuildWatcher runs the registration's phase-1 stake immediately. +// On current main the signed commitment is stored by that stake, not +// by a direct insert from the signing state. +type rebuildWatcher struct { + q *sqlc.Queries +} + +func (w rebuildWatcher) Register(ctx context.Context, + _ tapreorg.RegistrationSpec, phase1 func(context.Context, + tapreorg.RegistryTx, tapreorg.AnchoringID) error, +) (tapreorg.AnchoringID, error) { + + if phase1 != nil { + err := phase1(ctx, rebuildRegTx{q: w.q}, 1) + if err != nil { + return 0, err + } + } + + return 1, nil +} + +func (rebuildWatcher) AllAnchorings(context.Context, + tapreorg.SiteID) ([]*tapreorg.Anchoring, error) { + + return nil, nil +} + +func (rebuildWatcher) LookupByMatchKey(context.Context, tapreorg.SiteID, + []byte) (*tapreorg.Anchoring, error) { + + return nil, nil +} + +// TestMigration74RebuildsValidCommitment abandons a commitment that +// spends neither a pre-commit nor a confirmed commitment, then starts +// the supply-commit manager the way a restarted node does. The next +// tick rebuilds a commitment that spends the caller-selected pre-commit +// output and broadcasts it. +func TestMigration74RebuildsValidCommitment(t *testing.T) { + h := newSupplyCommitTestHarness(t) + const preCommitIdx = int32(1) + preOut, mintTx, mintChainID := insertCustomGenesisPreCommit( + t, h, preCommitIdx, + ) + transferID := retargetGenesisAnchor(t, h, mintTx) + assertGenesisAnchorID( + t, h, mintTx.TxIn[0].PreviousOutPoint, transferID, + ) + + err := h.commitMachine.InsertPendingUpdate( + h.ctx, h.assetSpec, h.randMintEvent(), + ) + require.NoError(t, err) + bogus := test.RandOp(t) + signedCommitTx(t, h, bogus) + h.assertCurrentStateIs(&supplycommit.CommitBroadcastState{}) + + err = repairGenesisAnchorMigration(h.ctx, h.db) + require.NoError(t, err) + + assertGenesisAnchorID( + t, h, mintTx.TxIn[0].PreviousOutPoint, mintChainID, + ) + h.assertCurrentStateIs(&supplycommit.UpdatesPendingState{}) + transition := h.assertPendingTransitionExists() + require.False(t, transition.NewCommitmentID.Valid) + require.False(t, transition.PendingCommitTxnID.Valid) + fetched := h.currentTransition().UnwrapOrFail(t) + require.Len(t, fetched.PendingUpdates, 1) + + daemon := rebuildDaemon{ + broadcast: make(chan *wire.MsgTx, 1), + } + mgr := supplycommit.NewManager(supplycommit.ManagerCfg{ + AnchoringWatcher: rebuildWatcher{ + q: h.baseDB.Queries, + }, + AnchoringThreshold: 6, + TreeView: h.commitTreeStore, + Commitments: h.commitMachine, + Wallet: rebuildWallet{}, + AssetLookup: rebuildAssetLookup{}, + KeyRing: rebuildKeyRing{}, + Chain: rebuildChain{}, + SupplySyncer: rebuildSyncer{}, + DaemonAdapters: daemon, + StateLog: h.commitMachine, + ChainParams: chaincfg.RegressionNetParams, + IgnoreCheckerCache: rebuildIgnoreCache{}, + }) + require.NoError(t, mgr.Start()) + defer func() { + require.NoError(t, mgr.Stop()) + }() + + err = mgr.SendEvent(h.ctx, h.assetSpec, &supplycommit.CommitTickEvent{}) + require.NoError(t, err) + + var broadcast *wire.MsgTx + select { + case broadcast = <-daemon.broadcast: + case <-time.After(20 * time.Second): + t.Fatal("node did not broadcast a rebuilt commitment") + } + + spendsPreCommit := false + for _, in := range broadcast.TxIn { + if in.PreviousOutPoint == preOut { + spendsPreCommit = true + } + require.NotEqual(t, bogus, in.PreviousOutPoint) + } + require.True(t, spendsPreCommit) + require.Equal(t, uint32(preCommitIdx), preOut.Index) + + h.assertCurrentStateIs(&supplycommit.CommitBroadcastState{}) + kept := h.assertPendingTransitionExists() + require.True(t, kept.NewCommitmentID.Valid) + + chainTx, err := h.fetchChainTxByID(kept.PendingCommitTxnID.Int64) + require.NoError(t, err) + var stored wire.MsgTx + err = stored.Deserialize(bytes.NewReader(chainTx.RawTx)) + require.NoError(t, err) + require.Equal(t, broadcast.TxHash(), stored.TxHash()) +} diff --git a/tapdb/genesis_anchor_test.go b/tapdb/genesis_anchor_test.go new file mode 100644 index 0000000000..b6ee5fb1be --- /dev/null +++ b/tapdb/genesis_anchor_test.go @@ -0,0 +1,783 @@ +package tapdb + +import ( + "bytes" + "context" + "database/sql" + "testing" + "time" + + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec/v2/schnorr" + "github.com/btcsuite/btcd/wire/v2" + "github.com/lightninglabs/taproot-assets/asset" + "github.com/lightninglabs/taproot-assets/internal/test" + "github.com/lightninglabs/taproot-assets/proof" + "github.com/lightninglabs/taproot-assets/tapdb/sqlc" + "github.com/lightninglabs/taproot-assets/universe/supplycommit" + "github.com/stretchr/testify/require" +) + +// customMintTx builds a mint transaction that spends the genesis outpoint +// as input 0, carries an extra input, and places the pre-commit at output +// index 1. That is the shape of a custom-genesis mint. +func customMintTx(t *testing.T, genesis wire.OutPoint) *wire.MsgTx { + t.Helper() + + tx := wire.NewMsgTx(2) + tx.AddTxIn(&wire.TxIn{PreviousOutPoint: genesis}) + tx.AddTxIn(&wire.TxIn{PreviousOutPoint: test.RandOp(t)}) + tx.AddTxOut(&wire.TxOut{ + Value: 1000, + PkScript: []byte{0x51, 0x20}, + }) + tx.AddTxOut(&wire.TxOut{ + Value: 2000, + PkScript: []byte{0x51, 0x21}, + }) + + return tx +} + +// transferSpendingMint spends a mint output and does not spend the genesis +// outpoint. +func transferSpendingMint(mint *wire.MsgTx) *wire.MsgTx { + tx := wire.NewMsgTx(2) + tx.AddTxIn(&wire.TxIn{ + PreviousOutPoint: wire.OutPoint{ + Hash: mint.TxHash(), + Index: 0, + }, + }) + tx.AddTxOut(&wire.TxOut{ + Value: 900, + PkScript: []byte{0x00}, + }) + + return tx +} + +// anchorProof wraps an anchor transaction in the proof upsertAssetGen reads. +func anchorProof(t *testing.T, genesis asset.Genesis, + anchor wire.MsgTx) *proof.Proof { + + t.Helper() + + proofAsset := asset.RandAsset(t, asset.Normal) + proofAsset.Genesis = genesis + proofAsset.GroupKey = nil + proofAsset.PrevWitnesses = nil + + return &proof.Proof{ + BlockHeader: wire.BlockHeader{ + Timestamp: time.Unix(1, 0), + }, + BlockHeight: 100, + AnchorTx: anchor, + Asset: *proofAsset, + } +} + +// genesisAnchorID returns the chain_txns id stored for the genesis outpoint. +func genesisAnchorID(t *testing.T, db sqlc.Querier, + genesis wire.OutPoint) int64 { + + t.Helper() + + ctx := context.Background() + encoded, err := encodeOutpoint(genesis) + require.NoError(t, err) + + points, err := db.GenesisPoints(ctx) + require.NoError(t, err) + + for _, point := range points { + if bytes.Equal(point.PrevOut, encoded) { + require.True(t, point.AnchorTxID.Valid) + return point.AnchorTxID.Int64 + } + } + + t.Fatalf("genesis outpoint %v not found", genesis) + return 0 +} + +// TestTransferProofKeepsCustomGenesisPreCommit upserts a custom-genesis +// mint proof and then a transfer proof. The genesis anchor and the +// unspent pre-commit query must still return the mint transaction and +// the caller-selected output index. +func TestTransferProofKeepsCustomGenesisPreCommit(t *testing.T) { + t.Parallel() + + ctx := context.Background() + db := NewTestDB(t) + + genesis := asset.RandGenesis(t, asset.Normal) + mintTx := customMintTx(t, genesis.FirstPrevOut) + const preCommitIdx = int32(1) + + _, err := upsertAssetGen( + ctx, db, genesis, nil, + anchorProof(t, genesis, *mintTx), + ) + require.NoError(t, err) + + mintTxid := mintTx.TxHash() + mintChain, err := db.FetchChainTx(ctx, mintTxid[:]) + require.NoError(t, err) + require.Equal(t, mintChain.TxnID, genesisAnchorID( + t, db, genesis.FirstPrevOut, + )) + + groupPriv, err := btcec.NewPrivateKey() + require.NoError(t, err) + groupKey := schnorr.SerializePubKey(groupPriv.PubKey()) + insertMintPreCommit(t, db, genesis, mintTx, preCommitIdx, groupKey) + + transfer := transferSpendingMint(mintTx) + _, err = upsertAssetGen( + ctx, db, genesis, nil, + anchorProof(t, genesis, *transfer), + ) + require.NoError(t, err) + + require.Equal(t, mintChain.TxnID, genesisAnchorID( + t, db, genesis.FirstPrevOut, + )) + + rows, err := db.FetchUnspentMintSupplyPreCommits(ctx, groupKey) + require.NoError(t, err) + require.Len(t, rows, 1) + require.Equal(t, preCommitIdx, rows[0].TxOutputIndex) + + var got wire.MsgTx + err = got.Deserialize(bytes.NewReader(rows[0].RawTx)) + require.NoError(t, err) + require.Equal(t, mintTx.TxHash(), got.TxHash()) +} + +// insertMintPreCommit stores a local mint pre-commit at the given index +// of mintTx, bound to the genesis already inserted for that mint. +func insertMintPreCommit(t *testing.T, db sqlc.Querier, genesis asset.Genesis, + mintTx *wire.MsgTx, outIdx int32, groupKey []byte) { + + t.Helper() + + ctx := context.Background() + batchDesc, _ := test.RandKeyDesc(t) + batchKey := batchDesc.PubKey.SerializeCompressed() + batchID, err := db.UpsertInternalKey(ctx, sqlc.UpsertInternalKeyParams{ + RawKey: batchKey, + KeyFamily: int32(batchDesc.Family), + KeyIndex: int32(batchDesc.Index), + }) + require.NoError(t, err) + + genBytes, err := encodeOutpoint(genesis.FirstPrevOut) + require.NoError(t, err) + genID, err := db.UpsertGenesisPoint(ctx, genBytes) + require.NoError(t, err) + + err = db.NewMintingBatch(ctx, sqlc.NewMintingBatchParams{ + BatchID: batchID, + HeightHint: 100, + CreationTimeUnix: time.Now(), + }) + require.NoError(t, err) + + _, err = db.BindMintingBatchWithTx( + ctx, sqlc.BindMintingBatchWithTxParams{ + RawKey: batchKey, + GenesisID: sqlInt64(genID), + }, + ) + require.NoError(t, err) + + internalDesc, _ := test.RandKeyDesc(t) + internalID, err := db.UpsertInternalKey( + ctx, sqlc.UpsertInternalKeyParams{ + RawKey: internalDesc.PubKey.SerializeCompressed(), + KeyFamily: int32( + internalDesc.KeyLocator.Family, + ), + KeyIndex: int32(internalDesc.KeyLocator.Index), + }, + ) + require.NoError(t, err) + + outpoint := wire.OutPoint{ + Hash: mintTx.TxHash(), + Index: uint32(outIdx), + } + var buf bytes.Buffer + err = wire.WriteOutPoint(&buf, 0, 0, &outpoint) + require.NoError(t, err) + + _, err = db.UpsertMintSupplyPreCommit( + ctx, sqlc.UpsertMintSupplyPreCommitParams{ + TxOutputIndex: outIdx, + TaprootInternalKeyID: internalID, + GroupKey: groupKey, + Outpoint: buf.Bytes(), + BatchKey: batchKey, + }, + ) + require.NoError(t, err) +} + +// confirmChainTx sets block metadata on a stored chain transaction. +func confirmChainTx(t *testing.T, db sqlc.Querier, tx *wire.MsgTx) { + t.Helper() + + txid := tx.TxHash() + blockHash := test.RandHash() + err := db.ConfirmChainAnchorTx( + context.Background(), sqlc.ConfirmChainAnchorTxParams{ + Txid: txid[:], + BlockHeight: sql.NullInt32{ + Int32: 100, + Valid: true, + }, + BlockHash: blockHash[:], + TxIndex: sql.NullInt32{ + Int32: 0, + Valid: true, + }, + }, + ) + require.NoError(t, err) +} + +// upsertChainTx inserts a transaction and returns its chain_txns id. +func upsertChainTx(t *testing.T, db sqlc.Querier, tx *wire.MsgTx) int64 { + t.Helper() + + raw, err := encodeTx(tx) + require.NoError(t, err) + txid := tx.TxHash() + id, err := db.UpsertChainTx( + context.Background(), sqlc.UpsertChainTxParams{ + Txid: txid[:], + RawTx: raw, + }, + ) + require.NoError(t, err) + + return id +} + +// anchorGenesisTo points the genesis outpoint at a chain_txns row. +func anchorGenesisTo(t *testing.T, db sqlc.Querier, genesis wire.OutPoint, + chainID int64) { + + t.Helper() + + encoded, err := encodeOutpoint(genesis) + require.NoError(t, err) + err = db.AnchorGenesisPoint( + context.Background(), sqlc.AnchorGenesisPointParams{ + PrevOut: encoded, + AnchorTxID: sql.NullInt64{ + Int64: chainID, + Valid: true, + }, + }, + ) + require.NoError(t, err) +} + +// TestRepairGenesisAnchor restores a retargeted anchor from the mint +// transaction still stored in chain_txns, leaves a genesis with no +// spender alone, and resolves ambiguous multi-spender cases. +func TestRepairGenesisAnchor(t *testing.T) { + t.Parallel() + + ctx := context.Background() + + t.Run("restores mint spend", func(t *testing.T) { + db := NewTestDB(t) + genesis := asset.RandGenesis(t, asset.Normal) + mintTx := customMintTx(t, genesis.FirstPrevOut) + + _, err := upsertAssetGen( + ctx, db, genesis, nil, + anchorProof(t, genesis, *mintTx), + ) + require.NoError(t, err) + mintID := genesisAnchorID(t, db, genesis.FirstPrevOut) + + bogus := wire.NewMsgTx(2) + bogus.AddTxIn(&wire.TxIn{PreviousOutPoint: test.RandOp(t)}) + bogus.AddTxOut(&wire.TxOut{Value: 1, PkScript: []byte{0x51}}) + bogusID := upsertChainTx(t, db, bogus) + anchorGenesisTo(t, db, genesis.FirstPrevOut, bogusID) + require.Equal(t, bogusID, genesisAnchorID( + t, db, genesis.FirstPrevOut, + )) + + require.NoError(t, repairGenesisAnchors(ctx, db)) + require.Equal(t, mintID, genesisAnchorID( + t, db, genesis.FirstPrevOut, + )) + + require.NoError(t, repairGenesisAnchors(ctx, db)) + require.Equal(t, mintID, genesisAnchorID( + t, db, genesis.FirstPrevOut, + )) + }) + + t.Run("no spender is left unchanged", func(t *testing.T) { + db := NewTestDB(t) + genesis := asset.RandGenesis(t, asset.Normal) + encoded, err := encodeOutpoint(genesis.FirstPrevOut) + require.NoError(t, err) + _, err = db.UpsertGenesisPoint(ctx, encoded) + require.NoError(t, err) + + bogus := wire.NewMsgTx(2) + bogus.AddTxIn(&wire.TxIn{PreviousOutPoint: test.RandOp(t)}) + bogus.AddTxOut(&wire.TxOut{Value: 1, PkScript: []byte{0x51}}) + bogusID := upsertChainTx(t, db, bogus) + anchorGenesisTo(t, db, genesis.FirstPrevOut, bogusID) + + require.NoError(t, repairGenesisAnchors(ctx, db)) + require.Equal(t, bogusID, genesisAnchorID( + t, db, genesis.FirstPrevOut, + )) + }) + + t.Run("prefers mint batch spender", func(t *testing.T) { + db := NewTestDB(t) + genesis := asset.RandGenesis(t, asset.Normal) + mintTx := customMintTx(t, genesis.FirstPrevOut) + + _, err := upsertAssetGen( + ctx, db, genesis, nil, + anchorProof(t, genesis, *mintTx), + ) + require.NoError(t, err) + mintID := genesisAnchorID(t, db, genesis.FirstPrevOut) + + groupPriv, err := btcec.NewPrivateKey() + require.NoError(t, err) + groupKey := schnorr.SerializePubKey(groupPriv.PubKey()) + insertMintPreCommit(t, db, genesis, mintTx, 1, groupKey) + + transfer := transferSpendingMint(mintTx) + transfer.AddTxIn(&wire.TxIn{ + PreviousOutPoint: genesis.FirstPrevOut, + }) + transferID := upsertChainTx(t, db, transfer) + + bogus := wire.NewMsgTx(2) + bogus.AddTxIn(&wire.TxIn{PreviousOutPoint: test.RandOp(t)}) + bogus.AddTxOut(&wire.TxOut{Value: 1, PkScript: []byte{0x51}}) + bogusID := upsertChainTx(t, db, bogus) + anchorGenesisTo(t, db, genesis.FirstPrevOut, bogusID) + require.Equal(t, bogusID, genesisAnchorID( + t, db, genesis.FirstPrevOut, + )) + + require.NoError(t, repairGenesisAnchors(ctx, db)) + require.Equal(t, mintID, genesisAnchorID( + t, db, genesis.FirstPrevOut, + )) + require.NotEqual(t, transferID, mintID) + }) + + t.Run("prefers confirmed spender", func(t *testing.T) { + db := NewTestDB(t) + genesis := asset.RandGenesis(t, asset.Normal) + encoded, err := encodeOutpoint(genesis.FirstPrevOut) + require.NoError(t, err) + _, err = db.UpsertGenesisPoint(ctx, encoded) + require.NoError(t, err) + + unconfirmed := wire.NewMsgTx(2) + unconfirmed.AddTxIn(&wire.TxIn{ + PreviousOutPoint: genesis.FirstPrevOut, + }) + unconfirmed.AddTxOut(&wire.TxOut{ + Value: 100, PkScript: []byte{0x01}, + }) + upsertChainTx(t, db, unconfirmed) + + confirmed := wire.NewMsgTx(2) + confirmed.AddTxIn(&wire.TxIn{ + PreviousOutPoint: genesis.FirstPrevOut, + }) + confirmed.AddTxOut(&wire.TxOut{ + Value: 200, PkScript: []byte{0x02}, + }) + confirmedID := upsertChainTx(t, db, confirmed) + confirmChainTx(t, db, confirmed) + + bogus := wire.NewMsgTx(2) + bogus.AddTxIn(&wire.TxIn{PreviousOutPoint: test.RandOp(t)}) + bogus.AddTxOut(&wire.TxOut{Value: 1, PkScript: []byte{0x51}}) + bogusID := upsertChainTx(t, db, bogus) + anchorGenesisTo(t, db, genesis.FirstPrevOut, bogusID) + + require.NoError(t, repairGenesisAnchors(ctx, db)) + require.Equal(t, confirmedID, genesisAnchorID( + t, db, genesis.FirstPrevOut, + )) + }) + + t.Run("ambiguous spenders leave anchor unchanged", func(t *testing.T) { + db := NewTestDB(t) + genesis := asset.RandGenesis(t, asset.Normal) + encoded, err := encodeOutpoint(genesis.FirstPrevOut) + require.NoError(t, err) + _, err = db.UpsertGenesisPoint(ctx, encoded) + require.NoError(t, err) + + bogus := wire.NewMsgTx(2) + bogus.AddTxIn(&wire.TxIn{PreviousOutPoint: test.RandOp(t)}) + bogus.AddTxOut(&wire.TxOut{Value: 1, PkScript: []byte{0x51}}) + bogusID := upsertChainTx(t, db, bogus) + anchorGenesisTo(t, db, genesis.FirstPrevOut, bogusID) + + for i := 0; i < 2; i++ { + spender := wire.NewMsgTx(2) + spender.AddTxIn(&wire.TxIn{ + PreviousOutPoint: genesis.FirstPrevOut, + }) + spender.AddTxOut(&wire.TxOut{ + Value: int64(100 + i), + PkScript: []byte{byte(i + 1)}, + }) + upsertChainTx(t, db, spender) + } + + require.NoError(t, repairGenesisAnchors(ctx, db)) + require.Equal(t, bogusID, genesisAnchorID( + t, db, genesis.FirstPrevOut, + )) + }) +} + +// signedCommitTx inserts a signed commitment that spends prevOut and +// returns that transaction. +func signedCommitTx(t *testing.T, h *supplyCommitTestHarness, + prevOut wire.OutPoint) *wire.MsgTx { + + t.Helper() + + tx := wire.NewMsgTx(2) + tx.AddTxIn(&wire.TxIn{PreviousOutPoint: prevOut}) + tx.AddTxOut(&wire.TxOut{Value: 1000, PkScript: []byte{0x51}}) + + internalKey, _ := test.RandKeyDesc(t) + err := h.commitMachine.InsertSignedCommitTx( + h.ctx, h.assetSpec, supplycommit.SupplyCommitTxn{ + Txn: tx, + InternalKey: internalKey, + OutputKey: test.RandPubKey(t), + OutputIndex: 0, + }, + ) + require.NoError(t, err) + + return tx +} + +// TestAbandonInvalidUnconfirmedSupplyCommit drops a broadcast commitment +// that spends neither the mint pre-commit nor a confirmed commitment, +// and keeps the pending update. A commitment that spends the real +// pre-commit is left in CommitBroadcastState. +func TestAbandonInvalidUnconfirmedSupplyCommit(t *testing.T) { + t.Parallel() + + t.Run("abandons unknown input", func(t *testing.T) { + h := newSupplyCommitTestHarness(t) + batchKey, _, mintTx, _, _ := h.addTestMintingBatch() + _, _ = h.addTestMintAnchorUniCommitment( + batchKey, sql.NullInt64{}, mintTx.TxHash(), + ) + + err := h.commitMachine.InsertPendingUpdate( + h.ctx, h.assetSpec, h.randMintEvent(), + ) + require.NoError(t, err) + + signedCommitTx(t, h, test.RandOp(t)) + h.assertCurrentStateIs(&supplycommit.CommitBroadcastState{}) + + err = abandonInvalidUnconfirmedSupplyCommits(h.ctx, h.db) + require.NoError(t, err) + + h.assertCurrentStateIs(&supplycommit.UpdatesPendingState{}) + transition := h.assertPendingTransitionExists() + require.False(t, transition.NewCommitmentID.Valid) + require.False(t, transition.PendingCommitTxnID.Valid) + require.False(t, transition.Frozen) + + fetched := h.currentTransition().UnwrapOrFail(t) + require.Nil(t, fetched.NewCommitment.Txn) + require.Len(t, fetched.PendingUpdates, 1) + + left, err := h.db.FetchUnconfirmedBroadcastSupplyCommits( + h.ctx, + ) + require.NoError(t, err) + require.Empty(t, left) + }) + + t.Run("keeps pre-commit spend", func(t *testing.T) { + h := newSupplyCommitTestHarness(t) + batchKey, _, mintTx, _, _ := h.addTestMintingBatch() + _, preOut := h.addTestMintAnchorUniCommitment( + batchKey, sql.NullInt64{}, mintTx.TxHash(), + ) + + err := h.commitMachine.InsertPendingUpdate( + h.ctx, h.assetSpec, h.randMintEvent(), + ) + require.NoError(t, err) + + signedCommitTx(t, h, preOut) + err = abandonInvalidUnconfirmedSupplyCommits(h.ctx, h.db) + require.NoError(t, err) + + h.assertCurrentStateIs(&supplycommit.CommitBroadcastState{}) + transition := h.assertPendingTransitionExists() + require.True(t, transition.NewCommitmentID.Valid) + }) +} + +// confirmBroadcastTx writes block metadata onto the signed commitment +// transaction. The state machine stays in CommitBroadcastState. +func confirmBroadcastTx(h *supplyCommitTestHarness, tx *wire.MsgTx) { + h.t.Helper() + + raw, err := encodeTx(tx) + require.NoError(h.t, err) + txid := tx.TxHash() + transition := h.assertPendingTransitionExists() + require.True(h.t, transition.PendingCommitTxnID.Valid) + h.confirmChainTx(transition.PendingCommitTxnID.Int64, txid[:], raw) +} + +// insertCustomGenesisPreCommit stores a mint whose pre-commit output is +// at outIdx, then points the genesis anchor at that mint transaction. +// The returned outpoint is mint_txid:outIdx. +func insertCustomGenesisPreCommit(t *testing.T, h *supplyCommitTestHarness, + outIdx int32) (wire.OutPoint, *wire.MsgTx, int64) { + + t.Helper() + + genesis := asset.RandGenesis(t, asset.Normal) + mintTx := customMintTx(t, genesis.FirstPrevOut) + raw, err := encodeTx(mintTx) + require.NoError(t, err) + mintTxid := mintTx.TxHash() + mintChainID, err := h.db.UpsertChainTx( + h.ctx, sqlc.UpsertChainTxParams{ + Txid: mintTxid[:], + RawTx: raw, + }, + ) + require.NoError(t, err) + + insertMintPreCommit( + t, h.db, genesis, mintTx, outIdx, h.groupKeyBytes, + ) + + encoded, err := encodeOutpoint(genesis.FirstPrevOut) + require.NoError(t, err) + err = h.db.AnchorGenesisPoint( + h.ctx, sqlc.AnchorGenesisPointParams{ + PrevOut: encoded, + AnchorTxID: sql.NullInt64{ + Int64: mintChainID, + Valid: true, + }, + }, + ) + require.NoError(t, err) + + preOut := wire.OutPoint{ + Hash: mintTxid, + Index: uint32(outIdx), + } + return preOut, mintTx, mintChainID +} + +// retargetGenesisAnchor points the mint's genesis outpoint at a transfer +// that does not spend it. That is the anchor-retarget corruption. +func retargetGenesisAnchor(t *testing.T, h *supplyCommitTestHarness, + mintTx *wire.MsgTx) int64 { + + t.Helper() + + transfer := transferSpendingMint(mintTx) + raw, err := encodeTx(transfer) + require.NoError(t, err) + txid := transfer.TxHash() + transferID, err := h.db.UpsertChainTx( + h.ctx, sqlc.UpsertChainTxParams{ + Txid: txid[:], + RawTx: raw, + }, + ) + require.NoError(t, err) + + encoded, err := encodeOutpoint(mintTx.TxIn[0].PreviousOutPoint) + require.NoError(t, err) + err = h.db.AnchorGenesisPoint( + h.ctx, sqlc.AnchorGenesisPointParams{ + PrevOut: encoded, + AnchorTxID: sql.NullInt64{ + Int64: transferID, + Valid: true, + }, + }, + ) + require.NoError(t, err) + + return transferID +} + +// assertGenesisAnchorID asserts the genesis outpoint's anchor_tx_id. +func assertGenesisAnchorID(t *testing.T, h *supplyCommitTestHarness, + genesis wire.OutPoint, chainID int64) { + + t.Helper() + + encoded, err := encodeOutpoint(genesis) + require.NoError(t, err) + points, err := h.db.GenesisPoints(h.ctx) + require.NoError(t, err) + + for _, point := range points { + if bytes.Equal(point.PrevOut, encoded) { + require.True(t, point.AnchorTxID.Valid) + require.Equal(t, chainID, point.AnchorTxID.Int64) + return + } + } + + t.Fatalf("genesis outpoint %v not found", genesis) +} + +// TestMigration74CommitmentSafety checks the abandon rule against a +// confirmed commitment and against an unconfirmed commitment that spends +// a confirmed commitment outpoint. +func TestMigration74CommitmentSafety(t *testing.T) { + t.Parallel() + + t.Run("keeps confirmed commitment", func(t *testing.T) { + h := newSupplyCommitTestHarness(t) + batchKey, _, mintTx, _, _ := h.addTestMintingBatch() + _, _ = h.addTestMintAnchorUniCommitment( + batchKey, sql.NullInt64{}, mintTx.TxHash(), + ) + + err := h.commitMachine.InsertPendingUpdate( + h.ctx, h.assetSpec, h.randMintEvent(), + ) + require.NoError(t, err) + + // The input is not a pre-commit. Confirmation is what + // keeps the row: the migration only considers commitments + // whose chain transaction has no block hash. + tx := signedCommitTx(t, h, test.RandOp(t)) + confirmBroadcastTx(h, tx) + + err = repairGenesisAnchorMigration(h.ctx, h.db) + require.NoError(t, err) + + h.assertCurrentStateIs(&supplycommit.CommitBroadcastState{}) + kept := h.assertPendingTransitionExists() + require.True(t, kept.NewCommitmentID.Valid) + _, err = h.fetchCommitmentByID(kept.NewCommitmentID.Int64) + require.NoError(t, err) + + left, err := h.db.FetchUnconfirmedBroadcastSupplyCommits( + h.ctx, + ) + require.NoError(t, err) + require.Empty(t, left) + }) + + t.Run("keeps mempool spend of confirmed commit", func(t *testing.T) { + h := newSupplyCommitTestHarness(t) + + prior := wire.NewMsgTx(2) + prior.AddTxIn(&wire.TxIn{ + PreviousOutPoint: test.RandOp(t), + }) + prior.AddTxOut(&wire.TxOut{ + Value: 1000, + PkScript: []byte{0x51}, + }) + raw, err := encodeTx(prior) + require.NoError(t, err) + txid := prior.TxHash() + chainID, err := h.db.UpsertChainTx( + h.ctx, sqlc.UpsertChainTxParams{ + Txid: txid[:], + RawTx: raw, + }, + ) + require.NoError(t, err) + h.addTestSupplyCommitment(chainID, txid[:], raw, true) + + spent := wire.OutPoint{Hash: txid, Index: 0} + err = h.commitMachine.InsertPendingUpdate( + h.ctx, h.assetSpec, h.randMintEvent(), + ) + require.NoError(t, err) + signedCommitTx(t, h, spent) + + err = repairGenesisAnchorMigration(h.ctx, h.db) + require.NoError(t, err) + + h.assertCurrentStateIs(&supplycommit.CommitBroadcastState{}) + kept := h.assertPendingTransitionExists() + require.True(t, kept.NewCommitmentID.Valid) + require.True(t, kept.PendingCommitTxnID.Valid) + }) + + t.Run("keeps custom genesis pre-commit spend", func(t *testing.T) { + h := newSupplyCommitTestHarness(t) + const preCommitIdx = int32(1) + preOut, mintTx, mintChainID := insertCustomGenesisPreCommit( + t, h, preCommitIdx, + ) + require.Equal(t, uint32(preCommitIdx), preOut.Index) + + transferID := retargetGenesisAnchor(t, h, mintTx) + assertGenesisAnchorID( + t, h, mintTx.TxIn[0].PreviousOutPoint, transferID, + ) + + err := h.commitMachine.InsertPendingUpdate( + h.ctx, h.assetSpec, h.randMintEvent(), + ) + require.NoError(t, err) + signedCommitTx(t, h, preOut) + + err = repairGenesisAnchorMigration(h.ctx, h.db) + require.NoError(t, err) + + assertGenesisAnchorID( + t, h, mintTx.TxIn[0].PreviousOutPoint, mintChainID, + ) + h.assertCurrentStateIs(&supplycommit.CommitBroadcastState{}) + kept := h.assertPendingTransitionExists() + require.True(t, kept.NewCommitmentID.Valid) + + rows, err := h.db.FetchUnspentMintSupplyPreCommits( + h.ctx, h.groupKeyBytes, + ) + require.NoError(t, err) + require.NotEmpty(t, rows) + require.Equal(t, preCommitIdx, rows[0].TxOutputIndex) + var got wire.MsgTx + err = got.Deserialize(bytes.NewReader(rows[0].RawTx)) + require.NoError(t, err) + require.Equal(t, mintTx.TxHash(), got.TxHash()) + }) +} diff --git a/tapdb/migrations.go b/tapdb/migrations.go index 019b8330f9..f8f4e6b676 100644 --- a/tapdb/migrations.go +++ b/tapdb/migrations.go @@ -24,7 +24,7 @@ const ( // daemon. // // NOTE: This MUST be updated when a new migration is added. - LatestMigrationVersion = 73 + LatestMigrationVersion = 74 ) // DatabaseBackend is an interface that contains all methods our different diff --git a/tapdb/programmatic_migrations.go b/tapdb/programmatic_migrations.go index 9b24b85c59..efff55bf0a 100644 --- a/tapdb/programmatic_migrations.go +++ b/tapdb/programmatic_migrations.go @@ -40,6 +40,12 @@ const ( // for proof files written before the index existed. Proof files are // recursive values, so this cannot be expressed as portable SQL. Migration72BackfillProofProvenance = 72 + + // Migration74RepairGenesisAnchor restores genesis_points.anchor_tx_id + // after a transfer proof retargeted it, then abandons never-confirmed + // supply commitments that spend neither a pre-commit nor a confirmed + // commitment outpoint. + Migration74RepairGenesisAnchor = 74 ) // programmaticMigration is a function type for a function that performs a @@ -59,6 +65,8 @@ var ( Migration65BackfillEventKeys: backfillSupplyUpdateEventKeys, Migration72BackfillProofProvenance: backfillProofProvenance, + + Migration74RepairGenesisAnchor: repairGenesisAnchorMigration, } ) diff --git a/tapdb/sqlc/assets.sql.go b/tapdb/sqlc/assets.sql.go index c9e76fb0ae..1db18a298e 100644 --- a/tapdb/sqlc/assets.sql.go +++ b/tapdb/sqlc/assets.sql.go @@ -629,6 +629,48 @@ func (q *Queries) FetchAllAssetMeta(ctx context.Context) ([]FetchAllAssetMetaRow return items, nil } +const FetchAllChainTxns = `-- name: FetchAllChainTxns :many +SELECT txn_id, txid, raw_tx, block_height, block_hash +FROM chain_txns +` + +type FetchAllChainTxnsRow struct { + TxnID int64 + Txid []byte + RawTx []byte + BlockHeight sql.NullInt32 + BlockHash []byte +} + +func (q *Queries) FetchAllChainTxns(ctx context.Context) ([]FetchAllChainTxnsRow, error) { + rows, err := q.db.QueryContext(ctx, FetchAllChainTxns) + if err != nil { + return nil, err + } + defer rows.Close() + var items []FetchAllChainTxnsRow + for rows.Next() { + var i FetchAllChainTxnsRow + if err := rows.Scan( + &i.TxnID, + &i.Txid, + &i.RawTx, + &i.BlockHeight, + &i.BlockHash, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const FetchAssetID = `-- name: FetchAssetID :many SELECT asset_id FROM assets @@ -1903,6 +1945,37 @@ func (q *Queries) FetchManagedUTXOs(ctx context.Context) ([]FetchManagedUTXOsRow return items, nil } +const FetchMintPreCommitOutpointsByGenesisID = `-- name: FetchMintPreCommitOutpointsByGenesisID :many +SELECT msp.outpoint +FROM mint_supply_pre_commits msp +JOIN asset_minting_batches amb ON msp.batch_id = amb.batch_id +WHERE amb.genesis_id = $1 + AND msp.outpoint IS NOT NULL +` + +func (q *Queries) FetchMintPreCommitOutpointsByGenesisID(ctx context.Context, genesisID sql.NullInt64) ([][]byte, error) { + rows, err := q.db.QueryContext(ctx, FetchMintPreCommitOutpointsByGenesisID, genesisID) + if err != nil { + return nil, err + } + defer rows.Close() + var items [][]byte + for rows.Next() { + var outpoint []byte + if err := rows.Scan(&outpoint); err != nil { + return nil, err + } + items = append(items, outpoint) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const FetchMintSupplyPreCommits = `-- name: FetchMintSupplyPreCommits :many SELECT precommits.id, diff --git a/tapdb/sqlc/migrations/000074_repair_genesis_anchor.down.sql b/tapdb/sqlc/migrations/000074_repair_genesis_anchor.down.sql new file mode 100644 index 0000000000..28b223b8cf --- /dev/null +++ b/tapdb/sqlc/migrations/000074_repair_genesis_anchor.down.sql @@ -0,0 +1,3 @@ +-- The genesis-anchor repair and invalid-commitment cleanup cannot be +-- reversed in isolation. This file exists only so the migration runner +-- can step back through version 73. diff --git a/tapdb/sqlc/migrations/000074_repair_genesis_anchor.up.sql b/tapdb/sqlc/migrations/000074_repair_genesis_anchor.up.sql new file mode 100644 index 0000000000..9bde3f5d3b --- /dev/null +++ b/tapdb/sqlc/migrations/000074_repair_genesis_anchor.up.sql @@ -0,0 +1,5 @@ +-- Programmatic migration. repairGenesisAnchorMigration points each +-- genesis anchor at the chain transaction that spends the genesis +-- outpoint, then abandons never-confirmed supply commitments whose +-- inputs are not a real pre-commit or confirmed commitment outpoint. +-- This file exists so the migration version is picked up. diff --git a/tapdb/sqlc/querier.go b/tapdb/sqlc/querier.go index 53aa15fb30..7a13b8f4d8 100644 --- a/tapdb/sqlc/querier.go +++ b/tapdb/sqlc/querier.go @@ -61,8 +61,11 @@ type Querier interface { // the same message is still recognized as one. substr yields an empty value of // the column's own type on both database backends. ClearAuthMailboxMessagePayload(ctx context.Context, arg ClearAuthMailboxMessagePayloadParams) (int64, error) + ClearPendingSupplyCommitTransition(ctx context.Context, transitionID int64) error // A certified foreclosure is absorbing and never cleared. ClearReorgDependencyForeclosure(ctx context.Context, arg ClearReorgDependencyForeclosureParams) error + ClearSpentCommitmentRef(ctx context.Context, spentCommitment sql.NullInt64) error + ClearSupplyCommitmentRefs(ctx context.Context, oldCommitmentID sql.NullInt64) error ConfirmChainAnchorTx(ctx context.Context, arg ConfirmChainAnchorTxParams) error ConfirmChainTx(ctx context.Context, arg ConfirmChainTxParams) error CountAuthMailboxMessages(ctx context.Context) (int64, error) @@ -158,6 +161,7 @@ type Querier interface { FetchAddrEventProofs(ctx context.Context, addrEventID int64) ([]FetchAddrEventProofsRow, error) FetchAddrs(ctx context.Context, arg FetchAddrsParams) ([]FetchAddrsRow, error) FetchAllAssetMeta(ctx context.Context) ([]FetchAllAssetMetaRow, error) + FetchAllChainTxns(ctx context.Context) ([]FetchAllChainTxnsRow, error) FetchAllNodes(ctx context.Context) ([]MssmtNode, error) FetchAssetID(ctx context.Context, arg FetchAssetIDParams) ([]int64, error) FetchAssetMeta(ctx context.Context, metaID int64) (FetchAssetMetaRow, error) @@ -203,6 +207,7 @@ type Querier interface { // independent of the size of the subtree beneath the node. FetchChildren(ctx context.Context, arg FetchChildrenParams) ([]MssmtNode, error) FetchChildrenSelfJoin(ctx context.Context, arg FetchChildrenSelfJoinParams) ([]FetchChildrenSelfJoinRow, error) + FetchConfirmedSupplyCommitOutpoints(ctx context.Context) ([]FetchConfirmedSupplyCommitOutpointsRow, error) FetchGenesisByAssetID(ctx context.Context, assetID []byte) (GenesisInfoView, error) FetchGenesisByGroupKey(ctx context.Context, tweakedGroupKey []byte) (GenesisInfoView, error) FetchGenesisByID(ctx context.Context, genAssetID int64) (FetchGenesisByIDRow, error) @@ -230,6 +235,8 @@ type Querier interface { FetchLiveReorgParentsByCandidate(ctx context.Context, spenderTxid []byte) ([]int64, error) FetchManagedUTXO(ctx context.Context, arg FetchManagedUTXOParams) (FetchManagedUTXORow, error) FetchManagedUTXOs(ctx context.Context) ([]FetchManagedUTXOsRow, error) + FetchMintPreCommitOutpointsByGenesisID(ctx context.Context, genesisID sql.NullInt64) ([][]byte, error) + FetchMintSupplyPreCommitOutpoints(ctx context.Context) ([][]byte, error) // Fetch records from the supply_pre_commits table with optional // filtering. FetchMintSupplyPreCommits(ctx context.Context, arg FetchMintSupplyPreCommitsParams) ([]FetchMintSupplyPreCommitsRow, error) @@ -242,6 +249,7 @@ type Querier interface { FetchMultiverseRoot(ctx context.Context, namespaceRoot string) (FetchMultiverseRootRow, error) FetchOrphanManagedUTXOs(ctx context.Context, arg FetchOrphanManagedUTXOsParams) ([]FetchOrphanManagedUTXOsRow, error) FetchPeerAcceptedBuyPeerByScid(ctx context.Context, scid int64) ([]byte, error) + FetchRemoteSupplyPreCommitOutpoints(ctx context.Context) ([][]byte, error) FetchReorgAnchoring(ctx context.Context, id int64) (ReorgAnchoring, error) FetchReorgCandidateSpends(ctx context.Context, anchoringID int64) ([]ReorgCandidateSpend, error) FetchReorgDependencyEdgesByChild(ctx context.Context, childID int64) ([]ReorgDependency, error) @@ -281,6 +289,10 @@ type Querier interface { FetchTapscriptTree(ctx context.Context, rootHash []byte) ([]FetchTapscriptTreeRow, error) FetchTransferInputs(ctx context.Context, transferID int64) ([]FetchTransferInputsRow, error) FetchTransferOutputs(ctx context.Context, transferID int64) ([]FetchTransferOutputsRow, error) + // Never-confirmed commitments whose state machine is still waiting to + // broadcast. Used to drop a commitment that cannot confirm because its + // inputs are not real pre-commit or prior-commitment outpoints. + FetchUnconfirmedBroadcastSupplyCommits(ctx context.Context) ([]FetchUnconfirmedBroadcastSupplyCommitsRow, error) FetchUnindexedAssetProofs(ctx context.Context, rowLimit int32) ([]FetchUnindexedAssetProofsRow, error) // Note on hash construction: mssmt_nodes.hash_key on a compacted leaf // commits to the subtree root at that leaf's tree height, which @@ -496,6 +508,7 @@ type Querier interface { // Their expected-output rows (addr_event_outputs) stand: they // describe the address's expectation, not materialized state. ResetAddrEventsByAnchorTx(ctx context.Context, arg ResetAddrEventsByAnchorTxParams) (int64, error) + ResetSupplyCommitMachineForAbandon(ctx context.Context, arg ResetSupplyCommitMachineForAbandonParams) error // The inverse of ReAnchorPassiveAssets: restore the anchor UTXO and // the spend-template fields that the re-anchor reset. RestoreAssetSpendTemplate(ctx context.Context, arg RestoreAssetSpendTemplateParams) error @@ -572,6 +585,8 @@ type Querier interface { UniverseLeaves(ctx context.Context) ([]UniverseLeafe, error) UniverseRoots(ctx context.Context, arg UniverseRootsParams) ([]UniverseRootsRow, error) UniverseRootsAfterID(ctx context.Context, arg UniverseRootsAfterIDParams) ([]UniverseRootsAfterIDRow, error) + UnmarkMintPreCommitsSpentBy(ctx context.Context, spentBy sql.NullInt64) error + UnmarkRemotePreCommitsSpentBy(ctx context.Context, spentBy sql.NullInt64) error // The inverse of SupersedeConflictingTransfers for one rivalry loser, // applied when the transfer that superseded it is abandoned: the loser // becomes live again, provided no confirmed transfer conflicts with it diff --git a/tapdb/sqlc/queries/assets.sql b/tapdb/sqlc/queries/assets.sql index 02de659454..2e1ecb80a1 100644 --- a/tapdb/sqlc/queries/assets.sql +++ b/tapdb/sqlc/queries/assets.sql @@ -1444,3 +1444,14 @@ WHERE asset_id = (SELECT asset_id FROM target_asset) AND claimant_txn.block_hash IS NOT NULL ) RETURNING assets.asset_id; + +-- name: FetchAllChainTxns :many +SELECT txn_id, txid, raw_tx, block_height, block_hash +FROM chain_txns; + +-- name: FetchMintPreCommitOutpointsByGenesisID :many +SELECT msp.outpoint +FROM mint_supply_pre_commits msp +JOIN asset_minting_batches amb ON msp.batch_id = amb.batch_id +WHERE amb.genesis_id = $1 + AND msp.outpoint IS NOT NULL; diff --git a/tapdb/sqlc/queries/supply_commit.sql b/tapdb/sqlc/queries/supply_commit.sql index 8469d7f252..3777a19960 100644 --- a/tapdb/sqlc/queries/supply_commit.sql +++ b/tapdb/sqlc/queries/supply_commit.sql @@ -393,3 +393,88 @@ WHERE commit_id = @commit_id; SELECT sqlc.embed(t) FROM supply_commit_transitions t WHERE t.new_commitment_id = @new_commitment_id; + +-- name: FetchUnconfirmedBroadcastSupplyCommits :many +-- Never-confirmed commitments whose state machine is still waiting to +-- broadcast. Used to drop a commitment that cannot confirm because its +-- inputs are not real pre-commit or prior-commitment outpoints. +SELECT + sm.group_key, + sc.commit_id, + t.transition_id, + ct.raw_tx +FROM supply_commit_state_machines sm +JOIN supply_commit_states states + ON sm.current_state_id = states.id +JOIN supply_commit_transitions t + ON t.state_machine_group_key = sm.group_key + AND t.finalized = FALSE +JOIN supply_commitments sc + ON t.new_commitment_id = sc.commit_id +JOIN chain_txns ct + ON sc.chain_txn_id = ct.txn_id +WHERE states.state_name = 'CommitBroadcastState' + AND ct.block_hash IS NULL; + +-- name: FetchMintSupplyPreCommitOutpoints :many +SELECT outpoint +FROM mint_supply_pre_commits +WHERE outpoint IS NOT NULL; + +-- name: FetchRemoteSupplyPreCommitOutpoints :many +SELECT outpoint +FROM supply_pre_commits +WHERE outpoint IS NOT NULL; + +-- name: FetchConfirmedSupplyCommitOutpoints :many +SELECT ct.txid, sc.output_index +FROM supply_commitments sc +JOIN chain_txns ct + ON sc.chain_txn_id = ct.txn_id +WHERE ct.block_hash IS NOT NULL + AND sc.output_index IS NOT NULL; + +-- name: ClearPendingSupplyCommitTransition :exec +UPDATE supply_commit_transitions +SET new_commitment_id = NULL, + pending_commit_txn_id = NULL, + frozen = FALSE +WHERE transition_id = $1; + +-- name: ClearSupplyCommitmentRefs :exec +UPDATE supply_commit_transitions +SET old_commitment_id = CASE + WHEN old_commitment_id = $1 THEN NULL + ELSE old_commitment_id + END, + new_commitment_id = CASE + WHEN new_commitment_id = $1 THEN NULL + ELSE new_commitment_id + END; + +-- name: UnmarkMintPreCommitsSpentBy :exec +UPDATE mint_supply_pre_commits +SET spent_by = NULL +WHERE spent_by = $1; + +-- name: UnmarkRemotePreCommitsSpentBy :exec +UPDATE supply_pre_commits +SET spent_by = NULL +WHERE spent_by = $1; + +-- name: ClearSpentCommitmentRef :exec +UPDATE supply_commitments +SET spent_commitment = NULL +WHERE spent_commitment = $1; + +-- name: ResetSupplyCommitMachineForAbandon :exec +UPDATE supply_commit_state_machines +SET current_state_id = ( + SELECT id FROM supply_commit_states + WHERE state_name = $1 + ), + latest_commitment_id = CASE + WHEN latest_commitment_id = $2 THEN NULL + ELSE latest_commitment_id + END +WHERE group_key = $3; diff --git a/tapdb/sqlc/supply_commit.sql.go b/tapdb/sqlc/supply_commit.sql.go index ae2b634cf8..0b8dd6934d 100644 --- a/tapdb/sqlc/supply_commit.sql.go +++ b/tapdb/sqlc/supply_commit.sql.go @@ -10,6 +10,47 @@ import ( "database/sql" ) +const ClearPendingSupplyCommitTransition = `-- name: ClearPendingSupplyCommitTransition :exec +UPDATE supply_commit_transitions +SET new_commitment_id = NULL, + pending_commit_txn_id = NULL, + frozen = FALSE +WHERE transition_id = $1 +` + +func (q *Queries) ClearPendingSupplyCommitTransition(ctx context.Context, transitionID int64) error { + _, err := q.db.ExecContext(ctx, ClearPendingSupplyCommitTransition, transitionID) + return err +} + +const ClearSpentCommitmentRef = `-- name: ClearSpentCommitmentRef :exec +UPDATE supply_commitments +SET spent_commitment = NULL +WHERE spent_commitment = $1 +` + +func (q *Queries) ClearSpentCommitmentRef(ctx context.Context, spentCommitment sql.NullInt64) error { + _, err := q.db.ExecContext(ctx, ClearSpentCommitmentRef, spentCommitment) + return err +} + +const ClearSupplyCommitmentRefs = `-- name: ClearSupplyCommitmentRefs :exec +UPDATE supply_commit_transitions +SET old_commitment_id = CASE + WHEN old_commitment_id = $1 THEN NULL + ELSE old_commitment_id + END, + new_commitment_id = CASE + WHEN new_commitment_id = $1 THEN NULL + ELSE new_commitment_id + END +` + +func (q *Queries) ClearSupplyCommitmentRefs(ctx context.Context, oldCommitmentID sql.NullInt64) error { + _, err := q.db.ExecContext(ctx, ClearSupplyCommitmentRefs, oldCommitmentID) + return err +} + const DeleteSupplyCommitTransition = `-- name: DeleteSupplyCommitTransition :exec DELETE FROM supply_commit_transitions WHERE transition_id = $1 @@ -71,6 +112,43 @@ func (q *Queries) FetchChainTxByID(ctx context.Context, txnID int64) (FetchChain return i, err } +const FetchConfirmedSupplyCommitOutpoints = `-- name: FetchConfirmedSupplyCommitOutpoints :many +SELECT ct.txid, sc.output_index +FROM supply_commitments sc +JOIN chain_txns ct + ON sc.chain_txn_id = ct.txn_id +WHERE ct.block_hash IS NOT NULL + AND sc.output_index IS NOT NULL +` + +type FetchConfirmedSupplyCommitOutpointsRow struct { + Txid []byte + OutputIndex sql.NullInt32 +} + +func (q *Queries) FetchConfirmedSupplyCommitOutpoints(ctx context.Context) ([]FetchConfirmedSupplyCommitOutpointsRow, error) { + rows, err := q.db.QueryContext(ctx, FetchConfirmedSupplyCommitOutpoints) + if err != nil { + return nil, err + } + defer rows.Close() + var items []FetchConfirmedSupplyCommitOutpointsRow + for rows.Next() { + var i FetchConfirmedSupplyCommitOutpointsRow + if err := rows.Scan(&i.Txid, &i.OutputIndex); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const FetchInternalKeyByID = `-- name: FetchInternalKeyByID :one SELECT raw_key, key_family, key_index FROM internal_keys @@ -90,6 +168,64 @@ func (q *Queries) FetchInternalKeyByID(ctx context.Context, keyID int64) (FetchI return i, err } +const FetchMintSupplyPreCommitOutpoints = `-- name: FetchMintSupplyPreCommitOutpoints :many +SELECT outpoint +FROM mint_supply_pre_commits +WHERE outpoint IS NOT NULL +` + +func (q *Queries) FetchMintSupplyPreCommitOutpoints(ctx context.Context) ([][]byte, error) { + rows, err := q.db.QueryContext(ctx, FetchMintSupplyPreCommitOutpoints) + if err != nil { + return nil, err + } + defer rows.Close() + var items [][]byte + for rows.Next() { + var outpoint []byte + if err := rows.Scan(&outpoint); err != nil { + return nil, err + } + items = append(items, outpoint) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const FetchRemoteSupplyPreCommitOutpoints = `-- name: FetchRemoteSupplyPreCommitOutpoints :many +SELECT outpoint +FROM supply_pre_commits +WHERE outpoint IS NOT NULL +` + +func (q *Queries) FetchRemoteSupplyPreCommitOutpoints(ctx context.Context) ([][]byte, error) { + rows, err := q.db.QueryContext(ctx, FetchRemoteSupplyPreCommitOutpoints) + if err != nil { + return nil, err + } + defer rows.Close() + var items [][]byte + for rows.Next() { + var outpoint []byte + if err := rows.Scan(&outpoint); err != nil { + return nil, err + } + items = append(items, outpoint) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const FetchSupplyCommit = `-- name: FetchSupplyCommit :one SELECT sc.commit_id, sc.group_key, sc.chain_txn_id, sc.output_index, sc.internal_key_id, sc.output_key, sc.block_header, sc.block_height, sc.merkle_proof, sc.supply_root_hash, sc.supply_root_sum, sc.spent_commitment, txn.tx_index @@ -196,6 +332,64 @@ func (q *Queries) FetchSupplyUpdateEventsForBackfill(ctx context.Context, numLim return items, nil } +const FetchUnconfirmedBroadcastSupplyCommits = `-- name: FetchUnconfirmedBroadcastSupplyCommits :many +SELECT + sm.group_key, + sc.commit_id, + t.transition_id, + ct.raw_tx +FROM supply_commit_state_machines sm +JOIN supply_commit_states states + ON sm.current_state_id = states.id +JOIN supply_commit_transitions t + ON t.state_machine_group_key = sm.group_key + AND t.finalized = FALSE +JOIN supply_commitments sc + ON t.new_commitment_id = sc.commit_id +JOIN chain_txns ct + ON sc.chain_txn_id = ct.txn_id +WHERE states.state_name = 'CommitBroadcastState' + AND ct.block_hash IS NULL +` + +type FetchUnconfirmedBroadcastSupplyCommitsRow struct { + GroupKey []byte + CommitID int64 + TransitionID int64 + RawTx []byte +} + +// Never-confirmed commitments whose state machine is still waiting to +// broadcast. Used to drop a commitment that cannot confirm because its +// inputs are not real pre-commit or prior-commitment outpoints. +func (q *Queries) FetchUnconfirmedBroadcastSupplyCommits(ctx context.Context) ([]FetchUnconfirmedBroadcastSupplyCommitsRow, error) { + rows, err := q.db.QueryContext(ctx, FetchUnconfirmedBroadcastSupplyCommits) + if err != nil { + return nil, err + } + defer rows.Close() + var items []FetchUnconfirmedBroadcastSupplyCommitsRow + for rows.Next() { + var i FetchUnconfirmedBroadcastSupplyCommitsRow + if err := rows.Scan( + &i.GroupKey, + &i.CommitID, + &i.TransitionID, + &i.RawTx, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const FetchUnspentMintSupplyPreCommits = `-- name: FetchUnspentMintSupplyPreCommits :many SELECT mac.tx_output_index, @@ -1027,6 +1221,30 @@ func (q *Queries) QuerySupplyUpdateEvents(ctx context.Context, transitionID sql. return items, nil } +const ResetSupplyCommitMachineForAbandon = `-- name: ResetSupplyCommitMachineForAbandon :exec +UPDATE supply_commit_state_machines +SET current_state_id = ( + SELECT id FROM supply_commit_states + WHERE state_name = $1 + ), + latest_commitment_id = CASE + WHEN latest_commitment_id = $2 THEN NULL + ELSE latest_commitment_id + END +WHERE group_key = $3 +` + +type ResetSupplyCommitMachineForAbandonParams struct { + StateName string + LatestCommitmentID sql.NullInt64 + GroupKey []byte +} + +func (q *Queries) ResetSupplyCommitMachineForAbandon(ctx context.Context, arg ResetSupplyCommitMachineForAbandonParams) error { + _, err := q.db.ExecContext(ctx, ResetSupplyCommitMachineForAbandon, arg.StateName, arg.LatestCommitmentID, arg.GroupKey) + return err +} + const SetSupplyUpdateEventKey = `-- name: SetSupplyUpdateEventKey :exec UPDATE supply_update_events SET event_key = $1 @@ -1057,6 +1275,28 @@ func (q *Queries) UnbindSupplyUpdateEvents(ctx context.Context, transitionID sql return err } +const UnmarkMintPreCommitsSpentBy = `-- name: UnmarkMintPreCommitsSpentBy :exec +UPDATE mint_supply_pre_commits +SET spent_by = NULL +WHERE spent_by = $1 +` + +func (q *Queries) UnmarkMintPreCommitsSpentBy(ctx context.Context, spentBy sql.NullInt64) error { + _, err := q.db.ExecContext(ctx, UnmarkMintPreCommitsSpentBy, spentBy) + return err +} + +const UnmarkRemotePreCommitsSpentBy = `-- name: UnmarkRemotePreCommitsSpentBy :exec +UPDATE supply_pre_commits +SET spent_by = NULL +WHERE spent_by = $1 +` + +func (q *Queries) UnmarkRemotePreCommitsSpentBy(ctx context.Context, spentBy sql.NullInt64) error { + _, err := q.db.ExecContext(ctx, UnmarkRemotePreCommitsSpentBy, spentBy) + return err +} + const UpdateSupplyCommitTransitionCommitment = `-- name: UpdateSupplyCommitTransitionCommitment :exec UPDATE supply_commit_transitions SET new_commitment_id = $1, diff --git a/tapdb/universe.go b/tapdb/universe.go index 5ba6347fa5..d04d6b6eef 100644 --- a/tapdb/universe.go +++ b/tapdb/universe.go @@ -634,17 +634,29 @@ func upsertAssetGen(ctx context.Context, db UpsertAssetStore, return 0, fmt.Errorf("unable to upsert chain tx: %w", err) } - // Finally, we'll anchor the genesis point to link to the chain - // transaction we upserted above. - genesisPoint, err := encodeOutpoint(assetGen.FirstPrevOut) - if err != nil { - return 0, fmt.Errorf("unable to encode genesis point: %w", err) - } - if err := db.AnchorGenesisPoint(ctx, GenesisPointAnchor{ - PrevOut: genesisPoint, - AnchorTxID: sqlInt64(chainTXID), - }); err != nil { - return 0, fmt.Errorf("unable to anchor genesis tx: %w", err) + // The genesis anchor is the transaction that spends the genesis + // outpoint: the mint. Transfer proofs carry the same genesis and a + // later anchor transaction. Writing that later transaction here + // retargets the mint anchor, and supply pre-commit resolution then + // builds commitment inputs against the transfer txid. + // + // Re-upserting the issuance proof heals a pointer that a transfer + // previously overwrote, because only the mint spends the genesis + // outpoint. + if anchorSpendsGenesis(&genesisProof.AnchorTx, assetGen.FirstPrevOut) { + genesisPoint, err := encodeOutpoint(assetGen.FirstPrevOut) + if err != nil { + return 0, fmt.Errorf("unable to encode genesis "+ + "point: %w", err) + } + err = db.AnchorGenesisPoint(ctx, GenesisPointAnchor{ + PrevOut: genesisPoint, + AnchorTxID: sqlInt64(chainTXID), + }) + if err != nil { + return 0, fmt.Errorf("unable to anchor genesis "+ + "tx: %w", err) + } } // TODO(roasbeef): need to mark that this is a imported gen? @@ -652,6 +664,22 @@ func upsertAssetGen(ctx context.Context, db UpsertAssetStore, return genAssetID, nil } +// anchorSpendsGenesis reports whether tx spends the genesis outpoint. The +// minting transaction is the spend of that outpoint. +func anchorSpendsGenesis(tx *wire.MsgTx, genesis wire.OutPoint) bool { + if tx == nil { + return false + } + + for _, txIn := range tx.TxIn { + if txIn.PreviousOutPoint == genesis { + return true + } + } + + return false +} + // shouldInsertPreCommit determines whether a supply pre-commitment // output should be inserted for a given issuance proof. func shouldInsertPreCommit(proofType universe.ProofType, diff --git a/tapdb/universe_stats.go b/tapdb/universe_stats.go index 4ef53473ed..02bc7eedbd 100644 --- a/tapdb/universe_stats.go +++ b/tapdb/universe_stats.go @@ -893,14 +893,23 @@ func (u *UniverseStats) QuerySyncStats(ctx context.Context, err) } - hash, err := chainhash.NewHash(assetStat.AnchorTxid[:]) - if err != nil { - return err - } + // genesis_points.anchor_tx_id is nullable. It stays + // unset until a proof whose anchor transaction + // spends the genesis outpoint is stored. A stats + // query still succeeds for that asset; the anchor + // outpoint stays empty until the mint is known. + if len(assetStat.AnchorTxid) > 0 { + hash, err := chainhash.NewHash( + assetStat.AnchorTxid, + ) + if err != nil { + return err + } - stats.AnchorPoint = wire.OutPoint{ - Hash: *hash, - Index: uint32(assetStat.AnchorIndex), + stats.AnchorPoint = wire.OutPoint{ + Hash: *hash, + Index: uint32(assetStat.AnchorIndex), + } } resp.SyncStats = append(resp.SyncStats, stats) diff --git a/universe/supplycommit/hydrate_test.go b/universe/supplycommit/hydrate_test.go new file mode 100644 index 0000000000..0a31effca3 --- /dev/null +++ b/universe/supplycommit/hydrate_test.go @@ -0,0 +1,36 @@ +package supplycommit + +import ( + "testing" + + lfn "github.com/lightningnetwork/lnd/fn/v2" + "github.com/stretchr/testify/require" +) + +// TestHydrateInitialState checks that a restarted UpdatesPendingState +// picks up the transition's pending updates, and that a broadcast state +// is left alone. +func TestHydrateInitialState(t *testing.T) { + t.Parallel() + + event := &NewMintEvent{} + pending := &UpdatesPendingState{} + hydrateInitialState(pending, lfn.Some(SupplyStateTransition{ + PendingUpdates: []SupplyUpdateEvent{event}, + })) + require.Equal(t, []SupplyUpdateEvent{event}, pending.pendingUpdates) + + kept := &NewMintEvent{} + already := &UpdatesPendingState{ + pendingUpdates: []SupplyUpdateEvent{kept}, + } + hydrateInitialState(already, lfn.Some(SupplyStateTransition{ + PendingUpdates: []SupplyUpdateEvent{event}, + })) + require.Equal(t, []SupplyUpdateEvent{kept}, already.pendingUpdates) + + broadcast := &CommitBroadcastState{} + hydrateInitialState(broadcast, lfn.Some(SupplyStateTransition{ + PendingUpdates: []SupplyUpdateEvent{event}, + })) +} diff --git a/universe/supplycommit/manager.go b/universe/supplycommit/manager.go index eb944233eb..127c87f73f 100644 --- a/universe/supplycommit/manager.go +++ b/universe/supplycommit/manager.go @@ -16,6 +16,7 @@ import ( "github.com/lightninglabs/taproot-assets/mssmt" "github.com/lightninglabs/taproot-assets/tapnode" "github.com/lightninglabs/taproot-assets/universe" + lfn "github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/msgmux" "github.com/lightningnetwork/lnd/protofsm" ) @@ -155,6 +156,27 @@ func (m *Manager) Stop() error { return nil } +// hydrateInitialState copies a persisted transition's pending updates onto +// an UpdatesPendingState that was loaded without them. Other states are +// left unchanged, including a CommitBroadcastState whose signed +// transaction is restored separately. +func hydrateInitialState(state State, + transition lfn.Option[SupplyStateTransition]) { + + pending, ok := state.(*UpdatesPendingState) + if !ok { + return + } + + transition.WhenSome(func(tr SupplyStateTransition) { + if len(pending.pendingUpdates) == 0 && + len(tr.PendingUpdates) > 0 { + + pending.pendingUpdates = tr.PendingUpdates + } + }) +} + // startAssetSM creates and starts a new supply commitment state // machine for the given asset specifier. func (m *Manager) startAssetSM(ctx context.Context, @@ -197,6 +219,12 @@ func (m *Manager) startAssetSM(ctx context.Context, } }) + // intToState does not carry the pending update log. After a + // never-confirmed commitment is abandoned, the machine is in + // UpdatesPendingState and the next tick must rebuild from those + // events. A broadcast state is left as the block above restored it. + hydrateInitialState(initialState, initialTransition) + // A restored broadcast state must hold its anchoring before the // machine resumes and rests on it. A record persisted before the // watcher existed has none; adopt it now. The state it watches From f5c7da10a4398ae8207403336ae06ce5c2bd7f79 Mon Sep 17 00:00:00 2001 From: Sadiq Jaffer Date: Mon, 12 Oct 2026 09:38:52 +0100 Subject: [PATCH 2/5] supplycommit: hydrate pending updates before the restart tick 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 --- universe/supplycommit/anchoring_transitions_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/universe/supplycommit/anchoring_transitions_test.go b/universe/supplycommit/anchoring_transitions_test.go index ce071ee856..61862e2ddf 100644 --- a/universe/supplycommit/anchoring_transitions_test.go +++ b/universe/supplycommit/anchoring_transitions_test.go @@ -791,8 +791,10 @@ func TestSupplyCommitRestart(t *testing.T) { }) // A machine restored with a parked batch resumes the interrupted - // cycle: the restart tick re-derives the batch from the record and - // runs it through to a fresh, registered broadcast. + // cycle. startAssetSM copies the transition's pending updates + // onto UpdatesPendingState, so the restart tick commits that + // in-memory batch instead of fetching the record again, and runs + // it through to a fresh, registered broadcast. t.Run("anchoring_updates_pending_starts_cycle", func(t *testing.T) { registrar := &mockAnchoringRegistrar{} h, manager := newManagerHarness(t, &harnessCfg{ @@ -801,7 +803,6 @@ func TestSupplyCommitRestart(t *testing.T) { }) defer stopManager(h, manager) - h.expectFetchState(&UpdatesPendingState{}, pendingBatch) h.expectFetchState(&UpdatesPendingState{}, pendingBatch) h.expectFreezePendingTransition() h.expectAnchoringCommitCycle(registrar) From 76cfb0d8424eca39bc05344abf445dba61b59274 Mon Sep 17 00:00:00 2001 From: Sadiq Jaffer Date: Mon, 12 Oct 2026 09:38:53 +0100 Subject: [PATCH 3/5] itest: cover a send before the first supply commitment 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 --- itest/supply_commit_test.go | 241 ++++++++++++++++++++++++++++++++++++ itest/test_list_on_test.go | 4 + 2 files changed, 245 insertions(+) diff --git a/itest/supply_commit_test.go b/itest/supply_commit_test.go index 1c7c1c2258..4e1538f56c 100644 --- a/itest/supply_commit_test.go +++ b/itest/supply_commit_test.go @@ -1863,3 +1863,244 @@ func testSupplyVerifyPeerNode(t *harnessTest) { // primary's. assertFetchCommitResponse(t, thirdSupplyCommitResp, peerFetchResp3) } + +// testSupplyCommitSendBeforeCommit mints a grouped asset, sends some of +// it, and only then publishes the first supply commitment. A second +// tranche is minted and transferred before the next commitment. Both +// commitments must spend the mint pre-commit output, not the transfer. +func testSupplyCommitSendBeforeCommit(t *harnessTest) { + ctxb := context.Background() + + secondLnd := t.lndHarness.NewNodeWithCoins("SecondLnd", nil) + secondTapd := setupTapdHarness(t.t, t, secondLnd, t.universeServer) + defer func() { + require.NoError(t.t, secondTapd.stop(!*noDelete)) + }() + + mintReq := CopyRequest(issuableAssets[0]) + mintReq.Asset.Amount = 5000 + rpcFirstAsset, delegationKey := MintAssetWithSupplyCommit( + t, mintReq, fn.None[btcec.PublicKey](), + ) + groupKeyBytes := rpcFirstAsset.AssetGroup.TweakedGroupKey + require.NotNil(t.t, groupKeyBytes) + + firstPreCommitIdx := preCommitOutputIndex( + t.t, rpcFirstAsset.ChainAnchor.AnchorTx, delegationKey, + ) + + t.Log("Sending asset before the first supply commitment") + const sendAmt = uint64(100) + firstSend := sendAssetAndAssert( + ctxb, t, t.tapd, secondTapd, sendAmt, + rpcFirstAsset.Amount-sendAmt, rpcFirstAsset.AssetGenesis, + rpcFirstAsset, 0, 1, 1, + ) + importIssuerTransferProof( + t, secondTapd, rpcFirstAsset, groupKeyBytes, + firstSend.RpcResp, + ) + + require.Empty(t.t, t.lndHarness.Miner().GetRawMempool()) + + t.Log("Publishing the first supply commitment after the send") + UpdateAndMineSupplyCommit( + t.t, ctxb, t.tapd, t.lndHarness.Miner(), groupKeyBytes, 1, + ) + fetchResp, supplyOutpoint := WaitForSupplyCommit( + t.t, ctxb, t.tapd, groupKeyBytes, fn.None[wire.OutPoint](), + func(resp *unirpc.FetchSupplyCommitResponse) bool { + return resp.ChainData.BlockHeight > 0 && + len(resp.ChainData.Txn) > 0 + }, + ) + assertCommitSpendsMintPreCommit( + t.t, fetchResp.ChainData.Txn, + rpcFirstAsset.ChainAnchor.AnchorTx, firstPreCommitIdx, + nil, + ) + + t.Log("Minting a second tranche, then sending before the next " + + "commit") + secondMintReq := &mintrpc.MintAssetRequest{ + Asset: &mintrpc.MintAsset{ + AssetType: taprpc.AssetType_NORMAL, + Name: "itestbuxx-supply-commit-send-tranche-2", + AssetMeta: &taprpc.AssetMeta{ + Data: []byte("second tranche metadata"), + }, + Amount: 3000, + AssetVersion: taprpc.AssetVersion_ASSET_VERSION_V1, //nolint:lll + NewGroupedAsset: false, + GroupedAsset: true, + GroupKey: groupKeyBytes, + EnableSupplyCommitments: true, + }, + } + rpcSecondAsset, _ := MintAssetWithSupplyCommit( + t, secondMintReq, fn.Some(delegationKey), + ) + secondPreCommitIdx := preCommitOutputIndex( + t.t, rpcSecondAsset.ChainAnchor.AnchorTx, delegationKey, + ) + + const secondSendAmt = uint64(50) + secondSend := sendAssetAndAssert( + ctxb, t, t.tapd, secondTapd, secondSendAmt, + rpcSecondAsset.Amount-secondSendAmt, + rpcSecondAsset.AssetGenesis, rpcSecondAsset, 1, 2, 2, + ) + importIssuerTransferProof( + t, secondTapd, rpcSecondAsset, groupKeyBytes, + secondSend.RpcResp, + ) + + require.Empty(t.t, t.lndHarness.Miner().GetRawMempool()) + UpdateAndMineSupplyCommit( + t.t, ctxb, t.tapd, t.lndHarness.Miner(), groupKeyBytes, 1, + ) + fetchResp, _ = WaitForSupplyCommit( + t.t, ctxb, t.tapd, groupKeyBytes, fn.Some(supplyOutpoint), + func(resp *unirpc.FetchSupplyCommitResponse) bool { + return resp.ChainData.BlockHeight > 0 && + len(resp.ChainData.Txn) > 0 && + resp.ChainData.CommitOutpoint != + supplyOutpoint.String() + }, + ) + assertCommitSpendsMintPreCommit( + t.t, fetchResp.ChainData.Txn, + rpcSecondAsset.ChainAnchor.AnchorTx, secondPreCommitIdx, + &supplyOutpoint, + ) +} + +// preCommitOutputIndex returns the output index of the supply +// pre-commitment in a mint anchor transaction. +func preCommitOutputIndex(t *testing.T, anchorTx []byte, + delegationKey btcec.PublicKey) uint32 { + + t.Helper() + + var msgTx wire.MsgTx + err := msgTx.Deserialize(bytes.NewReader(anchorTx)) + require.NoError(t, err) + + expected, err := supplycommit.PreCommitTxOut(delegationKey) + require.NoError(t, err) + + var found []uint32 + for idx := range msgTx.TxOut { + txOut := msgTx.TxOut[idx] + if txOut.Value != expected.Value { + continue + } + if !bytes.Equal(txOut.PkScript, expected.PkScript) { + continue + } + found = append(found, uint32(idx)) + } + require.Len(t, found, 1) + + return found[0] +} + +// importIssuerTransferProof waits until the transfer proof is on the +// proof-courier universe, syncs it into the issuer, and inserts it +// directly if that sync did not land the leaf. The issuer must be able +// to query the transfer leaf before the supply commitment is built. +func importIssuerTransferProof(t *harnessTest, recipient *tapdHarness, + rpcAsset *taprpc.Asset, groupKey []byte, + sendResp *taprpc.SendAssetResponse) { + + out := sendResp.Transfer.Outputs[0] + if out.ScriptKeyIsLocal { + out = sendResp.Transfer.Outputs[1] + } + + key := &unirpc.UniverseKey{ + Id: &unirpc.ID{ + Id: &unirpc.ID_GroupKey{ + GroupKey: groupKey, + }, + ProofType: unirpc.ProofType_PROOF_TYPE_TRANSFER, + }, + LeafKey: &unirpc.AssetKey{ + Outpoint: &unirpc.AssetKey_OpStr{ + OpStr: out.Anchor.Outpoint, + }, + ScriptKey: &unirpc.AssetKey_ScriptKeyBytes{ + ScriptKeyBytes: out.ScriptKey, + }, + }, + } + + ctxb := context.Background() + require.Eventually(t.t, func() bool { + resp, err := t.universeServer.service.QueryProof(ctxb, key) + return err == nil && resp != nil + }, defaultWaitTimeout, 200*time.Millisecond) + + _, err := t.tapd.SyncUniverse(ctxb, &unirpc.SyncRequest{ + UniverseHost: t.universeServer.service.rpcHost(), + SyncMode: unirpc.UniverseSyncMode_SYNC_FULL, + }) + if err != nil { + t.Logf("Full universe sync did not complete: %v", err) + } + + _, err = t.tapd.QueryProof(ctxb, key) + if err != nil { + transferProofNormalExportUniInsert( + t, recipient, t.tapd, out.ScriptKey, + rpcAsset.AssetGenesis, + ) + } + + require.Eventually(t.t, func() bool { + resp, err := t.tapd.QueryProof(ctxb, key) + return err == nil && resp != nil + }, defaultWaitTimeout, 200*time.Millisecond) +} + +// assertCommitSpendsMintPreCommit checks that the commitment +// transaction spends mintTx at preCommitIdx. When prevCommit is set, +// that outpoint must be spent as well. The first commitment's input 0 +// is the pre-commit. +func assertCommitSpendsMintPreCommit(t *testing.T, rawCommit, + mintTx []byte, preCommitIdx uint32, prevCommit *wire.OutPoint) { + + t.Helper() + + var commit wire.MsgTx + err := commit.Deserialize(bytes.NewReader(rawCommit)) + require.NoError(t, err) + + var mint wire.MsgTx + err = mint.Deserialize(bytes.NewReader(mintTx)) + require.NoError(t, err) + + want := wire.OutPoint{ + Hash: mint.TxHash(), + Index: preCommitIdx, + } + foundPre := false + foundPrev := prevCommit == nil + for _, in := range commit.TxIn { + if in.PreviousOutPoint == want { + foundPre = true + } + if prevCommit != nil && in.PreviousOutPoint == *prevCommit { + foundPrev = true + } + } + require.True(t, foundPre, "commitment does not spend mint "+ + "pre-commit %v", want) + require.True(t, foundPrev, "commitment does not spend prior "+ + "commitment") + + if prevCommit == nil { + require.NotEmpty(t, commit.TxIn) + require.Equal(t, want, commit.TxIn[0].PreviousOutPoint) + } +} diff --git a/itest/test_list_on_test.go b/itest/test_list_on_test.go index 7e99343368..f1a8caf24c 100644 --- a/itest/test_list_on_test.go +++ b/itest/test_list_on_test.go @@ -488,6 +488,10 @@ var allTestCases = []*testCase{ name: "supply commit mint burn", test: testSupplyCommitMintBurn, }, + { + name: "supply commit send before commit", + test: testSupplyCommitSendBeforeCommit, + }, { name: "supply verify peer node", test: testSupplyVerifyPeerNode, From bd9f47f58c04fe3a100cdb01f45cacebee05c281 Mon Sep 17 00:00:00 2001 From: Sadiq Jaffer Date: Mon, 12 Oct 2026 09:38:53 +0100 Subject: [PATCH 4/5] docs: add release note Signed-off-by: Sadiq Jaffer --- docs/release-notes/release-notes-0.9.0.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/release-notes/release-notes-0.9.0.md b/docs/release-notes/release-notes-0.9.0.md index e77bfd8daf..9f61f3635f 100644 --- a/docs/release-notes/release-notes-0.9.0.md +++ b/docs/release-notes/release-notes-0.9.0.md @@ -21,6 +21,14 @@ # Bug Fixes +- [A transfer proof no longer moves the genesis + anchor](https://github.com/lightninglabs/taproot-assets/pull/2345) off the + mint transaction. Previously, sending a grouped asset before its first supply + commitment made that commitment spend the transfer output instead of the mint + supply pre-commit, so the backend rejected it and later supply updates + stalled. A database migration repairs affected genesis anchors, and pending + supply updates are rebuilt into a valid commitment on restart. + - [Forward-port the v0.8.5 proof upgrade](https://github.com/lightninglabs/taproot-assets/pull/2333) to the btcd-v2 development line: spender commitments, split-root evidence, asset identity checks, and network activation heights. Released spender From 4d7a9f8a830454e7761c36f4c14c91600d2da177 Mon Sep 17 00:00:00 2001 From: Sadiq Jaffer Date: Thu, 12 Nov 2026 13:58:15 +0000 Subject: [PATCH 5/5] tapdb+itest: fix lint and add itest duration entry 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. --- itest/test_durations_test.go | 1 + tapdb/genesis_anchor_rebuild_test.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/itest/test_durations_test.go b/itest/test_durations_test.go index 49b54c1e51..7235ee7a09 100644 --- a/itest/test_durations_test.go +++ b/itest/test_durations_test.go @@ -138,6 +138,7 @@ var testCaseSeconds = map[string]int{ "pre commit output": 48, "supply commit ignore asset": 76, "supply commit mint burn": 51, + "supply commit send before commit": 57, "supply verify peer node": 87, "fetch supply leaves": 52, "auth mailbox message store and fetch": 47, diff --git a/tapdb/genesis_anchor_rebuild_test.go b/tapdb/genesis_anchor_rebuild_test.go index d7a5cc7386..9a29b1b7fb 100644 --- a/tapdb/genesis_anchor_rebuild_test.go +++ b/tapdb/genesis_anchor_rebuild_test.go @@ -304,7 +304,7 @@ func (w rebuildWatcher) Register(ctx context.Context, ) (tapreorg.AnchoringID, error) { if phase1 != nil { - err := phase1(ctx, rebuildRegTx{q: w.q}, 1) + err := phase1(ctx, rebuildRegTx(w), 1) if err != nil { return 0, err }