55 "context"
66 "crypto/sha256"
77 "database/sql"
8+ "fmt"
89 "math/rand"
910 "os"
1011 "testing"
@@ -1839,7 +1840,7 @@ func TestTapscriptTreeManager(t *testing.T) {
18391840// storeMintSupplyPreCommit stores a mint anchor supply pre-commitment in the
18401841// DB.
18411842func storeMintSupplyPreCommit (t * testing.T , assetStore AssetMintingStore ,
1842- batchKey []byte , txOutputIndex int32 ,
1843+ batchKey []byte , txOutputIndex uint32 ,
18431844 taprootInternalKey keychain.KeyDescriptor , groupKey []byte ,
18441845 outpoint wire.OutPoint ) {
18451846
@@ -1863,7 +1864,7 @@ func storeMintSupplyPreCommit(t *testing.T, assetStore AssetMintingStore,
18631864 _ , err = q .UpsertMintSupplyPreCommit (
18641865 ctx , UpsertBatchPreCommitParams {
18651866 BatchKey : batchKey ,
1866- TxOutputIndex : txOutputIndex ,
1867+ TxOutputIndex : int32 ( txOutputIndex ) ,
18671868 TaprootInternalKeyID : internalKeyID ,
18681869 GroupKey : groupKey ,
18691870 Outpoint : opBytes ,
@@ -1880,7 +1881,7 @@ func storeMintSupplyPreCommit(t *testing.T, assetStore AssetMintingStore,
18801881// supply pre-commitment from the DB and asserts that it matches the expected
18811882// values.
18821883func assertMintSupplyPreCommit (t * testing.T , assetStore AssetMintingStore ,
1883- batchKey []byte , txOutputIndex int32 ,
1884+ batchKey []byte , txOutputIndex uint32 ,
18841885 preCommitInternalKey keychain.KeyDescriptor , groupPubKeyBytes []byte ,
18851886 outpoint wire.OutPoint ) {
18861887
@@ -1906,7 +1907,7 @@ func assertMintSupplyPreCommit(t *testing.T, assetStore AssetMintingStore,
19061907 // Ensure the mint anchor commitment matches the one we inserted.
19071908 require .NotNil (t , preCommit )
19081909 require .Equal (t , batchKey , preCommit .BatchKey )
1909- require .Equal (t , txOutputIndex , preCommit .TxOutputIndex )
1910+ require .EqualValues (t , txOutputIndex , preCommit .TxOutputIndex )
19101911
19111912 rawInternalKey := preCommitInternalKey .PubKey .SerializeCompressed ()
19121913 require .Equal (
@@ -1927,6 +1928,39 @@ func assertMintSupplyPreCommit(t *testing.T, assetStore AssetMintingStore,
19271928 require .Equal (t , opBytes , preCommit .Outpoint )
19281929}
19291930
1931+ // storeSeedlingGroupGenesis stores the group genesis and an initial asset
1932+ // associated with the given seedling in the DB. This is necessary before we can
1933+ // commit a minting batch that contains the seedling.
1934+ func storeSeedlingGroupGenesis (t * testing.T , ctx context.Context ,
1935+ assetStore * AssetMintingStore , seedling tapgarden.Seedling ) {
1936+
1937+ genesis := seedling .GroupInfo .Genesis
1938+ groupKey := seedling .GroupInfo .GroupKey
1939+ initialAsset := asset .RandAssetWithValues (
1940+ t , * genesis , groupKey , asset .RandScriptKey (t ),
1941+ )
1942+
1943+ upsertAsset := func (q PendingAssetStore ) error {
1944+ _ , err := maybeUpsertAssetMeta (ctx , q , genesis , nil )
1945+ require .NoError (t , err )
1946+
1947+ // Insert a random managed UTXO.
1948+ utxoID := addRandomManagedUTXO (t , ctx , q , initialAsset )
1949+
1950+ _ , _ , err = upsertAssetsWithGenesis (
1951+ ctx , q , genesis .FirstPrevOut ,
1952+ []* asset.Asset {initialAsset },
1953+ []sql.NullInt64 {sqlInt64 (utxoID )},
1954+ )
1955+ require .NoError (t , err )
1956+ return nil
1957+ }
1958+
1959+ var writeTxOpts AssetStoreTxOptions
1960+ err := assetStore .db .ExecTx (ctx , & writeTxOpts , upsertAsset )
1961+ require .NoError (t , err )
1962+ }
1963+
19301964// TestUpsertMintSupplyPreCommit tests the UpsertMintSupplyPreCommit and
19311965// FetchSupplyPreCommits SQL queries. In particular, it tests that upsert works
19321966// correctly.
@@ -1937,13 +1971,21 @@ func TestUpsertMintSupplyPreCommit(t *testing.T) {
19371971 assetStore , _ , _ := newAssetStore (t )
19381972
19391973 // Create a new batch with one asset group seedling.
1940- mintingBatch := tapgarden .RandSeedlingMintingBatch (t , 1 )
1941- mintingBatch .SupplyCommitments = true
1942-
1943- _ , _ , group := addRandGroupToBatch (
1944- t , assetStore , ctx , mintingBatch .Seedlings ,
1974+ mintingBatch := tapgarden .RandMintingBatch (
1975+ t , tapgarden .WithTotalGroups ([]int {1 }),
1976+ tapgarden .WithUniverseCommitments (true ),
19451977 )
19461978
1979+ // Store group genesis associated with seedling. This is necessary
1980+ // before we can commit the batch.
1981+ require .Len (t , mintingBatch .Seedlings , 1 )
1982+ var seedling tapgarden.Seedling
1983+ for _ , s := range mintingBatch .Seedlings {
1984+ seedling = * s
1985+ break
1986+ }
1987+ storeSeedlingGroupGenesis (t , ctx , assetStore , seedling )
1988+
19471989 // Commit batch.
19481990 require .NoError (t , assetStore .CommitMintingBatch (ctx , mintingBatch ))
19491991
@@ -1962,45 +2004,47 @@ func TestUpsertMintSupplyPreCommit(t *testing.T) {
19622004 )
19632005
19642006 // Define pre-commit outpoint for the batch mint anchor tx.
1965- txOutputIndex := int32 (2 )
1966- txidStr := mintingBatch .GenesisPacket .FundedPsbt .Pkt .UnsignedTx .TxID ()
2007+ genesisPkt := mintingBatch .GenesisPacket
2008+ require .NotNil (t , genesisPkt )
2009+
2010+ preCommitOut , err := genesisPkt .PreCommitmentOutput .UnwrapOrErr (
2011+ fmt .Errorf ("no pre-commitment output" ),
2012+ )
2013+ require .NoError (t , err )
19672014
2015+ txidStr := genesisPkt .FundedPsbt .Pkt .UnsignedTx .TxID ()
19682016 txid , err := chainhash .NewHashFromStr (txidStr )
19692017 require .NoError (t , err )
19702018
19712019 preCommitOutpoint := wire.OutPoint {
19722020 Hash : * txid ,
1973- Index : uint32 ( txOutputIndex ) ,
2021+ Index : preCommitOut . OutIdx ,
19742022 }
19752023
19762024 // Serialize keys into bytes for easier handling.
1977- preCommitInternalKey , _ := test .RandKeyDesc (t )
1978-
1979- groupPubKeyBytes := schnorr .SerializePubKey (& group .GroupPubKey )
1980-
1981- // Upsert a mint anchor commitment for the batch.
1982- storeMintSupplyPreCommit (
1983- t , * assetStore , batchKey , txOutputIndex ,
1984- preCommitInternalKey , groupPubKeyBytes , preCommitOutpoint ,
2025+ preCommitGroupKey , err := preCommitOut .GroupPubKey .UnwrapOrErr (
2026+ fmt .Errorf ("no group key" ),
19852027 )
2028+ require .NoError (t , err )
2029+ groupPubKeyBytes := schnorr .SerializePubKey (& preCommitGroupKey )
19862030
19872031 // Retrieve and inspect the mint anchor commitment we just inserted.
19882032 assertMintSupplyPreCommit (
1989- t , * assetStore , batchKey , txOutputIndex ,
1990- preCommitInternalKey , groupPubKeyBytes , preCommitOutpoint ,
2033+ t , * assetStore , batchKey , preCommitOut . OutIdx ,
2034+ preCommitOut . InternalKey , groupPubKeyBytes , preCommitOutpoint ,
19912035 )
19922036
19932037 // Upsert-ing a new taproot internal key for the same pre-commit
19942038 // outpoint should overwrite the existing one.
19952039 internalKey2 , _ := test .RandKeyDesc (t )
19962040
19972041 storeMintSupplyPreCommit (
1998- t , * assetStore , batchKey , txOutputIndex , internalKey2 ,
2042+ t , * assetStore , batchKey , preCommitOut . OutIdx , internalKey2 ,
19992043 groupPubKeyBytes , preCommitOutpoint ,
20002044 )
20012045
20022046 assertMintSupplyPreCommit (
2003- t , * assetStore , batchKey , txOutputIndex , internalKey2 ,
2047+ t , * assetStore , batchKey , preCommitOut . OutIdx , internalKey2 ,
20042048 groupPubKeyBytes , preCommitOutpoint ,
20052049 )
20062050
@@ -2010,12 +2054,12 @@ func TestUpsertMintSupplyPreCommit(t *testing.T) {
20102054 groupPubKey2Bytes := schnorr .SerializePubKey (groupPubKey2 )
20112055
20122056 storeMintSupplyPreCommit (
2013- t , * assetStore , batchKey , txOutputIndex , internalKey2 ,
2057+ t , * assetStore , batchKey , preCommitOut . OutIdx , internalKey2 ,
20142058 groupPubKey2Bytes , preCommitOutpoint ,
20152059 )
20162060
20172061 assertMintSupplyPreCommit (
2018- t , * assetStore , batchKey , txOutputIndex , internalKey2 ,
2062+ t , * assetStore , batchKey , preCommitOut . OutIdx , internalKey2 ,
20192063 groupPubKey2Bytes , preCommitOutpoint ,
20202064 )
20212065}
0 commit comments