From 187fe04302dbd39ccf9259a207c65632d5a4b308 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 10 Oct 2026 10:59:16 +0000 Subject: [PATCH 1/4] tapdb: keep the genesis anchor on the mint transaction Transfer proofs share a genesis with the mint but carry a later anchor transaction. upsertAssetGen wrote that transaction into genesis_points.anchor_tx_id for every proof type, so supply pre-commit resolution built commitment inputs against the transfer txid. Only anchor the genesis when the proof transaction spends the genesis outpoint. Migration 61 restores anchors from the chain transaction that spends each genesis outpoint, and abandons never-confirmed supply commitments that spend neither a real pre-commit nor a confirmed commitment outpoint so a restarted node can rebuild them. Signed-off-by: Cursor Agent Co-authored-by: Sadiq Jaffer --- itest/supply_commit_test.go | 239 ++++++++++ itest/test_list_on_test.go | 4 + tapdb/genesis_anchor.go | 335 +++++++++++++ tapdb/genesis_anchor_test.go | 442 ++++++++++++++++++ tapdb/migrations.go | 2 +- tapdb/programmatic_migrations.go | 11 +- tapdb/sqlc/assets.sql.go | 34 ++ .../000061_repair_genesis_anchor.up.sql | 5 + tapdb/sqlc/querier.go | 12 + tapdb/sqlc/queries/assets.sql | 4 + tapdb/sqlc/queries/supply_commit.sql | 89 ++++ tapdb/sqlc/supply_commit.sql.go | 247 ++++++++++ tapdb/universe.go | 50 +- universe/supplycommit/hydrate_test.go | 36 ++ universe/supplycommit/manager.go | 32 +- 15 files changed, 1527 insertions(+), 15 deletions(-) create mode 100644 tapdb/genesis_anchor.go create mode 100644 tapdb/genesis_anchor_test.go create mode 100644 tapdb/sqlc/migrations/000061_repair_genesis_anchor.up.sql create mode 100644 universe/supplycommit/hydrate_test.go diff --git a/itest/supply_commit_test.go b/itest/supply_commit_test.go index 252cf1935f..0bb85260a7 100644 --- a/itest/supply_commit_test.go +++ b/itest/supply_commit_test.go @@ -1864,3 +1864,242 @@ 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, 0, 1, 1, + ) + 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 := tapgarden.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 070dcd8e92..6c3c8ae55b 100644 --- a/itest/test_list_on_test.go +++ b/itest/test_list_on_test.go @@ -431,6 +431,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, diff --git a/tapdb/genesis_anchor.go b/tapdb/genesis_anchor.go new file mode 100644 index 0000000000..4ec91dd0b6 --- /dev/null +++ b/tapdb/genesis_anchor.go @@ -0,0 +1,335 @@ +package tapdb + +import ( + "bytes" + "context" + "database/sql" + "fmt" + + "github.com/btcsuite/btcd/chaincfg/chainhash" + "github.com/btcsuite/btcd/wire" + "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 +} + +// repairGenesisAnchorMigration is migration 61. 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. More than one spending +// transaction is an error, because the migration will not guess which +// one is the mint. 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, + }) + } + + 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 + + case 1: + spenderID := spenders[0].id + 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 + + default: + return fmt.Errorf("genesis %d (%v) is spent by %d "+ + "chain transactions", point.GenesisID, genesis, + len(spenders)) + } +} + +// 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, + ClearCommitmentID: 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_test.go b/tapdb/genesis_anchor_test.go new file mode 100644 index 0000000000..9a5a1d6af4 --- /dev/null +++ b/tapdb/genesis_anchor_test.go @@ -0,0 +1,442 @@ +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" + "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 *SqliteStore, + 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 *SqliteStore, 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) +} + +// upsertChainTx inserts a transaction and returns its chain_txns id. +func upsertChainTx(t *testing.T, db *SqliteStore, 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 *SqliteStore, 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 refuses to guess among several spenders. +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("multiple spenders error", 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) + } + + err = repairGenesisAnchors(ctx, db) + require.Error(t, err) + require.Equal(t, bogusID, genesisAnchorID( + t, db, genesis.FirstPrevOut, + )) + }) +} + +// signedCommitTx inserts a signed commitment that spends prevOut. +func signedCommitTx(t *testing.T, h *supplyCommitTestHarness, + prevOut wire.OutPoint) { + + 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) +} + +// 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) + }) +} diff --git a/tapdb/migrations.go b/tapdb/migrations.go index bb75bdabf2..d9770b43a3 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 = 60 + LatestMigrationVersion = 61 ) // DatabaseBackend is an interface that contains all methods our different diff --git a/tapdb/programmatic_migrations.go b/tapdb/programmatic_migrations.go index c47e32cd0d..0d95d3d7c3 100644 --- a/tapdb/programmatic_migrations.go +++ b/tapdb/programmatic_migrations.go @@ -27,6 +27,12 @@ const ( // table by querying all assets and detecting burns from their // witnesses. Migration51InsertAssetBurns = 51 + + // Migration61RepairGenesisAnchor is the version of the programmatic + // migration that points each genesis anchor at the mint transaction + // and abandons never-confirmed supply commitments that do not spend + // a real pre-commit or confirmed commitment outpoint. + Migration61RepairGenesisAnchor = 61 ) // programmaticMigration is a function type for a function that performs a @@ -39,8 +45,9 @@ var ( // These functions are used to perform additional checks on the // database state that are not fully expressible in SQL. programmaticMigrations = map[uint]programmaticMigration{ - Migration50ScriptKeyType: determineAndAssignScriptKeyType, - Migration51InsertAssetBurns: insertAssetBurns, + Migration50ScriptKeyType: determineAndAssignScriptKeyType, + Migration51InsertAssetBurns: insertAssetBurns, + Migration61RepairGenesisAnchor: repairGenesisAnchorMigration, } ) diff --git a/tapdb/sqlc/assets.sql.go b/tapdb/sqlc/assets.sql.go index c8bc3285c3..cd204a7eda 100644 --- a/tapdb/sqlc/assets.sql.go +++ b/tapdb/sqlc/assets.sql.go @@ -3730,3 +3730,37 @@ func (q *Queries) UpsertTapscriptTreeRootHash(ctx context.Context, arg UpsertTap err := row.Scan(&root_id) return root_id, err } + +const FetchAllChainTxns = `-- name: FetchAllChainTxns :many +SELECT txn_id, txid, raw_tx +FROM chain_txns +` + +type FetchAllChainTxnsRow struct { + TxnID int64 + Txid []byte + RawTx []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); 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 +} diff --git a/tapdb/sqlc/migrations/000061_repair_genesis_anchor.up.sql b/tapdb/sqlc/migrations/000061_repair_genesis_anchor.up.sql new file mode 100644 index 0000000000..9bde3f5d3b --- /dev/null +++ b/tapdb/sqlc/migrations/000061_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 c17ef127fd..0c4b172af8 100644 --- a/tapdb/sqlc/querier.go +++ b/tapdb/sqlc/querier.go @@ -23,6 +23,9 @@ type Querier interface { AssetsInBatch(ctx context.Context, rawKey []byte) ([]AssetsInBatchRow, error) BindMintingBatchWithTapSibling(ctx context.Context, arg BindMintingBatchWithTapSiblingParams) error BindMintingBatchWithTx(ctx context.Context, arg BindMintingBatchWithTxParams) (int64, error) + ClearPendingSupplyCommitTransition(ctx context.Context, transitionID int64) error + ClearSpentCommitmentRef(ctx context.Context, spentCommitment sql.NullInt64) error + ClearSupplyCommitmentRefs(ctx context.Context, commitID sql.NullInt64) error ConfirmChainAnchorTx(ctx context.Context, arg ConfirmChainAnchorTxParams) error ConfirmChainTx(ctx context.Context, arg ConfirmChainTxParams) error CountAuthMailboxMessages(ctx context.Context) (int64, error) @@ -39,6 +42,7 @@ type Querier interface { DeleteMultiverseRoot(ctx context.Context, namespaceRoot string) error DeleteNode(ctx context.Context, arg DeleteNodeParams) (int64, error) DeleteRoot(ctx context.Context, namespace string) (int64, error) + DeleteSupplyCommitment(ctx context.Context, commitID int64) error DeleteSupplyCommitTransition(ctx context.Context, transitionID int64) error DeleteSupplyUpdateEvents(ctx context.Context, transitionID sql.NullInt64) error DeleteTapscriptTreeEdges(ctx context.Context, rootHash []byte) error @@ -61,6 +65,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) @@ -93,6 +98,7 @@ type Querier interface { FetchAuthMailboxMessageByOutpoint(ctx context.Context, claimedOutpoint []byte) (FetchAuthMailboxMessageByOutpointRow, error) FetchAuxCloseInfo(ctx context.Context, chanPoint []byte) ([]byte, error) FetchChainTx(ctx context.Context, txid []byte) (ChainTxn, error) + FetchConfirmedSupplyCommitOutpoints(ctx context.Context) ([]FetchConfirmedSupplyCommitOutpointsRow, error) FetchChainTxByID(ctx context.Context, txnID int64) (FetchChainTxByIDRow, error) // Returns the node with the given hash key alongside its direct children, in // no particular order. Each row is a primary key lookup, so the cost is @@ -115,12 +121,14 @@ type Querier interface { FetchManagedUTXOs(ctx context.Context) ([]FetchManagedUTXOsRow, error) // Fetch records from the supply_pre_commits table with optional // filtering. + FetchMintSupplyPreCommitOutpoints(ctx context.Context) ([][]byte, error) FetchMintSupplyPreCommits(ctx context.Context, arg FetchMintSupplyPreCommitsParams) ([]FetchMintSupplyPreCommitsRow, error) FetchMintingBatch(ctx context.Context, rawKey []byte) (FetchMintingBatchRow, error) FetchMintingBatchesByInverseState(ctx context.Context, batchState int16) ([]FetchMintingBatchesByInverseStateRow, error) 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) FetchRootNode(ctx context.Context, namespace string) (MssmtNode, error) FetchScriptKeyByTweakedKey(ctx context.Context, tweakedScriptKey []byte) (FetchScriptKeyByTweakedKeyRow, error) FetchScriptKeyIDByTweakedKey(ctx context.Context, tweakedScriptKey []byte) (int64, error) @@ -142,6 +150,7 @@ type Querier interface { // Fetch unspent supply pre-commitment outputs. Each pre-commitment output // comes from a mint anchor transaction and relates to an asset issuance // where the local node acted as the issuer. + FetchUnconfirmedBroadcastSupplyCommits(ctx context.Context) ([]FetchUnconfirmedBroadcastSupplyCommitsRow, error) FetchUnspentMintSupplyPreCommits(ctx context.Context, groupKey []byte) ([]FetchUnspentMintSupplyPreCommitsRow, error) // Fetch unspent supply pre-commitment outputs. Each pre-commitment output // comes from a mint anchor transaction and relates to an asset issuance @@ -252,6 +261,7 @@ type Querier interface { QueryUniverseStats(ctx context.Context) (QueryUniverseStatsRow, error) QueryUniverseSupplyLeaves(ctx context.Context, arg QueryUniverseSupplyLeavesParams) ([]QueryUniverseSupplyLeavesRow, error) ReAnchorPassiveAssets(ctx context.Context, arg ReAnchorPassiveAssetsParams) error + ResetSupplyCommitMachineForAbandon(ctx context.Context, arg ResetSupplyCommitMachineForAbandonParams) error SetAddrManaged(ctx context.Context, arg SetAddrManagedParams) error SetAssetSpent(ctx context.Context, arg SetAssetSpentParams) (int64, error) SetTransferOutputProofDeliveryStatus(ctx context.Context, arg SetTransferOutputProofDeliveryStatusParams) error @@ -260,6 +270,8 @@ type Querier interface { // conflicting transfer has confirmed on-chain, these transfers' anchor // transactions can never confirm. SupersedeConflictingTransfers(ctx context.Context, arg SupersedeConflictingTransfersParams) (int64, error) + UnmarkMintPreCommitsSpentBy(ctx context.Context, spentBy sql.NullInt64) error + UnmarkRemotePreCommitsSpentBy(ctx context.Context, spentBy sql.NullInt64) error UniverseLeaves(ctx context.Context) ([]UniverseLeafe, error) UniverseRoots(ctx context.Context, arg UniverseRootsParams) ([]UniverseRootsRow, error) UpdateBatchGenesisTx(ctx context.Context, arg UpdateBatchGenesisTxParams) error diff --git a/tapdb/sqlc/queries/assets.sql b/tapdb/sqlc/queries/assets.sql index d791be0fbe..9609e95286 100644 --- a/tapdb/sqlc/queries/assets.sql +++ b/tapdb/sqlc/queries/assets.sql @@ -732,6 +732,10 @@ FROM genesis_assets; SELECT * FROM genesis_points; +-- name: FetchAllChainTxns :many +SELECT txn_id, txid, raw_tx +FROM chain_txns; + -- name: FetchGenesisID :one WITH target_point(genesis_id) AS ( SELECT genesis_id diff --git a/tapdb/sqlc/queries/supply_commit.sql b/tapdb/sqlc/queries/supply_commit.sql index c92d35bd1e..1dc81c402b 100644 --- a/tapdb/sqlc/queries/supply_commit.sql +++ b/tapdb/sqlc/queries/supply_commit.sql @@ -289,3 +289,92 @@ WHERE key_id = @key_id; SELECT raw_tx, block_height -- Include block_height needed by FetchState FROM chain_txns WHERE txn_id = @txn_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: DeleteSupplyCommitment :exec +DELETE FROM supply_commitments +WHERE commit_id = $1; + +-- 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 196f1aab74..831addedc4 100644 --- a/tapdb/sqlc/supply_commit.sql.go +++ b/tapdb/sqlc/supply_commit.sql.go @@ -919,3 +919,250 @@ func (q *Queries) UpsertSupplyCommitStateMachine(ctx context.Context, arg Upsert err := row.Scan(&i.CurrentStateID, &i.LatestCommitmentID) return i, err } + +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 +} + +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 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 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 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 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, commitID sql.NullInt64) error { + _, err := q.db.ExecContext(ctx, ClearSupplyCommitmentRefs, commitID) + return err +} + +const DeleteSupplyCommitment = `-- name: DeleteSupplyCommitment :exec +DELETE FROM supply_commitments +WHERE commit_id = $1 +` + +func (q *Queries) DeleteSupplyCommitment(ctx context.Context, commitID int64) error { + _, err := q.db.ExecContext(ctx, DeleteSupplyCommitment, commitID) + 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 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 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 + ClearCommitmentID sql.NullInt64 + GroupKey []byte +} + +func (q *Queries) ResetSupplyCommitMachineForAbandon(ctx context.Context, arg ResetSupplyCommitMachineForAbandonParams) error { + _, err := q.db.ExecContext( + ctx, ResetSupplyCommitMachineForAbandon, arg.StateName, + arg.ClearCommitmentID, arg.GroupKey, + ) + return err +} diff --git a/tapdb/universe.go b/tapdb/universe.go index 932b27df97..174388720f 100644 --- a/tapdb/universe.go +++ b/tapdb/universe.go @@ -580,17 +580,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? @@ -598,6 +610,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/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 a8ed0a9741..f4883c58f9 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/tapgarden" "github.com/lightninglabs/taproot-assets/universe" + lfn "github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/msgmux" "github.com/lightningnetwork/lnd/protofsm" ) @@ -146,6 +147,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 not rehydrated here. +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, @@ -168,11 +190,19 @@ func (m *Manager) startAssetSM(ctx context.Context, // Before we start the state machine, we'll need to fetch the current // state from disk, to see if we need to emit any new events. - initialState, _, err := m.cfg.StateLog.FetchState(ctx, assetSpec) + initialState, transitionOpt, err := m.cfg.StateLog.FetchState( + ctx, assetSpec, + ) if err != nil { return nil, fmt.Errorf("unable to fetch current state: %w", err) } + // 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. + hydrateInitialState(initialState, transitionOpt) + // Create a new error reporter for the state machine. errorReporter := NewErrorReporter(assetSpec) From 8f77295121b68dd27d7741b50b0d19dae0d26b22 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 10 Oct 2026 11:15:55 +0000 Subject: [PATCH 2/4] tapdb: allow universe stats without a mint anchor QuerySyncStats treated genesis_points.anchor_tx_id as always set. After transfer proofs stop retargeting that column, a genesis point can have no mint transaction yet, and the stats query returned "invalid hash length of 0". Leave the anchor outpoint empty until the mint transaction is known. Signed-off-by: Cursor Agent Co-authored-by: Sadiq Jaffer --- tapdb/universe_stats.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tapdb/universe_stats.go b/tapdb/universe_stats.go index 638620d8fe..a442bacf1b 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) From edf285dec1f4a34fe478e113381e4fc1785efeac Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 10 Oct 2026 11:29:14 +0000 Subject: [PATCH 3/4] itest: count both sends in supply commit send before commit The second transfer is the issuer's second outbound transfer and the recipient's second receive. The assertion asked for a single transfer and timed out. Signed-off-by: Cursor Agent Co-authored-by: Sadiq Jaffer --- itest/supply_commit_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/itest/supply_commit_test.go b/itest/supply_commit_test.go index 0bb85260a7..c1f6596be8 100644 --- a/itest/supply_commit_test.go +++ b/itest/supply_commit_test.go @@ -1948,7 +1948,7 @@ func testSupplyCommitSendBeforeCommit(t *harnessTest) { secondSend := sendAssetAndAssert( ctxb, t, t.tapd, secondTapd, secondSendAmt, rpcSecondAsset.Amount-secondSendAmt, - rpcSecondAsset.AssetGenesis, rpcSecondAsset, 0, 1, 1, + rpcSecondAsset.AssetGenesis, rpcSecondAsset, 1, 2, 2, ) importIssuerTransferProof( t, secondTapd, rpcSecondAsset, groupKeyBytes, From ac101ed0e86b104b094996160e374d546df49153 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 10 Oct 2026 12:02:47 +0000 Subject: [PATCH 4/4] tapdb: prove migration 61 leaves valid commitments A confirmed commitment stays, and so does an unconfirmed commitment that spends a real pre-commit or a confirmed commitment outpoint. Only a commitment whose inputs match neither is abandoned. After that abandon, a restarted supply-commit manager rebuilds from the pending updates and broadcasts a commitment that spends the caller-selected pre-commit output. A custom-genesis mint at output index 1 is covered, including an anchor that a transfer had retargeted. Signed-off-by: Cursor Agent Co-authored-by: Sadiq Jaffer --- tapdb/genesis_anchor_rebuild_test.go | 363 +++++++++++++++++++++++++++ tapdb/genesis_anchor_test.go | 245 +++++++++++++++++- 2 files changed, 605 insertions(+), 3 deletions(-) create mode 100644 tapdb/genesis_anchor_rebuild_test.go diff --git a/tapdb/genesis_anchor_rebuild_test.go b/tapdb/genesis_anchor_rebuild_test.go new file mode 100644 index 0000000000..69763128e7 --- /dev/null +++ b/tapdb/genesis_anchor_rebuild_test.go @@ -0,0 +1,363 @@ +package tapdb + +import ( + "bytes" + "context" + "net/url" + "testing" + "time" + + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/psbt" + "github.com/btcsuite/btcd/btcutil/txsort" + "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/chaincfg/chainhash" + "github.com/btcsuite/btcd/wire" + "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/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) (btcutil.Address, error) { + + return btcutil.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 +} + +// TestMigration61RebuildsValidCommitment 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 TestMigration61RebuildsValidCommitment(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{ + 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 index 9a5a1d6af4..262b325be0 100644 --- a/tapdb/genesis_anchor_test.go +++ b/tapdb/genesis_anchor_test.go @@ -158,7 +158,7 @@ func TestTransferProofKeepsCustomGenesisPreCommit(t *testing.T) { // 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 *SqliteStore, genesis asset.Genesis, +func insertMintPreCommit(t *testing.T, db sqlc.Querier, genesis asset.Genesis, mintTx *wire.MsgTx, outIdx int32, groupKey []byte) { t.Helper() @@ -355,9 +355,10 @@ func TestRepairGenesisAnchor(t *testing.T) { }) } -// signedCommitTx inserts a signed commitment that spends prevOut. +// signedCommitTx inserts a signed commitment that spends prevOut and +// returns that transaction. func signedCommitTx(t *testing.T, h *supplyCommitTestHarness, - prevOut wire.OutPoint) { + prevOut wire.OutPoint) *wire.MsgTx { t.Helper() @@ -375,6 +376,8 @@ func signedCommitTx(t *testing.T, h *supplyCommitTestHarness, }, ) require.NoError(t, err) + + return tx } // TestAbandonInvalidUnconfirmedSupplyCommit drops a broadcast commitment @@ -440,3 +443,239 @@ func TestAbandonInvalidUnconfirmedSupplyCommit(t *testing.T) { 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) +} + +// TestMigration61CommitmentSafety checks the abandon rule against a +// confirmed commitment and against an unconfirmed commitment that spends +// a confirmed commitment outpoint. +func TestMigration61CommitmentSafety(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 commitment", 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()) + }) +}