Skip to content

Commit 088b2d8

Browse files
committed
tapdb: add unit test for mint anchor uni commitment queries
Add unit tests for the SQL queries `UpsertMintAnchorUniCommitment` and `FetchMintAnchorUniCommitment`.
1 parent 8b624e3 commit 088b2d8

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

tapdb/asset_minting_test.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,144 @@ func TestTapscriptTreeManager(t *testing.T) {
17701770
loadTapscriptTreeChecked(t, ctx, assetStore, tree5, tree5Hash)
17711771
}
17721772

1773+
// storeMintAnchorUniCommitment stores a mint anchor commitment in the DB.
1774+
func storeMintAnchorUniCommitment(t *testing.T, assetStore AssetMintingStore,
1775+
batchID int32, txOutputIndex int32, taprootInternalKey []byte,
1776+
groupKey []byte) {
1777+
1778+
ctx := context.Background()
1779+
1780+
var writeTxOpts AssetStoreTxOptions
1781+
upsertMintAnchorPreCommit := func(q PendingAssetStore) error {
1782+
_, err := q.UpsertMintAnchorUniCommitment(
1783+
ctx, sqlc.UpsertMintAnchorUniCommitmentParams{
1784+
BatchID: batchID,
1785+
TxOutputIndex: txOutputIndex,
1786+
TaprootInternalKey: taprootInternalKey,
1787+
GroupKey: groupKey,
1788+
},
1789+
)
1790+
require.NoError(t, err)
1791+
1792+
return nil
1793+
}
1794+
_ = assetStore.db.ExecTx(ctx, &writeTxOpts, upsertMintAnchorPreCommit)
1795+
}
1796+
1797+
// assertMintAnchorUniCommitment is a helper function that reads a mint anchor
1798+
// commitment from the DB and asserts that it matches the expected values.
1799+
func assertMintAnchorUniCommitment(t *testing.T, assetStore AssetMintingStore,
1800+
batchID int32, txOutputIndex int32, preCommitInternalKeyBytes,
1801+
groupPubKeyBytes []byte) {
1802+
1803+
ctx := context.Background()
1804+
readOpts := NewAssetStoreReadTx()
1805+
1806+
var mintAnchorCommitment *sqlc.MintAnchorUniCommitment
1807+
readMintAnchorCommitment := func(q PendingAssetStore) error {
1808+
res, err := q.FetchMintAnchorUniCommitment(ctx, batchID)
1809+
require.NoError(t, err)
1810+
1811+
mintAnchorCommitment = &res
1812+
return nil
1813+
}
1814+
_ = assetStore.db.ExecTx(ctx, &readOpts, readMintAnchorCommitment)
1815+
1816+
// Ensure the mint anchor commitment matches the one we inserted.
1817+
require.NotNil(t, mintAnchorCommitment)
1818+
require.Equal(t, batchID, mintAnchorCommitment.BatchID)
1819+
require.Equal(t, txOutputIndex, mintAnchorCommitment.TxOutputIndex)
1820+
require.Equal(
1821+
t, preCommitInternalKeyBytes,
1822+
mintAnchorCommitment.TaprootInternalKey,
1823+
)
1824+
require.Equal(t, groupPubKeyBytes, mintAnchorCommitment.GroupKey)
1825+
}
1826+
1827+
// TestUpsertMintAnchorUniCommitment tests the UpsertMintAnchorUniCommitment
1828+
// FetchMintAnchorUniCommitment and SQL queries. In particular, it tests that
1829+
// upsert works correctly.
1830+
func TestUpsertMintAnchorUniCommitment(t *testing.T) {
1831+
t.Parallel()
1832+
1833+
ctx := context.Background()
1834+
assetStore, _, _ := newAssetStore(t)
1835+
1836+
// Create a new batch with one asset group seedling.
1837+
mintingBatch := tapgarden.RandSeedlingMintingBatch(t, 1)
1838+
mintingBatch.UniverseCommitments = true
1839+
1840+
_, _, group := addRandGroupToBatch(
1841+
t, assetStore, ctx, mintingBatch.Seedlings,
1842+
)
1843+
1844+
// Commit batch.
1845+
require.NoError(t, assetStore.CommitMintingBatch(ctx, mintingBatch))
1846+
1847+
// Retrieve the batch ID of the batch we just inserted.
1848+
var batchID int32
1849+
readOpts := NewAssetStoreReadTx()
1850+
_ = assetStore.db.ExecTx(
1851+
ctx, &readOpts, func(q PendingAssetStore) error {
1852+
batches, err := q.AllMintingBatches(ctx)
1853+
require.NoError(t, err)
1854+
require.Len(t, batches, 1)
1855+
1856+
batchID = int32(batches[0].BatchID)
1857+
return nil
1858+
},
1859+
)
1860+
1861+
// Serialize keys into bytes for easier handling.
1862+
preCommitInternalKey := test.RandPubKey(t)
1863+
preCommitInternalKeyBytes := preCommitInternalKey.SerializeCompressed()
1864+
1865+
groupPubKeyBytes := group.GroupPubKey.SerializeCompressed()
1866+
1867+
// Upsert a mint anchor commitment for the batch.
1868+
txOutputIndex := int32(2)
1869+
storeMintAnchorUniCommitment(
1870+
t, *assetStore, batchID, txOutputIndex,
1871+
preCommitInternalKeyBytes, groupPubKeyBytes,
1872+
)
1873+
1874+
// Retrieve and inspect the mint anchor commitment we just inserted.
1875+
assertMintAnchorUniCommitment(
1876+
t, *assetStore, batchID, txOutputIndex,
1877+
preCommitInternalKeyBytes, groupPubKeyBytes,
1878+
)
1879+
1880+
// Upsert-ing a new taproot internal key for the same batch should
1881+
// overwrite the existing one.
1882+
internalKey2 := test.RandPubKey(t)
1883+
internalKey2Bytes := internalKey2.SerializeCompressed()
1884+
1885+
storeMintAnchorUniCommitment(
1886+
t, *assetStore, batchID, txOutputIndex, internalKey2Bytes,
1887+
groupPubKeyBytes,
1888+
)
1889+
1890+
assertMintAnchorUniCommitment(
1891+
t, *assetStore, batchID, txOutputIndex, internalKey2Bytes,
1892+
groupPubKeyBytes,
1893+
)
1894+
1895+
// Upsert-ing a new group key for the same batch should overwrite the
1896+
// existing one.
1897+
groupPubKey2 := test.RandPubKey(t)
1898+
groupPubKey2Bytes := groupPubKey2.SerializeCompressed()
1899+
1900+
storeMintAnchorUniCommitment(
1901+
t, *assetStore, batchID, txOutputIndex, internalKey2Bytes,
1902+
groupPubKey2Bytes,
1903+
)
1904+
1905+
assertMintAnchorUniCommitment(
1906+
t, *assetStore, batchID, txOutputIndex, internalKey2Bytes,
1907+
groupPubKey2Bytes,
1908+
)
1909+
}
1910+
17731911
func init() {
17741912
rand.Seed(time.Now().Unix())
17751913

0 commit comments

Comments
 (0)